/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-07 23:48:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090907234817-op1lgg9gpruejsiv
* initramfs-tools-hook: Bug fix: Really install user-supplied plugins.
                        Bug fix: Warn about empty plugin directory.

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
27
                                   sigemptyset(), sigaddset(), SIGINT,
5
29
                                   SIG_IGN, kill(), SIGKILL */
6
30
#include <stdbool.h>            /* bool, false, true */
7
31
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
 
32
#include <iso646.h>             /* and, or, not*/
8
33
#include <errno.h>              /* errno, EINTR */
9
 
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR,
10
 
                                   struct dirent */
 
34
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
 
35
                                   dirent */
11
36
#include <stddef.h>             /* NULL */
12
37
#include <string.h>             /* strlen(), memcmp() */
13
38
#include <stdio.h>              /* asprintf(), perror() */
16
41
                                   fork(), setuid(), geteuid(),
17
42
                                   setsid(), chdir(), dup2(),
18
43
                                   STDERR_FILENO, execv() */
19
 
#include <stdlib.h>             /* free(), EXIT_FAILURE, strtoul(),
20
 
                                   realloc(), EXIT_SUCCESS, malloc(),
21
 
                                   _exit() */
 
44
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
 
45
                                   EXIT_SUCCESS, malloc(), _exit() */
22
46
#include <stdlib.h>             /* getenv() */
23
47
#include <dirent.h>             /* opendir(), readdir(), closedir() */
24
 
 
25
 
 
26
 
 
27
 
#include <iso646.h>             /* and */
28
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
29
 
                                   WEXITSTATUS() */
 
48
#include <inttypes.h>           /* intmax_t, strtoimax() */
 
49
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
30
50
 
31
51
sig_atomic_t interrupted_by_signal = 0;
32
52
 
36
56
 
37
57
static bool usplash_write(const char *cmd, const char *arg){
38
58
  /* 
39
 
   * usplash_write("TIMEOUT", "15"); -> "TIMEOUT 15\0"
40
 
   * 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"
41
61
   * SEE ALSO
42
62
   *         usplash_write(8)
43
63
   */
44
64
  int ret;
45
65
  int fifo_fd;
46
 
  do{
 
66
  do {
47
67
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
48
68
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
49
69
      return false;
50
70
    }
51
 
  }while(fifo_fd == -1);
 
71
  } while(fifo_fd == -1);
52
72
  
53
73
  const char *cmd_line;
54
74
  size_t cmd_line_len;
56
76
  if(arg == NULL){
57
77
    cmd_line = cmd;
58
78
    cmd_line_len = strlen(cmd);
59
 
  }else{
60
 
    do{
 
79
  } else {
 
80
    do {
61
81
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
62
82
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
63
83
        int e = errno;
65
85
        errno = e;
66
86
        return false;
67
87
      }
68
 
    }while(ret == -1);
 
88
    } while(ret == -1);
69
89
    cmd_line = cmd_line_alloc;
70
90
    cmd_line_len = (size_t)ret + 1;
71
91
  }
72
92
  
73
93
  size_t written = 0;
 
94
  ssize_t sret = 0;
74
95
  while(not interrupted_by_signal and written < cmd_line_len){
75
 
    ret = write(fifo_fd, cmd_line + written,
76
 
                cmd_line_len - written);
77
 
    if(ret == -1){
 
96
    sret = write(fifo_fd, cmd_line + written,
 
97
                 cmd_line_len - written);
 
98
    if(sret == -1){
78
99
      if(errno != EINTR or interrupted_by_signal){
79
100
        int e = errno;
80
101
        close(fifo_fd);
85
106
        continue;
86
107
      }
87
108
    }
88
 
    written += (size_t)ret;
 
109
    written += (size_t)sret;
89
110
  }
90
111
  free(cmd_line_alloc);
91
 
  do{
 
112
  do {
92
113
    ret = close(fifo_fd);
93
114
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
94
115
      return false;
95
116
    }
96
 
  }while(ret == -1);
 
117
  } while(ret == -1);
97
118
  if(interrupted_by_signal){
98
119
    return false;
99
120
  }
