/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

todo

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-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 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
37
#include <string.h>             /* strlen(), memcmp() */
39
 
#include <stdio.h>              /* asprintf()*/
 
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
 
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
52
 
#include <argz.h>               /* argz_count(), argz_extract() */
53
50
 
54
51
sig_atomic_t interrupted_by_signal = 0;
55
52
int signal_received;
155
152
  size_t cmdline_len = 0;
156
153
  DIR *proc_dir = opendir("/proc");
157
154
  if(proc_dir == NULL){
158
 
    error(0, errno, "opendir");
 
155
    perror("opendir");
159
156
    return -1;
160
157
  }
161
158
  errno = 0;
183
180
      char *exe_link;
184
181
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
182
      if(ret == -1){
186
 
        error(0, errno, "asprintf");
 
183
        perror("asprintf");
187
184
        goto fail_find_usplash;
188
185
      }
189
186
      
195
192
          free(exe_link);
196
193
          continue;
197
194
        }
198
 
        error(0, errno, "lstat");
 
195
        perror("lstat");
199
196
        free(exe_link);
200
197
        goto fail_find_usplash;
201
198
      }
226
223
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
227
224
                       proc_ent->d_name);
228
225
        if(ret == -1){
229
 
          error(0, errno, "asprintf");
 
226
          perror("asprintf");
230
227
          goto fail_find_usplash;
231
228
        }
232
229
        cl_fd = open(cmdline_filename, O_RDONLY);
233
230
        free(cmdline_filename);
234
231
        if(cl_fd == -1){
235
 
          error(0, errno, "open");
 
232
          perror("open");
236
233
          goto fail_find_usplash;
237
234
        }
238
235
      }
244
241
        if(cmdline_len + blocksize > cmdline_allocated){
245
242
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
246
243
          if(tmp == NULL){
247
 
            error(0, errno, "realloc");
 
244
            perror("realloc");
248
245
            close(cl_fd);
249
246
            goto fail_find_usplash;
250
247
          }
255
252
        sret = read(cl_fd, cmdline + cmdline_len,
256
253
                    cmdline_allocated - cmdline_len);
257
254
        if(sret == -1){
258
 
          error(0, errno, "read");
 
255
          perror("read");
259
256
          close(cl_fd);
260
257
          goto fail_find_usplash;
261
258
        }
263
260
      } while(sret != 0);
264
261
      ret = close(cl_fd);
265
262
      if(ret == -1){
266
 
        error(0, errno, "close");
 
263
        perror("close");
267
264
        goto fail_find_usplash;
268
265
      }
269
266
    }
270
267
    /* Close directory */
271
268
    ret = closedir(proc_dir);
272
269
    if(ret == -1){
273
 
      error(0, errno, "closedir");
 
270
      perror("closedir");
274
271
      goto fail_find_usplash;
275
272
    }
276
273
    /* Success */
300
297
  size_t buf_len = 0;
301
298
  pid_t usplash_pid = -1;
302
299
  bool usplash_accessed = false;
303
 
  int status = EXIT_FAILURE;    /* Default failure exit status */
304
300
  
305
301
  char *prompt = makeprompt();
306
302
  if(prompt == NULL){
307
 
    status = EX_OSERR;
308
303
    goto failure;
309
304
  }
310
305
  
313
308
  size_t cmdline_len = 0;
314
309
  usplash_pid = find_usplash(&cmdline, &cmdline_len);
315
310
  if(usplash_pid == 0){
316
 
    status = EX_UNAVAILABLE;
317
311
    goto failure;
318
312
  }
319
313
  
325
319
    sigemptyset(&new_action.sa_mask);
326
320
    ret = sigaddset(&new_action.sa_mask, SIGINT);
327
321
    if(ret == -1){
328
 
      error(0, errno, "sigaddset");
329
 
      status = EX_OSERR;
 
322
      perror("sigaddset");
330
323
      goto failure;
331
324
    }
332
325
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
326
    if(ret == -1){
334
 
      error(0, errno, "sigaddset");
335
 
      status = EX_OSERR;
 
327
      perror("sigaddset");
336
328
      goto failure;
337
329
    }
