/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/usplash.c

* init.d-mandos: Bug fix: Correct the LSB header.

* mandos.xml (CHECKING): Add text about client disabling requiring
                         manual intervention to reverse.

* plugin-runner.c: Cast return value of TEMP_FAILURE_RETRY to "int"
                   where necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Passprompt - Read a password from usplash and output it
 
3
 * Usplash - Read a password from usplash and output it
4
4
 * 
5
5
 * Copyright © 2008,2009 Teddy Hogeborn
6
6
 * Copyright © 2008,2009 Björn Påhlsson
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
23
 
 * <https://www.fukt.bsnet.se/~teddy/>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
25
#define _GNU_SOURCE             /* asprintf() */
42
41
                                   fork(), setuid(), geteuid(),
43
42
                                   setsid(), chdir(), dup2(),
44
43
                                   STDERR_FILENO, execv() */
45
 
#include <stdlib.h>             /* free(), EXIT_FAILURE, strtoul(),
46
 
                                   realloc(), EXIT_SUCCESS, malloc(),
47
 
                                   _exit() */
 
44
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
 
45
                                   EXIT_SUCCESS, malloc(), _exit() */
48
46
#include <stdlib.h>             /* getenv() */
49
47
#include <dirent.h>             /* opendir(), readdir(), closedir() */
 
48
#include <inttypes.h>           /* intmax_t, strtoimax() */
50
49
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
51
50
 
52
51
sig_atomic_t interrupted_by_signal = 0;
64
63
   */
65
64
  int ret;
66
65
  int fifo_fd;
67
 
  do{
 
66
  do {
68
67
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
69
68
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
70
69
      return false;
71
70
    }
72
 
  }while(fifo_fd == -1);
 
71
  } while(fifo_fd == -1);
73
72
  
74
73
  const char *cmd_line;
75
74
  size_t cmd_line_len;
77
76
  if(arg == NULL){
78
77
    cmd_line = cmd;
79
78
    cmd_line_len = strlen(cmd);
80
 
  }else{
81
 
    do{
 
79
  } else {
 
80
    do {
82
81
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
83
82
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
84
83
        int e = errno;
86
85
        errno = e;
87
86
        return false;
88
87
      }
89
 
    }while(ret == -1);
 
88
    } while(ret == -1);
90
89
    cmd_line = cmd_line_alloc;
91
90
    cmd_line_len = (size_t)ret + 1;
92
91
  }
110
109
    written += (size_t)sret;
111
110
  }
112
111
  free(cmd_line_alloc);
113
 
  do{
 
112
  do {
114
113
    ret = close(fifo_fd);
115
114
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
116
115
      return false;
117
116
    }
118
 
  }while(ret == -1);
 
117
  } while(ret == -1);
119
118
  if(interrupted_by_signal){
120
119
    return false;
121
120
  }
170
169
    for(struct dirent *proc_ent = readdir(proc_dir);
171
170
        proc_ent != NULL;
172
171
        proc_ent = readdir(proc_dir)){
173
 
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
174
 
      if(pid == 0){
175
 
        /* Not a process */
176
 
        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;
177
184
      }
178
185
      /* Find the executable name by doing readlink() on the
179
186
         /proc/<pid>/exe link */
193
200
        struct stat exe_stat;
194
201
        ret = lstat(exe_link, &exe_stat);
195
202
        if(ret == -1){
 
203
          if(errno == ENOENT){
 
204
            free(exe_link);
 
205
            continue;
 
206
          }
196
207
          perror("lstat");
197
208
          free(exe_link);
198
209
          free(prompt);
208
219
        
209
220
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
210
221
        free(exe_link);
211
 
        if(sret == -1){
212
 
          continue;
213
 
        }
214
222
      }
215
223
      if((sret == ((ssize_t)sizeof(exe_target)-1))
216
224
         and (memcmp(usplash_name, exe_target,
243
251
          size_t cmdline_allocated = 0;
244
252
          char *tmp;
245
253
          const size_t blocksize = 1024;
246
 
          do{
 
254
          do {
247
255
            if(cmdline_len + blocksize > cmdline_allocated){
248
256
              tmp = realloc(cmdline, cmdline_allocated + blocksize);
249
257
              if(tmp == NULL){
357
365
    
358
366
    /* Open FIFO */
359
367
    int fifo_fd;
360
 
    do{
 
368
    do {
361
369
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
362
370
      if(fifo_fd == -1){
363
371
        if(errno != EINTR){
369
377
          break;
370
378
        }
371
379
      }
372
 
    }while(fifo_fd == -1);
 
380
    } while(fifo_fd == -1);
373
381
    if(interrupted_by_signal or an_error_occured){
374
382
      break;                    /* Big */
375
383
    }
377
385
    /* Read from FIFO */
378
386
    size_t buf_allocated = 0;
379
387
    const size_t blocksize = 1024;
380
 
    do{
 
388
    do {
381
389
      if(buf_len + blocksize > buf_allocated){
382
390
        char *tmp = realloc(buf, buf_allocated + blocksize);
383
391
        if(tmp == NULL){
388
396
        buf = tmp;
389
397
        buf_allocated += blocksize;
390
398
      }
391
 
      do{
 
399
      do {
392
400
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
393
401
        if(sret == -1){
394
402
          if(errno != EINTR){
400
408
            break;
401
409
          }
402
410
        }
403
 
      }while(sret == -1);
 
411
      } while(sret == -1);
404
412
      if(interrupted_by_signal or an_error_occured){
405
413
        break;
406
414
      }
407
415
      
408
416
      buf_len += (size_t)sret;
409
 
    }while(sret != 0);
 
417
    } while(sret != 0);
410
418
    close(fifo_fd);
411
419
    if(interrupted_by_signal or an_error_occured){
412
420
      break;                    /* Big */
424
432
    /* Print password to stdout */
425
433
    size_t written = 0;
426
434
    while(written < buf_len){
427
 
      do{
 
435
      do {
428
436
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
429
437
        if(sret == -1){
430
438
          if(errno != EINTR){
436
444
            break;
437
445
          }
438
446
        }
439
 
      }while(sret == -1);
 
447
      } while(sret == -1);
440
448
      if(interrupted_by_signal or an_error_occured){
441
449
        break;
442
450
      }
485
493
    /* Child; will become new usplash process */
486
494
    
487
495
    /* Make the effective user ID (root) the only user ID instead of
488
 
       the real user ID (mandos) */
 
496
       the real user ID (_mandos) */
489
497
    ret = setuid(geteuid());
490
498
    if(ret == -1){
491
499
      perror("setuid");