/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 at bsnet
  • Date: 2011-12-24 23:17:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20111224231702-1ffgu6r02p0bz9co
* plugins.d/splashy.c (error_plus): Check format string.
* plugins.d/askpass-fifo.c (error_plus): - '' -
* plugins.d/plymouth.c (error_plus): - '' -
* plugins.d/password-prompt.c (error_plus): - '' -
* plugins.d/usplash.c (error_plus): - '' -

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-2011 Teddy Hogeborn
 
6
 * Copyright © 2008-2011 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
82
82
  
83
83
  va_start(ap, formatstring);
84
84
  ret = vasprintf(&text, formatstring, ap);
85
 
  if(ret == -1){
 
85
  if (ret == -1){
86
86
    fprintf(stderr, "Mandos plugin %s: ",
87
87
            program_invocation_short_name);
88
88
    vfprintf(stderr, formatstring, ap);
89
 
    fprintf(stderr, ": %s\n", strerror(errnum));
 
89
    fprintf(stderr, ": ");
 
90
    fprintf(stderr, "%s\n", strerror(errnum));
90
91
    error(status, errno, "vasprintf while printing error");
91
92
    return;
92
93
  }
109
110
     from the terminal.  Password-prompt will exit if it detects
110
111
     plymouth since plymouth performs the same functionality.
111
112
   */
112
 
  __attribute__((nonnull))
113
113
  int is_plymouth(const struct dirent *proc_entry){
114
114
    int ret;
115
115
    int cl_fd;
116
116
    {
117
 
      uintmax_t proc_id;
 
117
      uintmax_t maxvalue;
118
118
      char *tmp;
119
119
      errno = 0;
120
 
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
120
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
121
121
      
122
122
      if(errno != 0 or *tmp != '\0'
123
 
         or proc_id != (uintmax_t)((pid_t)proc_id)){
 
123
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
124
124
        return 0;
125
125
      }
126
126
    }
129
129
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
130
130
                   proc_entry->d_name);
131
131
    if(ret == -1){
132
 
      error_plus(0, errno, "asprintf");
 
132
      error(0, errno, "asprintf");
133
133
      return 0;
134
134
    }
135
135
    
138
138
    free(cmdline_filename);
139
139
    if(cl_fd == -1){
140
140
      if(errno != ENOENT){
141
 
        error_plus(0, errno, "open");
 
141
        error(0, errno, "open");
142
142
      }
143
143
      return 0;
144
144
    }
155
155
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
156
156
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
157
157
          if(tmp == NULL){
158
 
            error_plus(0, errno, "realloc");
 
158
            error(0, errno, "realloc");
159
159
            free(cmdline);
160
160
            close(cl_fd);
161
161
            return 0;
168
168
        sret = read(cl_fd, cmdline + cmdline_len,
169
169
                    cmdline_allocated - cmdline_len);
170
170
        if(sret == -1){
171
 
          error_plus(0, errno, "read");
 
171
          error(0, errno, "read");
172
172
          free(cmdline);
173
173
          close(cl_fd);
174
174
          return 0;
177
177
      } while(sret != 0);
178
178
      ret = close(cl_fd);
179
179
      if(ret == -1){
180
 
        error_plus(0, errno, "close");
 
180
        error(0, errno, "close");
181
181
        free(cmdline);
182
182
        return 0;
183
183
      }
212
212
  struct dirent **direntries = NULL;
213
213
  int ret;
214
214
  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
 
    }
 
215
  if (ret == -1){
 
216
    error(1, errno, "scandir");
223
217
  }
224
218
  free(direntries);
225
219
  return ret > 0;
256
250
      { .name = NULL }
257
251
    };
258
252
    
259
 
    __attribute__((nonnull(3)))
