/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-09-24 23:12:49 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080924231249-0xhjl5ydrbcjvce7
* debian/mandos-client.lintian-overrides: Ignore setuid
                                          "plugins.d/usplash".

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#define _GNU_SOURCE             /* asprintf() */
2
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
3
 
                                   sigemptyset(), sigaddset(), SIGINT,
4
 
                                   SIGHUP, SIGTERM, sigaction(),
5
 
                                   SIG_IGN, kill(), SIGKILL */
6
 
#include <stdbool.h>            /* bool, false, true */
7
 
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
8
 
#include <iso646.h>             /* and, or, not*/
9
 
#include <errno.h>              /* errno, EINTR */
10
 
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
11
 
                                   dirent */
 
3
                                   sigemptyset(), sigaddset(),
 
4
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
 
5
                                   SIGTERM, kill(), SIGKILL */
12
6
#include <stddef.h>             /* NULL */
13
 
#include <string.h>             /* strlen(), memcmp() */
 
7
#include <stdlib.h>             /* getenv() */
14
8
#include <stdio.h>              /* asprintf(), perror() */
15
 
#include <unistd.h>             /* close(), write(), readlink(),
16
 
                                   read(), STDOUT_FILENO, sleep(),
17
 
                                   fork(), setuid(), geteuid(),
18
 
                                   setsid(), chdir(), dup2(),
19
 
                                   STDERR_FILENO, execv() */
20
 
#include <stdlib.h>             /* free(), EXIT_FAILURE, strtoul(),
21
 
                                   realloc(), EXIT_SUCCESS, malloc(),
22
 
                                   _exit() */
23
 
#include <stdlib.h>             /* getenv() */
 
9
#include <stdlib.h>             /* EXIT_FAILURE, EXIT_SUCCESS,
 
10
                                   strtoul(), free() */
 
11
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
 
12
                                   ssize_t */
24
13
#include <dirent.h>             /* opendir(), readdir(), closedir() */
25
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
 
14
#include <unistd.h>             /* readlink(), fork(), execl(),
 
15
                                   _exit */
 
16
#include <string.h>             /* memcmp() */
 
17
#include <iso646.h>             /* and */
 
18
#include <stdbool.h>            /* bool, false, true */
 
19
#include <errno.h>              /* errno */
 
20
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
 
21
                                   WEXITSTATUS() */
 
22
#include <fcntl.h>              /* open(), O_RDONLY */
26
23
 
27
24
sig_atomic_t interrupted_by_signal = 0;
28
25
 
30
27
  interrupted_by_signal = 1;
31
28
}
32
29
 
33
 
static bool usplash_write(const char *cmd, const char *arg){
34
 
  /* 
35
 
   * usplash_write("TIMEOUT", "15") will write "TIMEOUT 15\0"
36
 
   * usplash_write("PULSATE", NULL) will write "PULSATE\0"
37
 
   * SEE ALSO
38
 
   *         usplash_write(8)
39
 
   */
40
 
  int ret;
41
 
  int fifo_fd;
42
 
  do{
43
 
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
44
 
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
45
 
      return false;
46
 
    }
47
 
  }while(fifo_fd == -1);
48
 
  
49
 
  const char *cmd_line;
50
 
  size_t cmd_line_len;
51
 
  char *cmd_line_alloc = NULL;
52
 
  if(arg == NULL){
53
 
    cmd_line = cmd;
54
 
    cmd_line_len = strlen(cmd);
55
 
  }else{
56
 
    do{
57
 
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
58
 
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
59
 
        int e = errno;
60
 
        close(fifo_fd);
61
 
        errno = e;
62
 
        return false;
63
 
      }
64
 
    }while(ret == -1);
65
 
    cmd_line = cmd_line_alloc;
66
 
    cmd_line_len = (size_t)ret + 1;
67
 
  }
68
 
  
69
 
  size_t written = 0;
70
 
  while(not interrupted_by_signal and written < cmd_line_len){
71
 
    ret = write(fifo_fd, cmd_line + written,
72
 
                cmd_line_len - written);
73
 
    if(ret == -1){
74
 
      if(errno != EINTR or interrupted_by_signal){
75
 
        int e = errno;
76
 
        close(fifo_fd);
77
 
        free(cmd_line_alloc);
78
 
        errno = e;
79
 
        return false;
80
 
      } else {
81
 
        continue;
82
 
      }
83
 
    }
84
 
    written += (size_t)ret;
85
 
  }
86
 
  free(cmd_line_alloc);
87
 
  do{
88
 
    ret = close(fifo_fd);
89
 
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
90
 
      return false;
91
 
    }
