/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/password-prompt.c

  • Committer: Teddy Hogeborn
  • Date: 2012-06-01 21:48:12 UTC
  • Revision ID: teddy@recompile.se-20120601214812-g7685v5oeiuhi2qw
* debian/copyright (Copyright): Join the two lines to one line.
* debian/mandos-client.README.Debian: Refer to new location of example
  network hooks.
* debian/mandos-client.docs (network-hooks.d): Removed.
* debian/mandos-client.examples: New; contains "network-hooks.d".
* debian/rules (binary-common): Added "dh_installexamples".
  (binary-common/dh_fixperms): Changed to exclude new location of
  "network-hooks.d".
* init.d-mandos (status): Support new "status" action.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
5
 
 * Copyright © 2008-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 Björn Påhlsson
 
5
 * Copyright © 2008-2012 Teddy Hogeborn
 
6
 * Copyright © 2008-2012 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* getline(), asprintf() */
26
26
 
27
 
#include <termios.h>            /* struct termios, tcsetattr(),
 
27
#include <termios.h>            /* struct termios, tcsetattr(),
28
28
                                   TCSAFLUSH, tcgetattr(), ECHO */
29
29
#include <unistd.h>             /* struct termios, tcsetattr(),
30
30
                                   STDIN_FILENO, TCSAFLUSH,
35
35
                                   SIGQUIT, SIGHUP, SIGTERM,
36
36
                                   raise() */
37
37
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
 
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t, ssize_t */
 
38
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t,
 
39
                                   ssize_t, open() */
39
40
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
40
41
                                   getenv(), free() */
41
42
#include <dirent.h>             /* scandir(), alphasort() */
42
43
#include <stdio.h>              /* fprintf(), stderr, getline(),
43
 
                                   stdin, feof(), fputc()
44
 
                                */
 
44
                                   stdin, feof(), fputc(), vfprintf(),
 
45
                                   vasprintf() */
45
46
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
46
47
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
47
48
                                */
49
50
#include <iso646.h>             /* or, not */
50
51
#include <stdbool.h>            /* bool, false, true */
51
52
#include <inttypes.h>           /* strtoumax() */
52
 
#include <sys/stat.h>           /* struct stat, lstat() */
53
 
#include <string.h>             /* strlen, rindex, memcmp */
 
53
#include <sys/stat.h>           /* struct stat, lstat(), open() */
 
54
#include <string.h>             /* strlen, rindex, memcmp, strerror()
 
55
                                 */
54
56
#include <argp.h>               /* struct argp_option, struct
55
57
                                   argp_state, struct argp,
56
58
                                   argp_parse(), error_t,
58
60
                                   ARGP_ERR_UNKNOWN */
59
61
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
60
62
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
 
63
#include <fcntl.h>              /* open() */
 
64
#include <stdarg.h>             /* va_list, va_start(), ... */
61
65
 
62
66
volatile sig_atomic_t quit_now = 0;
63
67
int signal_received;
64
68
bool debug = false;
65
69
const char *argp_program_version = "password-prompt " VERSION;
66
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
67
 
 
68
 
/* Needed for conflic resolution */
69
 
const char plymouthd_path[] = "/sbin/plymouth";
70
 
 
 
70
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
71
 
 
72
/* Needed for conflict resolution */
 
73
const char plymouth_name[] = "plymouthd";
 
74
 
 
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
 
 
85
/* Function to use when printing errors */
 
86
__attribute__((format (gnu_printf, 3, 4)))
 
87
void error_plus(int status, int errnum, const char *formatstring,
 
88
                ...){
 
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){
 
96
    fprintf(stderr, "Mandos plugin %s: ",
 
97
            program_invocation_short_name);
 
98
    vfprintf(stderr, formatstring, ap);
 
99
    fprintf(stderr, ": %s\n", strerror(errnum));
 
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
}
71
107
 
