/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 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:
29
29
                                   SIG_IGN, kill(), SIGKILL */
30
30
#include <stddef.h>             /* NULL */
31
31
#include <stdlib.h>             /* getenv() */
32
 
#include <stdio.h>              /* asprintf() */
 
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,
54
55
                                   WEXITSTATUS() */
55
56
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
56
57
                                   EX_UNAVAILABLE */
 
58
#include <stdarg.h>             /* va_list, va_start(), ... */
57
59
 
58
60
sig_atomic_t interrupted_by_signal = 0;
59
61
int signal_received;
60
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
 
61
87
static void termination_handler(int signum){
62
88
  if(interrupted_by_signal){
63
89
    return;
110
136
    proc_dir = opendir("/proc");
111
137
    if(proc_dir == NULL){
112
138
      int e = errno;
113
 
      error(0, errno, "opendir");
 
139
      error_plus(0, errno, "opendir");
114
140
      switch(e){
115
141
      case EACCES:
116
142
      case ENOTDIR:
152
178
        char *exe_link;
153
179
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
154
180
        if(ret == -1){
155
 
          error(0, errno, "asprintf");
 
181
          error_plus(0, errno, "asprintf");
156
182
          exitstatus = EX_OSERR;
157
183
          goto failure;
158
184
        }
166
192
            continue;
167
193
          }
168
194
          int e = errno;
169
 
          error(0, errno, "lstat");
 
195
          error_plus(0, errno, "lstat");
170
196
          free(exe_link);
171
197
          switch(e){
172
198
          case EACCES:
214
240
    sigemptyset(&new_action.sa_mask);
215
241
    ret = sigaddset(&new_action.sa_mask, SIGINT);
216
242
    if(ret == -1){
217
 
      error(0, errno, "sigaddset");
 
243
      error_plus(0, errno, "sigaddset");
218
244
      exitstatus = EX_OSERR;
219
245
      goto failure;
220
246
    }
221
247
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
222
248
    if(ret == -1){
223
 
      error(0, errno, "sigaddset");
 
249
      error_plus(0, errno, "sigaddset");
224
250
      exitstatus = EX_OSERR;
225
251
      goto failure;
226
252
    }
227
253
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
228
254
    if(ret == -1){
229
 
      error(0, errno, "sigaddset");
 
255
      error_plus(0, errno, "sigaddset");
230
256
      exitstatus = EX_OSERR;
231
257
      goto failure;
232
258
    }
233
259
    ret = sigaction(SIGINT, NULL, &old_action);
234
260
    if(ret == -1){
235
 
      error(0, errno, "sigaction");
 
261
      error_plus(0, errno, "sigaction");
236
262
      exitstatus = EX_OSERR;
237
263
      goto failure;
238
264
    }
239
265
    if(old_action.sa_handler != SIG_IGN){
240
266
      ret = sigaction(SIGINT, &new_action, NULL);
241
267
      if(ret == -1){
242
 
        error(0, errno, "sigaction");
 
268
        error_plus(0, errno, "sigaction");
243
269
        exitstatus = EX_OSERR;
244
270
        goto failure;
245
271
      }
246
272
    }
247
273
    ret = sigaction(SIGHUP, NULL, &old_action);
248
274
    if(ret == -1){
249
 
      error(0, errno, "sigaction");
 
275
      error_plus(0, errno, "sigaction");
250
276
      exitstatus = EX_OSERR;
251
277
      goto failure;
252
278
    }
253
279
    if(old_action.sa_handler != SIG_IGN){
254
280
      ret = sigaction(SIGHUP, &new_action, NULL);
255
281
      if(ret == -1){
256
 
        error(0, errno, "sigaction");
 
282
        error_plus(0, errno, "sigaction");
257
283
        exitstatus = EX_OSERR;
258
284
        goto failure;
259
285
      }
260
286
    }
261
287
    ret = sigaction(SIGTERM, NULL, &old_action);
262
288
    if(ret == -1){
263
 
      error(0, errno, "sigaction");
 
289
      error_plus(0, errno, "sigaction");
264
290
      exitstatus = EX_OSERR;
265
291
      goto failure;
266
292
    }
267
293
    if(old_action.sa_handler != SIG_IGN){
268
294
      ret = sigaction(SIGTERM, &new_action, NULL);
269
295
      if(ret == -1){
270
 
        error(0, errno, "sigaction");
 
296
        error_plus(0, errno, "sigaction");
271
297
        exitstatus = EX_OSERR;
272
298
        goto failure;
273
299
      }
284
310
    goto failure;
285
311
  }
286
312
  if(splashy_command_pid == -1){
287
 
    error(0, errno, "fork");
 
313
    error_plus(0, errno, "fork");
288
314
    exitstatus = EX_OSERR;
289
315
    goto failure;
290
316
  }
294
320
      const char splashy_command[] = "/sbin/splashy_update";
295
321
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
296
322
      int e = errno;
297
 
      error(0, errno, "execl");
 
323
      error_plus(0, errno, "execl");
298
324
      switch(e){
299
325
      case EACCES:
300
326
      case ENOENT:
344
370
      goto failure;
345
371
    }
346
372
    if(ret == -1){
347
 
      error(0, errno, "waitpid");
 
373
      error_plus(0, errno, "waitpid");
348
374
      if(errno == ECHILD){
349
375
        splashy_command_pid = 0;
350
376
      }
382
408
         the real user ID (_mandos) */
383
409
      ret = setuid(geteuid());
384
410
      if(ret == -1){
385
 
        error(0, errno, "setuid");
 
411
        error_plus(0, errno, "setuid");
386
412
      }
387
413
      
388
414
      setsid();
389
415
      ret = chdir("/");
390
416
      if(ret == -1){
391
 
        error(0, errno, "chdir");
 
417
        error_plus(0, errno, "chdir");
392
418
      }
393
419
/*       if(fork() != 0){ */
394
420
/*      _exit(EXIT_SUCCESS); */
395
421
/*       } */
396
422
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
397
423
      if(ret == -1){
398
 
        error(0, errno, "dup2");
 
424
        error_plus(0, errno, "dup2");
399
425
        _exit(EX_OSERR);
400
426
      }
401
427
      
402
428
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
403
429
      {
404
430
        int e = errno;
405
 
        error(0, errno, "execl");
 
431
        error_plus(0, errno, "execl");
406
432
        switch(e){
407
433
        case EACCES:
408
434
        case ENOENT:
428
454
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
429
455
                                            &signal_action, NULL));
430
456
    if(ret == -1){
431
 
      error(0, errno, "sigaction");
 
457
      error_plus(0, errno, "sigaction");
432
458
    }
433
459
    do {
434
460
      ret = raise(signal_received);
435
461
    } while(ret != 0 and errno == EINTR);
436
462
    if(ret != 0){
437
 
      error(0, errno, "raise");
 
463
      error_plus(0, errno, "raise");
438
464
      abort();
439
465
    }
440
466
    TEMP_FAILURE_RETRY(pause());