/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-07 01:20:43 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080907012043-w05a7qh6258eqh27
* Makefile: Clearly separate "local" install variable settings from
            "package" variable settings.
  (INITRAMFSTOOLS): New variable.
  (install-server): Use $(DESTDIR) to install init script and
                    /etc/default file.
  (install-client): Use $(INITRAMFSTOOLS).
  (uninstall-client): - '' -
  (purge-server): Use $(DESTDIR).

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
 
#define _GNU_SOURCE             /* asprintf() */
27
 
#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 */
37
 
#include <stddef.h>             /* NULL */
38
 
#include <string.h>             /* strlen(), memcmp() */
39
 
#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() */
49
 
#include <dirent.h>             /* opendir(), readdir(), closedir() */
50
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
51
 
 
52
 
sig_atomic_t interrupted_by_signal = 0;
53
 
 
54
 
static void termination_handler(__attribute__((unused))int signum){
55
 
  interrupted_by_signal = 1;
56
 
}
57
 
 
58
 
static bool usplash_write(const char *cmd, const char *arg){
59
 
  /* 
60
 
   * usplash_write("TIMEOUT", "15") will write "TIMEOUT 15\0"
61
 
   * usplash_write("PULSATE", NULL) will write "PULSATE\0"
62
 
   * SEE ALSO
63
 
   *         usplash_write(8)
64
 
   */
65
 
  int ret;
66
 
  int fifo_fd;
67
 
  do{
68
 
    fifo_fd = open("/dev/.initramfs/usplash_fifo", O_WRONLY);
69
 
    if(fifo_fd == -1 and (errno != EINTR or interrupted_by_signal)){
70
 
      return false;
71
 
    }
72
 
  }while(fifo_fd == -1);
73
 
  
74
 
  const char *cmd_line;
75
 
  size_t cmd_line_len;
76
 
  char *cmd_line_alloc = NULL;
77
 
  if(arg == NULL){
78
 
    cmd_line = cmd;
79
 
    cmd_line_len = strlen(cmd);
80
 
  }else{
81
 
    do{
82
 
      ret = asprintf(&cmd_line_alloc, "%s %s", cmd, arg);
83
 
      if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
84
 
        int e = errno;
85
 
        close(fifo_fd);
86
 
        errno = e;
87
 
        return false;
88
 
      }
89
 
    }while(ret == -1);
90
 
    cmd_line = cmd_line_alloc;
91
 
    cmd_line_len = (size_t)ret + 1;
92
 
  }
93
 
  
94
 
  size_t written = 0;
95
 
  ssize_t sret = 0;
96
 
  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){
100
 
      if(errno != EINTR or interrupted_by_signal){
101
 
        int e = errno;
102
 
        close(fifo_fd);
103
 
        free(cmd_line_alloc);
104
 
        errno = e;
105
 
        return false;
106
 
      } else {
107
 
        continue;
108
 
      }
109
 
    }
110
 
    written += (size_t)sret;
111
 
  }
112
 
  free(cmd_line_alloc);
113
 
  do{
114
 
    ret = close(fifo_fd);
115
 
    if(ret == -1 and (errno != EINTR or interrupted_by_signal)){
116
 
      return false;
117
 
    }
118
 
  }while(ret == -1);
119
 
  if(interrupted_by_signal){
120
 
    return false;
121
 
  }
122
 
  return true;
123
 
}
124
 
 
125
 
int main(__attribute__((unused))int argc,
126
 
         __attribute__((unused))char **argv){
127
 
  int ret = 0;
128
 
  ssize_t sret;
129
 
  bool an_error_occured = false;
130
 
  
131
 
  /* Create prompt string */
132
 
  char *prompt = NULL;
133
 
  {
134
 
    const char *const cryptsource = getenv("cryptsource");
135
 
    const char *const crypttarget = getenv("crypttarget");
136
 
    const char prompt_start[] = "Enter passphrase to unlock the disk";
137
 
    
138
 
    if(cryptsource == NULL){
139
 
      if(crypttarget == NULL){
140
 
        ret = asprintf(&prompt, "%s: ", prompt_start);
141
 
      } else {
142
 
        ret = asprintf(&prompt, "%s (%s): ", prompt_start,
143
 
                       crypttarget);
144
 
      }
145
 
    } else {
146
 
      if(crypttarget == NULL){
147
 
        ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
148
 
      } else {
149
 
        ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
150
 
                       cryptsource, crypttarget);
151
 
      }
152
 
    }
153
 
    if(ret == -1){
154
 
      return EXIT_FAILURE;
155
 
    }
156
 
  }
