/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 at bsnet
  • Date: 2010-09-09 18:16:14 UTC
  • mfrom: (237.2.35 mandos-empty-device)
  • Revision ID: teddy@fukt.bsnet.se-20100909181614-oanlmvkzsiodbo3c
Merge in branch to interpret an empty device name to mean
"autodetect".

Show diffs side-by-side

added added

removed removed

Lines of Context:
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,
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() */
 
43
                                   STDOUT_FILENO, _exit(),
 
44
                                   pause() */
45
45
#include <string.h>             /* memcmp() */
46
 
#include <errno.h>              /* errno */
 
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 */
47
52
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
48
53
                                   WEXITSTATUS() */
 
54
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
 
55
                                   EX_UNAVAILABLE */
49
56
 
50
57
sig_atomic_t interrupted_by_signal = 0;
 
58
int signal_received;
51
59
 
52
 
static void termination_handler(__attribute__((unused))int signum){
 
60
static void termination_handler(int signum){
 
61
  if(interrupted_by_signal){
 
62
    return;
 
63
  }
53
64
  interrupted_by_signal = 1;
 
65
  signal_received = signum;
54
66
}
55
67
 
56
68
int main(__attribute__((unused))int argc,
57
69
         __attribute__((unused))char **argv){
58
70
  int ret = 0;
 
71
  char *prompt = NULL;
 
72
  DIR *proc_dir = NULL;
 
73
  pid_t splashy_pid = 0;
 
74
  pid_t splashy_command_pid = 0;
 
75
  int exitstatus = EXIT_FAILURE;
59
76
  
60
77
  /* Create prompt string */
61
 
  char *prompt = NULL;
62
78
  {
63
79
    const char *const cryptsource = getenv("cryptsource");
64
80
    const char *const crypttarget = getenv("crypttarget");
81
97
      }
82
98
    }
83
99
    if(ret == -1){
84
 
      return EXIT_FAILURE;
 
100
      prompt = NULL;
 
101
      exitstatus = EX_OSERR;
 
102
      goto failure;
85
103
    }
86
104
  }
87
105
  
88
106
  /* Find splashy process */
89
 
  pid_t splashy_pid = 0;
90
107
  {
91
108
    const char splashy_name[] = "/sbin/splashy";
92
 
    DIR *proc_dir = opendir("/proc");
 
109
    proc_dir = opendir("/proc");
93
110
    if(proc_dir == NULL){
94
 
      free(prompt);
 
111
      int e = errno;
95
112
      perror("opendir");
96
 
      return EXIT_FAILURE;
 
113
      switch(e){
 
114
      case EACCES:
 
115
      case ENOTDIR:
 
116
      case ELOOP:
 
117
      case ENOENT:
 
118
      default:
 
119
        exitstatus = EX_OSFILE;
 
120
        break;
 
121
      case ENAMETOOLONG:
 
122
      case EMFILE:
 
123
      case ENFILE:
 
124
      case ENOMEM:
 
125
        exitstatus = EX_OSERR;
 
126
        break;
 
127
      }
 
128
      goto failure;
97
129
    }
98
130
    for(struct dirent *proc_ent = readdir(proc_dir);
99
131
        proc_ent != NULL;
120
152
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
121
153
        if(ret == -1){
122
154
          perror("asprintf");
123
 
          free(prompt);
124
 
          closedir(proc_dir);
125
 
          return EXIT_FAILURE;
 
155
          exitstatus = EX_OSERR;
 
156
          goto failure;
126
157
        }
127
158
        
128
159
        /* Check that it refers to a symlink owned by root:root */
133
164
            free(exe_link);
134
165
            continue;
135
166
          }
 
167
          int e = errno;
136
168
          perror("lstat");
137
169
          free(exe_link);
138
 
          free(prompt);
139
 
          closedir(proc_dir);
140
 
          return EXIT_FAILURE;
 
170
          switch(e){
 
171
          case EACCES:
 
172
          case ENOTDIR:
 
173
          case ELOOP:
 
174
          default:
 
175
            exitstatus = EX_OSFILE;
 
176
            break;
 
177
          case ENAMETOOLONG:
 
178
            exitstatus = EX_OSERR;
 
179
            break;
 
180
          }
 
181
          goto failure;
141
182
        }
142
183
        if(not S_ISLNK(exe_stat.st_mode)
143
184
           or exe_stat.st_uid != 0
157
198
      }
158
199
    }
159
200
    closedir(proc_dir);
 
201
    proc_dir = NULL;
160
202
  }
