/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-09-12 03:00:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100912030040-b0uopyennste9fdh
Documentation changes:

* DBUS-API: New file documenting the server D-Bus interface.

* clients.conf: Add examples of new approval settings.

* debian/mandos.docs: Added "DBUS-API".

* mandos-clients.conf.xml (OPTIONS): Added "approved_by_default",
                                     "approval_delay", and
                                     "approval_duration".
* mandos.xml (D-BUS INTERFACE): Refer to the "DBUS-API" file.
  (BUGS): Remove mention of lack of a remote query interface.

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