/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
1
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
2
/*
237.2.24 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Fix name in header.
3
 * Password-prompt - Read a password from the terminal and print it
237.1.2 by Teddy Hogeborn
Further steps towards a D-Bus server interface, plus minor syntax
4
 * 
237.7.527 by Teddy Hogeborn
Update copyright year to 2019
5
 * Copyright © 2008-2019 Teddy Hogeborn
6
 * Copyright © 2008-2019 Björn Påhlsson
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
7
 * 
237.7.455 by Teddy Hogeborn
Alter copyright notices slightly. Actual license is unchanged!
8
 * This file is part of Mandos.
9
 * 
10
 * Mandos is free software: you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 * 
15
 * Mandos is distributed in the hope that it will be useful, but
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * General Public License for more details.
19
 * 
20
 * You should have received a copy of the GNU General Public License
237.7.455 by Teddy Hogeborn
Alter copyright notices slightly. Actual license is unchanged!
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
22
 * 
237.11.2 by Teddy Hogeborn
Change "fukt.bsnet.se" to "recompile.se" throughout.
23
 * Contact the authors at <mandos@recompile.se>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
24
 */
25
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
26
#define _GNU_SOURCE		/* getline(), asprintf() */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
27
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
28
#include <termios.h>		/* struct termios, tcsetattr(),
13 by Björn Påhlsson
Added following support:
29
				   TCSAFLUSH, tcgetattr(), ECHO */
237.7.675 by Teddy Hogeborn
Add dracut(8) support
30
#include <unistd.h>		/* access(), struct termios,
31
				   tcsetattr(), STDIN_FILENO,
32
				   TCSAFLUSH, tcgetattr(), ECHO,
33
				   readlink() */
13 by Björn Påhlsson
Added following support:
34
#include <signal.h>		/* sig_atomic_t, raise(), struct
35
				   sigaction, sigemptyset(),
36
				   sigaction(), sigaddset(), SIGINT,
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
37
				   SIGQUIT, SIGHUP, SIGTERM,
38
				   raise() */
24.1.26 by Björn Påhlsson
tally count of used symbols
39
#include <stddef.h>		/* NULL, size_t, ssize_t */
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
40
#include <sys/types.h>		/* ssize_t, struct dirent, pid_t,
41
				   ssize_t, open() */
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
42
#include <stdlib.h>		/* EXIT_SUCCESS, EXIT_FAILURE,
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
43
				   getenv(), free() */
44
#include <dirent.h>		/* scandir(), alphasort() */
13 by Björn Påhlsson
Added following support:
45
#include <stdio.h>		/* fprintf(), stderr, getline(),
237.7.33 by Teddy Hogeborn
Merge from Björn.
46
				   stdin, feof(), fputc(), vfprintf(),
47
				   vasprintf() */
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
48
#include <errno.h>		/* errno, EBADF, ENOTTY, EINVAL,
49
				   EFAULT, EFBIG, EIO, ENOSPC, EINTR
50
				*/
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
51
#include <error.h>		/* error() */
13 by Björn Påhlsson
Added following support:
52
#include <iso646.h>		/* or, not */
53
#include <stdbool.h>		/* bool, false, true */
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
54
#include <inttypes.h>		/* strtoumax() */
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
55
#include <sys/stat.h>		/* struct stat, lstat(), open() */
237.7.33 by Teddy Hogeborn
Merge from Björn.
56
#include <string.h>		/* strlen, rindex, memcmp, strerror()
57
				 */
24.1.26 by Björn Påhlsson
tally count of used symbols
58
#include <argp.h>		/* struct argp_option, struct
59
				   argp_state, struct argp,
60
				   argp_parse(), error_t,
61
				   ARGP_KEY_ARG, ARGP_KEY_END,
