/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
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Plymouth - Read a password from Plymouth and output it
 
3
 * Usplash - Read a password from usplash and output it
4
4
 * 
5
 
 * Copyright © 2010-2011 Teddy Hogeborn
6
 
 * Copyright © 2010-2011 Björn Påhlsson
 
5
 * Copyright © 2010 Teddy Hogeborn
 
6
 * Copyright © 2010 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
36
36
#include <stddef.h>             /* NULL */
37
37
#include <string.h>             /* strchr(), memcmp() */
38
38
#include <stdio.h>              /* asprintf(), perror(), fopen(),
39
 
                                   fscanf(), vasprintf(), fprintf(),
40
 
                                   vfprintf() */
 
39
                                   fscanf() */
41
40
#include <unistd.h>             /* close(), readlink(), read(),
42
41
                                   fork(), setsid(), chdir(), dup2(),
43
42
                                   STDERR_FILENO, execv(), access() */
51
50
#include <error.h>              /* error() */
52
51
#include <errno.h>              /* TEMP_FAILURE_RETRY */
53
52
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
53
 
56
54
sig_atomic_t interrupted_by_signal = 0;
57
55
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
72
70
  interrupted_by_signal = 1;
73
71
}
74
72
 
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
73
/* Create prompt string */
99
74
char *makeprompt(void){
100
75
  int ret = 0;
101
76
  char *prompt;
102
77
  const char *const cryptsource = getenv("cryptsource");
103
78
  const char *const crypttarget = getenv("crypttarget");
104
 
  const char prompt_start[] = "Unlocking the disk";
105
 
  const char prompt_end[] = "Enter passphrase";
 
79
  const char prompt_start[] = "Enter passphrase to unlock the disk";
106
80
  
107
81
  if(cryptsource == NULL){
108
82
    if(crypttarget == NULL){
109
 
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
 
83
      ret = asprintf(&prompt, "%s: ", prompt_start);
110
84
    } else {
111
 
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
112
 
                     crypttarget, prompt_end);
 
85
      ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
86
                     crypttarget);
113
87
    }
114
88
  } else {
115
89
    if(crypttarget == NULL){
116
 
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
117
 
                     prompt_end);
 
90
      ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
118
91
    } else {
119
 
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
120
 
                     cryptsource, crypttarget, prompt_end);
 
92
      ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
93
                     cryptsource, crypttarget);
121
94
    }
122
95
  }
123
96
  if(ret == -1){
134
107
bool become_a_daemon(void){
135
108
  int ret = setuid(geteuid());
136
109
  if(ret == -1){
137
 
    error_plus(0, errno, "setuid");
 
110
    error(0, errno, "setuid");
138
111
  }
139
112
    
140
113
  setsid();
141
114
  ret = chdir("/");
142
115
  if(ret == -1){
143
 
    error_plus(0, errno, "chdir");
 
116
    error(0, errno, "chdir");
144
117
    return false;
145
118
  }
146
119
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
147
120
  if(ret == -1){
148
 
    error_plus(0, errno, "dup2");
 
121
    error(0, errno, "dup2");
149
122
    return false;
150
123
  }
151
124
  return true;
159
132
  pid_t pid;
160
133
  pid = fork();
161
134
  if(pid == -1){
162
 
    error_plus(0, errno, "fork");
 
135
    error(0, errno, "fork");
163
136
    return false;
164
137
  }
165
138
  if(pid == 0){
169
142
        _exit(EX_OSERR);
170
143
      }
171
144
    }
172
 
    
 
145
 
173
146
    char **new_argv = NULL;
174
 
    char **tmp;
 
147
    char *tmp;
175
148
    int i = 0;
176
 
    for (; argv[i]!=NULL; i++){
 
149
    for (; argv[i]!=(char *)NULL; i++){
177
150
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
178
151
      if (tmp == NULL){
179
 
        error_plus(0, errno, "realloc");
 
152
        error(0, errno, "realloc");
180
153
        free(new_argv);
181
154
        _exit(EX_OSERR);
182
155
      }
183
 
      new_argv = tmp;
 
156
      new_argv = (char **)tmp;
184
157
      new_argv[i] = strdup(argv[i]);
185
158
    }
186
 
    new_argv[i] = NULL;
 
159
    new_argv[i] = (char *) NULL;
187
160
    
188
161
    execv(path, (char *const *)new_argv);
189
 
    error_plus(0, errno, "execv");
 
162
    error(0, errno, "execv");
190
163
    _exit(EXIT_FAILURE);
191
164
  }
192
165
  if(pid_return != NULL){
201
174
    return false;
202
175
  }
203
176
  if(ret == -1){
204
 
    error_plus(0, errno, "waitpid");
 
177
    error(0, errno, "waitpid");
205
178
    return false;
206
179
  }
207
180
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
223
196
      return 0;
224
197
    }
