/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-09-30 06:24:20 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100930062420-nm7nlb160myyzj82
* mandos-monitor.xml (KEYS): Bug fix: Changed "r" to "R".
* .bzrignore: Updated.
  (debian/po/messages.mo, debian/po/templates.pot): Removed.
  (plugins.d/plymouth): Added.

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