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

  • Committer: Björn Påhlsson
  • Date: 2011-06-19 20:25:38 UTC
  • mto: (237.7.33 trunk)
  • mto: This revision was merged to the branch mainline in revision 284.
  • Revision ID: belorn@fukt.bsnet.se-20110619202538-0js072v8fso12u07
prepended mandos plugin to error messages in each plugin. Added a better way in TODO.

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(), vasprintf(), vprintf(), fprintf() */
 
32
#include <stdio.h>              /* asprintf() */
33
33
#include <stdlib.h>             /* EXIT_FAILURE, free(),
34
34
                                   EXIT_SUCCESS */
35
35
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
42
42
                                   sleep(), dup2() STDERR_FILENO,
43
43
                                   STDOUT_FILENO, _exit(),
44
44
                                   pause() */
45
 
#include <string.h>             /* memcmp(), strerror() */
 
45
#include <string.h>             /* memcmp() */
46
46
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
47
47
                                   ENOENT, ENAMETOOLONG, EMFILE,
48
48
                                   ENFILE, ENOMEM, ENOEXEC, EINVAL,
54
54
                                   WEXITSTATUS() */
55
55
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
56
56
                                   EX_UNAVAILABLE */
57
 
#include <stdarg.h>             /* va_list, va_start(), ... */
58
57
 
59
58
sig_atomic_t interrupted_by_signal = 0;
60
59
int signal_received;
61
60
 
62
 
/* Function to use when printing errors */
63
 
