/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
356 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
/*
261 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
 * 
444 by Teddy Hogeborn
Update copyright year to "2010" wherever appropriate.
5
 * Copyright © 2008-2010 Teddy Hogeborn
6
 * Copyright © 2008-2010 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
 * 
363 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
23
 */
24
237.5.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
469.1.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,
237.5.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,
356 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 */
237.5.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,
237.5.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(),
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
44
				   stdin, feof(), fputc()
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
45
				*/
391 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 */
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
52
#include <inttypes.h>		/* strtoumax() */
469.1.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() */
54
#include <string.h>		/* strlen, rindex, memcmp */
24.1.26 by Björn Påhlsson
tally count of used symbols
55
#include <argp.h>		/* struct argp_option, struct
56
				   argp_state, struct argp,
57
				   argp_parse(), error_t,
58
				   ARGP_KEY_ARG, ARGP_KEY_END,
59
				   ARGP_ERR_UNKNOWN */
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
60
#include <sysexits.h>		/* EX_SOFTWARE, EX_OSERR,
61
				   EX_UNAVAILABLE, EX_IOERR, EX_OK */
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
62
#include <fcntl.h>		/* open() */
13 by Björn Påhlsson
Added following support:
63
294 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
64
volatile sig_atomic_t quit_now = 0;
356 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
65
int signal_received;
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
66
bool debug = false;
217 by Teddy Hogeborn
* .bzrignore: Added "man" directory (created by "make install-html").
67
const char *argp_program_version = "password-prompt " VERSION;
24.1.17 by Björn Påhlsson
passprompt
68
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
13 by Björn Påhlsson
Added following support:
69
469 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Check for both
70
/* Needed for conflict resolution */
71
const char plymouth_name[] = "plymouthd";
72
const char plymouth_alt_name[] = "plymouthd";
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
73
74
356 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
75
static void termination_handler(int signum){
76
  if(quit_now){
77
    return;
78
  }
294 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
79
  quit_now = 1;
356 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
80
  signal_received = signum;
13 by Björn Påhlsson
Added following support:
81
}
82
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
83
bool conflict_detection(void){
84
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
85
  /* plymouth conflicts with password-prompt since both want to read
86
     from the terminal.  Password-prompt will exit if it detects
87
     plymouth since plymouth performs the same functionality.
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
88
   */
89
  int is_plymouth(const struct dirent *proc_entry){
90
    int ret;
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
91
    int cl_fd;
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
92
    {
93
      uintmax_t maxvalue;
94
      char *tmp;
95
      errno = 0;
96
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
97
      
98
      if(errno != 0 or *tmp != '\0'
99
	 or maxvalue != (uintmax_t)((pid_t)maxvalue)){
100
	return 0;
101
      }
102
    }
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
103
    
104
    char *cmdline_filename;
469.1.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
105
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
106
		   proc_entry->d_name);
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
107
    if(ret == -1){
108
      error(0, errno, "asprintf");
109
      return 0;
110
    }
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
111
    
469.1.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
112
    /* Open /proc/<pid>/cmdline */
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
113
    cl_fd = open(cmdline_filename, O_RDONLY);
114
    free(cmdline_filename);
115
    if(cl_fd == -1){
116
      error(0, errno, "open");
117
      return 0;
118
    }
119
    
120
    char *cmdline = NULL;
121
    {
122
      size_t cmdline_len = 0;
123
      size_t cmdline_allocated = 0;
124
      char *tmp;
125
      const size_t blocksize = 1024;
126
      ssize_t sret;
127
      do {
128
	/* Allocate more space? */
129
	if(cmdline_len + blocksize + 1 > cmdline_allocated){
130
	  tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
131
	  if(tmp == NULL){
132
	    error(0, errno, "realloc");
133
	    free(cmdline);
134
	    close(cl_fd);
135
	    return 0;
136
	  }
137
	  cmdline = tmp;
138
	  cmdline_allocated += blocksize;
139
	}
140
	
141
	/* Read data */
142
	sret = read(cl_fd, cmdline + cmdline_len,
143
		    cmdline_allocated - cmdline_len);
144
	if(sret == -1){
145
	  error(0, errno, "read");
146
	  free(cmdline);
147
	  close(cl_fd);
148
	  return 0;
149
	}
150
	cmdline_len += (size_t)sret;
151
      } while(sret != 0);
152
      ret = close(cl_fd);
153
      if(ret == -1){
154
	error(0, errno, "close");
155
	free(cmdline);
156
	return 0;
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
157
      }
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
158
      cmdline[cmdline_len] = '\0'; /* Make sure it is terminated */
159
    }
160
    /* we now have cmdline */
161
    
162
    /* get basename */
163
    char *cmdline_base = strrchr(cmdline, '/');
164
    if(cmdline_base != NULL){
165
      cmdline_base += 1;		/* skip the slash */
166
    } else {
167
      cmdline_base = cmdline;
168
    }
169
    
469 by Teddy Hogeborn
* plugins.d/password-prompt.c (conflict_detection): Check for both
170
    if((strcmp(cmdline_base, plymouth_name) != 0)
171
       and (strcmp(cmdline_base, plymouth_alt_name) != 0)){
469.1.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
172
      if(debug){
173
	fprintf(stderr, "\"%s\" is not \"%s\" or \"%s\"\n",
174
		cmdline_base, plymouth_name, plymouth_alt_name);
175
      }
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
176
      free(cmdline);
177
      return 0;
178
    }
469.1.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
179
    fprintf(stderr, "\"%s\" equals \"%s\" or \"%s\"\n",
180
	    cmdline_base, plymouth_name, plymouth_alt_name);
237.5.2 by Björn Påhlsson
fixed bug with conflict detection between plymouthd and password-prompt
181
    free(cmdline);
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
182
    return 1;
183
  }
