/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 Hogeborn
  • Date: 2009-09-06 05:37:34 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090906053734-uf180lq30ivv4nfy
* plugin-runner.c (getplugin, add_environment, main): Handle EINTR
                                                      properly.

* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
  move out "decrypted_buffer_size" to where it is needed.

* plugins.d/splashy.c (termination_handler): Save signal received.
  (main): Check return value from "sigaddset()".

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 <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,
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() */
 
44
                                   STDOUT_FILENO, _exit() */
45
45
#include <string.h>             /* memcmp() */
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 */
 
46
#include <errno.h>              /* errno */
52
47
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
53
48
                                   WEXITSTATUS() */
54
 
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
55
 
                                   EX_UNAVAILABLE */
56
49
 
57
50
sig_atomic_t interrupted_by_signal = 0;
58
51
int signal_received;
68
61
int main(__attribute__((unused))int argc,
69
62
         __attribute__((unused))char **argv){
70
63
  int ret = 0;
 
64
  
 
65
  /* Create prompt string */
71
66
  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;
76
 
  
77
 
  /* Create prompt string */
78
67
  {
79
68
    const char *const cryptsource = getenv("cryptsource");
80
69
    const char *const crypttarget = getenv("crypttarget");
97
86
      }
98
87
    }
99
88
    if(ret == -1){
100
 
      prompt = NULL;
101
 
      exitstatus = EX_OSERR;
102
 
      goto failure;
 
89
      return EXIT_FAILURE;
103
90
    }
104
91
  }
105
92
  
106
93
  /* Find splashy process */
 
94
  pid_t splashy_pid = 0;
107
95
  {
108
96
    const char splashy_name[] = "/sbin/splashy";
109
 
    proc_dir = opendir("/proc");
 
97
    DIR *proc_dir = opendir("/proc");
110
98
    if(proc_dir == NULL){
111
 
      int e = errno;
 
99
      free(prompt);
112
100
      perror("opendir");
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;
 
101
      return EXIT_FAILURE;
129
102
    }
130
103
    for(struct dirent *proc_ent = readdir(proc_dir);
131
104
        proc_ent != NULL;
152
125
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
153
126
        if(ret == -1){
154
127
          perror("asprintf");
155
 
          exitstatus = EX_OSERR;
156
 
          goto failure;
 
128
          free(prompt);
 
129
          closedir(proc_dir);
 
130
          return EXIT_FAILURE;
157
131
        }
158
132
        
159
133
        /* Check that it refers to a symlink owned by root:root */
164
138
            free(exe_link);
165
139
            continue;
166
140
          }
167
 
          int e = errno;
168
141
          perror("lstat");
169
142
          free(exe_link);
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;
 
143
          free(prompt);
 
144
          closedir(proc_dir);
 
145
          return EXIT_FAILURE;
182
146
        }
183
147
        if(not S_ISLNK(exe_stat.st_mode)
184
148
           or exe_stat.st_uid != 0
198
162
      }
199
163
    }
200
164
    closedir(proc_dir);
201
 
    proc_dir = NULL;
202
165
  }
203
166
  if(splashy_pid == 0){
204
 
    exitstatus = EX_UNAVAILABLE;
205
 
    goto failure;
 
167
    free(prompt);
 
168
    return EXIT_FAILURE;
206
169
  }
207
170
  
208
171
  /* Set up the signal handler */
211
174
      new_action = { .sa_handler = termination_handler,
212
175
                     .sa_flags = 0 };
213
176
    sigemptyset(&new_action.sa_mask);
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;
 
177
    sigaddset(&new_action.sa_mask, SIGINT);
 
178
    if(ret == -1){
 
179
      perror("sigaddset");
 
180
      free(prompt);
 
181
      return EXIT_FAILURE;
 
182
    }
 
183
    sigaddset(&new_action.sa_mask, SIGHUP);
 
184
    if(ret == -1){
 
185
      perror("sigaddset");
 
186
      free(prompt);
 
187
      return EXIT_FAILURE;
 
188
    }
 
189
    sigaddset(&new_action.sa_mask, SIGTERM);
 
190
    if(ret == -1){
 
191
      perror("sigaddset");
 
192
      free(prompt);
 
193
      return EXIT_FAILURE;
231
194
    }
232
195
    ret = sigaction(SIGINT, NULL, &old_action);
233
196
    if(ret == -1){
234
197
      perror("sigaction");
235
 
      exitstatus = EX_OSERR;
236
 
      goto failure;
 
198
      free(prompt);
 
199
      return EXIT_FAILURE;
237
200
    }
238
201
    if(old_action.sa_handler != SIG_IGN){
239
202
      ret = sigaction(SIGINT, &new_action, NULL);
240
203
      if(ret == -1){
241
204
        perror("sigaction");
242
 
        exitstatus = EX_OSERR;
243
 
        goto failure;
 
205
        free(prompt);
 
206
        return EXIT_FAILURE;
244
207
      }
245
208
    }
246
209
    ret = sigaction(SIGHUP, NULL, &old_action);