62
				   ARGP_ERR_UNKNOWN */
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
63
#include <sysexits.h>		/* EX_SOFTWARE, EX_OSERR,
64
				   EX_UNAVAILABLE, EX_IOERR, EX_OK */
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
65
#include <fcntl.h>		/* open() */
24.1.173 by Björn Påhlsson
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.
66
#include <stdarg.h>		/* va_list, va_start(), ... */
13 by Björn Påhlsson
Added following support:
67
237.2.57 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
68
volatile sig_atomic_t quit_now = 0;
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
69
int signal_received;
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
70
bool debug = false;
217 by Teddy Hogeborn
* .bzrignore: Added "man" directory (created by "make install-html").
71
const char *argp_program_version = "password-prompt " VERSION;
237.11.2 by Teddy Hogeborn
Change "fukt.bsnet.se" to "recompile.se" throughout.
72
const char *argp_program_bug_address = "<mandos@recompile.se>";
13 by Björn Påhlsson
Added following support:
73
237.7.17 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Check for both
74
/* Needed for conflict resolution */
75
const char plymouth_name[] = "plymouthd";
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
76
24.1.173 by Björn Påhlsson
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.
77
/* Function to use when printing errors */
237.7.81 by teddy at bsnet
* plugins.d/splashy.c (error_plus): Check format string.
78
__attribute__((format (gnu_printf, 3, 4)))
237.7.33 by Teddy Hogeborn
Merge from Björn.
79
void error_plus(int status, int errnum, const char *formatstring,
80
		...){
24.1.173 by Björn Påhlsson
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.
81
  va_list ap;
82
  char *text;
83
  int ret;
84
  
85
  va_start(ap, formatstring);
86
  ret = vasprintf(&text, formatstring, ap);
237.7.218 by Teddy Hogeborn
White space fix: change "if (" to "if(" in C code.
87
  if(ret == -1){
237.7.33 by Teddy Hogeborn
Merge from Björn.
88
    fprintf(stderr, "Mandos plugin %s: ",
89
	    program_invocation_short_name);
24.1.173 by Björn Påhlsson
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.
90
    vfprintf(stderr, formatstring, ap);
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
91
    fprintf(stderr, ": %s\n", strerror(errnum));
24.1.173 by Björn Påhlsson
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.
92
    error(status, errno, "vasprintf while printing error");
93
    return;
94
  }
95
  fprintf(stderr, "Mandos plugin ");
96
  error(status, errnum, "%s", text);
97
  free(text);
98
}
99
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
100
static void termination_handler(int signum){
101
  if(quit_now){
102
    return;
103
  }
237.2.57 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
104
  quit_now = 1;
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
105
  signal_received = signum;
13 by Björn Påhlsson
Added following support:
106
}
107
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
108
bool conflict_detection(void){
109
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
110
  /* plymouth conflicts with password-prompt since both want to read
111
     from the terminal.  Password-prompt will exit if it detects
112
     plymouth since plymouth performs the same functionality.
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
113
   */
237.7.675 by Teddy Hogeborn
Add dracut(8) support
114
  if(access("/run/plymouth/pid", R_OK) == 0){
115
    return true;
116
  }
117
  
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
118
  __attribute__((nonnull))
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
119
  int is_plymouth(const struct dirent *proc_entry){
120
    int ret;
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
121
    int cl_fd;
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
122
    {
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
123
      uintmax_t proc_id;
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
124
      char *tmp;
125
      errno = 0;
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
126
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
127
      
128
      if(errno != 0 or *tmp != '\0'
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
129
	 or proc_id != (uintmax_t)((pid_t)proc_id)){
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
130
	return 0;
131
      }
132
    }
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
133
    
134
    char *cmdline_filename;
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
135
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
136
		   proc_entry->d_name);
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
137
    if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
138
      error_plus(0, errno, "asprintf");
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
139
      return 0;
140
    }
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
141
    
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
142
    /* Open /proc/<pid>/cmdline */
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
143
    cl_fd = open(cmdline_filename, O_RDONLY);
144
    free(cmdline_filename);
145
    if(cl_fd == -1){
237.7.26 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Don't print error
146
      if(errno != ENOENT){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
147
	error_plus(0, errno, "open");
237.7.26 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Don't print error
148
      }
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
149
      return 0;
150
    }
151
    
152
    char *cmdline = NULL;
