/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/usplash.c

  • Committer: Björn Påhlsson
  • Date: 2011-06-23 22:27:15 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Usplash - Read a password from usplash and output it
4
4
 * 
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 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
31
31
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
32
32
#include <iso646.h>             /* and, or, not*/
33
33
#include <errno.h>              /* errno, EINTR */
 
34
#include <error.h>
34
35
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
35
36
                                   dirent */
36
37
#include <stddef.h>             /* NULL */
37
 
#include <string.h>             /* strlen(), memcmp() */
38
 
#include <stdio.h>              /* asprintf(), perror() */
 
38
#include <string.h>             /* strlen(), memcmp(), strerror() */
 
39
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(), fprintf() */
39
40
#include <unistd.h>             /* close(), write(), readlink(),
40
41
                                   read(), STDOUT_FILENO, sleep(),
41
42
                                   fork(), setuid(), geteuid(),
42
43
                                   setsid(), chdir(), dup2(),
43
44
                                   STDERR_FILENO, execv() */
44
45
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
45
 
                                   EXIT_SUCCESS, malloc(), _exit() */
46
 
#include <stdlib.h>             /* getenv() */
 
46
                                   EXIT_SUCCESS, malloc(), _exit(),
 
47
                                   getenv() */
47
48
#include <dirent.h>             /* opendir(), readdir(), closedir() */
48
49
#include <inttypes.h>           /* intmax_t, strtoimax() */
49
50
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
51
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
52
#include <argz.h>               /* argz_count(), argz_extract() */
 
53
#include <stdarg.h>             /* va_list, va_start(), ... */
50
54
 
51
55
sig_atomic_t interrupted_by_signal = 0;
52
56
int signal_received;
53
57
const char usplash_name[] = "/sbin/usplash";
54
58
 
 
59
/* Function to use when printing errors */
 
60
void error_plus(int status, int errnum, const char *formatstring, ...){
 
61
  va_list ap;
 
62
  char *text;
 
63
  int ret;
 
64
  
 
65
  va_start(ap, formatstring);
 
66
  ret = vasprintf(&text, formatstring, ap);
 
67
  if (ret == -1){
 
68
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
 
69
    vfprintf(stderr, formatstring, ap);
 
70
    fprintf(stderr, ": ");
 
71
    fprintf(stderr, "%s\n", strerror(errnum));
 
72
    error(status, errno, "vasprintf while printing error");
 
73
    return;
 
74
  }
 
75
  fprintf(stderr, "Mandos plugin ");
 
76
  error(status, errnum, "%s", text);
 
77
  free(text);
 
78
}
 
79
 
55
80
static void termination_handler(int signum){
56
81
  if(interrupted_by_signal){
57
82
    return;
152
177
  size_t cmdline_len = 0;
153
178
  DIR *proc_dir = opendir("/proc");
154
179
  if(proc_dir == NULL){
155
 
    perror("opendir");
 
180
    error_plus(0, errno, "opendir");
156
181
    return -1;
157
182
  }
158
183
  errno = 0;
180
205
      char *exe_link;
181
206
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
182
207
      if(ret == -1){
183
 
        perror("asprintf");
 
208
        error_plus(0, errno, "asprintf");
184
209
        goto fail_find_usplash;
185
210
      }
186
211
      
192
217
          free(exe_link);
193
218
          continue;
194
219
        }
195
 
        perror("lstat");
 
220
        error_plus(0, errno, "lstat");
196
221
        free(exe_link);
197
222
        goto fail_find_usplash;
198
223
      }
223
248
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
224
249
                       proc_ent->d_name);
225
250
        if(ret == -1){
226
 
          perror("asprintf");
 
251
          error_plus(0, errno, "asprintf");
227
252
          goto fail_find_usplash;
228
253
        }
229
254
        cl_fd = open(cmdline_filename, O_RDONLY);
230
255
        free(cmdline_filename);
231
256
        if(cl_fd == -1){
232
 
          perror("open");
 
257
          error_plus(0, errno, "open");
233
258
          goto fail_find_usplash;
234
259
        }
