/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

* plugins.d/splashy.c: Use exit codes from <sysexits.h>.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#define _GNU_SOURCE             /* asprintf() */
 
1
/*  -*- coding: utf-8 -*- */
 
2
/*
 
3
 * Splashy - Read a password from splashy and output it
 
4
 * 
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
 
7
 * 
 
8
 * This program is free software: you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation, either version 3 of the
 
11
 * License, or (at your option) any later version.
 
12
 * 
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 * 
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program.  If not, see
 
20
 * <http://www.gnu.org/licenses/>.
 
21
 * 
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
23
 */
 
24
 
 
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
2
26
#include <signal.h>             /* sig_atomic_t, struct sigaction,
3
27
                                   sigemptyset(), sigaddset(), SIGINT,
4
28
                                   SIGHUP, SIGTERM, sigaction,
6
30
#include <stddef.h>             /* NULL */
7
31
#include <stdlib.h>             /* getenv() */
8
32
#include <stdio.h>              /* asprintf(), perror() */
9
 
#include <stdlib.h>             /* EXIT_FAILURE, free(), strtoul(),
 
33
#include <stdlib.h>             /* EXIT_FAILURE, free(),
10
34
                                   EXIT_SUCCESS */
11
35
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
12
36
                                   ssize_t */
13
37
#include <dirent.h>             /* opendir(), readdir(), closedir() */
 
38
#include <inttypes.h>           /* intmax_t, strtoimax() */
14
39
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
15
40
#include <iso646.h>             /* not, or, and */
16
41
#include <unistd.h>             /* readlink(), fork(), execl(),
17
42
                                   sleep(), dup2() STDERR_FILENO,
18
 
                                   STDOUT_FILENO, _exit() */
 
43
                                   STDOUT_FILENO, _exit(),
 
44
                                   pause() */
19
45
#include <string.h>             /* memcmp() */
20
46
#include <errno.h>              /* errno */
21
47
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
22
48
                                   WEXITSTATUS() */
23
49
 
 
50
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
 
51
                                   EX_UNAVAILABLE */
 
52
 
24
53
sig_atomic_t interrupted_by_signal = 0;
 
54
int signal_received;
25
55
 
26
 
static void termination_handler(__attribute__((unused))int signum){
 
56
static void termination_handler(int signum){
 
57
  if(interrupted_by_signal){
 
58
    return;
 
59
  }
27
60
  interrupted_by_signal = 1;
 
61
  signal_received = signum;
28
62
}
29
63
 
30
64
int main(__attribute__((unused))int argc,
31
65
         __attribute__((unused))char **argv){
32
66
  int ret = 0;
 
67
  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;
33
72
  
34
73
  /* Create prompt string */
35
 
  char *prompt = NULL;
36
74
  {
37
75
    const char *const cryptsource = getenv("cryptsource");
38
76
    const char *const crypttarget = getenv("crypttarget");
55
93
      }
56
94
    }
57
95
    if(ret == -1){
58
 
      return EXIT_FAILURE;
 
96
      prompt = NULL;
 
97
      exitstatus = EX_OSERR;
 
98
      goto failure;
59
99
    }
60
100
  }
61
101
  
62
102
  /* Find splashy process */
63
 
  pid_t splashy_pid = 0;
