/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/plymouth.c

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
2
 
/*
3
 
 * Plymouth - Read a password from Plymouth and output it
4
 
 * 
5
 
 * Copyright © 2010-2011 Teddy Hogeborn
6
 
 * Copyright © 2010-2011 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 <mandos@fukt.bsnet.se>.
23
 
 */
24
 
 
25
1
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
26
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
27
3
                                   sigemptyset(), sigaddset(), SIGINT,
30
6
#include <stdbool.h>            /* bool, false, true */
31
7
#include <fcntl.h>              /* open(), O_RDONLY */
32
8
#include <iso646.h>             /* and, or, not*/
33
 
#include <sys/types.h>          /* size_t, ssize_t, pid_t, struct
34
 
                                   dirent, waitpid() */
 
9
#include <sys/types.h>          /* size_t, ssize_t, pid_t, struct dirent,
 
10
                                   waitpid() */
35
11
#include <sys/wait.h>           /* waitpid() */
36
12
#include <stddef.h>             /* NULL */
37
13
#include <string.h>             /* strchr(), memcmp() */
38
 
#include <stdio.h>              /* asprintf(), perror(), fopen(),
39
 
                                   fscanf(), vasprintf(), fprintf(),
40
 
                                   vfprintf() */
41
 
#include <unistd.h>             /* close(), readlink(), read(),
42
 
                                   fork(), setsid(), chdir(), dup2(),
 
14
#include <stdio.h>              /* asprintf(), perror(), fopen(), fscanf() */
 
15
#include <unistd.h>             /* close(), readlink(), read(), fork()
 
16
                                   setsid(), chdir(), dup2()
43
17
                                   STDERR_FILENO, execv(), access() */
44
18
#include <stdlib.h>             /* free(), EXIT_FAILURE, realloc(),
45
19
                                   EXIT_SUCCESS, malloc(), _exit(),
47
21
#include <dirent.h>             /* scandir(), alphasort() */
48
22
#include <inttypes.h>           /* intmax_t, strtoumax(), SCNuMAX */
49
23
#include <sys/stat.h>           /* struct stat, lstat() */
50
 
#include <sysexits.h>           /* EX_OSERR, EX_UNAVAILABLE */
 
24
#include <sysexits.h>           /* EX_OSERR */
51
25
#include <error.h>              /* error() */
52
26
#include <errno.h>              /* TEMP_FAILURE_RETRY */
53
27
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
28
 
56
29
sig_atomic_t interrupted_by_signal = 0;
57
30
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
58
31
const char plymouth_path[] = "/bin/plymouth";
59
32
const char plymouthd_path[] = "/sbin/plymouthd";
60
 
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
61
 
                                        "--mode=boot",
 
33
const char *plymouthd_default_argv[] = {"/sbin/plymouthd", "--mode=boot",
62
34
                                        "--attach-to-session",
63
 
                                        "--pid-file="
64
 
                                        "/dev/.initramfs/"
65
 
                                        "plymouth.pid",
 
35
                                        "--pid-file=/dev/.initramfs/plymouth.pid",
66
36
                                        NULL };
67
37
 
68
38
static void termination_handler(__attribute__((unused))int signum){
72
42
  interrupted_by_signal = 1;
73
43
}
74
44
 
75
 
/* Function to use when printing errors */
76
 
void error_plus(int status, int errnum, const char *formatstring,
77
 
                ...){
78
 
  va_list ap;
79
 
  char *text;
80
 
  int ret;
81
 
  
82
 
  va_start(ap, formatstring);
83
 
  ret = vasprintf(&text, formatstring, ap);
84
 
  if (ret == -1){
85
 
    fprintf(stderr, "Mandos plugin %s: ",
86
 
            program_invocation_short_name);
87
 
    vfprintf(stderr, formatstring, ap);
88
 
    fprintf(stderr, ": ");
89
 
    fprintf(stderr, "%s\n", strerror(errnum));
90
 
    error(status, errno, "vasprintf while printing error");
91
 
    return;
92
 
  }
93
 
  fprintf(stderr, "Mandos plugin ");
94
 
  error(status, errnum, "%s", text);
95
 
  free(text);
96
 
}
97
 
 
98
45
/* Create prompt string */
99
46
char *makeprompt(void){
100
47
  int ret = 0;
101
48
  char *prompt;
102
49
  const char *const cryptsource = getenv("cryptsource");
103
50
  const char *const crypttarget = getenv("crypttarget");
104
 
  const char prompt_start[] = "Unlocking the disk";
105
 
  const char prompt_end[] = "Enter passphrase";
 
51
  const char prompt_start[] = "Enter passphrase to unlock the disk";
106
52
  
107
53
  if(cryptsource == NULL){
108
54
    if(crypttarget == NULL){
109
 
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
 
55
      ret = asprintf(&prompt, "%s: ", prompt_start);
110
56
    } else {
111
 
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
112
 
                     crypttarget, prompt_end);
 
57
      ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
58
                     crypttarget);
113
59
    }