72
108
static void termination_handler(int signum){
73
109
  if(quit_now){
79
115
 
80
116
bool conflict_detection(void){
81
117
 
82
 
  /* plymouth conflicts with password-promt since both want to control the
83
 
     associated terminal. Password-prompt exit since plymouth perferms the same
84
 
     functionallity.
 
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.
85
121
   */
 
122
  __attribute__((nonnull))
86
123
  int is_plymouth(const struct dirent *proc_entry){
87
124
    int ret;
 
125
    int cl_fd;
88
126
    {
89
 
      uintmax_t maxvalue;
 
127
      uintmax_t proc_id;
90
128
      char *tmp;
91
129
      errno = 0;
92
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
130
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
93
131
      
94
132
      if(errno != 0 or *tmp != '\0'
95
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
133
         or proc_id != (uintmax_t)((pid_t)proc_id)){
96
134
        return 0;
97
135
      }
98
136
    }
99
 
    char exe_target[sizeof(plymouthd_path)];
100
 
    char *exe_link;
101
 
    ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
 
137
    
 
138
    char *cmdline_filename;
 
139
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
 
140
                   proc_entry->d_name);
102
141
    if(ret == -1){
103
 
      error(0, errno, "asprintf");
 
142
      error_plus(0, errno, "asprintf");
104
143
      return 0;
105
144
    }
106
 
  
107
 
    struct stat exe_stat;
108
 
    ret = lstat(exe_link, &exe_stat);
109
 
    if(ret == -1){
110
 
      free(exe_link);
 
145
    
 
146
    /* Open /proc/<pid>/cmdline */
 
147
    cl_fd = open(cmdline_filename, O_RDONLY);
 
148
    free(cmdline_filename);
 
149
    if(cl_fd == -1){
111
150
      if(errno != ENOENT){
112
 
        error(0, errno, "lstat");
113
 
      }
114
 
      return 0;
115
 
    }
116
 
  
117
 
    if(not S_ISLNK(exe_stat.st_mode)
118
 
       or exe_stat.st_uid != 0
119
 
       or exe_stat.st_gid != 0){
120
 
      free(exe_link);
121
 
      return 0;
122
 
    }
123
 
  
124
 
    ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
125
 
    free(exe_link);
126
 
    if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
127
 
       (memcmp(plymouthd_path, exe_target,
128
 
               sizeof(plymouthd_path)-1) != 0)){
129
 
      return 0;
130
 
    }
 
151
        error_plus(0, errno, "open");
 
152
      }
 
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){
 
168
            error_plus(0, errno, "realloc");
 
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){
 
181
          error_plus(0, errno, "read");
 
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){
 
190
        error_plus(0, errno, "close");
 
191
        free(cmdline);
 
192
        return 0;
 
193
      }
 
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
    
 
206
    if(strcmp(cmdline_base, plymouth_name) != 0){
 
207
      if(debug){
 
208
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
 
209
                plymouth_name);
 
210
      }
 
211
      free(cmdline);
 
212
      return 0;
 
213
    }
 
214
    if(debug){
 
215
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
 
216
              plymouth_name);
 
217
    }
 
218
    free(cmdline);
131
219
    return 1;
132
220
  }
133
 
 
134
 
  struct dirent **direntries;
 
221
  
 
222
  struct dirent **direntries = NULL;
135
223
  int ret;
136
224
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
137
225
  if (ret == -1){
138
 
    error(1, errno, "scandir");
139
 
  }
140
 
  if (ret < 0){
141
 
    return 1;
142
 
  } else {
143
 
    return 0;
144
 
  }
 
226
    error_plus(1, errno, "scandir");
 
227
  }
 
228
  free(direntries);
 
229
  return ret > 0;
145
230
}
146
231
 
147
232
 
175
260
      { .name = NULL }
176
261
    };
177
262
    
 
263
    __attribute__((nonnull(3)))
