/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: 2019-02-10 04:20:26 UTC
  • Revision ID: teddy@recompile.se-20190210042026-3ntr3anw5scz07s0
Update copyright year to 2019

* DBUS-API: Update copyright year to 2019.
* debian/copyright: - '' -
* 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.xml: - '' -
* plugin-runner.xml: - '' -
* plugins.d/askpass-fifo.xml: - '' -
* plugins.d/mandos-client.c: - '' -
* plugins.d/mandos-client.xml: - '' -
* plugins.d/password-prompt.xml: - '' -
* plugins.d/plymouth.xml: - '' -
* plugins.d/splashy.xml: - '' -
* 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
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
 
5
 * Copyright © 2008-2018 Teddy Hogeborn
 
6
 * Copyright © 2008-2018 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
14
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
18
 * General Public License for more details.
17
19
 * 
18
20
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see
20
 
 * <http://www.gnu.org/licenses/>.
 
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
21
22
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
23
 * Contact the authors at <mandos@recompile.se>.
23
24
 */
24
25
 
25
26
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
35
36
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
36
37
                                   dirent */
37
38
#include <stddef.h>             /* NULL */
38
 
#include <string.h>             /* strlen(), memcmp() */
39
 
#include <stdio.h>              /* asprintf()*/
 
39
#include <string.h>             /* strlen(), memcmp(), strerror() */
 
40
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(),
 
41
                                   fprintf() */
40
42
#include <unistd.h>             /* close(), write(), readlink(),
41
43
                                   read(), STDOUT_FILENO, sleep(),
42
44
                                   fork(), setuid(), geteuid(),
50
52
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
51
53
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
52
54
#include <argz.h>               /* argz_count(), argz_extract() */
 
55
#include <stdarg.h>             /* va_list, va_start(), ... */
53
56
 
54
57
sig_atomic_t interrupted_by_signal = 0;
55
58
int signal_received;
56
59
const char usplash_name[] = "/sbin/usplash";
57
60
 
 
61
/* Function to use when printing errors */
 
62
__attribute__((format (gnu_printf, 3, 4)))
 
63
void error_plus(int status, int errnum, const char *formatstring,
 
64
                ...){
 
65
  va_list ap;
 
66
  char *text;
 
67
  int ret;
 
68
  
 
69
  va_start(ap, formatstring);
 
70
  ret = vasprintf(&text, formatstring, ap);
 
71
  if(ret == -1){
 
72
    fprintf(stderr, "Mandos plugin %s: ",
 
73
            program_invocation_short_name);
 
74
    vfprintf(stderr, formatstring, ap);
 
75
    fprintf(stderr, ": ");
 
76
    fprintf(stderr, "%s\n", strerror(errnum));
 
77
    error(status, errno, "vasprintf while printing error");
 
78
    return;
 
79
  }
 
80
  fprintf(stderr, "Mandos plugin ");
 
81
  error(status, errnum, "%s", text);
 
82
  free(text);
 
83
}
 
84
 
58
85
static void termination_handler(int signum){
59
86
  if(interrupted_by_signal){
60
87
    return;
91
118
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
92
119
      if(ret == -1){
93
120
        int e = errno;
94
 
        TEMP_FAILURE_RETRY(close(*fifo_fd_r));
 
121
        close(*fifo_fd_r);
95
122
        errno = e;
96
123
        return false;
97
124
      }
107
134
                 cmd_line_len - written);
108
135
    if(sret == -1){
109
136
      int e = errno;
110
 
      TEMP_FAILURE_RETRY(close(*fifo_fd_r));
 
137
      close(*fifo_fd_r);
111
138
      free(cmd_line_alloc);
112
139
      errno = e;
113
140
      return false;
155
182
  size_t cmdline_len = 0;
156
183
  DIR *proc_dir = opendir("/proc");
157
184
  if(proc_dir == NULL){
158
 
    error(0, errno, "opendir");
 
185
    error_plus(0, errno, "opendir");
159
186
    return -1;
160
187
  }
161
188
  errno = 0;
183
210
      char *exe_link;
184
211
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
212
      if(ret == -1){
186
 
        error(0, errno, "asprintf");
 
213
        error_plus(0, errno, "asprintf");
187
214
        goto fail_find_usplash;
188
215
      }
189
216
      
195
222
          free(exe_link);
196
223
          continue;
197
224
        }
198
 
        error(0, errno, "lstat");
 
225
        error_plus(0, errno, "lstat");
199
226
        free(exe_link);
200
227
        goto fail_find_usplash;
201
228
      }
