/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: teddy at bsnet
  • Date: 2011-02-15 19:27:23 UTC
  • mto: (237.7.13 mandos)
  • mto: This revision was merged to the branch mainline in revision 282.
  • Revision ID: teddy@fukt.bsnet.se-20110215192723-qpftjhlvzadm5cjc
* mandos-ctl: Use unicode string literals.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Plymouth - Read a password from Plymouth 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
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(), vfprintf() */
 
39
                                   fscanf() */
40
40
#include <unistd.h>             /* close(), readlink(), read(),
41
41
                                   fork(), setsid(), chdir(), dup2(),
42
42
                                   STDERR_FILENO, execv(), access() */
50
50
#include <error.h>              /* error() */
51
51
#include <errno.h>              /* TEMP_FAILURE_RETRY */
52
52
#include <argz.h>               /* argz_count(), argz_extract() */
53
 
#include <stdarg.h>             /* va_list, va_start(), ... */
54
53
 
55
54
sig_atomic_t interrupted_by_signal = 0;
56
55
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
71
70
  interrupted_by_signal = 1;
72
71
}
73
72
 
74
 
/* Function to use when printing errors */
75
 
void error_plus(int status, int errnum, const char *formatstring, ...){
76
 
  va_list ap;
77
 
  char *text;
78
 
  int ret;
79
 
  
80
 
  va_start(ap, formatstring);
81
 
  ret = vasprintf(&text, formatstring, ap);
82
 
  if (ret == -1){
83
 
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
84
 
    vfprintf(stderr, formatstring, ap);
85
 
    fprintf(stderr, ": ");
86
 
    fprintf(stderr, "%s\n", strerror(errnum));
87
 
    error(status, errno, "vasprintf while printing error");
88
 
    return;
89
 
  }
90
 
  fprintf(stderr, "Mandos plugin ");
91
 
  error(status, errnum, "%s", text);
92
 
  free(text);
93
 
}
94
 
 
95
73
/* Create prompt string */
96
74
char *makeprompt(void){
97
75
  int ret = 0;
131
109
bool become_a_daemon(void){
132
110
  int ret = setuid(geteuid());
133
111
  if(ret == -1){
134
 
    error_plus(0, errno, "setuid");
 
112
    error(0, errno, "setuid");
135
113
  }
136
114
    
137
115
  setsid();
138
116
  ret = chdir("/");
139
117
  if(ret == -1){
140
 
    error_plus(0, errno, "chdir");
 
118
    error(0, errno, "chdir");
141
119
    return false;
142
120
  }
143
121
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
144
122
  if(ret == -1){
145
 
    error_plus(0, errno, "dup2");
 
123
    error(0, errno, "dup2");
146
124
    return false;
147
125
  }
148
126
  return true;
156
134
  pid_t pid;
157
135
  pid = fork();
158
136
  if(pid == -1){
159
 
    error_plus(0, errno, "fork");
 
137
    error(0, errno, "fork");
160
138
    return false;
161
139
  }
162
140
  if(pid == 0){
173
151
    for (; argv[i]!=NULL; i++){
174
152
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
175
153
      if (tmp == NULL){
176
 
        error_plus(0, errno, "realloc");
 
154
        error(0, errno, "realloc");
177
155
        free(new_argv);
178
156
        _exit(EX_OSERR);
179
157
      }
183
161
    new_argv[i] = NULL;
184
162
    
185
163
    execv(path, (char *const *)new_argv);
186
 
    error_plus(0, errno, "execv");
 
164
    error(0, errno, "execv");
187
165
    _exit(EXIT_FAILURE);
188
166
  }
189
167
  if(pid_return != NULL){
198
176
    return false;
199
177
  }
200
178
  if(ret == -1){
201
 
    error_plus(0, errno, "waitpid");
 
179
    error(0, errno, "waitpid");
202
180
    return false;
203
181
  }
204
182
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
224
202
  char *exe_link;
225
203
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
226
204
  if(ret == -1){
227
 
    error_plus(0, errno, "asprintf");
 
205
    error(0, errno, "asprintf");
228
206
    return 0;
229
207
  }
230
208
  
233
211
  if(ret == -1){
234
212
    free(exe_link);
235
213
    if(errno != ENOENT){
236
 
      error_plus(0, errno, "lstat");
 
214
      error(0, errno, "lstat");
237
215
    }
238
216
    return 0;
239
217
  }
267
245
    fclose(pidfile);
268
246
  }
269
247
  if(maxvalue == 0){
270
 
    struct dirent **direntries = NULL;
 
248
    struct dirent **direntries;
271
249
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
272
250
    if (ret == -1){
273
 
      error_plus(0, errno, "scandir");
 
251
      error(0, errno, "scandir");
274
252
    }
275
253
    if (ret > 0){
276
254
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
277
255
      if (ret < 0){
278
 
        error_plus(0, errno, "sscanf");
 
256
        error(0, errno, "sscanf");
279
257
      }
280
258
    }
281
 
    /* scandir might preallocate for this variable (man page unclear).
282
 
       even if ret == 0, we need to free it. */
283
 
    free(direntries);
284
259
  }
285
260
  pid_t pid;
286
261
  pid = (pid_t)maxvalue;
300
275
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
301
276
                 (uintmax_t)pid);
302
277
  if(ret == -1){
303
 
    error_plus(0, errno, "asprintf");
 
278
    error(0, errno, "asprintf");
304
279
    return NULL;
305
280
  }
306
281
  
308
283
  cl_fd = open(cmdline_filename, O_RDONLY);
309
284
  free(cmdline_filename);
310
285
  if(cl_fd == -1){
311
 
    error_plus(0, errno, "open");
 
286
    error(0, errno, "open");
312
287
    return NULL;
313
288
  }
314
289
  
322
297
    if(cmdline_len + blocksize > cmdline_allocated){
323
298
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
324
299
      if(tmp == NULL){
325
 
        error_plus(0, errno, "realloc");
 
300
        error(0, errno, "realloc");
326
301
        free(cmdline);
327
302
        close(cl_fd);
328
303
        return NULL;
335
310
    sret = read(cl_fd, cmdline + cmdline_len,
336
311
                cmdline_allocated - cmdline_len);
337
312
    if(sret == -1){
338
 
      error_plus(0, errno, "read");
 
313
      error(0, errno, "read");
339
314
      free(cmdline);
340
315
      close(cl_fd);
341
316
      return NULL;
344
319
  } while(sret != 0);
345
320
  ret = close(cl_fd);
346
321
  if(ret == -1){
347
 
    error_plus(0, errno, "close");
 
322
    error(0, errno, "close");
348
323
    free(cmdline);
349
324
    return NULL;
350
325
  }
353
328
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
354
329
                       * sizeof(char *)); /* Get number of args */
355
330
  if(argv == NULL){
356
 
    error_plus(0, errno, "argv = malloc()");
 
331
    error(0, errno, "argv = malloc()");
357
332
    free(cmdline);
358
333
    return NULL;
359
334
  }
386
361
        *sig != 0; sig++){
387
362
      ret = sigaddset(&new_action.sa_mask, *sig);
388
363
      if(ret == -1){
389
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
364
        error(EX_OSERR, errno, "sigaddset");
390
365
      }
391
366
      ret = sigaction(*sig, NULL, &old_action);
392
367
      if(ret == -1){
393
 
        error_plus(EX_OSERR, errno, "sigaction");
 
368
        error(EX_OSERR, errno, "sigaction");
394
369
      }
395
370
      if(old_action.sa_handler != SIG_IGN){
396
371
        ret = sigaction(*sig, &new_action, NULL);
397
372
        if(ret == -1){
398
 
          error_plus(EX_OSERR, errno, "sigaction");
 
373
          error(EX_OSERR, errno, "sigaction");
399
374
        }
400
375
      }
401
376
    }
420
395
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
421
396
  free(prompt);
422
397
  if(ret == -1){
423
 
    error_plus(EX_OSERR, errno, "asprintf");
 
398
    error(EX_OSERR, errno, "asprintf");
424
399
  }
425
400
  
426
401
  /* plymouth ask-for-password --prompt="$prompt" */
442
417
  const char **plymouthd_argv;
443
418
  pid_t pid = get_pid();
444
419
  if(pid == 0){
445
 
    error_plus(0, 0, "plymouthd pid not found");
 
420
    error(0, 0, "plymouthd pid not found");
446
421
    plymouthd_argv = plymouthd_default_argv;
447
422
  } else {
448
423
    plymouthd_argv = getargv(pid);