/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:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Passprompt - Read a password from splashy and output it
 
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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
23
 
 * <https://www.fukt.bsnet.se/~teddy/>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
 
#define _GNU_SOURCE             /* asprintf() */
 
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
27
26
#include <signal.h>             /* sig_atomic_t, struct sigaction,
28
27
                                   sigemptyset(), sigaddset(), SIGINT,
29
28
                                   SIGHUP, SIGTERM, sigaction,
30
29
                                   SIG_IGN, kill(), SIGKILL */
31
30
#include <stddef.h>             /* NULL */
32
31
#include <stdlib.h>             /* getenv() */
33
 
#include <stdio.h>              /* asprintf(), perror() */
34
 
#include <stdlib.h>             /* EXIT_FAILURE, free(), strtoul(),
 
32
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(), fprintf() */
 
33
#include <stdlib.h>             /* EXIT_FAILURE, free(),
35
34
                                   EXIT_SUCCESS */
36
35
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
37
36
                                   ssize_t */
38
37
#include <dirent.h>             /* opendir(), readdir(), closedir() */
 
38
#include <inttypes.h>           /* intmax_t, strtoimax() */
39
39
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
40
40
#include <iso646.h>             /* not, or, and */
41
41
#include <unistd.h>             /* readlink(), fork(), execl(),
42
42
                                   sleep(), dup2() STDERR_FILENO,
43
 
                                   STDOUT_FILENO, _exit() */
44
 
#include <string.h>             /* memcmp() */
45
 
#include <errno.h>              /* errno */
 
43
                                   STDOUT_FILENO, _exit(),
 
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() */
46
53
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
47
54
                                   WEXITSTATUS() */
 
55
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
 
56
                                   EX_UNAVAILABLE */
 
57
#include <stdarg.h>             /* va_list, va_start(), ... */
48
58
 
49
59
sig_atomic_t interrupted_by_signal = 0;
50
 
 
51
 
static void termination_handler(__attribute__((unused))int signum){
 
60
int signal_received;
 
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
 
 
84
static void termination_handler(int signum){
 
85
  if(interrupted_by_signal){
 
86
    return;
 
87
  }
52
88
  interrupted_by_signal = 1;
 
89
  signal_received = signum;
53
90
}
54
91
 
55
92
int main(__attribute__((unused))int argc,
56
93
         __attribute__((unused))char **argv){
57
94
  int ret = 0;
 
95
  char *prompt = NULL;
 
96
  DIR *proc_dir = NULL;
 
97
  pid_t splashy_pid = 0;
 
98
  pid_t splashy_command_pid = 0;
 
99
  int exitstatus = EXIT_FAILURE;
58
100
  
59
101
  /* Create prompt string */
60
 
  char *prompt = NULL;
61
102
  {
62
103
    const char *const cryptsource = getenv("cryptsource");
63
104
    const char *const crypttarget = getenv("crypttarget");
80
121
      }
81
122
    }
82
123
    if(ret == -1){
83
 
      return EXIT_FAILURE;
 
124
      prompt = NULL;
 
125
      exitstatus = EX_OSERR;
 
126
      goto failure;
84
127
    }
85
128
  }
86
129
  
87
130
  /* Find splashy process */
88
 
  pid_t splashy_pid = 0;
89
131
  {
90
132
    const char splashy_name[] = "/sbin/splashy";
91
 
    DIR *proc_dir = opendir("/proc");
 
133
    proc_dir = opendir("/proc");
92
134
    if(proc_dir == NULL){
93
 
      free(prompt);
94
 
      perror("opendir");
95
 
      return EXIT_FAILURE;
 
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
      }
 
152
      goto failure;
96
153
    }
