/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: 2008-09-26 04:54:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080926045435-0thnnqops1kzclag
* debian/mandos-client.postinst: Change home directory to
                                 "/nonexistent".
* debian/mandos.postinst: - '' -

* plugin-runner.c (main): Bug fix: Block signals while modifying
                          "plugin_list".

* plugins.d/usplash.c (usplash_write): New function.
  (main): Use "usplash_write" to write "INPUTQUIET" command.  Also
          write "TIMEOUT 0" before it, and write "TIMEOUT 15" and
          "PULSATE" if starting a new usplash process.  Kill old
          usplash before forking.  Bug fix: do setuid(geteuid()) to
          preserve genuine rootness.  Better interrupted/error logic
          overall.

* debian/mandos-client.lintian-overrides: Ignore setuid
                                          "plugins.d/usplash".

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
2
 
/*
3
 
 * Usplash - Read a password from usplash and output it
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
14
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 * General Public License for more details.
17
 
 * 
18
 
 * 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
 
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
 
 */
24
 
 
25
1
#define _GNU_SOURCE             /* asprintf() */
26
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
27
 
                                   sigemptyset(), sigaddset(), SIGINT,
28
 
                                   SIGHUP, SIGTERM, sigaction(),
29
 
                                   SIG_IGN, kill(), SIGKILL */
30
 
#include <stdbool.h>            /* bool, false, true */
31
 
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
32
 
#include <iso646.h>             /* and, or, not*/
33
 
#include <errno.h>              /* errno, EINTR */
34
 
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
35
 
                                   dirent */
 
3
                                   sigemptyset(), sigaddset(),
 
4
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
 
5
                                   SIGTERM, kill(), SIGKILL */
36
6
#include <stddef.h>             /* NULL */
37
 
#include <string.h>             /* strlen(), memcmp() */
 
7
#include <stdlib.h>             /* getenv() */
38
8
#include <stdio.h>              /* asprintf(), perror() */
39
 
#include <unistd.h>             /* close(), write(), readlink(),
40
 
                                   read(), STDOUT_FILENO, sleep(),
41
 
                                   fork(), setuid(), geteuid(),
42
 
                                   setsid(), chdir(), dup2(),
43
 
                                   STDERR_FILENO, execv() */
44
 
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
45
 
                                   EXIT_SUCCESS, malloc(), _exit() */
46
 
#include <stdlib.h>             /* getenv() */
 
9
#include <stdlib.h>             /* EXIT_FAILURE, EXIT_SUCCESS,
 
10
                                   strtoul(), free() */
 
11
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
 
12
                                   ssize_t */
47
13
#include <dirent.h>             /* opendir(), readdir(), closedir() */
48
 
#include <inttypes.h>           /* intmax_t, strtoimax() */
49
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
14
#include <unistd.h>             /* readlink(), fork(), execl(),
 
15
                                   _exit */
 
16
#include <string.h>             /* memcmp() */
 
17
#include <iso646.h>             /* and */
 
18
#include <stdbool.h>            /* bool, false, true */
 
19
#include <errno.h>              /* errno */
 
20
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
21
                                   WEXITSTATUS() */
 
22
#include <fcntl.h>              /* open(), O_RDONLY */
50
23
 
51
24
sig_atomic_t interrupted_by_signal = 0;
52
25
 
56
29
 
57
30
static bool usplash_write(const char *cmd, const char *arg){
58
31
  /* 
59
 
   * usplash_write("TIMEOUT", "15") will write "TIMEOUT 15\0"
60
 
   * usplash_write("PULSATE", NULL) will write "PULSATE\0"
 
32
   * usplash_write("TIMEOUT", "15"); -> "TIMEOUT 15\0"
 
33
   * usplash_write("PULSATE", NULL); -> "PULSATE\0"
61
34
   * SEE ALSO
62
35
   *         usplash_write(8)
63
36
   */
64
37
  int ret;
65
38
  int fifo_fd;
66
 
  do {
 
39
  do{
67
40
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
68
41
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
69
42
      return false;
70
43
    }
71
 
  } while(fifo_fd == -1);
 
44
  }while(fifo_fd == -1);
72
45
  
73
46
  const char *cmd_line;
74
 
  size_t cmd_line_len;
75
47
  char *cmd_line_alloc = NULL;
76
48
  if(arg == NULL){
77
49
    cmd_line = cmd;
78
 
    cmd_line_len = strlen(cmd);
79
 
  } else {
80
 
    do {
 
50
    ret = (int)strlen(cmd);
 
51
  }else{
 
52
    do{
81
53
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
82
54
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
83
55
        int e = errno;
85
57
        errno = e;
86
58
        return false;
87
59
      }
88
 
    } while(ret == -1);
 
60
    }while(ret == -1);
89
61
    cmd_line = cmd_line_alloc;
90
 
    cmd_line_len = (size_t)ret + 1;
91
62
  }
