/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

Merge from trunk.  Just a minor release to make it lintian clean.

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-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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
38
#include <string.h>             /* strlen(), memcmp() */
38
 
#include <stdio.h>              /* asprintf(), perror() */
 
39
#include <stdio.h>              /* asprintf()*/
39
40
#include <unistd.h>             /* close(), write(), readlink(),
40
41
                                   read(), STDOUT_FILENO, sleep(),
41
42
                                   fork(), setuid(), geteuid(),
48
49
#include <inttypes.h>           /* intmax_t, strtoimax() */
49
50
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
50
51
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
52
#include <argz.h>               /* argz_count(), argz_extract() */
51
53
 
52
54
sig_atomic_t interrupted_by_signal = 0;
53
55
int signal_received;
153
155
  size_t cmdline_len = 0;
154
156
  DIR *proc_dir = opendir("/proc");
155
157
  if(proc_dir == NULL){
156
 
    perror("opendir");
 
158
    error(0, errno, "opendir");
157
159
    return -1;
158
160
  }
159
161
  errno = 0;
181
183
      char *exe_link;
182
184
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
183
185
      if(ret == -1){
184
 
        perror("asprintf");
 
186
        error(0, errno, "asprintf");
185
187
        goto fail_find_usplash;
186
188
      }
187
189
      
193
195
          free(exe_link);
194
196
          continue;
195
197
        }
196
 
        perror("lstat");
 
198
        error(0, errno, "lstat");
197
199
        free(exe_link);
198
200
        goto fail_find_usplash;
199
201
      }
224
226
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
225
227
                       proc_ent->d_name);
226
228
        if(ret == -1){
227
 
          perror("asprintf");
 
229
          error(0, errno, "asprintf");
228
230
          goto fail_find_usplash;
229
231
        }
230
232
        cl_fd = open(cmdline_filename, O_RDONLY);
231
233
        free(cmdline_filename);
232
234
        if(cl_fd == -1){
233
 
          perror("open");
 
235
          error(0, errno, "open");
234
236
          goto fail_find_usplash;
235
237
        }
236
238
      }
242
244
        if(cmdline_len + blocksize > cmdline_allocated){
243
245
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
244
246
          if(tmp == NULL){
245
 
            perror("realloc");
 
247
            error(0, errno, "realloc");
246
248
            close(cl_fd);
247
249
            goto fail_find_usplash;
248
250
          }
253
255
        sret = read(cl_fd, cmdline + cmdline_len,
254
256
                    cmdline_allocated - cmdline_len);
255
257
        if(sret == -1){
256
 
          perror("read");
 
258
          error(0, errno, "read");
257
259
          close(cl_fd);
258
260
          goto fail_find_usplash;
259
261
        }
261
263
      } while(sret != 0);
262
264
      ret = close(cl_fd);
263
265
      if(ret == -1){
264
 
        perror("close");
 
266
        error(0, errno, "close");
265
267
        goto fail_find_usplash;
266
268
      }
267
269
    }
268
270
    /* Close directory */
269
271
    ret = closedir(proc_dir);
270
272
    if(ret == -1){
271
 
      perror("closedir");
 
273
      error(0, errno, "closedir");
272
274
      goto fail_find_usplash;
273
275
    }
274
276
    /* Success */
323
325
    sigemptyset(&new_action.sa_mask);
324
326
    ret = sigaddset(&new_action.sa_mask, SIGINT);
325
327
    if(ret == -1){
326
 
      perror("sigaddset");
 
328
      error(0, errno, "sigaddset");
327
329
      status = EX_OSERR;
328
330
      goto failure;
329
331
    }
330
332
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
331
333
    if(ret == -1){
332
 
      perror("sigaddset");
 
334
      error(0, errno, "sigaddset");
333
335
      status = EX_OSERR;
334
336
      goto failure;
335
337
    }
336
338
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
337
339
    if(ret == -1){
338
 
      perror("sigaddset");
 
340
      error(0, errno, "sigaddset");
339
341
      status = EX_OSERR;
340
342
      goto failure;
341
343
    }
342
344
    ret = sigaction(SIGINT, NULL, &old_action);
