/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

Tags: version-1.0.13-1
* Makefile (version): Changed to "1.0.13".
* NEWS (Version 1.0.13): New entry.
* debian/changelog (1.0.13-1): - '' -

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-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.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>
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(),
40
 
                                   fprintf() */
 
37
#include <string.h>             /* strlen(), memcmp() */
 
38
#include <stdio.h>              /* asprintf(), perror() */
41
39
#include <unistd.h>             /* close(), write(), readlink(),
42
40
                                   read(), STDOUT_FILENO, sleep(),
43
41
                                   fork(), setuid(), geteuid(),
44
42
                                   setsid(), chdir(), dup2(),
45
43
                                   STDERR_FILENO, execv() */
46
44
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
47
 
                                   EXIT_SUCCESS, malloc(), _exit(),
48
 
                                   getenv() */
 
45
                                   EXIT_SUCCESS, malloc(), _exit() */
 
46
#include <stdlib.h>             /* getenv() */
49
47
#include <dirent.h>             /* opendir(), readdir(), closedir() */
50
48
#include <inttypes.h>           /* intmax_t, strtoimax() */
51
49
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
52
 
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
53
 
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
50
 
56
51
sig_atomic_t interrupted_by_signal = 0;
57
52
int signal_received;
58
53
const char usplash_name[] = "/sbin/usplash";
59
54
 
60
 
/* Function to use when printing errors */
61
 
__attribute__((format (gnu_printf, 3, 4)))
62
 
void error_plus(int status, int errnum, const char *formatstring,
63
 
                ...){
64
 
  va_list ap;
65
 
  char *text;
66
 
  int ret;
67
 
  
68
 
  va_start(ap, formatstring);
69
 
  ret = vasprintf(&text, formatstring, ap);
70
 
  if(ret == -1){
71
 
    fprintf(stderr, "Mandos plugin %s: ",
72
 
            program_invocation_short_name);
73
 
    vfprintf(stderr, formatstring, ap);
74
 
    fprintf(stderr, ": ");
75
 
    fprintf(stderr, "%s\n", strerror(errnum));
76
 
    error(status, errno, "vasprintf while printing error");
77
 
    return;
78
 
  }
79
 
  fprintf(stderr, "Mandos plugin ");
80
 
  error(status, errnum, "%s", text);
81
 
  free(text);
82
 
}
83
 
 
84
55
static void termination_handler(int signum){
85
56
  if(interrupted_by_signal){
86
57
    return;
117
88
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
118
89
      if(ret == -1){
119
90
        int e = errno;
120
 
        close(*fifo_fd_r);
 
91
        TEMP_FAILURE_RETRY(close(*fifo_fd_r));
121
92
        errno = e;
122
93
        return false;
123
94
      }
133
104
                 cmd_line_len - written);
134
105
    if(sret == -1){
135
106
      int e = errno;
136
 
      close(*fifo_fd_r);
 
107
      TEMP_FAILURE_RETRY(close(*fifo_fd_r));
137
108
      free(cmd_line_alloc);
138
109
      errno = e;
139
110
      return false;
181
152
  size_t cmdline_len = 0;
182
153
  DIR *proc_dir = opendir("/proc");
183
154
  if(proc_dir == NULL){
184
 
    error_plus(0, errno, "opendir");
 
155
    perror("opendir");
185
156
    return -1;
186
157
  }
187
158
  errno = 0;
209
180
      char *exe_link;
210
181
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
211
182
      if(ret == -1){
212
 
        error_plus(0, errno, "asprintf");
 
183
        perror("asprintf");
213
184
        goto fail_find_usplash;
214
185
      }
215
186
      
221
192
          free(exe_link);
222
193
          continue;
223
194
        }
224
 
        error_plus(0, errno, "lstat");
 
195
        perror("lstat");
225
196
        free(exe_link);
226
197
        goto fail_find_usplash;
227
198
      }
252
223
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
253
224
                       proc_ent->d_name);