226
253
        ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
227
254
                       proc_ent->d_name);
228
255
        if(ret == -1){
229
 
          error(0, errno, "asprintf");
 
256
          error_plus(0, errno, "asprintf");
230
257
          goto fail_find_usplash;
231
258
        }
232
259
        cl_fd = open(cmdline_filename, O_RDONLY);
233
260
        free(cmdline_filename);
234
261
        if(cl_fd == -1){
235
 
          error(0, errno, "open");
 
262
          error_plus(0, errno, "open");
236
263
          goto fail_find_usplash;
237
264
        }
238
265
      }
244
271
        if(cmdline_len + blocksize > cmdline_allocated){
245
272
          tmp = realloc(cmdline, cmdline_allocated + blocksize);
246
273
          if(tmp == NULL){
247
 
            error(0, errno, "realloc");
 
274
            error_plus(0, errno, "realloc");
248
275
            close(cl_fd);
249
276
            goto fail_find_usplash;
250
277
          }
255
282
        sret = read(cl_fd, cmdline + cmdline_len,
256
283
                    cmdline_allocated - cmdline_len);
257
284
        if(sret == -1){
258
 
          error(0, errno, "read");
 
285
          error_plus(0, errno, "read");
259
286
          close(cl_fd);
260
287
          goto fail_find_usplash;
261
288
        }
263
290
      } while(sret != 0);
264
291
      ret = close(cl_fd);
265
292
      if(ret == -1){
266
 
        error(0, errno, "close");
 
293
        error_plus(0, errno, "close");
267
294
        goto fail_find_usplash;
268
295
      }
269
296
    }
270
297
    /* Close directory */
271
298
    ret = closedir(proc_dir);
272
299
    if(ret == -1){
273
 
      error(0, errno, "closedir");
 
300
      error_plus(0, errno, "closedir");
274
301
      goto fail_find_usplash;
275
302
    }
276
303
    /* Success */
325
352
    sigemptyset(&new_action.sa_mask);
326
353
    ret = sigaddset(&new_action.sa_mask, SIGINT);
327
354
    if(ret == -1){
328
 
      error(0, errno, "sigaddset");
 
355
      error_plus(0, errno, "sigaddset");
329
356
      status = EX_OSERR;
330
357
      goto failure;
331
358
    }
332
359
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
333
360
    if(ret == -1){
334
 
      error(0, errno, "sigaddset");
 
361
      error_plus(0, errno, "sigaddset");
335
362
      status = EX_OSERR;
336
363
      goto failure;
337
364
    }
338
365
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
366
    if(ret == -1){
340
 
      error(0, errno, "sigaddset");
 
367
      error_plus(0, errno, "sigaddset");
341
368
      status = EX_OSERR;
342
369
      goto failure;
343
370
    }
344
371
    ret = sigaction(SIGINT, NULL, &old_action);
