/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: 2011-03-08 19:09:03 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110308190903-j499ebtb9bpk31ar
* INSTALL: Updated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
136
109
bool become_a_daemon(void){
137
110
  int ret = setuid(geteuid());
138
111
  if(ret == -1){
139
 
    error_plus(0, errno, "setuid");
 
112
    error(0, errno, "setuid");
140
113
  }
141
114
    
142
115
  setsid();
143
116
  ret = chdir("/");
144
117
  if(ret == -1){
145
 
    error_plus(0, errno, "chdir");
 
118
    error(0, errno, "chdir");
146
119
    return false;
147
120
  }
148
121
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
149
122
  if(ret == -1){
150
 
    error_plus(0, errno, "dup2");
 
123
    error(0, errno, "dup2");
151
124
    return false;
152
125
  }
153
126
  return true;
161
134
  pid_t pid;
162
135
  pid = fork();
163
136
  if(pid == -1){
164
 
    error_plus(0, errno, "fork");
 
137
    error(0, errno, "fork");
165
138
    return false;
166
139
  }
167
140
  if(pid == 0){
178
151
    for (; argv[i]!=NULL; i++){
179
152
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
180
153
      if (tmp == NULL){
181
 
        error_plus(0, errno, "realloc");
 
154
        error(0, errno, "realloc");
182
155
        free(new_argv);
183
156
        _exit(EX_OSERR);
184
157
      }
188
161
    new_argv[i] = NULL;
189
162
    
190
163
    execv(path, (char *const *)new_argv);
191
 
    error_plus(0, errno, "execv");
 
164
    error(0, errno, "execv");
192
165
    _exit(EXIT_FAILURE);
193
166
  }
194
167
  if(pid_return != NULL){
203
176
    return false;
204
177
  }
205
178
  if(ret == -1){
206
 
    error_plus(0, errno, "waitpid");
 
179
    error(0, errno, "waitpid");
207
180
    return false;
208
181
  }
209
182
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
229
202
  char *exe_link;
230
203
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
231
204
  if(ret == -1){
232
 
    error_plus(0, errno, "asprintf");
 
205
    error(0, errno, "asprintf");
233
206
    return 0;
234
207
  }
235
208
  
238
211
  if(ret == -1){
239
212
    free(exe_link);
240
213
    if(errno != ENOENT){
241
 
      error_plus(0, errno, "lstat");
 
214
      error(0, errno, "lstat");
242
215
    }
243
216
    return 0;
244
217
  }
262
235
 
263
236
pid_t get_pid(void){
264
237
  int ret;
 
238
  FILE *pidfile = fopen(plymouth_pid, "r");
265
239
  uintmax_t maxvalue = 0;
266
 
  FILE *pidfile = fopen(plymouth_pid, "r");
267
 
  /* Try the new pid file location */
268
240
  if(pidfile != NULL){
269
241
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
270
242
    if(ret != 1){
272
244
    }
273
245
    fclose(pidfile);
274
246
  }
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;
 
247
  if(maxvalue == 0){
 
248
    struct dirent **direntries;
289
249
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
290
250
    if (ret == -1){
291
 
      error_plus(0, errno, "scandir");
 
251
      error(0, errno, "scandir");
292
252
    }
293
253
    if (ret > 0){
294
254
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
295
255
      if (ret < 0){
296
 
        error_plus(0, errno, "sscanf");
 
256
        error(0, errno, "sscanf");
297
257
      }
298
258
    }
299
 
    /* scandir might preallocate for this variable (man page unclear).
300
 
       even if ret == 0, therefore we need to free it. */
301
 
    free(direntries);
302
259
  }
303
260
  pid_t pid;
304
261
  pid = (pid_t)maxvalue;
318
275
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
319
276
                 (uintmax_t)pid);
320
277
  if(ret == -1){
321
 
    error_plus(0, errno, "asprintf");
 
278
    error(0, errno, "asprintf");
322
279
    return NULL;
323
280
  }
324
281
  
326
283
  cl_fd = open(cmdline_filename, O_RDONLY);
327
284
  free(cmdline_filename);
328
285
  if(cl_fd == -1){
329
 
    error_plus(0, errno, "open");
 
286
    error(0, errno, "open");
330
287
    return NULL;
331
288
  }
332
289
  
340
297
    if(cmdline_len + blocksize > cmdline_allocated){
341
298
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
342
299
      if(tmp == NULL){
343
 
        error_plus(0, errno, "realloc");
 
300
        error(0, errno, "realloc");
344
301
        free(cmdline);
345
302
        close(cl_fd);
346
303
        return NULL;
353
310
    sret = read(cl_fd, cmdline + cmdline_len,
354
311
                cmdline_allocated - cmdline_len);
355
312
    if(sret == -1){
356
 
      error_plus(0, errno, "read");
 
313
      error(0, errno, "read");
357
314
      free(cmdline);
358
315
      close(cl_fd);
359
316
      return NULL;
362
319
  } while(sret != 0);
363
320
  ret = close(cl_fd);
364
321
  if(ret == -1){
365
 
    error_plus(0, errno, "close");
 
322
    error(0, errno, "close");
366
323
    free(cmdline);
367
324
    return NULL;
368
325
  }
371
328
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
372
329
                       * sizeof(char *)); /* Get number of args */
373
330
  if(argv == NULL){
374
 
    error_plus(0, errno, "argv = malloc()");
 
331
    error(0, errno, "argv = malloc()");
375
332
    free(cmdline);
376
333
    return NULL;
377
334
  }
404
361
        *sig != 0; sig++){
405
362
      ret = sigaddset(&new_action.sa_mask, *sig);
406
363
      if(ret == -1){
407
 
        error_plus(EX_OSERR, errno, "sigaddset");
 
364
        error(EX_OSERR, errno, "sigaddset");
408
365
      }
409
366
      ret = sigaction(*sig, NULL, &old_action);
410
367
      if(ret == -1){
411
 
        error_plus(EX_OSERR, errno, "sigaction");
 
368
        error(EX_OSERR, errno, "sigaction");
412
369
      }
413
370
      if(old_action.sa_handler != SIG_IGN){
414
371
        ret = sigaction(*sig, &new_action, NULL);
415
372
        if(ret == -1){
416
 
          error_plus(EX_OSERR, errno, "sigaction");
 
373
          error(EX_OSERR, errno, "sigaction");
417
374
        }
418
375
      }
419
376
    }
438
395
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
439
396
  free(prompt);
440
397
  if(ret == -1){
441
 
    error_plus(EX_OSERR, errno, "asprintf");
 
398
    error(EX_OSERR, errno, "asprintf");
442
399
  }
443
400
  
444
401
  /* plymouth ask-for-password --prompt="$prompt" */
460
417
  const char **plymouthd_argv;
461
418
  pid_t pid = get_pid();
462
419
  if(pid == 0){
463
 
    error_plus(0, 0, "plymouthd pid not found");
 
420
    error(0, 0, "plymouthd pid not found");
464
421
    plymouthd_argv = plymouthd_default_argv;
465
422
  } else {
466
423
    plymouthd_argv = getargv(pid);