/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: 2008-09-23 18:46:17 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080923184617-r2rd8r7o2jvgz5ey
* Makefile (PLUGINS, uninstall-client,
            install-client-nokey): Added "plugins.d/splashy".

* plugin-runner.c: Change in comments.

* plugins.d/splashy: Removed; replaced by "splashy.c".
* plugins.d/splashy.c: New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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() */
 
1
#define _GNU_SOURCE             /* asprintf() */
26
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
27
 
                                   sigemptyset(), sigaddset(), SIGINT,
28
 
                                   SIGHUP, SIGTERM, sigaction,
29
 
                                   SIG_IGN, kill(), SIGKILL */
 
3
                                   sigemptyset(), sigaddset(),
 
4
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
 
5
                                   SIGTERM, kill(), SIGKILL */
30
6
#include <stddef.h>             /* NULL */
31
7
#include <stdlib.h>             /* getenv() */
32
8
#include <stdio.h>              /* asprintf(), perror() */
33
 
#include <stdlib.h>             /* EXIT_FAILURE, free(),
34
 
                                   EXIT_SUCCESS */
 
9
#include <stdlib.h>             /* EXIT_FAILURE, EXIT_SUCCESS,
 
10
                                   strtoul(), free() */
35
11
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
36
12
                                   ssize_t */
37
13
#include <dirent.h>             /* opendir(), readdir(), closedir() */
38
 
#include <inttypes.h>           /* intmax_t, strtoimax() */
39
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
40
 
#include <iso646.h>             /* not, or, and */
41
14
#include <unistd.h>             /* readlink(), fork(), execl(),
42
 
                                   sleep(), dup2() STDERR_FILENO,
43
 
                                   STDOUT_FILENO, _exit(),
44
 
                                   pause() */
 
15
                                   _exit */
45
16
#include <string.h>             /* memcmp() */
 
17
#include <iso646.h>             /* and */
46
18
#include <errno.h>              /* errno */
47
19
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
48
20
                                   WEXITSTATUS() */
49
21
 
50
 
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
51
 
                                   EX_UNAVAILABLE */
52
 
 
53
22
sig_atomic_t interrupted_by_signal = 0;
54
 
int signal_received;
55
23
 
56
 
