/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:03:31 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080924230331-2qvhppypeo852ksr
* Makefile (PLUGINS): Added "plugins.d/usplash".
  (install-client-nokey): Add setuid bit on "plugins.d/usplash".

* plugins.d/splashy.c (main): Move "sret" to top.

* plugins.d/usplash: Removed.
* plugins.d/usplash.c: New.

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 <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
 
55
27
  interrupted_by_signal = 1;
56
28
}
57
29
 
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
30
int main(__attribute__((unused))int argc,
126
31
         __attribute__((unused))char **argv){
127
32
  int ret = 0;
133
38
  {
134
39
    const char *const cryptsource = getenv("cryptsource");
135
40
    const char *const crypttarget = getenv("crypttarget");
136
 
    const char prompt_start[] = "Enter passphrase to unlock the disk";
 
41
    const char *const prompt_start = "Enter passphrase to unlock the disk";
137
42
    
138
43
    if(cryptsource == NULL){
139
44
      if(crypttarget == NULL){
159
64
  pid_t usplash_pid = 0;
160
65
  char *cmdline = NULL;
161
66
  size_t cmdline_len = 0;
162
 
  const char usplash_name[] = "/sbin/usplash";
163
67
  {
 
68
    const char usplash_name[] = "/sbin/usplash";
164
69
    DIR *proc_dir = opendir("/proc");
165
70
    if(proc_dir == NULL){
166
71
      free(prompt);
179
84
         /proc/<pid>/exe link */
180
85
      char exe_target[sizeof(usplash_name)];
181
86
      {
182
 
        /* create file name string */
183
87
        char *exe_link;
184
88
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
185
89
        if(ret == -1){
188
92
          closedir(proc_dir);
189
93
          return EXIT_FAILURE;
190
94
        }
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
95
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
210
96
        free(exe_link);
211
 
        if(sret == -1){
212
 
          continue;
213
 
        }
214
97
      }
215
98
      if((sret == ((ssize_t)sizeof(exe_target)-1))
216
99
         and (memcmp(usplash_name, exe_target,
294
177
      free(prompt);
295
178
      return EXIT_FAILURE;
296
179
    }
297
 
    if(old_action.sa_handler != SIG_IGN){
 
180
    if (old_action.sa_handler != SIG_IGN){
298
181
      ret = sigaction(SIGINT, &new_action, NULL);
299
182
      if(ret == -1){
300
183
        perror("sigaction");
308
191
      free(prompt);
309
192
      return EXIT_FAILURE;
310
193
    }
311
 
    if(old_action.sa_handler != SIG_IGN){
 
194
    if (old_action.sa_handler != SIG_IGN){
312
195
      ret = sigaction(SIGHUP, &new_action, NULL);
313
196
      if(ret == -1){
314
197
        perror("sigaction");
322
205
      free(prompt);
323
206
      return EXIT_FAILURE;
324
207
    }
325
 
    if(old_action.sa_handler != SIG_IGN){
 
208
    if (old_action.sa_handler != SIG_IGN){
326
209
      ret = sigaction(SIGTERM, &new_action, NULL);
327
210
      if(ret == -1){
328
211
        perror("sigaction");
334
217
  
335
218
  /* Write command to FIFO */
336
219
  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);
 
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
  }
351
261
  
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){
 
262
  {
355
263
    char *buf = NULL;
356
264
    size_t buf_len = 0;
357
265
    
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
266
    /* 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;
 
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;
390
272
      }
 
273
      size_t buf_allocated = 0;
 
274
      const int blocksize = 1024;
391
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
        }
392
286
        sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
393
287
        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
 
          }
 
288
          perror("read");
 
289
          an_error_occured = true;
 
290
          break;
402
291
        }
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
 
    
 
292
        buf_len += (size_t)sret;
 
293
      }while(not interrupted_by_signal and sret != 0);
 
294
      close(fifo_fd);
 
295
    }
 
296
  
424
297
    /* Print password to stdout */
425
 
    size_t written = 0;
426
 
    while(written < buf_len){
 
298
    if(not interrupted_by_signal and not an_error_occured){
 
299
      size_t written = 0;
427
300
      do{
428
301
        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
 
          }
 
302
        if(sret == -1 and not interrupted_by_signal){
 
303
          perror("write");
 
304
          an_error_occured = true;
 
305
          break;
438
306
        }
439
 
      }while(sret == -1);
440
 
      if(interrupted_by_signal or an_error_occured){
441
 
        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;
442
311
      }
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*/
 
312
    }
 
313
  }
 
314
  
 
315
  kill(usplash_pid, SIGTERM);
 
316
  
456
317
  int cmdline_argc = 0;
457
318
  char **cmdline_argv = malloc(sizeof(char *));
 
319
  /* Create argv and argc for new usplash*/
458
320
  {
459
 
    size_t position = 0;
460
 
    while(position < cmdline_len){
 
321
    ptrdiff_t position = 0;
 
322
    while((size_t)position < cmdline_len){
461
323
      char **tmp = realloc(cmdline_argv,
462
 
                           (sizeof(char *)
463
 
                            * (size_t)(cmdline_argc + 2)));
 
324
                           (sizeof(char *) * (size_t)(cmdline_argc + 2)));
464
325
      if(tmp == NULL){
465
326
        perror("realloc");
466
327
        free(cmdline_argv);
469
330
      cmdline_argv = tmp;
470
331
      cmdline_argv[cmdline_argc] = cmdline + position;
471
332
      cmdline_argc++;
472
 
      position += strlen(cmdline + position) + 1;
 
333
      position = (char *)rawmemchr(cmdline + position, '\0')
 
334
        - cmdline + 1;
473
335
    }
474
336
    cmdline_argv[cmdline_argc] = NULL;
475
337
  }
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
338
  pid_t new_usplash_pid = fork();
484
339
  if(new_usplash_pid == 0){
485
340
    /* 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");
 
341
    while(kill(usplash_pid, 0)){
 
342
      sleep(2);
 
343
      kill(usplash_pid, SIGKILL);
 
344
      sleep(1);
492
345
    }
493
 
    
494
 
    setsid();
495
 
    ret = chdir("/");
496
 
/*     if(fork() != 0){ */
497
 
/*       _exit(EXIT_SUCCESS); */
498
 
/*     } */
499
346
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
500
347
    if(ret == -1){
501
348
      perror("dup2");
502
349
      _exit(EXIT_FAILURE);
503
350
    }
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");
 
351
    execv("/sbin/usplash", cmdline_argv);
519
352
  }
520
353
  
521
354
  return EXIT_FAILURE;