/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/usplash.c

* mandos (Client.runtime_expansions): New attribute containing the
                                      allowed runtime expansions.

* mandos-clients.conf.xml (OPTIONS): Reordered alphabetically.

* mandos-ctl: Bug fix: print timeout and interval values pretty again.

* mandos-ctl.xml (EXAMPLE): Added more examples.

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-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 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
35
35
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
36
36
                                   dirent */
37
37
#include <stddef.h>             /* NULL */
38
 
#include <string.h>             /* strlen(), memcmp(), strerror() */
39
 
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(), fprintf() */
 
38
#include <string.h>             /* strlen(), memcmp() */
 
39
#include <stdio.h>              /* asprintf()*/
40
40
#include <unistd.h>             /* close(), write(), readlink(),
41
41
                                   read(), STDOUT_FILENO, sleep(),
42
42
                                   fork(), setuid(), geteuid(),
50
50
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
51
51
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
52
52
#include <argz.h>               /* argz_count(), argz_extract() */
53
 
#include <stdarg.h>             /* va_list, va_start(), ... */
54
53
 
55
54
sig_atomic_t interrupted_by_signal = 0;
56
55
int signal_received;
57
56
const char usplash_name[] = "/sbin/usplash";
58
57
 
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
 
 
80
58
static void termination_handler(int signum){
81
59
  if(interrupted_by_signal){
82
60
    return;
177
155
  size_t cmdline_len = 0;
178
156
  DIR *proc_dir = opendir("/proc");
179
157
  if(proc_dir == NULL){
180
 
    error_plus(0, errno, "opendir");
 
158
    error(0, errno, "opendir");
181
159
    return -1;
182
160
  }
183
161
  errno = 0;
205
183
      char *exe_link;
206
184
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
207
185
      if(ret == -1){
208
 
        error_plus(0, errno, "asprintf");
 
186
        error(0, errno, "asprintf");
209
187
        goto fail_find_usplash;
210
188
      }
211
189
      
217
195
          free(exe_link);
218
196
          continue;
219
197
        }
220
 
        error_plus(0, errno, "lstat");
 
198
        error(0, errno, "lstat");
221
199
        free(exe_link);
222
200
        goto fail_find_usplash;
223
201
      }
248
226
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
249
227
                       proc_ent->d_name);
250
228
        if(ret == -1){
251
 
          error_plus(0, errno, "asprintf");
 
229
          error(0, errno, "asprintf");
252
230
          goto fail_find_usplash;
253
231
        }
254
232
        cl_fd = open(cmdline_filename, O_RDONLY);
255
233
        free(cmdline_filename);
256
234
        if(cl_fd == -1){
257
 
          error_plus(0, errno, "open");
 
235
          error(0, errno, "open");
258
236
          goto fail_find_usplash;
259
237
        }
260
238
      }
266
244
        if(cmdline_len + blocksize > cmdline_allocated){
267
245
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
268
246
          if(tmp == NULL){
269
 
            error_plus(0, errno, "realloc");
 
247
            error(0, errno, "realloc");
270
248
            close(cl_fd);
271
249
            goto fail_find_usplash;
272
250
          }
277
255
        sret = read(cl_fd, cmdline + cmdline_len,
278
256
                    cmdline_allocated - cmdline_len);
279
257
        if(sret == -1){
280
 
          error_plus(0, errno, "read");
 
258
          error(0, errno, "read");
281
259
          close(cl_fd);
282
260
          goto fail_find_usplash;
283
261
        }
285
263
      } while(sret != 0);
286
264
      ret = close(cl_fd);
287
265
      if(ret == -1){
288
 
        error_plus(0, errno, "close");
 
266
        error(0, errno, "close");
289
267
        goto fail_find_usplash;
290
268
      }
291
269
    }
292
270
    /* Close directory */
293
271
    ret = closedir(proc_dir);
294
272
    if(ret == -1){
295
 
      error_plus(0, errno, "closedir");
 
273
      error(0, errno, "closedir");
296
274
      goto fail_find_usplash;
297
275
    }
298
276
    /* Success */
347
325
    sigemptyset(&new_action.sa_mask);
348
326
    ret = sigaddset(&new_action.sa_mask, SIGINT);