161
203
  if(splashy_pid == 0){
162
 
    free(prompt);
163
 
    return EXIT_FAILURE;
 
204
    exitstatus = EX_UNAVAILABLE;
 
205
    goto failure;
164
206
  }
165
207
  
166
208
  /* Set up the signal handler */
169
211
      new_action = { .sa_handler = termination_handler,
170
212
                     .sa_flags = 0 };
171
213
    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);
 
214
    ret = sigaddset(&new_action.sa_mask, SIGINT);
 
215
    if(ret == -1){
 
216
      perror("sigaddset");
 
217
      exitstatus = EX_OSERR;
 
218
      goto failure;
 
219
    }
 
220
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
221
    if(ret == -1){
 
222
      perror("sigaddset");
 
223
      exitstatus = EX_OSERR;
 
224
      goto failure;
 
225
    }
 
226
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
227
    if(ret == -1){
 
228
      perror("sigaddset");
 
229
      exitstatus = EX_OSERR;
 
230
      goto failure;
 
231
    }
175
232
    ret = sigaction(SIGINT, NULL, &old_action);
176
233
    if(ret == -1){
177
234
      perror("sigaction");
178
 
      free(prompt);
179
 
      return EXIT_FAILURE;
 
235
      exitstatus = EX_OSERR;
 
236
      goto failure;
180
237
    }
181
238
    if(old_action.sa_handler != SIG_IGN){
182
239
      ret = sigaction(SIGINT, &new_action, NULL);
183
240
      if(ret == -1){
184
241
        perror("sigaction");
185
 
        free(prompt);
186
 
        return EXIT_FAILURE;
 
242
        exitstatus = EX_OSERR;
 
243
        goto failure;
187
244
      }
188
245
    }
189
246
    ret = sigaction(SIGHUP, NULL, &old_action);
190
247
    if(ret == -1){
191
248
      perror("sigaction");
192
 
      free(prompt);
193
 
      return EXIT_FAILURE;
 
249
      exitstatus = EX_OSERR;
 
250
      goto failure;
194
251
    }
195
252
    if(old_action.sa_handler != SIG_IGN){
196
253
      ret = sigaction(SIGHUP, &new_action, NULL);
197
254
      if(ret == -1){
198
255
        perror("sigaction");
199
 
        free(prompt);
200
 
        return EXIT_FAILURE;
 
256
        exitstatus = EX_OSERR;
 
257
        goto failure;
201
258
      }
202
259
    }
203
260
    ret = sigaction(SIGTERM, NULL, &old_action);
204
261
    if(ret == -1){
205
262
      perror("sigaction");
206
 
      free(prompt);
207
 
      return EXIT_FAILURE;
 
263
      exitstatus = EX_OSERR;
 
264
      goto failure;
208
265
    }
209
266
    if(old_action.sa_handler != SIG_IGN){
210
267
      ret = sigaction(SIGTERM, &new_action, NULL);
211
268
      if(ret == -1){
212
269
        perror("sigaction");
213
 
        free(prompt);
214
 
        return EXIT_FAILURE;
 
270
        exitstatus = EX_OSERR;
 
271
        goto failure;
215
272
      }
216
273
    }
217
274
  }
218
275
  
 
276
  if(interrupted_by_signal){
 
277
    goto failure;
 
278
  }
 
279
  
219
280
  /* 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){
 
281
  splashy_command_pid = fork();
 
282
  if(splashy_command_pid != 0 and interrupted_by_signal){
 
283
    goto failure;
 
284
  }
 
285
  if(splashy_command_pid == -1){
 
286
    perror("fork");
 
287
    exitstatus = EX_OSERR;
 
288
    goto failure;
 
289
  }
 
290
  /* Child */
 
291
  if(splashy_command_pid == 0){
 
292
    if(not interrupted_by_signal){
231
293
      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");
 
294
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
 
295
      int e = errno;
 
296
      perror("execl");
 
297
      switch(e){
 
298
      case EACCES:
 
299
      case ENOENT:
 
300
      case ENOEXEC:
 
301
      case EINVAL:
 
302
        _exit(EX_UNAVAILABLE);
 
303
      case ENAMETOOLONG:
 
304
      case E2BIG:
 
305
      case ENOMEM:
 
306
      case EFAULT:
 
307
      case EIO:
 
308
      case EMFILE:
 
309
      case ENFILE:
 
310
      case ETXTBSY:
 
311
      default:
 
312
        _exit(EX_OSERR);
 
313
      case ENOTDIR:
 
314
      case ELOOP:
 
315
      case EISDIR:
 
316
      case ELIBBAD:
 
317
      case EPERM:
 
318
        _exit(EX_OSFILE);
236
319
      }
237
 
      free(prompt);
238
 
      _exit(EXIT_FAILURE);
239
320
    }
 
321
    free(prompt);
 
322
    _exit(EXIT_FAILURE);
240
323
  }