235
260
      }
241
266
        if(cmdline_len + blocksize > cmdline_allocated){
242
267
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
243
268
          if(tmp == NULL){
244
 
            perror("realloc");
 
269
            error_plus(0, errno, "realloc");
245
270
            close(cl_fd);
246
271
            goto fail_find_usplash;
247
272
          }
252
277
        sret = read(cl_fd, cmdline + cmdline_len,
253
278
                    cmdline_allocated - cmdline_len);
254
279
        if(sret == -1){
255
 
          perror("read");
 
280
          error_plus(0, errno, "read");
256
281
          close(cl_fd);
257
282
          goto fail_find_usplash;
258
283
        }
260
285
      } while(sret != 0);
261
286
      ret = close(cl_fd);
262
287
      if(ret == -1){
263
 
        perror("close");
 
288
        error_plus(0, errno, "close");
264
289
        goto fail_find_usplash;
265
290
      }
266
291
    }
267
292
    /* Close directory */
268
293
    ret = closedir(proc_dir);
269
294
    if(ret == -1){
270
 
      perror("closedir");
 
295
      error_plus(0, errno, "closedir");
271
296
      goto fail_find_usplash;
272
297
    }
273
298
    /* Success */
297
322
  size_t buf_len = 0;
298
323
  pid_t usplash_pid = -1;
299
324
  bool usplash_accessed = false;
 
325
  int status = EXIT_FAILURE;    /* Default failure exit status */
300
326
  
301
327
  char *prompt = makeprompt();
302
328
  if(prompt == NULL){
 
329
    status = EX_OSERR;
303
330
    goto failure;
304
331
  }
305
332
  
308
335
  size_t cmdline_len = 0;
309
336
  usplash_pid = find_usplash(&cmdline, &cmdline_len);
310
337
  if(usplash_pid == 0){
 
338
    status = EX_UNAVAILABLE;
311
339
    goto failure;
312
340
  }
313
341
  
319
347
    sigemptyset(&new_action.sa_mask);
320
348
    ret = sigaddset(&new_action.sa_mask, SIGINT);
321
349
    if(ret == -1){
322
 
      perror("sigaddset");
 
350
      error_plus(0, errno, "sigaddset");
 
351
      status = EX_OSERR;
323
352
      goto failure;
324
353
    }
325
354
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
326
355
    if(ret == -1){
327
 
      perror("sigaddset");
 
356
      error_plus(0, errno, "sigaddset");
 
357
      status = EX_OSERR;
328
358
      goto failure;
329
359
    }
330
360
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
331
361
    if(ret == -1){
332
 
      perror("sigaddset");
 
362
      error_plus(0, errno, "sigaddset");
 
363
      status = EX_OSERR;
333
364
      goto failure;
334
365
    }
335
366
    ret = sigaction(SIGINT, NULL, &old_action);
336
367
    if(ret == -1){
337
368
      if(errno != EINTR){
338
 
        perror("sigaction");
 
369
        error_plus(0, errno, "sigaction");
 
370
        status = EX_OSERR;
339
371
      }
340
372
      goto failure;
341
373
    }
343
375
      ret = sigaction(SIGINT, &new_action, NULL);
344
376
      if(ret == -1){
345
377
        if(errno != EINTR){
346
 
          perror("sigaction");
 
378
          error_plus(0, errno, "sigaction");
 
379
          status = EX_OSERR;
347
380
        }
348
381
        goto failure;
349
382
      }
351
384
    ret = sigaction(SIGHUP, NULL, &old_action);
352
385
    if(ret == -1){
353
386
      if(errno != EINTR){
354
 
        perror("sigaction");
 
387
        error_plus(0, errno, "sigaction");
 
388
        status = EX_OSERR;
355
389
      }
356
390
      goto failure;
357
391
    }
359
393
      ret = sigaction(SIGHUP, &new_action, NULL);
360
394
      if(ret == -1){
361
395
        if(errno != EINTR){
362
 
          perror("sigaction");
 
396
          error_plus(0, errno, "sigaction");
 
397
          status = EX_OSERR;
363
398
        }
364
399
        goto failure;
365
400
      }
