/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

Merge from trunk.  Lots of bug fixes, including Debian bug #546928.

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