157
 
  
158
 
  /* Find usplash process */
159
 
  pid_t usplash_pid = 0;
160
 
  char *cmdline = NULL;
161
 
  size_t cmdline_len = 0;
162
 
  const char usplash_name[] = "/sbin/usplash";
163
 
  {
164
 
    DIR *proc_dir = opendir("/proc");
165
 
    if(proc_dir == NULL){
166
 
      free(prompt);
167
 
      perror("opendir");
168
 
      return EXIT_FAILURE;
169
 
    }
170
 
    for(struct dirent *proc_ent = readdir(proc_dir);
171
 
        proc_ent != NULL;
172
 
        proc_ent = readdir(proc_dir)){
173
 
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
174
 
      if(pid == 0){
175
 
        /* Not a process */
176
 
        continue;
177
 
      }
178
 
      /* Find the executable name by doing readlink() on the
179
 
         /proc/<pid>/exe link */
180
 
      char exe_target[sizeof(usplash_name)];
181
 
      {
182
 
        /* create file name string */
183
 
        char *exe_link;
184
 
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
 
        if(ret == -1){
186
 
          perror("asprintf");
187
 
          free(prompt);
188
 
          closedir(proc_dir);
189
 
          return EXIT_FAILURE;
190
 
        }
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
 
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
210
 
        free(exe_link);
211
 
        if(sret == -1){
212
 
          continue;
213
 
        }
214
 
      }
215
 
      if((sret == ((ssize_t)sizeof(exe_target)-1))
216
 
         and (memcmp(usplash_name, exe_target,
217
 
                     sizeof(exe_target)-1) == 0)){
218
 
        usplash_pid = pid;
219
 
        /* Read and save the command line of usplash in "cmdline" */
220
 
        {
221
 
          /* Open /proc/<pid>/cmdline  */
222
 
          int cl_fd;
223
 
          {
224
 
            char *cmdline_filename;
225
 
            ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
226
 
                           proc_ent->d_name);
227
 
            if(ret == -1){
228
 
              perror("asprintf");
229
 
              free(prompt);
230
 
              closedir(proc_dir);
231
 
              return EXIT_FAILURE;
232
 
            }
233
 
            cl_fd = open(cmdline_filename, O_RDONLY);
234
 
            if(cl_fd == -1){
235
 
              perror("open");
236
 
              free(cmdline_filename);
237
 
              free(prompt);
238
 
              closedir(proc_dir);
239
 
              return EXIT_FAILURE;
240
 
            }
241
 
            free(cmdline_filename);
242
 
          }
243
 
          size_t cmdline_allocated = 0;
244
 
          char *tmp;
245
 
          const size_t blocksize = 1024;
246
 
          do{
247
 
            if(cmdline_len + blocksize > cmdline_allocated){
248
 
              tmp = realloc(cmdline, cmdline_allocated + blocksize);
249
 
              if(tmp == NULL){
250
 
                perror("realloc");
251
 
                free(cmdline);
252
 
                free(prompt);
253
 
                closedir(proc_dir);
254
 
                return EXIT_FAILURE;
255
 
              }
256
 
              cmdline = tmp;
257
 
              cmdline_allocated += blocksize;
258
 
            }
259
 
            sret = read(cl_fd, cmdline + cmdline_len,
260
 
                        cmdline_allocated - cmdline_len);
261
 
            if(sret == -1){
262
 
              perror("read");
263
 
              free(cmdline);
264
 
              free(prompt);
265
 
              closedir(proc_dir);
266
 
              return EXIT_FAILURE;
267
 
            }
268
 
            cmdline_len += (size_t)sret;
269
 
          } while(sret != 0);
270
 
          close(cl_fd);
271
 
        }
272
 
        break;
273
 
      }
274
 
    }
275
 
    closedir(proc_dir);
276
 
  }
277
 
  if(usplash_pid == 0){
278
 
    free(prompt);
279
 
    return EXIT_FAILURE;
280
 
  }
281
 
  
282
 
  /* Set up the signal handler */
