/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: 2009-09-19 17:41:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090919174118-274yt9wptmtx0ykn
* plugins.d/password-prompt.c (main): Fix "-Wconversion" warning.

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-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 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
 
#include <string.h>             /* strlen(), memcmp(), strerror() */
39
 
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(), fprintf() */
 
37
#include <string.h>             /* strlen(), memcmp() */
 
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
 
#include <stdarg.h>             /* va_list, va_start(), ... */
54
50
 
55
51
sig_atomic_t interrupted_by_signal = 0;
56
52
int signal_received;
57
53
const char usplash_name[] = "/sbin/usplash";
58
54
 
59
 
/* Function to use when printing errors */
60
 
void error_plus(int status, int errnum, const char *formatstring, ...){
61
 
  va_list ap;
62
 
  char *text;
63
 
  int ret;
64
 
  
65
 
  va_start(ap, formatstring);
66
 
  ret = vasprintf(&text, formatstring, ap);
67
 
  if (ret == -1){
68
 
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
69
 
    vfprintf(stderr, formatstring, ap);
70
 
    fprintf(stderr, ": ");
71
 
    fprintf(stderr, "%s\n", strerror(errnum));
72
 
    error(status, errno, "vasprintf while printing error");
73
 
    return;
74
 
  }
75
 
  fprintf(stderr, "Mandos plugin ");
76
 
  error(status, errnum, "%s", text);
77
 
  free(text);
78
 
}
79
 
 
80
55
static void termination_handler(int signum){
81
56
  if(interrupted_by_signal){
82
57
    return;
177
152
  size_t cmdline_len = 0;
178
153
  DIR *proc_dir = opendir("/proc");
179
154
  if(proc_dir == NULL){
180
 
    error_plus(0, errno, "opendir");
 
155
    perror("opendir");
181
156
    return -1;
182
157
  }
183
158
  errno = 0;
205
180
      char *exe_link;
206
181
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
207
182
      if(ret == -1){
208
 
        error_plus(0, errno, "asprintf");
 
183
        perror("asprintf");
209
184
        goto fail_find_usplash;
210
185
      }
211
186
      
217
192
          free(exe_link);
218
193
          continue;
219
194
        }
220
 
        error_plus(0, errno, "lstat");
 
195
        perror("lstat");
221
196
        free(exe_link);
222
197
        goto fail_find_usplash;
223
198
      }
248
223
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
249
224
                       proc_ent->d_name);
250
225
        if(ret == -1){
251
 
          error_plus(0, errno, "asprintf");
 
226
          perror("asprintf");
252
227
          goto fail_find_usplash;
253
228
        }
254
229
        cl_fd = open(cmdline_filename, O_RDONLY);
255
230
        free(cmdline_filename);
256
231
        if(cl_fd == -1){
257
 
          error_plus(0, errno, "open");
 
232
          perror("open");
258
233
          goto fail_find_usplash;
259
234
        }
260
235
      }
266
241
        if(cmdline_len + blocksize > cmdline_allocated){
267
242
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
268
243
          if(tmp == NULL){
269
 
            error_plus(0, errno, "realloc");
 
244
            perror("realloc");
270
245
            close(cl_fd);
271
246
            goto fail_find_usplash;
272
247
          }
277
252
        sret = read(cl_fd, cmdline + cmdline_len,
278
253
                    cmdline_allocated - cmdline_len);
279
254
        if(sret == -1){
280
 
          error_plus(0, errno, "read");
 
255
          perror("read");
281
256
          close(cl_fd);
282
257
          goto fail_find_usplash;
283
258
        }
285
260
      } while(sret != 0);
286
261
      ret = close(cl_fd);
287
262
      if(ret == -1){
288
 
        error_plus(0, errno, "close");
 
263
        perror("close");
289
264
        goto fail_find_usplash;
290
265
      }
291
266
    }
292
267
    /* Close directory */
293
268
    ret = closedir(proc_dir);
294
269
    if(ret == -1){
295
 
      error_plus(0, errno, "closedir");
 
270
      perror("closedir");
296
271
      goto fail_find_usplash;
297
272
    }
298
273
    /* Success */
322
297
  size_t buf_len = 0;
323
298
  pid_t usplash_pid = -1;
324
299
  bool usplash_accessed = false;
325
 
  int status = EXIT_FAILURE;    /* Default failure exit status */
326
300
  
327
301
  char *prompt = makeprompt();
328
302
  if(prompt == NULL){
329
 
    status = EX_OSERR;
330
303
    goto failure;
331
304
  }
332
305
  
335
308
  size_t cmdline_len = 0;
336
309
  usplash_pid = find_usplash(&cmdline, &cmdline_len);