static void termination_handler(int signum){
57
 
  if(interrupted_by_signal){
58
 
    return;
59
 
  }
 
24
static void termination_handler(__attribute__((unused))int signum){
60
25
  interrupted_by_signal = 1;
61
 
  signal_received = signum;
62
26
}
63
27
 
64
 
int main(__attribute__((unused))int argc,
65
 
         __attribute__((unused))char **argv){
 
28
int main(__attribute__((unused))int argc, char **argv){
66
29
  int ret = 0;
 
30
  
 
31
  /* Create prompt string */
67
32
  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
33
  {
75
34
    const char *const cryptsource = getenv("cryptsource");
76
35
    const char *const crypttarget = getenv("crypttarget");
93
52
      }
94
53
    }
95
54
    if(ret == -1){
96
 
      prompt = NULL;
97
 
      exitstatus = EX_OSERR;
98
 
      goto failure;
 
55
      return EXIT_FAILURE;
99
56
    }
100
57
  }
101
58
  
102
59
  /* Find splashy process */
 
60
  pid_t splashy_pid = 0;
103
61
  {
104
62
    const char splashy_name[] = "/sbin/splashy";
105
 
    proc_dir = opendir("/proc");
 
63
    DIR *proc_dir = opendir("/proc");
106
64
    if(proc_dir == NULL){
107
 
      int e = errno;
 
65
      free(prompt);
108
66
      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;
 
67
      return EXIT_FAILURE;
125
68
    }
126
69
    for(struct dirent *proc_ent = readdir(proc_dir);
127
70
        proc_ent != NULL;
128
71
        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;
 
72
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
 
73
      if(pid == 0){
 
74
        /* Not a process */
 
75
        continue;
141
76
      }
142
77
      /* Find the executable name by doing readlink() on the
143
78
         /proc/<pid>/exe link */
 
79
      char *exe_link;
 
80
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
81
      if(ret == -1){
 
82
        perror("asprintf");
 
83
        free(prompt);
 
84
        closedir(proc_dir);
 
85
        return EXIT_FAILURE;
 
86
      }
144
87
      char exe_target[sizeof(splashy_name)];
145
 
      ssize_t sret;
146
 
      {
147
 
        char *exe_link;
148
 
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
149
 
        if(ret == -1){
150
 
          perror("asprintf");
151
 
          exitstatus = EX_OSERR;
152
 
          goto failure;
153
 
        }
154
 
        
155
 
        /* Check that it refers to a symlink owned by root:root */
156
 
        struct stat exe_stat;
157
 
        ret = lstat(exe_link, &exe_stat);
158
 
        if(ret == -1){
159
 
          if(errno == ENOENT){
160
 
            free(exe_link);
161
 
            continue;
162
 
          }
163
 
          int e = errno;
164
 
          perror("lstat");
165
 
          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;
178
 
        }
179
 
        if(not S_ISLNK(exe_stat.st_mode)
180
 
           or exe_stat.st_uid != 0
181
 
           or exe_stat.st_gid != 0){
182
 
          free(exe_link);
183
 
          continue;
184
 
        }
185
 
        
186
 
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
187
 
        free(exe_link);
188
 
      }
 
88
      ssize_t sret = readlink(exe_link, exe_target,
 
89
                              sizeof(exe_target));
189
90
      if((sret == ((ssize_t)sizeof(exe_target)-1))
190
91
         and (memcmp(splashy_name, exe_target,
191
92
                     sizeof(exe_target)-1) == 0)){
194
95
      }
195
96
    }
196
97
    closedir(proc_dir);
197
 
    proc_dir = NULL;
198
98
  }
199
99
  if(splashy_pid == 0){
200
 
    exitstatus = EX_UNAVAILABLE;
201
 
    goto failure;
 
100
    free(prompt);
 
101
    return EXIT_FAILURE;
202
102
  }
203
103
  
204
104
  /* Set up the signal handler */
207
107
      new_action = { .sa_handler = termination_handler,
208
108
                     .sa_flags = 0 };
209
109
    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
 
    }
 
110
    sigaddset(&new_action.sa_mask, SIGINT);
 
111
    sigaddset(&new_action.sa_mask, SIGHUP);
 
112
    sigaddset(&new_action.sa_mask, SIGTERM);
228
113
    ret = sigaction(SIGINT, NULL, &old_action);
229
114
    if(ret == -1){
230
115
      perror("sigaction");
231
 
      exitstatus = EX_OSERR;
232
 
      goto failure;
 
116
      free(prompt);
 
117
      return EXIT_FAILURE;
233
118
    }
