/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-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

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-2012 Teddy Hogeborn
6
 
 * Copyright © 2008-2012 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() */
67
67
int signal_received;
68
68
bool debug = false;
69
69
const char *argp_program_version = "password-prompt " VERSION;
70
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
71
 
72
72
/* Needed for conflict resolution */
73
73
const char plymouth_name[] = "plymouthd";
74
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
75
/* Function to use when printing errors */
86
 
__attribute__((format (gnu_printf, 3, 4)))
87
76
void error_plus(int status, int errnum, const char *formatstring,
88
77
                ...){
89
78
  va_list ap;
96
85
    fprintf(stderr, "Mandos plugin %s: ",
97
86
            program_invocation_short_name);
98
87
    vfprintf(stderr, formatstring, ap);
99
 
    fprintf(stderr, ": %s\n", strerror(errnum));
 
88
    fprintf(stderr, ": ");
 
89
    fprintf(stderr, "%s\n", strerror(errnum));
100
90
    error(status, errno, "vasprintf while printing error");
101
91
    return;
102
92
  }
119
109
     from the terminal.  Password-prompt will exit if it detects
120
110
     plymouth since plymouth performs the same functionality.
121
111
   */
122
 
  __attribute__((nonnull))
123
112
  int is_plymouth(const struct dirent *proc_entry){
124
113
    int ret;
125
114
    int cl_fd;
126
115
    {
127
 
      uintmax_t proc_id;
 
116
      uintmax_t maxvalue;
128
117
      char *tmp;
129
118
      errno = 0;
130
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
119
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
131
120
      
132
121
      if(errno != 0 or *tmp != '\0'
133
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
122
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
134
123
        return 0;
135
124
      }
136
125
    }
139
128
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
140
129
                   proc_entry->d_name);
141
130
    if(ret == -1){
142
 
      error_plus(0, errno, "asprintf");
 
131
      error(0, errno, "asprintf");
143
132
      return 0;
144
133
    }
145
134
    
148
137
    free(cmdline_filename);
149
138
    if(cl_fd == -1){
150
139
      if(errno != ENOENT){
151
 
        error_plus(0, errno, "open");
 
140
        error(0, errno, "open");
152
141
      }
153
142
      return 0;
154
143
    }
165
154
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
166
155
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
167
156
          if(tmp == NULL){
168
 
            error_plus(0, errno, "realloc");
 
157
            error(0, errno, "realloc");
169
158
            free(cmdline);
170
159
            close(cl_fd);
171
160
            return 0;
178
167
        sret = read(cl_fd, cmdline + cmdline_len,
179
168
                    cmdline_allocated - cmdline_len);
180
169
        if(sret == -1){
181
 
          error_plus(0, errno, "read");
 
170
          error(0, errno, "read");
182
171
          free(cmdline);
183
172
          close(cl_fd);
184
173
          return 0;
187
176
      } while(sret != 0);
188
177
      ret = close(cl_fd);
189
178
      if(ret == -1){
190
 
        error_plus(0, errno, "close");
 
179
        error(0, errno, "close");
191
180
        free(cmdline);
192
181
        return 0;
193
182
      }
223
212
  int ret;
224
213
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
225
214
  if (ret == -1){
226
 
    error_plus(1, errno, "scandir");
 
215
    error(1, errno, "scandir");
227
216
  }
228
217
  free(direntries);
229
218
  return ret > 0;
260
249
      { .name = NULL }
261
250
    };
262
251
    
263
 
    __attribute__((nonnull(3)))
