/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

merge

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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
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(),
 
40
                                   fprintf() */
39
41
#include <unistd.h>             /* close(), write(), readlink(),
40
42
                                   read(), STDOUT_FILENO, sleep(),
41
43
                                   fork(), setuid(), geteuid(),
42
44
                                   setsid(), chdir(), dup2(),
43
45
                                   STDERR_FILENO, execv() */
44
46
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
45
 
                                   EXIT_SUCCESS, malloc(), _exit() */
46
 
#include <stdlib.h>             /* getenv() */
 
47
                                   EXIT_SUCCESS, malloc(), _exit(),
 
48
                                   getenv() */
47
49
#include <dirent.h>             /* opendir(), readdir(), closedir() */
48
50
#include <inttypes.h>           /* intmax_t, strtoimax() */
49
51
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
50
52
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
53
#include <argz.h>               /* argz_count(), argz_extract() */
 
54
#include <stdarg.h>             /* va_list, va_start(), ... */
51
55
 
52
56
sig_atomic_t interrupted_by_signal = 0;
53
57
int signal_received;
54
58
const char usplash_name[] = "/sbin/usplash";
55
59
 
 
60
/* Function to use when printing errors */
 
61
void error_plus(int status, int errnum, const char *formatstring,
 
62
                ...){
 
63
  va_list ap;
 
64
  char *text;
 
65
  int ret;
 
66
  
 
67
  va_start(ap, formatstring);
 
68
  ret = vasprintf(&text, formatstring, ap);
 
69
  if (ret == -1){
 
70
    fprintf(stderr, "Mandos plugin %s: ",
 
71
            program_invocation_short_name);
 
72
    vfprintf(stderr, formatstring, ap);
 
73
    fprintf(stderr, ": ");
 
74
    fprintf(stderr, "%s\n", strerror(errnum));
 
75
    error(status, errno, "vasprintf while printing error");
 
76
    return;
 
77
  }
 
78
  fprintf(stderr, "Mandos plugin ");
 
79
  error(status, errnum, "%s", text);
 
80
  free(text);
 
81
}
 
82
 
56
83
static void termination_handler(int signum){
57
84
  if(interrupted_by_signal){
58
85
    return;
153
180
  size_t cmdline_len = 0;
154
181
  DIR *proc_dir = opendir("/proc");
155
182
  if(proc_dir == NULL){
156
 
    perror("opendir");
 
183
    error_plus(0, errno, "opendir");
157
184
    return -1;
158
185
  }
159
186
  errno = 0;
181
208
      char *exe_link;
182
209
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
183
210
      if(ret == -1){
184
 
        perror("asprintf");
 
211
        error_plus(0, errno, "asprintf");
185
212
        goto fail_find_usplash;
186
213
      }
187
214
      
193
220
          free(exe_link);
194
221
          continue;
195
222
        }
196
 
        perror("lstat");
 
223
        error_plus(0, errno, "lstat");
197
224
        free(exe_link);
198
225
        goto fail_find_usplash;
199
226
      }
224
251
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
225
252
                       proc_ent->d_name);
226
253
        if(ret == -1){
227
 
          perror("asprintf");
 
254
          error_plus(0, errno, "asprintf");
228
255
          goto fail_find_usplash;
229
256
        }
230
257
        cl_fd = open(cmdline_filename, O_RDONLY);
231
258
        free(cmdline_filename);
232
259
        if(cl_fd == -1){
233
 
          perror("open");
 
260
          error_plus(0, errno, "open");
234
261
          goto fail_find_usplash;
235
262
        }
236
263
      }
242
269
        if(cmdline_len + blocksize > cmdline_allocated){
243
270
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
244
271
          if(tmp == NULL){
245
 
            perror("realloc");
 
272
            error_plus(0, errno, "realloc");
246
273
            close(cl_fd);
247
274
            goto fail_find_usplash;
248
275
          }
253
280
        sret = read(cl_fd, cmdline + cmdline_len,
254
281
                    cmdline_allocated - cmdline_len);
255
282
        if(sret == -1){
256
 
          perror("read");
 
283
          error_plus(0, errno, "read");
257
284
          close(cl_fd);
258
285
          goto fail_find_usplash;
259
286
        }
261
288
      } while(sret != 0);
262
289
      ret = close(cl_fd);
