/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: Teddy Hogeborn
  • Date: 2009-11-09 07:35:16 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091109073516-v1vem352uz0vuwrd
* dbus-mandos.conf: New; to be copied to
                    "/etc/dbus-1/system.d/mandos.conf".

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