/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: Björn Påhlsson
  • Date: 2011-10-02 19:18:24 UTC
  • mto: This revision was merged to the branch mainline in revision 505.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

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