343
345
    if(ret == -1){
344
346
      if(errno != EINTR){
345
 
        perror("sigaction");
 
347
        error(0, errno, "sigaction");
346
348
        status = EX_OSERR;
347
349
      }
348
350
      goto failure;
351
353
      ret = sigaction(SIGINT, &new_action, NULL);
352
354
      if(ret == -1){
353
355
        if(errno != EINTR){
354
 
          perror("sigaction");
 
356
          error(0, errno, "sigaction");
355
357
          status = EX_OSERR;
356
358
        }
357
359
        goto failure;
360
362
    ret = sigaction(SIGHUP, NULL, &old_action);
361
363
    if(ret == -1){
362
364
      if(errno != EINTR){
363
 
        perror("sigaction");
 
365
        error(0, errno, "sigaction");
364
366
        status = EX_OSERR;
365
367
      }
366
368
      goto failure;
369
371
      ret = sigaction(SIGHUP, &new_action, NULL);
370
372
      if(ret == -1){
371
373
        if(errno != EINTR){
372
 
          perror("sigaction");
 
374
          error(0, errno, "sigaction");
373
375
          status = EX_OSERR;
374
376
        }
375
377
        goto failure;
378
380
    ret = sigaction(SIGTERM, NULL, &old_action);
379
381
    if(ret == -1){
380
382
      if(errno != EINTR){
381
 
        perror("sigaction");
 
383
        error(0, errno, "sigaction");
382
384
        status = EX_OSERR;
383
385
      }
384
386
      goto failure;
387
389
      ret = sigaction(SIGTERM, &new_action, NULL);
388
390
      if(ret == -1){
389
391
        if(errno != EINTR){
390
 
          perror("sigaction");
 
392
          error(0, errno, "sigaction");
391
393
          status = EX_OSERR;
392
394
        }
393
395
        goto failure;
399
401
  /* Write command to FIFO */
400
402
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
401
403
    if(errno != EINTR){
402
 
      perror("usplash_write");
 
404
      error(0, errno, "usplash_write");
403
405
      status = EX_OSERR;
404
406
    }
405
407
    goto failure;
411
413
  
412
414
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
413
415
    if(errno != EINTR){
414
 
      perror("usplash_write");
 
416
      error(0, errno, "usplash_write");
415
417
      status = EX_OSERR;
416
418
    }
417
419
    goto failure;
429
431
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
430
432
  if(outfifo_fd == -1){
431
433
    if(errno != EINTR){
432
 
      perror("open");
 
434
      error(0, errno, "open");
433
435
      status = EX_OSERR;
434
436
    }
435
437
    goto failure;
448
450
      char *tmp = realloc(buf, buf_allocated + blocksize);
449
451
      if(tmp == NULL){
450
452
        if(errno != EINTR){
451
 
          perror("realloc");
 
453
          error(0, errno, "realloc");
452
454
          status = EX_OSERR;
453
455
        }
454
456
        goto failure;
460
462
                buf_allocated - buf_len);
461
463
    if(sret == -1){
462
464
      if(errno != EINTR){
463
 
        perror("read");
 
465
        error(0, errno, "read");
464
466
        status = EX_OSERR;
465
467
      }
466
468
      TEMP_FAILURE_RETRY(close(outfifo_fd));
475
477
  ret = close(outfifo_fd);
476
478
  if(ret == -1){
477
479
    if(errno != EINTR){
478
 
      perror("close");
 
480
      error(0, errno, "close");
479
481
      status = EX_OSERR;
480
482
    }
481
483
    goto failure;
488
490
  
489
491
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
490
492
    if(errno != EINTR){
491
 
      perror("usplash_write");
 
493
      error(0, errno, "usplash_write");
492
494
      status = EX_OSERR;
493
495
    }
494
496
    goto failure;
501
503
  ret = close(fifo_fd);
502
504
  if(ret == -1){
503
505
    if(errno != EINTR){
504
 
      perror("close");
 
506
      error(0, errno, "close");
505
507
      status = EX_OSERR;
506
508
    }
507
509
    goto failure;
515
517
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
516
518
      if(sret == -1){
517
519
        if(errno != EINTR){
518
 
          perror("write");
 
520
          error(0, errno, "write");
519
521
          status = EX_OSERR;
520
522
        }
521
523
        goto failure;
552
554
  if(fifo_fd != -1){
553
555
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
554
556
    if(ret == -1 and errno != EINTR){
555
 
      perror("close");
 
557
      error(0, errno, "close");
556
558
    }
557
559
    fifo_fd = -1;
558
560
  }
561
563
  if(outfifo_fd != -1){
562
564
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
563
565
    if(ret == -1){
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
 
  }
 
566
      error(0, errno, "close");
 
567
    }
 
568
  }
 
569
  
 
570
  /* Create argv for new usplash*/
 
571
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
 
572
                               * sizeof(char *)); /* Count args */
 
573
  if(cmdline_argv == NULL){
 
574
    error(0, errno, "malloc");
 
575
    return status;
 
576
  }
 
577
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
 
578
  
589
579
  /* Kill old usplash */
590
580
  kill(usplash_pid, SIGTERM);
591
581
  sleep(2);
602
592
       the real user ID (_mandos) */
603
593
    ret = setuid(geteuid());
604
594
    if(ret == -1){
605
 
      perror("setuid");
 
595
      error(0, errno, "setuid");
606
596
    }
607
597
    
608
598
    setsid();
609
599
    ret = chdir("/");
610
600
    if(ret == -1){
611
 
      perror("chdir");
 
601
      error(0, errno, "chdir");
612
602
      _exit(EX_OSERR);
613
603
    }
614
604
/*     if(fork() != 0){ */
616
606
/*     } */
617
607
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
618
608
    if(ret == -1){
619
 
      perror("dup2");
 
609
      error(0, errno, "dup2");
620
610
      _exit(EX_OSERR);
621
611
    }
622
612
    
623
613
    execv(usplash_name, cmdline_argv);
624
614
    if(not interrupted_by_signal){
625
 
      perror("execv");
 
615
      error(0, errno, "execv");
626
616
    }
627
617
    free(cmdline);
628
618
    free(cmdline_argv);
633
623
  sleep(2);
634
624
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
635
625
    if(errno != EINTR){
636
 
      perror("usplash_write");
 
626
      error(0, errno, "usplash_write");
637
627
    }
638
628
  }
639
629
  
641
631
  if(fifo_fd != -1){
642
632
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
643
633
    if(ret == -1 and errno != EINTR){
644
 
      perror("close");
 
634
      error(0, errno, "close");
645
635
    }
646
636
    fifo_fd = -1;
647
637
  }
652
642
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
653
643
                                            &signal_action, NULL));
654
644
    if(ret == -1){
655
 
      perror("sigaction");
 
645
      error(0, errno, "sigaction");
656
646
    }
657
647
    do {
658
648
      ret = raise(signal_received);
659
649
    } while(ret != 0 and errno == EINTR);
660
650
    if(ret != 0){
661
 
      perror("raise");
 
651
      error(0, errno, "raise");
662
652
      abort();
663
653
    }
664
654
    TEMP_FAILURE_RETRY(pause());