/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-15 16:48:03 UTC
  • mto: (237.4.29 release)
  • mto: This revision was merged to the branch mainline in revision 518.
  • Revision ID: belorn@fukt.bsnet.se-20111015164803-61q3hzrv91d042mb
Tags: version-1.4.1-1
* Makefile (version): Changed to "1.4.1".
* NEWS (Version 1.4.1): New entry.
* debian/changelog (1.4.1-1): - '' -

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