/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: 2008-10-17 18:56:25 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081017185625-xrfv8oevzm7lyvl6
Tags: version-1.0.2-1
* Makefile (version): Changed to "1.0.2".
* NEWS (Version 1.0.2): New entry.
* debian/changelog (1.0.2-1): New entry.

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,
41
17
                                   fork(), setuid(), geteuid(),
42
18
                                   setsid(), chdir(), dup2(),
43
19
                                   STDERR_FILENO, execv() */
44
 
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
45
 
                                   EXIT_SUCCESS, malloc(), _exit() */
 
20
#include <stdlib.h>             /* free(), EXIT_FAILURE, strtoul(),
 
21
                                   realloc(), EXIT_SUCCESS, malloc(),
 
22
                                   _exit() */
46
23
#include <stdlib.h>             /* getenv() */
47
24
#include <dirent.h>             /* opendir(), readdir(), closedir() */
48
 
#include <inttypes.h>           /* intmax_t, strtoimax() */
49
25
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
50
26
 
51
27
sig_atomic_t interrupted_by_signal = 0;
63
39
   */
64
40
  int ret;
65
41
  int fifo_fd;
66
 
  do {
 
42
  do{
67
43
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
68
44
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
69
45
      return false;
70
46
    }
71
 
  } while(fifo_fd == -1);
 
47
  }while(fifo_fd == -1);
72
48
  
73
49
  const char *cmd_line;
74
50
  size_t cmd_line_len;
76
52
  if(arg == NULL){
77
53
    cmd_line = cmd;
78
54
    cmd_line_len = strlen(cmd);
79
 
  } else {
80
 
    do {
 
55
  }else{
 
56
    do{
81
57
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
82
58
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
83
59
        int e = errno;
85
61
        errno = e;
86
62
        return false;
87
63
      }
88
 
    } while(ret == -1);
 
64
    }while(ret == -1);
89
65
    cmd_line = cmd_line_alloc;
90
66
    cmd_line_len = (size_t)ret + 1;
91
67
  }
92
68
  
93
69
  size_t written = 0;
94
 
  ssize_t sret = 0;
95
70
  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){
 
71
    ret = write(fifo_fd, cmd_line + written,
 
72
                cmd_line_len - written);
 
73
    if(ret == -1){
99
74
      if(errno != EINTR or interrupted_by_signal){
100
75
        int e = errno;
101
76
        close(fifo_fd);
106
81
        continue;
107
82
      }
108
83
    }
109
 
    written += (size_t)sret;
 
84
    written += (size_t)ret;
110
85
  }
111
86
  free(cmd_line_alloc);
112
 
  do {
 
87
  do{
113
88
    ret = close(fifo_fd);
114
89
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
115
90
      return false;
116
91
    }
117
 
  } while(ret == -1);
 
92
  }while(ret == -1);
118
93
  if(interrupted_by_signal){
119
94
    return false;
120
95
  }
169
144
    for(struct dirent *proc_ent = readdir(proc_dir);
170
145
        proc_ent != NULL;
171
146
        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;
 
147
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
 
148
      if(pid == 0){
 
149
        /* Not a process */
 
150
        continue;
184
151
      }
185
152
      /* Find the executable name by doing readlink() on the
186
153
         /proc/<pid>/exe link */
200
167
        struct stat exe_stat;
201
168
        ret = lstat(exe_link, &exe_stat);
202
169
        if(ret == -1){
203
 
          if(errno == ENOENT){
204
 
            free(exe_link);
205
 
            continue;
206
 
          }
207
170
          perror("lstat");
208
171
          free(exe_link);
209
172
          free(prompt);
219
182
        
220
183
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
221
184
        free(exe_link);
 
185
        if(sret == -1){
 
186
          continue;
 
187
        }
222
188
      }
223
189
      if((sret == ((ssize_t)sizeof(exe_target)-1))
224
190
         and (memcmp(usplash_name, exe_target,
251
217
          size_t cmdline_allocated = 0;
252
218
          char *tmp;
253
219
          const size_t blocksize = 1024;
254
 
          do {
 
220
          do{
255
221
            if(cmdline_len + blocksize > cmdline_allocated){
256
222
              tmp = realloc(cmdline, cmdline_allocated + blocksize);
257
223
              if(tmp == NULL){
365
331
    
366
332
    /* Open FIFO */
367
333
    int fifo_fd;
368
 
    do {
 
334
    do{
369
335
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
370
336
      if(fifo_fd == -1){
371
337
        if(errno != EINTR){
377
343
          break;
378
344
        }
379
345
      }
380
 
    } while(fifo_fd == -1);
 
346
    }while(fifo_fd == -1);
381
347
    if(interrupted_by_signal or an_error_occured){
382
348
      break;                    /* Big */
383
349
    }
385
351
    /* Read from FIFO */
386
352
    size_t buf_allocated = 0;
387
353
    const size_t blocksize = 1024;
388
 
    do {
 
354
    do{
389
355
      if(buf_len + blocksize > buf_allocated){
390
356
        char *tmp = realloc(buf, buf_allocated + blocksize);
391
357
        if(tmp == NULL){
396
362
        buf = tmp;
397
363
        buf_allocated += blocksize;
398
364
      }
399
 
      do {
 
365
      do{
400
366
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
401
367
        if(sret == -1){
402
368
          if(errno != EINTR){
408
374
            break;
409
375
          }
410
376
        }
411
 
      } while(sret == -1);
 
377
      }while(sret == -1);
412
378
      if(interrupted_by_signal or an_error_occured){
413
379
        break;
414
380
      }
415
381
      
416
382
      buf_len += (size_t)sret;
417
 
    } while(sret != 0);
 
383
    }while(sret != 0);
418
384
    close(fifo_fd);
419
385
    if(interrupted_by_signal or an_error_occured){
420
386
      break;                    /* Big */
432
398
    /* Print password to stdout */
433
399
    size_t written = 0;
434
400
    while(written < buf_len){
435
 
      do {
 
401
      do{
436
402
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
437
403
        if(sret == -1){
438
404
          if(errno != EINTR){
444
410
            break;
445
411
          }
446
412
        }
447
 
      } while(sret == -1);
 
413
      }while(sret == -1);
448
414
      if(interrupted_by_signal or an_error_occured){
449
415
        break;
450
416
      }
493
459
    /* Child; will become new usplash process */
494
460
    
495
461
    /* Make the effective user ID (root) the only user ID instead of
496
 
       the real user ID (_mandos) */
 
462
       the real user ID (mandos) */
497
463
    ret = setuid(geteuid());
498
464
    if(ret == -1){
499
465
      perror("setuid");