97
154
    for(struct dirent *proc_ent = readdir(proc_dir);
98
155
        proc_ent != NULL;
99
156
        proc_ent = readdir(proc_dir)){
100
 
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
101
 
      if(pid == 0){
102
 
        /* Not a process */
103
 
        continue;
 
157
      pid_t pid;
 
158
      {
 
159
        intmax_t tmpmax;
 
160
        char *tmp;
 
161
        errno = 0;
 
162
        tmpmax = strtoimax(proc_ent->d_name, &tmp, 10);
 
163
        if(errno != 0 or tmp == proc_ent->d_name or *tmp != '\0'
 
164
           or tmpmax != (pid_t)tmpmax){
 
165
          /* Not a process */
 
166
          continue;
 
167
        }
 
168
        pid = (pid_t)tmpmax;
104
169
      }
105
170
      /* Find the executable name by doing readlink() on the
106
171
         /proc/<pid>/exe link */
110
175
        char *exe_link;
111
176
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
112
177
        if(ret == -1){
113
 
          perror("asprintf");
114
 
          free(prompt);
115
 
          closedir(proc_dir);
116
 
          return EXIT_FAILURE;
 
178
          error_plus(0, errno, "asprintf");
 
179
          exitstatus = EX_OSERR;
 
180
          goto failure;
117
181
        }
118
182
        
119
183
        /* Check that it refers to a symlink owned by root:root */
120
184
        struct stat exe_stat;
121
185
        ret = lstat(exe_link, &exe_stat);
122
186
        if(ret == -1){
123
 
          perror("lstat");
 
187
          if(errno == ENOENT){
 
188
            free(exe_link);
 
189
            continue;
 
190
          }
 
191
          int e = errno;
 
192
          error_plus(0, errno, "lstat");
124
193
          free(exe_link);
125
 
          free(prompt);
126
 
          closedir(proc_dir);
127
 
          return EXIT_FAILURE;
 
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
          goto failure;
128
206
        }
129
207
        if(not S_ISLNK(exe_stat.st_mode)
130
208
           or exe_stat.st_uid != 0
144
222
      }
145
223
    }
146
224
    closedir(proc_dir);
 
225
    proc_dir = NULL;
147
226
  }
148
227
  if(splashy_pid == 0){
149
 
    free(prompt);
150
 
    return EXIT_FAILURE;
 
228
    exitstatus = EX_UNAVAILABLE;
 
229
    goto failure;
151
230
  }
152
231
  
153
232
  /* Set up the signal handler */
156
235
      new_action = { .sa_handler = termination_handler,
157
236
                     .sa_flags = 0 };
158
237
    sigemptyset(&new_action.sa_mask);
159
 
    sigaddset(&new_action.sa_mask, SIGINT);
160
 
    sigaddset(&new_action.sa_mask, SIGHUP);
161
 
    sigaddset(&new_action.sa_mask, SIGTERM);
 
238
    ret = sigaddset(&new_action.sa_mask, SIGINT);
 
239
    if(ret == -1){
 
240
      error_plus(0, errno, "sigaddset");
 
241
      exitstatus = EX_OSERR;
 
242
      goto failure;
 
243
    }
 
244
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
245
    if(ret == -1){
 
246
      error_plus(0, errno, "sigaddset");
 
247
      exitstatus = EX_OSERR;
 
248
      goto failure;
 
249
    }
 
250
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
251
    if(ret == -1){
 
252
      error_plus(0, errno, "sigaddset");
 
253
      exitstatus = EX_OSERR;
 
254
      goto failure;
 
255
    }
162
256
    ret = sigaction(SIGINT, NULL, &old_action);
163
257
    if(ret == -1){
164
 
      perror("sigaction");
165
 
      free(prompt);
166
 
      return EXIT_FAILURE;
 
258
      error_plus(0, errno, "sigaction");
 
259
      exitstatus = EX_OSERR;
 
260
      goto failure;
167
261
    }
168
262
    if(old_action.sa_handler != SIG_IGN){
169
263
      ret = sigaction(SIGINT, &new_action, NULL);
170
264
      if(ret == -1){
171
 
        perror("sigaction");
172
 
        free(prompt);
173
 
        return EXIT_FAILURE;
 
265
        error_plus(0, errno, "sigaction");
 
266
        exitstatus = EX_OSERR;
 
267
        goto failure;
174
268
      }
175
269
    }
176
270
    ret = sigaction(SIGHUP, NULL, &old_action);
177
271
    if(ret == -1){
178
 
      perror("sigaction");
179
 
      free(prompt);
180
 
      return EXIT_FAILURE;
 
272
      error_plus(0, errno, "sigaction");
 
273
      exitstatus = EX_OSERR;
 
274
      goto failure;
181
275
    }
