/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-10-18 08:47:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091018084740-fa1qgm22lg125r10
* plugins.d/splashy.c: Use exit codes from <sysexits.h>.

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-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 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() */
 
32
#include <stdio.h>              /* asprintf(), perror() */
33
33
#include <stdlib.h>             /* EXIT_FAILURE, free(),
34
34
                                   EXIT_SUCCESS */
35
35
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
43
43
                                   STDOUT_FILENO, _exit(),
44
44
                                   pause() */
45
45
#include <string.h>             /* memcmp() */
46
 
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
47
 
                                   ENOENT, ENAMETOOLONG, EMFILE,
48
 
                                   ENFILE, ENOMEM, ENOEXEC, EINVAL,
49
 
                                   E2BIG, EFAULT, EIO, ETXTBSY,
50
 
                                   EISDIR, ELIBBAD, EPERM, EINTR,
51
 
                                   ECHILD */
52
 
#include <error.h>              /* error() */
 
46
#include <errno.h>              /* errno */
53
47
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
54
48
                                   WEXITSTATUS() */
 
49
 
55
50
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
56
51
                                   EX_UNAVAILABLE */
57
52
 
110
105
    proc_dir = opendir("/proc");
111
106
    if(proc_dir == NULL){
112
107
      int e = errno;
113
 
      error(0, errno, "opendir");
 
108
      perror("opendir");
114
109
      switch(e){
115
110
      case EACCES:
116
111
      case ENOTDIR:
152
147
        char *exe_link;
153
148
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
154
149
        if(ret == -1){
155
 
          error(0, errno, "asprintf");
 
150
          perror("asprintf");
156
151
          exitstatus = EX_OSERR;
157
152
          goto failure;
158
153
        }
166
161
            continue;
167
162
          }
168
163
          int e = errno;
169
 
          error(0, errno, "lstat");
 
164
          perror("lstat");
170
165
          free(exe_link);
171
166
          switch(e){
172
167
          case EACCES:
214
209
    sigemptyset(&new_action.sa_mask);
215
210
    ret = sigaddset(&new_action.sa_mask, SIGINT);
216
211
    if(ret == -1){
217
 
      error(0, errno, "sigaddset");
 
212
      perror("sigaddset");
218
213
      exitstatus = EX_OSERR;
219
214
      goto failure;
220
215
    }
221
216
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
222
217
    if(ret == -1){
223
 
      error(0, errno, "sigaddset");
 
218
      perror("sigaddset");
224
219
      exitstatus = EX_OSERR;
225
220
      goto failure;
226
221
    }
227
222
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
228
223
    if(ret == -1){
229
 
      error(0, errno, "sigaddset");
 
224
      perror("sigaddset");
230
225
      exitstatus = EX_OSERR;
231
226
      goto failure;
232
227
    }
233
228
    ret = sigaction(SIGINT, NULL, &old_action);
234
229
    if(ret == -1){
235
 
      error(0, errno, "sigaction");
 
230
      perror("sigaction");
236
231
      exitstatus = EX_OSERR;
237
232
      goto failure;
238
233
    }
239
234
    if(old_action.sa_handler != SIG_IGN){
240
235
      ret = sigaction(SIGINT, &new_action, NULL);
241
236
      if(ret == -1){
242
 
        error(0, errno, "sigaction");
 
237
        perror("sigaction");
243
238
        exitstatus = EX_OSERR;
244
239
        goto failure;
245
240
      }
246
241
    }
247
242
    ret = sigaction(SIGHUP, NULL, &old_action);
248
243
    if(ret == -1){
249
 
      error(0, errno, "sigaction");
 
244
      perror("sigaction");
250
245
      exitstatus = EX_OSERR;
251
246
      goto failure;
252
247
    }
253
248
    if(old_action.sa_handler != SIG_IGN){
254
249
      ret = sigaction(SIGHUP, &new_action, NULL);
255
250
      if(ret == -1){
256
 
        error(0, errno, "sigaction");
 
251
        perror("sigaction");
257
252
        exitstatus = EX_OSERR;
258
253
        goto failure;
259
254
      }
260
255
    }
261
256
    ret = sigaction(SIGTERM, NULL, &old_action);
262
257
    if(ret == -1){
263
 
      error(0, errno, "sigaction");
 
258
      perror("sigaction");
264
259
      exitstatus = EX_OSERR;
265
260
      goto failure;
266
261
    }
267
262
    if(old_action.sa_handler != SIG_IGN){
268
263
      ret = sigaction(SIGTERM, &new_action, NULL);
269
264
      if(ret == -1){
270
 
        error(0, errno, "sigaction");
 
265
        perror("sigaction");
271
266
        exitstatus = EX_OSERR;
272
267
        goto failure;
273
268
      }
284
279
    goto failure;
285
280
  }
286
281
  if(splashy_command_pid == -1){
287
 
    error(0, errno, "fork");
 
282
    perror("fork");
288
283
    exitstatus = EX_OSERR;
289
284
    goto failure;
290
285
  }
293
288
    if(not interrupted_by_signal){
294
289
      const char splashy_command[] = "/sbin/splashy_update";
295
290
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
296
 
      int e = errno;
297
 
      error(0, errno, "execl");
298
 
      switch(e){
299
 
      case EACCES:
300
 
      case ENOENT:
301
 
      case ENOEXEC:
302
 
      case EINVAL:
303
 
        _exit(EX_UNAVAILABLE);
304
 
      case ENAMETOOLONG:
305
 
      case E2BIG:
306
 
      case ENOMEM:
307
 
      case EFAULT:
308
 
      case EIO:
309
 
      case EMFILE:
310
 
      case ENFILE:
311
 
      case ETXTBSY:
312
 
      default:
313
 
        _exit(EX_OSERR);
314
 
      case ENOTDIR:
315
 
      case ELOOP:
316
 
      case EISDIR:
317
 
      case ELIBBAD:
318
 
      case EPERM:
319
 
        _exit(EX_OSFILE);
320
 
      }
 
291
      perror("execl");
321
292
    }
322
293
    free(prompt);
323
294
    _exit(EXIT_FAILURE);
342
313
      goto failure;
343
314
    }
344
315
    if(ret == -1){
345
 
      error(0, errno, "waitpid");
 
316
      perror("waitpid");
346
317
      if(errno == ECHILD){
347
318
        splashy_command_pid = 0;
348
319
      }
380
351
         the real user ID (_mandos) */
381
352
      ret = setuid(geteuid());
382
353
      if(ret == -1){
383
 
        error(0, errno, "setuid");
 
354
        perror("setuid");
384
355
      }
385
356
      
386
357
      setsid();
387
358
      ret = chdir("/");
388
359
      if(ret == -1){
389
 
        error(0, errno, "chdir");
 
360
        perror("chdir");
390
361
      }
391
362
/*       if(fork() != 0){ */
392
363
/*      _exit(EXIT_SUCCESS); */
393
364
/*       } */
394
365
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
395
366
      if(ret == -1){
396
 
        error(0, errno, "dup2");
 
367
        perror("dup2");
397
368
        _exit(EX_OSERR);
398
369
      }
399
370
      
400
371
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
401
372
      {
402
373
        int e = errno;
403
 
        error(0, errno, "execl");
 
374
        perror("execl");
404
375
        switch(e){
405
376
        case EACCES:
406
377
        case ENOENT:
426
397
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
427
398
                                            &signal_action, NULL));
428
399
    if(ret == -1){
429
 
      error(0, errno, "sigaction");
 
400
      perror("sigaction");
430
401
    }
431
402
    do {
432
403
      ret = raise(signal_received);
433
404
    } while(ret != 0 and errno == EINTR);
434
405
    if(ret != 0){
435
 
      error(0, errno, "raise");
 
406
      perror("raise");
436
407
      abort();
437
408
    }
438
409
    TEMP_FAILURE_RETRY(pause());