/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-09-16 23:28:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090916232839-3o7i8qmcdcz5j1ya
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
                 "$syslog", thanks to Petter Reinholdtsen
                 <pere@hungry.com> (Debian bug #546928).

* initramfs-tools-script: Removed erroneous comment.

* plugins.d/askpass-fifo.c: Removed TEMP_FAILURE_RETRY since it is
                            not needed.

* plugins.d/mandos-client.c (main): Bug fix: Initialize
                                    "old_sigterm_action".

* plugins.d/splashy.c (main): Bug fix: really check return value from
                              "sigaddset".  Fix some warnings on
                              64-bit systems.

* plugins.d/usplash.c (termination_handler, main): Save received
                                                   signal and
                                                   re-raise it on
                                                   exit.
  (usplash_write): Do not close FIFO, instead, take an additional file
                   descriptor pointer to it and open only when needed
                   (all callers changed).  Abort immediately on EINTR.
                   Bug fix:  Add NUL byte on single-word commands.
                   Ignore "interrupted_by_signal".
  (makeprompt, find_usplash): New; broken out from "main()".
  (find_usplash): Bug fix: close /proc/<pid>/cmdline FD on error.
  (main): Reorganized to jump to a new "failure" label on any error.
          Bug fix: check return values from sigaddset.
          New variable "usplash_accessed" to flag if usplash(8) needs
          to be killed and restarted.  Removed the "an_error_occured"
          variable.

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
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() */
56
 
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
57
 
                                   EX_UNAVAILABLE */
58
 
#include <stdarg.h>             /* va_list, va_start(), ... */
59
49
 
60
50
sig_atomic_t interrupted_by_signal = 0;
61
51
int signal_received;
62
52
 
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
53
static void termination_handler(int signum){
88
54
  if(interrupted_by_signal){
89
55
    return;
99
65
  DIR *proc_dir = NULL;
100
66
  pid_t splashy_pid = 0;
101
67
  pid_t splashy_command_pid = 0;
102
 
  int exitstatus = EXIT_FAILURE;
103
68
  
104
69
  /* Create prompt string */
105
70
  {
125
90
    }
126
91
    if(ret == -1){
127
92
      prompt = NULL;
128
 
      exitstatus = EX_OSERR;
129
93
      goto failure;
130
94
    }
131
95
  }
135
99
    const char splashy_name[] = "/sbin/splashy";
136
100
    proc_dir = opendir("/proc");
137
101
    if(proc_dir == NULL){
138
 
      int e = errno;
139
 
      error_plus(0, errno, "opendir");
140
 
      switch(e){
141
 
      case EACCES:
142
 
      case ENOTDIR:
143
 
      case ELOOP:
144
 
      case ENOENT:
145
 
      default:
146
 
        exitstatus = EX_OSFILE;
147
 
        break;
148
 
      case ENAMETOOLONG:
149
 
      case EMFILE:
150
 
      case ENFILE:
151
 
      case ENOMEM:
152
 
        exitstatus = EX_OSERR;
153
 
        break;
154
 
      }
 
102
      perror("opendir");
155
103
      goto failure;
156
104
    }
157
105
    for(struct dirent *proc_ent = readdir(proc_dir);
178
126
        char *exe_link;
179
127
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
180
128
        if(ret == -1){
181
 
          error_plus(0, errno, "asprintf");
182
 
          exitstatus = EX_OSERR;
 
129
          perror("asprintf");
183
130
          goto failure;
184
131
        }
185
132
        
191
138
            free(exe_link);
192
139
            continue;
193
140
          }
194
 
          int e = errno;
195
 
          error_plus(0, errno, "lstat");
 
141
          perror("lstat");
196
142
          free(exe_link);
197
 
          switch(e){
198
 
          case EACCES:
199
 
          case ENOTDIR:
200
 
          case ELOOP:
201
 
          default:
202
 
            exitstatus = EX_OSFILE;
203
 
            break;
204
 
          case ENAMETOOLONG:
205
 
            exitstatus = EX_OSERR;
206
 
            break;
207
 
          }
208
143
          goto failure;
209
144
        }
210
145
        if(not S_ISLNK(exe_stat.st_mode)
228
163
    proc_dir = NULL;
229
164
  }
230
165
  if(splashy_pid == 0){
231
 
    exitstatus = EX_UNAVAILABLE;
232
166
    goto failure;
233
167
  }
234
168
  
240
174
    sigemptyset(&new_action.sa_mask);
241
175
    ret = sigaddset(&new_action.sa_mask, SIGINT);
242
176
    if(ret == -1){
243
 
      error_plus(0, errno, "sigaddset");
244
 
      exitstatus = EX_OSERR;
 
177
      perror("sigaddset");
245
178
      goto failure;
246
179
    }
247
180
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
248
181
    if(ret == -1){
249
 
      error_plus(0, errno, "sigaddset");
250
 
      exitstatus = EX_OSERR;
 
182
      perror("sigaddset");
251
183
      goto failure;
252
184
    }
253
185
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
254
186
    if(ret == -1){
255
 
      error_plus(0, errno, "sigaddset");
256
 
      exitstatus = EX_OSERR;
 
187
      perror("sigaddset");
257
188
      goto failure;
258
189
    }
259
190
    ret = sigaction(SIGINT, NULL, &old_action);
260
191
    if(ret == -1){
261
 
      error_plus(0, errno, "sigaction");
262
 
      exitstatus = EX_OSERR;
 
192
      perror("sigaction");
263
193
      goto failure;
264
194
    }
265
195
    if(old_action.sa_handler != SIG_IGN){
266
196
      ret = sigaction(SIGINT, &new_action, NULL);
267
197
      if(ret == -1){
268
 
        error_plus(0, errno, "sigaction");
269
 
        exitstatus = EX_OSERR;
 
198
        perror("sigaction");
270
199
        goto failure;
271
200
      }
272
201
    }
273
202
    ret = sigaction(SIGHUP, NULL, &old_action);
274
203
    if(ret == -1){
275
 
      error_plus(0, errno, "sigaction");
276
 
      exitstatus = EX_OSERR;
 
204
      perror("sigaction");
277
205
      goto failure;
278
206
    }
279
207
    if(old_action.sa_handler != SIG_IGN){
280
208
      ret = sigaction(SIGHUP, &new_action, NULL);
281
209
      if(ret == -1){
282
 
        error_plus(0, errno, "sigaction");
283
 
        exitstatus = EX_OSERR;
 
210
        perror("sigaction");
284
211
        goto failure;
285
212
      }
286
213
    }
287
214
    ret = sigaction(SIGTERM, NULL, &old_action);
288
215
    if(ret == -1){
289
 
      error_plus(0, errno, "sigaction");
290
 
      exitstatus = EX_OSERR;
 
216
      perror("sigaction");
291
217
      goto failure;
292
218
    }
293
219
    if(old_action.sa_handler != SIG_IGN){
294
220
      ret = sigaction(SIGTERM, &new_action, NULL);
295
221
      if(ret == -1){
296
 
        error_plus(0, errno, "sigaction");
297
 
        exitstatus = EX_OSERR;
 
222
        perror("sigaction");
298
223
        goto failure;
299
224
      }
300
225
    }
310
235
    goto failure;
311
236
  }
