/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-09-06 05:37:34 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090906053734-uf180lq30ivv4nfy
* plugin-runner.c (getplugin, add_environment, main): Handle EINTR
                                                      properly.

* plugins.d/mandos-client.c (start_mandos_communication): Bug fix:
  move out "decrypted_buffer_size" to where it is needed.

* plugins.d/splashy.c (termination_handler): Save signal received.
  (main): Check return value from "sigaddset()".

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(),
61
61
int main(__attribute__((unused))int argc,
62
62
         __attribute__((unused))char **argv){
63
63
  int ret = 0;
 
64
  
 
65
  /* Create prompt string */
64
66
  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
67
  {
71
68
    const char *const cryptsource = getenv("cryptsource");
72
69
    const char *const crypttarget = getenv("crypttarget");
89
86
      }
90
87
    }
91
88
    if(ret == -1){
92
 
      prompt = NULL;
93
 
      goto failure;
 
89
      return EXIT_FAILURE;
94
90
    }
95
91
  }
96
92
  
97
93
  /* Find splashy process */
 
94
  pid_t splashy_pid = 0;
98
95
  {
99
96
    const char splashy_name[] = "/sbin/splashy";
100
 
    proc_dir = opendir("/proc");
 
97
    DIR *proc_dir = opendir("/proc");
101
98
    if(proc_dir == NULL){
 
99
      free(prompt);
102
100
      perror("opendir");
103
 
      goto failure;
 
101
      return EXIT_FAILURE;
104
102
    }
105
103
    for(struct dirent *proc_ent = readdir(proc_dir);
106
104
        proc_ent != NULL;
127
125
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
128
126
        if(ret == -1){
129
127
          perror("asprintf");
130
 
          goto failure;
 
128
          free(prompt);
 
129
          closedir(proc_dir);
 
130
          return EXIT_FAILURE;
131
131
        }
132
132
        
133
133
        /* Check that it refers to a symlink owned by root:root */
140
140
          }
141
141
          perror("lstat");
142
142
          free(exe_link);
143
 
          goto failure;
 
143
          free(prompt);
 
144
          closedir(proc_dir);
 
145
          return EXIT_FAILURE;
144
146
        }
145
147
        if(not S_ISLNK(exe_stat.st_mode)
146
148
           or exe_stat.st_uid != 0
160
162
      }
161
163
    }
162
164
    closedir(proc_dir);
163
 
    proc_dir = NULL;
164
165
  }
165
166
  if(splashy_pid == 0){
166
 
    goto failure;
 
167
    free(prompt);
 
168
    return EXIT_FAILURE;
167
169
  }
168
170
  
169
171
  /* Set up the signal handler */
175
177
    sigaddset(&new_action.sa_mask, SIGINT);
176
178
    if(ret == -1){
177
179
      perror("sigaddset");
178
 
      goto failure;
 
180
      free(prompt);
 
181
      return EXIT_FAILURE;
179
182
    }
180
183
    sigaddset(&new_action.sa_mask, SIGHUP);
181
184
    if(ret == -1){
182
185
      perror("sigaddset");
183
 
      goto failure;
 
186
      free(prompt);
 
187
      return EXIT_FAILURE;
184
188
    }
185
189
    sigaddset(&new_action.sa_mask, SIGTERM);
186
190
    if(ret == -1){
187
191
      perror("sigaddset");
188
 
      goto failure;
 
192
      free(prompt);
 
193
      return EXIT_FAILURE;
189
194
    }
190
195
    ret = sigaction(SIGINT, NULL, &old_action);
191
196
    if(ret == -1){
192
197
      perror("sigaction");
193
 
      goto failure;
 
198
      free(prompt);
 
199
      return EXIT_FAILURE;
194
200
    }
195
201
    if(old_action.sa_handler != SIG_IGN){
196
202
      ret = sigaction(SIGINT, &new_action, NULL);
197
203
      if(ret == -1){
198
204
        perror("sigaction");
199
 
        goto failure;
 
205
        free(prompt);
 
206
        return EXIT_FAILURE;
200
207
      }
201
208
    }
202
209
    ret = sigaction(SIGHUP, NULL, &old_action);
203
210
    if(ret == -1){
204
211
      perror("sigaction");
205
 
      goto failure;
 
212
      free(prompt);
 
213
      return EXIT_FAILURE;
206
214
    }
