/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-07-13 02:24:39 UTC
  • mfrom: (24.1.174 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20110713022439-wbv6kghshdsc2x24
Merge from Björn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include <stddef.h>             /* NULL */
37
37
#include <string.h>             /* strchr(), memcmp() */
38
38
#include <stdio.h>              /* asprintf(), perror(), fopen(),
39
 
                                   fscanf() */
 
39
                                   fscanf(), vasprintf(), fprintf(),
 
40
                                   vfprintf() */
40
41
#include <unistd.h>             /* close(), readlink(), read(),
41
42
                                   fork(), setsid(), chdir(), dup2(),
42
43
                                   STDERR_FILENO, execv(), access() */
50
51
#include <error.h>              /* error() */
51
52
#include <errno.h>              /* TEMP_FAILURE_RETRY */
52
53
#include <argz.h>               /* argz_count(), argz_extract() */
 
54
#include <stdarg.h>             /* va_list, va_start(), ... */
53
55
 
54
56
sig_atomic_t interrupted_by_signal = 0;
55
57
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
70
72
  interrupted_by_signal = 1;
71
73
}
72
74
 
 
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
 
73
98
/* Create prompt string */
74
99
char *makeprompt(void){
75
100
  int ret = 0;
109
134
bool become_a_daemon(void){
110
135
  int ret = setuid(geteuid());
111
136
  if(ret == -1){
112
 
    error(0, errno, "setuid");
 
137
    error_plus(0, errno, "setuid");
113
138
  }
114
139
    
115
140
  setsid();
116
141
  ret = chdir("/");
117
142
  if(ret == -1){
118
 
    error(0, errno, "chdir");
 
143
    error_plus(0, errno, "chdir");
119
144
    return false;
120
145
  }
121
146
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
122
147
  if(ret == -1){
123
 
    error(0, errno, "dup2");
 
148
    error_plus(0, errno, "dup2");
124
149
    return false;
125
150
  }
126
151
  return true;
134
159
  pid_t pid;
135
160
  pid = fork();
136
161
  if(pid == -1){
137
 
    error(0, errno, "fork");
 
162
    error_plus(0, errno, "fork");
138
163
    return false;
139
164
  }
140
165
  if(pid == 0){
151
176
    for (; argv[i]!=NULL; i++){
152
177
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
153
178
      if (tmp == NULL){
154
 
        error(0, errno, "realloc");
 
179
        error_plus(0, errno, "realloc");
155
180
        free(new_argv);
156
181
        _exit(EX_OSERR);
157
182
      }
161
186
    new_argv[i] = NULL;
162
187
    
163
188
    execv(path, (char *const *)new_argv);
164
 
    error(0, errno, "execv");
 
189
    error_plus(0, errno, "execv");
165
190
    _exit(EXIT_FAILURE);
166
191
  }
167
192
  if(pid_return != NULL){
176
201
    return false;
177
202
  }
178
203
  if(ret == -1){
179
 
    error(0, errno, "waitpid");
 
204
    error_plus(0, errno, "waitpid");
180
205
    return false;
181
206
  }
182
207
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
202
227
  char *exe_link;
203
228
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
204
229
  if(ret == -1){
205
 
    error(0, errno, "asprintf");
 
230
    error_plus(0, errno, "asprintf");
206
231
    return 0;
207
232
  }
208
233
  
211
236
  if(ret == -1){
212
237
    free(exe_link);
213
238
    if(errno != ENOENT){
214
 
      error(0, errno, "lstat");
 
239
      error_plus(0, errno, "lstat");
215
240
    }
216
241
    return 0;
217
242
  }
245
270
    fclose(pidfile);
246
271
  }
247
272
  if(maxvalue == 0){
248
 
    struct dirent **direntries;
 
273
    struct dirent **direntries = NULL;
249
274
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
250
275
    if (ret == -1){
251
 
      error(0, errno, "scandir");
 
276
      error_plus(0, errno, "scandir");
252
277
    }
253
278
    if (ret > 0){
254
279
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
255
280
      if (ret < 0){
256
 
        error(0, errno, "sscanf");
 
281
        error_plus(0, errno, "sscanf");
257
282
      }
258
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);
259
287
  }
260
288
  pid_t pid;
261
289
  pid = (pid_t)maxvalue;
275
303
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
276
304
                 (uintmax_t)pid);
277
305
  if(ret == -1){
278
 
    error(0, errno, "asprintf");
 
306
    error_plus(0, errno, "asprintf");
279
307
    return NULL;
280
308
  }
281
309
  
283
311
  cl_fd = open(cmdline_filename, O_RDONLY);
284
312
  free(cmdline_filename);
285
313
  if(cl_fd == -1){
286
 
    error(0, errno, "open");
 
314
    error_plus(0, errno, "open");
287
315
    return NULL;
288
316
  }
289
317
  
297
325
    if(cmdline_len + blocksize > cmdline_allocated){
298
326
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
299
327
      if(tmp == NULL){
300
 
        error(0, errno, "realloc");
 
328
        error_plus(0, errno, "realloc");
301
329
        free(cmdline);
302
330
        close(cl_fd);
303
331
        return NULL;
310
338
    sret = read(cl_fd, cmdline + cmdline_len,
311
339
                cmdline_allocated - cmdline_len);
312
340
    if(sret == -1){
313
 
      error(0, errno, "read");
 
341
      error_plus(0, errno, "read");
314
342
      free(cmdline);
315
343
      close(cl_fd);
316
344
      return NULL;
319
347
  } while(sret != 0);
320
348
  ret = close(cl_fd);
321
349
  if(ret == -1){
322
 
    error(0, errno, "close");
 
350
    error_plus(0, errno, "close");
323
351
    free(cmdline);
324
352
    return NULL;
325
353
  }
328
356
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
329
357
                       * sizeof(char *)); /* Get number of args */
330
358
  if(argv == NULL){
331
 
    error(0, errno, "argv = malloc()");
 
359
    error_plus(0, errno, "argv = malloc()");
332
360
    free(cmdline);
333
361
    return NULL;
334
362
  }
361
389
        *sig != 0; sig++){
362
390
      ret = sigaddset(&new_action.sa_mask, *sig);
363
391
      if(ret == -1){
364
 
        error(EX_OSERR, errno, "sigaddset");
 
392
        error_plus(EX_OSERR, errno, "sigaddset");
365
393
      }
366
394
      ret = sigaction(*sig, NULL, &old_action);
367
395
      if(ret == -1){
368
 
        error(EX_OSERR, errno, "sigaction");
 
396
        error_plus(EX_OSERR, errno, "sigaction");
369
397
      }
370
398
      if(old_action.sa_handler != SIG_IGN){
371
399
        ret = sigaction(*sig, &new_action, NULL);
372
400
        if(ret == -1){
373
 
          error(EX_OSERR, errno, "sigaction");
 
401
          error_plus(EX_OSERR, errno, "sigaction");
374
402
        }
375
403
      }
376
404
    }
395
423
  ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
396
424
  free(prompt);
397
425
  if(ret == -1){
398
 
    error(EX_OSERR, errno, "asprintf");
 
426
    error_plus(EX_OSERR, errno, "asprintf");
399
427
  }
400
428
  
401
429
  /* plymouth ask-for-password --prompt="$prompt" */
417
445
  const char **plymouthd_argv;
418
446
  pid_t pid = get_pid();
419
447
  if(pid == 0){
420
 
    error(0, 0, "plymouthd pid not found");
 
448
    error_plus(0, 0, "plymouthd pid not found");
421
449
    plymouthd_argv = plymouthd_default_argv;
422
450
  } else {
423
451
    plymouthd_argv = getargv(pid);