247
210
    if(ret == -1){
248
211
      perror("sigaction");
249
 
      exitstatus = EX_OSERR;
250
 
      goto failure;
 
212
      free(prompt);
 
213
      return EXIT_FAILURE;
251
214
    }
252
215
    if(old_action.sa_handler != SIG_IGN){
253
216
      ret = sigaction(SIGHUP, &new_action, NULL);
254
217
      if(ret == -1){
255
218
        perror("sigaction");
256
 
        exitstatus = EX_OSERR;
257
 
        goto failure;
 
219
        free(prompt);
 
220
        return EXIT_FAILURE;
258
221
      }
259
222
    }
260
223
    ret = sigaction(SIGTERM, NULL, &old_action);
261
224
    if(ret == -1){
262
225
      perror("sigaction");
263
 
      exitstatus = EX_OSERR;
264
 
      goto failure;
 
226
      free(prompt);
 
227
      return EXIT_FAILURE;
265
228
    }
266
229
    if(old_action.sa_handler != SIG_IGN){
267
230
      ret = sigaction(SIGTERM, &new_action, NULL);
268
231
      if(ret == -1){
269
232
        perror("sigaction");
270
 
        exitstatus = EX_OSERR;
271
 
        goto failure;
 
233
        free(prompt);
 
234
        return EXIT_FAILURE;
272
235
      }
273
236
    }
274
237
  }
275
238
  
276
 
  if(interrupted_by_signal){
277
 
    goto failure;
278
 
  }
279
 
  
280
239
  /* Fork off the splashy command to prompt for password */
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){
 
240
  pid_t splashy_command_pid = 0;
 
241
  if(not interrupted_by_signal){
 
242
    splashy_command_pid = fork();
 
243
    if(splashy_command_pid == -1){
 
244
      if(not interrupted_by_signal){
 
245
        perror("fork");
 
246
      }
 
247
      return EXIT_FAILURE;
 
248
    }
 
249
    /* Child */
 
250
    if(splashy_command_pid == 0){
293
251
      const char splashy_command[] = "/sbin/splashy_update";
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);
 
252
      ret = execl(splashy_command, splashy_command, prompt,
 
253
                  (char *)NULL);
 
254
      if(not interrupted_by_signal){
 
255
        perror("execl");
319
256
      }
 
257
      free(prompt);
 
258
      _exit(EXIT_FAILURE);
320
259
    }
321
 
    free(prompt);
322
 
    _exit(EXIT_FAILURE);
323
260
  }
324
261
  
325
262
  /* Parent */
326
263
  free(prompt);
327
 
  prompt = NULL;
328
 
  
329
 
  if(interrupted_by_signal){
330
 
    goto failure;
331
 
  }
332
264
  
333
265
  /* Wait for command to complete */
334
 
  {
 
266
  if(not interrupted_by_signal and splashy_command_pid != 0){
335
267
    int status;
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
 
    }
 
268
    ret = waitpid(splashy_command_pid, &status, 0);
343
269
    if(ret == -1){
344
 
      perror("waitpid");
 
270
      if(errno != EINTR){
 
271
        perror("waitpid");
 
272
      }
345
273
      if(errno == ECHILD){
346
274
        splashy_command_pid = 0;
347
275
      }
348
276
    } else {
349
277
      /* The child process has exited */
350
278
      splashy_command_pid = 0;
351
 
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
 
279
      if(not interrupted_by_signal and WIFEXITED(status)
 
280
         and WEXITSTATUS(status)==0){
352
281
        return EXIT_SUCCESS;
353
282
      }
354
283
    }
355
284
  }
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;
 
285
  kill(splashy_pid, SIGTERM);
 
286
  if(interrupted_by_signal and splashy_command_pid != 0){
 
287
    kill(splashy_command_pid, SIGTERM);
 
288
  }
 
289
  sleep(2);
 
290
  while(kill(splashy_pid, 0) == 0){
 
291
    kill(splashy_pid, SIGKILL);
 
292
    sleep(1);
 
293
  }
 
294
  pid_t new_splashy_pid = fork();
 
295
  if(new_splashy_pid == 0){
 
296
    /* Child; will become new splashy process */
 
297
    
 
298
    /* Make the effective user ID (root) the only user ID instead of
 
299
       the real user ID (_mandos) */
 
300
    ret = setuid(geteuid());
 
301
    if(ret == -1){
 
302
      perror("setuid");
 
303
    }
 
304
    
 
305
    setsid();
 
306
    ret = chdir("/");
 
307
/*     if(fork() != 0){ */
 
308
/*       _exit(EXIT_SUCCESS); */
 
309
/*     } */
 
310
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
311
    if(ret == -1){
 
312
      perror("dup2");
 
313
      _exit(EXIT_FAILURE);
 
314
    }
 
315
    
 
316
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
317
    if(not interrupted_by_signal){
 
318
      perror("execl");
 
319
    }
 
320
    _exit(EXIT_FAILURE);
 
321
  }
 
322
  
 
323
  return EXIT_FAILURE;
441
324
}