/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-10-02 17:41:05 UTC
  • mto: (24.1.169 mandos)
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: teddy@fukt.bsnet.se-20101002174105-xfqa0j8y1puvedo3
* debian/source/format: New; contains "3.0 (quilt)".

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-2018, 2021 Teddy Hogeborn
6
 
 * Copyright © 2008-2018, 2021 Björn Påhlsson
7
 
 * 
8
 
 * This file is part of Mandos.
9
 
 * 
10
 
 * Mandos is free software: you can redistribute it and/or modify it
11
 
 * under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 * 
15
 
 * Mandos is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 Björn Påhlsson
 
7
 * 
 
8
 * This program is free software: you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation, either version 3 of the
 
11
 * License, or (at your option) any later version.
 
12
 * 
 
13
 * This program is distributed in the hope that it will be useful, but
16
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
16
 * General Public License for more details.
19
17
 * 
20
18
 * You should have received a copy of the GNU General Public License
21
 
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
22
21
 * 
23
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
 
#define _GNU_SOURCE             /* vasprintf(),
27
 
                                   program_invocation_short_name,
28
 
                                   asprintf(), TEMP_FAILURE_RETRY() */
29
 
#include <sys/types.h>          /* sig_atomic_t, pid_t, setuid(),
30
 
                                   geteuid(), setsid() */
31
 
#include <stdarg.h>             /* va_list, va_start(), vfprintf() */
32
 
#include <stdio.h>              /* vasprintf(), fprintf(), stderr,
33
 
                                   vfprintf(), asprintf() */
34
 
#include <errno.h>              /* program_invocation_short_name,
35
 
                                   errno, ENOENT, EINTR */
36
 
#include <string.h>             /* strerror(), strlen(), memcmp() */
37
 
#include <error.h>              /* error() */
38
 
#include <stdlib.h>             /* free(), getenv(), realloc(),
39
 
                                   EXIT_FAILURE, EXIT_SUCCESS,
40
 
                                   malloc(), abort() */
 
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
 
26
#include <signal.h>             /* sig_atomic_t, struct sigaction,
 
27
                                   sigemptyset(), sigaddset(), SIGINT,
 
28
                                   SIGHUP, SIGTERM, sigaction(),
 
29
                                   SIG_IGN, kill(), SIGKILL */
41
30
#include <stdbool.h>            /* bool, false, true */
42
31
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
43
 
#include <stddef.h>             /* size_t, NULL */
44
 
#include <unistd.h>             /* close(), ssize_t, write(),
45
 
                                   readlink(), read(), STDOUT_FILENO,
46
 
                                   sleep(), fork(), setuid(),
47
 
                                   geteuid(), setsid(), chdir(),
48
 
                                   _exit(), dup2(), STDERR_FILENO,
49
 
                                   execv(), TEMP_FAILURE_RETRY(),
50
 
                                   pause() */
51
 
#include <dirent.h>             /* DIR, opendir(), struct dirent,
52
 
                                   readdir(), closedir() */
 
32
#include <iso646.h>             /* and, or, not*/
 
33
#include <errno.h>              /* errno, EINTR */
 
34
#include <error.h>
 
35
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
 
36
                                   dirent */
 
37
#include <stddef.h>             /* NULL */
 
38
#include <string.h>             /* strlen(), memcmp() */
 
39
#include <stdio.h>              /* asprintf()*/
 
40
#include <unistd.h>             /* close(), write(), readlink(),
 
41
                                   read(), STDOUT_FILENO, sleep(),
 
42
                                   fork(), setuid(), geteuid(),
 
43
                                   setsid(), chdir(), dup2(),
 
44
                                   STDERR_FILENO, execv() */
 
45
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
 
46
                                   EXIT_SUCCESS, malloc(), _exit(),
 
47
                                   getenv() */
 
48
#include <dirent.h>             /* opendir(), readdir(), closedir() */
53
49
#include <inttypes.h>           /* intmax_t, strtoimax() */
54
 
#include <iso646.h>             /* or, not, and */
55
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK() */
 
50
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
56
51
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
57
 
#include <signal.h>             /* struct sigaction, sigemptyset(),
58
 
                                   sigaddset(), SIGINT, SIGHUP,
59
 
                                   SIGTERM, SIG_IGN, kill(), SIGKILL,
60
 
                                   SIG_DFL, raise() */
61
52
#include <argz.h>               /* argz_count(), argz_extract() */
62
53
 