337
310
  if(usplash_pid == 0){
338
 
    status = EX_UNAVAILABLE;
339
311
    goto failure;
340
312
  }
341
313
  
347
319
    sigemptyset(&new_action.sa_mask);
348
320
    ret = sigaddset(&new_action.sa_mask, SIGINT);
349
321
    if(ret == -1){
350
 
      error_plus(0, errno, "sigaddset");
351
 
      status = EX_OSERR;
 
322
      perror("sigaddset");
352
323
      goto failure;
353
324
    }
354
325
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
355
326
    if(ret == -1){
356
 
      error_plus(0, errno, "sigaddset");
357
 
      status = EX_OSERR;
 
327
      perror("sigaddset");
358
328
      goto failure;
359
329
    }
360
330
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
361
331
    if(ret == -1){
362
 
      error_plus(0, errno, "sigaddset");
363
 
      status = EX_OSERR;
 
332
      perror("sigaddset");
364
333
      goto failure;
365
334
    }
366
335
    ret = sigaction(SIGINT, NULL, &old_action);
367
336
    if(ret == -1){
368
337
      if(errno != EINTR){
369
 
        error_plus(0, errno, "sigaction");
370
 
        status = EX_OSERR;
 
338
        perror("sigaction");
371
339
      }
372
340
      goto failure;
373
341
    }
375
343
      ret = sigaction(SIGINT, &new_action, NULL);
376
344
      if(ret == -1){
377
345
        if(errno != EINTR){
378
 
          error_plus(0, errno, "sigaction");
379
 
          status = EX_OSERR;
 
346
          perror("sigaction");
380
347
        }
381
348
        goto failure;
382
349
      }
384
351
    ret = sigaction(SIGHUP, NULL, &old_action);
385
352
    if(ret == -1){
386
353
      if(errno != EINTR){
387
 
        error_plus(0, errno, "sigaction");
388
 
        status = EX_OSERR;
 
354
        perror("sigaction");
389
355
      }
390
356
      goto failure;
391
357
    }
393
359
      ret = sigaction(SIGHUP, &new_action, NULL);
394
360
      if(ret == -1){
395
361
        if(errno != EINTR){
396
 
          error_plus(0, errno, "sigaction");
397
 
          status = EX_OSERR;
 
362
          perror("sigaction");
398
363
        }
399
364
        goto failure;
400
365
      }
402
367
    ret = sigaction(SIGTERM, NULL, &old_action);
403
368
    if(ret == -1){
404
369
      if(errno != EINTR){
405
 
        error_plus(0, errno, "sigaction");
406
 
        status = EX_OSERR;
 
370
        perror("sigaction");
407
371
      }
408
372
      goto failure;
409
373
    }
411
375
      ret = sigaction(SIGTERM, &new_action, NULL);
412
376
      if(ret == -1){
413
377
        if(errno != EINTR){
414
 
          error_plus(0, errno, "sigaction");
415
 
          status = EX_OSERR;
 
378
          perror("sigaction");
416
379
        }
417
380
        goto failure;
418
381
      }
423
386
  /* Write command to FIFO */
424
387
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
425
388
    if(errno != EINTR){
426
 
      error_plus(0, errno, "usplash_write");
427
 
      status = EX_OSERR;
 
389
      perror("usplash_write");
428
390
    }
429
391
    goto failure;
430
392
  }
435
397
  
436
398
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
437
399
    if(errno != EINTR){
438
 
      error_plus(0, errno, "usplash_write");
439
 
      status = EX_OSERR;
 
400
      perror("usplash_write");
440
401
    }
441
402
    goto failure;
442
403
  }
453
414
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
454
415
  if(outfifo_fd == -1){
455
416
    if(errno != EINTR){
456
 
      error_plus(0, errno, "open");
457
 
      status = EX_OSERR;
 
417
      perror("open");
458
418
    }
459
419
    goto failure;
460
420
  }
472
432
      char *tmp = realloc(buf, buf_allocated + blocksize);
473
433
      if(tmp == NULL){
474
434
        if(errno != EINTR){
475
 
          error_plus(0, errno, "realloc");
476
 
          status = EX_OSERR;
 
435
          perror("realloc");
477
436
        }
478
437
        goto failure;
479
438
      }
484
443
                buf_allocated - buf_len);
