/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: 2010-09-01 18:03:03 UTC
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100901180303-u47edb73fczu56ob
bug fixes that prevent problems when runing server as root

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 Teddy Hogeborn
6
 
 * Copyright © 2008 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 <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,
31
30
#include <stddef.h>             /* NULL */
32
31
#include <stdlib.h>             /* getenv() */
33
32
#include <stdio.h>              /* asprintf(), perror() */
34
 
#include <stdlib.h>             /* EXIT_FAILURE, free(), strtoul(),
 
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() */
 
43
                                   STDOUT_FILENO, _exit(),
 
44
                                   pause() */
44
45
#include <string.h>             /* memcmp() */
45
 
#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 */
46
52
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
47
53
                                   WEXITSTATUS() */
 
54
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
 
55
                                   EX_UNAVAILABLE */
48
56
 
49
57
sig_atomic_t interrupted_by_signal = 0;
 
58
int signal_received;
50
59
 
51
 
static void termination_handler(__attribute__((unused))int signum){
 
60
static void termination_handler(int signum){
 
61
  if(interrupted_by_signal){
 
62
    return;
 
63
  }
52
64
  interrupted_by_signal = 1;
 
65
  signal_received = signum;
53
66
}
54
67
 
55
68
int main(__attribute__((unused))int argc,
56
69
         __attribute__((unused))char **argv){
57
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;
58
76
  
59
77
  /* Create prompt string */
60
 
  char *prompt = NULL;
61
78
  {
62
79
    const char *const cryptsource = getenv("cryptsource");
63
80
    const char *const crypttarget = getenv("crypttarget");
80
97
      }
81
98
    }
82
99
    if(ret == -1){
83
 
      return EXIT_FAILURE;
 
100
      prompt = NULL;
 
101
      exitstatus = EX_OSERR;
 
102
      goto failure;
84
103
    }
85
104
  }
86
105
  
87
106
  /* Find splashy process */
88
 
  pid_t splashy_pid = 0;
89
107
  {
90
108
    const char splashy_name[] = "/sbin/splashy";
91
 
    DIR *proc_dir = opendir("/proc");
 
109
    proc_dir = opendir("/proc");
92
110
    if(proc_dir == NULL){
93
 
      free(prompt);
 
111
      int e = errno;
94
112
      perror("opendir");
95
 
      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;
96
129
    }
97
130
    for(struct dirent *proc_ent = readdir(proc_dir);
98
131
        proc_ent != NULL;
99
132
        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;
 
133
      pid_t pid;
 
134
      {
 
135
        intmax_t tmpmax;
 
136
        char *tmp;
 
137
        errno = 0;
 
138
        tmpmax = strtoimax(proc_ent->d_name, &tmp, 10);
 
139
        if(errno != 0 or tmp == proc_ent->d_name or *tmp != '\0'
 
140
           or tmpmax != (pid_t)tmpmax){
 
141
          /* Not a process */
 
142
          continue;
 
143
        }
 
144
        pid = (pid_t)tmpmax;
104
145
      }
105
146
      /* Find the executable name by doing readlink() on the
106
147
         /proc/<pid>/exe link */
111
152
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
112
153
        if(ret == -1){
113
154
          perror("asprintf");
114
 
          free(prompt);
115
 
          closedir(proc_dir);
116
 
          return EXIT_FAILURE;
 
155
          exitstatus = EX_OSERR;
 
156
          goto failure;
117
157
        }
118
158
        
119
159
        /* Check that it refers to a symlink owned by root:root */
120
160
        struct stat exe_stat;
121
161
        ret = lstat(exe_link, &exe_stat);
122
162
        if(ret == -1){
 
163
          if(errno == ENOENT){
 
164
            free(exe_link);
 
165
            continue;
 
166
          }
 
167
          int e = errno;
123
168
          perror("lstat");
124
169
          free(exe_link);
125
 
          free(prompt);
126
 
          closedir(proc_dir);
127
 
          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;
128
182
        }
129
183
        if(not S_ISLNK(exe_stat.st_mode)
130
184
           or exe_stat.st_uid != 0
144
198
      }
145
199
    }
146
200
    closedir(proc_dir);
 
201
    proc_dir = NULL;
147
202
  }