338
330
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
331
    if(ret == -1){
340
 
      error(0, errno, "sigaddset");
341
 
      status = EX_OSERR;
 
332
      perror("sigaddset");
342
333
      goto failure;
343
334
    }
344
335
    ret = sigaction(SIGINT, NULL, &old_action);
345
336
    if(ret == -1){
346
337
      if(errno != EINTR){
347
 
        error(0, errno, "sigaction");
348
 
        status = EX_OSERR;
 
338
        perror("sigaction");
349
339
      }
350
340
      goto failure;
351
341
    }
353
343
      ret = sigaction(SIGINT, &new_action, NULL);
354
344
      if(ret == -1){
355
345
        if(errno != EINTR){
356
 
          error(0, errno, "sigaction");
357
 
          status = EX_OSERR;
 
346
          perror("sigaction");
358
347
        }
359
348
        goto failure;
360
349
      }
362
351
    ret = sigaction(SIGHUP, NULL, &old_action);
363
352
    if(ret == -1){
364
353
      if(errno != EINTR){
365
 
        error(0, errno, "sigaction");
366
 
        status = EX_OSERR;
 
354
        perror("sigaction");
367
355
      }
368
356
      goto failure;
369
357
    }
371
359
      ret = sigaction(SIGHUP, &new_action, NULL);
372
360
      if(ret == -1){
373
361
        if(errno != EINTR){
374
 
          error(0, errno, "sigaction");
375
 
          status = EX_OSERR;
 
362
          perror("sigaction");
376
363
        }
377
364
        goto failure;
378
365
      }
380
367
    ret = sigaction(SIGTERM, NULL, &old_action);
381
368
    if(ret == -1){
382
369
      if(errno != EINTR){
383
 
        error(0, errno, "sigaction");
384
 
        status = EX_OSERR;
 
370
        perror("sigaction");
385
371
      }
386
372
      goto failure;
387
373
    }
389
375
      ret = sigaction(SIGTERM, &new_action, NULL);
390
376
      if(ret == -1){
391
377
        if(errno != EINTR){
392
 
          error(0, errno, "sigaction");
393
 
          status = EX_OSERR;
 
378
          perror("sigaction");
394
379
        }
395
380
        goto failure;
396
381
      }
401
386
  /* Write command to FIFO */
402
387
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
403
388
    if(errno != EINTR){
404
 
      error(0, errno, "usplash_write");
405
 
      status = EX_OSERR;
 
389
      perror("usplash_write");
406
390
    }
407
391
    goto failure;
408
392
  }
413
397
  
414
398
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
415
399
    if(errno != EINTR){
416
 
      error(0, errno, "usplash_write");
417
 
      status = EX_OSERR;
 
400
      perror("usplash_write");
418
401
    }
419
402
    goto failure;
420
403
  }
431
414
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
432
415
  if(outfifo_fd == -1){
433
416
    if(errno != EINTR){
434
 
      error(0, errno, "open");
435
 
      status = EX_OSERR;
 
417
      perror("open");
436
418
    }
437
419
    goto failure;
438
420
  }
450
432
      char *tmp = realloc(buf, buf_allocated + blocksize);
451
433
      if(tmp == NULL){
452
434
        if(errno != EINTR){
453
 
          error(0, errno, "realloc");
454
 
          status = EX_OSERR;
 
435
          perror("realloc");
455
436
        }
456
437
        goto failure;
457
438
      }
462
443
                buf_allocated - buf_len);
463
444
    if(sret == -1){
464
445
      if(errno != EINTR){
465
 
        error(0, errno, "read");
466
 
        status = EX_OSERR;
 
446
        perror("read");
467
447
      }
468
448
      TEMP_FAILURE_RETRY(close(outfifo_fd));
469
449
      goto failure;
477
457
  ret = close(outfifo_fd);
478
458
  if(ret == -1){
479
459
    if(errno != EINTR){
480
 
      error(0, errno, "close");
481
 
      status = EX_OSERR;
 
460
      perror("close");
482
461
    }
483
462
    goto failure;
484
463
  }
490
469
  
491
470
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
492
471
    if(errno != EINTR){
493
 
      error(0, errno, "usplash_write");
494
 
      status = EX_OSERR;
 
472
      perror("usplash_write");
495
473
    }
496
474
    goto failure;
497
475
  }