178
264
    error_t parse_opt (int key, char *arg, struct argp_state *state){
179
265
      errno = 0;
180
266
      switch (key){
216
302
    case ENOMEM:
217
303
    default:
218
304
      errno = ret;
219
 
      error(0, errno, "argp_parse");
 
305
      error_plus(0, errno, "argp_parse");
220
306
      return EX_OSERR;
221
307
    case EINVAL:
222
308
      return EX_USAGE;
229
315
 
230
316
  if (conflict_detection()){
231
317
    if(debug){
232
 
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
 
318
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
233
319
    }
234
320
    return EXIT_FAILURE;
235
321
  }
240
326
  
241
327
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
242
328
    int e = errno;
243
 
    error(0, errno, "tcgetattr");
 
329
    error_plus(0, errno, "tcgetattr");
244
330
    switch(e){
245
331
    case EBADF:
246
332
    case ENOTTY:
253
339
  sigemptyset(&new_action.sa_mask);
254
340
  ret = sigaddset(&new_action.sa_mask, SIGINT);
255
341
  if(ret == -1){
256
 
    error(0, errno, "sigaddset");
 
342
    error_plus(0, errno, "sigaddset");
257
343
    return EX_OSERR;
258
344
  }
259
345
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
260
346
  if(ret == -1){
261
 
    error(0, errno, "sigaddset");
 
347
    error_plus(0, errno, "sigaddset");
262
348
    return EX_OSERR;
263
349
  }
264
350
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
265
351
  if(ret == -1){
266
 
    error(0, errno, "sigaddset");
 
352
    error_plus(0, errno, "sigaddset");
267
353
    return EX_OSERR;
268
354
  }
269
355
  /* Need to check if the handler is SIG_IGN before handling:
272
358
  */
273
359
  ret = sigaction(SIGINT, NULL, &old_action);
274
360
  if(ret == -1){
275
 
    error(0, errno, "sigaction");
 
361
    error_plus(0, errno, "sigaction");
276
362
    return EX_OSERR;
277
363
  }
278
364
  if(old_action.sa_handler != SIG_IGN){
279
365
    ret = sigaction(SIGINT, &new_action, NULL);
280
366
    if(ret == -1){
281
 
      error(0, errno, "sigaction");
 
367
      error_plus(0, errno, "sigaction");
282
368
      return EX_OSERR;
283
369
    }
284
370
  }
285
371
  ret = sigaction(SIGHUP, NULL, &old_action);
286
372
  if(ret == -1){
287
 
    error(0, errno, "sigaction");
 
373
    error_plus(0, errno, "sigaction");
288
374
    return EX_OSERR;
289
375
  }
290
376
  if(old_action.sa_handler != SIG_IGN){
291
377
    ret = sigaction(SIGHUP, &new_action, NULL);
292
378
    if(ret == -1){
293
 
      error(0, errno, "sigaction");
 
379
      error_plus(0, errno, "sigaction");
294
380
      return EX_OSERR;
295
381
    }
296
382
  }
297
383
  ret = sigaction(SIGTERM, NULL, &old_action);
298
384
  if(ret == -1){
299
 
    error(0, errno, "sigaction");
 
385
    error_plus(0, errno, "sigaction");
300
386
    return EX_OSERR;
301
387
  }
302
388
  if(old_action.sa_handler != SIG_IGN){
303
389
    ret = sigaction(SIGTERM, &new_action, NULL);
304
390
    if(ret == -1){
305
 
      error(0, errno, "sigaction");
 
391
      error_plus(0, errno, "sigaction");
306
392
      return EX_OSERR;
307
393
    }
308
394
  }
316
402
  t_new.c_lflag &= ~(tcflag_t)ECHO;
317
403
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
318
404
    int e = errno;
319
 
    error(0, errno, "tcsetattr-echo");
 
405
    error_plus(0, errno, "tcsetattr-echo");
320
406
    switch(e){
321
407
    case EBADF:
322
408
    case ENOTTY:
386
472
        sret = write(STDOUT_FILENO, buffer + written, n - written);
387
473
        if(sret < 0){
388
474
          int e = errno;
389
 
          error(0, errno, "write");
 
475
          error_plus(0, errno, "write");
390
476
          switch(e){
391
477
          case EBADF:
392
478
          case EFAULT:
408
494
      sret = close(STDOUT_FILENO);
409
495
      if(sret == -1){
410
496
        int e = errno;
411
 
        error(0, errno, "close");
 
497
        error_plus(0, errno, "close");
412
498
        switch(e){
413
499
        case EBADF:
414
500
          status = EX_OSFILE;
424
510
    if(sret < 0){
425
511
      int e = errno;
426
512
      if(errno != EINTR and not feof(stdin)){
427
 
        error(0, errno, "getline");
 
513
        error_plus(0, errno, "getline");
428
514
        switch(e){
429
515
        case EBADF:
430
516
          status = EX_UNAVAILABLE;
 
517
          break;
431
518
        case EIO:
432
519
        case EINVAL:
433
520
        default:
437
524
        break;
438
525
      }
439
526
    }
440
 
    /* if(sret == 0), then the only sensible thing to do is to retry to
441
 
       read from stdin */
 
527
    /* if(sret == 0), then the only sensible thing to do is to retry
 
528
       to read from stdin */
442
529
    fputc('\n', stderr);
443
530
    if(debug and not quit_now){
444
531
      /* If quit_now is nonzero, we were interrupted by a signal, and
453
540
    fprintf(stderr, "Restoring terminal attributes\n");
454
541
  }
455
542
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
456
 
    error(0, errno, "tcsetattr+echo");
 
543
    error_plus(0, errno, "tcsetattr+echo");
457
544
  }
458
545
  
459
546
  if(quit_now){
461
548
    old_action.sa_handler = SIG_DFL;
462
549
    ret = sigaction(signal_received, &old_action, NULL);
463
550
    if(ret == -1){
464
 
      error(0, errno, "sigaction");
 
551
      error_plus(0, errno, "sigaction");
465
552
    }
466
553
    raise(signal_received);
467
554
  }