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