/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

Code cleanup.

* mandos: Move some global stuff into classes.
  (server, group): Moved into "AvahiService".  All users changed.
  (AvahiService.group, AvahiService.server): New attributes.
  (entry_group_state_changed, server_state_changed): Moved into
                                                     "AvahiService".
                                                     All callers
                                                     changed.
  (AvahiService.cleanup, AvahiService.activate): New.
  (_datetime_to_dbus): Moved into "ClientDBus".  All callers changed.
  (ClientDBus._datetime_to_dbus): New.

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
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
 
23
 * <https://www.fukt.bsnet.se/~teddy/>.
23
24
 */
24
25
 
25
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
 
26
#define _GNU_SOURCE             /* asprintf() */
26
27
#include <signal.h>             /* sig_atomic_t, struct sigaction,
27
28
                                   sigemptyset(), sigaddset(), SIGINT,
28
29
                                   SIGHUP, SIGTERM, sigaction,
29
30
                                   SIG_IGN, kill(), SIGKILL */
30
31
#include <stddef.h>             /* NULL */
31
32
#include <stdlib.h>             /* getenv() */
32
 
#include <stdio.h>              /* asprintf(), vasprintf(), vprintf(), fprintf() */
 
33
#include <stdio.h>              /* asprintf(), perror() */
33
34
#include <stdlib.h>             /* EXIT_FAILURE, free(),
34
35
                                   EXIT_SUCCESS */
35
36
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
40
41
#include <iso646.h>             /* not, or, and */
41
42
#include <unistd.h>             /* readlink(), fork(), execl(),
42
43
                                   sleep(), dup2() STDERR_FILENO,
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() */
 
44
                                   STDOUT_FILENO, _exit() */
 
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
 
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
 
  }
 
51
 
 
52
static void termination_handler(__attribute__((unused))int signum){
88
53
  interrupted_by_signal = 1;
89
 
  signal_received = signum;
90
54
}
91
55
 
92
56
int main(__attribute__((unused))int argc,
93
57
         __attribute__((unused))char **argv){
94
58
  int ret = 0;
 
59
  
 
60
  /* Create prompt string */
95
61
  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;
100
 
  
101
 
  /* Create prompt string */
102
62
  {
103
63
    const char *const cryptsource = getenv("cryptsource");
104
64
    const char *const crypttarget = getenv("crypttarget");
121
81
      }
122
82
    }
123
83
    if(ret == -1){
124
 
      prompt = NULL;
125
 
      exitstatus = EX_OSERR;
126
 
      goto failure;
 
84
      return EXIT_FAILURE;
127
85
    }
128
86
  }
129
87
  
130
88
  /* Find splashy process */
 
89
  pid_t splashy_pid = 0;
131
90
  {
132
91
    const char splashy_name[] = "/sbin/splashy";
133
 
    proc_dir = opendir("/proc");
 
92
    DIR *proc_dir = opendir("/proc");
134
93
    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
 
      }
152
 
      goto failure;
 
94
      free(prompt);
 
95
      perror("opendir");
 
96
      return EXIT_FAILURE;
153
97
    }
154
98
    for(struct dirent *proc_ent = readdir(proc_dir);
155
99
        proc_ent != NULL;
175
119
        char *exe_link;
176
120
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
177
121
        if(ret == -1){
178
 
          error_plus(0, errno, "asprintf");
179
 
          exitstatus = EX_OSERR;
180
 
          goto failure;
 
122
          perror("asprintf");
 
123
          free(prompt);
 
124
          closedir(proc_dir);
 
125
          return EXIT_FAILURE;
181
126
        }
182
127
        
183
128
        /* Check that it refers to a symlink owned by root:root */
188
133
            free(exe_link);
189
134
            continue;
190
135
          }
191
 
          int e = errno;
192
 
          error_plus(0, errno, "lstat");
 
136
          perror("lstat");
193
137
          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
 
          goto failure;
 
138
          free(prompt);
 
139
          closedir(proc_dir);
 
140
          return EXIT_FAILURE;
206
141
        }
207
142
        if(not S_ISLNK(exe_stat.st_mode)
208
143
           or exe_stat.st_uid != 0
222
157
      }
223
158
    }
224
159
    closedir(proc_dir);
225
 
    proc_dir = NULL;
226
160
  }
227
161
  if(splashy_pid == 0){
228
 
    exitstatus = EX_UNAVAILABLE;
229
 
    goto failure;
 
162
    free(prompt);
 
163
    return EXIT_FAILURE;
230
164
  }
231
165
  
232
166
  /* Set up the signal handler */
235
169
      new_action = { .sa_handler = termination_handler,
236
170
                     .sa_flags = 0 };
