/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-01-12 22:02:33 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090112220233-lawxo4uvyzb38u1f
* README (The Plugin System): Removed redundant text about options and
                              files for the plugins, this is now
                              documented in the manuals for the
                              plugins.

* plugins.d/mandos-client.c (main): Remove comment which was copied
                                    from another program by mistake.
                                    Use "sscanf" instead of "strtol"
                                    to parse numbers; this uses the
                                    correct type instead of casting.
                                    Don't report errors when removing
                                    temporary directory if directory
                                    is already gone.

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