/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: Björn Påhlsson
  • Date: 2011-02-27 15:45:24 UTC
  • mto: This revision was merged to the branch mainline in revision 467.
  • Revision ID: belorn@fukt.bsnet.se-20110227154524-zac8443jod8v1g1g
fixed bug with conflict detection between plymouthd and password-prompt

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-2018 Teddy Hogeborn
6
 
 * Copyright © 2008-2018 Björn Påhlsson
7
 
 * 
8
 
 * This file is part of Mandos.
9
 
 * 
10
 
 * Mandos is free software: you can redistribute it and/or modify it
11
 
 * under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 * 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 Björn Påhlsson
 
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
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
25
#define _GNU_SOURCE             /* getline(), asprintf() */
27
26
 
28
 
#include <termios.h>            /* struct termios, tcsetattr(),
 
27
#include <termios.h>            /* struct termios, tcsetattr(),
29
28
                                   TCSAFLUSH, tcgetattr(), ECHO */
30
29
#include <unistd.h>             /* struct termios, tcsetattr(),
31
30
                                   STDIN_FILENO, TCSAFLUSH,
42
41
                                   getenv(), free() */
43
42
#include <dirent.h>             /* scandir(), alphasort() */
44
43
#include <stdio.h>              /* fprintf(), stderr, getline(),
45
 
                                   stdin, feof(), fputc(), vfprintf(),
46
 
                                   vasprintf() */
 
44
                                   stdin, feof(), fputc()
 
45
                                */
47
46
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
48
47
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
49
48
                                */
51
50
#include <iso646.h>             /* or, not */
52
51
#include <stdbool.h>            /* bool, false, true */
53
52
#include <inttypes.h>           /* strtoumax() */
54
 
#include <sys/stat.h>           /* struct stat, lstat(), open() */
55
 
#include <string.h>             /* strlen, rindex, memcmp, strerror()
56
 
                                 */
 
53
#include <sys/stat.h>           /* struct stat, lstat(), open() */
 
54
#include <string.h>             /* strlen, rindex, memcmp */
57
55
#include <argp.h>               /* struct argp_option, struct
58
56
                                   argp_state, struct argp,
59
57
                                   argp_parse(), error_t,
62
60
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
63
61
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
64
62
#include <fcntl.h>              /* open() */
65
 
#include <stdarg.h>             /* va_list, va_start(), ... */
66
63
 
67
64
volatile sig_atomic_t quit_now = 0;
68
65
int signal_received;
69
66
bool debug = false;
70
67
const char *argp_program_version = "password-prompt " VERSION;
71
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
72
 
 
73
 
/* Needed for conflict resolution */
74
 
const char plymouth_name[] = "plymouthd";
75
 
 
76
 
/* Function to use when printing errors */
77
 
__attribute__((format (gnu_printf, 3, 4)))
78
 
void error_plus(int status, int errnum, const char *formatstring,
79
 
                ...){
80
 
  va_list ap;
81
 
  char *text;
82
 
  int ret;
83
 
  
84
 
  va_start(ap, formatstring);
85
 
  ret = vasprintf(&text, formatstring, ap);
86
 
  if(ret == -1){
87
 
    fprintf(stderr, "Mandos plugin %s: ",
88
 
            program_invocation_short_name);
89
 
    vfprintf(stderr, formatstring, ap);
90
 
    fprintf(stderr, ": %s\n", strerror(errnum));
91
 
    error(status, errno, "vasprintf while printing error");
92
 
    return;
93
 
  }
94
 
  fprintf(stderr, "Mandos plugin ");
95
 
  error(status, errnum, "%s", text);
96
 
  free(text);
97
 
}
 
68
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
69
 
 
70
/* Needed for conflic resolution */
 
71
const char plymouthd_name[] = "plymouthd";
 
72
 
98
73
 
