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