153
    {
154
      size_t cmdline_len = 0;
155
      size_t cmdline_allocated = 0;
156
      char *tmp;
157
      const size_t blocksize = 1024;
158
      ssize_t sret;
159
      do {
160
	/* Allocate more space? */
161
	if(cmdline_len + blocksize + 1 > cmdline_allocated){
162
	  tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
163
	  if(tmp == NULL){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
164
	    error_plus(0, errno, "realloc");
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
165
	    free(cmdline);
166
	    close(cl_fd);
167
	    return 0;
168
	  }
169
	  cmdline = tmp;
170
	  cmdline_allocated += blocksize;
171
	}
172
	
173
	/* Read data */
174
	sret = read(cl_fd, cmdline + cmdline_len,
175
		    cmdline_allocated - cmdline_len);
176
	if(sret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
177
	  error_plus(0, errno, "read");
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
178
	  free(cmdline);
179
	  close(cl_fd);
180
	  return 0;
181
	}
182
	cmdline_len += (size_t)sret;
183
      } while(sret != 0);
184
      ret = close(cl_fd);
185
      if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
186
	error_plus(0, errno, "close");
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
187
	free(cmdline);
188
	return 0;
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
189
      }
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
190
      cmdline[cmdline_len] = '\0'; /* Make sure it is terminated */
191
    }
192
    /* we now have cmdline */
193
    
194
    /* get basename */
195
    char *cmdline_base = strrchr(cmdline, '/');
196
    if(cmdline_base != NULL){
197
      cmdline_base += 1;		/* skip the slash */
198
    } else {
199
      cmdline_base = cmdline;
200
    }
201
    
237.7.19 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Check for both
202
    if(strcmp(cmdline_base, plymouth_name) != 0){
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
203
      if(debug){
237.7.19 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Check for both
204
	fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
205
		plymouth_name);
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
206
      }
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
207
      free(cmdline);
208
      return 0;
209
    }
237.7.19 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Check for both
210
    if(debug){
211
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
212
	      plymouth_name);
213
    }
275.1.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
214
    free(cmdline);
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
215
    return 1;
216
  }
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
217
  
24.1.176 by Björn Påhlsson
free dirents in password-prompt
218
  struct dirent **direntries = NULL;
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
219
  int ret;
220
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
237.7.218 by Teddy Hogeborn
White space fix: change "if (" to "if(" in C code.
221
  if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
222
    error_plus(1, errno, "scandir");
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
223
  }
237.7.449 by Teddy Hogeborn
Fix memory leak in password-prompt
224
  {
225
    int i = ret;
226
    while(i--){
227
      free(direntries[i]);
228
    }
229
  }
285 by Teddy Hogeborn
* Makefile (version): Changed to "1.3.1".
230
  free(direntries);
