/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

  • Committer: Teddy Hogeborn
  • Date: 2010-10-02 17:41:05 UTC
  • mto: (24.1.169 mandos)
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: teddy@fukt.bsnet.se-20101002174105-xfqa0j8y1puvedo3
* debian/source/format: New; contains "3.0 (quilt)".

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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
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
 
 
58
 
/* Used by Ubuntu 11.04 (Natty Narwahl) */
59
 
const char plymouth_old_pid[] = "/dev/.initramfs/plymouth.pid";
60
 
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
61
 
const char plymouth_pid[] = "/run/initramfs/plymouth.pid";
62
 
 
 
55
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
63
56
const char plymouth_path[] = "/bin/plymouth";
64
57
const char plymouthd_path[] = "/sbin/plymouthd";
65
58
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
66
59
                                        "--mode=boot",
67
60
                                        "--attach-to-session",
 
61
                                        "--pid-file="
 
62
                                        "/dev/.initramfs/"
 
63
                                        "plymouth.pid",
68
64
                                        NULL };
69
65
 
70
66
static void termination_handler(__attribute__((unused))int signum){
74
70
  interrupted_by_signal = 1;
75
71
}
76
72
 
77
 
/* Function to use when printing errors */
78
 
__attribute__((format (gnu_printf, 3, 4)))
79
 
void error_plus(int status, int errnum, const char *formatstring,
80
 
                ...){
81
 
  va_list ap;
82
 
  char *text;
83
 
  int ret;
84
 
  
85
 
  va_start(ap, formatstring);
86
 
  ret = vasprintf(&text, formatstring, ap);
87
 
  if (ret == -1){
88
 
    fprintf(stderr, "Mandos plugin %s: ",
89
 
            program_invocation_short_name);
90
 
    vfprintf(stderr, formatstring, ap);
91
 
    fprintf(stderr, ": ");
92
 
    fprintf(stderr, "%s\n", strerror(errnum));
93
 
    error(status, errno, "vasprintf while printing error");
94
 
    return;
95
 
  }
96
 
  fprintf(stderr, "Mandos plugin ");
97
 
  error(status, errnum, "%s", text);
98
 
  free(text);
99
 
}
100
 
 
101
73
/* Create prompt string */
102
74
char *makeprompt(void){
103
75
  int ret = 0;
104
76
  char *prompt;
105
77
  const char *const cryptsource = getenv("cryptsource");
106
78
  const char *const crypttarget = getenv("crypttarget");
107
 
  const char prompt_start[] = "Unlocking the disk";
108
 
  const char prompt_end[] = "Enter passphrase";
 
79
  const char prompt_start[] = "Enter passphrase to unlock the disk";
109
80
  
110
81
  if(cryptsource == NULL){
111
82
    if(crypttarget == NULL){
112
 
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
 
83
      ret = asprintf(&prompt, "%s: ", prompt_start);
113
84
    } else {
114
 
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
115
 
                     crypttarget, prompt_end);
 
85
      ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
86
                     crypttarget);
116
87
    }
117
88
  } else {
118
89
    if(crypttarget == NULL){
119
 
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
120
 
                     prompt_end);
 
90
      ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
121
91
    } else {
122
 
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
123
 
                     cryptsource, crypttarget, prompt_end);
 
92
      ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
93
                     cryptsource, crypttarget);
124
94
    }
125
95
  }
