/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 from trunk.  Just a minor release to make it lintian clean.

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-2017 Teddy Hogeborn
6
 
 * Copyright © 2010-2017 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;
155
125
}
156
126
 
157
 
__attribute__((nonnull (2, 3)))
158
127
bool exec_and_wait(pid_t *pid_return, const char *path,
159
 
                   const char * const *argv, bool interruptable,
 
128
                   const char **argv, bool interruptable,
160
129
                   bool daemonize){
161
130
  int status;
162
131
  int ret;
163
132
  pid_t pid;
164
133
  pid = fork();
165
134
  if(pid == -1){
166
 
    error_plus(0, errno, "fork");
 
135
    error(0, errno, "fork");
167
136
    return false;
168
137
  }
169
138
  if(pid == 0){
173
142
        _exit(EX_OSERR);
174
143
      }
175
144
    }
176
 
    
177
 
    char **new_argv = malloc(sizeof(const char *));
178
 
    if(new_argv == NULL){
179
 
      error_plus(0, errno, "malloc");
180
 
      _exit(EX_OSERR);
181
 
    }
182
 
    char **tmp;
 
145
 
 
146
    char **new_argv = NULL;
 
147
    char *tmp;
183
148
    int i = 0;
184
 
    for (; argv[i] != NULL; i++){
185
 
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 2));
186
 
      if(tmp == NULL){
187
 
        error_plus(0, errno, "realloc");
 
149
    for (; argv[i]!=(char *)NULL; i++){
 
150
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
 
151
      if (tmp == NULL){
 
152
        error(0, errno, "realloc");
188
153
        free(new_argv);
189
154
        _exit(EX_OSERR);
190
155
      }
191
 
      new_argv = tmp;
 
156
      new_argv = (char **)tmp;
192
157
      new_argv[i] = strdup(argv[i]);
193
158
    }
194
 
    new_argv[i] = NULL;
 
159
    new_argv[i] = (char *) NULL;
195
160
    
196
161
    execv(path, (char *const *)new_argv);
197
 
    error_plus(0, errno, "execv");
 
162
    error(0, errno, "execv");
198
163
    _exit(EXIT_FAILURE);
199
164
  }
200
165
  if(pid_return != NULL){
209
174
    return false;
210
175
  }
211
176
  if(ret == -1){
212
 
    error_plus(0, errno, "waitpid");
 
177
    error(0, errno, "waitpid");
213
178
    return false;
214
179
  }
215
180
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
218
183
  return false;
219
184
}
220
185
 
221
 
