/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

Merge from trunk.  Just a minor release to make it lintian clean.

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-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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() */
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() */
 
45
#include <string.h>             /* memcmp() */
47
46
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
48
47
                                   ENOENT, ENAMETOOLONG, EMFILE,
49
48
                                   ENFILE, ENOMEM, ENOEXEC, EINVAL,
55
54
                                   WEXITSTATUS() */
56
55
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
57
56
                                   EX_UNAVAILABLE */
58
 
#include <stdarg.h>             /* va_list, va_start(), ... */
59
57
 
60
58
sig_atomic_t interrupted_by_signal = 0;
61
59
int signal_received;
62
60
 
63
 
/* Function to use when printing errors */
64
 
__attribute__((format (gnu_printf, 3, 4)))
65
 
void error_plus(int status, int errnum, const char *formatstring,
66
 
                ...){
67
 
  va_list ap;
68
 
  char *text;
69
 
  int ret;
70
 
  
71
 
  va_start(ap, formatstring);
72
 
  ret = vasprintf(&text, formatstring, ap);
73
 
  if(ret == -1){
74
 
    fprintf(stderr, "Mandos plugin %s: ",
75
 
            program_invocation_short_name);
76
 
    vfprintf(stderr, formatstring, ap);
77
 
    fprintf(stderr, ": ");
78
 
    fprintf(stderr, "%s\n", strerror(errnum));
79
 
    error(status, errno, "vasprintf while printing error");
80
 
    return;
81
 
  }
82
 
  fprintf(stderr, "Mandos plugin ");
83
 
  error(status, errnum, "%s", text);
84
 
  free(text);
85
 
}
86
 
 
87
 
 
88
61
static void termination_handler(int signum){
89
62
  if(interrupted_by_signal){
90
63
    return;
137
110
    proc_dir = opendir("/proc");
138
111
    if(proc_dir == NULL){
139
112
      int e = errno;
140
 
      error_plus(0, errno, "opendir");
 
113
      error(0, errno, "opendir");
141
114
      switch(e){
142
115
      case EACCES:
143
116
      case ENOTDIR:
179
152
        char *exe_link;
180
153
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
181
154
        if(ret == -1){
182
 
          error_plus(0, errno, "asprintf");
 
155
          error(0, errno, "asprintf");
183
156
          exitstatus = EX_OSERR;
184
157
          goto failure;
185
158
        }
193
166
            continue;
194
167
          }
195
168
          int e = errno;
196
 
          error_plus(0, errno, "lstat");
 
169
          error(0, errno, "lstat");
197
170
          free(exe_link);
198
171
          switch(e){
199
172
          case EACCES:
241
214
    sigemptyset(&new_action.sa_mask);
242
215
    ret = sigaddset(&new_action.sa_mask, SIGINT);
243
216
    if(ret == -1){
244
 
      error_plus(0, errno, "sigaddset");
 
217
      error(0, errno, "sigaddset");
245
218
      exitstatus = EX_OSERR;
246
219
      goto failure;
247
220
    }
248
221
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
249
222
    if(ret == -1){
250
 
      error_plus(0, errno, "sigaddset");
 
223
      error(0, errno, "sigaddset");
251
224
      exitstatus = EX_OSERR;
252
225
      goto failure;
253
226
    }
254
227
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
255
228
    if(ret == -1){
256
 
      error_plus(0, errno, "sigaddset");
 
229
      error(0, errno, "sigaddset");
257
230
      exitstatus = EX_OSERR;
258
231
      goto failure;
259
232
    }
260
233
    ret = sigaction(SIGINT, NULL, &old_action);
261
234
    if(ret == -1){
262
 
      error_plus(0, errno, "sigaction");
 
235
      error(0, errno, "sigaction");
263
236
      exitstatus = EX_OSERR;
264
237
      goto failure;
265
238
    }
266
239
    if(old_action.sa_handler != SIG_IGN){
267
240
      ret = sigaction(SIGINT, &new_action, NULL);
268
241
      if(ret == -1){
269
 
        error_plus(0, errno, "sigaction");
 
242
        error(0, errno, "sigaction");
270
243
        exitstatus = EX_OSERR;
271
244
        goto failure;
272
245
      }
273
246
    }
274
247
    ret = sigaction(SIGHUP, NULL, &old_action);
275
248
    if(ret == -1){
276
 
      error_plus(0, errno, "sigaction");
 
249
      error(0, errno, "sigaction");
277
250
      exitstatus = EX_OSERR;
278
251
      goto failure;
279
252
    }
280
253
    if(old_action.sa_handler != SIG_IGN){
281
254
      ret = sigaction(SIGHUP, &new_action, NULL);
282
255
      if(ret == -1){
283
 
        error_plus(0, errno, "sigaction");
 
256
        error(0, errno, "sigaction");
284
257
        exitstatus = EX_OSERR;
285
258
        goto failure;
286
259
      }
287
260
    }
288
261
    ret = sigaction(SIGTERM, NULL, &old_action);
289
262
    if(ret == -1){
290
 
      error_plus(0, errno, "sigaction");
 
263
      error(0, errno, "sigaction");
291
264
      exitstatus = EX_OSERR;
292
265
      goto failure;
293
266
    }
294
267
    if(old_action.sa_handler != SIG_IGN){
295
268
      ret = sigaction(SIGTERM, &new_action, NULL);
296
269
      if(ret == -1){
297
 
        error_plus(0, errno, "sigaction");
 
270
        error(0, errno, "sigaction");
298
271
        exitstatus = EX_OSERR;
299
272
        goto failure;
300
273
      }
311
284
    goto failure;
312
285
  }
313
286
  if(splashy_command_pid == -1){
314
 
    error_plus(0, errno, "fork");
 
287
    error(0, errno, "fork");
315
288
    exitstatus = EX_OSERR;
316
289
    goto failure;
317
290
  }
321
294
      const char splashy_command[] = "/sbin/splashy_update";
322
295
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
323
296
      int e = errno;
324
 
      error_plus(0, errno, "execl");
 
297
      error(0, errno, "execl");
325
298
      switch(e){
326
299
      case EACCES:
327
300
      case ENOENT:
341
314
      case ENOTDIR:
342
315
      case ELOOP:
343
316
      case EISDIR:
344
 
#ifdef ELIBBAD
345
 
      case ELIBBAD:             /* Linux only */
346
 
#endif
 
317
      case ELIBBAD:
347
318
      case EPERM:
348
319
        _exit(EX_OSFILE);
349
320
      }
371
342
      goto failure;
372
343
    }
373
344
    if(ret == -1){
374
 
      error_plus(0, errno, "waitpid");
 
345
      error(0, errno, "waitpid");
375
346
      if(errno == ECHILD){
376
347
        splashy_command_pid = 0;
377
348
      }
409
380
         the real user ID (_mandos) */
410
381
      ret = setuid(geteuid());
411
382
      if(ret == -1){
412
 
        error_plus(0, errno, "setuid");
 
383
        error(0, errno, "setuid");
413
384
      }
414
385
      
415
386
      setsid();
416
387
      ret = chdir("/");
417
388
      if(ret == -1){
418
 
        error_plus(0, errno, "chdir");
 
389
        error(0, errno, "chdir");
419
390
      }
420
391
/*       if(fork() != 0){ */
421
392
/*      _exit(EXIT_SUCCESS); */
422
393
/*       } */
423
394
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
424
395
      if(ret == -1){
425
 
        error_plus(0, errno, "dup2");
 
396
        error(0, errno, "dup2");
426
397
        _exit(EX_OSERR);
427
398
      }
428
399
      
429
400
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
430
401
      {
431
402
        int e = errno;
432
 
        error_plus(0, errno, "execl");
 
403
        error(0, errno, "execl");
433
404
        switch(e){
434
405
        case EACCES:
435
406
        case ENOENT:
455
426
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
456
427
                                            &signal_action, NULL));
457
428
    if(ret == -1){
458
 
      error_plus(0, errno, "sigaction");
 
429
      error(0, errno, "sigaction");
459
430
    }
460
431
    do {
461
432
      ret = raise(signal_received);
462
433
    } while(ret != 0 and errno == EINTR);
463
434
    if(ret != 0){
464
 
      error_plus(0, errno, "raise");
 
435
      error(0, errno, "raise");
465
436
      abort();
466
437
    }
467
438
    TEMP_FAILURE_RETRY(pause());