/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

* plugin-runner.c: Minor stylistic changes.

* plugins.d/askpass-fifo.c: Changed contact information and did minor
                            stylistic changes.

* plugins.d/mandos-client.c: Minor stylistic changes.

* plugins.d/password-prompt.c: Changed contact information.

* plugins.d/splashy.c: Changed contact information.
  (main): Changed error handling design.  Bug fix: Will now be much
          more tolerant against signals.  Bug fix: Will now re-raise
          signal received.

* plugins.d/usplash.c: Changed contact information and did minor
                       stylistic changes.

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 <https://www.fukt.bsnet.se/~belorn/> and
23
 
 * <https://www.fukt.bsnet.se/~teddy/>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
24
23
 */
25
24
 
26
 
#define _GNU_SOURCE             /* asprintf() */
 
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
27
26
#include <signal.h>             /* sig_atomic_t, struct sigaction,
28
27
                                   sigemptyset(), sigaddset(), SIGINT,
29
28
                                   SIGHUP, SIGTERM, sigaction,
41
40
#include <iso646.h>             /* not, or, and */
42
41
#include <unistd.h>             /* readlink(), fork(), execl(),
43
42
                                   sleep(), dup2() STDERR_FILENO,
44
 
                                   STDOUT_FILENO, _exit() */
 
43
                                   STDOUT_FILENO, _exit(),
 
44
                                   pause() */
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
  char *prompt = NULL;
 
65
  DIR *proc_dir = NULL;
 
66
  pid_t splashy_pid = 0;
 
67
  pid_t splashy_command_pid = 0;
64
68
  
65
69
  /* Create prompt string */
66
 
  char *prompt = NULL;
67
70
  {
68
71
    const char *const cryptsource = getenv("cryptsource");
69
72
    const char *const crypttarget = getenv("crypttarget");
86
89
      }
87
90
    }
88
91
    if(ret == -1){
89
 
      return EXIT_FAILURE;
 
92
      prompt = NULL;
 
93
      goto failure;
90
94
    }
91
95
  }
92
96
  
93
97
  /* Find splashy process */
94
 
  pid_t splashy_pid = 0;
95
98
  {
96
99
    const char splashy_name[] = "/sbin/splashy";
97
 
    DIR *proc_dir = opendir("/proc");
 
100
    proc_dir = opendir("/proc");
98
101
    if(proc_dir == NULL){
99
 
      free(prompt);
100
102
      perror("opendir");
101
 
      return EXIT_FAILURE;
 
103
      goto failure;
102
104
    }
103
105
    for(struct dirent *proc_ent = readdir(proc_dir);
104
106
        proc_ent != NULL;
125
127
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
126
128
        if(ret == -1){
127
129
          perror("asprintf");
128
 
          free(prompt);
129
 
          closedir(proc_dir);
130
 
          return EXIT_FAILURE;
 
130
          goto 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
 
          free(prompt);
144
 
          closedir(proc_dir);
145
 
          return EXIT_FAILURE;
 
143
          goto failure;
146
144
        }
147
145
        if(not S_ISLNK(exe_stat.st_mode)
148
146
           or exe_stat.st_uid != 0
162
160
      }
163
161
    }
164
162
    closedir(proc_dir);
 
163
    proc_dir = NULL;
165
164
  }
166
165
  if(splashy_pid == 0){
167
 
    free(prompt);
168
 
    return EXIT_FAILURE;
 
166
    goto failure;
169
167
  }
170
168
  
171
169
  /* Set up the signal handler */
177
175
    sigaddset(&new_action.sa_mask, SIGINT);
178
176
    if(ret == -1){
179
177
      perror("sigaddset");
180
 
      free(prompt);
181
 
      return EXIT_FAILURE;
 
178
      goto failure;
182
179
    }
183
180
    sigaddset(&new_action.sa_mask, SIGHUP);
184
181
    if(ret == -1){
185
182
      perror("sigaddset");
186
 
      free(prompt);
187
 
      return EXIT_FAILURE;
 
183
      goto failure;
188
184
    }
