/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: 2011-02-27 17:26:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110227172635-hw1sire7k3vuo1co
Update copyright year to "2011" wherever appropriate.

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-2017 Teddy Hogeborn
6
 
 * Copyright © 2008-2017 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.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,
39
 
                                   ssize_t, open() */
 
38
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t, ssize_t */
40
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
41
40
                                   getenv(), free() */
42
41
#include <dirent.h>             /* scandir(), alphasort() */
43
42
#include <stdio.h>              /* fprintf(), stderr, getline(),
44
 
                                   stdin, feof(), fputc(), vfprintf(),
45
 
                                   vasprintf() */
 
43
                                   stdin, feof(), fputc()
 
44
                                */
46
45
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
47
46
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
48
47
                                */
50
49
#include <iso646.h>             /* or, not */
51
50
#include <stdbool.h>            /* bool, false, true */
52
51
#include <inttypes.h>           /* strtoumax() */
53
 
#include <sys/stat.h>           /* struct stat, lstat(), open() */
54
 
#include <string.h>             /* strlen, rindex, memcmp, strerror()
55
 
                                 */
 
52
#include <sys/stat.h>           /* struct stat, lstat() */
 
53
#include <string.h>             /* strlen, rindex, memcmp */
56
54
#include <argp.h>               /* struct argp_option, struct
57
55
                                   argp_state, struct argp,
58
56
                                   argp_parse(), error_t,
60
58
                                   ARGP_ERR_UNKNOWN */
61
59
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
62
60
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
63
 
#include <fcntl.h>              /* open() */
64
 
#include <stdarg.h>             /* va_list, va_start(), ... */
65
61
 
66
62
volatile sig_atomic_t quit_now = 0;
67
63
int signal_received;
68
64
bool debug = false;
69
65
const char *argp_program_version = "password-prompt " VERSION;
70
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
71
 
 
72
 
/* Needed for conflict resolution */
73
 
const char plymouth_name[] = "plymouthd";
74
 
 
75
 
/* Function to use when printing errors */
76
 
__attribute__((format (gnu_printf, 3, 4)))
77
 
void error_plus(int status, int errnum, const char *formatstring,
78
 
                ...){
79
 
  va_list ap;
80
 
  char *text;
81
 
  int ret;
82
 
  
83
 
  va_start(ap, formatstring);
84
 
  ret = vasprintf(&text, formatstring, ap);
85
 
  if(ret == -1){
86
 
    fprintf(stderr, "Mandos plugin %s: ",
87
 
            program_invocation_short_name);
88
 
    vfprintf(stderr, formatstring, ap);
89
 
    fprintf(stderr, ": %s\n", strerror(errnum));
90
 
    error(status, errno, "vasprintf while printing error");
91
 
    return;
92
 
  }
93
 
  fprintf(stderr, "Mandos plugin ");
94
 
  error(status, errnum, "%s", text);
95
 
  free(text);
96
 
}
 
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
 
97
71
 
