/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 at bsnet
  • Date: 2010-09-13 06:19:45 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100913061945-y6qw52q01ptn7q71
* mandos-keygen: Don't use "echo -e".

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