/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-25 21:24:49 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100925212449-h9hpy9n406tkkakb
* mandos.xml (APPROVAL): New section.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
2
 
/*
3
 
 * Plymouth - Read a password from Plymouth and output it
4
 
 * 
5
 
 * Copyright © 2010-2011 Teddy Hogeborn
6
 
 * Copyright © 2010-2011 Björn Påhlsson
7
 
 * 
8
 
 * This program is free software: you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation, either version 3 of the
11
 
 * License, or (at your option) any later version.
12
 
 * 
13
 
 * This program is distributed in the hope that it will be useful, but
14
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 * General Public License for more details.
17
 
 * 
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see
20
 
 * <http://www.gnu.org/licenses/>.
21
 
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
 
 */
24
 
 
25
1
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
26
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
27
3
                                   sigemptyset(), sigaddset(), SIGINT,
36
12
#include <stddef.h>             /* NULL */
37
13
#include <string.h>             /* strchr(), memcmp() */
38
14
#include <stdio.h>              /* asprintf(), perror(), fopen(),
39
 
                                   fscanf(), vasprintf(), fprintf(),
40
 
                                   vfprintf() */
 
15
                                   fscanf() */
41
16
#include <unistd.h>             /* close(), readlink(), read(),
42
17
                                   fork(), setsid(), chdir(), dup2(),
43
18
                                   STDERR_FILENO, execv(), access() */
51
26
#include <error.h>              /* error() */
52
27
#include <errno.h>              /* TEMP_FAILURE_RETRY */
53
28
#include <argz.h>               /* argz_count(), argz_extract() */
54
 
#include <stdarg.h>             /* va_list, va_start(), ... */
55
29
 
56
30
sig_atomic_t interrupted_by_signal = 0;
57
31
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
72
46
  interrupted_by_signal = 1;
73
47
}
74
48
 
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
49
/* Create prompt string */
99
50
char *makeprompt(void){
100
51
  int ret = 0;
101
52
  char *prompt;
102
53
  const char *const cryptsource = getenv("cryptsource");
103
54
  const char *const crypttarget = getenv("crypttarget");
104
 
  const char prompt_start[] = "Unlocking the disk";
105
 
  const char prompt_end[] = "Enter passphrase";
 
55
  const char prompt_start[] = "Enter passphrase to unlock the disk";
106
56
  
107
57
  if(cryptsource == NULL){
108
58
    if(crypttarget == NULL){
109
 
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
 
59
      ret = asprintf(&prompt, "%s: ", prompt_start);
110
60
    } else {
111
 
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
112
 
                     crypttarget, prompt_end);
 
61
      ret = asprintf(&prompt, "%s (%s): ", prompt_start,
 
62
                     crypttarget);
113
63
    }
114
64
  } else {
115
65
    if(crypttarget == NULL){
116
 
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
117
 
                     prompt_end);
 
66
      ret = asprintf(&prompt, "%s %s: ", prompt_start, cryptsource);
118
67
    } else {
119
 
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
120
 
                     cryptsource, crypttarget, prompt_end);
 
68
      ret = asprintf(&prompt, "%s %s (%s): ", prompt_start,
 
69
                     cryptsource, crypttarget);
121
70
    }
122
71
  }
123
72
  if(ret == -1){
134
83
bool become_a_daemon(void){
135
84
  int ret = setuid(geteuid());
136
85
  if(ret == -1){
137
 
    error_plus(0, errno, "setuid");
 
86
    error(0, errno, "setuid");
138
87
  }
139
88
    
140
89
  setsid();
141
90
  ret = chdir("/");
142
91
  if(ret == -1){
143
 
    error_plus(0, errno, "chdir");
 
92
    error(0, errno, "chdir");
144
93
    return false;
145
94
  }
146
95
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
147
96
  if(ret == -1){
148
 
    error_plus(0, errno, "dup2");
 
97
    error(0, errno, "dup2");
149
98
    return false;
150
99
  }
151
100
  return true;
159
108
  pid_t pid;
160
109
  pid = fork();
161
110
  if(pid == -1){
162
 
    error_plus(0, errno, "fork");
 
111
    error(0, errno, "fork");
163
112
    return false;
164
113
  }
165
114
  if(pid == 0){
169
118
        _exit(EX_OSERR);
170
119
      }
171
120
    }
172
 
    
 
121
 
173
122
    char **new_argv = NULL;
174
 
    char **tmp;
 
123
    char *tmp;
175
124
    int i = 0;
176
 
    for (; argv[i]!=NULL; i++){
 
125
    for (; argv[i]!=(char *)NULL; i++){
177
126
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
178
127
      if (tmp == NULL){
179
 
        error_plus(0, errno, "realloc");
 
128
        error(0, errno, "realloc");
180
129
        free(new_argv);
181
130
        _exit(EX_OSERR);
182
131
      }
183
 
      new_argv = tmp;
 
132
      new_argv = (char **)tmp;
184
133
      new_argv[i] = strdup(argv[i]);
185
134
    }
186
 
    new_argv[i] = NULL;
 
135
    new_argv[i] = (char *) NULL;
187
136
    
188
137
    execv(path, (char *const *)new_argv);
189
 
    error_plus(0, errno, "execv");
 
138
    error(0, errno, "execv");
190
139
    _exit(EXIT_FAILURE);
191
140
  }
192
141
  if(pid_return != NULL){
201
150
    return false;
202
151
  }
203
152
  if(ret == -1){
204
 
    error_plus(0, errno, "waitpid");
 
153
    error(0, errno, "waitpid");
205
154
    return false;
206
155
  }
207
156
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
223
172
      return 0;
224
173
    }
