/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

Merge from trunk.  Just a minor release to make it lintian clean.

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