64
103
  {
65
104
    const char splashy_name[] = "/sbin/splashy";
66
 
    DIR *proc_dir = opendir("/proc");
 
105
    proc_dir = opendir("/proc");
67
106
    if(proc_dir == NULL){
68
 
      free(prompt);
 
107
      int e = errno;
69
108
      perror("opendir");
70
 
      return EXIT_FAILURE;
 
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;
71
125
    }
72
126
    for(struct dirent *proc_ent = readdir(proc_dir);
73
127
        proc_ent != NULL;
74
128
        proc_ent = readdir(proc_dir)){
75
 
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
76
 
      if(pid == 0){
77
 
        /* Not a process */
78
 
        continue;
 
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;
79
141
      }
80
142
      /* Find the executable name by doing readlink() on the
81
143
         /proc/<pid>/exe link */
86
148
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
87
149
        if(ret == -1){
88
150
          perror("asprintf");
89
 
          free(prompt);
90
 
          closedir(proc_dir);
91
 
          return EXIT_FAILURE;
 
151
          exitstatus = EX_OSERR;
 
152
          goto failure;
92
153
        }
93
154
        
94
155
        /* Check that it refers to a symlink owned by root:root */
95
156
        struct stat exe_stat;
96
157
        ret = lstat(exe_link, &exe_stat);
97
158
        if(ret == -1){
 
159
          if(errno == ENOENT){
 
160
            free(exe_link);
 
161
            continue;
 
162
          }
 
163
          int e = errno;
98
164
          perror("lstat");
99
165
          free(exe_link);
100
 
          free(prompt);
101
 
          closedir(proc_dir);
102
 
          return EXIT_FAILURE;
 
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;
103
178
        }
104
179
        if(not S_ISLNK(exe_stat.st_mode)
105
180
           or exe_stat.st_uid != 0
119
194
      }
120
195
    }
121
196
    closedir(proc_dir);
 
197
    proc_dir = NULL;
122
198
  }
123
199
  if(splashy_pid == 0){
124
 
    free(prompt);
125
 
    return EXIT_FAILURE;
 
200
    exitstatus = EX_UNAVAILABLE;
 
201
    goto failure;
126
202
  }
127
203
  
128
204
  /* Set up the signal handler */
131
207
      new_action = { .sa_handler = termination_handler,
132
208
                     .sa_flags = 0 };
133
209
    sigemptyset(&new_action.sa_mask);
134
 
    sigaddset(&new_action.sa_mask, SIGINT);
135
 
    sigaddset(&new_action.sa_mask, SIGHUP);
136
 
    sigaddset(&new_action.sa_mask, SIGTERM);
 
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
    }
137
228
    ret = sigaction(SIGINT, NULL, &old_action);
138
229
    if(ret == -1){
139
230
      perror("sigaction");
140
 
      free(prompt);
141
 
      return EXIT_FAILURE;
 
231
      exitstatus = EX_OSERR;
 
232
      goto failure;
142
233
    }
143
234
    if(old_action.sa_handler != SIG_IGN){
144
235
      ret = sigaction(SIGINT, &new_action, NULL);
145
236
      if(ret == -1){
146
237
        perror("sigaction");
147
 
        free(prompt);
148
 
        return EXIT_FAILURE;
 
238
        exitstatus = EX_OSERR;
 
239
        goto failure;
149
240
      }
150
241
    }
151
242
    ret = sigaction(SIGHUP, NULL, &old_action);
152
243
    if(ret == -1){
153
244
      perror("sigaction");
154
 
      free(prompt);
155
 
      return EXIT_FAILURE;
 
245
      exitstatus = EX_OSERR;
 
246
      goto failure;
156
247
    }
157
248
    if(old_action.sa_handler != SIG_IGN){
158
249
      ret = sigaction(SIGHUP, &new_action, NULL);
159
250
      if(ret == -1){
160
251
        perror("sigaction");
161
 
        free(prompt);
162
 
        return EXIT_FAILURE;
 
252
        exitstatus = EX_OSERR;
 
253
        goto failure;
163
254
      }
164
255
    }
165
256
    ret = sigaction(SIGTERM, NULL, &old_action);
166
257
    if(ret == -1){
167
258
      perror("sigaction");
168
 
      free(prompt);
169
 
      return EXIT_FAILURE;
 
259
      exitstatus = EX_OSERR;
 
260
      goto failure;
170
261
    }