254
225
        if(ret == -1){
255
 
          error_plus(0, errno, "asprintf");
 
226
          perror("asprintf");
256
227
          goto fail_find_usplash;
257
228
        }
258
229
        cl_fd = open(cmdline_filename, O_RDONLY);
259
230
        free(cmdline_filename);
260
231
        if(cl_fd == -1){
261
 
          error_plus(0, errno, "open");
 
232
          perror("open");
262
233
          goto fail_find_usplash;
263
234
        }
264
235
      }
270
241
        if(cmdline_len + blocksize > cmdline_allocated){
271
242
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
272
243
          if(tmp == NULL){
273
 
            error_plus(0, errno, "realloc");
 
244
            perror("realloc");
274
245
            close(cl_fd);
275
246
            goto fail_find_usplash;
276
247
          }
281
252
        sret = read(cl_fd, cmdline + cmdline_len,
282
253
                    cmdline_allocated - cmdline_len);
283
254
        if(sret == -1){
284
 
          error_plus(0, errno, "read");
 
255
          perror("read");
285
256
          close(cl_fd);
286
257
          goto fail_find_usplash;
287
258
        }
289
260
      } while(sret != 0);
290
261
      ret = close(cl_fd);
291
262
      if(ret == -1){
292
 
        error_plus(0, errno, "close");
 
263
        perror("close");
293
264
        goto fail_find_usplash;
294
265
      }
295
266
    }
296
267
    /* Close directory */
297
268
    ret = closedir(proc_dir);
298
269
    if(ret == -1){
299
 
      error_plus(0, errno, "closedir");
 
270
      perror("closedir");
300
271
      goto fail_find_usplash;
301
272
    }
302
273
    /* Success */
326
297
  size_t buf_len = 0;
327
298
  pid_t usplash_pid = -1;
328
299
  bool usplash_accessed = false;
329
 
  int status = EXIT_FAILURE;    /* Default failure exit status */
330
300
  
331
301
  char *prompt = makeprompt();
332
302
  if(prompt == NULL){
333
 
    status = EX_OSERR;
334
303
    goto failure;
335
304
  }
336
305
  
339
308
  size_t cmdline_len = 0;
340
309
  usplash_pid = find_usplash(&cmdline, &cmdline_len);
341
310
  if(usplash_pid == 0){
342
 
    status = EX_UNAVAILABLE;
343
311
    goto failure;
344
312
  }
345
313
  
351
319
    sigemptyset(&new_action.sa_mask);
352
320
    ret = sigaddset(&new_action.sa_mask, SIGINT);
353
321
    if(ret == -1){
354
 
      error_plus(0, errno, "sigaddset");
355
 
      status = EX_OSERR;
 
322
      perror("sigaddset");
356
323
      goto failure;
357
324
    }
358
325
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
359
326
    if(ret == -1){
360
 
      error_plus(0, errno, "sigaddset");
361
 
      status = EX_OSERR;
 
327
      perror("sigaddset");
362
328
      goto failure;
363
329
    }
364
330
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
365
331
    if(ret == -1){
366
 
      error_plus(0, errno, "sigaddset");
367
 
      status = EX_OSERR;
 
332
      perror("sigaddset");
368
333
      goto failure;
369
334
    }
370
335
    ret = sigaction(SIGINT, NULL, &old_action);
371
336
    if(ret == -1){
372
337
      if(errno != EINTR){
373
 
        error_plus(0, errno, "sigaction");
374
 
        status = EX_OSERR;
 
338
        perror("sigaction");
375
339
      }
376
340
      goto failure;
377
341
    }
379
343
      ret = sigaction(SIGINT, &new_action, NULL);
380
344
      if(ret == -1){
381
345
        if(errno != EINTR){
382
 
          error_plus(0, errno, "sigaction");
383
 
          status = EX_OSERR;
 
346
          perror("sigaction");
384
347
        }
385
348
        goto failure;
386
349
      }
388
351
    ret = sigaction(SIGHUP, NULL, &old_action);
