/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: Björn Påhlsson
  • Date: 2010-09-01 18:03:03 UTC
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100901180303-u47edb73fczu56ob
bug fixes that prevent problems when runing server as root

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