/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.2.9 by Teddy Hogeborn
* README: Update copyright year; add "2009".
5
 * Copyright © 2008,2009 Teddy Hogeborn
6
 * Copyright © 2008,2009 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.2.126 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
13 by Björn Påhlsson
Added following support:
25
#define _GNU_SOURCE		/* getline() */
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
26
13 by Björn Påhlsson
Added following support:
27
#include <termios.h> 		/* struct termios, tcsetattr(),
28
				   TCSAFLUSH, tcgetattr(), ECHO */
29
#include <unistd.h>		/* struct termios, tcsetattr(),
30
				   STDIN_FILENO, TCSAFLUSH,
31
				   tcgetattr(), ECHO */
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 */
13 by Björn Påhlsson
Added following support:
38
#include <sys/types.h>		/* ssize_t */
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
39
#include <stdlib.h>		/* EXIT_SUCCESS, EXIT_FAILURE,
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
40
				   getopt_long, getenv() */
13 by Björn Påhlsson
Added following support:
41
#include <stdio.h>		/* fprintf(), stderr, getline(),
42
				   stdin, feof(), perror(), fputc(),
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
43
				   stdout, getopt_long */
13 by Björn Påhlsson
Added following support:
44
#include <errno.h>		/* errno, EINVAL */
45
#include <iso646.h>		/* or, not */
46
#include <stdbool.h>		/* bool, false, true */
24.1.141 by Björn Påhlsson
fixed incorrect include comments
47
#include <string.h> 		/* strlen, rindex */
24.1.26 by Björn Påhlsson
tally count of used symbols
48
#include <argp.h>		/* struct argp_option, struct
49
				   argp_state, struct argp,
50
				   argp_parse(), error_t,
51
				   ARGP_KEY_ARG, ARGP_KEY_END,
52
				   ARGP_ERR_UNKNOWN */
13 by Björn Påhlsson
Added following support:
53
237.2.57 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
54
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
55
int signal_received;
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
56
bool debug = false;
217 by Teddy Hogeborn
* .bzrignore: Added "man" directory (created by "make install-html").
57
const char *argp_program_version = "password-prompt " VERSION;
24.1.17 by Björn Påhlsson
passprompt
58
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
13 by Björn Påhlsson
Added following support:
59
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
60
static void termination_handler(int signum){
61
  if(quit_now){
62
    return;
63
  }
237.2.57 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
64
  quit_now = 1;
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
65
  signal_received = signum;
13 by Björn Påhlsson
Added following support:
66
}
67
68
int main(int argc, char **argv){
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
69
  ssize_t ret;
13 by Björn Påhlsson
Added following support:
70
  size_t n;
71
  struct termios t_new, t_old;
72
  char *buffer = NULL;
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
73
  char *prefix = NULL;
13 by Björn Påhlsson
Added following support:
74
  int status = EXIT_SUCCESS;
75
  struct sigaction old_action,
76
    new_action = { .sa_handler = termination_handler,
77
		   .sa_flags = 0 };
24.1.17 by Björn Påhlsson
passprompt
78
  {
79
    struct argp_option options[] = {
80
      { .name = "prefix", .key = 'p',
81
	.arg = "PREFIX", .flags = 0,
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
82
	.doc = "Prefix shown before the prompt", .group = 2 },
24.1.17 by Björn Påhlsson
passprompt
83
      { .name = "debug", .key = 128,
84
	.doc = "Debug mode", .group = 3 },
85
      { .name = NULL }
86
    };
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
87
    
237.2.56 by Teddy Hogeborn
* plugin-runner.c: Whitespace changes only.
88
    error_t parse_opt (int key, char *arg, struct argp_state *state){
89
      switch (key){
24.1.17 by Björn Påhlsson
passprompt
90
      case 'p':
91
	prefix = arg;
92
	break;
93
      case 128:
94
	debug = true;
95
	break;
96
      case ARGP_KEY_ARG:
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
97
	argp_usage(state);
24.1.17 by Björn Påhlsson
passprompt
98
	break;
99
      case ARGP_KEY_END:
100
	break;
101
      default:
102
	return ARGP_ERR_UNKNOWN;
103
      }
104
      return 0;
105
    }
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
106
    
24.1.17 by Björn Påhlsson
passprompt
107
    struct argp argp = { .options = options, .parser = parse_opt,
108
			 .args_doc = "",
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
109
			 .doc = "Mandos password-prompt -- Read and"
110
			 " output a password" };
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
111
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
112
    if(ret == ARGP_ERR_UNKNOWN){
24.1.44 by Björn Påhlsson
Fixed unkown -> unknown
113
      fprintf(stderr, "Unknown error while parsing arguments\n");
24.1.26 by Björn Påhlsson
tally count of used symbols
114
      return EXIT_FAILURE;
115
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
116
  }
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
117
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
118
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
119
    fprintf(stderr, "Starting %s\n", argv[0]);
120
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
121
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
122
    fprintf(stderr, "Storing current terminal attributes\n");
123
  }
13 by Björn Påhlsson
Added following support:
124
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
125
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
126
    perror("tcgetattr");
13 by Björn Påhlsson
Added following support:
127
    return EXIT_FAILURE;
128
  }
