/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-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 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
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
 
void error_plus(int status, int errnum, const char *formatstring,
62
 
                ...){
63
 
  va_list ap;
64
 
  char *text;
65
 
  int ret;
66
 
  
67
 
  va_start(ap, formatstring);
68
 
  ret = vasprintf(&text, formatstring, ap);
69
 
  if (ret == -1){
70
 
    fprintf(stderr, "Mandos plugin %s: ",
71
 
            program_invocation_short_name);
72
 
    vfprintf(stderr, formatstring, ap);
73
 
    fprintf(stderr, ": ");
74
 
    fprintf(stderr, "%s\n", strerror(errnum));
75
 
    error(status, errno, "vasprintf while printing error");
76
 
    return;
77
 
  }
78
 
  fprintf(stderr, "Mandos plugin ");
79
 
  error(status, errnum, "%s", text);
80
 
  free(text);
81
 
}
82
 
 
83
57
static void termination_handler(int signum){
84
58
  if(interrupted_by_signal){
85
59
    return;
180
154
  size_t cmdline_len = 0;
181
155
  DIR *proc_dir = opendir("/proc");
182
156
  if(proc_dir == NULL){
183
 
    error_plus(0, errno, "opendir");
 
157
    error(0, errno, "opendir");
184
158
    return -1;
185
159
  }
186
160
  errno = 0;
208
182
      char *exe_link;
209
183
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
210
184
      if(ret == -1){
211
 
        error_plus(0, errno, "asprintf");
 
185
        error(0, errno, "asprintf");
212
186
        goto fail_find_usplash;
213
187
      }
214
188
      
220
194
          free(exe_link);
221
195
          continue;
222
196
        }
223
 
        error_plus(0, errno, "lstat");
 
197
        error(0, errno, "lstat");
224
198
        free(exe_link);
225
199
        goto fail_find_usplash;
226
200
      }
251
225
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
252
226
                       proc_ent->d_name);
253
227
        if(ret == -1){
254
 
          error_plus(0, errno, "asprintf");
 
228
          error(0, errno, "asprintf");
255
229
          goto fail_find_usplash;
256
230
        }
257
231
        cl_fd = open(cmdline_filename, O_RDONLY);
258
232
        free(cmdline_filename);
259
233
        if(cl_fd == -1){
260
 
          error_plus(0, errno, "open");
 
234
          error(0, errno, "open");
261
235
          goto fail_find_usplash;
262
236
        }
263
237
      }
269
243
        if(cmdline_len + blocksize > cmdline_allocated){
270
244
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
271
245
          if(tmp == NULL){
272
 
            error_plus(0, errno, "realloc");
 
246
            error(0, errno, "realloc");
273
247
            close(cl_fd);
274
248
            goto fail_find_usplash;
275
249
          }
280
254
        sret = read(cl_fd, cmdline + cmdline_len,
281
255
                    cmdline_allocated - cmdline_len);
282
256
        if(sret == -1){
283
 
          error_plus(0, errno, "read");
 
257
          error(0, errno, "read");
284
258
          close(cl_fd);
285
259
          goto fail_find_usplash;
286
260
        }
288
262
      } while(sret != 0);
289
263
      ret = close(cl_fd);
290
264
      if(ret == -1){
291
 
        error_plus(0, errno, "close");
 
265
        error(0, errno, "close");
292
266
        goto fail_find_usplash;
293
267
      }
294
268
    }
295
269
    /* Close directory */
296
270
    ret = closedir(proc_dir);
297
271
    if(ret == -1){
298
 
      error_plus(0, errno, "closedir");
 
272
      error(0, errno, "closedir");
299
273
      goto fail_find_usplash;
300
274
    }
301
275
    /* Success */
350
324
    sigemptyset(&new_action.sa_mask);
351
325
    ret = sigaddset(&new_action.sa_mask, SIGINT);
352
326
    if(ret == -1){
353
 
      error_plus(0, errno, "sigaddset");
 
327
      error(0, errno, "sigaddset");
354
328
      status = EX_OSERR;
355
329
      goto failure;
356
330
    }
