/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

* network-hooks.d: New directory.
* network-hooks.d/bridge: New example hook.
* network-hooks.d/bridge.conf: Config file for bridge example hook.

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