/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: 2010-09-11 18:34:55 UTC
  • mfrom: (24.1.162 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20100911183455-kmzttvbywdbfbepm
Merge from Björn.

Show diffs side-by-side

added added

removed removed

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