/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-02-25 01:14:29 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090225011429-dwz2bj41zqn0w8eo
* initramfs-tools-hook: Add missing terminating `" when setting
                        mandos_user and mandos_group.

* mandos.conf.xml (SEE ALSO): Improved grammar.

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,
40
41
#include <iso646.h>             /* not, or, and */
41
42
#include <unistd.h>             /* readlink(), fork(), execl(),
42
43
                                   sleep(), dup2() STDERR_FILENO,
43
 
                                   STDOUT_FILENO, _exit(),
44
 
                                   pause() */
 
44
                                   STDOUT_FILENO, _exit() */
45
45
#include <string.h>             /* memcmp() */
46
46
#include <errno.h>              /* errno */
47
47
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
48
48
                                   WEXITSTATUS() */
49
49
 
50
50
sig_atomic_t interrupted_by_signal = 0;
51
 
int signal_received;
52
51
 
53
 
static void termination_handler(int signum){
54
 
  if(interrupted_by_signal){
55
 
    return;
56
 
  }
 
52
static void termination_handler(__attribute__((unused))int signum){
57
53
  interrupted_by_signal = 1;
58
 
  signal_received = signum;
59
54
}
60
55
 
61
56
int main(__attribute__((unused))int argc,
62
57
         __attribute__((unused))char **argv){
63
58
  int ret = 0;
 
59
  
 
60
  /* Create prompt string */
64
61
  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
62
  {
71
63
    const char *const cryptsource = getenv("cryptsource");
72
64
    const char *const crypttarget = getenv("crypttarget");
89
81
      }
90
82
    }
91
83
    if(ret == -1){
92
 
      prompt = NULL;
93
 
      goto failure;
 
84
      return EXIT_FAILURE;
94
85
    }
95
86
  }
96
87
  
97
88
  /* Find splashy process */
 
89
  pid_t splashy_pid = 0;
98
90
  {
99
91
    const char splashy_name[] = "/sbin/splashy";
100
 
    proc_dir = opendir("/proc");
 
92
    DIR *proc_dir = opendir("/proc");
101
93
    if(proc_dir == NULL){
 
94
      free(prompt);
102
95
      perror("opendir");
103
 
      goto failure;
 
96
      return EXIT_FAILURE;
104
97
    }
105
98
    for(struct dirent *proc_ent = readdir(proc_dir);
106
99
        proc_ent != NULL;
127
120
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
128
121
        if(ret == -1){
129
122
          perror("asprintf");
130
 
          goto failure;
 
123
          free(prompt);
 
124
          closedir(proc_dir);
 
125
          return EXIT_FAILURE;
131
126
        }
132
127
        
133
128
        /* Check that it refers to a symlink owned by root:root */
140
135
          }
141
136
          perror("lstat");
142
137
          free(exe_link);
143
 
          goto failure;
 
138
          free(prompt);
 
139
          closedir(proc_dir);
 
140
          return EXIT_FAILURE;
144
141
        }
145
142
        if(not S_ISLNK(exe_stat.st_mode)
146
143
           or exe_stat.st_uid != 0
160
157
      }
161
158
    }
162
159
    closedir(proc_dir);
163
 
    proc_dir = NULL;
164
160
  }
165
161
  if(splashy_pid == 0){
166
 
    goto failure;
 
162
    free(prompt);
 
163
    return EXIT_FAILURE;
167
164
  }
168
165
  
169
166
  /* Set up the signal handler */
172
169
      new_action = { .sa_handler = termination_handler,
173
170
                     .sa_flags = 0 };
174
171
    sigemptyset(&new_action.sa_mask);
175
 
    ret = sigaddset(&new_action.sa_mask, SIGINT);
176
 
    if(ret == -1){
177
 
      perror("sigaddset");
178
 
      goto failure;
179
 
    }
180
 
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
181
 
    if(ret == -1){
182
 
      perror("sigaddset");
183
 
      goto failure;
184
 
    }
185
 
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
186
 
    if(ret == -1){
187
 
      perror("sigaddset");
188
 
      goto failure;
189
 
    }
 
172
    sigaddset(&new_action.sa_mask, SIGINT);
 
173
    sigaddset(&new_action.sa_mask, SIGHUP);
 
174
    sigaddset(&new_action.sa_mask, SIGTERM);
190
175
    ret = sigaction(SIGINT, NULL, &old_action);
191
176
    if(ret == -1){
192
177
      perror("sigaction");
193
 
      goto failure;
 
178
      free(prompt);
 
179
      return EXIT_FAILURE;
194
180
    }
195
181
    if(old_action.sa_handler != SIG_IGN){
196
182
      ret = sigaction(SIGINT, &new_action, NULL);
197
183
      if(ret == -1){
198
184
        perror("sigaction");
199
 
        goto failure;
 
185
        free(prompt);
 
186
        return EXIT_FAILURE;
200
187
      }
201
188
    }
202
189
    ret = sigaction(SIGHUP, NULL, &old_action);
203
190
    if(ret == -1){
204
191
      perror("sigaction");
205
 
      goto failure;
 
192
      free(prompt);
 
193
      return EXIT_FAILURE;
206
194
    }
207
195
    if(old_action.sa_handler != SIG_IGN){
208
196
      ret = sigaction(SIGHUP, &new_action, NULL);
209
197
      if(ret == -1){
210
198
        perror("sigaction");
211
 
        goto failure;
 
199
        free(prompt);
 
200
        return EXIT_FAILURE;
212
201
      }
213
202
    }
214
203
    ret = sigaction(SIGTERM, NULL, &old_action);
215
204
    if(ret == -1){
216
205
      perror("sigaction");
217
 
      goto failure;
 
206
      free(prompt);
 
207
      return EXIT_FAILURE;
218
208
    }
219
209
    if(old_action.sa_handler != SIG_IGN){
220
210
      ret = sigaction(SIGTERM, &new_action, NULL);
221
211
      if(ret == -1){
222
212
        perror("sigaction");
223
 
        goto failure;
 
213
        free(prompt);
 
214
        return EXIT_FAILURE;
224
215
      }
225
216
    }
226
217
  }
227
218
  
228
 
  if(interrupted_by_signal){
229
 
    goto failure;
230
 
  }
231
 
  
232
219
  /* 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){
 
220
  pid_t splashy_command_pid = 0;
 
221
  if(not interrupted_by_signal){
 
222
    splashy_command_pid = fork();
 
223
    if(splashy_command_pid == -1){
 
224
      if(not interrupted_by_signal){
 
225
        perror("fork");
 
226
      }
 
227
      return EXIT_FAILURE;
 
228
    }
 
229
    /* Child */
 
230
    if(splashy_command_pid == 0){
244
231
      const char splashy_command[] = "/sbin/splashy_update";
245
 
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
246
 
      perror("execl");
 
232
      ret = execl(splashy_command, splashy_command, prompt,
 
233
                  (char *)NULL);
 
234
      if(not interrupted_by_signal){
 
235
        perror("execl");
 
236
      }
 
237
      free(prompt);
 
238
      _exit(EXIT_FAILURE);
247
239
    }
248
 
    free(prompt);
249
 
    _exit(EXIT_FAILURE);
250
240
  }
251
241
  
252
242
  /* Parent */
253
243
  free(prompt);
254
 
  prompt = NULL;
255
 
  
256
 
  if(interrupted_by_signal){
257
 
    goto failure;
258
 
  }
259
244
  
260
245
  /* Wait for command to complete */
261
 
  {
 
246
  if(not interrupted_by_signal and splashy_command_pid != 0){
262
247
    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
 
    }
 
248
    ret = waitpid(splashy_command_pid, &status, 0);
270
249
    if(ret == -1){
271
 
      perror("waitpid");
 
250
      if(errno != EINTR){
 
251
        perror("waitpid");
 
252
      }
272
253
      if(errno == ECHILD){
273
254
        splashy_command_pid = 0;
274
255
      }
275
256
    } else {
276
257
      /* The child process has exited */
277
258
      splashy_command_pid = 0;
278
 
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
 
259
      if(not interrupted_by_signal and WIFEXITED(status)
 
260
         and WEXITSTATUS(status)==0){
279
261
        return EXIT_SUCCESS;
280
262
      }
281
263
    }
282
264
  }
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 = (pid_t)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);
 
265
  kill(splashy_pid, SIGTERM);
 
266
  if(interrupted_by_signal and splashy_command_pid != 0){
 
267
    kill(splashy_command_pid, SIGTERM);
 
268
  }
 
269
  sleep(2);
 
270
  while(kill(splashy_pid, 0) == 0){
 
271
    kill(splashy_pid, SIGKILL);
 
272
    sleep(1);
 
273
  }
 
274
  pid_t new_splashy_pid = fork();
 
275
  if(new_splashy_pid == 0){
 
276
    /* Child; will become new splashy process */
 
277
    
 
278
    /* Make the effective user ID (root) the only user ID instead of
 
279
       the real user ID (_mandos) */
 
280
    ret = setuid(geteuid());
 
281
    if(ret == -1){
 
282
      perror("setuid");
 
283
    }
 
284
    
 
285
    setsid();
 
286
    ret = chdir("/");
 
287
/*     if(fork() != 0){ */
 
288
/*       _exit(EXIT_SUCCESS); */
 
289
/*     } */
 
290
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
291
    if(ret == -1){
 
292
      perror("dup2");
 
293
      _exit(EXIT_FAILURE);
 
294
    }
 
295
    
 
296
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
297
    if(not interrupted_by_signal){
327
298
      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 = (int)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());
 
299
    }
 
300
    _exit(EXIT_FAILURE);
349
301
  }
350
302
  
351
303
  return EXIT_FAILURE;