/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
new approve/deny functionallity in mandos-monitor

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,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 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 <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
 
35
                                   dirent */
 
36
#include <stddef.h>             /* NULL */
 
37
#include <string.h>             /* strlen(), memcmp() */
 
38
#include <stdio.h>              /* asprintf(), perror() */
 
39
#include <unistd.h>             /* close(), write(), readlink(),
 
40
                                   read(), STDOUT_FILENO, sleep(),
 
41
                                   fork(), setuid(), geteuid(),
 
42
                                   setsid(), chdir(), dup2(),
 
43
                                   STDERR_FILENO, execv() */
 
44
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
 
45
                                   EXIT_SUCCESS, malloc(), _exit(),
 
46
                                   getenv() */
 
47
#include <dirent.h>             /* opendir(), readdir(), closedir() */
53
48
#include <inttypes.h>           /* intmax_t, strtoimax() */
54
 
#include <iso646.h>             /* or, not, and */
55
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK() */
 
49
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
56
50
#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
 
#include <argz.h>               /* argz_count(), argz_extract() */
62
51
 
63
52
sig_atomic_t interrupted_by_signal = 0;
64
53
int signal_received;
65
54
const char usplash_name[] = "/sbin/usplash";
66
55
 
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
56
static void termination_handler(int signum){
92
57
  if(interrupted_by_signal){
93
58
    return;
124
89
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
125
90
      if(ret == -1){
126
91
        int e = errno;
127
 
        close(*fifo_fd_r);
 
92
        TEMP_FAILURE_RETRY(close(*fifo_fd_r));
128
93
        errno = e;
129
94
        return false;
130
95
      }
140
105
                 cmd_line_len - written);
141
106
    if(sret == -1){
142
107
      int e = errno;
143
 
      close(*fifo_fd_r);
 
108
      TEMP_FAILURE_RETRY(close(*fifo_fd_r));
144
109
      free(cmd_line_alloc);
145
110
      errno = e;
146
111
      return false;
188
153
  size_t cmdline_len = 0;
189
154
  DIR *proc_dir = opendir("/proc");
190
155
  if(proc_dir == NULL){
191
 
    error_plus(0, errno, "opendir");
 
156
    perror("opendir");
192
157
    return -1;
193
158
  }
194
159
  errno = 0;
216
181
      char *exe_link;
217
182
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
218
183
      if(ret == -1){
219
 
        error_plus(0, errno, "asprintf");
 
184
        perror("asprintf");
220
185
        goto fail_find_usplash;
221
186
      }
222
187
      
228
193
          free(exe_link);
229
194
          continue;
230
195
        }
231
 
        error_plus(0, errno, "lstat");
 
196
        perror("lstat");
232
197
        free(exe_link);
233
198
        goto fail_find_usplash;
234
199
      }
259
224
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
260
225
                       proc_ent->d_name);
261
226
        if(ret == -1){
262
 
          error_plus(0, errno, "asprintf");
 
227
          perror("asprintf");
263
228
          goto fail_find_usplash;
264
229
        }
265
230
        cl_fd = open(cmdline_filename, O_RDONLY);
266
231
        free(cmdline_filename);
267
232
        if(cl_fd == -1){
268
 
          error_plus(0, errno, "open");
 
233
          perror("open");
269
234
          goto fail_find_usplash;
270
235
        }
271
236
      }
277
242
        if(cmdline_len + blocksize > cmdline_allocated){
278
243
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
279
244
          if(tmp == NULL){
280
 
            error_plus(0, errno, "realloc");
 
245
            perror("realloc");
281
246
            close(cl_fd);
282
247
            goto fail_find_usplash;
283
248
          }
288
253
        sret = read(cl_fd, cmdline + cmdline_len,
289
254
                    cmdline_allocated - cmdline_len);
290
255
        if(sret == -1){
291
 
          error_plus(0, errno, "read");
 
256
          perror("read");
292
257
          close(cl_fd);
293
258
          goto fail_find_usplash;
294
259
        }
296
261
      } while(sret != 0);
297
262
      ret = close(cl_fd);
298
263
      if(ret == -1){
299
 
        error_plus(0, errno, "close");
 
264
        perror("close");
300
265
        goto fail_find_usplash;
301
266
      }
302
267
    }
303
268
    /* Close directory */
304
269
    ret = closedir(proc_dir);
305
270
    if(ret == -1){
306
 
      error_plus(0, errno, "closedir");
 
271
      perror("closedir");
307
272
      goto fail_find_usplash;
308
273
    }
309
274
    /* Success */
358
323
    sigemptyset(&new_action.sa_mask);
359
324
    ret = sigaddset(&new_action.sa_mask, SIGINT);