129
  
130
  sigemptyset(&new_action.sa_mask);
237.2.120 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
131
  ret = sigaddset(&new_action.sa_mask, SIGINT);
132
  if(ret == -1){
133
    perror("sigaddset");
134
    return EXIT_FAILURE;
135
  }
136
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
137
  if(ret == -1){
138
    perror("sigaddset");
139
    return EXIT_FAILURE;
140
  }
141
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
142
  if(ret == -1){
143
    perror("sigaddset");
24.1.26 by Björn Påhlsson
tally count of used symbols
144
    return EXIT_FAILURE;
145
  }
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
146
  /* Need to check if the handler is SIG_IGN before handling:
147
     | [[info:libc:Initial Signal Actions]] |
148
     | [[info:libc:Basic Signal Handling]]  |
149
  */
237.2.120 by Teddy Hogeborn
* plugins.d/mandos-client.c (main): Do not handle ignored signals.
150
  ret = sigaction(SIGINT, NULL, &old_action);
151
  if(ret == -1){
152
    perror("sigaction");
153
    return EXIT_FAILURE;
154
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
155
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
156
    ret = sigaction(SIGINT, &new_action, NULL);
157
    if(ret == -1){
158
      perror("sigaction");
159
      return EXIT_FAILURE;
160
    }
161
  }
162
  ret = sigaction(SIGHUP, NULL, &old_action);
163
  if(ret == -1){
164
    perror("sigaction");
165
    return EXIT_FAILURE;
166
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
167
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
168
    ret = sigaction(SIGHUP, &new_action, NULL);
169
    if(ret == -1){
170
      perror("sigaction");
171
      return EXIT_FAILURE;
172
    }
173
  }
174
  ret = sigaction(SIGTERM, NULL, &old_action);
175
  if(ret == -1){
176
    perror("sigaction");
177
    return EXIT_FAILURE;
178
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
179
  if(old_action.sa_handler != SIG_IGN){
24.1.26 by Björn Påhlsson
tally count of used symbols
180
    ret = sigaction(SIGTERM, &new_action, NULL);
181
    if(ret == -1){
182
      perror("sigaction");
183
      return EXIT_FAILURE;
184
    }
185
  }
186
  
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
187
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
188
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
189
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
190
  }
13 by Björn Påhlsson
Added following support:
191
  
192
  t_new = t_old;
193
  t_new.c_lflag &= ~ECHO;
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
194
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
13 by Björn Påhlsson
Added following support:
195
    perror("tcsetattr-echo");
196
    return EXIT_FAILURE;
197
  }
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
198
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
199
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
200
    fprintf(stderr, "Waiting for input from stdin \n");
201
  }