469.1.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
184
  
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
185
  struct dirent **direntries;
186
  int ret;
187
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
188
  if (ret == -1){
189
    error(1, errno, "scandir");
190
  }
467 by Teddy Hogeborn
Merge fix for password-prompt/plymouth conflict from Björn.
191
  return ret > 0;
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
192
}
193
194
13 by Björn Påhlsson
Added following support:
195
int main(int argc, char **argv){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
196
  ssize_t sret;
197
  int ret;
13 by Björn Påhlsson
Added following support:
198
  size_t n;
199
  struct termios t_new, t_old;
200
  char *buffer = NULL;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
201
  char *prefix = NULL;
13 by Björn Påhlsson
Added following support:
202
  int status = EXIT_SUCCESS;
203
  struct sigaction old_action,
204
    new_action = { .sa_handler = termination_handler,
205
		   .sa_flags = 0 };
24.1.17 by Björn Påhlsson
passprompt
206
  {
207
    struct argp_option options[] = {
208
      { .name = "prefix", .key = 'p',
209
	.arg = "PREFIX", .flags = 0,
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
210
	.doc = "Prefix shown before the prompt", .group = 2 },
24.1.17 by Björn Påhlsson
passprompt
211
      { .name = "debug", .key = 128,
212
	.doc = "Debug mode", .group = 3 },
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
213
      /*
214
       * These reproduce what we would get without ARGP_NO_HELP
215
       */
216
      { .name = "help", .key = '?',
217
	.doc = "Give this help list", .group = -1 },
218
      { .name = "usage", .key = -3,
219
	.doc = "Give a short usage message", .group = -1 },
220
      { .name = "version", .key = 'V',
221
	.doc = "Print program version", .group = -1 },
24.1.17 by Björn Påhlsson
passprompt
222
      { .name = NULL }
223
    };
257 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
224
    
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
225
    error_t parse_opt (int key, char *arg, struct argp_state *state){
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
226
      errno = 0;
293 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
227
      switch (key){
24.1.17 by Björn Påhlsson
passprompt
228
      case 'p':
229
	prefix = arg;
230
	break;
231
      case 128:
232
	debug = true;
233
	break;
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
234
	/*
235
	 * These reproduce what we would get without ARGP_NO_HELP
236
	 */
237
      case '?':			/* --help */
238
	argp_state_help(state, state->out_stream,
239
			(ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
240
			& ~(unsigned int)ARGP_HELP_EXIT_OK);
241
      case -3:			/* --usage */
242
	argp_state_help(state, state->out_stream,
243
			ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
244
      case 'V':			/* --version */
245
	fprintf(state->out_stream, "%s\n", argp_program_version);
246
	exit(argp_err_exit_status);
24.1.17 by Björn Påhlsson
passprompt
247
	break;
248
      default:
249
	return ARGP_ERR_UNKNOWN;
250
      }
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
251
      return errno;
24.1.17 by Björn Påhlsson
passprompt
252
    }
257 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
253
    
24.1.17 by Björn Påhlsson
passprompt
254
    struct argp argp = { .options = options, .parser = parse_opt,
255
			 .args_doc = "",
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
256
			 .doc = "Mandos password-prompt -- Read and"
257
			 " output a password" };
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
258
    ret = argp_parse(&argp, argc, argv,
259
		     ARGP_IN_ORDER | ARGP_NO_HELP, NULL, NULL);
260
    switch(ret){
261
    case 0:
262
      break;
263
    case ENOMEM:
264
    default:
265
      errno = ret;
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
266
      error(0, errno, "argp_parse");
394 by Teddy Hogeborn
Convert some programs to use the exit codes from <sysexits.h>. Change
267
      return EX_OSERR;
268
    case EINVAL:
269
      return EX_USAGE;
24.1.26 by Björn Påhlsson
tally count of used symbols
270
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
271
  }
257 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
272
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
273
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
274
    fprintf(stderr, "Starting %s\n", argv[0]);
275
  }
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
276
277
  if (conflict_detection()){
278
    if(debug){
469.1.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
279
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
237.5.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
280
    }
281
    return EXIT_FAILURE;
282
  }
283
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
284
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
285
    fprintf(stderr, "Storing current terminal attributes\n");
286
  }
13 by Björn Påhlsson
Added following support:
287
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
288
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
289
    int e = errno;
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
290
    error(0, errno, "tcgetattr");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
291
    switch(e){
292
    case EBADF:
293
    case ENOTTY:
294
      return EX_UNAVAILABLE;
295
    default:
296
      return EX_OSERR;
297
    }
13 by Björn Påhlsson
Added following support:
298
  }
299
  
300
  sigemptyset(&new_action.sa_mask);
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
301
  ret = sigaddset(&new_action.sa_mask, SIGINT);
302
  if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
303
    error(0, errno, "sigaddset");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
304
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
305
  }