360
325
    if(ret == -1){
361
 
      error_plus(0, errno, "sigaddset");
 
326
      perror("sigaddset");
362
327
      status = EX_OSERR;
363
328
      goto failure;
364
329
    }
365
330
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
366
331
    if(ret == -1){
367
 
      error_plus(0, errno, "sigaddset");
 
332
      perror("sigaddset");
368
333
      status = EX_OSERR;
369
334
      goto failure;
370
335
    }
371
336
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
372
337
    if(ret == -1){
373
 
      error_plus(0, errno, "sigaddset");
 
338
      perror("sigaddset");
374
339
      status = EX_OSERR;
375
340
      goto failure;
376
341
    }
377
342
    ret = sigaction(SIGINT, NULL, &old_action);
378
343
    if(ret == -1){
379
344
      if(errno != EINTR){
380
 
        error_plus(0, errno, "sigaction");
 
345
        perror("sigaction");
381
346
        status = EX_OSERR;
382
347
      }
383
348
      goto failure;
386
351
      ret = sigaction(SIGINT, &new_action, NULL);
387
352
      if(ret == -1){
388
353
        if(errno != EINTR){
389
 
          error_plus(0, errno, "sigaction");
 
354
          perror("sigaction");
390
355
          status = EX_OSERR;
391
356
        }
392
357
        goto failure;
395
360
    ret = sigaction(SIGHUP, NULL, &old_action);
396
361
    if(ret == -1){
397
362
      if(errno != EINTR){
398
 
        error_plus(0, errno, "sigaction");
 
363
        perror("sigaction");
399
364
        status = EX_OSERR;
400
365
      }
401
366
      goto failure;
404
369
      ret = sigaction(SIGHUP, &new_action, NULL);
405
370
      if(ret == -1){
406
371
        if(errno != EINTR){
407
 
          error_plus(0, errno, "sigaction");
 
372
          perror("sigaction");
408
373
          status = EX_OSERR;
409
374
        }
410
375
        goto failure;
413
378
    ret = sigaction(SIGTERM, NULL, &old_action);
414
379
    if(ret == -1){
415
380
      if(errno != EINTR){
416
 
        error_plus(0, errno, "sigaction");
 
381
        perror("sigaction");
417
382
        status = EX_OSERR;
418
383
      }
419
384
      goto failure;
422
387
      ret = sigaction(SIGTERM, &new_action, NULL);
423
388
      if(ret == -1){
424
389
        if(errno != EINTR){
425
 
          error_plus(0, errno, "sigaction");
 
390
          perror("sigaction");
426
391
          status = EX_OSERR;
427
392
        }
428
393
        goto failure;
434
399
  /* Write command to FIFO */
435
400
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
436
401
    if(errno != EINTR){
437
 
      error_plus(0, errno, "usplash_write");
 
402
      perror("usplash_write");
438
403
      status = EX_OSERR;
439
404
    }
440
405
    goto failure;
446
411
  
447
412
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
448
413
    if(errno != EINTR){
449
 
      error_plus(0, errno, "usplash_write");
 
414
      perror("usplash_write");
450
415
      status = EX_OSERR;
451
416
    }
452
417
    goto failure;
464
429
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
465
430
  if(outfifo_fd == -1){
466
431
    if(errno != EINTR){
467
 
      error_plus(0, errno, "open");
 
432
      perror("open");
468
433
      status = EX_OSERR;
469
434
    }
470
435
    goto failure;
483
448
      char *tmp = realloc(buf, buf_allocated + blocksize);
484
449
      if(tmp == NULL){
485
450
        if(errno != EINTR){
486
 
          error_plus(0, errno, "realloc");
 
451
          perror("realloc");
487
452
          status = EX_OSERR;
488
453
        }
489
454
        goto failure;
495
460
                buf_allocated - buf_len);
496
461
    if(sret == -1){
497
462
      if(errno != EINTR){
498
 
        error_plus(0, errno, "read");
 
463
        perror("read");
499
464
        status = EX_OSERR;
500
465
      }
501
 
      close(outfifo_fd);
 
466
      TEMP_FAILURE_RETRY(close(outfifo_fd));
502
467
      goto failure;
503
468
    }
504
469
    if(interrupted_by_signal){
510
475
  ret = close(outfifo_fd);
511
476
  if(ret == -1){
512
477
    if(errno != EINTR){
513
 
      error_plus(0, errno, "close");
 
478
      perror("close");
514
479
      status = EX_OSERR;
515
480
    }
516
481
    goto failure;
523
488
  
524
489
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
525
490
    if(errno != EINTR){
526
 
      error_plus(0, errno, "usplash_write");
 
491
      perror("usplash_write");
527
492
      status = EX_OSERR;
528
493
    }
529
494
    goto failure;
536
501
  ret = close(fifo_fd);
537
502
  if(ret == -1){
538
503
    if(errno != EINTR){
539
 
      error_plus(0, errno, "close");
 
504
      perror("close");
540
505
      status = EX_OSERR;
541
506
    }
542
507
    goto failure;
550
515
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
551
516
      if(sret == -1){
552
517
        if(errno != EINTR){
553
 
          error_plus(0, errno, "write");
 
518
          perror("write");
554
519
          status = EX_OSERR;
555
520
        }
556
521
        goto failure;
585
550
  
586
551
  /* Close FIFO */
587
552
  if(fifo_fd != -1){
588
 
    ret = close(fifo_fd);
 
553
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
589
554
    if(ret == -1 and errno != EINTR){
590
 
      error_plus(0, errno, "close");
 
555
      perror("close");
591
556
    }
592
557
    fifo_fd = -1;
593
558
  }
594
559
  
595
560
  /* Close output FIFO */
596
561
  if(outfifo_fd != -1){
597
 
    ret = close(outfifo_fd);
 
562
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
598
563
    if(ret == -1){
599
 
      error_plus(0, errno, "close");
600
 
    }
601
 
  }
602
 
  
603
 
  /* Create argv for new usplash*/
604
 
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
605
 
                               * sizeof(char *)); /* Count args */
606
 
  if(cmdline_argv == NULL){
607
 
    error_plus(0, errno, "malloc");
608
 
    return status;
609
 
  }
610
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
611
 
  
 
564
      perror("close");
 
565
    }
 
566
  }
 
567
  
 
568
  /* Create argc and argv for new usplash*/
 
569
  int cmdline_argc = 0;
 
570
  char **cmdline_argv = malloc(sizeof(char *));
 
571
  {
 
572
    size_t position = 0;
 
573
    while(position < cmdline_len){
 
574
      char **tmp = realloc(cmdline_argv,
 
575
                           (sizeof(char *)
 
576
                            * (size_t)(cmdline_argc + 2)));
 
577
      if(tmp == NULL){
 
578
        perror("realloc");
 
579
        free(cmdline_argv);
 
580
        return status;
 
581
      }
 
582
      cmdline_argv = tmp;
 
583
      cmdline_argv[cmdline_argc] = cmdline + position;
 
584
      cmdline_argc++;
 
585
      position += strlen(cmdline + position) + 1;
 
586
    }
 
587
    cmdline_argv[cmdline_argc] = NULL;
 
588
  }
612
589
  /* Kill old usplash */
613
590
  kill(usplash_pid, SIGTERM);
614
591
  sleep(2);
625
602
       the real user ID (_mandos) */
626
603
    ret = setuid(geteuid());
627
604
    if(ret == -1){
628
 
      error_plus(0, errno, "setuid");
 
605
      perror("setuid");
629
606
    }
630
607
    
631
608
    setsid();
632
609
    ret = chdir("/");
633
610
    if(ret == -1){
634
 
      error_plus(0, errno, "chdir");
 
611
      perror("chdir");
635
612
      _exit(EX_OSERR);
636
613
    }
637
614
/*     if(fork() != 0){ */
639
616
/*     } */
640
617
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
641
618
    if(ret == -1){
642
 
      error_plus(0, errno, "dup2");
 
619
      perror("dup2");
643
620
      _exit(EX_OSERR);
644
621
    }
645
622
    
646
623
    execv(usplash_name, cmdline_argv);
647
624
    if(not interrupted_by_signal){
648
 
      error_plus(0, errno, "execv");
 
625
      perror("execv");
649
626
    }
650
627
    free(cmdline);
651
628
    free(cmdline_argv);
656
633
  sleep(2);
657
634
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
658
635
    if(errno != EINTR){
659
 
      error_plus(0, errno, "usplash_write");
 
636
      perror("usplash_write");
660
637
    }
661
638
  }
662
639
  
663
640
  /* Close FIFO (again) */
664
641
  if(fifo_fd != -1){
665
 
    ret = close(fifo_fd);
 
642
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
666
643
    if(ret == -1 and errno != EINTR){
667
 
      error_plus(0, errno, "close");
 
644
      perror("close");
668
645
    }
669
646
    fifo_fd = -1;
670
647
  }
675
652
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
676
653
                                            &signal_action, NULL));
677
654
    if(ret == -1){
678
 
      error_plus(0, errno, "sigaction");
 
655
      perror("sigaction");
679
656
    }
680
657
    do {
681
658
      ret = raise(signal_received);
682
659
    } while(ret != 0 and errno == EINTR);
683
660
    if(ret != 0){
684
 
      error_plus(0, errno, "raise");
 
661
      perror("raise");
685
662
      abort();
686
663
    }
687
664
    TEMP_FAILURE_RETRY(pause());