126
96
  if(ret == -1){
137
107
bool become_a_daemon(void){
138
108
  int ret = setuid(geteuid());
139
109
  if(ret == -1){
140
 
    error_plus(0, errno, "setuid");
 
110
    error(0, errno, "setuid");
141
111
  }
142
112
    
143
113
  setsid();
144
114
  ret = chdir("/");
145
115
  if(ret == -1){
146
 
    error_plus(0, errno, "chdir");
 
116
    error(0, errno, "chdir");
147
117
    return false;
148
118
  }
149
119
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
150
120
  if(ret == -1){
151
 
    error_plus(0, errno, "dup2");
 
121
    error(0, errno, "dup2");
152
122
    return false;
153
123
  }
154
124
  return true;
162
132
  pid_t pid;
163
133
  pid = fork();
164
134
  if(pid == -1){
165
 
    error_plus(0, errno, "fork");
 
135
    error(0, errno, "fork");
166
136
    return false;
167
137
  }
168
138
  if(pid == 0){
172
142
        _exit(EX_OSERR);
173
143
      }
174
144
    }
175
 
    
 
145
 
176
146
    char **new_argv = NULL;
177
 
    char **tmp;
 
147
    char *tmp;
178
148
    int i = 0;
179
 
    for (; argv[i]!=NULL; i++){
 
149
    for (; argv[i]!=(char *)NULL; i++){
180
150
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
181
151
      if (tmp == NULL){
182
 
        error_plus(0, errno, "realloc");
 
152
        error(0, errno, "realloc");
183
153
        free(new_argv);
184
154
        _exit(EX_OSERR);
185
155
      }
186
 
      new_argv = tmp;
 
156
      new_argv = (char **)tmp;
187
157
      new_argv[i] = strdup(argv[i]);
188
158
    }
189
 
    new_argv[i] = NULL;
 
159
    new_argv[i] = (char *) NULL;
190
160
    
191
161
    execv(path, (char *const *)new_argv);
192
 
    error_plus(0, errno, "execv");
 
162
    error(0, errno, "execv");
193
163
    _exit(EXIT_FAILURE);
194
164
  }
195
165
  if(pid_return != NULL){
204
174
    return false;
205
175
  }
206
176
  if(ret == -1){
207
 
    error_plus(0, errno, "waitpid");
 
177
    error(0, errno, "waitpid");
208
178
    return false;
209
179
  }
210
180
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
226
196
      return 0;
227
197
    }
228
198
  }
229
 
  char exe_target[sizeof(plymouthd_path)];
 
199
  char exe_target[sizeof(plymouth_path)];
230
200
  char *exe_link;
231
201
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
232
202
  if(ret == -1){
233
 
    error_plus(0, errno, "asprintf");
 
203
    error(0, errno, "asprintf");
234
204
    return 0;
235
205
  }
236
206
  
239
209
  if(ret == -1){
240
210
    free(exe_link);
241
211
    if(errno != ENOENT){
242
 
      error_plus(0, errno, "lstat");
 
212
      error(0, errno, "lstat");
243
213
    }
244
214
    return 0;
245
215
  }
253
223
  
254
224
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
255
225
  free(exe_link);
256
 
  if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
257
 
      (memcmp(plymouthd_path, exe_target,
258
 
              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)){
259
229
    return 0;
260
230
  }
261
231
  return 1;
263
233
 
