/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

* network-hooks.d: New directory.
* network-hooks.d/bridge: New example hook.
* network-hooks.d/bridge.conf: Config file for bridge example hook.

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