114
60
  } else {
115
61
    if(crypttarget == NULL){
116
 
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
117
 
                     prompt_end);
 
62
      ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
118
63
    } else {
119
 
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
120
 
                     cryptsource, crypttarget, prompt_end);
 
64
      ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
65
                     cryptsource, crypttarget);
121
66
    }
122
67
  }
123
68
  if(ret == -1){
134
79
bool become_a_daemon(void){
135
80
  int ret = setuid(geteuid());
136
81
  if(ret == -1){
137
 
    error_plus(0, errno, "setuid");
 
82
    error(0, errno, "setuid");
138
83
  }
139
84
    
140
85
  setsid();
141
86
  ret = chdir("/");
142
87
  if(ret == -1){
143
 
    error_plus(0, errno, "chdir");
 
88
    error(0, errno, "chdir");
144
89
    return false;
145
90
  }
146
91
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
147
92
  if(ret == -1){
148
 
    error_plus(0, errno, "dup2");
 
93
    error(0, errno, "dup2");
149
94
    return false;
150
95
  }
151
96
  return true;
159
104
  pid_t pid;
160
105
  pid = fork();
161
106
  if(pid == -1){
162
 
    error_plus(0, errno, "fork");
 
107
    error(0, errno, "fork");
163
108
    return false;
164
109
  }
165
110
  if(pid == 0){
169
114
        _exit(EX_OSERR);
170
115
      }
171
116
    }
172
 
    
 
117
 
173
118
    char **new_argv = NULL;
174
 
    char **tmp;
 
119
    char *tmp;
175
120
    int i = 0;
176
 
    for (; argv[i]!=NULL; i++){
 
121
    for (; argv[i]!=(char *)NULL; i++){
177
122
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
178
123
      if (tmp == NULL){
179
 
        error_plus(0, errno, "realloc");
 
124
        error(0, errno, "realloc");
180
125
        free(new_argv);
181
 
        _exit(EX_OSERR);
 
126
        _exit(EXIT_FAILURE);
182
127
      }
183
 
      new_argv = tmp;
 
128
      new_argv = (char **)tmp;
184
129
      new_argv[i] = strdup(argv[i]);
185
130
    }
186
 
    new_argv[i] = NULL;
 
131
    new_argv[i] = (char *) NULL;
187
132
    
188
133
    execv(path, (char *const *)new_argv);
189
 
    error_plus(0, errno, "execv");
 
134
    error(0, errno, "execv");
190
135
    _exit(EXIT_FAILURE);
191
136
  }
192
137
  if(pid_return != NULL){
201
146
    return false;
202
147
  }
203
148
  if(ret == -1){
204
 
    error_plus(0, errno, "waitpid");
 
149
    error(0, errno, "waitpid");
205
150
    return false;
206
151
  }
207
 
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
 
152
  if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
208
153
    return true;
209
154
  }
210
155
  return false;
218
163
    errno = 0;
219
164
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
220
165
 
221
 
    if(errno != 0 or *tmp != '\0'
222
 
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
166
    if(errno != 0 or *tmp != '\0' or maxvalue != (uintmax_t)((pid_t)maxvalue)){
223
167
      return 0;
224
168
    }
225
169
  }
226
 
  char exe_target[sizeof(plymouthd_path)];
 
170
  char exe_target[sizeof(plymouth_path)];
227
171
  char *exe_link;
228
172
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
229
173
  if(ret == -1){
230
 
    error_plus(0, errno, "asprintf");
 
174
    error(0, errno, "asprintf");
231
175
    return 0;
232
176
  }
233
 
  
 
177
 
234
178
  struct stat exe_stat;
235
179
  ret = lstat(exe_link, &exe_stat);
236
180
  if(ret == -1){
237
181
    free(exe_link);
238
182
    if(errno != ENOENT){
239
 
      error_plus(0, errno, "lstat");
 
183
      error(0, errno, "lstat");
240
184
    }
241
185
    return 0;
242
186
  }
