/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« 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: (237.7.33 trunk)
  • mto: This revision was merged to the branch mainline in revision 284.
  • 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
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(), sscanf() */
 
32
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(), fprintf() */
34
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() */
39
 
#include <inttypes.h>           /* intmax_t, SCNdMAX */
 
38
#include <inttypes.h>           /* intmax_t, strtoimax() */
40
39
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
41
40
#include <iso646.h>             /* not, or, and */
42
41
#include <unistd.h>             /* readlink(), fork(), execl(),
43
42
                                   sleep(), dup2() STDERR_FILENO,
44
 
                                   STDOUT_FILENO, _exit() */
45
 
#include <string.h>             /* memcmp() */
46
 
#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() */
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
 
 
52
 
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
  }
53
88
  interrupted_by_signal = 1;
 
89
  signal_received = signum;
54
90
}
55
91
 
56
92
int main(__attribute__((unused))int argc,
57
93
         __attribute__((unused))char **argv){
58
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;
59
100
  
60
101
  /* Create prompt string */
61
 
  char *prompt = NULL;
62
102
  {
63
103
    const char *const cryptsource = getenv("cryptsource");
64
104
    const char *const crypttarget = getenv("crypttarget");
81
121
      }
82
122
    }
83
123
    if(ret == -1){
84
 
      return EXIT_FAILURE;
 
124
      prompt = NULL;
 
125
      exitstatus = EX_OSERR;
 
126
      goto failure;
85
127
    }
86
128
  }
87
129
  
88
130
  /* Find splashy process */
89
 
  pid_t splashy_pid = 0;
90
131
  {
91
132
    const char splashy_name[] = "/sbin/splashy";
92
 
    DIR *proc_dir = opendir("/proc");
 
133
    proc_dir = opendir("/proc");
93
134
    if(proc_dir == NULL){
94
 
      free(prompt);
95
 
      perror("opendir");
96
 
      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;
97
153
    }
98
154
    for(struct dirent *proc_ent = readdir(proc_dir);
99
155
        proc_ent != NULL;
101
157
      pid_t pid;
102
158
      {
103
159
        intmax_t tmpmax;
104
 
        int numchars;
105
 
        ret = sscanf(proc_ent->d_name, "%" SCNdMAX "%n", &tmpmax,
106
 
                     &numchars);
107
 
        if(ret < 1 or tmpmax != (pid_t)tmpmax
108
 
           or proc_ent->d_name[numchars] != '\0'){
 
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){
109
165
          /* Not a process */
110
166
          continue;
111
167
        }
119
175
        char *exe_link;
120
176
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
121
177
        if(ret == -1){
122
 
          perror("asprintf");
123
 
          free(prompt);
124
 
          closedir(proc_dir);
125
 
          return EXIT_FAILURE;
 
178
          error_plus(0, errno, "asprintf");
 
179
          exitstatus = EX_OSERR;
 
180
          goto failure;
126
181
        }
127
182
        
128
183
        /* Check that it refers to a symlink owned by root:root */
133
188
            free(exe_link);
134
189
            continue;
135
190
          }
136
 
          perror("lstat");
 
191
          int e = errno;
 
192
          error_plus(0, errno, "lstat");
137
193
          free(exe_link);
138
 
          free(prompt);
139
 
          closedir(proc_dir);
140
 
          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;
141
206
        }
142
207
        if(not S_ISLNK(exe_stat.st_mode)
143
208
           or exe_stat.st_uid != 0
157
222
      }
158
223
    }
159
224
    closedir(proc_dir);
 
225
    proc_dir = NULL;
160
226
  }
161
227
  if(splashy_pid == 0){
162
 
    free(prompt);
163
 
    return EXIT_FAILURE;
 
228
    exitstatus = EX_UNAVAILABLE;
 
229
    goto failure;
164
230
  }
165
231
  
166
232
  /* Set up the signal handler */
169
235
      new_action = { .sa_handler = termination_handler,
170
236
                     .sa_flags = 0 };
171
237
    sigemptyset(&new_action.sa_mask);
172
 
    sigaddset(&new_action.sa_mask, SIGINT);
173
 
    sigaddset(&new_action.sa_mask, SIGHUP);
174
 
    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
    }
175
256
    ret = sigaction(SIGINT, NULL, &old_action);
176
257
    if(ret == -1){
177
 
      perror("sigaction");
178
 
      free(prompt);
179
 
      return EXIT_FAILURE;
 
258
      error_plus(0, errno, "sigaction");
 
259
      exitstatus = EX_OSERR;
 
260
      goto failure;
180
261
    }
181
262
    if(old_action.sa_handler != SIG_IGN){
182
263
      ret = sigaction(SIGINT, &new_action, NULL);
183
264
      if(ret == -1){
184
 
        perror("sigaction");
185
 
        free(prompt);
186
 
        return EXIT_FAILURE;
 
265
        error_plus(0, errno, "sigaction");
 
266
        exitstatus = EX_OSERR;
 
267
        goto failure;
187
268
      }
188
269
    }
189
270
    ret = sigaction(SIGHUP, NULL, &old_action);
