/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: Björn Påhlsson
  • Date: 2011-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

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