234
 
    if(old_action.sa_handler != SIG_IGN){
 
119
    if (old_action.sa_handler != SIG_IGN){
235
120
      ret = sigaction(SIGINT, &new_action, NULL);
236
121
      if(ret == -1){
237
122
        perror("sigaction");
238
 
        exitstatus = EX_OSERR;
239
 
        goto failure;
 
123
        free(prompt);
 
124
        return EXIT_FAILURE;
240
125
      }
241
126
    }
242
127
    ret = sigaction(SIGHUP, NULL, &old_action);
243
128
    if(ret == -1){
244
129
      perror("sigaction");
245
 
      exitstatus = EX_OSERR;
246
 
      goto failure;
 
130
      free(prompt);
 
131
      return EXIT_FAILURE;
247
132
    }
248
 
    if(old_action.sa_handler != SIG_IGN){
 
133
    if (old_action.sa_handler != SIG_IGN){
249
134
      ret = sigaction(SIGHUP, &new_action, NULL);
250
135
      if(ret == -1){
251
136
        perror("sigaction");
252
 
        exitstatus = EX_OSERR;
253
 
        goto failure;
 
137
        free(prompt);
 
138
        return EXIT_FAILURE;
254
139
      }
255
140
    }
256
141
    ret = sigaction(SIGTERM, NULL, &old_action);
257
142
    if(ret == -1){
258
143
      perror("sigaction");
259
 
      exitstatus = EX_OSERR;
260
 
      goto failure;
 
144
      free(prompt);
 
145
      return EXIT_FAILURE;
261
146
    }
262
 
    if(old_action.sa_handler != SIG_IGN){
 
147
    if (old_action.sa_handler != SIG_IGN){
263
148
      ret = sigaction(SIGTERM, &new_action, NULL);
264
149
      if(ret == -1){
265
150
        perror("sigaction");
266
 
        exitstatus = EX_OSERR;
267
 
        goto failure;
 
151
        free(prompt);
 
152
        return EXIT_FAILURE;
268
153
      }
269
154
    }
270
155
  }
271
156
  
272
 
  if(interrupted_by_signal){
273
 
    goto failure;
274
 
  }
275
 
  
276
157
  /* 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){
 
158
  pid_t splashy_command_pid = 0;
 
159
  if(not interrupted_by_signal){
 
160
    splashy_command_pid = fork();
 
161
    if(splashy_command_pid == -1){
 
162
      if(not interrupted_by_signal){
 
163
        perror("fork");
 
164
      }
 
165
      return EXIT_FAILURE;
 
166
    }
 
167
    /* Child */
 
168
    if(splashy_command_pid == 0){
289
169
      const char splashy_command[] = "/sbin/splashy_update";
290
 
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
291
 
      perror("execl");
 
170
      ret = execl(splashy_command, splashy_command, prompt,
 
171
                  (char *)NULL);
 
172
      if(not interrupted_by_signal and errno != ENOENT){
 
173
        /* Don't report "File not found", since splashy might not be
 
174
           installed. */
 
175
        perror("execl");
 
176
      }
 
177
      free(prompt);
 
178
      return EXIT_FAILURE;
292
179
    }
293
 
    free(prompt);
294
 
    _exit(EXIT_FAILURE);
295
180
  }
296
181
  
297
182
  /* Parent */
298
183
  free(prompt);
299
 
  prompt = NULL;
300
 
  
301
 
  if(interrupted_by_signal){
302
 
    goto failure;
303
 
  }
304
184
  
305
185
  /* Wait for command to complete */
306
 
  {
307
 
    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
 
    }
315
 
    if(ret == -1){
316
 
      perror("waitpid");
317
 
      if(errno == ECHILD){
318
 
        splashy_command_pid = 0;
319
 
      }
320
 
    } else {
321
 
      /* The child process has exited */
322
 
      splashy_command_pid = 0;
323
 
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
324
 
        return EXIT_SUCCESS;
325
 
      }
326
 
    }
327
 
  }
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));
 
186
  int status;
 
187
  while(not interrupted_by_signal){
 
188
    waitpid(splashy_command_pid, &status, 0);
 
189
    if(not interrupted_by_signal
 
190
       and WIFEXITED(status) and WEXITSTATUS(status)==0){
 
191
      return EXIT_SUCCESS;
 
192
    }
 
193
  }
 
194
  kill(splashy_pid, SIGTERM);
 
195
  if(interrupted_by_signal){
 
196
    kill(splashy_command_pid, SIGTERM);
 
197
  }
 
198
 
 
199
  pid_t new_splashy_pid = fork();
 
200
  if(new_splashy_pid == 0){
 
201
    while(kill(splashy_pid, 0)){
 
202
      sleep(2);
 
203
      kill(splashy_pid, SIGKILL);
344
204
      sleep(1);
345
205
    }
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));
 
206
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
399
207
    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());
 
208
      perror("dup2");
 
209
      _exit(EXIT_FAILURE);
 
210
    }
 
211
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
410
212
  }
411
213
  
412
 
  return exitstatus;
 
214
  return EXIT_FAILURE;
413
215
}