98
72
static void termination_handler(int signum){
99
73
  if(quit_now){
105
79
 
106
80
bool conflict_detection(void){
107
81
 
108
 
  /* plymouth conflicts with password-prompt since both want to read
109
 
     from the terminal.  Password-prompt will exit if it detects
110
 
     plymouth since plymouth performs the same functionality.
 
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.
111
85
   */
112
 
  __attribute__((nonnull))
113
86
  int is_plymouth(const struct dirent *proc_entry){
114
87
    int ret;
115
 
    int cl_fd;
116
88
    {
117
 
      uintmax_t proc_id;
 
89
      uintmax_t maxvalue;
118
90
      char *tmp;
119
91
      errno = 0;
120
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
92
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
121
93
      
122
94
      if(errno != 0 or *tmp != '\0'
123
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
95
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
124
96
        return 0;
125
97
      }
126
98
    }
127
 
    
128
 
    char *cmdline_filename;
129
 
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
130
 
                   proc_entry->d_name);
 
99
    char exe_target[sizeof(plymouthd_path)];
 
100
    char *exe_link;
 
101
    ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
131
102
    if(ret == -1){
132
 
      error_plus(0, errno, "asprintf");
 
103
      error(0, errno, "asprintf");
133
104
      return 0;
134
105
    }
135
 
    
136
 
    /* Open /proc/<pid>/cmdline */
137
 
    cl_fd = open(cmdline_filename, O_RDONLY);
138
 
    free(cmdline_filename);
139
 
    if(cl_fd == -1){
 
106
  
 
107
    struct stat exe_stat;
 
108
    ret = lstat(exe_link, &exe_stat);
 
109
    if(ret == -1){
 
110
      free(exe_link);
140
111
      if(errno != ENOENT){
141
 
        error_plus(0, errno, "open");
142
 
      }
143
 
      return 0;
144
 
    }
145
 
    
146
 
    char *cmdline = NULL;
147
 
    {
148
 
      size_t cmdline_len = 0;
149
 
      size_t cmdline_allocated = 0;
150
 
      char *tmp;
151
 
      const size_t blocksize = 1024;
152
 
      ssize_t sret;
153
 
      do {
154
 
        /* Allocate more space? */
155
 
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
156
 
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
157
 
          if(tmp == NULL){
158
 
            error_plus(0, errno, "realloc");
159
 
            free(cmdline);
160
 
            close(cl_fd);
161
 
            return 0;
162
 
          }
163
 
          cmdline = tmp;
164
 
          cmdline_allocated += blocksize;
165
 
        }
166
 
        
167
 
        /* Read data */
168
 
        sret = read(cl_fd, cmdline + cmdline_len,
169
 
                    cmdline_allocated - cmdline_len);
170
 
        if(sret == -1){
171
 
          error_plus(0, errno, "read");
172
 
          free(cmdline);
173
 
          close(cl_fd);
174
 
          return 0;
175
 
        }
176
 
        cmdline_len += (size_t)sret;
177
 
      } while(sret != 0);
178
 
      ret = close(cl_fd);
179
 
      if(ret == -1){
180
 
        error_plus(0, errno, "close");
181
 
        free(cmdline);
182
 
        return 0;
183
 
      }
184
 
      cmdline[cmdline_len] = '\0'; /* Make sure it is terminated */
185
 
    }
186
 
    /* we now have cmdline */
187
 
    
188
 
    /* get basename */
189
 
    char *cmdline_base = strrchr(cmdline, '/');
190
 
    if(cmdline_base != NULL){
191
 
      cmdline_base += 1;                /* skip the slash */
192
 
    } else {
193
 
      cmdline_base = cmdline;
194
 
    }
195
 
    
196
 
    if(strcmp(cmdline_base, plymouth_name) != 0){
197
 
      if(debug){
198
 
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
199
 
                plymouth_name);
200
 
      }
201
 
      free(cmdline);
202
 
      return 0;
203
 
    }
204
 
    if(debug){
205
 
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
206
 
              plymouth_name);
207
 
    }
208
 
    free(cmdline);
 
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
    }
209
131
    return 1;
210
132
  }
211
 
  
212
 
  struct dirent **direntries = NULL;
 
133
 
 
134
  struct dirent **direntries;
213
135
  int ret;
214
136
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
215
 
  if(ret == -1){
216
 
    error_plus(1, errno, "scandir");
217
 
  }
218
 
  {
219
 
    int i = ret;
220
 
    while(i--){
221
 
      free(direntries[i]);
222
 
    }
223
 
  }
224
 
  free(direntries);
225
 
  return ret > 0;
 
137
  if (ret == -1){
 
138
    error(1, errno, "scandir");
 
139
  }
 
140
  if (ret < 0){
 
141
    return 1;
 
142
  } else {
 
143
    return 0;
 
144
  }
226
145
}
227
146
 
228
147
 
256
175
      { .name = NULL }
257
176
    };
258
177
    
259
 
    __attribute__((nonnull(3)))