263
290
      if(ret == -1){
264
 
        perror("close");
 
291
        error_plus(0, errno, "close");
265
292
        goto fail_find_usplash;
266
293
      }
267
294
    }
268
295
    /* Close directory */
269
296
    ret = closedir(proc_dir);
270
297
    if(ret == -1){
271
 
      perror("closedir");
 
298
      error_plus(0, errno, "closedir");
272
299
      goto fail_find_usplash;
273
300
    }
274
301
    /* Success */
323
350
    sigemptyset(&new_action.sa_mask);
324
351
    ret = sigaddset(&new_action.sa_mask, SIGINT);
325
352
    if(ret == -1){
326
 
      perror("sigaddset");
 
353
      error_plus(0, errno, "sigaddset");
327
354
      status = EX_OSERR;
328
355
      goto failure;
329
356
    }
330
357
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
331
358
    if(ret == -1){
332
 
      perror("sigaddset");
 
359
      error_plus(0, errno, "sigaddset");
333
360
      status = EX_OSERR;
334
361
      goto failure;
335
362
    }
336
363
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
337
364
    if(ret == -1){
338
 
      perror("sigaddset");
 
365
      error_plus(0, errno, "sigaddset");
339
366
      status = EX_OSERR;
340
367
      goto failure;
341
368
    }
342
369
    ret = sigaction(SIGINT, NULL, &old_action);
