/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: 2009-10-18 08:47:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091018084740-fa1qgm22lg125r10
* plugins.d/splashy.c: Use exit codes from <sysexits.h>.

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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
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() */
47
 
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
48
 
                                   ENOENT, ENAMETOOLONG, EMFILE,
49
 
                                   ENFILE, ENOMEM, ENOEXEC, EINVAL,
50
 
                                   E2BIG, EFAULT, EIO, ETXTBSY,
51
 
                                   EISDIR, ELIBBAD, EPERM, EINTR,
52
 
                                   ECHILD */
53
 
#include <error.h>              /* error() */
 
45
#include <string.h>             /* memcmp() */
 
46
#include <errno.h>              /* errno */
54
47
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
55
48
                                   WEXITSTATUS() */
 
49
 
56
50
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
57
51
                                   EX_UNAVAILABLE */
58
 
#include <stdarg.h>             /* va_list, va_start(), ... */
59
52
 
60
53
sig_atomic_t interrupted_by_signal = 0;
61
54
int signal_received;
62
55
 
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
56
static void termination_handler(int signum){
88
57
  if(interrupted_by_signal){
89
58
    return;
136
105
    proc_dir = opendir("/proc");
137
106
    if(proc_dir == NULL){
138
107
      int e = errno;
139
 
      error_plus(0, errno, "opendir");
 
108
      perror("opendir");
140
109
      switch(e){
141
110
      case EACCES:
142
111
      case ENOTDIR:
178
147
        char *exe_link;
179
148
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
180
149
        if(ret == -1){
181
 
          error_plus(0, errno, "asprintf");
 
150
          perror("asprintf");
182
151
          exitstatus = EX_OSERR;
183
152
          goto failure;
184
153
        }
192
161
            continue;
193
162
          }
194
163
          int e = errno;
195
 
          error_plus(0, errno, "lstat");
 
164
          perror("lstat");
196
165
          free(exe_link);
197
166
          switch(e){
198
167
          case EACCES:
240
209
    sigemptyset(&new_action.sa_mask);
241
210
    ret = sigaddset(&new_action.sa_mask, SIGINT);
242
211
    if(ret == -1){
243
 
      error_plus(0, errno, "sigaddset");
 
212
      perror("sigaddset");
244
213
      exitstatus = EX_OSERR;
245
214
      goto failure;
246
215
    }
247
216
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
248
217
    if(ret == -1){
249
 
      error_plus(0, errno, "sigaddset");
 
218
      perror("sigaddset");
250
219
      exitstatus = EX_OSERR;
251
220
      goto failure;
252
221
    }
253
222
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
254
223
    if(ret == -1){
255
 
      error_plus(0, errno, "sigaddset");
 
224
      perror("sigaddset");
256
225
      exitstatus = EX_OSERR;
257
226
      goto failure;
258
227
    }
259
228
    ret = sigaction(SIGINT, NULL, &old_action);
260
229
    if(ret == -1){
261
 
      error_plus(0, errno, "sigaction");
 
230
      perror("sigaction");
262
231
      exitstatus = EX_OSERR;
263
232
      goto failure;
264
233
    }
265
234
    if(old_action.sa_handler != SIG_IGN){
266
235
      ret = sigaction(SIGINT, &new_action, NULL);
267
236
      if(ret == -1){
268
 
        error_plus(0, errno, "sigaction");
 
237
        perror("sigaction");
269
238
        exitstatus = EX_OSERR;
270
239
        goto failure;
271
240
      }
272
241
    }
273
242
    ret = sigaction(SIGHUP, NULL, &old_action);
274
243
    if(ret == -1){
275
 
      error_plus(0, errno, "sigaction");
 
244
      perror("sigaction");
276
245
      exitstatus = EX_OSERR;
277
246
      goto failure;
278
247
    }
279
248
    if(old_action.sa_handler != SIG_IGN){
280
249
      ret = sigaction(SIGHUP, &new_action, NULL);
281
250
      if(ret == -1){
282
 
        error_plus(0, errno, "sigaction");
 
251
        perror("sigaction");
283
252
        exitstatus = EX_OSERR;
284
253
        goto failure;
285
254
      }
286
255
    }
287
256
    ret = sigaction(SIGTERM, NULL, &old_action);
288
257
    if(ret == -1){
289
 
      error_plus(0, errno, "sigaction");
 
258
      perror("sigaction");
290
259
      exitstatus = EX_OSERR;
291
260
      goto failure;
292
261
    }
293
262
    if(old_action.sa_handler != SIG_IGN){
294
263
      ret = sigaction(SIGTERM, &new_action, NULL);
295
264
      if(ret == -1){
296
 
        error_plus(0, errno, "sigaction");
 
265
        perror("sigaction");
297
266
        exitstatus = EX_OSERR;
298
267
        goto failure;
299
268
      }
310
279
    goto failure;
311
280
  }
312
281
  if(splashy_command_pid == -1){
313
 
    error_plus(0, errno, "fork");
 
282
    perror("fork");
314
283
    exitstatus = EX_OSERR;
315
284
    goto failure;
316
285
  }
319
288
    if(not interrupted_by_signal){
320
289
      const char splashy_command[] = "/sbin/splashy_update";
321
290
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
322
 
      int e = errno;
323
 
      error_plus(0, errno, "execl");
324
 
      switch(e){
325
 
      case EACCES:
326
 
      case ENOENT:
327
 
      case ENOEXEC:
328
 
      case EINVAL:
329
 
        _exit(EX_UNAVAILABLE);
330
 
      case ENAMETOOLONG:
331
 
      case E2BIG:
332
 
      case ENOMEM:
333
 
      case EFAULT:
334
 
      case EIO:
335
 
      case EMFILE:
336
 
      case ENFILE:
337
 
      case ETXTBSY:
338
 
      default:
339
 
        _exit(EX_OSERR);
340
 
      case ENOTDIR:
341
 
      case ELOOP:
342
 
      case EISDIR:
343
 
#ifdef ELIBBAD
344
 
      case ELIBBAD:             /* Linux only */
345
 
#endif
346
 
      case EPERM:
347
 
        _exit(EX_OSFILE);
348
 
      }
 
291
      perror("execl");
349
292
    }
350
293
    free(prompt);
351
294
    _exit(EXIT_FAILURE);
370
313
      goto failure;
371
314
    }
372
315
    if(ret == -1){
373
 
      error_plus(0, errno, "waitpid");
 
316
      perror("waitpid");
374
317
      if(errno == ECHILD){
375
318
        splashy_command_pid = 0;
376
319
      }
408
351
         the real user ID (_mandos) */
409
352
      ret = setuid(geteuid());
410
353
      if(ret == -1){
411
 
        error_plus(0, errno, "setuid");
 
354
        perror("setuid");
412
355
      }
413
356
      
414
357
      setsid();
415
358
      ret = chdir("/");
416
359
      if(ret == -1){
417
 
        error_plus(0, errno, "chdir");
 
360
        perror("chdir");
418
361
      }
419
362
/*       if(fork() != 0){ */
420
363
/*      _exit(EXIT_SUCCESS); */
421
364
/*       } */
422
365
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
423
366
      if(ret == -1){
424
 
        error_plus(0, errno, "dup2");
 
367
        perror("dup2");
425
368
        _exit(EX_OSERR);
426
369
      }
427
370
      
428
371
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
429
372
      {
430
373
        int e = errno;
431
 
        error_plus(0, errno, "execl");
 
374
        perror("execl");
432
375
        switch(e){
433
376
        case EACCES:
434
377
        case ENOENT:
454
397
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
455
398
                                            &signal_action, NULL));
456
399
    if(ret == -1){
457
 
      error_plus(0, errno, "sigaction");
 
400
      perror("sigaction");
458
401
    }
459
402
    do {
460
403
      ret = raise(signal_received);
461
404
    } while(ret != 0 and errno == EINTR);
462
405
    if(ret != 0){
463
 
      error_plus(0, errno, "raise");
 
406
      perror("raise");
464
407
      abort();
465
408
    }
466
409
    TEMP_FAILURE_RETRY(pause());