/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 04:54:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080926045435-0thnnqops1kzclag
* debian/mandos-client.postinst: Change home directory to
                                 "/nonexistent".
* debian/mandos.postinst: - '' -

* plugin-runner.c (main): Bug fix: Block signals while modifying
                          "plugin_list".

* plugins.d/usplash.c (usplash_write): New function.
  (main): Use "usplash_write" to write "INPUTQUIET" command.  Also
          write "TIMEOUT 0" before it, and write "TIMEOUT 15" and
          "PULSATE" if starting a new usplash process.  Kill old
          usplash before forking.  Bug fix: do setuid(geteuid()) to
          preserve genuine rootness.  Better interrupted/error logic
          overall.

* debian/mandos-client.lintian-overrides: Ignore setuid
                                          "plugins.d/usplash".

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
2
 
/*
3
 
 * Passprompt - 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 <https://www.fukt.bsnet.se/~belorn/> and
23
 
 * <https://www.fukt.bsnet.se/~teddy/>.
24
 
 */
25
 
 
26
1
#define _GNU_SOURCE             /* asprintf() */
27
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
28
 
                                   sigemptyset(), sigaddset(), SIGINT,
29
 
                                   SIGHUP, SIGTERM, sigaction(),
30
 
                                   SIG_IGN, kill(), SIGKILL */
31
 
#include <stdbool.h>            /* bool, false, true */
32
 
#include <fcntl.h>              /* open(), O_WRONLY, O_RDONLY */
33
 
#include <iso646.h>             /* and, or, not*/
34
 
#include <errno.h>              /* errno, EINTR */
35
 
#include <sys/types.h>          /* size_t, ssize_t, pid_t, DIR, struct
36
 
                                   dirent */
 
3
                                   sigemptyset(), sigaddset(),
 
4
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
 
5
                                   SIGTERM, kill(), SIGKILL */
37
6
#include <stddef.h>             /* NULL */
38
 
#include <string.h>             /* strlen(), memcmp() */
 
7
#include <stdlib.h>             /* getenv() */
39
8
#include <stdio.h>              /* asprintf(), perror() */
40
 
#include <unistd.h>             /* close(), write(), readlink(),
41
 
                                   read(), STDOUT_FILENO, sleep(),
42
 
                                   fork(), setuid(), geteuid(),
43
 
                                   setsid(), chdir(), dup2(),
44
 
                                   STDERR_FILENO, execv() */
45
 
#include <stdlib.h>             /* free(), EXIT_FAILURE, strtoul(),
46
 
                                   realloc(), EXIT_SUCCESS, malloc(),
47
 
                                   _exit() */
48
 
#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 */
49
13
#include <dirent.h>             /* opendir(), readdir(), closedir() */
50
 
#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 */
51
23
 
52
24
sig_atomic_t interrupted_by_signal = 0;
53
25
 
57
29
 
58
30
static bool usplash_write(const char *cmd, const char *arg){
59
31
  /* 
60
 
   * usplash_write("TIMEOUT", "15") will write "TIMEOUT 15\0"
61
 
   * usplash_write("PULSATE", NULL) will write "PULSATE\0"
 
32
   * usplash_write("TIMEOUT", "15"); -> "TIMEOUT 15\0"
 
33
   * usplash_write("PULSATE", NULL); -> "PULSATE\0"
62
34
   * SEE ALSO
63
35
   *         usplash_write(8)
64
36
   */
72
44
  }while(fifo_fd == -1);
73
45
  
74
46
  const char *cmd_line;
75
 
  size_t cmd_line_len;
76
47
  char *cmd_line_alloc = NULL;
77
48
  if(arg == NULL){
78
49
    cmd_line = cmd;
79
 
    cmd_line_len = strlen(cmd);
 
50
    ret = (int)strlen(cmd);
80
51
  }else{
81
52
    do{
82
53
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
88
59
      }
89
60
    }while(ret == -1);
90
61
    cmd_line = cmd_line_alloc;
91
 
    cmd_line_len = (size_t)ret + 1;
92
62
  }
93
63
  
 
64
  size_t cmd_line_len = (size_t)ret + 1;
94
65
  size_t written = 0;
95
 
  ssize_t sret = 0;