247
191
    free(exe_link);
248
192
    return 0;
249
193
  }
250
 
  
 
194
 
251
195
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
252
196
  free(exe_link);
253
 
  if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
254
 
      (memcmp(plymouthd_path, exe_target,
255
 
              sizeof(plymouthd_path)-1) != 0)){
 
197
  if((sret != (ssize_t)sizeof(plymouth_path)-1) or
 
198
      (memcmp(plymouth_path, exe_target,
 
199
              sizeof(plymouth_path)-1) != 0)){
256
200
    return 0;
257
201
  }
258
202
  return 1;
270
214
    fclose(pidfile);
271
215
  }
272
216
  if(maxvalue == 0){
273
 
    struct dirent **direntries = NULL;
 
217
    struct dirent **direntries;
274
218
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
275
 
    if (ret == -1){
276
 
      error_plus(0, errno, "scandir");
277
 
    }
278
 
    if (ret > 0){
279
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
280
 
      if (ret < 0){
281
 
        error_plus(0, errno, "sscanf");
282
 
      }
283
 
    }
284
 
    /* scandir might preallocate for this variable (man page unclear).
285
 
       even if ret == 0, therefore we need to free it. */
286
 
    free(direntries);
 
219
    sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
287
220
  }
288
221
  pid_t pid;
289
222
  pid = (pid_t)maxvalue;
303
236
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
304
237
                 (uintmax_t)pid);
305
238
  if(ret == -1){
306
 
    error_plus(0, errno, "asprintf");
 
239
    error(0, errno, "asprintf");
307
240
    return NULL;
308
241
  }
309
242
  
311
244
  cl_fd = open(cmdline_filename, O_RDONLY);
312
245
  free(cmdline_filename);
313
246
  if(cl_fd == -1){
314
 
    error_plus(0, errno, "open");
 
247
    error(0, errno, "open");
315
248
    return NULL;
316
249
  }
317
250
  
325
258
    if(cmdline_len + blocksize > cmdline_allocated){
326
259
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
327
260
      if(tmp == NULL){
328
 
        error_plus(0, errno, "realloc");
 
261
        error(0, errno, "realloc");
329
262
        free(cmdline);
330
263
        close(cl_fd);
331
264
        return NULL;
338
271
    sret = read(cl_fd, cmdline + cmdline_len,
339
272
                cmdline_allocated - cmdline_len);
340
273
    if(sret == -1){
341
 
      error_plus(0, errno, "read");
 
274
      error(0, errno, "read");
342
275
      free(cmdline);
343
276
      close(cl_fd);
344
277
      return NULL;
347
280
  } while(sret != 0);
348
281
  ret = close(cl_fd);
349
282
  if(ret == -1){
350
 
    error_plus(0, errno, "close");
 
283
    error(0, errno, "close");
351
284
    free(cmdline);
352
285
    return NULL;
353
286
  }
356
289
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
357
290
                       * sizeof(char *)); /* Get number of args */
358
291
  if(argv == NULL){
359
 
    error_plus(0, errno, "argv = malloc()");
 
292
    error(0, errno, "argv = malloc()");
360
293
    free(cmdline);
361
294
    return NULL;
362
295
  }
375
308
  /* test -x /bin/plymouth */
376
309
  ret = access(plymouth_path, X_OK);
377
310
  if(ret == -1){
378
 
    /* Plymouth is probably not installed.  Don't print an error
379
 
       message, just exit. */
380
 
    exit(EX_UNAVAILABLE);
 
311
    exit(EXIT_FAILURE);
381
312
  }
382
 
  
 
313
 
383
314
  { /* Add signal handlers */
384
315
    struct sigaction old_action,
385
316
      new_action = { .sa_handler = termination_handler,
386
317
                     .sa_flags = 0 };
387
318
    sigemptyset(&new_action.sa_mask);
388
 
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
389
 
        *sig != 0; sig++){
 
319
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 }; *sig != 0; sig++){
390
320
      ret = sigaddset(&new_action.sa_mask, *sig);
391
321
      if(ret == -1){
392
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
322
        error(0, errno, "sigaddset");
 
323
        exit(EX_OSERR);
393
324
      }
394
325
      ret = sigaction(*sig, NULL, &old_action);
395
326
      if(ret == -1){
396
 
        error_plus(EX_OSERR, errno, "sigaction");
 
327
        error(0, errno, "sigaction");
 
328
        exit(EX_OSERR);
397
329
      }
398
330
      if(old_action.sa_handler != SIG_IGN){
399
331
        ret = sigaction(*sig, &new_action, NULL);
400
332
        if(ret == -1){
401
 
          error_plus(EX_OSERR, errno, "sigaction");
 
333
          error(0, errno, "sigaction");
 
334
          exit(EX_OSERR);
402
335
        }
403
336
      }
404
337
    }