485
444
    if(sret == -1){
486
445
      if(errno != EINTR){
487
 
        error_plus(0, errno, "read");
488
 
        status = EX_OSERR;
 
446
        perror("read");
489
447
      }
490
448
      TEMP_FAILURE_RETRY(close(outfifo_fd));
491
449
      goto failure;
499
457
  ret = close(outfifo_fd);
500
458
  if(ret == -1){
501
459
    if(errno != EINTR){
502
 
      error_plus(0, errno, "close");
503
 
      status = EX_OSERR;
 
460
      perror("close");
504
461
    }
505
462
    goto failure;
506
463
  }
512
469
  
513
470
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
514
471
    if(errno != EINTR){
515
 
      error_plus(0, errno, "usplash_write");
516
 
      status = EX_OSERR;
 
472
      perror("usplash_write");
517
473
    }
518
474
    goto failure;
519
475
  }
525
481
  ret = close(fifo_fd);
526
482
  if(ret == -1){
527
483
    if(errno != EINTR){
528
 
      error_plus(0, errno, "close");
529
 
      status = EX_OSERR;
 
484
      perror("close");
530
485
    }
531
486
    goto failure;
532
487
  }
539
494
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
540
495
      if(sret == -1){
541
496
        if(errno != EINTR){
542
 
          error_plus(0, errno, "write");
543
 
          status = EX_OSERR;
 
497
          perror("write");
544
498
        }
545
499
        goto failure;
546
500
      }
569
523
  
570
524
  /* If usplash was never accessed, we can stop now */
571
525
  if(not usplash_accessed){
572
 
    return status;
 
526
    return EXIT_FAILURE;
573
527
  }
574
528
  
575
529
  /* Close FIFO */
576
530
  if(fifo_fd != -1){
577
531
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
578
532
    if(ret == -1 and errno != EINTR){
579
 
      error_plus(0, errno, "close");
 
533
      perror("close");
580
534
    }
581
535
    fifo_fd = -1;
582
536
  }
585
539
  if(outfifo_fd != -1){
586
540
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
587
541
    if(ret == -1){
588
 
      error_plus(0, errno, "close");
589
 
    }
590
 
  }
591
 
  
592
 
  /* Create argv for new usplash*/
593
 
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
594
 
                               * sizeof(char *)); /* Count args */
595
 
  if(cmdline_argv == NULL){
596
 
    error_plus(0, errno, "malloc");
597
 
    return status;
598
 
  }
599
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
600
 
  
 
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
  }
601
567
  /* Kill old usplash */
602
568
  kill(usplash_pid, SIGTERM);
603
569
  sleep(2);
614
580
       the real user ID (_mandos) */
615
581
    ret = setuid(geteuid());
616
582
    if(ret == -1){
617
 
      error_plus(0, errno, "setuid");
 
583
      perror("setuid");
618
584
    }
619
585
    
620
586
    setsid();
621
587
    ret = chdir("/");
622
 
    if(ret == -1){
623
 
      error_plus(0, errno, "chdir");
624
 
      _exit(EX_OSERR);
625
 
    }
626
588
/*     if(fork() != 0){ */
627
589
/*       _exit(EXIT_SUCCESS); */
628
590
/*     } */
629
591
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
630
592
    if(ret == -1){
631
 
      error_plus(0, errno, "dup2");
632
 
      _exit(EX_OSERR);
 
593
      perror("dup2");
 
594
      _exit(EXIT_FAILURE);
633
595
    }
634
596
    
635
597
    execv(usplash_name, cmdline_argv);
636
598
    if(not interrupted_by_signal){
637
 
      error_plus(0, errno, "execv");
 
599
      perror("execv");
638
600
    }
639
601
    free(cmdline);
640
602
    free(cmdline_argv);
641
 
    _exit(EX_OSERR);
 
603
    _exit(EXIT_FAILURE);
642
604
  }
643
605
  free(cmdline);
644
606
  free(cmdline_argv);
645
607
  sleep(2);
646
608
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
647
609
    if(errno != EINTR){
648
 
      error_plus(0, errno, "usplash_write");
 
610
      perror("usplash_write");
649
611
    }
650
612
  }
651
613
  
653
615
  if(fifo_fd != -1){
654
616
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
655
617
    if(ret == -1 and errno != EINTR){
656
 
      error_plus(0, errno, "close");
 
618
      perror("close");
657
619
    }
658
620
    fifo_fd = -1;
659
621
  }
664
626
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
665
627
                                            &signal_action, NULL));
666
628
    if(ret == -1){
667
 
      error_plus(0, errno, "sigaction");
 
629
      perror("sigaction");
668
630
    }
669
631
    do {
670
632
      ret = raise(signal_received);
671
633
    } while(ret != 0 and errno == EINTR);
672
634
    if(ret != 0){
673
 
      error_plus(0, errno, "raise");
 
635
      perror("raise");
674
636
      abort();
675
637
    }
676
638
    TEMP_FAILURE_RETRY(pause());
677
639
  }
678
640
  
679
 
  return status;
 
641
  return EXIT_FAILURE;
680
642
}