237
171
    sigemptyset(&new_action.sa_mask);
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
 
    }
 
172
    sigaddset(&new_action.sa_mask, SIGINT);
 
173
    sigaddset(&new_action.sa_mask, SIGHUP);
 
174
    sigaddset(&new_action.sa_mask, SIGTERM);
256
175
    ret = sigaction(SIGINT, NULL, &old_action);
257
176
    if(ret == -1){
258
 
      error_plus(0, errno, "sigaction");
259
 
      exitstatus = EX_OSERR;
260
 
      goto failure;
 
177
      perror("sigaction");
 
178
      free(prompt);
 
179
      return EXIT_FAILURE;
261
180
    }
262
181
    if(old_action.sa_handler != SIG_IGN){
263
182
      ret = sigaction(SIGINT, &new_action, NULL);
264
183
      if(ret == -1){
265
 
        error_plus(0, errno, "sigaction");
266
 
        exitstatus = EX_OSERR;
267
 
        goto failure;
 
184
        perror("sigaction");
 
185
        free(prompt);
 
186
        return EXIT_FAILURE;
268
187
      }
269
188
    }
270
189
    ret = sigaction(SIGHUP, NULL, &old_action);
271
190
    if(ret == -1){
272
 
      error_plus(0, errno, "sigaction");
273
 
      exitstatus = EX_OSERR;
274
 
      goto failure;
 
191
      perror("sigaction");
 
192
      free(prompt);
 
193
      return EXIT_FAILURE;
275
194
    }
276
195
    if(old_action.sa_handler != SIG_IGN){
277
196
      ret = sigaction(SIGHUP, &new_action, NULL);
278
197
      if(ret == -1){
279
 
        error_plus(0, errno, "sigaction");
280
 
        exitstatus = EX_OSERR;
281
 
        goto failure;
 
198
        perror("sigaction");
 
199
        free(prompt);
 
200
        return EXIT_FAILURE;
282
201
      }
283
202
    }
284
203
    ret = sigaction(SIGTERM, NULL, &old_action);
285
204
    if(ret == -1){
286
 
      error_plus(0, errno, "sigaction");
287
 
      exitstatus = EX_OSERR;
288
 
      goto failure;
 
205
      perror("sigaction");
 
206
      free(prompt);
 
207
      return EXIT_FAILURE;
289
208
    }
290
209
    if(old_action.sa_handler != SIG_IGN){
291
210
      ret = sigaction(SIGTERM, &new_action, NULL);
292
211
      if(ret == -1){
293
 
        error_plus(0, errno, "sigaction");
294
 
        exitstatus = EX_OSERR;
295
 
        goto failure;
 
212
        perror("sigaction");
 
213
        free(prompt);
 
214
        return EXIT_FAILURE;
296
215
      }
297
216
    }
298
217
  }
299
218
  
300
 
  if(interrupted_by_signal){
301
 
    goto failure;
302
 
  }
303
 
  
304
219
  /* Fork off the splashy command to prompt for password */
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){
 
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){
317
231
      const char splashy_command[] = "/sbin/splashy_update";
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);
 
232
      ret = execl(splashy_command, splashy_command, prompt,
 
233
                  (char *)NULL);
 
234
      if(not interrupted_by_signal){
 
235
        perror("execl");
345
236
      }
 
237
      free(prompt);
 
238
      _exit(EXIT_FAILURE);
346
239
    }
347
 
    free(prompt);
348
 
    _exit(EXIT_FAILURE);
349
240
  }
350
241
  
351
242
  /* Parent */
352
243
  free(prompt);
353
 
  prompt = NULL;
354
 
  
355
 
  if(interrupted_by_signal){
356
 
    goto failure;
357
 
  }
358
244
  
359
245
  /* Wait for command to complete */
360
 
  {
 
246
  if(not interrupted_by_signal and splashy_command_pid != 0){
361
247
    int status;
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
 
    }
 
248
    ret = waitpid(splashy_command_pid, &status, 0);
369
249
    if(ret == -1){
370
 
      error_plus(0, errno, "waitpid");
 
250
      if(errno != EINTR){
 
251
        perror("waitpid");
 
252
      }
371
253
      if(errno == ECHILD){
372
254
        splashy_command_pid = 0;
373
255
      }
374
256
    } else {
375
257
      /* The child process has exited */
376
258
      splashy_command_pid = 0;
377
 
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
 
259
      if(not interrupted_by_signal and WIFEXITED(status)
 
260
         and WEXITSTATUS(status)==0){
378
261
        return EXIT_SUCCESS;
379
262
      }
380
263
    }
381
264
  }
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;
 
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;
467
304
}