void error_plus(int status, int errnum, const char *formatstring, ...){
64
 
  va_list ap;
65
 
  char *text;
66
 
  int ret;
67
 
  
68
 
  va_start(ap, formatstring);
69
 
  ret = vasprintf(&text, formatstring, ap);
70
 
  if (ret == -1){
71
 
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
72
 
    vfprintf(stderr, formatstring, ap);
73
 
    fprintf(stderr, ": ");
74
 
    fprintf(stderr, "%s\n", strerror(errnum));
75
 
    error(status, errno, "vasprintf while printing error");
76
 
    return;
77
 
  }
78
 
  fprintf(stderr, "Mandos plugin ");
79
 
  error(status, errnum, "%s", text);
80
 
  free(text);
81
 
}
82
 
 
83
 
 
84
61
static void termination_handler(int signum){
85
62
  if(interrupted_by_signal){
86
63
    return;
133
110
    proc_dir = opendir("/proc");
134
111
    if(proc_dir == NULL){
135
112
      int e = errno;
136
 
      error_plus(0, errno, "opendir");
 
113
      error(0, errno, "opendir");
137
114
      switch(e){
138
115
      case EACCES:
139
116
      case ENOTDIR:
175
152
        char *exe_link;
176
153
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
177
154
        if(ret == -1){
178
 
          error_plus(0, errno, "asprintf");
 
155
          error(0, errno, "asprintf");
179
156
          exitstatus = EX_OSERR;
180
157
          goto failure;
181
158
        }
189
166
            continue;
190
167
          }
191
168
          int e = errno;
192
 
          error_plus(0, errno, "lstat");
 
169
          error(0, errno, "lstat");
193
170
          free(exe_link);
194
171
          switch(e){
195
172
          case EACCES:
237
214
    sigemptyset(&new_action.sa_mask);
238
215
    ret = sigaddset(&new_action.sa_mask, SIGINT);
239
216
    if(ret == -1){
240
 
      error_plus(0, errno, "sigaddset");
 
217
      error(0, errno, "sigaddset");
241
218
      exitstatus = EX_OSERR;
242
219
      goto failure;
243
220
    }
244
221
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
245
222
    if(ret == -1){
246
 
      error_plus(0, errno, "sigaddset");
 
223
      error(0, errno, "sigaddset");
247
224
      exitstatus = EX_OSERR;
248
225
      goto failure;
249
226
    }
250
227
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
251
228
    if(ret == -1){
252
 
      error_plus(0, errno, "sigaddset");
 
229
      error(0, errno, "sigaddset");
253
230
      exitstatus = EX_OSERR;
254
231
      goto failure;
255
232
    }
256
233
    ret = sigaction(SIGINT, NULL, &old_action);
257
234
    if(ret == -1){
258
 
      error_plus(0, errno, "sigaction");
 
235
      error(0, errno, "sigaction");
259
236
      exitstatus = EX_OSERR;
260
237
      goto failure;
261
238
    }
262
239
    if(old_action.sa_handler != SIG_IGN){
263
240
      ret = sigaction(SIGINT, &new_action, NULL);
264
241
      if(ret == -1){
265
 
        error_plus(0, errno, "sigaction");
 
242
        error(0, errno, "sigaction");
266
243
        exitstatus = EX_OSERR;
267
244
        goto failure;
268
245
      }
269
246
    }
270
247
    ret = sigaction(SIGHUP, NULL, &old_action);
271
248
    if(ret == -1){
272
 
      error_plus(0, errno, "sigaction");
 
249
      error(0, errno, "sigaction");
273
250
      exitstatus = EX_OSERR;
274
251
      goto failure;
275
252
    }
276
253
    if(old_action.sa_handler != SIG_IGN){
277
254
      ret = sigaction(SIGHUP, &new_action, NULL);
278
255
      if(ret == -1){
279
 
        error_plus(0, errno, "sigaction");
 
256
        error(0, errno, "sigaction");
280
257
        exitstatus = EX_OSERR;
281
258
        goto failure;
282
259
      }
283
260
    }
284
261
    ret = sigaction(SIGTERM, NULL, &old_action);
285
262
    if(ret == -1){
286
 
      error_plus(0, errno, "sigaction");
 
263
      error(0, errno, "sigaction");
287
264
      exitstatus = EX_OSERR;
288
265
      goto failure;
289
266
    }
290
267
    if(old_action.sa_handler != SIG_IGN){
291
268
      ret = sigaction(SIGTERM, &new_action, NULL);
292
269
      if(ret == -1){
293
 
        error_plus(0, errno, "sigaction");
 
270
        error(0, errno, "sigaction");
294
271
        exitstatus = EX_OSERR;
295
272
        goto failure;
296
273
      }
307
284
    goto failure;
308
285
  }
309
286
  if(splashy_command_pid == -1){
310
 
    error_plus(0, errno, "fork");
 
287
    error(0, errno, "fork");
311
288
    exitstatus = EX_OSERR;
312
289
    goto failure;
313
290
  }
317
294
      const char splashy_command[] = "/sbin/splashy_update";
318
295
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
319
296
      int e = errno;
320
 
      error_plus(0, errno, "execl");
 
297
      error(0, errno, "execl");
321
298
      switch(e){
322
299
      case EACCES:
323
300
      case ENOENT:
367
344
      goto failure;
368
345
    }
369
346
    if(ret == -1){
370
 
      error_plus(0, errno, "waitpid");
 
347
      error(0, errno, "waitpid");
371
348
      if(errno == ECHILD){
372
349
        splashy_command_pid = 0;
373
350
      }
405
382
         the real user ID (_mandos) */
406
383
      ret = setuid(geteuid());
407
384
      if(ret == -1){
408
 
        error_plus(0, errno, "setuid");
 
385
        error(0, errno, "setuid");
409
386
      }
410
387
      
411
388
      setsid();
412
389
      ret = chdir("/");
413
390
      if(ret == -1){
414
 
        error_plus(0, errno, "chdir");
 
391
        error(0, errno, "chdir");
415
392
      }
416
393
/*       if(fork() != 0){ */
417
394
/*      _exit(EXIT_SUCCESS); */
418
395
/*       } */
419
396
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
420
397
      if(ret == -1){
421
 
        error_plus(0, errno, "dup2");
 
398
        error(0, errno, "dup2");
422
399
        _exit(EX_OSERR);
423
400
      }
424
401
      
425
402
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
426
403
      {
427
404
        int e = errno;
428
 
        error_plus(0, errno, "execl");
 
405
        error(0, errno, "execl");
429
406
        switch(e){
430
407
        case EACCES:
431
408
        case ENOENT:
451
428
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
452
429
                                            &signal_action, NULL));
453
430
    if(ret == -1){
454
 
      error_plus(0, errno, "sigaction");
 
431
      error(0, errno, "sigaction");
455
432
    }
456
433
    do {
457
434
      ret = raise(signal_received);
458
435
    } while(ret != 0 and errno == EINTR);
459
436
    if(ret != 0){
460
 
      error_plus(0, errno, "raise");
 
437
      error(0, errno, "raise");
461
438
      abort();
462
439
    }
463
440
    TEMP_FAILURE_RETRY(pause());