367
402
    ret = sigaction(SIGTERM, NULL, &old_action);
368
403
    if(ret == -1){
369
404
      if(errno != EINTR){
370
 
        perror("sigaction");
 
405
        error_plus(0, errno, "sigaction");
 
406
        status = EX_OSERR;
371
407
      }
372
408
      goto failure;
373
409
    }
375
411
      ret = sigaction(SIGTERM, &new_action, NULL);
376
412
      if(ret == -1){
377
413
        if(errno != EINTR){
378
 
          perror("sigaction");
 
414
          error_plus(0, errno, "sigaction");
 
415
          status = EX_OSERR;
379
416
        }
380
417
        goto failure;
381
418
      }
386
423
  /* Write command to FIFO */
387
424
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
388
425
    if(errno != EINTR){
389
 
      perror("usplash_write");
 
426
      error_plus(0, errno, "usplash_write");
 
427
      status = EX_OSERR;
390
428
    }
391
429
    goto failure;
392
430
  }
397
435
  
398
436
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
399
437
    if(errno != EINTR){
400
 
      perror("usplash_write");
 
438
      error_plus(0, errno, "usplash_write");
 
439
      status = EX_OSERR;
401
440
    }
402
441
    goto failure;
403
442
  }
414
453
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
415
454
  if(outfifo_fd == -1){
416
455
    if(errno != EINTR){
417
 
      perror("open");
 
456
      error_plus(0, errno, "open");
 
457
      status = EX_OSERR;
418
458
    }
419
459
    goto failure;
420
460
  }
432
472
      char *tmp = realloc(buf, buf_allocated + blocksize);
433
473
      if(tmp == NULL){
434
474
        if(errno != EINTR){
435
 
          perror("realloc");
 
475
          error_plus(0, errno, "realloc");
 
476
          status = EX_OSERR;
436
477
        }
437
478
        goto failure;
438
479
      }
443
484
                buf_allocated - buf_len);
444
485
    if(sret == -1){
445
486
      if(errno != EINTR){
446
 
        perror("read");
 
487
        error_plus(0, errno, "read");
 
488
        status = EX_OSERR;
447
489
      }
448
490
      TEMP_FAILURE_RETRY(close(outfifo_fd));
449
491
      goto failure;
457
499
  ret = close(outfifo_fd);
458
500
  if(ret == -1){
459
501
    if(errno != EINTR){
460
 
      perror("close");
 
502
      error_plus(0, errno, "close");
 
503
      status = EX_OSERR;
461
504
    }
462
505
    goto failure;
463
506
  }
469
512
  
470
513
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
471
514
    if(errno != EINTR){
472
 
      perror("usplash_write");
 
515
      error_plus(0, errno, "usplash_write");
 
516
      status = EX_OSERR;
473
517
    }
474
518
    goto failure;
475
519
  }
481
525
  ret = close(fifo_fd);
482
526
  if(ret == -1){
483
527
    if(errno != EINTR){
484
 
      perror("close");
 
528
      error_plus(0, errno, "close");
 
529
      status = EX_OSERR;
485
530
    }
486
531
    goto failure;
487
532
  }
494
539
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
495
540
      if(sret == -1){
496
541
        if(errno != EINTR){
497
 
          perror("write");
 
542
          error_plus(0, errno, "write");
 
543
          status = EX_OSERR;
498
544
        }
499
545
        goto failure;
500
546
      }
523
569
  
524
570
  /* If usplash was never accessed, we can stop now */
525
571
  if(not usplash_accessed){
526
 
    return EXIT_FAILURE;
 
572
    return status;
527
573
  }
528
574
  
529
575
  /* Close FIFO */
530
576
  if(fifo_fd != -1){
531
577
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
532
578
    if(ret == -1 and errno != EINTR){
533
 
      perror("close");
 
579
      error_plus(0, errno, "close");
534
580
    }
535
581
    fifo_fd = -1;
536
582
  }
