/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-06-23 22:27:15 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

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