/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: 2011-07-13 02:24:39 UTC
  • mfrom: (24.1.174 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20110713022439-wbv6kghshdsc2x24
Merge from Björn.

Show diffs side-by-side

added added

removed removed

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