/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-19 17:41:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090919174118-274yt9wptmtx0ykn
* plugins.d/password-prompt.c (main): Fix "-Wconversion" warning.

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