92
63
  
 
64
  size_t cmd_line_len = (size_t)ret + 1;
93
65
  size_t written = 0;
94
 
  ssize_t sret = 0;
95
66
  while(not interrupted_by_signal and written < cmd_line_len){
96
 
    sret = write(fifo_fd, cmd_line + written,
97
 
                 cmd_line_len - written);
98
 
    if(sret == -1){
 
67
    ret = write(fifo_fd, cmd_line + written,
 
68
                cmd_line_len - written);
 
69
    if(ret == -1){
99
70
      if(errno != EINTR or interrupted_by_signal){
100
71
        int e = errno;
101
72
        close(fifo_fd);
106
77
        continue;
107
78
      }
108
79
    }
109
 
    written += (size_t)sret;
 
80
    written += (size_t)ret;
110
81
  }
111
82
  free(cmd_line_alloc);
112
 
  do {
 
83
  do{
113
84
    ret = close(fifo_fd);
114
85
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
115
86
      return false;
116
87
    }
117
 
  } while(ret == -1);
 
88
  }while(ret == -1);
118
89
  if(interrupted_by_signal){
119
90
    return false;
120
91
  }
132
103
  {
133
104
    const char *const cryptsource = getenv("cryptsource");
134
105
    const char *const crypttarget = getenv("crypttarget");
135
 
    const char prompt_start[] = "Enter passphrase to unlock the disk";
 
106
    const char *const prompt_start = "Enter passphrase to unlock the disk";
136
107
    
137
108
    if(cryptsource == NULL){
138
109
      if(crypttarget == NULL){
169
140
    for(struct dirent *proc_ent = readdir(proc_dir);
170
141
        proc_ent != NULL;
171
142
        proc_ent = readdir(proc_dir)){
172
 
      pid_t pid;
173
 
      {
174
 
        intmax_t tmpmax;
175
 
        char *tmp;
176
 
        errno = 0;
177
 
        tmpmax = strtoimax(proc_ent->d_name, &tmp, 10);
178
 
        if(errno != 0 or tmp == proc_ent->d_name or *tmp != '\0'
179
 
           or tmpmax != (pid_t)tmpmax){
180
 
          /* Not a process */
181
 
          continue;
182
 
        }
183
 
        pid = (pid_t)tmpmax;
 
143
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
 
144
      if(pid == 0){
 
145
        /* Not a process */
 
146
        continue;
184
147
      }
185
148
      /* Find the executable name by doing readlink() on the
186
149
         /proc/<pid>/exe link */
187
150
      char exe_target[sizeof(usplash_name)];
188
151
      {
189
 
        /* create file name string */
190
152
        char *exe_link;
191
153
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
192
154
        if(ret == -1){
195
157
          closedir(proc_dir);
196
158
          return EXIT_FAILURE;
197
159
        }
198
 
        
199
 
        /* Check that it refers to a symlink owned by root:root */
200
 
        struct stat exe_stat;
201
 
        ret = lstat(exe_link, &exe_stat);
202
 
        if(ret == -1){
203
 
          if(errno == ENOENT){
204
 
            free(exe_link);
205
 
            continue;
206
 
          }
207
 
          perror("lstat");
208
 
          free(exe_link);
209
 
          free(prompt);
210
 
          closedir(proc_dir);
211
 
          return EXIT_FAILURE;
212
 
        }
213
 
        if(not S_ISLNK(exe_stat.st_mode)
214
 
           or exe_stat.st_uid != 0
215
 
           or exe_stat.st_gid != 0){
216
 
          free(exe_link);
217
 
          continue;
218
 
        }
219
 
        
220
160
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
221
161
        free(exe_link);
222
162
      }
251
191
          size_t cmdline_allocated = 0;
252
192
          char *tmp;
253
193
          const size_t blocksize = 1024;
254
 
          do {
 
194
          do{
255
195
            if(cmdline_len + blocksize > cmdline_allocated){
256
196
              tmp = realloc(cmdline, cmdline_allocated + blocksize);
257
197
              if(tmp == NULL){
302
242
      free(prompt);
303
243
      return EXIT_FAILURE;
304
244
    }
305
 
    if(old_action.sa_handler != SIG_IGN){
 
245
    if (old_action.sa_handler != SIG_IGN){
306
246
      ret = sigaction(SIGINT, &new_action, NULL);
307
247
      if(ret == -1){
308
248
        perror("sigaction");
316
256
      free(prompt);
317
257
      return EXIT_FAILURE;
318
258
    }
319
 
    if(old_action.sa_handler != SIG_IGN){
 
259
    if (old_action.sa_handler != SIG_IGN){
320
260
      ret = sigaction(SIGHUP, &new_action, NULL);
321
261
      if(ret == -1){
322
262
        perror("sigaction");
330
270
      free(prompt);
331
271
      return EXIT_FAILURE;
332
272
    }
333
 
    if(old_action.sa_handler != SIG_IGN){
 
273
    if (old_action.sa_handler != SIG_IGN){
334
274
      ret = sigaction(SIGTERM, &new_action, NULL);
335
275
      if(ret == -1){
336
276
        perror("sigaction");
365
305
    
366
306
    /* Open FIFO */
367
307
    int fifo_fd;
368
 
    do {
 
308
    do{
369
309
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
370
310
      if(fifo_fd == -1){
371
311
        if(errno != EINTR){
377
317
          break;
378
318
        }
379
319
      }
380
 
    } while(fifo_fd == -1);
 
320
    }while(fifo_fd == -1);
381
321
    if(interrupted_by_signal or an_error_occured){
382
322
      break;                    /* Big */
383
323
    }
385
325
    /* Read from FIFO */
386
326
    size_t buf_allocated = 0;
387
327
    const size_t blocksize = 1024;
388
 
    do {
 
328
    do{
389
329
      if(buf_len + blocksize > buf_allocated){
390
330
        char *tmp = realloc(buf, buf_allocated + blocksize);
391
331
        if(tmp == NULL){
396
336
        buf = tmp;
397
337
        buf_allocated += blocksize;
398
338
      }
399
 
      do {
 
339
      do{
400
340
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
401
341
        if(sret == -1){
402
342
          if(errno != EINTR){
408
348
            break;
409
349
          }
410
350
        }
411
 
      } while(sret == -1);
 
351
      }while(sret == -1);
412
352
      if(interrupted_by_signal or an_error_occured){
413
353
        break;
414
354
      }
415
355
      
416
356
      buf_len += (size_t)sret;
417
 
    } while(sret != 0);
 
357
    }while(sret != 0);
418
358
    close(fifo_fd);
419
359
    if(interrupted_by_signal or an_error_occured){
420
360
      break;                    /* Big */
431
371
    
432
372
    /* Print password to stdout */
433
373
    size_t written = 0;
434
 
    while(written < buf_len){
435
 
      do {
 
374
    do{
 
375
      do{
436
376
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
437
377
        if(sret == -1){
438
378
          if(errno != EINTR){
444
384
            break;
445
385
          }
446
386
        }
447
 
      } while(sret == -1);
 
387
      }while(sret == -1);
448
388
      if(interrupted_by_signal or an_error_occured){
449
389
        break;
450
390
      }
 
391
      
451
392
      written += (size_t)sret;
452
 
    }
453
 
    free(buf);
 
393
    }while(written < buf_len);
454
394
    if(not interrupted_by_signal and not an_error_occured){
455
 
      free(cmdline);
456
395
      return EXIT_SUCCESS;
457
396
    }
458
397
    break;                      /* Big */
459
 
  }                             /* end of non-loop while() */
 
398
  }
460
399
  
461
400
  /* If we got here, an error or interrupt must have happened */
462
401
  
463
 
  /* Create argc and argv for new usplash*/
464
402
  int cmdline_argc = 0;
465
403
  char **cmdline_argv = malloc(sizeof(char *));
 
404
  /* Create argv and argc for new usplash*/
466
405
  {
467
406
    size_t position = 0;
468
407
    while(position < cmdline_len){
469
408
      char **tmp = realloc(cmdline_argv,
470
 
                           (sizeof(char *)
471
 
                            * (size_t)(cmdline_argc + 2)));
 
409
                           (sizeof(char *) * (size_t)(cmdline_argc + 2)));
472
410
      if(tmp == NULL){
473
411
        perror("realloc");
474
412
        free(cmdline_argv);
482
420
    cmdline_argv[cmdline_argc] = NULL;
483
421
  }
484
422
  /* Kill old usplash */
485
 
  kill(usplash_pid, SIGTERM);
486
 
  sleep(2);
 
423
    kill(usplash_pid, SIGTERM);
 
424
    sleep(2);
487
425
  while(kill(usplash_pid, 0) == 0){
488
426
    kill(usplash_pid, SIGKILL);
489
427
    sleep(1);
493
431
    /* Child; will become new usplash process */
494
432
    
495
433
    /* Make the effective user ID (root) the only user ID instead of
496
 
       the real user ID (_mandos) */
 
434
       the real user ID (mandos) */
497
435
    ret = setuid(geteuid());
498
 
    if(ret == -1){
 
436
    if (ret == -1){
499
437
      perror("setuid");
500
438
    }
501
439
    
511
449
    }
512
450
    
513
451
    execv(usplash_name, cmdline_argv);
514
 
    if(not interrupted_by_signal){
515
 
      perror("execv");
516
 
    }
517
 
    free(cmdline);
518
 
    free(cmdline_argv);
519
 
    _exit(EXIT_FAILURE);
520
452
  }
521
 
  free(cmdline);
522
 
  free(cmdline_argv);
523
453
  sleep(2);
524
454
  if(not usplash_write("PULSATE", NULL)
525
455
     and (errno != EINTR)){