/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-01-12 22:02:33 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090112220233-lawxo4uvyzb38u1f
* README (The Plugin System): Removed redundant text about options and
                              files for the plugins, this is now
                              documented in the manuals for the
                              plugins.

* plugins.d/mandos-client.c (main): Remove comment which was copied
                                    from another program by mistake.
                                    Use "sscanf" instead of "strtol"
                                    to parse numbers; this uses the
                                    correct type instead of casting.
                                    Don't report errors when removing
                                    temporary directory if directory
                                    is already gone.

Show diffs side-by-side

added added

removed removed

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