171
262
    if(old_action.sa_handler != SIG_IGN){
172
263
      ret = sigaction(SIGTERM, &new_action, NULL);
173
264
      if(ret == -1){
174
265
        perror("sigaction");
175
 
        free(prompt);
176
 
        return EXIT_FAILURE;
 
266
        exitstatus = EX_OSERR;
 
267
        goto failure;
177
268
      }
178
269
    }
179
270
  }
180
271
  
 
272
  if(interrupted_by_signal){
 
273
    goto failure;
 
274
  }
 
275
  
181
276
  /* Fork off the splashy command to prompt for password */
182
 
  pid_t splashy_command_pid = 0;
183
 
  if(not interrupted_by_signal){
184
 
    splashy_command_pid = fork();
185
 
    if(splashy_command_pid == -1){
186
 
      if(not interrupted_by_signal){
187
 
        perror("fork");
188
 
      }
189
 
      return EXIT_FAILURE;
190
 
    }
191
 
    /* Child */
192
 
    if(splashy_command_pid == 0){
 
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){
193
289
      const char splashy_command[] = "/sbin/splashy_update";
194
 
      ret = execl(splashy_command, splashy_command, prompt,
195
 
                  (char *)NULL);
196
 
      if(not interrupted_by_signal){
197
 
        perror("execl");
198
 
      }
199
 
      free(prompt);
200
 
      _exit(EXIT_FAILURE);
 
290
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
 
291
      perror("execl");
201
292
    }
 
293
    free(prompt);
 
294
    _exit(EXIT_FAILURE);
202
295
  }
203
296
  
204
297
  /* Parent */
205
298
  free(prompt);
 
299
  prompt = NULL;
 
300
  
 
301
  if(interrupted_by_signal){
 
302
    goto failure;
 
303
  }
206
304
  
207
305
  /* Wait for command to complete */
208
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
306
  {
209
307
    int status;
210
 
    ret = waitpid(splashy_command_pid, &status, 0);
 
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
    }
211
315
    if(ret == -1){
212
 
      if(errno != EINTR){
213
 
        perror("waitpid");
214
 
      }
 
316
      perror("waitpid");
215
317
      if(errno == ECHILD){
216
318
        splashy_command_pid = 0;
217
319
      }
218
320
    } else {
219
321
      /* The child process has exited */
220
322
      splashy_command_pid = 0;
221
 
      if(not interrupted_by_signal and WIFEXITED(status)
222
 
         and WEXITSTATUS(status)==0){
 
323
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
223
324
        return EXIT_SUCCESS;
224
325
      }
225
326
    }
226
327
  }
227
 
  kill(splashy_pid, SIGTERM);
228
 
  if(interrupted_by_signal and splashy_command_pid != 0){
229
 
    kill(splashy_command_pid, SIGTERM);
230
 
  }
231
 
  sleep(2);
232
 
  while(kill(splashy_pid, 0) == 0){
233
 
    kill(splashy_pid, SIGKILL);
234
 
    sleep(1);
235
 
  }
236
 
  pid_t new_splashy_pid = fork();
237
 
  if(new_splashy_pid == 0){
238
 
    /* Child; will become new splashy process */
239
 
    
240
 
    /* Make the effective user ID (root) the only user ID instead of
241
 
       the real user ID (mandos) */
242
 
    ret = setuid(geteuid());
243
 
    if(ret == -1){
244
 
      perror("setuid");
245
 
    }
246
 
    
247
 
    setsid();
248
 
    ret = chdir("/");
249
 
/*     if(fork() != 0){ */
250
 
/*       _exit(EXIT_SUCCESS); */
251
 
/*     } */
252
 
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
253
 
    if(ret == -1){
254
 
      perror("dup2");
255
 
      _exit(EXIT_FAILURE);
256
 
    }
257
 
    
258
 
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
259
 
    if(not interrupted_by_signal){
260
 
      perror("execl");
261
 
    }
262
 
    _exit(EXIT_FAILURE);
263
 
  }
264
 
  
265
 
  return EXIT_FAILURE;
 
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;
266
413
}