225
198
  }
226
 
  char exe_target[sizeof(plymouthd_path)];
 
199
  char exe_target[sizeof(plymouth_path)];
227
200
  char *exe_link;
228
201
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
229
202
  if(ret == -1){
230
 
    error_plus(0, errno, "asprintf");
 
203
    error(0, errno, "asprintf");
231
204
    return 0;
232
205
  }
233
206
  
236
209
  if(ret == -1){
237
210
    free(exe_link);
238
211
    if(errno != ENOENT){
239
 
      error_plus(0, errno, "lstat");
 
212
      error(0, errno, "lstat");
240
213
    }
241
214
    return 0;
242
215
  }
250
223
  
251
224
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
252
225
  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)){
 
226
  if((sret != (ssize_t)sizeof(plymouth_path)-1) or
 
227
      (memcmp(plymouth_path, exe_target,
 
228
              sizeof(plymouth_path)-1) != 0)){
256
229
    return 0;
257
230
  }
258
231
  return 1;
270
243
    fclose(pidfile);
271
244
  }
272
245
  if(maxvalue == 0){
273
 
    struct dirent **direntries = NULL;
 
246
    struct dirent **direntries;
274
247
    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);
 
248
    sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
287
249
  }
288
250
  pid_t pid;
289
251
  pid = (pid_t)maxvalue;
303
265
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
304
266
                 (uintmax_t)pid);
305
267
  if(ret == -1){
306
 
    error_plus(0, errno, "asprintf");
 
268
    error(0, errno, "asprintf");
307
269
    return NULL;
308
270
  }
309
271
  
311
273
  cl_fd = open(cmdline_filename, O_RDONLY);
312
274
  free(cmdline_filename);
313
275
  if(cl_fd == -1){
314
 
    error_plus(0, errno, "open");
 
276
    error(0, errno, "open");
315
277
    return NULL;
316
278
  }
317
279
  
325
287
    if(cmdline_len + blocksize > cmdline_allocated){
326
288
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
327
289
      if(tmp == NULL){
328
 
        error_plus(0, errno, "realloc");
 
290
        error(0, errno, "realloc");
329
291
        free(cmdline);
330
292
        close(cl_fd);
331
293
        return NULL;
338
300
    sret = read(cl_fd, cmdline + cmdline_len,
339
301
                cmdline_allocated - cmdline_len);
340
302
    if(sret == -1){
341
 
      error_plus(0, errno, "read");
 
303
      error(0, errno, "read");
342
304
      free(cmdline);
343
305
      close(cl_fd);
344
306
      return NULL;
347
309
  } while(sret != 0);
348
310
  ret = close(cl_fd);
349
311
  if(ret == -1){
350
 
    error_plus(0, errno, "close");
 
312
    error(0, errno, "close");
351
313
    free(cmdline);
352
314
    return NULL;
353
315
  }
356
318
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
357
319
                       * sizeof(char *)); /* Get number of args */
358
320
  if(argv == NULL){
359
 
    error_plus(0, errno, "argv = malloc()");
 
321
    error(0, errno, "argv = malloc()");
360
322
    free(cmdline);
361
323
    return NULL;
362
324
  }
389
351
        *sig != 0; sig++){
390
352
      ret = sigaddset(&new_action.sa_mask, *sig);
391
353
      if(ret == -1){
392
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
354
        error(EX_OSERR, errno, "sigaddset");
393
355
      }
394
356
      ret = sigaction(*sig, NULL, &old_action);
395
357
      if(ret == -1){
396
 
        error_plus(EX_OSERR, errno, "sigaction");
 
358
        error(EX_OSERR, errno, "sigaction");
397
359
      }
398
360
      if(old_action.sa_handler != SIG_IGN){
399
361
        ret = sigaction(*sig, &new_action, NULL);
400
362
        if(ret == -1){
401
 
          error_plus(EX_OSERR, errno, "sigaction");
 
363
          error(EX_OSERR, errno, "sigaction");
402
364
        }
403
365
      }
404
366
    }
423
385
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
424
386
  free(prompt);
425
387
  if(ret == -1){
426
 
    error_plus(EX_OSERR, errno, "asprintf");
 
388
    error(EX_OSERR, errno, "asprintf");
427
389
  }
428
390
  
429
391
  /* plymouth ask-for-password --prompt="$prompt" */
445
407
  const char **plymouthd_argv;
446
408
  pid_t pid = get_pid();
447
409
  if(pid == 0){
448
 
    error_plus(0, 0, "plymouthd pid not found");
 
410
    error(0, 0, "plymouthd pid not found");
449
411
    plymouthd_argv = plymouthd_default_argv;
450
412
  } else {
451
413
    plymouthd_argv = getargv(pid);