264
234
pid_t get_pid(void){
265
235
  int ret;
 
236
  FILE *pidfile = fopen(plymouth_pid, "r");
266
237
  uintmax_t maxvalue = 0;
267
 
  FILE *pidfile = fopen(plymouth_pid, "r");
268
 
  /* Try the new pid file location */
269
238
  if(pidfile != NULL){
270
239
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
271
240
    if(ret != 1){
273
242
    }
274
243
    fclose(pidfile);
275
244
  }
276
 
  /* Try the old pid file location */
277
 
  if(maxvalue == 0){
278
 
    pidfile = fopen(plymouth_pid, "r");
279
 
    if(pidfile != NULL){
280
 
      ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
281
 
      if(ret != 1){
282
 
        maxvalue = 0;
283
 
      }
284
 
      fclose(pidfile);
285
 
    }
286
 
  }
287
 
  /* Look for a plymouth process */
288
 
  if(maxvalue == 0){
289
 
    struct dirent **direntries = NULL;
 
245
  if(maxvalue == 0){
 
246
    struct dirent **direntries;
290
247
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
291
 
    if (ret == -1){
292
 
      error_plus(0, errno, "scandir");
293
 
    }
294
 
    if (ret > 0){
295
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
296
 
      if (ret < 0){
297
 
        error_plus(0, errno, "sscanf");
298
 
      }
299
 
    }
300
 
    /* scandir might preallocate for this variable (man page unclear).
301
 
       even if ret == 0, therefore we need to free it. */
302
 
    free(direntries);
 
248
    sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
303
249
  }
304
250
  pid_t pid;
305
251
  pid = (pid_t)maxvalue;
319
265
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
320
266
                 (uintmax_t)pid);
321
267
  if(ret == -1){
322
 
    error_plus(0, errno, "asprintf");
 
268
    error(0, errno, "asprintf");
323
269
    return NULL;
324
270
  }
325
271
  
327
273
  cl_fd = open(cmdline_filename, O_RDONLY);
328
274
  free(cmdline_filename);
329
275
  if(cl_fd == -1){
330
 
    error_plus(0, errno, "open");
 
276
    error(0, errno, "open");
331
277
    return NULL;
332
278
  }
333
279
  
341
287
    if(cmdline_len + blocksize > cmdline_allocated){
342
288
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
343
289
      if(tmp == NULL){
344
 
        error_plus(0, errno, "realloc");
 
290
        error(0, errno, "realloc");
345
291
        free(cmdline);
346
292
        close(cl_fd);
347
293
        return NULL;
354
300
    sret = read(cl_fd, cmdline + cmdline_len,
355
301
                cmdline_allocated - cmdline_len);
356
302
    if(sret == -1){
357
 
      error_plus(0, errno, "read");
 
303
      error(0, errno, "read");
358
304
      free(cmdline);
359
305
      close(cl_fd);
360
306
      return NULL;
363
309
  } while(sret != 0);
364
310
  ret = close(cl_fd);
365
311
  if(ret == -1){
366
 
    error_plus(0, errno, "close");
 
312
    error(0, errno, "close");
367
313
    free(cmdline);
368
314
    return NULL;
369
315
  }
372
318
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
373
319
                       * sizeof(char *)); /* Get number of args */
374
320
  if(argv == NULL){
375
 
    error_plus(0, errno, "argv = malloc()");
 
321
    error(0, errno, "argv = malloc()");
376
322
    free(cmdline);
377
323
    return NULL;
378
324
  }
405
351
        *sig != 0; sig++){
406
352
      ret = sigaddset(&new_action.sa_mask, *sig);
407
353
      if(ret == -1){
408
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
354
        error(EX_OSERR, errno, "sigaddset");
409
355
      }
410
356
      ret = sigaction(*sig, NULL, &old_action);
411
357
      if(ret == -1){
412
 
        error_plus(EX_OSERR, errno, "sigaction");
 
358
        error(EX_OSERR, errno, "sigaction");
413
359
      }
414
360
      if(old_action.sa_handler != SIG_IGN){
415
361
        ret = sigaction(*sig, &new_action, NULL);
416
362
        if(ret == -1){
417
 
          error_plus(EX_OSERR, errno, "sigaction");
 
363
          error(EX_OSERR, errno, "sigaction");
418
364
        }
419
365
      }
420
366
    }
439
385
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
440
386
  free(prompt);
441
387
  if(ret == -1){
442
 
    error_plus(EX_OSERR, errno, "asprintf");
 
388
    error(EX_OSERR, errno, "asprintf");
443
389
  }
444
390
  
445
391
  /* plymouth ask-for-password --prompt="$prompt" */
461
407
  const char **plymouthd_argv;
462
408
  pid_t pid = get_pid();
463
409
  if(pid == 0){
464
 
    error_plus(0, 0, "plymouthd pid not found");
 
410
    error(0, 0, "plymouthd pid not found");
465
411
    plymouthd_argv = plymouthd_default_argv;
466
412
  } else {
467
413
    plymouthd_argv = getargv(pid);