237.7.15 by Teddy Hogeborn
Merge fix for password-prompt/plymouth conflict from Björn.
231
  return ret > 0;
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
232
}
233
234
13 by Björn Påhlsson
Added following support:
235
int main(int argc, char **argv){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
236
  ssize_t sret;
237
  int ret;
13 by Björn Påhlsson
Added following support:
238
  size_t n;
239
  struct termios t_new, t_old;
240
  char *buffer = NULL;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
241
  char *prefix = NULL;
237.7.675 by Teddy Hogeborn
Add dracut(8) support
242
  char *prompt = NULL;
13 by Björn Påhlsson
Added following support:
243
  int status = EXIT_SUCCESS;
244
  struct sigaction old_action,
245
    new_action = { .sa_handler = termination_handler,
246
		   .sa_flags = 0 };
24.1.17 by Björn Påhlsson
passprompt
247
  {
248
    struct argp_option options[] = {
249
      { .name = "prefix", .key = 'p',
250
	.arg = "PREFIX", .flags = 0,
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
251
	.doc = "Prefix shown before the prompt", .group = 2 },
237.7.675 by Teddy Hogeborn
Add dracut(8) support
252
      { .name = "prompt", .key = 129,
253
	.arg = "PROMPT", .flags = 0,
254
	.doc = "The prompt to show", .group = 2 },
24.1.17 by Björn Påhlsson
passprompt
255
      { .name = "debug", .key = 128,
256
	.doc = "Debug mode", .group = 3 },
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
257
      /*
258
       * These reproduce what we would get without ARGP_NO_HELP
259
       */
260
      { .name = "help", .key = '?',
261
	.doc = "Give this help list", .group = -1 },
262
      { .name = "usage", .key = -3,
263
	.doc = "Give a short usage message", .group = -1 },
264
      { .name = "version", .key = 'V',
265
	.doc = "Print program version", .group = -1 },
24.1.17 by Björn Påhlsson
passprompt
266
      { .name = NULL }
267
    };
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
268
    
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
269
    __attribute__((nonnull(3)))
237.2.56 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
270
    error_t parse_opt (int key, char *arg, struct argp_state *state){
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
271
      errno = 0;
237.2.56 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
272
      switch (key){
237.7.675 by Teddy Hogeborn
Add dracut(8) support
273
      case 'p':			/* --prefix */
24.1.17 by Björn Påhlsson
passprompt
274
	prefix = arg;
275
	break;
237.7.675 by Teddy Hogeborn
Add dracut(8) support
276
      case 128:			/* --debug */
24.1.17 by Björn Påhlsson
passprompt
277
	debug = true;
278
	break;
237.7.675 by Teddy Hogeborn
Add dracut(8) support
279
      case 129:			/* --prompt */
280
	prompt = arg;
281
	break;
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
282
	/*
283
	 * These reproduce what we would get without ARGP_NO_HELP
284
	 */
285
      case '?':			/* --help */
286
	argp_state_help(state, state->out_stream,
287
			(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
288
			& ~(unsigned int)ARGP_HELP_EXIT_OK);
237.7.523 by Teddy Hogeborn
Fix warnings from -Wimplicit-fallthrough
289
	__builtin_unreachable();
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
290
      case -3:			/* --usage */
291
	argp_state_help(state, state->out_stream,
292
			ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
237.7.523 by Teddy Hogeborn
Fix warnings from -Wimplicit-fallthrough
293
	__builtin_unreachable();
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
294
      case 'V':			/* --version */
295
	fprintf(state->out_stream, "%s\n", argp_program_version);
296
	exit(argp_err_exit_status);
24.1.17 by Björn Påhlsson
passprompt
297
	break;
298
      default:
299
	return ARGP_ERR_UNKNOWN;
300
      }
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
301
      return errno;
24.1.17 by Björn Påhlsson
passprompt
302
    }
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
303
    
24.1.17 by Björn Påhlsson
passprompt
304
    struct argp argp = { .options = options, .parser = parse_opt,
305
			 .args_doc = "",
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
306
			 .doc = "Mandos password-prompt -- Read and"
307
			 " output a password" };
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
308
    ret = argp_parse(&argp, argc, argv,
309
		     ARGP_IN_ORDER | ARGP_NO_HELP, NULL, NULL);
310
    switch(ret){
311
    case 0:
312
      break;
313
    case ENOMEM:
314
    default:
315
      errno = ret;
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
316
      error_plus(0, errno, "argp_parse");
237.2.157 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
317
      return EX_OSERR;
318
    case EINVAL:
319
      return EX_USAGE;
24.1.26 by Björn Påhlsson
tally count of used symbols
320
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
321
  }
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
322
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
323
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
324
    fprintf(stderr, "Starting %s\n", argv[0]);
325
  }
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
326
237.7.218 by Teddy Hogeborn
White space fix: change "if (" to "if(" in C code.
327
  if(conflict_detection()){
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
328
    if(debug){
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
329
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
330
    }
331
    return EXIT_FAILURE;
332
  }
333
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
334
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
335
    fprintf(stderr, "Storing current terminal attributes\n");
336
  }
13 by Björn Påhlsson
Added following support:
337
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
338
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
339
    int e = errno;
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
340
    error_plus(0, errno, "tcgetattr");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
341
    switch(e){
342
    case EBADF:
343
    case ENOTTY:
344
      return EX_UNAVAILABLE;
345
    default:
346
      return EX_OSERR;
347
    }
13 by Björn Påhlsson
Added following support:
348
  }
349
  
350
  sigemptyset(&new_action.sa_mask);
237.2.120 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
351
  ret = sigaddset(&new_action.sa_mask, SIGINT);
352
  if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
353
    error_plus(0, errno, "sigaddset");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
354
    return EX_OSERR;
237.2.120 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
355
  }