63
54
sig_atomic_t interrupted_by_signal = 0;
64
55
int signal_received;
65
56
const char usplash_name[] = "/sbin/usplash";
66
57
 
67
 
/* Function to use when printing errors */
68
 
__attribute__((format (gnu_printf, 3, 4)))
69
 
void error_plus(int status, int errnum, const char *formatstring,
70
 
                ...){
71
 
  va_list ap;
72
 
  char *text;
73
 
  int ret;
74
 
  
75
 
  va_start(ap, formatstring);
76
 
  ret = vasprintf(&text, formatstring, ap);
77
 
  if(ret == -1){
78
 
    fprintf(stderr, "Mandos plugin %s: ",
79
 
            program_invocation_short_name);
80
 
    vfprintf(stderr, formatstring, ap);
81
 
    fprintf(stderr, ": ");
82
 
    fprintf(stderr, "%s\n", strerror(errnum));
83
 
    error(status, errno, "vasprintf while printing error");
84
 
    return;
85
 
  }
86
 
  fprintf(stderr, "Mandos plugin ");
87
 
  error(status, errnum, "%s", text);
88
 
  free(text);
89
 
}
90
 
 
91
58
static void termination_handler(int signum){
92
59
  if(interrupted_by_signal){
93
60
    return;
124
91
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
125
92
      if(ret == -1){
126
93
        int e = errno;
127
 
        close(*fifo_fd_r);
 
94
        TEMP_FAILURE_RETRY(close(*fifo_fd_r));
128
95
        errno = e;
129
96
        return false;
130
97
      }
140
107
                 cmd_line_len - written);
141
108
    if(sret == -1){
142
109
      int e = errno;
143
 
      close(*fifo_fd_r);
 
110
      TEMP_FAILURE_RETRY(close(*fifo_fd_r));
144
111
      free(cmd_line_alloc);
145
112
      errno = e;
146
113
      return false;
188
155
  size_t cmdline_len = 0;
189
156
  DIR *proc_dir = opendir("/proc");
190
157
  if(proc_dir == NULL){
191
 
    error_plus(0, errno, "opendir");
 
158
    error(0, errno, "opendir");
192
159
    return -1;
193
160
  }
194
161
  errno = 0;
216
183
      char *exe_link;
217
184
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
218
185
      if(ret == -1){
219
 
        error_plus(0, errno, "asprintf");
 
186
        error(0, errno, "asprintf");
220
187
        goto fail_find_usplash;
221
188
      }
222
189
      
228
195
          free(exe_link);
229
196
          continue;
230
197
        }
231
 
        error_plus(0, errno, "lstat");
 
198
        error(0, errno, "lstat");
232
199
        free(exe_link);
233
200
        goto fail_find_usplash;
234
201
      }
259
226
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
260
227
                       proc_ent->d_name);
261
228
        if(ret == -1){
262
 
          error_plus(0, errno, "asprintf");
 
229
          error(0, errno, "asprintf");
263
230
          goto fail_find_usplash;
264
231
        }
265
232
        cl_fd = open(cmdline_filename, O_RDONLY);
266
233
        free(cmdline_filename);
267
234
        if(cl_fd == -1){
268
 
          error_plus(0, errno, "open");
 
235
          error(0, errno, "open");
269
236
          goto fail_find_usplash;
270
237
        }
271
238
      }
277
244
        if(cmdline_len + blocksize > cmdline_allocated){
278
245
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
279
246
          if(tmp == NULL){
280
 
            error_plus(0, errno, "realloc");
 
247
            error(0, errno, "realloc");
281
248
            close(cl_fd);
282
249
            goto fail_find_usplash;
283
250
          }
288
255
        sret = read(cl_fd, cmdline + cmdline_len,
289
256
                    cmdline_allocated - cmdline_len);
290
257
        if(sret == -1){
291
 
          error_plus(0, errno, "read");
 
258
          error(0, errno, "read");
292
259
          close(cl_fd);
293
260
          goto fail_find_usplash;
294
261
        }
296
263
      } while(sret != 0);
297
264
      ret = close(cl_fd);
298
265
      if(ret == -1){
299
 
        error_plus(0, errno, "close");
 
266
        error(0, errno, "close");
300
267
        goto fail_find_usplash;
301
268
      }
302
269
    }
303
270
    /* Close directory */
304
271
    ret = closedir(proc_dir);
305
272
    if(ret == -1){
306
 
      error_plus(0, errno, "closedir");
 
273
      error(0, errno, "closedir");
307
274
      goto fail_find_usplash;
308
275
    }
