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