13 by Björn Påhlsson
Added following support:
202
  while(true){
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
203
    if(quit_now){
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
204
      if(debug){
205
	fprintf(stderr, "Interrupted by signal, exiting.\n");
206
      }
13 by Björn Påhlsson
Added following support:
207
      status = EXIT_FAILURE;
208
      break;
209
    }
15.1.3 by Björn Påhlsson
Added getopt_long support for mandosclient and passprompt
210
211
    if(prefix){
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
212
      fprintf(stderr, "%s ", prefix);
213
    }
214
    {
215
      const char *cryptsource = getenv("cryptsource");
216
      const char *crypttarget = getenv("crypttarget");
217
      const char *const prompt
237.2.118 by Teddy Hogeborn
* mandos: White-space fixes only.
218
	= "Enter passphrase to unlock the disk";
79 by Teddy Hogeborn
* plugins.d/password-request.c (main): Include environment variables
219
      if(cryptsource == NULL){
220
	if(crypttarget == NULL){
221
	  fprintf(stderr, "%s: ", prompt);
222
	} else {
223
	  fprintf(stderr, "%s (%s): ", prompt, crypttarget);
224
	}
225
      } else {
226
	if(crypttarget == NULL){
227
	  fprintf(stderr, "%s %s: ", prompt, cryptsource);
228
	} else {
229
	  fprintf(stderr, "%s %s (%s): ", prompt, cryptsource,
230
		  crypttarget);
231
	}
232
      }
233
    }
13 by Björn Påhlsson
Added following support:
234
    ret = getline(&buffer, &n, stdin);
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
235
    if(ret > 0){
13 by Björn Påhlsson
Added following support:
236
      status = EXIT_SUCCESS;
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
237
      /* Make n = data size instead of allocated buffer size */
238
      n = (size_t)ret;
168 by Teddy Hogeborn
* initramfs-tools-hook: Use long options where available. Use only
239
      /* Strip final newline */
240
      if(n>0 and buffer[n-1] == '\n'){
241
	buffer[n-1] = '\0';	/* not strictly necessary */
242
	n--;
243
      }
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
244
      size_t written = 0;
245
      while(written < n){
246
	ret = write(STDOUT_FILENO, buffer + written, n - written);
247
	if(ret < 0){
248
	  perror("write");
249
	  status = EXIT_FAILURE;
250
	  break;
251
	}
252
	written += (size_t)ret;
253
      }
13 by Björn Påhlsson
Added following support:
254
      break;
255
    }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
256
    if(ret < 0){
257
      if(errno != EINTR and not feof(stdin)){
13 by Björn Påhlsson
Added following support:
258
	perror("getline");
259
	status = EXIT_FAILURE;
260
	break;
261
      }
262
    }
41 by Teddy Hogeborn
Merge.
263
    /* if(ret == 0), then the only sensible thing to do is to retry to
264
       read from stdin */
13 by Björn Påhlsson
Added following support:
265
    fputc('\n', stderr);
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
266
    if(debug and not quit_now){
237.2.57 by Teddy Hogeborn
* plugins.d/password-prompt.c (quit_now): Changed type to "volatile
267
      /* 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
268
	 will print that later, so no need to show this too. */
269
      fprintf(stderr, "getline() returned 0, retrying.\n");
270
    }
13 by Björn Påhlsson
Added following support:
271
  }
237.2.20 by Teddy Hogeborn
Change the default value of the "checker" option command to make the
272
  
24.1.92 by Björn Påhlsson
Several memory leaks detected by valgrind fixed
273
  free(buffer);
41 by Teddy Hogeborn
Merge.
274
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
275
  if(debug){
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
276
    fprintf(stderr, "Restoring terminal attributes\n");
277
  }
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
278
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
13 by Björn Påhlsson
Added following support:
279
    perror("tcsetattr+echo");
280
  }
41 by Teddy Hogeborn
Merge.
281
  
237.2.119 by Teddy Hogeborn
* plugin-runner.c (main): When a plugin is killed by a signal, show
282
  if(quit_now){
283
    sigemptyset(&old_action.sa_mask);
284
    old_action.sa_handler = SIG_DFL;
285
    ret = sigaction(signal_received, &old_action, NULL);
286
    if(ret == -1){
287
      perror("sigaction");
288
    }
289
    raise(signal_received);
290
  }
291
  
237.2.29 by Teddy Hogeborn
* plugin-runner.c: Only space changes.
292
  if(debug){
110 by Teddy Hogeborn
* mandos.xml (EXAMPLE): Replaced all occurences of command name with
293
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
294
	    status);
15.1.2 by Björn Påhlsson
Added debug options from passprompt as --debug and --debug=passprompt
295
  }
167 by Teddy Hogeborn
* plugins.d/password-prompt.c (main): If successful, output an extra
296
  if(status == EXIT_SUCCESS){
297
    fputc('\n', stderr);
298
  }
13 by Björn Påhlsson
Added following support:
299
  
300
  return status;
301
}