182
276
    if(old_action.sa_handler != SIG_IGN){
183
277
      ret = sigaction(SIGHUP, &new_action, NULL);
184
278
      if(ret == -1){
185
 
        perror("sigaction");
186
 
        free(prompt);
187
 
        return EXIT_FAILURE;
 
279
        error_plus(0, errno, "sigaction");
 
280
        exitstatus = EX_OSERR;
 
281
        goto failure;
188
282
      }
189
283
    }
190
284
    ret = sigaction(SIGTERM, NULL, &old_action);
191
285
    if(ret == -1){
192
 
      perror("sigaction");
193
 
      free(prompt);
194
 
      return EXIT_FAILURE;
 
286
      error_plus(0, errno, "sigaction");
 
287
      exitstatus = EX_OSERR;
 
288
      goto failure;
195
289
    }
196
290
    if(old_action.sa_handler != SIG_IGN){
197
291
      ret = sigaction(SIGTERM, &new_action, NULL);
198
292
      if(ret == -1){
199
 
        perror("sigaction");
200
 
        free(prompt);
201
 
        return EXIT_FAILURE;
 
293
        error_plus(0, errno, "sigaction");
 
294
        exitstatus = EX_OSERR;
 
295
        goto failure;
202
296
      }
203
297
    }
204
298
  }
205
299
  
 
300
  if(interrupted_by_signal){
 
301
    goto failure;
 
302
  }
 
303
  
206
304
  /* Fork off the splashy command to prompt for password */
207
 
  pid_t splashy_command_pid = 0;