389
352
    if(ret == -1){
390
353
      if(errno != EINTR){
391
 
        error_plus(0, errno, "sigaction");
392
 
        status = EX_OSERR;
 
354
        perror("sigaction");
393
355
      }
394
356
      goto failure;
395
357
    }
397
359
      ret = sigaction(SIGHUP, &new_action, NULL);
398
360
      if(ret == -1){
399
361
        if(errno != EINTR){
400
 
          error_plus(0, errno, "sigaction");
401
 
          status = EX_OSERR;
 
362
          perror("sigaction");
402
363
        }
403
364
        goto failure;
404
365
      }
406
367
    ret = sigaction(SIGTERM, NULL, &old_action);
407
368
    if(ret == -1){
408
369
      if(errno != EINTR){
409
 
        error_plus(0, errno, "sigaction");
410
 
        status = EX_OSERR;
 
370
        perror("sigaction");
411
371
      }
412
372
      goto failure;
413
373
    }
415
375
      ret = sigaction(SIGTERM, &new_action, NULL);
416
376
      if(ret == -1){
417
377
        if(errno != EINTR){
418
 
          error_plus(0, errno, "sigaction");
419
 
          status = EX_OSERR;
 
378
          perror("sigaction");
420
379
        }
421
380
        goto failure;
422
381
      }
427
386
  /* Write command to FIFO */
428
387
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
429
388
    if(errno != EINTR){
430
 
      error_plus(0, errno, "usplash_write");
431
 
      status = EX_OSERR;
 
389
      perror("usplash_write");
432
390
    }
433
391
    goto failure;
434
392
  }
439
397
  
440
398
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
441
399
    if(errno != EINTR){
442
 
      error_plus(0, errno, "usplash_write");
443
 
      status = EX_OSERR;
 
400
      perror("usplash_write");
444
401
    }
445
402
    goto failure;
446
403
  }
457
414
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
458
415
  if(outfifo_fd == -1){
459
416
    if(errno != EINTR){
460
 
      error_plus(0, errno, "open");
461
 
      status = EX_OSERR;
 
417
      perror("open");
462
418
    }
463
419
    goto failure;
464
420
  }
476
432
      char *tmp = realloc(buf, buf_allocated + blocksize);
477
433
      if(tmp == NULL){
478
434
        if(errno != EINTR){
479
 
          error_plus(0, errno, "realloc");
480
 
          status = EX_OSERR;
 
435
          perror("realloc");
481
436
        }
482
437
        goto failure;
483
438
      }
488
443
                buf_allocated - buf_len);
489
444
    if(sret == -1){
490
445
      if(errno != EINTR){
491
 
        error_plus(0, errno, "read");
492
 
        status = EX_OSERR;
 
446
        perror("read");
493
447
      }
494
 
      close(outfifo_fd);
 
448
      TEMP_FAILURE_RETRY(close(outfifo_fd));
495
449
      goto failure;
496
450
    }
497
451
    if(interrupted_by_signal){
503
457
  ret = close(outfifo_fd);
504
458
  if(ret == -1){
505
459
    if(errno != EINTR){
506
 
      error_plus(0, errno, "close");
507
 
      status = EX_OSERR;
 
460
      perror("close");
508
461
    }
509
462
    goto failure;
510
463
  }
516
469
  
517
470
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
518
471
    if(errno != EINTR){
519
 
      error_plus(0, errno, "usplash_write");
520
 
      status = EX_OSERR;
 
472
      perror("usplash_write");
521
473
    }
522
474
    goto failure;
523
475
  }
529
481
  ret = close(fifo_fd);
530
482
  if(ret == -1){
531
483
    if(errno != EINTR){
532
 
      error_plus(0, errno, "close");
533
 
      status = EX_OSERR;
 
484
      perror("close");
534
485
    }
535
486
    goto failure;
536
487
  }
543
494
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
544
495
      if(sret == -1){
545
496
        if(errno != EINTR){
546
 
          error_plus(0, errno, "write");
547
 
          status = EX_OSERR;
 
497
          perror("write");
548
498
        }
549
499
        goto failure;
550
500
      }