96
66
  while(not interrupted_by_signal and written < cmd_line_len){
97
 
    sret = write(fifo_fd, cmd_line + written,
98
 
                 cmd_line_len - written);
99
 
    if(sret == -1){
 
67
    ret = write(fifo_fd, cmd_line + written,
 
68
                cmd_line_len - written);
 
69
    if(ret == -1){
100
70
      if(errno != EINTR or interrupted_by_signal){
101
71
        int e = errno;
102
72
        close(fifo_fd);
107
77
        continue;
108
78
      }
109
79
    }
110
 
    written += (size_t)sret;
 
80
    written += (size_t)ret;
111
81
  }
112
82
  free(cmd_line_alloc);
113
83
  do{
133
103
  {
134
104
    const char *const cryptsource = getenv("cryptsource");
135
105
    const char *const crypttarget = getenv("crypttarget");
136
 
    const char prompt_start[] = "Enter passphrase to unlock the disk";
 
106
    const char *const prompt_start = "Enter passphrase to unlock the disk";
137
107
    
138
108
    if(cryptsource == NULL){
139
109
      if(crypttarget == NULL){
179
149
         /proc/<pid>/exe link */
180
150
      char exe_target[sizeof(usplash_name)];
181
151
      {
182
 
        /* create file name string */
183
152
        char *exe_link;
184
153
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
154
        if(ret == -1){
188
157
          closedir(proc_dir);
189
158
          return EXIT_FAILURE;
190
159
        }
191
 
        
192
 
        /* Check that it refers to a symlink owned by root:root */
193
 
        struct stat exe_stat;
194
 
        ret = lstat(exe_link, &exe_stat);
195
 
        if(ret == -1){
196
 
          perror("lstat");
197
 
          free(exe_link);
198
 
          free(prompt);
199
 
          closedir(proc_dir);
200
 
          return EXIT_FAILURE;
201
 
        }
202
 
        if(not S_ISLNK(exe_stat.st_mode)
203
 
           or exe_stat.st_uid != 0
204
 
           or exe_stat.st_gid != 0){
205
 
          free(exe_link);
206
 
          continue;
207
 
        }
208
 
        
209
160
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
210
161
        free(exe_link);
211
 
        if(sret == -1){
212
 
          continue;
213
 
        }
214
162
      }
215
163
      if((sret == ((ssize_t)sizeof(exe_target)-1))
216
164
         and (memcmp(usplash_name, exe_target,
294
242
      free(prompt);
295
243
      return EXIT_FAILURE;
296
244
    }
297
 
    if(old_action.sa_handler != SIG_IGN){
 
245
    if (old_action.sa_handler != SIG_IGN){
298
246
      ret = sigaction(SIGINT, &new_action, NULL);
299
247
      if(ret == -1){
300
248
        perror("sigaction");
308
256
      free(prompt);
309
257
      return EXIT_FAILURE;
310
258
    }
311
 
    if(old_action.sa_handler != SIG_IGN){
 
259
    if (old_action.sa_handler != SIG_IGN){
312
260
      ret = sigaction(SIGHUP, &new_action, NULL);
313
261
      if(ret == -1){
314
262
        perror("sigaction");
322
270
      free(prompt);
323
271
      return EXIT_FAILURE;
324
272
    }
325
 
    if(old_action.sa_handler != SIG_IGN){
 
273
    if (old_action.sa_handler != SIG_IGN){
326
274
      ret = sigaction(SIGTERM, &new_action, NULL);
327
275
      if(ret == -1){
328
276
        perror("sigaction");
423
371
    
424
372
    /* Print password to stdout */
425
373
    size_t written = 0;
426
 
    while(written < buf_len){
 
374
    do{
427
375
      do{
428
376
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
429
377
        if(sret == -1){
440
388
      if(interrupted_by_signal or an_error_occured){
441
389
        break;
442
390
      }
 
391
      
443
392
      written += (size_t)sret;
444
 
    }
445
 
    free(buf);
 
393
    }while(written < buf_len);
446
394
    if(not interrupted_by_signal and not an_error_occured){
447
 
      free(cmdline);
448
395
      return EXIT_SUCCESS;
449
396
    }
450
397
    break;                      /* Big */
451
 
  }                             /* end of non-loop while() */
 
398
  }
452
399
  
453
400
  /* If we got here, an error or interrupt must have happened */
454
401
  
455
 
  /* Create argc and argv for new usplash*/
456
402
  int cmdline_argc = 0;
457
403
  char **cmdline_argv = malloc(sizeof(char *));
 
404
  /* Create argv and argc for new usplash*/
458
405
  {
459
406
    size_t position = 0;
460
407
    while(position < cmdline_len){
461
408
      char **tmp = realloc(cmdline_argv,
462
 
                           (sizeof(char *)
463
 
                            * (size_t)(cmdline_argc + 2)));
 
409
                           (sizeof(char *) * (size_t)(cmdline_argc + 2)));
464
410
      if(tmp == NULL){
465
411
        perror("realloc");
466
412
        free(cmdline_argv);
474
420
    cmdline_argv[cmdline_argc] = NULL;
475
421
  }
476
422
  /* Kill old usplash */
477
 
  kill(usplash_pid, SIGTERM);
478
 
  sleep(2);
 
423
    kill(usplash_pid, SIGTERM);
 
424
    sleep(2);
479
425
  while(kill(usplash_pid, 0) == 0){
480
426
    kill(usplash_pid, SIGKILL);
481
427
    sleep(1);
487
433
    /* Make the effective user ID (root) the only user ID instead of
488
434
       the real user ID (mandos) */
489
435
    ret = setuid(geteuid());
490
 
    if(ret == -1){
 
436
    if (ret == -1){
491
437
      perror("setuid");
492
438
    }
493
439
    
503
449
    }
504
450
    
505
451
    execv(usplash_name, cmdline_argv);
506
 
    if(not interrupted_by_signal){
507
 
      perror("execv");
508
 
    }
509
 
    free(cmdline);
510
 
    free(cmdline_argv);
511
 
    _exit(EXIT_FAILURE);
512
452
  }
513
 
  free(cmdline);
514
 
  free(cmdline_argv);
515
453
  sleep(2);
516
454
  if(not usplash_write("PULSATE", NULL)
517
455
     and (errno != EINTR)){