/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/splashy.c

  • Committer: teddy at bsnet
  • Date: 2010-09-09 18:16:14 UTC
  • mfrom: (237.2.35 mandos-empty-device)
  • Revision ID: teddy@fukt.bsnet.se-20100909181614-oanlmvkzsiodbo3c
Merge in branch to interpret an empty device name to mean
"autodetect".

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Splashy - Read a password from splashy and output it
4
4
 * 
5
 
 * Copyright © 2008-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 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
29
29
                                   SIG_IGN, kill(), SIGKILL */
30
30
#include <stddef.h>             /* NULL */
31
31
#include <stdlib.h>             /* getenv() */
32
 
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(),
33
 
                                   fprintf() */
 
32
#include <stdio.h>              /* asprintf(), perror() */
34
33
#include <stdlib.h>             /* EXIT_FAILURE, free(),
35
34
                                   EXIT_SUCCESS */
36
35
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
43
42
                                   sleep(), dup2() STDERR_FILENO,
44
43
                                   STDOUT_FILENO, _exit(),
45
44
                                   pause() */
46
 
#include <string.h>             /* memcmp(), strerror() */
 
45
#include <string.h>             /* memcmp() */
47
46
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
48
47
                                   ENOENT, ENAMETOOLONG, EMFILE,
49
48
                                   ENFILE, ENOMEM, ENOEXEC, EINVAL,
50
49
                                   E2BIG, EFAULT, EIO, ETXTBSY,
51
50
                                   EISDIR, ELIBBAD, EPERM, EINTR,
52
51
                                   ECHILD */
53
 
#include <error.h>              /* error() */
54
52
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
55
53
                                   WEXITSTATUS() */
56
54
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
57
55
                                   EX_UNAVAILABLE */
58
 
#include <stdarg.h>             /* va_list, va_start(), ... */
59
56
 
60
57
sig_atomic_t interrupted_by_signal = 0;
61
58
int signal_received;
62
59
 
63
 
/* Function to use when printing errors */
64
 