92
 
  }while(ret == -1);
93
 
  if(interrupted_by_signal){
94
 
    return false;
95
 
  }
96
 
  return true;
97
 
}
98
 
 
99
30
int main(__attribute__((unused))int argc,
100
31
         __attribute__((unused))char **argv){
101
32
  int ret = 0;
107
38
  {
108
39
    const char *const cryptsource = getenv("cryptsource");
109
40
    const char *const crypttarget = getenv("crypttarget");
110
 
    const char prompt_start[] = "Enter passphrase to unlock the disk";
 
41
    const char *const prompt_start = "Enter passphrase to unlock the disk";
111
42
    
112
43
    if(cryptsource == NULL){
113
44
      if(crypttarget == NULL){
133
64
  pid_t usplash_pid = 0;
134
65
  char *cmdline = NULL;
135
66
  size_t cmdline_len = 0;
136
 
  const char usplash_name[] = "/sbin/usplash";
137
67
  {
 
68
    const char usplash_name[] = "/sbin/usplash";
138
69
    DIR *proc_dir = opendir("/proc");
139
70
    if(proc_dir == NULL){
140
71
      free(prompt);
153
84
         /proc/<pid>/exe link */
154
85
      char exe_target[sizeof(usplash_name)];
155
86
      {
156
 
        /* create file name string */
157
87
        char *exe_link;
158
88
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
159
89
        if(ret == -1){
162
92
          closedir(proc_dir);
163
93
          return EXIT_FAILURE;
164
94
        }
165
 
        
166
 
        /* Check that it refers to a symlink owned by root:root */
167
 
        struct stat exe_stat;
168
 
        ret = lstat(exe_link, &exe_stat);
169
 
        if(ret == -1){
170
 
          perror("lstat");
171
 
          free(exe_link);
172
 
          free(prompt);
173
 
          closedir(proc_dir);
174
 
          return EXIT_FAILURE;
175
 
        }
176
 
        if(not S_ISLNK(exe_stat.st_mode)
177
 
           or exe_stat.st_uid != 0
178
 
           or exe_stat.st_gid != 0){
179
 
          free(exe_link);
180
 
          continue;
181
 
        }
182
 
        
183
95
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
184
96
        free(exe_link);
185
 
        if(sret == -1){
186
 
          continue;
187
 
        }
188
97
      }
189
98
      if((sret == ((ssize_t)sizeof(exe_target)-1))
190
99
         and (memcmp(usplash_name, exe_target,
268
177
      free(prompt);
269
178
      return EXIT_FAILURE;
270
179
    }
271
 
    if(old_action.sa_handler != SIG_IGN){
 
180
    if (old_action.sa_handler != SIG_IGN){
272
181
      ret = sigaction(SIGINT, &new_action, NULL);
273
182
      if(ret == -1){
274
183
        perror("sigaction");
282
191
      free(prompt);
283
192
      return EXIT_FAILURE;
284
193
    }
285
 
    if(old_action.sa_handler != SIG_IGN){
 
194
    if (old_action.sa_handler != SIG_IGN){
286
195
      ret = sigaction(SIGHUP, &new_action, NULL);
287
196
      if(ret == -1){
288
197
        perror("sigaction");
296
205
      free(prompt);
297
206
      return EXIT_FAILURE;
298
207
    }
299
 
    if(old_action.sa_handler != SIG_IGN){
 
208
    if (old_action.sa_handler != SIG_IGN){
300
209
      ret = sigaction(SIGTERM, &new_action, NULL);
301
210
      if(ret == -1){
302
211
        perror("sigaction");
308
217
  
309
218
  /* Write command to FIFO */
310
219
  if(not interrupted_by_signal){
311
 
    if(not usplash_write("TIMEOUT", "0")
312
 
       and (errno != EINTR)){
313
 
      perror("usplash_write");
314
 
      an_error_occured = true;
315
 
    }
316
 
  }
317
 
  if(not interrupted_by_signal and not an_error_occured){
318
 
    if(not usplash_write("INPUTQUIET", prompt)
319
 
       and (errno != EINTR)){
320
 
      perror("usplash_write");
321
 
      an_error_occured = true;
322
 
    }
323
 
  }
324
 
  free(prompt);
 
220
    int fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
 
221
    if(fifo_fd == -1){
 
222
      perror("open");
 
223
      free(prompt);
 
224
      return EXIT_FAILURE;
 
225
    }
 
226
    char *command;
 
227
    ret = asprintf(&command, "INPUTQUIET %s", prompt);
 
228
    if(ret == -1){
 
229
      perror("asprintf");
 
230
      free(prompt);
 
231
      return EXIT_FAILURE;
 
232
    }
 
233
    free(prompt);
 
234
    
 
235
    size_t command_len = (size_t)ret + 1;
 
236
    size_t written = 0;
 
237
    while(not interrupted_by_signal and written < command_len){
 
238
      ret = write(fifo_fd, command + written, command_len - written);
 
239
      if(ret == -1){
 
240
        if(interrupted_by_signal){
 
241
          break;
 
242
        }
 
243
        perror("write");
 
244
        if(written == 0){
 
245
          free(command);
 
246
          return EXIT_FAILURE;
 
247
        }
 
248
        an_error_occured = true;
 
249
        break;
 
250
      }
 
251
      written += (size_t)ret;
 
252
    }
 
253
    ret = close(fifo_fd);
 
254
    if(ret == -1 and not interrupted_by_signal){
 
255
      an_error_occured = true;
 
256
    }
 
257
    free(command);
 
258
  }else{
 
259
    free(prompt);
 
260
  }
325
261
  
326
 
  /* This is not really a loop; while() is used to be able to "break"
327
 
     out of it; those breaks are marked "Big" */
328
 
  while(not interrupted_by_signal and not an_error_occured){
 
262
  {
329
263
    char *buf = NULL;
330
264
    size_t buf_len = 0;
331
265
    
332
 
    /* Open FIFO */
333
 
    int fifo_fd;
334
 
    do{
335
 
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
336
 
      if(fifo_fd == -1){
337
 
        if(errno != EINTR){
338
 
          perror("open");
339
 
          an_error_occured = true;
340
 
          break;
341
 
        }
342
 
        if(interrupted_by_signal){
343
 
          break;
344
 
        }
345
 
      }
346
 
    }while(fifo_fd == -1);
347
 
    if(interrupted_by_signal or an_error_occured){
348
 
      break;                    /* Big */
349
 
    }
350
 
    
351
266
    /* Read from FIFO */
352
 
    size_t buf_allocated = 0;
353
 
    const size_t blocksize = 1024;
354
 
    do{
355
 
      if(buf_len + blocksize > buf_allocated){
356
 
        char *tmp = realloc(buf, buf_allocated + blocksize);
357
 
        if(tmp == NULL){
358
 
          perror("realloc");
359
 
          an_error_occured = true;
360
 
          break;
361
 
        }
362
 
        buf = tmp;
363
 
        buf_allocated += blocksize;
 
267
    if(not interrupted_by_signal and not an_error_occured){
 
268
      int fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
 
269
      if(fifo_fd == -1 and not interrupted_by_signal){
 
270
        perror("open");
 
271
        return EXIT_FAILURE;
364
272
      }
 
273
      size_t buf_allocated = 0;
 
274
      const int blocksize = 1024;
365
275
      do{
 
276
        if(buf_len + blocksize > buf_allocated){
 
277
          char *tmp = realloc(buf, buf_allocated + blocksize);
 
278
          if(tmp == NULL){
 
279
            perror("realloc");
 
280
            an_error_occured = true;
 
281
            break;
 
282
          }
 
283
          buf = tmp;
 
284
          buf_allocated += blocksize;
 
285
        }
366
286
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
367
287
        if(sret == -1){
368
 
          if(errno != EINTR){
369
 
            perror("read");
370
 
            an_error_occured = true;
371
 
            break;
372
 
          }
373
 
          if(interrupted_by_signal){
374
 
            break;
375
 
          }
 
288
          perror("read");
 
289
          an_error_occured = true;
 
290
          break;
376
291
        }
377
 
      }while(sret == -1);
378
 
      if(interrupted_by_signal or an_error_occured){
379
 
        break;
380
 
      }
381
 
      
382
 
      buf_len += (size_t)sret;
383
 
    }while(sret != 0);
384
 
    close(fifo_fd);
385
 
    if(interrupted_by_signal or an_error_occured){
386
 
      break;                    /* Big */
387
 
    }
388
 
    
389
 
    if(not usplash_write("TIMEOUT", "15")
390
 
       and (errno != EINTR)){
391
 
        perror("usplash_write");
392
 
        an_error_occured = true;
393
 
    }
394
 
    if(interrupted_by_signal or an_error_occured){
395
 
      break;                    /* Big */
396
 
    }
397
 
    
 
292
        buf_len += (size_t)sret;
 
293
      }while(not interrupted_by_signal and sret != 0);
 
294
      close(fifo_fd);
 
295
    }
 
296
  
398
297
    /* Print password to stdout */
399
 
    size_t written = 0;
400
 
    while(written < buf_len){
 
298
    if(not interrupted_by_signal and not an_error_occured){
 
299
      size_t written = 0;
401
300
      do{
402
301
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
403
 
        if(sret == -1){
404
 
          if(errno != EINTR){
405
 
            perror("write");
406
 
            an_error_occured = true;
407
 
            break;
408
 
          }
409
 
          if(interrupted_by_signal){
410
 
            break;
411
 
          }
 
302
        if(sret == -1 and not interrupted_by_signal){
 
303
          perror("write");
 
304
          an_error_occured = true;
 
305
          break;
412
306
        }
413
 
      }while(sret == -1);
414
 
      if(interrupted_by_signal or an_error_occured){
415
 
        break;
 
307
        written += (size_t)sret;
 
308
      }while(written < buf_len);
 
309
      if(not interrupted_by_signal and not an_error_occured){
 
310
        return EXIT_SUCCESS;
416
311
      }
417
 
      written += (size_t)sret;
418
 
    }
419
 
    free(buf);
420
 
    if(not interrupted_by_signal and not an_error_occured){
421
 
      free(cmdline);
422
 
      return EXIT_SUCCESS;
423
 
    }
424
 
    break;                      /* Big */
425
 
  }                             /* end of non-loop while() */
426
 
  
427
 
  /* If we got here, an error or interrupt must have happened */
428
 
  
429
 
  /* Create argc and argv for new usplash*/
 
312
    }
 
313
  }
 
314
  
 
315
  kill(usplash_pid, SIGTERM);
 
316
  
430
317
  int cmdline_argc = 0;
431
318
  char **cmdline_argv = malloc(sizeof(char *));
 
319
  /* Create argv and argc for new usplash*/
432
320
  {
433
 
    size_t position = 0;
434
 
    while(position < cmdline_len){
 
321
    ptrdiff_t position = 0;
 
322
    while((size_t)position < cmdline_len){
435
323
      char **tmp = realloc(cmdline_argv,
436
 
                           (sizeof(char *)
437
 
                            * (size_t)(cmdline_argc + 2)));
 
324
                           (sizeof(char *) * (size_t)(cmdline_argc + 2)));
438
325
      if(tmp == NULL){
439
326
        perror("realloc");
440
327
        free(cmdline_argv);
443
330
      cmdline_argv = tmp;
444
331
      cmdline_argv[cmdline_argc] = cmdline + position;
445
332
      cmdline_argc++;
446
 
      position += strlen(cmdline + position) + 1;
 
333
      position = (char *)rawmemchr(cmdline + position, '\0')
 
334
        - cmdline + 1;
447
335
    }
448
336
    cmdline_argv[cmdline_argc] = NULL;
449
337
  }
450
 
  /* Kill old usplash */
451
 
  kill(usplash_pid, SIGTERM);
452
 
  sleep(2);
453
 
  while(kill(usplash_pid, 0) == 0){
454
 
    kill(usplash_pid, SIGKILL);
455
 
    sleep(1);
456
 
  }
457
338
  pid_t new_usplash_pid = fork();
458
339
  if(new_usplash_pid == 0){
459
340
    /* Child; will become new usplash process */
460
 
    
461
 
    /* Make the effective user ID (root) the only user ID instead of
462
 
       the real user ID (mandos) */
463
 
    ret = setuid(geteuid());
464
 
    if(ret == -1){
465
 
      perror("setuid");
 
341
    while(kill(usplash_pid, 0)){
 
342
      sleep(2);
 
343
      kill(usplash_pid, SIGKILL);
 
344
      sleep(1);
466
345
    }
467
 
    
468
 
    setsid();
469
 
    ret = chdir("/");
470
 
/*     if(fork() != 0){ */
471
 
/*       _exit(EXIT_SUCCESS); */
472
 
/*     } */
473
346
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
474
347
    if(ret == -1){
475
348
      perror("dup2");
476
349
      _exit(EXIT_FAILURE);
477
350
    }
478
 
    
479
 
    execv(usplash_name, cmdline_argv);
480
 
    if(not interrupted_by_signal){
481
 
      perror("execv");
482
 
    }
483
 
    free(cmdline);
484
 
    free(cmdline_argv);
485
 
    _exit(EXIT_FAILURE);
486
 
  }
487
 
  free(cmdline);
488
 
  free(cmdline_argv);
489
 
  sleep(2);
490
 
  if(not usplash_write("PULSATE", NULL)
491
 
     and (errno != EINTR)){
492
 
    perror("usplash_write");
 
351
    execv("/sbin/usplash", cmdline_argv);
493
352
  }
494
353
  
495
354
  return EXIT_FAILURE;