190
271
    if(ret == -1){
191
 
      perror("sigaction");
192
 
      free(prompt);
193
 
      return EXIT_FAILURE;
 
272
      error_plus(0, errno, "sigaction");
 
273
      exitstatus = EX_OSERR;
 
274
      goto failure;
194
275
    }
195
276
    if(old_action.sa_handler != SIG_IGN){
196
277
      ret = sigaction(SIGHUP, &new_action, NULL);
197
278
      if(ret == -1){
198
 
        perror("sigaction");
199
 
        free(prompt);
200
 
        return EXIT_FAILURE;
 
279
        error_plus(0, errno, "sigaction");
 
280
        exitstatus = EX_OSERR;
 
281
        goto failure;
201
282
      }
202
283
    }
203
284
    ret = sigaction(SIGTERM, NULL, &old_action);
204
285
    if(ret == -1){
205
 
      perror("sigaction");
206
 
      free(prompt);
207
 
      return EXIT_FAILURE;
 
286
      error_plus(0, errno, "sigaction");
 
287
      exitstatus = EX_OSERR;
 
288
      goto failure;
208
289
    }
209
290
    if(old_action.sa_handler != SIG_IGN){
210
291
      ret = sigaction(SIGTERM, &new_action, NULL);
211
292
      if(ret == -1){
212
 
        perror("sigaction");
213
 
        free(prompt);
214
 
        return EXIT_FAILURE;
 
293
        error_plus(0, errno, "sigaction");
 
294
        exitstatus = EX_OSERR;
 
295
        goto failure;
215
296
      }
216
297
    }
217
298
  }
218
299
  
 
300
  if(interrupted_by_signal){
 
301
    goto failure;
 
302
  }
 
303
  
219
304
  /* Fork off the splashy command to prompt for password */
220
 
  pid_t splashy_command_pid = 0;
221
 
  if(not interrupted_by_signal){
222
 
    splashy_command_pid = fork();
223
 
    if(splashy_command_pid == -1){
224
 
      if(not interrupted_by_signal){
225
 
        perror("fork");
226
 
      }
227
 
      return EXIT_FAILURE;
228
 
    }
229
 
    /* Child */
230
 
    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){
231
317
      const char splashy_command[] = "/sbin/splashy_update";
232
 
      ret = execl(splashy_command, splashy_command, prompt,
233
 
                  (char *)NULL);
234
 
      if(not interrupted_by_signal){
235
 
        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);
236
345
      }
237
 
      free(prompt);
238
 
      _exit(EXIT_FAILURE);
239
346
    }
 
347
    free(prompt);
 
348
    _exit(EXIT_FAILURE);
240
349
  }
241
350
  
242
351
  /* Parent */
243
352
  free(prompt);
 
353
  prompt = NULL;
 
354
  
 
355
  if(interrupted_by_signal){
 
356
    goto failure;
 
357
  }
244
358
  
245
359
  /* Wait for command to complete */
246
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
360
  {
247
361
    int status;
248
 
    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
    }
249
369
    if(ret == -1){
250
 
      if(errno != EINTR){
251
 
        perror("waitpid");
252
 
      }
 
370
      error_plus(0, errno, "waitpid");
253
371
      if(errno == ECHILD){
254
372
        splashy_command_pid = 0;
255
373
      }
256
374
    } else {
257
375
      /* The child process has exited */
258
376
      splashy_command_pid = 0;
259
 
      if(not interrupted_by_signal and WIFEXITED(status)
260
 
         and WEXITSTATUS(status)==0){
 
377
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
261
378
        return EXIT_SUCCESS;
262
379
      }
263
380
    }
264
381
  }
265
 
  kill(splashy_pid, SIGTERM);
266
 
  if(interrupted_by_signal and splashy_command_pid != 0){
267
 
    kill(splashy_command_pid, SIGTERM);
268
 
  }
269
 
  sleep(2);
270
 
  while(kill(splashy_pid, 0) == 0){
271
 
    kill(splashy_pid, SIGKILL);
272
 
    sleep(1);
273
 
  }
274
 
  pid_t new_splashy_pid = fork();
275
 
  if(new_splashy_pid == 0){
276
 
    /* Child; will become new splashy process */
277
 
    
278
 
    /* Make the effective user ID (root) the only user ID instead of
279
 
       the real user ID (_mandos) */
280
 
    ret = setuid(geteuid());
281
 
    if(ret == -1){
282
 
      perror("setuid");
283
 
    }
284
 
    
285
 
    setsid();
286
 
    ret = chdir("/");
287
 
/*     if(fork() != 0){ */
288
 
/*       _exit(EXIT_SUCCESS); */
289
 
/*     } */
290
 
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
291
 
    if(ret == -1){
292
 
      perror("dup2");
293
 
      _exit(EXIT_FAILURE);
294
 
    }
295
 
    
296
 
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
297
 
    if(not interrupted_by_signal){
298
 
      perror("execl");
299
 
    }
300
 
    _exit(EXIT_FAILURE);
301
 
  }
302
 
  
303
 
  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;
304
467
}