208
 
  if(not interrupted_by_signal){
209
 
    splashy_command_pid = fork();
210
 
    if(splashy_command_pid == -1){
211
 
      if(not interrupted_by_signal){
212
 
        perror("fork");
213
 
      }
214
 
      return EXIT_FAILURE;
215
 
    }
216
 
    /* Child */
217
 
    if(splashy_command_pid == 0){
 
305
  splashy_command_pid = fork();
 
306
  if(splashy_command_pid != 0 and interrupted_by_signal){
 
307
    goto failure;
 
308
  }
 
309
  if(splashy_command_pid == -1){
 
310
    error_plus(0, errno, "fork");
 
311
    exitstatus = EX_OSERR;
 
312
    goto failure;
 
313
  }
 
314
  /* Child */
 
315
  if(splashy_command_pid == 0){
 
316
    if(not interrupted_by_signal){
218
317
      const char splashy_command[] = "/sbin/splashy_update";
219
 
      ret = execl(splashy_command, splashy_command, prompt,
220
 
                  (char *)NULL);
221
 
      if(not interrupted_by_signal){
222
 
        perror("execl");
 
318
      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);
223
345
      }
224
 
      free(prompt);
225
 
      _exit(EXIT_FAILURE);
226
346
    }
 
347
    free(prompt);
 
348
    _exit(EXIT_FAILURE);
227
349
  }
228
350
  
229
351
  /* Parent */
230
352
  free(prompt);
 
353
  prompt = NULL;
 
354
  
 
355
  if(interrupted_by_signal){
 
356
    goto failure;
 
357
  }
231
358
  
232
359
  /* Wait for command to complete */
233
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
360
  {
234
361
    int status;
235
 
    ret = waitpid(splashy_command_pid, &status, 0);
 
362
    do {
 
363
      ret = waitpid(splashy_command_pid, &status, 0);
 
364
    } while(ret == -1 and errno == EINTR
 
365
            and not interrupted_by_signal);
 
366
    if(interrupted_by_signal){
 
367
      goto failure;
 
368
    }
236
369
    if(ret == -1){
237
 
      if(errno != EINTR){
238
 
        perror("waitpid");
239
 
      }
 
370
      error_plus(0, errno, "waitpid");
240
371
      if(errno == ECHILD){
241
372
        splashy_command_pid = 0;
242
373
      }
243
374
    } else {
244
375
      /* The child process has exited */
245
376
      splashy_command_pid = 0;
246
 
      if(not interrupted_by_signal and WIFEXITED(status)
247
 
         and WEXITSTATUS(status)==0){
 
377
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
248
378
        return EXIT_SUCCESS;
249
379
      }
250
380
    }
251
381
  }
252
 
  kill(splashy_pid, SIGTERM);
253
 
  if(interrupted_by_signal and splashy_command_pid != 0){
254
 
    kill(splashy_command_pid, SIGTERM);
255
 
  }
256
 
  sleep(2);
257
 
  while(kill(splashy_pid, 0) == 0){
258
 
    kill(splashy_pid, SIGKILL);
259
 
    sleep(1);
260
 
  }
261
 
  pid_t new_splashy_pid = fork();
262
 
  if(new_splashy_pid == 0){
263
 
    /* Child; will become new splashy process */
264
 
    
265
 
    /* Make the effective user ID (root) the only user ID instead of
266
 
       the real user ID (mandos) */
267
 
    ret = setuid(geteuid());
268
 
    if(ret == -1){
269
 
      perror("setuid");
270
 
    }
271
 
    
272
 
    setsid();
273
 
    ret = chdir("/");
274
 
/*     if(fork() != 0){ */
275
 
/*       _exit(EXIT_SUCCESS); */
276
 
/*     } */
277
 
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
278
 
    if(ret == -1){
279
 
      perror("dup2");
280
 
      _exit(EXIT_FAILURE);
281
 
    }
282
 
    
283
 
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
284
 
    if(not interrupted_by_signal){
285
 
      perror("execl");
286
 
    }
287
 
    _exit(EXIT_FAILURE);
288
 
  }
289
 
  
290
 
  return EXIT_FAILURE;
 
382
  
 
383
 failure:
 
384
  
 
385
  free(prompt);
 
386
  
 
387
  if(proc_dir != NULL){
 
388
    TEMP_FAILURE_RETRY(closedir(proc_dir));
 
389
  }
 
390
  
 
391
  if(splashy_command_pid != 0){
 
392
    TEMP_FAILURE_RETRY(kill(splashy_command_pid, SIGTERM));
 
393
    
 
394
    TEMP_FAILURE_RETRY(kill(splashy_pid, SIGTERM));
 
395
    sleep(2);
 
396
    while(TEMP_FAILURE_RETRY(kill(splashy_pid, 0)) == 0){
 
397
      TEMP_FAILURE_RETRY(kill(splashy_pid, SIGKILL));
 
398
      sleep(1);
 
399
    }
 
400
    pid_t new_splashy_pid = (pid_t)TEMP_FAILURE_RETRY(fork());
 
401
    if(new_splashy_pid == 0){
 
402
      /* Child; will become new splashy process */
 
403
      
 
404
      /* Make the effective user ID (root) the only user ID instead of
 
405
         the real user ID (_mandos) */
 
406
      ret = setuid(geteuid());
 
407
      if(ret == -1){
 
408
        error_plus(0, errno, "setuid");
 
409
      }
 
410
      
 
411
      setsid();
 
412
      ret = chdir("/");
 
413
      if(ret == -1){
 
414
        error_plus(0, errno, "chdir");
 
415
      }
 
416
/*       if(fork() != 0){ */
 
417
/*      _exit(EXIT_SUCCESS); */
 
418
/*       } */
 
419
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
 
420
      if(ret == -1){
 
421
        error_plus(0, errno, "dup2");
 
422
        _exit(EX_OSERR);
 
423
      }
 
424
      
 
425
      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
      }
 
444
    }
 
445
  }
 
446
  
 
447
  if(interrupted_by_signal){
 
448
    struct sigaction signal_action;
 
449
    sigemptyset(&signal_action.sa_mask);
 
450
    signal_action.sa_handler = SIG_DFL;
 
451
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
452
                                            &signal_action, NULL));
 
453
    if(ret == -1){
 
454
      error_plus(0, errno, "sigaction");
 
455
    }
 
456
    do {
 
457
      ret = raise(signal_received);
 
458
    } while(ret != 0 and errno == EINTR);
 
459
    if(ret != 0){
 
460
      error_plus(0, errno, "raise");
 
461
      abort();
 
462
    }
 
463
    TEMP_FAILURE_RETRY(pause());
 
464
  }
 
465
  
 
466
  return exitstatus;
291
467
}