/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/plymouth.c

  • Committer: Björn Påhlsson
  • Date: 2011-07-25 18:46:19 UTC
  • mfrom: (237.7.35 trunk)
  • mto: (237.7.37 trunk)
  • mto: This revision was merged to the branch mainline in revision 284.
  • Revision ID: belorn@fukt.bsnet.se-20110725184619-l7vfsobr0bh9fc3l
merge

Show diffs side-by-side

added added

removed removed

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