343
370
    if(ret == -1){
344
371
      if(errno != EINTR){
345
 
        perror("sigaction");
 
372
        error_plus(0, errno, "sigaction");
346
373
        status = EX_OSERR;
347
374
      }
348
375
      goto failure;
351
378
      ret = sigaction(SIGINT, &new_action, NULL);
352
379
      if(ret == -1){
353
380
        if(errno != EINTR){
354
 
          perror("sigaction");
 
381
          error_plus(0, errno, "sigaction");
355
382
          status = EX_OSERR;
356
383
        }
357
384
        goto failure;
360
387
    ret = sigaction(SIGHUP, NULL, &old_action);
361
388
    if(ret == -1){
362
389
      if(errno != EINTR){
363
 
        perror("sigaction");
 
390
        error_plus(0, errno, "sigaction");
364
391
        status = EX_OSERR;
365
392
      }
366
393
      goto failure;
369
396
      ret = sigaction(SIGHUP, &new_action, NULL);
370
397
      if(ret == -1){
371
398
        if(errno != EINTR){
372
 
          perror("sigaction");
 
399
          error_plus(0, errno, "sigaction");
373
400
          status = EX_OSERR;
374
401
        }
375
402
        goto failure;
378
405
    ret = sigaction(SIGTERM, NULL, &old_action);
379
406
    if(ret == -1){
380
407
      if(errno != EINTR){
381
 
        perror("sigaction");
 
408
        error_plus(0, errno, "sigaction");
382
409
        status = EX_OSERR;
383
410
      }
384
411
      goto failure;
387
414
      ret = sigaction(SIGTERM, &new_action, NULL);
388
415
      if(ret == -1){
389
416
        if(errno != EINTR){
390
 
          perror("sigaction");
 
417
          error_plus(0, errno, "sigaction");
391
418
          status = EX_OSERR;
392
419
        }
393
420
        goto failure;
399
426
  /* Write command to FIFO */
400
427
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
401
428
    if(errno != EINTR){
402
 
      perror("usplash_write");
 
429
      error_plus(0, errno, "usplash_write");
403
430
      status = EX_OSERR;
404
431
    }
405
432
    goto failure;
411
438
  
412
439
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
413
440
    if(errno != EINTR){
414
 
      perror("usplash_write");
 
441
      error_plus(0, errno, "usplash_write");
415
442
      status = EX_OSERR;
416
443
    }
417
444
    goto failure;
429
456
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
430
457
  if(outfifo_fd == -1){
431
458
    if(errno != EINTR){
432
 
      perror("open");
 
459
      error_plus(0, errno, "open");
433
460
      status = EX_OSERR;
434
461
    }
435
462
    goto failure;
448
475
      char *tmp = realloc(buf, buf_allocated + blocksize);
449
476
      if(tmp == NULL){
450
477
        if(errno != EINTR){
451
 
          perror("realloc");
 
478
          error_plus(0, errno, "realloc");
452
479
          status = EX_OSERR;
453
480
        }
454
481
        goto failure;
460
487
                buf_allocated - buf_len);
461
488
    if(sret == -1){
462
489
      if(errno != EINTR){
463
 
        perror("read");
 
490
        error_plus(0, errno, "read");
464
491
        status = EX_OSERR;
465
492
      }
466
493
      TEMP_FAILURE_RETRY(close(outfifo_fd));
475
502
  ret = close(outfifo_fd);
476
503
  if(ret == -1){
477
504
    if(errno != EINTR){
478
 
      perror("close");
 
505
      error_plus(0, errno, "close");
479
506
      status = EX_OSERR;
480
507
    }
481
508
    goto failure;
488
515
  
489
516
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
490
517
    if(errno != EINTR){
491
 
      perror("usplash_write");
 
518
      error_plus(0, errno, "usplash_write");
492
519
      status = EX_OSERR;
493
520
    }
494
521
    goto failure;
501
528
  ret = close(fifo_fd);
502
529
  if(ret == -1){
503
530
    if(errno != EINTR){
504
 
      perror("close");
 
531
      error_plus(0, errno, "close");
505
532
      status = EX_OSERR;
506
533
    }
507
534
    goto failure;
515
542
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
516
543
      if(sret == -1){
517
544
        if(errno != EINTR){
518
 
          perror("write");
 
545
          error_plus(0, errno, "write");
519
546
          status = EX_OSERR;
520
547
        }
521
548
        goto failure;
552
579
  if(fifo_fd != -1){
553
580
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
554
581
    if(ret == -1 and errno != EINTR){
555
 
      perror("close");
 
582
      error_plus(0, errno, "close");
556
583
    }
557
584
    fifo_fd = -1;
558
585
  }
561
588
  if(outfifo_fd != -1){
562
589
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
563
590
    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
 
  }
 
591
      error_plus(0, errno, "close");
 
592
    }
 
593
  }
 
594
  
 
595
  /* Create argv for new usplash*/
 
596
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
 
597
                               * sizeof(char *)); /* Count args */
 
598
  if(cmdline_argv == NULL){
 
599
    error_plus(0, errno, "malloc");
 
600
    return status;
 
601
  }
 
602
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
 
603
  
589
604
  /* Kill old usplash */
590
605
  kill(usplash_pid, SIGTERM);
591
606
  sleep(2);
602
617
       the real user ID (_mandos) */
603
618
    ret = setuid(geteuid());
604
619
    if(ret == -1){
605
 
      perror("setuid");
 
620
      error_plus(0, errno, "setuid");
606
621
    }
607
622
    
608
623
    setsid();
609
624
    ret = chdir("/");
 
625
    if(ret == -1){
 
626
      error_plus(0, errno, "chdir");
 
627
      _exit(EX_OSERR);
 
628
    }
610
629
/*     if(fork() != 0){ */
611
630
/*       _exit(EXIT_SUCCESS); */
612
631
/*     } */
613
632
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
614
633
    if(ret == -1){
615
 
      perror("dup2");
 
634
      error_plus(0, errno, "dup2");
616
635
      _exit(EX_OSERR);
617
636
    }
618
637
    
619
638
    execv(usplash_name, cmdline_argv);
620
639
    if(not interrupted_by_signal){
621
 
      perror("execv");
 
640
      error_plus(0, errno, "execv");
622
641
    }
623
642
    free(cmdline);
624
643
    free(cmdline_argv);
629
648
  sleep(2);
630
649
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
631
650
    if(errno != EINTR){
632
 
      perror("usplash_write");
 
651
      error_plus(0, errno, "usplash_write");
633
652
    }
634
653
  }
635
654
  
637
656
  if(fifo_fd != -1){
638
657
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
639
658
    if(ret == -1 and errno != EINTR){
640
 
      perror("close");
 
659
      error_plus(0, errno, "close");
641
660
    }
642
661
    fifo_fd = -1;
643
662
  }
648
667
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
649
668
                                            &signal_action, NULL));
650
669
    if(ret == -1){
651
 
      perror("sigaction");
 
670
      error_plus(0, errno, "sigaction");
652
671
    }
653
672
    do {
654
673
      ret = raise(signal_received);
655
674
    } while(ret != 0 and errno == EINTR);
656
675
    if(ret != 0){
657
 
      perror("raise");
 
676
      error_plus(0, errno, "raise");
658
677
      abort();
659
678
    }
660
679
    TEMP_FAILURE_RETRY(pause());