/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-26 18:03:10 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080926180310-6npcdb9v7sl60zlf
* plugins.d/usplash.c (usplash_write): Move "cmd_line_len" up.
  (main): Bug fix: Check result of "readlink".  Bug fix: do not write
          if "buf" is empty.  Bug fix:  Free "buf".  Bug fix: Free
          "cmdline".  Bug fix: Check result of "execv".  Bug fix: free
          "cmdline_argv".

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(),
4
 
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
5
 
                                   SIGTERM, kill(), SIGKILL */
 
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 <errno.h>              /* errno, EINTR */
 
9
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR,
 
10
                                   struct dirent */
6
11
#include <stddef.h>             /* NULL */
 
12
#include <string.h>             /* strlen(), memcmp() */
 
13
#include <stdio.h>              /* asprintf(), perror() */
 
14
#include <unistd.h>             /* close(), write(), readlink(),
 
15
                                   read(), STDOUT_FILENO, sleep(),
 
16
                                   fork(), setuid(), geteuid(),
 
17
                                   setsid(), chdir(), dup2(),
 
18
                                   STDERR_FILENO, execv() */
 
19
#include <stdlib.h>             /* free(), EXIT_FAILURE, strtoul(),
 
20
                                   realloc(), EXIT_SUCCESS, malloc(),
 
21
                                   _exit() */
7
22
#include <stdlib.h>             /* getenv() */
8
 
#include <stdio.h>              /* asprintf(), perror() */
9
 
#include <stdlib.h>             /* EXIT_FAILURE, EXIT_SUCCESS,
10
 
                                   strtoul(), free() */
11
 
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
12
 
                                   ssize_t */
13
23
#include <dirent.h>             /* opendir(), readdir(), closedir() */
14
 
#include <unistd.h>             /* readlink(), fork(), execl(),
15
 
                                   _exit */
16
 
#include <string.h>             /* memcmp() */
 
24
 
 
25
 
 
26
 
17
27
#include <iso646.h>             /* and */
18
 
#include <stdbool.h>            /* bool, false, true */
19
 
#include <errno.h>              /* errno */
20
28
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
21
29
                                   WEXITSTATUS() */
22
 
#include <fcntl.h>              /* open(), O_RDONLY */
23
30
 
24
31
sig_atomic_t interrupted_by_signal = 0;
25
32
 
44
51
  }while(fifo_fd == -1);
45
52
  
46
53
  const char *cmd_line;
 
54
  size_t cmd_line_len;
47
55
  char *cmd_line_alloc = NULL;
48
56
  if(arg == NULL){
49
57
    cmd_line = cmd;
50
 
    ret = (int)strlen(cmd);
 
58
    cmd_line_len = strlen(cmd);
51
59
  }else{
52
60
    do{
53
61
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
59
67
      }
60
68
    }while(ret == -1);
61
69
    cmd_line = cmd_line_alloc;
 
70
    cmd_line_len = (size_t)ret + 1;
62
71
  }
63
72
  
64
 
  size_t cmd_line_len = (size_t)ret + 1;
65
73
  size_t written = 0;
66
74
  while(not interrupted_by_signal and written < cmd_line_len){
67
75
    ret = write(fifo_fd, cmd_line + written,
103
111
  {
104
112
    const char *const cryptsource = getenv("cryptsource");
105
113
    const char *const crypttarget = getenv("crypttarget");
106
 
    const char *const prompt_start = "Enter passphrase to unlock the disk";
 
114
    const char prompt_start[] = "Enter passphrase to unlock the disk";
107
115
    
108
116
    if(cryptsource == NULL){
109
117
      if(crypttarget == NULL){
159
167
        }
160
168
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
161
169
        free(exe_link);
 
170
        if(sret == -1){
 
171
          continue;
 
172
        }
162
173
      }
163
174
      if((sret == ((ssize_t)sizeof(exe_target)-1))
164
175
         and (memcmp(usplash_name, exe_target,
371
382
    
372
383
    /* Print password to stdout */
373
384
    size_t written = 0;
374
 
    do{
 
385
    while(written < buf_len){
375
386
      do{
376
387
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
377
388
        if(sret == -1){
388
399
      if(interrupted_by_signal or an_error_occured){
389
400
        break;
390
401
      }
391
 
      
392
402
      written += (size_t)sret;
393
 
    }while(written < buf_len);
 
403
    }
 
404
    free(buf);
394
405
    if(not interrupted_by_signal and not an_error_occured){
 
406
      free(cmdline);
395
407
      return EXIT_SUCCESS;
396
408
    }
397
409
    break;                      /* Big */
406
418
    size_t position = 0;
407
419
    while(position < cmdline_len){
408
420
      char **tmp = realloc(cmdline_argv,
409
 
                           (sizeof(char *) * (size_t)(cmdline_argc + 2)));
 
421
                           (sizeof(char *)
 
422
                            * (size_t)(cmdline_argc + 2)));
410
423
      if(tmp == NULL){
411
424
        perror("realloc");
412
425
        free(cmdline_argv);
418
431
      position += strlen(cmdline + position) + 1;
419
432
    }
420
433
    cmdline_argv[cmdline_argc] = NULL;
 
434
    free(cmdline);
421
435
  }
422
436
  /* Kill old usplash */
423
437
    kill(usplash_pid, SIGTERM);
449
463
    }
450
464
    
451
465
    execv(usplash_name, cmdline_argv);
 
466
    perror("execv");
 
467
    free(cmdline_argv);
 
468
    _exit(EXIT_FAILURE);
452
469
  }
 
470
  free(cmdline_argv);
453
471
  sleep(2);
454
472
  if(not usplash_write("PULSATE", NULL)
455
473
     and (errno != EINTR)){