345
372
    if(ret == -1){
346
373
      if(errno != EINTR){
347
 
        error(0, errno, "sigaction");
 
374
        error_plus(0, errno, "sigaction");
348
375
        status = EX_OSERR;
349
376
      }
350
377
      goto failure;
353
380
      ret = sigaction(SIGINT, &new_action, NULL);
354
381
      if(ret == -1){
355
382
        if(errno != EINTR){
356
 
          error(0, errno, "sigaction");
 
383
          error_plus(0, errno, "sigaction");
357
384
          status = EX_OSERR;
358
385
        }
359
386
        goto failure;
362
389
    ret = sigaction(SIGHUP, NULL, &old_action);
363
390
    if(ret == -1){
364
391
      if(errno != EINTR){
365
 
        error(0, errno, "sigaction");
 
392
        error_plus(0, errno, "sigaction");
366
393
        status = EX_OSERR;
367
394
      }
368
395
      goto failure;
371
398
      ret = sigaction(SIGHUP, &new_action, NULL);
372
399
      if(ret == -1){
373
400
        if(errno != EINTR){
374
 
          error(0, errno, "sigaction");
 
401
          error_plus(0, errno, "sigaction");
375
402
          status = EX_OSERR;
376
403
        }
377
404
        goto failure;
380
407
    ret = sigaction(SIGTERM, NULL, &old_action);
381
408
    if(ret == -1){
382
409
      if(errno != EINTR){
383
 
        error(0, errno, "sigaction");
 
410
        error_plus(0, errno, "sigaction");
384
411
        status = EX_OSERR;
385
412
      }
386
413
      goto failure;
389
416
      ret = sigaction(SIGTERM, &new_action, NULL);
390
417
      if(ret == -1){
391
418
        if(errno != EINTR){
392
 
          error(0, errno, "sigaction");
 
419
          error_plus(0, errno, "sigaction");
393
420
          status = EX_OSERR;
394
421
        }
395
422
        goto failure;
401
428
  /* Write command to FIFO */
402
429
  if(not usplash_write(&fifo_fd, "TIMEOUT", "0")){
403
430
    if(errno != EINTR){
404
 
      error(0, errno, "usplash_write");
 
431
      error_plus(0, errno, "usplash_write");
405
432
      status = EX_OSERR;
406
433
    }
407
434
    goto failure;
413
440
  
414
441
  if(not usplash_write(&fifo_fd, "INPUTQUIET", prompt)){
415
442
    if(errno != EINTR){
416
 
      error(0, errno, "usplash_write");
 
443
      error_plus(0, errno, "usplash_write");
417
444
      status = EX_OSERR;
418
445
    }
419
446
    goto failure;
431
458
  outfifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
432
459
  if(outfifo_fd == -1){
433
460
    if(errno != EINTR){
434
 
      error(0, errno, "open");
 
461
      error_plus(0, errno, "open");
435
462
      status = EX_OSERR;
436
463
    }
437
464
    goto failure;
450
477
      char *tmp = realloc(buf, buf_allocated + blocksize);
451
478
      if(tmp == NULL){
452
479
        if(errno != EINTR){
453
 
          error(0, errno, "realloc");
 
480
          error_plus(0, errno, "realloc");
454
481
          status = EX_OSERR;
455
482
        }
456
483
        goto failure;
462
489
                buf_allocated - buf_len);
463
490
    if(sret == -1){
464
491
      if(errno != EINTR){
465
 
        error(0, errno, "read");
 
492
        error_plus(0, errno, "read");
466
493
        status = EX_OSERR;
467
494
      }
468
 
      TEMP_FAILURE_RETRY(close(outfifo_fd));
 
495
      close(outfifo_fd);
469
496
      goto failure;
470
497
    }
471
498
    if(interrupted_by_signal){
477
504
  ret = close(outfifo_fd);
478
505
  if(ret == -1){
479
506
    if(errno != EINTR){
480
 
      error(0, errno, "close");
 
507
      error_plus(0, errno, "close");
481
508
      status = EX_OSERR;
482
509
    }
483
510
    goto failure;
490
517
  
491
518
  if(not usplash_write(&fifo_fd, "TIMEOUT", "15")){
492
519
    if(errno != EINTR){
493
 
      error(0, errno, "usplash_write");
 
520
      error_plus(0, errno, "usplash_write");
494
521
      status = EX_OSERR;
495
522
    }
496
523
    goto failure;
503
530
  ret = close(fifo_fd);
504
531
  if(ret == -1){
505
532
    if(errno != EINTR){
506
 
      error(0, errno, "close");
 
533
      error_plus(0, errno, "close");
507
534
      status = EX_OSERR;
508
535
    }
509
536
    goto failure;
517
544
      sret = write(STDOUT_FILENO, buf + written, buf_len - written);
518
545
      if(sret == -1){
519
546
        if(errno != EINTR){
520
 
          error(0, errno, "write");
 
547
          error_plus(0, errno, "write");
521
548
          status = EX_OSERR;
522
549
        }
523
550
        goto failure;
552
579
  
553
580
  /* Close FIFO */
554
581
  if(fifo_fd != -1){
555
 
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
582
    ret = close(fifo_fd);
556
583
    if(ret == -1 and errno != EINTR){
557
 
      error(0, errno, "close");
 
584
      error_plus(0, errno, "close");
558
585
    }
559
586
    fifo_fd = -1;
560
587
  }
561
588
  
562
589
  /* Close output FIFO */
563
590
  if(outfifo_fd != -1){
564
 
    ret = (int)TEMP_FAILURE_RETRY(close(outfifo_fd));
 
591
    ret = close(outfifo_fd);
565
592
    if(ret == -1){
566
 
      error(0, errno, "close");
 
593
      error_plus(0, errno, "close");
567
594
    }
568
595
  }
569
596
  
571
598
  char **cmdline_argv = malloc((argz_count(cmdline, cmdline_len) + 1)
572
599
                               * sizeof(char *)); /* Count args */
573
600
  if(cmdline_argv == NULL){
574
 
    error(0, errno, "malloc");
 
601
    error_plus(0, errno, "malloc");
575
602
    return status;
576
603
  }
577
604
  argz_extract(cmdline, cmdline_len, cmdline_argv); /* Create argv */
592
619
       the real user ID (_mandos) */
593
620
    ret = setuid(geteuid());
594
621
    if(ret == -1){
595
 
      error(0, errno, "setuid");
 
622
      error_plus(0, errno, "setuid");
596
623
    }
597
624
    
598
625
    setsid();
599
626
    ret = chdir("/");
600
627
    if(ret == -1){
601
 
      error(0, errno, "chdir");
 
628
      error_plus(0, errno, "chdir");
602
629
      _exit(EX_OSERR);
603
630
    }
604
631
/*     if(fork() != 0){ */
606
633
/*     } */
607
634
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
608
635
    if(ret == -1){
609
 
      error(0, errno, "dup2");
 
636
      error_plus(0, errno, "dup2");
610
637
      _exit(EX_OSERR);
611
638
    }
612
639
    
613
640
    execv(usplash_name, cmdline_argv);
614
641
    if(not interrupted_by_signal){
615
 
      error(0, errno, "execv");
 
642
      error_plus(0, errno, "execv");
616
643
    }
617
644
    free(cmdline);
618
645
    free(cmdline_argv);
623
650
  sleep(2);
624
651
  if(not usplash_write(&fifo_fd, "PULSATE", NULL)){
625
652
    if(errno != EINTR){
626
 
      error(0, errno, "usplash_write");
 
653
      error_plus(0, errno, "usplash_write");
627
654
    }
628
655
  }
629
656
  
630
657
  /* Close FIFO (again) */
631
658
  if(fifo_fd != -1){
632
 
    ret = (int)TEMP_FAILURE_RETRY(close(fifo_fd));
 
659
    ret = close(fifo_fd);
633
660
    if(ret == -1 and errno != EINTR){
634
 
      error(0, errno, "close");
 
661
      error_plus(0, errno, "close");
635
662
    }
636
663
    fifo_fd = -1;
637
664
  }
642
669
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
643
670
                                            &signal_action, NULL));
644
671
    if(ret == -1){
645
 
      error(0, errno, "sigaction");
 
672
      error_plus(0, errno, "sigaction");
646
673
    }
647
674
    do {
648
675
      ret = raise(signal_received);
649
676
    } while(ret != 0 and errno == EINTR);
650
677
    if(ret != 0){
651
 
      error(0, errno, "raise");
 
678
      error_plus(0, errno, "raise");
652
679
      abort();
653
680
    }
654
681
    TEMP_FAILURE_RETRY(pause());