__attribute__((nonnull))
222
186
int is_plymouth(const struct dirent *proc_entry){
223
187
  int ret;
224
188
  {
225
 
    uintmax_t proc_id;
 
189
    uintmax_t maxvalue;
226
190
    char *tmp;
227
191
    errno = 0;
228
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
192
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
229
193
 
230
194
    if(errno != 0 or *tmp != '\0'
231
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
195
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
232
196
      return 0;
233
197
    }
234
198
  }
235
 
  char exe_target[sizeof(plymouthd_path)];
 
199
  char exe_target[sizeof(plymouth_path)];
236
200
  char *exe_link;
237
201
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
238
202
  if(ret == -1){
239
 
    error_plus(0, errno, "asprintf");
 
203
    error(0, errno, "asprintf");
240
204
    return 0;
241
205
  }
242
206
  
245
209
  if(ret == -1){
246
210
    free(exe_link);
247
211
    if(errno != ENOENT){
248
 
      error_plus(0, errno, "lstat");
 
212
      error(0, errno, "lstat");
249
213
    }
250
214
    return 0;
251
215
  }
259
223
  
260
224
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
261
225
  free(exe_link);
262
 
  if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
263
 
      (memcmp(plymouthd_path, exe_target,
264
 
              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)){
265
229
    return 0;
266
230
  }
267
231
  return 1;
269
233
 
270
234
pid_t get_pid(void){
271
235
  int ret;
272
 
  uintmax_t proc_id = 0;
273
236
  FILE *pidfile = fopen(plymouth_pid, "r");
274
 
  /* Try the new pid file location */
 
237
  uintmax_t maxvalue = 0;
275
238
  if(pidfile != NULL){
276
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
239
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
277
240
    if(ret != 1){
278
 
      proc_id = 0;
 
241
      maxvalue = 0;
279
242
    }
280
243
    fclose(pidfile);
281
244
  }
282
 
  /* Try the old pid file location */
283
 
  if(proc_id == 0){
284
 
    pidfile = fopen(plymouth_pid, "r");
285
 
    if(pidfile != NULL){
286
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
287
 
      if(ret != 1){
288
 
        proc_id = 0;
289
 
      }
290
 
      fclose(pidfile);
291
 
    }
292
 
  }
293
 
  /* Look for a plymouth process */
294
 
  if(proc_id == 0){
295
 
    struct dirent **direntries = NULL;
 
245
  if(maxvalue == 0){
 
246
    struct dirent **direntries;
296
247
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
297
 
    if(ret == -1){
298
 
      error_plus(0, errno, "scandir");
299
 
    }
300
 
    if(ret > 0){
301
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &proc_id);
302
 
      if(ret < 0){
303
 
        error_plus(0, errno, "sscanf");
304
 
      }
305
 
    }
306
 
    /* scandir might preallocate for this variable (man page unclear).
307
 
       even if ret == 0, therefore we need to free it. */
308
 
    free(direntries);
 
248
    sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
309
249
  }
310
250
  pid_t pid;
311
 
  pid = (pid_t)proc_id;
312
 
  if((uintmax_t)pid == proc_id){
 
251
  pid = (pid_t)maxvalue;
 
252
  if((uintmax_t)pid == maxvalue){
313
253
    return pid;
314
254
  }
315
255
  
316
256
  return 0;
317
257
}
318
258
 
319
 
const char * const * getargv(pid_t pid){
 
259
const char **getargv(pid_t pid){
320
260
  int cl_fd;
321
261
  char *cmdline_filename;
322
262
  ssize_t sret;
325
265
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
326
266
                 (uintmax_t)pid);
327
267
  if(ret == -1){
328
 
    error_plus(0, errno, "asprintf");
 
268
    error(0, errno, "asprintf");
329
269
    return NULL;
330
270
  }
331
271
  
333
273
  cl_fd = open(cmdline_filename, O_RDONLY);
334
274
  free(cmdline_filename);
335
275
  if(cl_fd == -1){
336
 
    error_plus(0, errno, "open");
 
276
    error(0, errno, "open");
337
277
    return NULL;
338
278
  }
339
279
  
347
287
    if(cmdline_len + blocksize > cmdline_allocated){
348
288
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
349
289
      if(tmp == NULL){
350
 
        error_plus(0, errno, "realloc");
 
290
        error(0, errno, "realloc");
351
291
        free(cmdline);
352
292
        close(cl_fd);
353
293
        return NULL;
360
300
    sret = read(cl_fd, cmdline + cmdline_len,
361
301
                cmdline_allocated - cmdline_len);
362
302
    if(sret == -1){
363
 
      error_plus(0, errno, "read");
 
303
      error(0, errno, "read");
364
304
      free(cmdline);
365
305
      close(cl_fd);
366
306
      return NULL;
369
309
  } while(sret != 0);
370
310
  ret = close(cl_fd);
371
311
  if(ret == -1){
372
 
    error_plus(0, errno, "close");
 
312
    error(0, errno, "close");
373
313
    free(cmdline);
374
314
    return NULL;
375
315
  }
378
318
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
379
319
                       * sizeof(char *)); /* Get number of args */
380
320
  if(argv == NULL){
381
 
    error_plus(0, errno, "argv = malloc()");
 
321
    error(0, errno, "argv = malloc()");
382
322
    free(cmdline);
383
323
    return NULL;
384
324
  }
385
325
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
386
 
  return (const char * const *)argv;
 
326
  return (const char **)argv;
387
327
}
388
328
 
389
329
int main(__attribute__((unused))int argc,
411
351
        *sig != 0; sig++){
412
352
      ret = sigaddset(&new_action.sa_mask, *sig);
413
353
      if(ret == -1){
414
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
354
        error(EX_OSERR, errno, "sigaddset");
415
355
      }
416
356
      ret = sigaction(*sig, NULL, &old_action);
417
357
      if(ret == -1){
418
 
        error_plus(EX_OSERR, errno, "sigaction");
 
358
        error(EX_OSERR, errno, "sigaction");
419
359
      }
420
360
      if(old_action.sa_handler != SIG_IGN){
421
361
        ret = sigaction(*sig, &new_action, NULL);
422
362
        if(ret == -1){
423
 
          error_plus(EX_OSERR, errno, "sigaction");
 
363
          error(EX_OSERR, errno, "sigaction");
424
364
        }
425
365
      }
426
366
    }
445
385
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
446
386
  free(prompt);
447
387
  if(ret == -1){
448
 
    error_plus(EX_OSERR, errno, "asprintf");
 
388
    error(EX_OSERR, errno, "asprintf");
449
389
  }
450
390
  
451
391
  /* plymouth ask-for-password --prompt="$prompt" */
464
404
  }
465
405
  kill_and_wait(plymouth_command_pid);
466
406
  
467
 
  const char * const *plymouthd_argv;
 
407
  const char **plymouthd_argv;
468
408
  pid_t pid = get_pid();
469
409
  if(pid == 0){
470
 
    error_plus(0, 0, "plymouthd pid not found");
 
410
    error(0, 0, "plymouthd pid not found");
471
411
    plymouthd_argv = plymouthd_default_argv;
472
412
  } else {
473
413
    plymouthd_argv = getargv(pid);