306
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
307
  if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
308
    error(0, errno, "sigaddset");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
309
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
310
  }
311
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
312
  if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
313
    error(0, errno, "sigaddset");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
314
    return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
315
  }
356 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
316
  /* Need to check if the handler is SIG_IGN before handling:
317
     | [[info:libc:Initial Signal Actions]] |
318
     | [[info:libc:Basic Signal Handling]]  |
319
  */
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
320
  ret = sigaction(SIGINT, NULL, &old_action);
321
  if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
322
    error(0, errno, "sigaction");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
323
    return EX_OSERR;
357 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
324
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
325
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
326
    ret = sigaction(SIGINT, &new_action, NULL);
327
    if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
328
      error(0, errno, "sigaction");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
329
      return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
330
    }
331
  }
332
  ret = sigaction(SIGHUP, NULL, &old_action);
333
  if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
334
    error(0, errno, "sigaction");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
335
    return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
336
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
337
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
338
    ret = sigaction(SIGHUP, &new_action, NULL);
339
    if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
340
      error(0, errno, "sigaction");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
341
      return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
342
    }
343
  }
344
  ret = sigaction(SIGTERM, NULL, &old_action);
345
  if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
346
    error(0, errno, "sigaction");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
347
    return EX_OSERR;
24.1.26 by Björn Påhlsson
tally count of used symbols
348
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
349
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
350
    ret = sigaction(SIGTERM, &new_action, NULL);
351
    if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
352
      error(0, errno, "sigaction");
391 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
    }
355
  }
356
  
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
357
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
358
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
359
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
360
  }
13 by Björn Påhlsson
Added following support:
361
  
362
  t_new = t_old;
377 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): Fix "-Wconversion" warning.
363
  t_new.c_lflag &= ~(tcflag_t)ECHO;
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
364
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
365
    int e = errno;
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
366
    error(0, errno, "tcsetattr-echo");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
367
    switch(e){
368
    case EBADF:
369
    case ENOTTY:
370
      return EX_UNAVAILABLE;
371
    case EINVAL:
372
    default:
373
      return EX_OSERR;
374
    }
13 by Björn Påhlsson
Added following support:
375
  }
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
376
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
377
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
378
    fprintf(stderr, "Waiting for input from stdin \n");
379
  }