148
169
    for(struct dirent *proc_ent = readdir(proc_dir);
149
170
        proc_ent != NULL;
150
171
        proc_ent = readdir(proc_dir)){
151
 
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
152
 
      if(pid == 0){
153
 
        /* Not a process */
154
 
        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;
155
184
      }
156
185
      /* Find the executable name by doing readlink() on the
157
186
         /proc/<pid>/exe link */
158
187
      char exe_target[sizeof(usplash_name)];
159
188
      {
 
189
        /* create file name string */
160
190
        char *exe_link;
161
191
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
162
192
        if(ret == -1){
165
195
          closedir(proc_dir);
166
196
          return EXIT_FAILURE;
167
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
        
168
220
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
169
221
        free(exe_link);
170
 
        if(sret == -1){
171
 
          continue;
172
 
        }
173
222
      }
174
223
      if((sret == ((ssize_t)sizeof(exe_target)-1))
175
224
         and (memcmp(usplash_name, exe_target,
202
251
          size_t cmdline_allocated = 0;
203
252
          char *tmp;
204
253
          const size_t blocksize = 1024;
205
 
          do{
 
254
          do {
206
255
            if(cmdline_len + blocksize > cmdline_allocated){
207
256
              tmp = realloc(cmdline, cmdline_allocated + blocksize);
208
257
              if(tmp == NULL){
253
302
      free(prompt);
254
303
      return EXIT_FAILURE;
255
304
    }
256
 
    if (old_action.sa_handler != SIG_IGN){
 
305
    if(old_action.sa_handler != SIG_IGN){
257
306
      ret = sigaction(SIGINT, &new_action, NULL);
258
307
      if(ret == -1){
259
308
        perror("sigaction");
267
316
      free(prompt);
268
317
      return EXIT_FAILURE;
269
318
    }
270
 
    if (old_action.sa_handler != SIG_IGN){
 
319
    if(old_action.sa_handler != SIG_IGN){
271
320
      ret = sigaction(SIGHUP, &new_action, NULL);
272
321
      if(ret == -1){
273
322
        perror("sigaction");
281
330
      free(prompt);
282
331
      return EXIT_FAILURE;
283
332
    }
284
 
    if (old_action.sa_handler != SIG_IGN){
 
333
    if(old_action.sa_handler != SIG_IGN){
285
334
      ret = sigaction(SIGTERM, &new_action, NULL);
286
335
      if(ret == -1){
287
336
        perror("sigaction");
316
365
    
317
366
    /* Open FIFO */
318
367
    int fifo_fd;
319
 
    do{
 
368
    do {
320
369
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
321
370
      if(fifo_fd == -1){
322
371
        if(errno != EINTR){
328
377
          break;
329
378
        }
330
379
      }
331
 
    }while(fifo_fd == -1);
 
380
    } while(fifo_fd == -1);
332
381
    if(interrupted_by_signal or an_error_occured){
333
382
      break;                    /* Big */
334
383
    }
336
385
    /* Read from FIFO */
337
386
    size_t buf_allocated = 0;
338
387
    const size_t blocksize = 1024;
339
 
    do{
 
388
    do {
340
389
      if(buf_len + blocksize > buf_allocated){
341
390
        char *tmp = realloc(buf, buf_allocated + blocksize);
342
391
        if(tmp == NULL){
347
396
        buf = tmp;
348
397
        buf_allocated += blocksize;
349
398
      }
350
 
      do{
 
399
      do {
351
400
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
352
401
        if(sret == -1){
353
402
          if(errno != EINTR){
359
408
            break;
360
409
          }
361
410
        }
362
 
      }while(sret == -1);
 
411
      } while(sret == -1);
363
412
      if(interrupted_by_signal or an_error_occured){
364
413
        break;
365
414
      }
366
415
      
367
416
      buf_len += (size_t)sret;
368
 
    }while(sret != 0);
 
417
    } while(sret != 0);
369
418
    close(fifo_fd);
370
419
    if(interrupted_by_signal or an_error_occured){
371
420
      break;                    /* Big */
383
432
    /* Print password to stdout */
384
433
    size_t written = 0;
385
434
    while(written < buf_len){
386
 
      do{
 
435
      do {
387
436
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
388
437
        if(sret == -1){
389
438
          if(errno != EINTR){
395
444
            break;
396
445
          }
397
446
        }
398
 
      }while(sret == -1);
 
447
      } while(sret == -1);
399
448
      if(interrupted_by_signal or an_error_occured){
400
449
        break;
401
450
      }
407
456
      return EXIT_SUCCESS;
408
457
    }
409
458
    break;                      /* Big */
410
 
  }
 
459
  }                             /* end of non-loop while() */
411
460
  
412
461
  /* If we got here, an error or interrupt must have happened */
413
462
  
 
463
  /* Create argc and argv for new usplash*/
414
464
  int cmdline_argc = 0;
415
465
  char **cmdline_argv = malloc(sizeof(char *));
416
 
  /* Create argv and argc for new usplash*/
417
466
  {
418
467
    size_t position = 0;
419
468
    while(position < cmdline_len){
433
482
    cmdline_argv[cmdline_argc] = NULL;
434
483
  }
435
484
  /* Kill old usplash */
436
 
    kill(usplash_pid, SIGTERM);
437
 
    sleep(2);
 
485
  kill(usplash_pid, SIGTERM);
 
486
  sleep(2);
438
487
  while(kill(usplash_pid, 0) == 0){
439
488
    kill(usplash_pid, SIGKILL);
440
489
    sleep(1);
444
493
    /* Child; will become new usplash process */
445
494
    
446
495
    /* Make the effective user ID (root) the only user ID instead of
447
 
       the real user ID (mandos) */
 
496
       the real user ID (_mandos) */
448
497
    ret = setuid(geteuid());
449
 
    if (ret == -1){
 
498
    if(ret == -1){
450
499
      perror("setuid");
451
500
    }
452
501
    
462
511
    }
463
512
    
464
513
    execv(usplash_name, cmdline_argv);
465
 
    perror("execv");
 
514
    if(not interrupted_by_signal){
 
515
      perror("execv");
 
516
    }
466
517
    free(cmdline);
467
518
    free(cmdline_argv);
468
519
    _exit(EXIT_FAILURE);