207
215
    if(old_action.sa_handler != SIG_IGN){
208
216
      ret = sigaction(SIGHUP, &new_action, NULL);
209
217
      if(ret == -1){
210
218
        perror("sigaction");
211
 
        goto failure;
 
219
        free(prompt);
 
220
        return EXIT_FAILURE;
212
221
      }
213
222
    }
214
223
    ret = sigaction(SIGTERM, NULL, &old_action);
215
224
    if(ret == -1){
216
225
      perror("sigaction");
217
 
      goto failure;
 
226
      free(prompt);
 
227
      return EXIT_FAILURE;
218
228
    }
219
229
    if(old_action.sa_handler != SIG_IGN){
220
230
      ret = sigaction(SIGTERM, &new_action, NULL);
221
231
      if(ret == -1){
222
232
        perror("sigaction");
223
 
        goto failure;
 
233
        free(prompt);
 
234
        return EXIT_FAILURE;
224
235
      }
225
236
    }
226
237
  }
227
238
  
228
 
  if(interrupted_by_signal){
229
 
    goto failure;
230
 
  }
231
 
  
232
239
  /* 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){
 
240
  pid_t splashy_command_pid = 0;
 
241
  if(not interrupted_by_signal){
 
242
    splashy_command_pid = fork();
 
243
    if(splashy_command_pid == -1){
 
244
      if(not interrupted_by_signal){
 
245
        perror("fork");
 
246
      }
 
247
      return EXIT_FAILURE;
 
248
    }
 
249
    /* Child */
 
250
    if(splashy_command_pid == 0){
244
251
      const char splashy_command[] = "/sbin/splashy_update";
245
 
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
246
 
      perror("execl");
 
252
      ret = execl(splashy_command, splashy_command, prompt,
 
253
                  (char *)NULL);
 
254
      if(not interrupted_by_signal){
 
255
        perror("execl");
 
256
      }
 
257
      free(prompt);
 
258
      _exit(EXIT_FAILURE);
247
259
    }
248
 
    free(prompt);
249
 
    _exit(EXIT_FAILURE);
250
260
  }
251
261
  
252
262
  /* Parent */
253
263
  free(prompt);
254
 
  prompt = NULL;
255
 
  
256
 
  if(interrupted_by_signal){
257
 
    goto failure;
258
 
  }
259
264
  
260
265
  /* Wait for command to complete */
261
 
  {
 
266
  if(not interrupted_by_signal and splashy_command_pid != 0){
262
267
    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
 
    }
 
268
    ret = waitpid(splashy_command_pid, &status, 0);
270
269
    if(ret == -1){
271
 
      perror("waitpid");
 
270
      if(errno != EINTR){
 
271
        perror("waitpid");
 
272
      }
272
273
      if(errno == ECHILD){
273
274
        splashy_command_pid = 0;
274
275
      }
275
276
    } else {
276
277
      /* The child process has exited */
277
278
      splashy_command_pid = 0;
278
 
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
 
279
      if(not interrupted_by_signal and WIFEXITED(status)
 
280
         and WEXITSTATUS(status)==0){
279
281
        return EXIT_SUCCESS;
280
282
      }
281
283
    }
282
284
  }
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);
 
285
  kill(splashy_pid, SIGTERM);
 
286
  if(interrupted_by_signal and splashy_command_pid != 0){
 
287
    kill(splashy_command_pid, SIGTERM);
 
288
  }
 
289
  sleep(2);
 
290
  while(kill(splashy_pid, 0) == 0){
 
291
    kill(splashy_pid, SIGKILL);
 
292
    sleep(1);
 
293
  }
 
294
  pid_t new_splashy_pid = fork();
 
295
  if(new_splashy_pid == 0){
 
296
    /* Child; will become new splashy process */
 
297
    
 
298
    /* Make the effective user ID (root) the only user ID instead of
 
299
       the real user ID (_mandos) */
 
300
    ret = setuid(geteuid());
 
301
    if(ret == -1){
 
302
      perror("setuid");
 
303
    }
 
304
    
 
305
    setsid();
 
306
    ret = chdir("/");
 
307
/*     if(fork() != 0){ */
 
308
/*       _exit(EXIT_SUCCESS); */
 
309
/*     } */
 
310
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
 
311
    if(ret == -1){
 
312
      perror("dup2");
 
313
      _exit(EXIT_FAILURE);
 
314
    }
 
315
    
 
316
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
317
    if(not interrupted_by_signal){
327
318
      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());
 
319
    }
 
320
    _exit(EXIT_FAILURE);
349
321
  }
350
322
  
351
323
  return EXIT_FAILURE;