241
324
  
242
325
  /* Parent */
243
326
  free(prompt);
 
327
  prompt = NULL;
 
328
  
 
329
  if(interrupted_by_signal){
 
330
    goto failure;
 
331
  }
244
332
  
245
333
  /* Wait for command to complete */
246
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
334
  {
247
335
    int status;
248
 
    ret = waitpid(splashy_command_pid, &status, 0);
 
336
    do {
 
337
      ret = waitpid(splashy_command_pid, &status, 0);
 
338
    } while(ret == -1 and errno == EINTR
 
339
            and not interrupted_by_signal);
 
340
    if(interrupted_by_signal){
 
341
      goto failure;
 
342
    }
249
343
    if(ret == -1){
250
 
      if(errno != EINTR){
251
 
        perror("waitpid");
252
 
      }
 
344
      perror("waitpid");
253
345
      if(errno == ECHILD){
254
346
        splashy_command_pid = 0;
255
347
      }
256
348
    } else {
257
349
      /* The child process has exited */
258
350
      splashy_command_pid = 0;
259
 
      if(not interrupted_by_signal and WIFEXITED(status)
260
 
         and WEXITSTATUS(status)==0){
 
351
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
261
352
        return EXIT_SUCCESS;
262
353
      }
263
354
    }
264
355
  }
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;
 
356
  
 
357
 failure:
 
358
  
 
359
  free(prompt);
 
360
  
 
361
  if(proc_dir != NULL){
 
362
    TEMP_FAILURE_RETRY(closedir(proc_dir));
 
363
  }
 
364
  
 
365
  if(splashy_command_pid != 0){
 
366
    TEMP_FAILURE_RETRY(kill(splashy_command_pid, SIGTERM));
 
367
    
 
368
    TEMP_FAILURE_RETRY(kill(splashy_pid, SIGTERM));
 
369
    sleep(2);
 
370
    while(TEMP_FAILURE_RETRY(kill(splashy_pid, 0)) == 0){
 
371
      TEMP_FAILURE_RETRY(kill(splashy_pid, SIGKILL));
 
372
      sleep(1);
 
373
    }
 
374
    pid_t new_splashy_pid = (pid_t)TEMP_FAILURE_RETRY(fork());
 
375
    if(new_splashy_pid == 0){
 
376
      /* Child; will become new splashy process */
 
377
      
 
378
      /* Make the effective user ID (root) the only user ID instead of
 
379
         the real user ID (_mandos) */
 
380
      ret = setuid(geteuid());
 
381
      if(ret == -1){
 
382
        perror("setuid");
 
383
      }
 
384
      
 
385
      setsid();
 
386
      ret = chdir("/");
 
387
      if(ret == -1){
 
388
        perror("chdir");
 
389
      }
 
390
/*       if(fork() != 0){ */
 
391
/*      _exit(EXIT_SUCCESS); */
 
392
/*       } */
 
393
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
 
394
      if(ret == -1){
 
395
        perror("dup2");
 
396
        _exit(EX_OSERR);
 
397
      }
 
398
      
 
399
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
400
      {
 
401
        int e = errno;
 
402
        perror("execl");
 
403
        switch(e){
 
404
        case EACCES:
 
405
        case ENOENT:
 
406
        case ENOEXEC:
 
407
        default:
 
408
          _exit(EX_UNAVAILABLE);
 
409
        case ENAMETOOLONG:
 
410
        case E2BIG:
 
411
        case ENOMEM:
 
412
          _exit(EX_OSERR);
 
413
        case ENOTDIR:
 
414
        case ELOOP:
 
415
          _exit(EX_OSFILE);
 
416
        }
 
417
      }
 
418
    }
 
419
  }
 
420
  
 
421
  if(interrupted_by_signal){
 
422
    struct sigaction signal_action;
 
423
    sigemptyset(&signal_action.sa_mask);
 
424
    signal_action.sa_handler = SIG_DFL;
 
425
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
426
                                            &signal_action, NULL));
 
427
    if(ret == -1){
 
428
      perror("sigaction");
 
429
    }
 
430
    do {
 
431
      ret = raise(signal_received);
 
432
    } while(ret != 0 and errno == EINTR);
 
433
    if(ret != 0){
 
434
      perror("raise");
 
435
      abort();
 
436
    }
 
437
    TEMP_FAILURE_RETRY(pause());
 
438
  }
 
439
  
 
440
  return exitstatus;
304
441
}