503
481
  ret = close(fifo_fd);
504
482
  if(ret == -1){
505
483
    if(errno != EINTR){
506
 
      error(0, errno, "close");
507
 
      status = EX_OSERR;
 
484
      perror("close");
508
485
    }
509
486
    goto failure;
510
487
  }
517
494
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
518
495
      if(sret == -1){
519
496
        if(errno != EINTR){
520
 
          error(0, errno, "write");
521
 
          status = EX_OSERR;
 
497
          perror("write");
522
498
        }
523
499
        goto failure;
524
500
      }
547
523
  
548
524
  /* If usplash was never accessed, we can stop now */
549
525
  if(not usplash_accessed){
550
 
    return status;
 
526
    return EXIT_FAILURE;
551
527
  }
552
528
  
553
529
  /* Close FIFO */
554
530
  if(fifo_fd != -1){
555
531
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
556
532
    if(ret == -1 and errno != EINTR){
557
 
      error(0, errno, "close");
 
533
      perror("close");
558
534
    }
559
535
    fifo_fd = -1;
560
536
  }
563
539
  if(outfifo_fd != -1){
564
540
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
565
541
    if(ret == -1){
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
 
  
 
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
  }
579
567
  /* Kill old usplash */
580
568
  kill(usplash_pid, SIGTERM);
581
569
  sleep(2);
592
580
       the real user ID (_mandos) */
593
581
    ret = setuid(geteuid());
594
582
    if(ret == -1){
595
 
      error(0, errno, "setuid");
 
583
      perror("setuid");
596
584
    }
597
585
    
598
586
    setsid();
599
587
    ret = chdir("/");
600
 
    if(ret == -1){
601
 
      error(0, errno, "chdir");
602
 
      _exit(EX_OSERR);
603
 
    }
604
588
/*     if(fork() != 0){ */
605
589
/*       _exit(EXIT_SUCCESS); */
606
590
/*     } */
607
591
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
608
592
    if(ret == -1){
609
 
      error(0, errno, "dup2");
610
 
      _exit(EX_OSERR);
 
593
      perror("dup2");
 
594
      _exit(EXIT_FAILURE);
611
595
    }
612
596
    
613
597
    execv(usplash_name, cmdline_argv);
614
598
    if(not interrupted_by_signal){
615
 
      error(0, errno, "execv");
 
599
      perror("execv");
616
600
    }
617
601
    free(cmdline);
618
602
    free(cmdline_argv);
619
 
    _exit(EX_OSERR);
 
603
    _exit(EXIT_FAILURE);
620
604
  }
621
605
  free(cmdline);
622
606
  free(cmdline_argv);
623
607
  sleep(2);
624
608
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
625
609
    if(errno != EINTR){
626
 
      error(0, errno, "usplash_write");
 
610
      perror("usplash_write");
627
611
    }
628
612
  }
629
613
  
631
615
  if(fifo_fd != -1){
632
616
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
633
617
    if(ret == -1 and errno != EINTR){
634
 
      error(0, errno, "close");
 
618
      perror("close");
635
619
    }
636
620
    fifo_fd = -1;
637
621
  }
642
626
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
643
627
                                            &signal_action, NULL));
644
628
    if(ret == -1){
645
 
      error(0, errno, "sigaction");
 
629
      perror("sigaction");
646
630
    }
647
631
    do {
648
632
      ret = raise(signal_received);
649
633
    } while(ret != 0 and errno == EINTR);
650
634
    if(ret != 0){
651
 
      error(0, errno, "raise");
 
635
      perror("raise");
652
636
      abort();
653
637
    }
654
638
    TEMP_FAILURE_RETRY(pause());
655
639
  }
656
640
  
657
 
  return status;
 
641
  return EXIT_FAILURE;
658
642
}