13 by Björn Påhlsson
Added following support:
380
  while(true){
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
381
    if(quit_now){
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
382
      if(debug){
383
	fprintf(stderr, "Interrupted by signal, exiting.\n");
384
      }
13 by Björn Påhlsson
Added following support:
385
      status = EXIT_FAILURE;
386
      break;
387
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
388
389
    if(prefix){
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
390
      fprintf(stderr, "%s ", prefix);
391
    }
392
    {
397 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
393
      const char *cryptsource = getenv("CRYPTTAB_SOURCE");
394
      const char *crypttarget = getenv("CRYPTTAB_NAME");
395
      /* Before cryptsetup 1.1.0~rc2 */
396
      if(cryptsource == NULL){
397
	cryptsource = getenv("cryptsource");
398
      }
399
      if(crypttarget == NULL){
400
	crypttarget = getenv("crypttarget");
401
      }
402
      const char *const prompt1 = "Unlocking the disk";
403
      const char *const prompt2 = "Enter passphrase";
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
404
      if(cryptsource == NULL){
405
	if(crypttarget == NULL){
397 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
406
	  fprintf(stderr, "%s to unlock the disk: ", prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
407
	} else {
397 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
408
	  fprintf(stderr, "%s (%s)\n%s: ", prompt1, crypttarget,
409
		  prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
410
	}
411
      } else {
412
	if(crypttarget == NULL){
397 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
413
	  fprintf(stderr, "%s %s\n%s: ", prompt1, cryptsource,
414
		  prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
415
	} else {
397 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use environment variables and prompt
416
	  fprintf(stderr, "%s %s (%s)\n%s: ", prompt1, cryptsource,
417
		  crypttarget, prompt2);
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
418
	}
419
      }
420
    }
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
421
    sret = getline(&buffer, &n, stdin);
422
    if(sret > 0){
13 by Björn Påhlsson
Added following support:
423
      status = EXIT_SUCCESS;
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
424
      /* 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
425
      n = (size_t)sret;
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
426
      /* Strip final newline */
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
427
      if(n > 0 and buffer[n-1] == '\n'){
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
428
	buffer[n-1] = '\0';	/* not strictly necessary */
429
	n--;
430
      }
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
431
      size_t written = 0;
432
      while(written < n){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
433
	sret = write(STDOUT_FILENO, buffer + written, n - written);
434
	if(sret < 0){
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
435
	  int e = errno;
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
436
	  error(0, errno, "write");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
437
	  switch(e){
438
	  case EBADF:
439
	  case EFAULT:
440
	  case EINVAL:
441
	  case EFBIG:
442
	  case EIO:
443
	  case ENOSPC:
444
	  default:
445
	    status = EX_IOERR;
446
	    break;
447
	  case EINTR:
448
	    status = EXIT_FAILURE;
449
	    break;
450
	  }
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
451
	  break;
452
	}
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
453
	written += (size_t)sret;
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
454
      }
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
455
      sret = close(STDOUT_FILENO);
456
      if(sret == -1){
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
457
	int e = errno;
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
458
	error(0, errno, "close");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
459
	switch(e){
460
	case EBADF:
461
	  status = EX_OSFILE;
462
	  break;
463
	case EIO:
464
	default:
465
	  status = EX_IOERR;
466
	  break;
467
	}
468
      }
13 by Björn Påhlsson
Added following support:
469
      break;
470
    }
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
471
    if(sret < 0){
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
472
      int e = errno;
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
473
      if(errno != EINTR and not feof(stdin)){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
474
	error(0, errno, "getline");
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
475
	switch(e){
476
	case EBADF:
477
	  status = EX_UNAVAILABLE;
478
	case EIO:
479
	case EINVAL:
480
	default:
481
	  status = EX_IOERR;
482
	  break;
483
	}
13 by Björn Påhlsson
Added following support:
484
	break;
485
      }
486
    }
469.1.1 by teddy at bsnet
* plugins.d/password-prompt.c: Some white space fixes. Break some
487
    /* if(sret == 0), then the only sensible thing to do is to retry
488
       to read from stdin */
13 by Björn Påhlsson
Added following support:
489
    fputc('\n', stderr);
356 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
490
    if(debug and not quit_now){
294 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
491
      /* 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
492
	 will print that later, so no need to show this too. */
493
      fprintf(stderr, "getline() returned 0, retrying.\n");
494
    }
13 by Björn Påhlsson
Added following support:
495
  }
257 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
496
  
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
497
  free(buffer);
41 by Teddy Hogeborn
Merge.
498
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
499
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
500
    fprintf(stderr, "Restoring terminal attributes\n");
501
  }
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
502
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
503
    error(0, errno, "tcsetattr+echo");
13 by Björn Påhlsson
Added following support:
504
  }
41 by Teddy Hogeborn
Merge.
505
  
356 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
506
  if(quit_now){
507
    sigemptyset(&old_action.sa_mask);
508
    old_action.sa_handler = SIG_DFL;
509
    ret = sigaction(signal_received, &old_action, NULL);
510
    if(ret == -1){
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
511
      error(0, errno, "sigaction");
356 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
512
    }
513
    raise(signal_received);
514
  }
515
  
266 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
516
  if(debug){
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
517
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
518
	    status);
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
519
  }
391 by Teddy Hogeborn
* plugins.d/password-prompt.c: Use exit codes from <sysexits.h>. Do
520
  if(status == EXIT_SUCCESS or status == EX_OK){
167 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): If successful, output an extra
521
    fputc('\n', stderr);
522
  }
13 by Björn Påhlsson
Added following support:
523
  
524
  return status;
525
}