/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

merge

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