189
185
    sigaddset(&new_action.sa_mask, SIGTERM);
190
186
    if(ret == -1){
191
187
      perror("sigaddset");
192
 
      free(prompt);
193
 
      return EXIT_FAILURE;
 
188
      goto failure;
194
189
    }
195
190
    ret = sigaction(SIGINT, NULL, &old_action);
196
191
    if(ret == -1){
197
192
      perror("sigaction");
198
 
      free(prompt);
199
 
      return EXIT_FAILURE;
 
193
      goto failure;
200
194
    }
201
195
    if(old_action.sa_handler != SIG_IGN){
202
196
      ret = sigaction(SIGINT, &new_action, NULL);
203
197
      if(ret == -1){
204
198
        perror("sigaction");
205
 
        free(prompt);
206
 
        return EXIT_FAILURE;
 
199
        goto failure;
207
200
      }
208
201
    }
209
202
    ret = sigaction(SIGHUP, NULL, &old_action);
210
203
    if(ret == -1){
211
204
      perror("sigaction");
212
 
      free(prompt);
213
 
      return EXIT_FAILURE;
 
205
      goto failure;
214
206
    }
215
207
    if(old_action.sa_handler != SIG_IGN){
216
208
      ret = sigaction(SIGHUP, &new_action, NULL);
217
209
      if(ret == -1){
218
210
        perror("sigaction");
219
 
        free(prompt);
220
 
        return EXIT_FAILURE;
 
211
        goto failure;
221
212
      }
222
213
    }
223
214
    ret = sigaction(SIGTERM, NULL, &old_action);
224
215
    if(ret == -1){
225
216
      perror("sigaction");
226
 
      free(prompt);
227
 
      return EXIT_FAILURE;
 
217
      goto failure;
228
218
    }
229
219
    if(old_action.sa_handler != SIG_IGN){
230
220
      ret = sigaction(SIGTERM, &new_action, NULL);
231
221
      if(ret == -1){
232
222
        perror("sigaction");
233
 
        free(prompt);
234
 
        return EXIT_FAILURE;
 
223
        goto failure;
235
224
      }
236
225
    }
237
226
  }
238
227
  
 
228
  if(interrupted_by_signal){
 
229
    goto failure;
 
230
  }
 
231
  
239
232
  /* Fork off the splashy command to prompt for password */
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){
 
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){
251
244
      const char splashy_command[] = "/sbin/splashy_update";
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);
 
245
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
 
246
      perror("execl");
259
247
    }
 
248
    free(prompt);
 
249
    _exit(EXIT_FAILURE);
260
250
  }
261
251
  
262
252
  /* Parent */
263
253
  free(prompt);
 
254
  prompt = NULL;
 
255
  
 
256
  if(interrupted_by_signal){
 
257
    goto failure;
 
258
  }
264
259
  
265
260
  /* Wait for command to complete */
266
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
261
  {
267
262
    int status;
268
 
    ret = waitpid(splashy_command_pid, &status, 0);
 
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
    }
269
270
    if(ret == -1){
270
 
      if(errno != EINTR){
271
 
        perror("waitpid");
272
 
      }
 
271
      perror("waitpid");
273
272
      if(errno == ECHILD){
274
273
        splashy_command_pid = 0;
275
274
      }
276
275
    } else {
277
276
      /* The child process has exited */
278
277
      splashy_command_pid = 0;
279
 
      if(not interrupted_by_signal and WIFEXITED(status)
280
 
         and WEXITSTATUS(status)==0){
 
278
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
281
279
        return EXIT_SUCCESS;
282
280
      }
283
281
    }
284
282
  }
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 */
 
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));
297
294
    
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");
 
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);
303
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
      }
304
325
    
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");
 
326
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
327
      perror("execl");
313
328
      _exit(EXIT_FAILURE);
314
329
    }
315
 
    
316
 
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
317
 
    if(not interrupted_by_signal){
318
 
      perror("execl");
319
 
    }
320
 
    _exit(EXIT_FAILURE);
 
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());
321
349
  }
322
350
  
323
351
  return EXIT_FAILURE;