349
327
    if(ret == -1){
350
 
      error_plus(0, errno, "sigaddset");
 
328
      error(0, errno, "sigaddset");
351
329
      status = EX_OSERR;
352
330
      goto failure;
353
331
    }
354
332
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
355
333
    if(ret == -1){
356
 
      error_plus(0, errno, "sigaddset");
 
334
      error(0, errno, "sigaddset");
357
335
      status = EX_OSERR;
358
336
      goto failure;
359
337
    }
360
338
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
361
339
    if(ret == -1){
362
 
      error_plus(0, errno, "sigaddset");
 
340
      error(0, errno, "sigaddset");
363
341
      status = EX_OSERR;
364
342
      goto failure;
365
343
    }
366
344
    ret = sigaction(SIGINT, NULL, &old_action);
367
345
    if(ret == -1){
368
346
      if(errno != EINTR){
369
 
        error_plus(0, errno, "sigaction");
 
347
        error(0, errno, "sigaction");
370
348
        status = EX_OSERR;
371
349
      }
372
350
      goto failure;
375
353
      ret = sigaction(SIGINT, &new_action, NULL);
376
354
      if(ret == -1){
377
355
        if(errno != EINTR){
378
 
          error_plus(0, errno, "sigaction");
 
356
          error(0, errno, "sigaction");
379
357
          status = EX_OSERR;
380
358
        }
381
359
        goto failure;
384
362
    ret = sigaction(SIGHUP, NULL, &old_action);
385
363
    if(ret == -1){
386
364
      if(errno != EINTR){
387
 
        error_plus(0, errno, "sigaction");
 
365
        error(0, errno, "sigaction");
388
366
        status = EX_OSERR;
389
367
      }
390
368
      goto failure;
393
371
      ret = sigaction(SIGHUP, &new_action, NULL);
394
372
      if(ret == -1){
395
373
        if(errno != EINTR){
396
 
          error_plus(0, errno, "sigaction");
 
374
          error(0, errno, "sigaction");
397
375
          status = EX_OSERR;
398
376
        }
399
377
        goto failure;
402
380
    ret = sigaction(SIGTERM, NULL, &old_action);
403
381
    if(ret == -1){
404
382
      if(errno != EINTR){
405
 
        error_plus(0, errno, "sigaction");
 
383
        error(0, errno, "sigaction");
406
384
        status = EX_OSERR;
407
385
      }
408
386
      goto failure;
411
389
      ret = sigaction(SIGTERM, &new_action, NULL);
412
390
      if(ret == -1){
413
391
        if(errno != EINTR){
414
 
          error_plus(0, errno, "sigaction");
 
392
          error(0, errno, "sigaction");
415
393
          status = EX_OSERR;
416
394
        }
417
395
        goto failure;
423
401
  /* Write command to FIFO */
424
402
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
425
403
    if(errno != EINTR){
426
 
      error_plus(0, errno, "usplash_write");
 
404
      error(0, errno, "usplash_write");
427
405
      status = EX_OSERR;
428
406
    }
429
407
    goto failure;
435
413
  
436
414
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
437
415
    if(errno != EINTR){
438
 
      error_plus(0, errno, "usplash_write");
 
416
      error(0, errno, "usplash_write");
439
417
      status = EX_OSERR;
440
418
    }
441
419
    goto failure;
453
431
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
454
432
  if(outfifo_fd == -1){
455
433
    if(errno != EINTR){
456
 
      error_plus(0, errno, "open");
 
434
      error(0, errno, "open");
457
435
      status = EX_OSERR;
458
436
    }
459
437
    goto failure;
472
450
      char *tmp = realloc(buf, buf_allocated + blocksize);
473
451
      if(tmp == NULL){
474
452
        if(errno != EINTR){
475
 
          error_plus(0, errno, "realloc");
 
453
          error(0, errno, "realloc");
476
454
          status = EX_OSERR;
477
455
        }
478
456
        goto failure;
484
462
                buf_allocated - buf_len);
485
463
    if(sret == -1){
486
464
      if(errno != EINTR){
487
 
        error_plus(0, errno, "read");
 
465
        error(0, errno, "read");
488
466
        status = EX_OSERR;
489
467
      }
490
468
      TEMP_FAILURE_RETRY(close(outfifo_fd));
499
477
  ret = close(outfifo_fd);
500
478
  if(ret == -1){
501
479
    if(errno != EINTR){
502
 
      error_plus(0, errno, "close");
 
480
      error(0, errno, "close");
503
481
      status = EX_OSERR;
504
482
    }
505
483
    goto failure;
512
490
  
513
491
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
514
492
    if(errno != EINTR){
515
 
      error_plus(0, errno, "usplash_write");
 
493
      error(0, errno, "usplash_write");
516
494
      status = EX_OSERR;
517
495
    }
518
496
    goto failure;
525
503
  ret = close(fifo_fd);
526
504
  if(ret == -1){
527
505
    if(errno != EINTR){
528
 
      error_plus(0, errno, "close");
 
506
      error(0, errno, "close");
529
507
      status = EX_OSERR;
530
508
    }
531
509
    goto failure;
539
517
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
540
518
      if(sret == -1){
541
519
        if(errno != EINTR){
542
 
          error_plus(0, errno, "write");
 
520
          error(0, errno, "write");
543
521
          status = EX_OSERR;
544
522
        }
545
523
        goto failure;
576
554
  if(fifo_fd != -1){
577
555
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
578
556
    if(ret == -1 and errno != EINTR){
579
 
      error_plus(0, errno, "close");
 
557
      error(0, errno, "close");
580
558
    }
581
559
    fifo_fd = -1;
582
560
  }
585
563
  if(outfifo_fd != -1){
586
564
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
587
565
    if(ret == -1){
588
 
      error_plus(0, errno, "close");
 
566
      error(0, errno, "close");
589
567
    }
590
568
  }
591
569
  
593
571
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
594
572
                               * sizeof(char *)); /* Count args */
595
573
  if(cmdline_argv == NULL){
596
 
    error_plus(0, errno, "malloc");
 
574
    error(0, errno, "malloc");
597
575
    return status;
598
576
  }
599
577
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
614
592
       the real user ID (_mandos) */
615
593
    ret = setuid(geteuid());
616
594
    if(ret == -1){
617
 
      error_plus(0, errno, "setuid");
 
595
      error(0, errno, "setuid");
618
596
    }
619
597
    
620
598
    setsid();
621
599
    ret = chdir("/");
622
600
    if(ret == -1){
623
 
      error_plus(0, errno, "chdir");
 
601
      error(0, errno, "chdir");
624
602
      _exit(EX_OSERR);
625
603
    }
626
604
/*     if(fork() != 0){ */
628
606
/*     } */
629
607
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
630
608
    if(ret == -1){
631
 
      error_plus(0, errno, "dup2");
 
609
      error(0, errno, "dup2");
632
610
      _exit(EX_OSERR);
633
611
    }
634
612
    
635
613
    execv(usplash_name, cmdline_argv);
636
614
    if(not interrupted_by_signal){
637
 
      error_plus(0, errno, "execv");
 
615
      error(0, errno, "execv");
638
616
    }
639
617
    free(cmdline);
640
618
    free(cmdline_argv);
645
623
  sleep(2);
646
624
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
647
625
    if(errno != EINTR){
648
 
      error_plus(0, errno, "usplash_write");
 
626
      error(0, errno, "usplash_write");
649
627
    }
650
628
  }
651
629
  
653
631
  if(fifo_fd != -1){
654
632
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
655
633
    if(ret == -1 and errno != EINTR){
656
 
      error_plus(0, errno, "close");
 
634
      error(0, errno, "close");
657
635
    }
658
636
    fifo_fd = -1;
659
637
  }
664
642
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
665
643
                                            &signal_action, NULL));
666
644
    if(ret == -1){
667
 
      error_plus(0, errno, "sigaction");
 
645
      error(0, errno, "sigaction");
668
646
    }
669
647
    do {
670
648
      ret = raise(signal_received);
671
649
    } while(ret != 0 and errno == EINTR);
672
650
    if(ret != 0){
673
 
      error_plus(0, errno, "raise");
 
651
      error(0, errno, "raise");
674
652
      abort();
675
653
    }
676
654
    TEMP_FAILURE_RETRY(pause());