99
74
static void termination_handler(int signum){
100
75
  if(quit_now){
110
85
     from the terminal.  Password-prompt will exit if it detects
111
86
     plymouth since plymouth performs the same functionality.
112
87
   */
113
 
  __attribute__((nonnull))
114
88
  int is_plymouth(const struct dirent *proc_entry){
115
89
    int ret;
116
90
    int cl_fd;
117
91
    {
118
 
      uintmax_t proc_id;
 
92
      uintmax_t maxvalue;
119
93
      char *tmp;
120
94
      errno = 0;
121
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
95
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
122
96
      
123
97
      if(errno != 0 or *tmp != '\0'
124
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
98
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
125
99
        return 0;
126
100
      }
127
101
    }
128
102
    
129
103
    char *cmdline_filename;
130
 
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
131
 
                   proc_entry->d_name);
 
104
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline", proc_entry->d_name);
132
105
    if(ret == -1){
133
 
      error_plus(0, errno, "asprintf");
 
106
      error(0, errno, "asprintf");
134
107
      return 0;
135
108
    }
136
109
    
137
 
    /* Open /proc/<pid>/cmdline */
 
110
    /* Open /proc/<pid>/cmdline  */
138
111
    cl_fd = open(cmdline_filename, O_RDONLY);
139
112
    free(cmdline_filename);
140
113
    if(cl_fd == -1){
141
 
      if(errno != ENOENT){
142
 
        error_plus(0, errno, "open");
143
 
      }
 
114
      error(0, errno, "open");
144
115
      return 0;
145
116
    }
146
117
    
156
127
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
157
128
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
158
129
          if(tmp == NULL){
159
 
            error_plus(0, errno, "realloc");
 
130
            error(0, errno, "realloc");
160
131
            free(cmdline);
161
132
            close(cl_fd);
162
133
            return 0;
169
140
        sret = read(cl_fd, cmdline + cmdline_len,
170
141
                    cmdline_allocated - cmdline_len);
171
142
        if(sret == -1){
172
 
          error_plus(0, errno, "read");
 
143
          error(0, errno, "read");
173
144
          free(cmdline);
174
145
          close(cl_fd);
175
146
          return 0;
178
149
      } while(sret != 0);
179
150
      ret = close(cl_fd);
180
151
      if(ret == -1){
181
 
        error_plus(0, errno, "close");
 
152
        error(0, errno, "close");
182
153
        free(cmdline);
183
154
        return 0;
184
155
      }
194
165
      cmdline_base = cmdline;
195
166
    }
196
167
    
197
 
    if(strcmp(cmdline_base, plymouth_name) != 0){
198
 
      if(debug){
199
 
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
200
 
                plymouth_name);
201
 
      }
 
168
    if(strcmp(cmdline_base, plymouthd_name) != 0){
202
169
      free(cmdline);
203
170
      return 0;
204
171
    }
205
 
    if(debug){
206
 
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
207
 
              plymouth_name);
208
 
    }
209
172
    free(cmdline);
210
173
    return 1;
211
174
  }
212
 
  
213
 
  struct dirent **direntries = NULL;
 
175
 
 
176
  struct dirent **direntries;
214
177
  int ret;
215
178
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
216
 
  if(ret == -1){
217
 
    error_plus(1, errno, "scandir");
218
 
  }
219
 
  {
220
 
    int i = ret;
221
 
    while(i--){
222
 
      free(direntries[i]);
223
 
    }
224
 
  }
225
 
  free(direntries);
226
 
  return ret > 0;
 
179
  if (ret == -1){
 
180
    error(1, errno, "scandir");
 
181
  }
 
182
  if (ret > 0){
 
183
    return true;
 
184
  } else {
 
185
    return false;
 
186
  }
227
187
}
228
188
 
229
189
 
257
217
      { .name = NULL }
258
218
    };
259
219
    
260
 
    __attribute__((nonnull(3)))
