/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-17 04:16:32 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090917041632-7m1rm83pxguw5zw7
* plugins.d/usplash.c (main): Bug fix: only close FIFO if opened.

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