309
276
    /* Success */
358
325
    sigemptyset(&new_action.sa_mask);
359
326
    ret = sigaddset(&new_action.sa_mask, SIGINT);
360
327
    if(ret == -1){
361
 
      error_plus(0, errno, "sigaddset");
 
328
      error(0, errno, "sigaddset");
362
329
      status = EX_OSERR;
363
330
      goto failure;
364
331
    }
365
332
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
366
333
    if(ret == -1){
367
 
      error_plus(0, errno, "sigaddset");
 
334
      error(0, errno, "sigaddset");
368
335
      status = EX_OSERR;
369
336
      goto failure;
370
337
    }
371
338
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
372
339
    if(ret == -1){
373
 
      error_plus(0, errno, "sigaddset");
 
340
      error(0, errno, "sigaddset");
374
341
      status = EX_OSERR;
375
342
      goto failure;
376
343
    }
377
344
    ret = sigaction(SIGINT, NULL, &old_action);
378
345
    if(ret == -1){
379
346
      if(errno != EINTR){
380
 
        error_plus(0, errno, "sigaction");
 
347
        error(0, errno, "sigaction");
381
348
        status = EX_OSERR;
382
349
      }
383
350
      goto failure;
386
353
      ret = sigaction(SIGINT, &new_action, NULL);
387
354
      if(ret == -1){
388
355
        if(errno != EINTR){
389
 
          error_plus(0, errno, "sigaction");
 
356
          error(0, errno, "sigaction");
390
357
          status = EX_OSERR;
391
358
        }
392
359
        goto failure;
395
362
    ret = sigaction(SIGHUP, NULL, &old_action);
396
363
    if(ret == -1){
397
364
      if(errno != EINTR){
398
 
        error_plus(0, errno, "sigaction");
 
365
        error(0, errno, "sigaction");
399
366
        status = EX_OSERR;
400
367
      }
401
368
      goto failure;
404
371
      ret = sigaction(SIGHUP, &new_action, NULL);
405
372
      if(ret == -1){
406
373
        if(errno != EINTR){
407
 
          error_plus(0, errno, "sigaction");
 
374
          error(0, errno, "sigaction");
408
375
          status = EX_OSERR;
409
376
        }
410
377
        goto failure;
413
380
    ret = sigaction(SIGTERM, NULL, &old_action);
414
381
    if(ret == -1){
415
382
      if(errno != EINTR){
416
 
        error_plus(0, errno, "sigaction");
 
383
        error(0, errno, "sigaction");
417
384
        status = EX_OSERR;
418
385
      }
419
386
      goto failure;
422
389
      ret = sigaction(SIGTERM, &new_action, NULL);
423
390
      if(ret == -1){
424
391
        if(errno != EINTR){
425
 
          error_plus(0, errno, "sigaction");
 
392
          error(0, errno, "sigaction");
426
393
          status = EX_OSERR;
427
394
        }
428
395
        goto failure;
434
401
  /* Write command to FIFO */
435
402
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
436
403
    if(errno != EINTR){
437
 
      error_plus(0, errno, "usplash_write");
 
404
      error(0, errno, "usplash_write");
438
405
      status = EX_OSERR;
439
406
    }
440
407
    goto failure;
446
413
  
447
414
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
448
415
    if(errno != EINTR){
449
 
      error_plus(0, errno, "usplash_write");
 
416
      error(0, errno, "usplash_write");
450
417
      status = EX_OSERR;
451
418
    }
452
419
    goto failure;
464
431
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
465
432
  if(outfifo_fd == -1){
466
433
    if(errno != EINTR){
467
 
      error_plus(0, errno, "open");
 
434
      error(0, errno, "open");
468
435
      status = EX_OSERR;
469
436
    }
470
437
    goto failure;
483
450
      char *tmp = realloc(buf, buf_allocated + blocksize);
484
451
      if(tmp == NULL){
485
452
        if(errno != EINTR){
486
 
          error_plus(0, errno, "realloc");
 
453
          error(0, errno, "realloc");
487
454
          status = EX_OSERR;
488
455
        }
489
456
        goto failure;
495
462
                buf_allocated - buf_len);
496
463
    if(sret == -1){
497
464
      if(errno != EINTR){
498
 
        error_plus(0, errno, "read");
 
465
        error(0, errno, "read");
499
466
        status = EX_OSERR;
500
467
      }
501
 
      close(outfifo_fd);
 
468
      TEMP_FAILURE_RETRY(close(outfifo_fd));
502
469
      goto failure;
503
470
    }
504
471
    if(interrupted_by_signal){
510
477
  ret = close(outfifo_fd);
511
478
  if(ret == -1){
512
479
    if(errno != EINTR){
513
 
      error_plus(0, errno, "close");
 
480
      error(0, errno, "close");
514
481
      status = EX_OSERR;
515
482
    }
516
483
    goto failure;
523
490
  
524
491
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
525
492
    if(errno != EINTR){
526
 
      error_plus(0, errno, "usplash_write");
 
493
      error(0, errno, "usplash_write");
527
494
      status = EX_OSERR;
528
495
    }
529
496
    goto failure;
536
503
  ret = close(fifo_fd);
537
504
  if(ret == -1){
538
505
    if(errno != EINTR){
539
 
      error_plus(0, errno, "close");
 
506
      error(0, errno, "close");
540
507
      status = EX_OSERR;
541
508
    }
542
509
    goto failure;
550
517
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
551
518
      if(sret == -1){
552
519
        if(errno != EINTR){
553
 
          error_plus(0, errno, "write");
 
520
          error(0, errno, "write");
554
521
          status = EX_OSERR;
555
522
        }
556
523
        goto failure;
585
552
  
586
553
  /* Close FIFO */
587
554
  if(fifo_fd != -1){
588
 
    ret = close(fifo_fd);
 
555
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
589
556
    if(ret == -1 and errno != EINTR){
590
 
      error_plus(0, errno, "close");
 
557
      error(0, errno, "close");
591
558
    }
592
559
    fifo_fd = -1;
593
560
  }
594
561
  
595
562
  /* Close output FIFO */
596
563
  if(outfifo_fd != -1){
597
 
    ret = close(outfifo_fd);
 
564
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
598
565
    if(ret == -1){
599
 
      error_plus(0, errno, "close");
 
566
      error(0, errno, "close");
600
567
    }
601
568
  }
602
569
  
604
571
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
605
572
                               * sizeof(char *)); /* Count args */
606
573
  if(cmdline_argv == NULL){
607
 
    error_plus(0, errno, "malloc");
 
574
    error(0, errno, "malloc");
608
575
    return status;
609
576
  }
610
577
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
625
592
       the real user ID (_mandos) */
626
593
    ret = setuid(geteuid());
627
594
    if(ret == -1){
628
 
      error_plus(0, errno, "setuid");
 
595
      error(0, errno, "setuid");
629
596
    }
630
597
    
631
598
    setsid();
632
599
    ret = chdir("/");
633
600
    if(ret == -1){
634
 
      error_plus(0, errno, "chdir");
 
601
      error(0, errno, "chdir");
635
602
      _exit(EX_OSERR);
636
603
    }
637
604
/*     if(fork() != 0){ */
639
606
/*     } */
640
607
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
641
608
    if(ret == -1){
642
 
      error_plus(0, errno, "dup2");
 
609
      error(0, errno, "dup2");
643
610
      _exit(EX_OSERR);
644
611
    }
645
612
    
646
613
    execv(usplash_name, cmdline_argv);
647
614
    if(not interrupted_by_signal){
648
 
      error_plus(0, errno, "execv");
 
615
      error(0, errno, "execv");
649
616
    }
650
617
    free(cmdline);
651
618
    free(cmdline_argv);
656
623
  sleep(2);
657
624
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
658
625
    if(errno != EINTR){
659
 
      error_plus(0, errno, "usplash_write");
 
626
      error(0, errno, "usplash_write");
660
627
    }
661
628
  }
662
629
  
663
630
  /* Close FIFO (again) */
664
631
  if(fifo_fd != -1){
665
 
    ret = close(fifo_fd);
 
632
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
666
633
    if(ret == -1 and errno != EINTR){
667
 
      error_plus(0, errno, "close");
 
634
      error(0, errno, "close");
668
635
    }
669
636
    fifo_fd = -1;
670
637
  }
675
642
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
676
643
                                            &signal_action, NULL));
677
644
    if(ret == -1){
678
 
      error_plus(0, errno, "sigaction");
 
645
      error(0, errno, "sigaction");
679
646
    }
680
647
    do {
681
648
      ret = raise(signal_received);
682
649
    } while(ret != 0 and errno == EINTR);
683
650
    if(ret != 0){
684
 
      error_plus(0, errno, "raise");
 
651
      error(0, errno, "raise");
685
652
      abort();
686
653
    }
687
654
    TEMP_FAILURE_RETRY(pause());