573
523
  
574
524
  /* If usplash was never accessed, we can stop now */
575
525
  if(not usplash_accessed){
576
 
    return status;
 
526
    return EXIT_FAILURE;
577
527
  }
578
528
  
579
529
  /* Close FIFO */
580
530
  if(fifo_fd != -1){
581
 
    ret = close(fifo_fd);
 
531
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
582
532
    if(ret == -1 and errno != EINTR){
583
 
      error_plus(0, errno, "close");
 
533
      perror("close");
584
534
    }
585
535
    fifo_fd = -1;
586
536
  }
587
537
  
588
538
  /* Close output FIFO */
589
539
  if(outfifo_fd != -1){
590
 
    ret = close(outfifo_fd);
 
540
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
591
541
    if(ret == -1){
592
 
      error_plus(0, errno, "close");
593
 
    }
594
 
  }
595
 
  
596
 
  /* Create argv for new usplash*/
597
 
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
598
 
                               * sizeof(char *)); /* Count args */
599
 
  if(cmdline_argv == NULL){
600
 
    error_plus(0, errno, "malloc");
601
 
    return status;
602
 
  }
603
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
604
 
  
 
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
  }
605
567
  /* Kill old usplash */
606
568
  kill(usplash_pid, SIGTERM);
607
569
  sleep(2);
618
580
       the real user ID (_mandos) */
619
581
    ret = setuid(geteuid());
620
582
    if(ret == -1){
621
 
      error_plus(0, errno, "setuid");
 
583
      perror("setuid");
622
584
    }
623
585
    
624
586
    setsid();
625
587
    ret = chdir("/");
626
 
    if(ret == -1){
627
 
      error_plus(0, errno, "chdir");
628
 
      _exit(EX_OSERR);
629
 
    }
630
588
/*     if(fork() != 0){ */
631
589
/*       _exit(EXIT_SUCCESS); */
632
590
/*     } */
633
591
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
634
592
    if(ret == -1){
635
 
      error_plus(0, errno, "dup2");
636
 
      _exit(EX_OSERR);
 
593
      perror("dup2");
 
594
      _exit(EXIT_FAILURE);
637
595
    }
638
596
    
639
597
    execv(usplash_name, cmdline_argv);
640
598
    if(not interrupted_by_signal){
641
 
      error_plus(0, errno, "execv");
 
599
      perror("execv");
642
600
    }
643
601
    free(cmdline);
644
602
    free(cmdline_argv);
645
 
    _exit(EX_OSERR);
 
603
    _exit(EXIT_FAILURE);
646
604
  }
647
605
  free(cmdline);
648
606
  free(cmdline_argv);
649
607
  sleep(2);
650
608
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
651
609
    if(errno != EINTR){
652
 
      error_plus(0, errno, "usplash_write");
 
610
      perror("usplash_write");
653
611
    }
654
612
  }
655
613
  
656
614
  /* Close FIFO (again) */
657
615
  if(fifo_fd != -1){
658
 
    ret = close(fifo_fd);
 
616
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
659
617
    if(ret == -1 and errno != EINTR){
660
 
      error_plus(0, errno, "close");
 
618
      perror("close");
661
619
    }
662
620
    fifo_fd = -1;
663
621
  }
668
626
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
669
627
                                            &signal_action, NULL));
670
628
    if(ret == -1){
671
 
      error_plus(0, errno, "sigaction");
 
629
      perror("sigaction");
672
630
    }
673
631
    do {
674
632
      ret = raise(signal_received);
675
633
    } while(ret != 0 and errno == EINTR);
676
634
    if(ret != 0){
677
 
      error_plus(0, errno, "raise");
 
635
      perror("raise");
678
636
      abort();
679
637
    }
680
638
    TEMP_FAILURE_RETRY(pause());
681
639
  }
682
640
  
683
 
  return status;
 
641
  return EXIT_FAILURE;
684
642
}