void error_plus(int status, int errnum, const char *formatstring,
65
 
                ...){
66
 
  va_list ap;
67
 
  char *text;
68
 
  int ret;
69
 
  
70
 
  va_start(ap, formatstring);
71
 
  ret = vasprintf(&text, formatstring, ap);
72
 
  if (ret == -1){
73
 
    fprintf(stderr, "Mandos plugin %s: ",
74
 
            program_invocation_short_name);
75
 
    vfprintf(stderr, formatstring, ap);
76
 
    fprintf(stderr, ": ");
77
 
    fprintf(stderr, "%s\n", strerror(errnum));
78
 
    error(status, errno, "vasprintf while printing error");
79
 
    return;
80
 
  }
81
 
  fprintf(stderr, "Mandos plugin ");
82
 
  error(status, errnum, "%s", text);
83
 
  free(text);
84
 
}
85
 
 
86
 
 
87
60
static void termination_handler(int signum){
88
61
  if(interrupted_by_signal){
89
62
    return;
136
109
    proc_dir = opendir("/proc");
137
110
    if(proc_dir == NULL){
138
111
      int e = errno;
139
 
      error_plus(0, errno, "opendir");
 
112
      perror("opendir");
140
113
      switch(e){
141
114
      case EACCES:
142
115
      case ENOTDIR:
178
151
        char *exe_link;
179
152
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
180
153
        if(ret == -1){
181
 
          error_plus(0, errno, "asprintf");
 
154
          perror("asprintf");
182
155
          exitstatus = EX_OSERR;
183
156
          goto failure;
184
157
        }
192
165
            continue;
193
166
          }
194
167
          int e = errno;
195
 
          error_plus(0, errno, "lstat");
 
168
          perror("lstat");
196
169
          free(exe_link);
197
170
          switch(e){
198
171
          case EACCES:
240
213
    sigemptyset(&new_action.sa_mask);
241
214
    ret = sigaddset(&new_action.sa_mask, SIGINT);
242
215
    if(ret == -1){
243
 
      error_plus(0, errno, "sigaddset");
 
216
      perror("sigaddset");
244
217
      exitstatus = EX_OSERR;
245
218
      goto failure;
246
219
    }
247
220
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
248
221
    if(ret == -1){
249
 
      error_plus(0, errno, "sigaddset");
 
222
      perror("sigaddset");
250
223
      exitstatus = EX_OSERR;
251
224
      goto failure;
252
225
    }
253
226
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
254
227
    if(ret == -1){
255
 
      error_plus(0, errno, "sigaddset");
 
228
      perror("sigaddset");
256
229
      exitstatus = EX_OSERR;
257
230
      goto failure;
258
231
    }
259
232
    ret = sigaction(SIGINT, NULL, &old_action);
260
233
    if(ret == -1){
261
 
      error_plus(0, errno, "sigaction");
 
234
      perror("sigaction");
262
235
      exitstatus = EX_OSERR;
263
236
      goto failure;
264
237
    }
265
238
    if(old_action.sa_handler != SIG_IGN){
266
239
      ret = sigaction(SIGINT, &new_action, NULL);
267
240
      if(ret == -1){
268
 
        error_plus(0, errno, "sigaction");
 
241
        perror("sigaction");
269
242
        exitstatus = EX_OSERR;
270
243
        goto failure;
271
244
      }
272
245
    }
273
246
    ret = sigaction(SIGHUP, NULL, &old_action);
274
247
    if(ret == -1){
275
 
      error_plus(0, errno, "sigaction");
 
248
      perror("sigaction");
276
249
      exitstatus = EX_OSERR;
277
250
      goto failure;
278
251
    }
279
252
    if(old_action.sa_handler != SIG_IGN){
280
253
      ret = sigaction(SIGHUP, &new_action, NULL);
281
254
      if(ret == -1){
282
 
        error_plus(0, errno, "sigaction");
 
255
        perror("sigaction");
283
256
        exitstatus = EX_OSERR;
284
257
        goto failure;
285
258
      }
286
259
    }
287
260
    ret = sigaction(SIGTERM, NULL, &old_action);
288
261
    if(ret == -1){
289
 
      error_plus(0, errno, "sigaction");
 
262
      perror("sigaction");
290
263
      exitstatus = EX_OSERR;
291
264
      goto failure;
292
265
    }
293
266
    if(old_action.sa_handler != SIG_IGN){
294
267
      ret = sigaction(SIGTERM, &new_action, NULL);
295
268
      if(ret == -1){
296
 
        error_plus(0, errno, "sigaction");
 
269
        perror("sigaction");
297
270
        exitstatus = EX_OSERR;
298
271
        goto failure;
299
272
      }
310
283
    goto failure;
311
284
  }
312
285
  if(splashy_command_pid == -1){
313
 
    error_plus(0, errno, "fork");
 
286
    perror("fork");
314
287
    exitstatus = EX_OSERR;
315
288
    goto failure;
316
289
  }
320
293
      const char splashy_command[] = "/sbin/splashy_update";
321
294
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
322
295
      int e = errno;
323
 
      error_plus(0, errno, "execl");
 
296
      perror("execl");
324
297
      switch(e){
325
298
      case EACCES:
326
299
      case ENOENT:
340
313
      case ENOTDIR:
341
314
      case ELOOP:
342
315
      case EISDIR:
343
 
#ifdef ELIBBAD
344
 
      case ELIBBAD:             /* Linux only */
345
 
#endif
 
316
      case ELIBBAD:
346
317
      case EPERM:
347
318
        _exit(EX_OSFILE);
348
319
      }
370
341
      goto failure;
371
342
    }
372
343
    if(ret == -1){
373
 
      error_plus(0, errno, "waitpid");
 
344
      perror("waitpid");
374
345
      if(errno == ECHILD){
375
346
        splashy_command_pid = 0;
376
347
      }
408
379
         the real user ID (_mandos) */
409
380
      ret = setuid(geteuid());
410
381
      if(ret == -1){
411
 
        error_plus(0, errno, "setuid");
 
382
        perror("setuid");
412
383
      }
413
384
      
414
385
      setsid();
415
386
      ret = chdir("/");
416
387
      if(ret == -1){
417
 
        error_plus(0, errno, "chdir");
 
388
        perror("chdir");
418
389
      }
419
390
/*       if(fork() != 0){ */
420
391
/*      _exit(EXIT_SUCCESS); */
421
392
/*       } */
422
393
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
423
394
      if(ret == -1){
424
 
        error_plus(0, errno, "dup2");
 
395
        perror("dup2");
425
396
        _exit(EX_OSERR);
426
397
      }
427
398
      
428
399
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
429
400
      {
430
401
        int e = errno;
431
 
        error_plus(0, errno, "execl");
 
402
        perror("execl");
432
403
        switch(e){
433
404
        case EACCES:
434
405
        case ENOENT:
454
425
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
455
426
                                            &signal_action, NULL));
456
427
    if(ret == -1){
457
 
      error_plus(0, errno, "sigaction");
 
428
      perror("sigaction");
458
429
    }
459
430
    do {
460
431
      ret = raise(signal_received);
461
432
    } while(ret != 0 and errno == EINTR);
462
433
    if(ret != 0){
463
 
      error_plus(0, errno, "raise");
 
434
      perror("raise");
464
435
      abort();
465
436
    }
466
437
    TEMP_FAILURE_RETRY(pause());