/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

Change "fukt.bsnet.se" to "recompile.se" throughout.

* README: - '' -
* debian/control: - '' -
* debian/copyright: - '' -
* debian/mandos-client.README.Debian: - '' - and some rewriting.
* debian/mandos.README.Debian: - '' -
* debian/watch: Change "fukt.bsnet.se" to "recompile.se".
* init.d-mandos: - '' -
* intro.xml: - '' -
* mandos: - '' -
* mandos-clients.conf.xml: - '' -
* mandos-ctl: - '' -
* mandos-ctl.xml: - '' -
* mandos-keygen: - '' -
* mandos-keygen.xml: - '' -
* mandos-monitor: - '' -
* mandos-monitor.xml: - '' -
* mandos.conf.xml: - '' -
* mandos.lsm: - '' -
* mandos.xml: - '' -
* plugin-runner.c: - '' -
* plugin-runner.xml: - '' -
* plugins.d/askpass-fifo.c: - '' -
* plugins.d/askpass-fifo.xml: - '' -
* plugins.d/mandos-client.c: - '' -
* plugins.d/mandos-client.xml: - '' -
* plugins.d/password-prompt.c: - '' -
* plugins.d/password-prompt.xml: - '' -
* plugins.d/plymouth.c: - '' -
* plugins.d/plymouth.xml: - '' -
* plugins.d/splashy.c: - '' -
* plugins.d/splashy.xml: - '' -
* plugins.d/usplash.c: - '' -
* plugins.d/usplash.xml: - '' -

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