312
237
  if(splashy_command_pid == -1){
313
 
    error_plus(0, errno, "fork");
314
 
    exitstatus = EX_OSERR;
 
238
    perror("fork");
315
239
    goto failure;
316
240
  }
317
241
  /* Child */
319
243
    if(not interrupted_by_signal){
320
244
      const char splashy_command[] = "/sbin/splashy_update";
321
245
      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
 
      }
 
246
      perror("execl");
349
247
    }
350
248
    free(prompt);
351
249
    _exit(EXIT_FAILURE);
370
268
      goto failure;
371
269
    }
372
270
    if(ret == -1){
373
 
      error_plus(0, errno, "waitpid");
 
271
      perror("waitpid");
374
272
      if(errno == ECHILD){
375
273
        splashy_command_pid = 0;
376
274
      }
408
306
         the real user ID (_mandos) */
409
307
      ret = setuid(geteuid());
410
308
      if(ret == -1){
411
 
        error_plus(0, errno, "setuid");
 
309
        perror("setuid");
412
310
      }
413
311
      
414
312
      setsid();
415
313
      ret = chdir("/");
416
314
      if(ret == -1){
417
 
        error_plus(0, errno, "chdir");
 
315
        perror("chdir");
418
316
      }
419
317
/*       if(fork() != 0){ */
420
318
/*      _exit(EXIT_SUCCESS); */
421
319
/*       } */
422
320
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
423
321
      if(ret == -1){
424
 
        error_plus(0, errno, "dup2");
425
 
        _exit(EX_OSERR);
 
322
        perror("dup2");
 
323
        _exit(EXIT_FAILURE);
426
324
      }
427
 
      
 
325
    
428
326
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
429
 
      {
430
 
        int e = errno;
431
 
        error_plus(0, errno, "execl");
432
 
        switch(e){
433
 
        case EACCES:
434
 
        case ENOENT:
435
 
        case ENOEXEC:
436
 
        default:
437
 
          _exit(EX_UNAVAILABLE);
438
 
        case ENAMETOOLONG:
439
 
        case E2BIG:
440
 
        case ENOMEM:
441
 
          _exit(EX_OSERR);
442
 
        case ENOTDIR:
443
 
        case ELOOP:
444
 
          _exit(EX_OSFILE);
445
 
        }
446
 
      }
 
327
      perror("execl");
 
328
      _exit(EXIT_FAILURE);
447
329
    }
448
330
  }
449
331
  
454
336
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
455
337
                                            &signal_action, NULL));
456
338
    if(ret == -1){
457
 
      error_plus(0, errno, "sigaction");
 
339
      perror("sigaction");
458
340
    }
459
341
    do {
460
342
      ret = raise(signal_received);
461
343
    } while(ret != 0 and errno == EINTR);
462
344
    if(ret != 0){
463
 
      error_plus(0, errno, "raise");
 
345
      perror("raise");
464
346
      abort();
465
347
    }
466
348
    TEMP_FAILURE_RETRY(pause());
467
349
  }
468
350
  
469
 
  return exitstatus;
 
351
  return EXIT_FAILURE;
470
352
}