/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
new approve/deny functionallity in mandos-monitor

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