264
252
    error_t parse_opt (int key, char *arg, struct argp_state *state){
265
253
      errno = 0;
266
254
      switch (key){
302
290
    case ENOMEM:
303
291
    default:
304
292
      errno = ret;
305
 
      error_plus(0, errno, "argp_parse");
 
293
      error(0, errno, "argp_parse");
306
294
      return EX_OSERR;
307
295
    case EINVAL:
308
296
      return EX_USAGE;
326
314
  
327
315
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
328
316
    int e = errno;
329
 
    error_plus(0, errno, "tcgetattr");
 
317
    error(0, errno, "tcgetattr");
330
318
    switch(e){
331
319
    case EBADF:
332
320
    case ENOTTY:
339
327
  sigemptyset(&new_action.sa_mask);
340
328
  ret = sigaddset(&new_action.sa_mask, SIGINT);
341
329
  if(ret == -1){
342
 
    error_plus(0, errno, "sigaddset");
 
330
    error(0, errno, "sigaddset");
343
331
    return EX_OSERR;
344
332
  }
345
333
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
346
334
  if(ret == -1){
347
 
    error_plus(0, errno, "sigaddset");
 
335
    error(0, errno, "sigaddset");
348
336
    return EX_OSERR;
349
337
  }
350
338
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
351
339
  if(ret == -1){
352
 
    error_plus(0, errno, "sigaddset");
 
340
    error(0, errno, "sigaddset");
353
341
    return EX_OSERR;
354
342
  }
355
343
  /* Need to check if the handler is SIG_IGN before handling:
358
346
  */
359
347
  ret = sigaction(SIGINT, NULL, &old_action);
360
348
  if(ret == -1){
361
 
    error_plus(0, errno, "sigaction");
 
349
    error(0, errno, "sigaction");
362
350
    return EX_OSERR;
363
351
  }
364
352
  if(old_action.sa_handler != SIG_IGN){
365
353
    ret = sigaction(SIGINT, &new_action, NULL);
366
354
    if(ret == -1){
367
 
      error_plus(0, errno, "sigaction");
 
355
      error(0, errno, "sigaction");
368
356
      return EX_OSERR;
369
357
    }
370
358
  }
371
359
  ret = sigaction(SIGHUP, NULL, &old_action);
372
360
  if(ret == -1){
373
 
    error_plus(0, errno, "sigaction");
 
361
    error(0, errno, "sigaction");
374
362
    return EX_OSERR;
375
363
  }
376
364
  if(old_action.sa_handler != SIG_IGN){
377
365
    ret = sigaction(SIGHUP, &new_action, NULL);
378
366
    if(ret == -1){
379
 
      error_plus(0, errno, "sigaction");
 
367
      error(0, errno, "sigaction");
380
368
      return EX_OSERR;
381
369
    }
382
370
  }
383
371
  ret = sigaction(SIGTERM, NULL, &old_action);
384
372
  if(ret == -1){
385
 
    error_plus(0, errno, "sigaction");
 
373
    error(0, errno, "sigaction");
386
374
    return EX_OSERR;
387
375
  }
388
376
  if(old_action.sa_handler != SIG_IGN){
389
377
    ret = sigaction(SIGTERM, &new_action, NULL);
390
378
    if(ret == -1){
391
 
      error_plus(0, errno, "sigaction");
 
379
      error(0, errno, "sigaction");
392
380
      return EX_OSERR;
393
381
    }
394
382
  }
402
390
  t_new.c_lflag &= ~(tcflag_t)ECHO;
403
391
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
404
392
    int e = errno;
405
 
    error_plus(0, errno, "tcsetattr-echo");
 
393
    error(0, errno, "tcsetattr-echo");
406
394
    switch(e){
407
395
    case EBADF:
408
396
    case ENOTTY:
472
460
        sret = write(STDOUT_FILENO, buffer + written, n - written);
473
461
        if(sret < 0){
474
462
          int e = errno;
475
 
          error_plus(0, errno, "write");
 
463
          error(0, errno, "write");
476
464
          switch(e){
477
465
          case EBADF:
478
466
          case EFAULT:
494
482
      sret = close(STDOUT_FILENO);
495
483
      if(sret == -1){
496
484
        int e = errno;
497
 
        error_plus(0, errno, "close");
 
485
        error(0, errno, "close");
498
486
        switch(e){
499
487
        case EBADF:
500
488
          status = EX_OSFILE;
510
498
    if(sret < 0){
511
499
      int e = errno;
512
500
      if(errno != EINTR and not feof(stdin)){
513
 
        error_plus(0, errno, "getline");
 
501
        error(0, errno, "getline");
514
502
        switch(e){
515
503
        case EBADF:
516
504
          status = EX_UNAVAILABLE;
517
 
          break;
518
505
        case EIO:
519
506
        case EINVAL:
520
507
        default:
540
527
    fprintf(stderr, "Restoring terminal attributes\n");
541
528
  }
542
529
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
543
 
    error_plus(0, errno, "tcsetattr+echo");
 
530
    error(0, errno, "tcsetattr+echo");
544
531
  }
545
532
  
546
533
  if(quit_now){
548
535
    old_action.sa_handler = SIG_DFL;
549
536
    ret = sigaction(signal_received, &old_action, NULL);
550
537
    if(ret == -1){
551
 
      error_plus(0, errno, "sigaction");
 
538
      error(0, errno, "sigaction");
552
539
    }
553
540
    raise(signal_received);
554
541
  }