283
 
  {
284
 
    struct sigaction old_action,
285
 
      new_action = { .sa_handler = termination_handler,
286
 
                     .sa_flags = 0 };
287
 
    sigemptyset(&new_action.sa_mask);
288
 
    sigaddset(&new_action.sa_mask, SIGINT);
289
 
    sigaddset(&new_action.sa_mask, SIGHUP);
290
 
    sigaddset(&new_action.sa_mask, SIGTERM);
291
 
    ret = sigaction(SIGINT, NULL, &old_action);
292
 
    if(ret == -1){
293
 
      perror("sigaction");
294
 
      free(prompt);
295
 
      return EXIT_FAILURE;
296
 
    }
297
 
    if(old_action.sa_handler != SIG_IGN){
298
 
      ret = sigaction(SIGINT, &new_action, NULL);
299
 
      if(ret == -1){
300
 
        perror("sigaction");
301
 
        free(prompt);
302
 
        return EXIT_FAILURE;
303
 
      }
304
 
    }
305
 
    ret = sigaction(SIGHUP, NULL, &old_action);
306
 
    if(ret == -1){
307
 
      perror("sigaction");
308
 
      free(prompt);
309
 
      return EXIT_FAILURE;
310
 
    }
311
 
    if(old_action.sa_handler != SIG_IGN){
312
 
      ret = sigaction(SIGHUP, &new_action, NULL);
313
 
      if(ret == -1){
314
 
        perror("sigaction");
315
 
        free(prompt);
316
 
        return EXIT_FAILURE;
317
 
      }
318
 
    }
319
 
    ret = sigaction(SIGTERM, NULL, &old_action);
320
 
    if(ret == -1){
321
 
      perror("sigaction");
322
 
      free(prompt);
323
 
      return EXIT_FAILURE;
324
 
    }
325
 
    if(old_action.sa_handler != SIG_IGN){
326
 
      ret = sigaction(SIGTERM, &new_action, NULL);
327
 
      if(ret == -1){
328
 
        perror("sigaction");
329
 
        free(prompt);
330
 
        return EXIT_FAILURE;
331
 
      }
332
 
    }
333
 
  }
334
 
  
335
 
  /* Write command to FIFO */
336
 
  if(not interrupted_by_signal){
337
 
    if(not usplash_write("TIMEOUT", "0")
338
 
       and (errno != EINTR)){
339
 
      perror("usplash_write");
340
 
      an_error_occured = true;
341
 
    }
342
 
  }
343
 
  if(not interrupted_by_signal and not an_error_occured){
344
 
    if(not usplash_write("INPUTQUIET", prompt)
345
 
       and (errno != EINTR)){
346
 
      perror("usplash_write");
347
 
      an_error_occured = true;
348
 
    }
349
 
  }
350
 
  free(prompt);
351
 
  
352
 
  /* This is not really a loop; while() is used to be able to "break"
353
 
     out of it; those breaks are marked "Big" */
