/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-23 22:27:15 UTC
  • mto: (237.7.33 trunk)
  • mto: This revision was merged to the branch mainline in revision 284.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

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