539
585
  if(outfifo_fd != -1){
540
586
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
541
587
    if(ret == -1){
542
 
      perror("close");
543
 
    }
544
 
  }
545
 
  
546
 
  /* Create argc and argv for new usplash*/
547
 
  int cmdline_argc = 0;
548
 
  char **cmdline_argv = malloc(sizeof(char *));
549
 
  {
550
 
    size_t position = 0;
551
 
    while(position < cmdline_len){
552
 
      char **tmp = realloc(cmdline_argv,
553
 
                           (sizeof(char *)
554
 
                            * (size_t)(cmdline_argc + 2)));
555
 
      if(tmp == NULL){
556
 
        perror("realloc");
557
 
        free(cmdline_argv);
558
 
        return EXIT_FAILURE;
559
 
      }
560
 
      cmdline_argv = tmp;
561
 
      cmdline_argv[cmdline_argc] = cmdline + position;
562
 
      cmdline_argc++;
563
 
      position += strlen(cmdline + position) + 1;
564
 
    }
565
 
    cmdline_argv[cmdline_argc] = NULL;
566
 
  }
 
588
      error_plus(0, errno, "close");
 
589
    }
 
590
  }
 
591
  
 
592
  /* Create argv for new usplash*/
 
593
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
 
594
                               * sizeof(char *)); /* Count args */
 
595
  if(cmdline_argv == NULL){
 
596
    error_plus(0, errno, "malloc");
 
597
    return status;
 
598
  }
 
599
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
 
600
  
567
601
  /* Kill old usplash */
568
602
  kill(usplash_pid, SIGTERM);
569
603
  sleep(2);
580
614
       the real user ID (_mandos) */
581
615
    ret = setuid(geteuid());
582
616
    if(ret == -1){
583
 
      perror("setuid");
 
617
      error_plus(0, errno, "setuid");
584
618
    }
585
619
    
586
620
    setsid();
587
621
    ret = chdir("/");
 
622
    if(ret == -1){
 
623
      error_plus(0, errno, "chdir");
 
624
      _exit(EX_OSERR);
 
625
    }
588
626
/*     if(fork() != 0){ */
589
627
/*       _exit(EXIT_SUCCESS); */
590
628
/*     } */
591
629
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
592
630
    if(ret == -1){
593
 
      perror("dup2");
594
 
      _exit(EXIT_FAILURE);
 
631
      error_plus(0, errno, "dup2");
 
632
      _exit(EX_OSERR);
595
633
    }
596
634
    
597
635
    execv(usplash_name, cmdline_argv);
598
636
    if(not interrupted_by_signal){
599
 
      perror("execv");
 
637
      error_plus(0, errno, "execv");
600
638
    }
601
639
    free(cmdline);
602
640
    free(cmdline_argv);
603
 
    _exit(EXIT_FAILURE);
 
641
    _exit(EX_OSERR);
604
642
  }
605
643
  free(cmdline);
606
644
  free(cmdline_argv);
607
645
  sleep(2);
608
646
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
609
647
    if(errno != EINTR){
610
 
      perror("usplash_write");
 
648
      error_plus(0, errno, "usplash_write");
611
649
    }
612
650
  }
613
651
  
615
653
  if(fifo_fd != -1){
616
654
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
617
655
    if(ret == -1 and errno != EINTR){
618
 
      perror("close");
 
656
      error_plus(0, errno, "close");
619
657
    }
620
658
    fifo_fd = -1;
621
659
  }
626
664
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
627
665
                                            &signal_action, NULL));
628
666
    if(ret == -1){
629
 
      perror("sigaction");
 
667
      error_plus(0, errno, "sigaction");
630
668
    }
631
669
    do {
632
670
      ret = raise(signal_received);
633
671
    } while(ret != 0 and errno == EINTR);
634
672
    if(ret != 0){
635
 
      perror("raise");
 
673
      error_plus(0, errno, "raise");
636
674
      abort();
637
675
    }
638
676
    TEMP_FAILURE_RETRY(pause());
639
677
  }
640
678
  
641
 
  return EXIT_FAILURE;
 
679
  return status;
642
680
}