/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: 2009-09-08 06:28:20 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090908062820-xgkdkrnvuze9vc72
* debian/mandos-client.README.Debian: Improved wording and formatting.
                                      Updated location of nfsroot.txt.
* debian/mandos.README.Debian: Improved wording and formatting.

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