260
178
    error_t parse_opt (int key, char *arg, struct argp_state *state){
261
179
      errno = 0;
262
180
      switch (key){
298
216
    case ENOMEM:
299
217
    default:
300
218
      errno = ret;
301
 
      error_plus(0, errno, "argp_parse");
 
219
      error(0, errno, "argp_parse");
302
220
      return EX_OSERR;
303
221
    case EINVAL:
304
222
      return EX_USAGE;
309
227
    fprintf(stderr, "Starting %s\n", argv[0]);
310
228
  }
311
229
 
312
 
  if(conflict_detection()){
 
230
  if (conflict_detection()){
313
231
    if(debug){
314
 
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
 
232
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
315
233
    }
316
234
    return EXIT_FAILURE;
317
235
  }
322
240
  
323
241
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
324
242
    int e = errno;
325
 
    error_plus(0, errno, "tcgetattr");
 
243
    error(0, errno, "tcgetattr");
326
244
    switch(e){
327
245
    case EBADF:
328
246
    case ENOTTY:
335
253
  sigemptyset(&new_action.sa_mask);
336
254
  ret = sigaddset(&new_action.sa_mask, SIGINT);
337
255
  if(ret == -1){
338
 
    error_plus(0, errno, "sigaddset");
 
256
    error(0, errno, "sigaddset");
339
257
    return EX_OSERR;
340
258
  }
341
259
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
342
260
  if(ret == -1){
343
 
    error_plus(0, errno, "sigaddset");
 
261
    error(0, errno, "sigaddset");
344
262
    return EX_OSERR;
345
263
  }
346
264
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
347
265
  if(ret == -1){
348
 
    error_plus(0, errno, "sigaddset");
 
266
    error(0, errno, "sigaddset");
349
267
    return EX_OSERR;
350
268
  }
351
269
  /* Need to check if the handler is SIG_IGN before handling:
354
272
  */
355
273
  ret = sigaction(SIGINT, NULL, &old_action);
356
274
  if(ret == -1){
357
 
    error_plus(0, errno, "sigaction");
 
275
    error(0, errno, "sigaction");
358
276
    return EX_OSERR;
359
277
  }
360
278
  if(old_action.sa_handler != SIG_IGN){
361
279
    ret = sigaction(SIGINT, &new_action, NULL);
362
280
    if(ret == -1){
363
 
      error_plus(0, errno, "sigaction");
 
281
      error(0, errno, "sigaction");
364
282
      return EX_OSERR;
365
283
    }
366
284
  }
367
285
  ret = sigaction(SIGHUP, NULL, &old_action);
368
286
  if(ret == -1){
369
 
    error_plus(0, errno, "sigaction");
 
287
    error(0, errno, "sigaction");
370
288
    return EX_OSERR;
371
289
  }
372
290
  if(old_action.sa_handler != SIG_IGN){
373
291
    ret = sigaction(SIGHUP, &new_action, NULL);
374
292
    if(ret == -1){
375
 
      error_plus(0, errno, "sigaction");
 
293
      error(0, errno, "sigaction");
376
294
      return EX_OSERR;
377
295
    }
378
296
  }
379
297
  ret = sigaction(SIGTERM, NULL, &old_action);
380
298
  if(ret == -1){
381
 
    error_plus(0, errno, "sigaction");
 
299
    error(0, errno, "sigaction");
382
300
    return EX_OSERR;
383
301
  }
384
302
  if(old_action.sa_handler != SIG_IGN){
385
303
    ret = sigaction(SIGTERM, &new_action, NULL);
386
304
    if(ret == -1){
387
 
      error_plus(0, errno, "sigaction");
 
305
      error(0, errno, "sigaction");
388
306
      return EX_OSERR;
389
307
    }
390
308
  }
398
316
  t_new.c_lflag &= ~(tcflag_t)ECHO;
399
317
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
400
318
    int e = errno;
401
 
    error_plus(0, errno, "tcsetattr-echo");
 
319
    error(0, errno, "tcsetattr-echo");
402
320
    switch(e){
403
321
    case EBADF:
404
322
    case ENOTTY:
468
386
        sret = write(STDOUT_FILENO, buffer + written, n - written);
469
387
        if(sret < 0){
470
388
          int e = errno;
471
 
          error_plus(0, errno, "write");
 
389
          error(0, errno, "write");
472
390
          switch(e){
473
391
          case EBADF:
474
392
          case EFAULT:
490
408
      sret = close(STDOUT_FILENO);
491
409
      if(sret == -1){
492
410
        int e = errno;
493
 
        error_plus(0, errno, "close");
 
411
        error(0, errno, "close");
494
412
        switch(e){
495
413
        case EBADF:
496
414
          status = EX_OSFILE;
506
424
    if(sret < 0){
507
425
      int e = errno;
508
426
      if(errno != EINTR and not feof(stdin)){
509
 
        error_plus(0, errno, "getline");
 
427
        error(0, errno, "getline");
510
428
        switch(e){
511
429
        case EBADF:
512
430
          status = EX_UNAVAILABLE;
513
 
          break;
514
431
        case EIO:
515
432
        case EINVAL:
516
433
        default:
520
437
        break;
521
438
      }
522
439
    }
523
 
    /* if(sret == 0), then the only sensible thing to do is to retry
524
 
       to read from stdin */
 
440
    /* if(sret == 0), then the only sensible thing to do is to retry to
 
441
       read from stdin */
525
442
    fputc('\n', stderr);
526
443
    if(debug and not quit_now){
527
444
      /* If quit_now is nonzero, we were interrupted by a signal, and
536
453
    fprintf(stderr, "Restoring terminal attributes\n");
537
454
  }
538
455
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
539
 
    error_plus(0, errno, "tcsetattr+echo");
 
456
    error(0, errno, "tcsetattr+echo");
540
457
  }
541
458
  
542
459
  if(quit_now){
544
461
    old_action.sa_handler = SIG_DFL;
545
462
    ret = sigaction(signal_received, &old_action, NULL);
546
463
    if(ret == -1){
547
 
      error_plus(0, errno, "sigaction");
 
464
      error(0, errno, "sigaction");
548
465
    }
549
466
    raise(signal_received);
550
467
  }