405
338
  }
406
 
  
 
339
    
407
340
  /* plymouth --ping */
408
341
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
409
 
                       (const char *[])
410
 
                       { plymouth_path, "--ping", NULL },
 
342
                       (const char *[]){ (const char *)plymouth_path, (const char *)"--ping", (const char *)NULL},
411
343
                       true, false);
412
344
  if(not bret){
413
345
    if(interrupted_by_signal){
414
346
      kill_and_wait(plymouth_command_pid);
415
 
      exit(EXIT_FAILURE);
416
347
    }
417
 
    /* Plymouth is probably not running.  Don't print an error
418
 
       message, just exit. */
419
 
    exit(EX_UNAVAILABLE);
 
348
    exit(EXIT_FAILURE);
420
349
  }
421
350
  
422
351
  prompt = makeprompt();
423
352
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
424
353
  free(prompt);
425
354
  if(ret == -1){
426
 
    error_plus(EX_OSERR, errno, "asprintf");
 
355
    error(0, errno, "asprintf");
 
356
    exit(EXIT_FAILURE);
427
357
  }
428
358
  
429
359
  /* plymouth ask-for-password --prompt="$prompt" */
430
 
  bret = exec_and_wait(&plymouth_command_pid,
431
 
                       plymouth_path, (const char *[])
432
 
                       { plymouth_path, "ask-for-password",
433
 
                           prompt_arg, NULL },
 
360
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
 
361
                       (const char *[]){plymouth_path, "ask-for-password", prompt_arg, NULL},
434
362
                       true, false);
435
363
  free(prompt_arg);
 
364
  if(not bret){
 
365
    if(interrupted_by_signal){
 
366
      kill_and_wait(plymouth_command_pid);
 
367
    } else {
 
368
      exit(EXIT_FAILURE);
 
369
    }
 
370
  }
 
371
  
436
372
  if(bret){
437
373
    exit(EXIT_SUCCESS);
438
374
  }
439
 
  if(not interrupted_by_signal){
440
 
    /* exec_and_wait failed for some other reason */
441
 
    exit(EXIT_FAILURE);
442
 
  }
443
 
  kill_and_wait(plymouth_command_pid);
444
375
  
445
 
  const char **plymouthd_argv;
 
376
  const char **plymouthd_argv = NULL;
446
377
  pid_t pid = get_pid();
447
378
  if(pid == 0){
448
 
    error_plus(0, 0, "plymouthd pid not found");
 
379
    error(0, 0, "plymouthd pid not found");
 
380
  } else {
 
381
    plymouthd_argv = getargv(pid);
 
382
  }
 
383
  if(plymouthd_argv == NULL){
449
384
    plymouthd_argv = plymouthd_default_argv;
450
 
  } else {
451
 
    plymouthd_argv = getargv(pid);
452
385
  }
453
386
  
454
 
  bret = exec_and_wait(NULL, plymouth_path, (const char *[])
455
 
                       { plymouth_path, "quit", NULL },
456
 
                       false, false);
457
 
  if(not bret){
458
 
    exit(EXIT_FAILURE);
459
 
  }
460
 
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv,
461
 
                       false, true);
462
 
  if(not bret){
463
 
    exit(EXIT_FAILURE);
464
 
  }
465
 
  exec_and_wait(NULL, plymouth_path, (const char *[])
466
 
                { plymouth_path, "show-splash", NULL },
467
 
                false, false);
 
387
  bret = exec_and_wait(NULL, plymouth_path,
 
388
                       (const char *[]){plymouth_path, "quit", NULL}, false, false);
 
389
  if(not bret){
 
390
    exit(EXIT_FAILURE);
 
391
  }
 
392
  bret = exec_and_wait(NULL, plymouthd_path, plymouthd_argv, false, true);
 
393
  if(not bret){
 
394
    exit(EXIT_FAILURE);
 
395
  }
 
396
  exec_and_wait(NULL, plymouth_path,
 
397
                (const char *[]){ plymouth_path, "show-splash", NULL }, false, false);
468
398
  exit(EXIT_FAILURE);
469
399
}