354
 
  while(not interrupted_by_signal and not an_error_occured){
355
 
    char *buf = NULL;
356
 
    size_t buf_len = 0;
357
 
    
358
 
    /* Open FIFO */
359
 
    int fifo_fd;
360
 
    do{
361
 
      fifo_fd = open("/dev/.initramfs/usplash_outfifo", O_RDONLY);
362
 
      if(fifo_fd == -1){
363
 
        if(errno != EINTR){
364
 
          perror("open");
365
 
          an_error_occured = true;
366
 
          break;
367
 
        }
368
 
        if(interrupted_by_signal){
369
 
          break;
370
 
        }
371
 
      }
372
 
    }while(fifo_fd == -1);
373
 
    if(interrupted_by_signal or an_error_occured){
374
 
      break;                    /* Big */
375
 
    }
376
 
    
377
 
    /* Read from FIFO */
378
 
    size_t buf_allocated = 0;
379
 
    const size_t blocksize = 1024;
380
 
    do{
381
 
      if(buf_len + blocksize > buf_allocated){
382
 
        char *tmp = realloc(buf, buf_allocated + blocksize);
383
 
        if(tmp == NULL){
384
 
          perror("realloc");
385
 
          an_error_occured = true;
386
 
          break;
387
 
        }
388
 
        buf = tmp;
389
 
        buf_allocated += blocksize;
390
 
      }
391
 
      do{
392
 
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
393
 
        if(sret == -1){
394
 
          if(errno != EINTR){
395
 
            perror("read");
396
 
            an_error_occured = true;
397
 
            break;
398
 
          }
399
 
          if(interrupted_by_signal){
400
 
            break;
401
 
          }
402
 
        }
403
 
      }while(sret == -1);
404
 
      if(interrupted_by_signal or an_error_occured){
405
 
        break;
406
 
      }
407
 
      
408
 
      buf_len += (size_t)sret;
409
 
    }while(sret != 0);
410
 
    close(fifo_fd);
411
 
    if(interrupted_by_signal or an_error_occured){
412
 
      break;                    /* Big */
413
 
    }
414
 
    
415
 
    if(not usplash_write("TIMEOUT", "15")
416
 
       and (errno != EINTR)){
417
 
        perror("usplash_write");
418
 
        an_error_occured = true;
419
 
    }
420
 
    if(interrupted_by_signal or an_error_occured){
421
 
      break;                    /* Big */
422
 
    }
423
 
    
424
 
    /* Print password to stdout */
425
 
    size_t written = 0;
426
 
    while(written < buf_len){
427
 
      do{
428
 
        sret = write(STDOUT_FILENO, buf + written, buf_len - written);
429
 
        if(sret == -1){
430
 
          if(errno != EINTR){
431
 
            perror("write");
432
 
            an_error_occured = true;
433
 
            break;
434
 
          }
435
 
          if(interrupted_by_signal){
436
 
            break;
437
 
          }
438
 
        }
439
 
      }while(sret == -1);
440
 
      if(interrupted_by_signal or an_error_occured){
441
 
        break;
442
 
      }
443
 
      written += (size_t)sret;
444
 
    }
445
 
    free(buf);
446
 
    if(not interrupted_by_signal and not an_error_occured){
447
 
      free(cmdline);
448
 
      return EXIT_SUCCESS;
449
 
    }
450
 
    break;                      /* Big */
451
 
  }                             /* end of non-loop while() */
452
 
  
453
 
  /* If we got here, an error or interrupt must have happened */
454
 
  
455
 
  /* Create argc and argv for new usplash*/
456
 
  int cmdline_argc = 0;
457
 
  char **cmdline_argv = malloc(sizeof(char *));
458
 
  {
459
 
    size_t position = 0;
460
 
    while(position < cmdline_len){
461
 
      char **tmp = realloc(cmdline_argv,
462
 
                           (sizeof(char *)
463
 
                            * (size_t)(cmdline_argc + 2)));
464
 
      if(tmp == NULL){
465
 
        perror("realloc");
466
 
        free(cmdline_argv);
467
 
        return EXIT_FAILURE;
468
 
      }
469
 
      cmdline_argv = tmp;
470
 
      cmdline_argv[cmdline_argc] = cmdline + position;
471
 
      cmdline_argc++;
472
 
      position += strlen(cmdline + position) + 1;
473
 
    }
474
 
    cmdline_argv[cmdline_argc] = NULL;
475
 
  }
476
 
  /* Kill old usplash */
477
 
  kill(usplash_pid, SIGTERM);
478
 
  sleep(2);
479
 
  while(kill(usplash_pid, 0) == 0){
480
 
    kill(usplash_pid, SIGKILL);
481
 
    sleep(1);
482
 
  }
483
 
  pid_t new_usplash_pid = fork();
484
 
  if(new_usplash_pid == 0){
485
 
    /* Child; will become new usplash process */
486
 
    
487
 
    /* Make the effective user ID (root) the only user ID instead of
488
 
       the real user ID (mandos) */
489
 
    ret = setuid(geteuid());
490
 
    if(ret == -1){
491
 
      perror("setuid");
492
 
    }
493
 
    
494
 
    setsid();
495
 
    ret = chdir("/");
496
 
/*     if(fork() != 0){ */
497
 
/*       _exit(EXIT_SUCCESS); */
498
 
/*     } */
499
 
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
500
 
    if(ret == -1){
501
 
      perror("dup2");
502
 
      _exit(EXIT_FAILURE);
503
 
    }
504
 
    
505
 
    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
 
  }
513
 
  free(cmdline);
514
 
  free(cmdline_argv);
515
 
  sleep(2);
516
 
  if(not usplash_write("PULSATE", NULL)
517
 
     and (errno != EINTR)){
518
 
    perror("usplash_write");
519
 
  }
520
 
  
521
 
  return EXIT_FAILURE;
522
 
}