357
331
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
358
332
    if(ret == -1){
359
 
      error_plus(0, errno, "sigaddset");
 
333
      error(0, errno, "sigaddset");
360
334
      status = EX_OSERR;
361
335
      goto failure;
362
336
    }
363
337
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
364
338
    if(ret == -1){
365
 
      error_plus(0, errno, "sigaddset");
 
339
      error(0, errno, "sigaddset");
366
340
      status = EX_OSERR;
367
341
      goto failure;
368
342
    }
369
343
    ret = sigaction(SIGINT, NULL, &old_action);
370
344
    if(ret == -1){
371
345
      if(errno != EINTR){
372
 
        error_plus(0, errno, "sigaction");
 
346
        error(0, errno, "sigaction");
373
347
        status = EX_OSERR;
374
348
      }
375
349
      goto failure;
378
352
      ret = sigaction(SIGINT, &new_action, NULL);
379
353
      if(ret == -1){
380
354
        if(errno != EINTR){
381
 
          error_plus(0, errno, "sigaction");
 
355
          error(0, errno, "sigaction");
382
356
          status = EX_OSERR;
383
357
        }
384
358
        goto failure;
387
361
    ret = sigaction(SIGHUP, NULL, &old_action);
388
362
    if(ret == -1){
389
363
      if(errno != EINTR){
390
 
        error_plus(0, errno, "sigaction");
 
364
        error(0, errno, "sigaction");
391
365
        status = EX_OSERR;
392
366
      }
393
367
      goto failure;
396
370
      ret = sigaction(SIGHUP, &new_action, NULL);
397
371
      if(ret == -1){
398
372
        if(errno != EINTR){
399
 
          error_plus(0, errno, "sigaction");
 
373
          error(0, errno, "sigaction");
400
374
          status = EX_OSERR;
401
375
        }
402
376
        goto failure;
405
379
    ret = sigaction(SIGTERM, NULL, &old_action);
406
380
    if(ret == -1){
407
381
      if(errno != EINTR){
408
 
        error_plus(0, errno, "sigaction");
 
382
        error(0, errno, "sigaction");
409
383
        status = EX_OSERR;
410
384
      }
411
385
      goto failure;
414
388
      ret = sigaction(SIGTERM, &new_action, NULL);
415
389
      if(ret == -1){
416
390
        if(errno != EINTR){
417
 
          error_plus(0, errno, "sigaction");
 
391
          error(0, errno, "sigaction");
418
392
          status = EX_OSERR;
419
393
        }
420
394
        goto failure;
426
400
  /* Write command to FIFO */
427
401
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
428
402
    if(errno != EINTR){
429
 
      error_plus(0, errno, "usplash_write");
 
403
      error(0, errno, "usplash_write");
430
404
      status = EX_OSERR;
431
405
    }
432
406
    goto failure;
438
412
  
439
413
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
440
414
    if(errno != EINTR){
441
 
      error_plus(0, errno, "usplash_write");
 
415
      error(0, errno, "usplash_write");
442
416
      status = EX_OSERR;
443
417
    }
444
418
    goto failure;
456
430
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
457
431
  if(outfifo_fd == -1){
458
432
    if(errno != EINTR){
459
 
      error_plus(0, errno, "open");
 
433
      error(0, errno, "open");
460
434
      status = EX_OSERR;
461
435
    }
462
436
    goto failure;
475
449
      char *tmp = realloc(buf, buf_allocated + blocksize);
476
450
      if(tmp == NULL){
477
451
        if(errno != EINTR){
478
 
          error_plus(0, errno, "realloc");
 
452
          error(0, errno, "realloc");
479
453
          status = EX_OSERR;
480
454
        }
481
455
        goto failure;
487
461
                buf_allocated - buf_len);
488
462
    if(sret == -1){
489
463
      if(errno != EINTR){
490
 
        error_plus(0, errno, "read");
 
464
        error(0, errno, "read");
491
465
        status = EX_OSERR;
492
466
      }
493
467
      TEMP_FAILURE_RETRY(close(outfifo_fd));
502
476
  ret = close(outfifo_fd);
503
477
  if(ret == -1){
504
478
    if(errno != EINTR){
505
 
      error_plus(0, errno, "close");
 
479
      error(0, errno, "close");
506
480
      status = EX_OSERR;
507
481
    }
508
482
    goto failure;
515
489
  
516
490
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
517
491
    if(errno != EINTR){
518
 
      error_plus(0, errno, "usplash_write");
 
492
      error(0, errno, "usplash_write");
519
493
      status = EX_OSERR;
520
494
    }
521
495
    goto failure;
528
502
  ret = close(fifo_fd);
529
503
  if(ret == -1){
530
504
    if(errno != EINTR){
531
 
      error_plus(0, errno, "close");
 
505
      error(0, errno, "close");
532
506
      status = EX_OSERR;
533
507
    }
534
508
    goto failure;
542
516
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
543
517
      if(sret == -1){
544
518
        if(errno != EINTR){
545
 
          error_plus(0, errno, "write");
 
519
          error(0, errno, "write");
546
520
          status = EX_OSERR;
547
521
        }
548
522
        goto failure;
579
553
  if(fifo_fd != -1){
580
554
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
581
555
    if(ret == -1 and errno != EINTR){
582
 
      error_plus(0, errno, "close");
 
556
      error(0, errno, "close");
583
557
    }
584
558
    fifo_fd = -1;
585
559
  }
588
562
  if(outfifo_fd != -1){
589
563
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
590
564
    if(ret == -1){
591
 
      error_plus(0, errno, "close");
592
 
    }
593
 
  }
594
 
  
595
 
  /* Create argv for new usplash*/
596
 
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
597
 
                               * sizeof(char *)); /* Count args */
598
 
  if(cmdline_argv == NULL){
599
 
    error_plus(0, errno, "malloc");
600
 
    return status;
601
 
  }
602
 
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
603
 
  
 
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
  }
604
590
  /* Kill old usplash */
605
591
  kill(usplash_pid, SIGTERM);
606
592
  sleep(2);
617
603
       the real user ID (_mandos) */
618
604
    ret = setuid(geteuid());
619
605
    if(ret == -1){
620
 
      error_plus(0, errno, "setuid");
 
606
      error(0, errno, "setuid");
621
607
    }
622
608
    
623
609
    setsid();
624
610
    ret = chdir("/");
625
611
    if(ret == -1){
626
 
      error_plus(0, errno, "chdir");
 
612
      error(0, errno, "chdir");
627
613
      _exit(EX_OSERR);
628
614
    }
629
615
/*     if(fork() != 0){ */
631
617
/*     } */
632
618
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
633
619
    if(ret == -1){
634
 
      error_plus(0, errno, "dup2");
 
620
      error(0, errno, "dup2");
635
621
      _exit(EX_OSERR);
636
622
    }
637
623
    
638
624
    execv(usplash_name, cmdline_argv);
639
625
    if(not interrupted_by_signal){
640
 
      error_plus(0, errno, "execv");
 
626
      error(0, errno, "execv");
641
627
    }
642
628
    free(cmdline);
643
629
    free(cmdline_argv);
648
634
  sleep(2);
649
635
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
650
636
    if(errno != EINTR){
651
 
      error_plus(0, errno, "usplash_write");
 
637
      error(0, errno, "usplash_write");
652
638
    }
653
639
  }
654
640
  
656
642
  if(fifo_fd != -1){
657
643
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
658
644
    if(ret == -1 and errno != EINTR){
659
 
      error_plus(0, errno, "close");
 
645
      error(0, errno, "close");
660
646
    }
661
647
    fifo_fd = -1;
662
648
  }
667
653
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
668
654
                                            &signal_action, NULL));
669
655
    if(ret == -1){
670
 
      error_plus(0, errno, "sigaction");
 
656
      error(0, errno, "sigaction");
671
657
    }
672
658
    do {
673
659
      ret = raise(signal_received);
674
660
    } while(ret != 0 and errno == EINTR);
675
661
    if(ret != 0){
676
 
      error_plus(0, errno, "raise");
 
662
      error(0, errno, "raise");
677
663
      abort();
678
664
    }
679
665
    TEMP_FAILURE_RETRY(pause());