356
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
357
  if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
358
    error_plus(0, errno, "sigaddset");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
359
    return EX_OSERR;
237.2.120 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
360
  }
361
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
362
  if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
363
    error_plus(0, errno, "sigaddset");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
364
    return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
365
  }
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
366
  /* Need to check if the handler is SIG_IGN before handling:
367
     | [[info:libc:Initial Signal Actions]] |
368
     | [[info:libc:Basic Signal Handling]]  |
369
  */
237.2.120 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
370
  ret = sigaction(SIGINT, NULL, &old_action);
371
  if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
372
    error_plus(0, errno, "sigaction");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
373
    return EX_OSERR;
237.2.120 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
374
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
375
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
376
    ret = sigaction(SIGINT, &new_action, NULL);
377
    if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
378
      error_plus(0, errno, "sigaction");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
379
      return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
380
    }
381
  }
382
  ret = sigaction(SIGHUP, NULL, &old_action);
383
  if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
384
    error_plus(0, errno, "sigaction");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
385
    return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
386
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
387
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
388
    ret = sigaction(SIGHUP, &new_action, NULL);
389
    if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
390
      error_plus(0, errno, "sigaction");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
391
      return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
392
    }
393
  }
394
  ret = sigaction(SIGTERM, NULL, &old_action);
395
  if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
396
    error_plus(0, errno, "sigaction");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
397
    return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
398
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
399
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
400
    ret = sigaction(SIGTERM, &new_action, NULL);
401
    if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
402
      error_plus(0, errno, "sigaction");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
403
      return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
404
    }
405
  }
406
  
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
407
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
408
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
409
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
410
  }
13 by Björn Påhlsson
Added following support:
411
  
412
  t_new = t_old;
237.2.140 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): Fix "-Wconversion" warning.
413
  t_new.c_lflag &= ~(tcflag_t)ECHO;
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
414
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
415
    int e = errno;
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
416
    error_plus(0, errno, "tcsetattr-echo");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
417
    switch(e){
418
    case EBADF:
419
    case ENOTTY:
420
      return EX_UNAVAILABLE;
421
    case EINVAL:
422
    default:
423
      return EX_OSERR;
424
    }
13 by Björn Påhlsson
Added following support:
425
  }
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
426
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
427
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
428
    fprintf(stderr, "Waiting for input from stdin \n");
429
  }