260
253
    error_t parse_opt (int key, char *arg, struct argp_state *state){
261
254
      errno = 0;
262
255
      switch (key){
298
291
    case ENOMEM:
299
292
    default:
300
293
      errno = ret;
301
 
      error_plus(0, errno, "argp_parse");
 
294
      error(0, errno, "argp_parse");
302
295
      return EX_OSERR;
303
296
    case EINVAL:
304
297
      return EX_USAGE;
309
302
    fprintf(stderr, "Starting %s\n", argv[0]);
310
303
  }
311
304
 
312
 
  if(conflict_detection()){
 
305
  if (conflict_detection()){
313
306
    if(debug){
314
307
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
315
308
    }
322
315
  
323
316
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
324
317
    int e = errno;
325
 
    error_plus(0, errno, "tcgetattr");
 
318
    error(0, errno, "tcgetattr");
326
319
    switch(e){
327
320
    case EBADF:
328
321
    case ENOTTY:
335
328
  sigemptyset(&new_action.sa_mask);
336
329
  ret = sigaddset(&new_action.sa_mask, SIGINT);
337
330
  if(ret == -1){
338
 
    error_plus(0, errno, "sigaddset");
 
331
    error(0, errno, "sigaddset");
339
332
    return EX_OSERR;
340
333
  }
341
334
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
342
335
  if(ret == -1){
343
 
    error_plus(0, errno, "sigaddset");
 
336
    error(0, errno, "sigaddset");
344
337
    return EX_OSERR;
345
338
  }
346
339
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
347
340
  if(ret == -1){
348
 
    error_plus(0, errno, "sigaddset");
 
341
    error(0, errno, "sigaddset");
349
342
    return EX_OSERR;
350
343
  }
351
344
  /* Need to check if the handler is SIG_IGN before handling:
354
347
  */
355
348
  ret = sigaction(SIGINT, NULL, &old_action);
356
349
  if(ret == -1){
357
 
    error_plus(0, errno, "sigaction");
 
350
    error(0, errno, "sigaction");
358
351
    return EX_OSERR;
359
352
  }
360
353
  if(old_action.sa_handler != SIG_IGN){
361
354
    ret = sigaction(SIGINT, &new_action, NULL);
362
355
    if(ret == -1){
363
 
      error_plus(0, errno, "sigaction");
 
356
      error(0, errno, "sigaction");
364
357
      return EX_OSERR;
365
358
    }
366
359
  }
367
360
  ret = sigaction(SIGHUP, NULL, &old_action);
368
361
  if(ret == -1){
369
 
    error_plus(0, errno, "sigaction");
 
362
    error(0, errno, "sigaction");
370
363
    return EX_OSERR;
371
364
  }
372
365
  if(old_action.sa_handler != SIG_IGN){
373
366
    ret = sigaction(SIGHUP, &new_action, NULL);
374
367
    if(ret == -1){
375
 
      error_plus(0, errno, "sigaction");
 
368
      error(0, errno, "sigaction");
376
369
      return EX_OSERR;
377
370
    }
378
371
  }
379
372
  ret = sigaction(SIGTERM, NULL, &old_action);
380
373
  if(ret == -1){
381
 
    error_plus(0, errno, "sigaction");
 
374
    error(0, errno, "sigaction");
382
375
    return EX_OSERR;
383
376
  }
384
377
  if(old_action.sa_handler != SIG_IGN){
385
378
    ret = sigaction(SIGTERM, &new_action, NULL);
386
379
    if(ret == -1){
387
 
      error_plus(0, errno, "sigaction");
 
380
      error(0, errno, "sigaction");
388
381
      return EX_OSERR;
389
382
    }
390
383
  }
398
391
  t_new.c_lflag &= ~(tcflag_t)ECHO;
399
392
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
400
393
    int e = errno;
401
 
    error_plus(0, errno, "tcsetattr-echo");
 
394
    error(0, errno, "tcsetattr-echo");
402
395
    switch(e){
403
396
    case EBADF:
404
397
    case ENOTTY:
468
461
        sret = write(STDOUT_FILENO, buffer + written, n - written);
469
462
        if(sret < 0){
470
463
          int e = errno;
471
 
          error_plus(0, errno, "write");
 
464
          error(0, errno, "write");
472
465
          switch(e){
473
466
          case EBADF:
474
467
          case EFAULT:
490
483
      sret = close(STDOUT_FILENO);
491
484
      if(sret == -1){
492
485
        int e = errno;
493
 
        error_plus(0, errno, "close");
 
486
        error(0, errno, "close");
494
487
        switch(e){
495
488
        case EBADF:
496
489
          status = EX_OSFILE;
506
499
    if(sret < 0){
507
500
      int e = errno;
508
501
      if(errno != EINTR and not feof(stdin)){
509
 
        error_plus(0, errno, "getline");
 
502
        error(0, errno, "getline");
510
503
        switch(e){
511
504
        case EBADF:
512
505
          status = EX_UNAVAILABLE;
513
 
          break;
514
506
        case EIO:
515
507
        case EINVAL:
516
508
        default:
536
528
    fprintf(stderr, "Restoring terminal attributes\n");
537
529
  }
538
530
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
539
 
    error_plus(0, errno, "tcsetattr+echo");
 
531
    error(0, errno, "tcsetattr+echo");
540
532
  }
541
533
  
542
534
  if(quit_now){
544
536
    old_action.sa_handler = SIG_DFL;
545
537
    ret = sigaction(signal_received, &old_action, NULL);
546
538
    if(ret == -1){
547
 
      error_plus(0, errno, "sigaction");
 
539
      error(0, errno, "sigaction");
548
540
    }
549
541
    raise(signal_received);
550
542
  }