/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-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
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");
217
 
  }
218
 
  {
219
 
    int i = ret;
220
 
    while(i--){
221
 
      free(direntries[i]);
222
 
    }
 
214
  if (ret == -1){
 
215
    error(1, errno, "scandir");
223
216
  }
224
217
  free(direntries);
225
218
  return ret > 0;
256
249
      { .name = NULL }
257
250
    };
258
251
    
259
 
    __attribute__((nonnull(3)))
260
252
    error_t parse_opt (int key, char *arg, struct argp_state *state){
261
253
      errno = 0;
262
254
      switch (key){
298
290
    case ENOMEM:
299
291
    default:
300
292
      errno = ret;
301
 
      error_plus(0, errno, "argp_parse");
 
293
      error(0, errno, "argp_parse");
302
294
      return EX_OSERR;
303
295
    case EINVAL:
304
296
      return EX_USAGE;
309
301
    fprintf(stderr, "Starting %s\n", argv[0]);
310
302
  }
311
303
 
312
 
  if(conflict_detection()){
 
304
  if (conflict_detection()){
313
305
    if(debug){
314
306
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
315
307
    }
322
314
  
323
315
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
324
316
    int e = errno;
325
 
    error_plus(0, errno, "tcgetattr");
 
317
    error(0, errno, "tcgetattr");
326
318
    switch(e){
327
319
    case EBADF:
328
320
    case ENOTTY:
335
327
  sigemptyset(&new_action.sa_mask);
336
328
  ret = sigaddset(&new_action.sa_mask, SIGINT);
337
329
  if(ret == -1){
338
 
    error_plus(0, errno, "sigaddset");
 
330
    error(0, errno, "sigaddset");
339
331
    return EX_OSERR;
340
332
  }
341
333
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
342
334
  if(ret == -1){
343
 
    error_plus(0, errno, "sigaddset");
 
335
    error(0, errno, "sigaddset");
344
336
    return EX_OSERR;
345
337
  }
346
338
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
347
339
  if(ret == -1){
348
 
    error_plus(0, errno, "sigaddset");
 
340
    error(0, errno, "sigaddset");
349
341
    return EX_OSERR;
350
342
  }
351
343
  /* Need to check if the handler is SIG_IGN before handling:
354
346
  */
355
347
  ret = sigaction(SIGINT, NULL, &old_action);
356
348
  if(ret == -1){
357
 
    error_plus(0, errno, "sigaction");
 
349
    error(0, errno, "sigaction");
358
350
    return EX_OSERR;
359
351
  }
360
352
  if(old_action.sa_handler != SIG_IGN){
361
353
    ret = sigaction(SIGINT, &new_action, NULL);
362
354
    if(ret == -1){
363
 
      error_plus(0, errno, "sigaction");
 
355
      error(0, errno, "sigaction");
364
356
      return EX_OSERR;
365
357
    }
366
358
  }
367
359
  ret = sigaction(SIGHUP, NULL, &old_action);
368
360
  if(ret == -1){
369
 
    error_plus(0, errno, "sigaction");
 
361
    error(0, errno, "sigaction");
370
362
    return EX_OSERR;
371
363
  }
372
364
  if(old_action.sa_handler != SIG_IGN){
373
365
    ret = sigaction(SIGHUP, &new_action, NULL);
374
366
    if(ret == -1){
375
 
      error_plus(0, errno, "sigaction");
 
367
      error(0, errno, "sigaction");
376
368
      return EX_OSERR;
377
369
    }
378
370
  }
379
371
  ret = sigaction(SIGTERM, NULL, &old_action);
380
372
  if(ret == -1){
381
 
    error_plus(0, errno, "sigaction");
 
373
    error(0, errno, "sigaction");
382
374
    return EX_OSERR;
383
375
  }
384
376
  if(old_action.sa_handler != SIG_IGN){
385
377
    ret = sigaction(SIGTERM, &new_action, NULL);
386
378
    if(ret == -1){
387
 
      error_plus(0, errno, "sigaction");
 
379
      error(0, errno, "sigaction");
388
380
      return EX_OSERR;
389
381
    }
390
382
  }
398
390
  t_new.c_lflag &= ~(tcflag_t)ECHO;
399
391
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
400
392
    int e = errno;
401
 
    error_plus(0, errno, "tcsetattr-echo");
 
393
    error(0, errno, "tcsetattr-echo");
402
394
    switch(e){
403
395
    case EBADF:
404
396
    case ENOTTY:
468
460
        sret = write(STDOUT_FILENO, buffer + written, n - written);
469
461
        if(sret < 0){
470
462
          int e = errno;
471
 
          error_plus(0, errno, "write");
 
463
          error(0, errno, "write");
472
464
          switch(e){
473
465
          case EBADF:
474
466
          case EFAULT:
490
482
      sret = close(STDOUT_FILENO);
491
483
      if(sret == -1){
492
484
        int e = errno;
493
 
        error_plus(0, errno, "close");
 
485
        error(0, errno, "close");
494
486
        switch(e){
495
487
        case EBADF:
496
488
          status = EX_OSFILE;
506
498
    if(sret < 0){
507
499
      int e = errno;
508
500
      if(errno != EINTR and not feof(stdin)){
509
 
        error_plus(0, errno, "getline");
 
501
        error(0, errno, "getline");
510
502
        switch(e){
511
503
        case EBADF:
512
504
          status = EX_UNAVAILABLE;
513
 
          break;
514
505
        case EIO:
515
506
        case EINVAL:
516
507
        default:
536
527
    fprintf(stderr, "Restoring terminal attributes\n");
537
528
  }
538
529
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
539
 
    error_plus(0, errno, "tcsetattr+echo");
 
530
    error(0, errno, "tcsetattr+echo");
540
531
  }
541
532
  
542
533
  if(quit_now){
544
535
    old_action.sa_handler = SIG_DFL;
545
536
    ret = sigaction(signal_received, &old_action, NULL);
546
537
    if(ret == -1){
547
 
      error_plus(0, errno, "sigaction");
 
538
      error(0, errno, "sigaction");
548
539
    }
549
540
    raise(signal_received);
550
541
  }