13 by Björn Påhlsson
Added following support:
430
  while(true){
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
431
    if(quit_now){
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
432
      if(debug){
433
	fprintf(stderr, "Interrupted by signal, exiting.\n");
434
      }
13 by Björn Påhlsson
Added following support:
435
      status = EXIT_FAILURE;
436
      break;
437
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
438
439
    if(prefix){
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
440
      fprintf(stderr, "%s ", prefix);
441
    }
237.7.675 by Teddy Hogeborn
Add dracut(8) support
442
    if(prompt != NULL){
443
      fprintf(stderr, "%s: ", prompt);
444
    } else {
237.2.160 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
445
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
446
      const char *crypttarget = getenv("CRYPTTAB_NAME");
447
      /* Before cryptsetup 1.1.0~rc2 */
448
      if(cryptsource == NULL){
449
	cryptsource = getenv("cryptsource");
450
      }
451
      if(crypttarget == NULL){
452
	crypttarget = getenv("crypttarget");
453
      }
454
      const char *const prompt1 = "Unlocking the disk";
455
      const char *const prompt2 = "Enter passphrase";
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
456
      if(cryptsource == NULL){
457
	if(crypttarget == NULL){
237.2.160 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
458
	  fprintf(stderr, "%s to unlock the disk: ", prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
459
	} else {
237.2.160 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
460
	  fprintf(stderr, "%s (%s)\n%s: ", prompt1, crypttarget,
461
		  prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
462
	}
463
      } else {
464
	if(crypttarget == NULL){
237.2.160 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
465
	  fprintf(stderr, "%s %s\n%s: ", prompt1, cryptsource,
466
		  prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
467
	} else {
237.2.160 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
468
	  fprintf(stderr, "%s %s (%s)\n%s: ", prompt1, cryptsource,
469
		  crypttarget, prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
470
	}
471
      }
472
    }
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
473
    sret = getline(&buffer, &n, stdin);
474
    if(sret > 0){
13 by Björn Påhlsson
Added following support:
475
      status = EXIT_SUCCESS;
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
476
      /* Make n = data size instead of allocated buffer size */
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
477
      n = (size_t)sret;
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
478
      /* Strip final newline */
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
479
      if(n > 0 and buffer[n-1] == '\n'){
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
480
	buffer[n-1] = '\0';	/* not strictly necessary */
481
	n--;
482
      }
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
483
      size_t written = 0;
484
      while(written < n){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
485
	sret = write(STDOUT_FILENO, buffer + written, n - written);
486
	if(sret < 0){
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
487
	  int e = errno;
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
488
	  error_plus(0, errno, "write");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
489
	  switch(e){
490
	  case EBADF:
491
	  case EFAULT:
492
	  case EINVAL:
493
	  case EFBIG:
494
	  case EIO:
495
	  case ENOSPC:
496
	  default:
497
	    status = EX_IOERR;
498
	    break;
499
	  case EINTR:
500
	    status = EXIT_FAILURE;
501
	    break;
502
	  }
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
503
	  break;
504
	}
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
505
	written += (size_t)sret;
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
506
      }
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
507
      sret = close(STDOUT_FILENO);
508
      if(sret == -1){
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
509
	int e = errno;
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
510
	error_plus(0, errno, "close");
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
511
	switch(e){
512
	case EBADF:
513
	  status = EX_OSFILE;
514
	  break;
515
	case EIO:
516
	default:
517
	  status = EX_IOERR;
518
	  break;
519
	}
520
      }
13 by Björn Påhlsson
Added following support:
521
      break;
522
    }
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
523
    if(sret < 0){
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
524
      int e = errno;
237.7.658 by Teddy Hogeborn
plugins.d/password-prompt.c: Clear EOF from stdin
525
      if(errno != EINTR){
526
	if(not feof(stdin)){
527
	  error_plus(0, errno, "getline");
528
	  switch(e){
529
	  case EBADF:
530
	    status = EX_UNAVAILABLE;
531
	    break;
532
	  case EIO:
533
	  case EINVAL:
534
	  default:
535
	    status = EX_IOERR;
536
	    break;
537
	  }
538
	  break;
539
	} else {
540
	  clearerr(stdin);
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
541
	}
13 by Björn Påhlsson
Added following support:
542
      }
543
    }
237.9.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
544
    /* if(sret == 0), then the only sensible thing to do is to retry
545
       to read from stdin */
13 by Björn Påhlsson
Added following support:
546
    fputc('\n', stderr);
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
547
    if(debug and not quit_now){
237.2.57 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
548
      /* If quit_now is nonzero, we were interrupted by a signal, and
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
549
	 will print that later, so no need to show this too. */
550
      fprintf(stderr, "getline() returned 0, retrying.\n");
551
    }
13 by Björn Påhlsson
Added following support:
552
  }
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
553
  
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
554
  free(buffer);
41 by Teddy Hogeborn
Merge.
555
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
556
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
557
    fprintf(stderr, "Restoring terminal attributes\n");
558
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
559
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
560
    error_plus(0, errno, "tcsetattr+echo");
13 by Björn Påhlsson
Added following support:
561
  }
41 by Teddy Hogeborn
Merge.
562
  
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
563
  if(quit_now){
564
    sigemptyset(&old_action.sa_mask);
565
    old_action.sa_handler = SIG_DFL;
566
    ret = sigaction(signal_received, &old_action, NULL);
567
    if(ret == -1){
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
568
      error_plus(0, errno, "sigaction");
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
569
    }
570
    raise(signal_received);
571
  }
572
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
573
  if(debug){
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
574
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
575
	    status);
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
576
  }
237.2.154 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
577
  if(status == EXIT_SUCCESS or status == EX_OK){
167 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): If successful, output an extra
578
    fputc('\n', stderr);
579
  }
13 by Björn Påhlsson
Added following support:
580
  
581
  return status;
582
}