261
220
    error_t parse_opt (int key, char *arg, struct argp_state *state){
262
221
      errno = 0;
263
222
      switch (key){
299
258
    case ENOMEM:
300
259
    default:
301
260
      errno = ret;
302
 
      error_plus(0, errno, "argp_parse");
 
261
      error(0, errno, "argp_parse");
303
262
      return EX_OSERR;
304
263
    case EINVAL:
305
264
      return EX_USAGE;
310
269
    fprintf(stderr, "Starting %s\n", argv[0]);
311
270
  }
312
271
 
313
 
  if(conflict_detection()){
 
272
  if (conflict_detection()){
314
273
    if(debug){
315
 
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
 
274
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
316
275
    }
317
276
    return EXIT_FAILURE;
318
277
  }
323
282
  
324
283
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
325
284
    int e = errno;
326
 
    error_plus(0, errno, "tcgetattr");
 
285
    error(0, errno, "tcgetattr");
327
286
    switch(e){
328
287
    case EBADF:
329
288
    case ENOTTY:
336
295
  sigemptyset(&new_action.sa_mask);
337
296
  ret = sigaddset(&new_action.sa_mask, SIGINT);
338
297
  if(ret == -1){
339
 
    error_plus(0, errno, "sigaddset");
 
298
    error(0, errno, "sigaddset");
340
299
    return EX_OSERR;
341
300
  }
342
301
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
343
302
  if(ret == -1){
344
 
    error_plus(0, errno, "sigaddset");
 
303
    error(0, errno, "sigaddset");
345
304
    return EX_OSERR;
346
305
  }
347
306
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
348
307
  if(ret == -1){
349
 
    error_plus(0, errno, "sigaddset");
 
308
    error(0, errno, "sigaddset");
350
309
    return EX_OSERR;
351
310
  }
352
311
  /* Need to check if the handler is SIG_IGN before handling:
355
314
  */
356
315
  ret = sigaction(SIGINT, NULL, &old_action);
357
316
  if(ret == -1){
358
 
    error_plus(0, errno, "sigaction");
 
317
    error(0, errno, "sigaction");
359
318
    return EX_OSERR;
360
319
  }
361
320
  if(old_action.sa_handler != SIG_IGN){
362
321
    ret = sigaction(SIGINT, &new_action, NULL);
363
322
    if(ret == -1){
364
 
      error_plus(0, errno, "sigaction");
 
323
      error(0, errno, "sigaction");
365
324
      return EX_OSERR;
366
325
    }
367
326
  }
368
327
  ret = sigaction(SIGHUP, NULL, &old_action);
369
328
  if(ret == -1){
370
 
    error_plus(0, errno, "sigaction");
 
329
    error(0, errno, "sigaction");
371
330
    return EX_OSERR;
372
331
  }
373
332
  if(old_action.sa_handler != SIG_IGN){
374
333
    ret = sigaction(SIGHUP, &new_action, NULL);
375
334
    if(ret == -1){
376
 
      error_plus(0, errno, "sigaction");
 
335
      error(0, errno, "sigaction");
377
336
      return EX_OSERR;
378
337
    }
379
338
  }
380
339
  ret = sigaction(SIGTERM, NULL, &old_action);
381
340
  if(ret == -1){
382
 
    error_plus(0, errno, "sigaction");
 
341
    error(0, errno, "sigaction");
383
342
    return EX_OSERR;
384
343
  }
385
344
  if(old_action.sa_handler != SIG_IGN){
386
345
    ret = sigaction(SIGTERM, &new_action, NULL);
387
346
    if(ret == -1){
388
 
      error_plus(0, errno, "sigaction");
 
347
      error(0, errno, "sigaction");
389
348
      return EX_OSERR;
390
349
    }
391
350
  }
399
358
  t_new.c_lflag &= ~(tcflag_t)ECHO;
400
359
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
401
360
    int e = errno;
402
 
    error_plus(0, errno, "tcsetattr-echo");
 
361
    error(0, errno, "tcsetattr-echo");
403
362
    switch(e){
404
363
    case EBADF:
405
364
    case ENOTTY:
469
428
        sret = write(STDOUT_FILENO, buffer + written, n - written);
470
429
        if(sret < 0){
471
430
          int e = errno;
472
 
          error_plus(0, errno, "write");
 
431
          error(0, errno, "write");
473
432
          switch(e){
474
433
          case EBADF:
475
434
          case EFAULT:
491
450
      sret = close(STDOUT_FILENO);
492
451
      if(sret == -1){
493
452
        int e = errno;
494
 
        error_plus(0, errno, "close");
 
453
        error(0, errno, "close");
495
454
        switch(e){
496
455
        case EBADF:
497
456
          status = EX_OSFILE;
507
466
    if(sret < 0){
508
467
      int e = errno;
509
468
      if(errno != EINTR and not feof(stdin)){
510
 
        error_plus(0, errno, "getline");
 
469
        error(0, errno, "getline");
511
470
        switch(e){
512
471
        case EBADF:
513
472
          status = EX_UNAVAILABLE;
514
 
          break;
515
473
        case EIO:
516
474
        case EINVAL:
517
475
        default:
521
479
        break;
522
480
      }
523
481
    }
524
 
    /* if(sret == 0), then the only sensible thing to do is to retry
525
 
       to read from stdin */
 
482
    /* if(sret == 0), then the only sensible thing to do is to retry to
 
483
       read from stdin */
526
484
    fputc('\n', stderr);
527
485
    if(debug and not quit_now){
528
486
      /* If quit_now is nonzero, we were interrupted by a signal, and
537
495
    fprintf(stderr, "Restoring terminal attributes\n");
538
496
  }
539
497
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
540
 
    error_plus(0, errno, "tcsetattr+echo");
 
498
    error(0, errno, "tcsetattr+echo");
541
499
  }
542
500
  
543
501
  if(quit_now){
545
503
    old_action.sa_handler = SIG_DFL;
546
504
    ret = sigaction(signal_received, &old_action, NULL);
547
505
    if(ret == -1){
548
 
      error_plus(0, errno, "sigaction");
 
506
      error(0, errno, "sigaction");
549
507
    }
550
508
    raise(signal_received);
551
509
  }