225
174
  }
226
 
  char exe_target[sizeof(plymouthd_path)];
 
175
  char exe_target[sizeof(plymouth_path)];
227
176
  char *exe_link;
228
177
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
229
178
  if(ret == -1){
230
 
    error_plus(0, errno, "asprintf");
 
179
    error(0, errno, "asprintf");
231
180
    return 0;
232
181
  }
233
182
  
236
185
  if(ret == -1){
237
186
    free(exe_link);
238
187
    if(errno != ENOENT){
239
 
      error_plus(0, errno, "lstat");
 
188
      error(0, errno, "lstat");
240
189
    }
241
190
    return 0;
242
191
  }
250
199
  
251
200
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
252
201
  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)){
 
202
  if((sret != (ssize_t)sizeof(plymouth_path)-1) or
 
203
      (memcmp(plymouth_path, exe_target,
 
204
              sizeof(plymouth_path)-1) != 0)){
256
205
    return 0;
257
206
  }
258
207
  return 1;
270
219
    fclose(pidfile);
271
220
  }
272
221
  if(maxvalue == 0){
273
 
    struct dirent **direntries = NULL;
 
222
    struct dirent **direntries;
274
223
    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);
 
224
    sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
287
225
  }
288
226
  pid_t pid;
289
227
  pid = (pid_t)maxvalue;
303
241
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
304
242
                 (uintmax_t)pid);
305
243
  if(ret == -1){
306
 
    error_plus(0, errno, "asprintf");
 
244
    error(0, errno, "asprintf");
307
245
    return NULL;
308
246
  }
309
247
  
311
249
  cl_fd = open(cmdline_filename, O_RDONLY);
312
250
  free(cmdline_filename);
313
251
  if(cl_fd == -1){
314
 
    error_plus(0, errno, "open");
 
252
    error(0, errno, "open");
315
253
    return NULL;
316
254
  }
317
255
  
325
263
    if(cmdline_len + blocksize > cmdline_allocated){
326
264
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
327
265
      if(tmp == NULL){
328
 
        error_plus(0, errno, "realloc");
 
266
        error(0, errno, "realloc");
329
267
        free(cmdline);
330
268
        close(cl_fd);
331
269
        return NULL;
338
276
    sret = read(cl_fd, cmdline + cmdline_len,
339
277
                cmdline_allocated - cmdline_len);
340
278
    if(sret == -1){
341
 
      error_plus(0, errno, "read");
 
279
      error(0, errno, "read");
342
280
      free(cmdline);
343
281
      close(cl_fd);
344
282
      return NULL;
347
285
  } while(sret != 0);
348
286
  ret = close(cl_fd);
349
287
  if(ret == -1){
350
 
    error_plus(0, errno, "close");
 
288
    error(0, errno, "close");
351
289
    free(cmdline);
352
290
    return NULL;
353
291
  }
356
294
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
357
295
                       * sizeof(char *)); /* Get number of args */
358
296
  if(argv == NULL){
359
 
    error_plus(0, errno, "argv = malloc()");
 
297
    error(0, errno, "argv = malloc()");
360
298
    free(cmdline);
361
299
    return NULL;
362
300
  }
389
327
        *sig != 0; sig++){
390
328
      ret = sigaddset(&new_action.sa_mask, *sig);
391
329
      if(ret == -1){
392
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
330
        error(EX_OSERR, errno, "sigaddset");
393
331
      }
394
332
      ret = sigaction(*sig, NULL, &old_action);
395
333
      if(ret == -1){
396
 
        error_plus(EX_OSERR, errno, "sigaction");
 
334
        error(EX_OSERR, errno, "sigaction");
397
335
      }
398
336
      if(old_action.sa_handler != SIG_IGN){
399
337
        ret = sigaction(*sig, &new_action, NULL);
400
338
        if(ret == -1){
401
 
          error_plus(EX_OSERR, errno, "sigaction");
 
339
          error(EX_OSERR, errno, "sigaction");
402
340
        }
403
341
      }
404
342
    }
423
361
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
424
362
  free(prompt);
425
363
  if(ret == -1){
426
 
    error_plus(EX_OSERR, errno, "asprintf");
 
364
    error(EX_OSERR, errno, "asprintf");
427
365
  }
428
366
  
429
367
  /* plymouth ask-for-password --prompt="$prompt" */
445
383
  const char **plymouthd_argv;
446
384
  pid_t pid = get_pid();
447
385
  if(pid == 0){
448
 
    error_plus(0, 0, "plymouthd pid not found");
 
386
    error(0, 0, "plymouthd pid not found");
449
387
    plymouthd_argv = plymouthd_default_argv;
450
388
  } else {
451
389
    plymouthd_argv = getargv(pid);