148
203
  if(splashy_pid == 0){
149
 
    free(prompt);
150
 
    return EXIT_FAILURE;
 
204
    exitstatus = EX_UNAVAILABLE;
 
205
    goto failure;
151
206
  }
152
207
  
153
208
  /* Set up the signal handler */
156
211
      new_action = { .sa_handler = termination_handler,
157
212
                     .sa_flags = 0 };
158
213
    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);
 
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
    }
162
232
    ret = sigaction(SIGINT, NULL, &old_action);
163
233
    if(ret == -1){
164
234
      perror("sigaction");
165
 
      free(prompt);
166
 
      return EXIT_FAILURE;
 
235
      exitstatus = EX_OSERR;
 
236
      goto failure;
167
237
    }
168
238
    if(old_action.sa_handler != SIG_IGN){
169
239
      ret = sigaction(SIGINT, &new_action, NULL);
170
240
      if(ret == -1){
171
241
        perror("sigaction");
172
 
        free(prompt);
173
 
        return EXIT_FAILURE;
 
242
        exitstatus = EX_OSERR;
 
243
        goto failure;
174
244
      }
175
245
    }
176
246
    ret = sigaction(SIGHUP, NULL, &old_action);
177
247
    if(ret == -1){
178
248
      perror("sigaction");
179
 
      free(prompt);
180
 
      return EXIT_FAILURE;
 
249
      exitstatus = EX_OSERR;
 
250
      goto failure;
181
251
    }
182
252
    if(old_action.sa_handler != SIG_IGN){
183
253
      ret = sigaction(SIGHUP, &new_action, NULL);
184
254
      if(ret == -1){
185
255
        perror("sigaction");
186
 
        free(prompt);
187
 
        return EXIT_FAILURE;
 
256
        exitstatus = EX_OSERR;
 
257
        goto failure;
188
258
      }
189
259
    }
190
260
    ret = sigaction(SIGTERM, NULL, &old_action);
191
261
    if(ret == -1){
192
262
      perror("sigaction");
193
 
      free(prompt);
194
 
      return EXIT_FAILURE;
 
263
      exitstatus = EX_OSERR;
 
264
      goto failure;
195
265
    }
196
266
    if(old_action.sa_handler != SIG_IGN){
197
267
      ret = sigaction(SIGTERM, &new_action, NULL);
198
268
      if(ret == -1){
199
269
        perror("sigaction");
200
 
        free(prompt);
201
 
        return EXIT_FAILURE;
 
270
        exitstatus = EX_OSERR;
 
271
        goto failure;
202
272
      }
203
273
    }
204
274
  }
205
275
  
 
276
  if(interrupted_by_signal){
 
277
    goto failure;
 
278
  }
 
279
  
206
280
  /* 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){
 
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){
218
293
      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");
 
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);
223
319
      }
224
 
      free(prompt);
225
 
      _exit(EXIT_FAILURE);
226
320
    }
 
321
    free(prompt);
 
322
    _exit(EXIT_FAILURE);
227
323
  }
228
324
  
229
325
  /* Parent */
230
326
  free(prompt);
 
327
  prompt = NULL;
 
328
  
 
329
  if(interrupted_by_signal){
 
330
    goto failure;
 
331
  }
231
332
  
232
333
  /* Wait for command to complete */
233
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
334
  {
234
335
    int status;
235
 
    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
    }
236
343
    if(ret == -1){
237
 
      if(errno != EINTR){
238
 
        perror("waitpid");
239
 
      }
 
344
      perror("waitpid");
240
345
      if(errno == ECHILD){
241
346
        splashy_command_pid = 0;
242
347
      }
243
348
    } else {
244
349
      /* The child process has exited */
245
350
      splashy_command_pid = 0;
246
 
      if(not interrupted_by_signal and WIFEXITED(status)
247
 
         and WEXITSTATUS(status)==0){
 
351
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
248
352
        return EXIT_SUCCESS;
249
353
      }
250
354
    }
251
355
  }
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;
 
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;
291
441
}