/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 at bsnet
  • Date: 2011-12-24 23:17:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20111224231702-1ffgu6r02p0bz9co
* plugins.d/splashy.c (error_plus): Check format string.
* plugins.d/askpass-fifo.c (error_plus): - '' -
* plugins.d/plymouth.c (error_plus): - '' -
* plugins.d/password-prompt.c (error_plus): - '' -
* plugins.d/usplash.c (error_plus): - '' -

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