/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-10-18 08:47:40 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091018084740-fa1qgm22lg125r10
* plugins.d/splashy.c: Use exit codes from <sysexits.h>.

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
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
 
51
                                   EX_UNAVAILABLE */
 
52
 
50
53
sig_atomic_t interrupted_by_signal = 0;
 
54
int signal_received;
51
55
 
52
 
static void termination_handler(__attribute__((unused))int signum){
 
56
static void termination_handler(int signum){
 
57
  if(interrupted_by_signal){
 
58
    return;
 
59
  }
53
60
  interrupted_by_signal = 1;
 
61
  signal_received = signum;
54
62
}
55
63
 
56
64
int main(__attribute__((unused))int argc,
57
65
         __attribute__((unused))char **argv){
58
66
  int ret = 0;
 
67
  char *prompt = NULL;
 
68
  DIR *proc_dir = NULL;
 
69
  pid_t splashy_pid = 0;
 
70
  pid_t splashy_command_pid = 0;
 
71
  int exitstatus = EXIT_FAILURE;
59
72
  
60
73
  /* Create prompt string */
61
 
  char *prompt = NULL;
62
74
  {
63
75
    const char *const cryptsource = getenv("cryptsource");
64
76
    const char *const crypttarget = getenv("crypttarget");
81
93
      }
82
94
    }
83
95
    if(ret == -1){
84
 
      return EXIT_FAILURE;
 
96
      prompt = NULL;
 
97
      exitstatus = EX_OSERR;
 
98
      goto failure;
85
99
    }
86
100
  }
87
101
  
88
102
  /* Find splashy process */
89
 
  pid_t splashy_pid = 0;
90
103
  {
91
104
    const char splashy_name[] = "/sbin/splashy";
92
 
    DIR *proc_dir = opendir("/proc");
 
105
    proc_dir = opendir("/proc");
93
106
    if(proc_dir == NULL){
94
 
      free(prompt);
 
107
      int e = errno;
95
108
      perror("opendir");
96
 
      return EXIT_FAILURE;
 
109
      switch(e){
 
110
      case EACCES:
 
111
      case ENOTDIR:
 
112
      case ELOOP:
 
113
      case ENOENT:
 
114
      default:
 
115
        exitstatus = EX_OSFILE;
 
116
        break;
 
117
      case ENAMETOOLONG:
 
118
      case EMFILE:
 
119
      case ENFILE:
 
120
      case ENOMEM:
 
121
        exitstatus = EX_OSERR;
 
122
        break;
 
123
      }
 
124
      goto failure;
97
125
    }
98
126
    for(struct dirent *proc_ent = readdir(proc_dir);
99
127
        proc_ent != NULL;
120
148
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
121
149
        if(ret == -1){
122
150
          perror("asprintf");
123
 
          free(prompt);
124
 
          closedir(proc_dir);
125
 
          return EXIT_FAILURE;
 
151
          exitstatus = EX_OSERR;
 
152
          goto failure;
126
153
        }
127
154
        
128
155
        /* Check that it refers to a symlink owned by root:root */
133
160
            free(exe_link);
134
161
            continue;
135
162
          }
 
163
          int e = errno;
136
164
          perror("lstat");
137
165
          free(exe_link);
138
 
          free(prompt);
139
 
          closedir(proc_dir);
140
 
          return EXIT_FAILURE;
 
166
          switch(e){
 
167
          case EACCES:
 
168
          case ENOTDIR:
 
169
          case ELOOP:
 
170
          default:
 
171
            exitstatus = EX_OSFILE;
 
172
            break;
 
173
          case ENAMETOOLONG:
 
174
            exitstatus = EX_OSERR;
 
175
            break;
 
176
          }
 
177
          goto failure;
141
178
        }
142
179
        if(not S_ISLNK(exe_stat.st_mode)
143
180
           or exe_stat.st_uid != 0
157
194
      }
158
195
    }
159
196
    closedir(proc_dir);
 
197
    proc_dir = NULL;
160
198
  }
161
199
  if(splashy_pid == 0){
162
 
    free(prompt);
163
 
    return EXIT_FAILURE;
 
200
    exitstatus = EX_UNAVAILABLE;
 
201
    goto failure;
164
202
  }
165
203
  
166
204
  /* Set up the signal handler */
169
207
      new_action = { .sa_handler = termination_handler,
170
208
                     .sa_flags = 0 };
171
209
    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);
 
210
    ret = sigaddset(&new_action.sa_mask, SIGINT);
 
211
    if(ret == -1){
 
212
      perror("sigaddset");
 
213
      exitstatus = EX_OSERR;
 
214
      goto failure;
 
215
    }
 
216
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
 
217
    if(ret == -1){
 
218
      perror("sigaddset");
 
219
      exitstatus = EX_OSERR;
 
220
      goto failure;
 
221
    }
 
222
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
 
223
    if(ret == -1){
 
224
      perror("sigaddset");
 
225
      exitstatus = EX_OSERR;
 
226
      goto failure;
 
227
    }
175
228
    ret = sigaction(SIGINT, NULL, &old_action);
176
229
    if(ret == -1){
177
230
      perror("sigaction");
178
 
      free(prompt);
179
 
      return EXIT_FAILURE;
 
231
      exitstatus = EX_OSERR;
 
232
      goto failure;
180
233
    }
181
234
    if(old_action.sa_handler != SIG_IGN){
182
235
      ret = sigaction(SIGINT, &new_action, NULL);
183
236
      if(ret == -1){
184
237
        perror("sigaction");
185
 
        free(prompt);
186
 
        return EXIT_FAILURE;
 
238
        exitstatus = EX_OSERR;
 
239
        goto failure;
187
240
      }
188
241
    }
189
242
    ret = sigaction(SIGHUP, NULL, &old_action);
190
243
    if(ret == -1){
191
244
      perror("sigaction");
192
 
      free(prompt);
193
 
      return EXIT_FAILURE;
 
245
      exitstatus = EX_OSERR;
 
246
      goto failure;
194
247
    }
195
248
    if(old_action.sa_handler != SIG_IGN){
196
249
      ret = sigaction(SIGHUP, &new_action, NULL);
197
250
      if(ret == -1){
198
251
        perror("sigaction");
199
 
        free(prompt);
200
 
        return EXIT_FAILURE;
 
252
        exitstatus = EX_OSERR;
 
253
        goto failure;
201
254
      }
202
255
    }
203
256
    ret = sigaction(SIGTERM, NULL, &old_action);
204
257
    if(ret == -1){
205
258
      perror("sigaction");
206
 
      free(prompt);
207
 
      return EXIT_FAILURE;
 
259
      exitstatus = EX_OSERR;
 
260
      goto failure;
208
261
    }
209
262
    if(old_action.sa_handler != SIG_IGN){
210
263
      ret = sigaction(SIGTERM, &new_action, NULL);
211
264
      if(ret == -1){
212
265
        perror("sigaction");
213
 
        free(prompt);
214
 
        return EXIT_FAILURE;
 
266
        exitstatus = EX_OSERR;
 
267
        goto failure;
215
268
      }
216
269
    }
217
270
  }
218
271
  
 
272
  if(interrupted_by_signal){
 
273
    goto failure;
 
274
  }
 
275
  
219
276
  /* 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){
 
277
  splashy_command_pid = fork();
 
278
  if(splashy_command_pid != 0 and interrupted_by_signal){
 
279
    goto failure;
 
280
  }
 
281
  if(splashy_command_pid == -1){
 
282
    perror("fork");
 
283
    exitstatus = EX_OSERR;
 
284
    goto failure;
 
285
  }
 
286
  /* Child */
 
287
  if(splashy_command_pid == 0){
 
288
    if(not interrupted_by_signal){
231
289
      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);
 
290
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
 
291
      perror("execl");
239
292
    }
 
293
    free(prompt);
 
294
    _exit(EXIT_FAILURE);
240
295
  }
241
296
  
242
297
  /* Parent */
243
298
  free(prompt);
 
299
  prompt = NULL;
 
300
  
 
301
  if(interrupted_by_signal){
 
302
    goto failure;
 
303
  }
244
304
  
245
305
  /* Wait for command to complete */
246
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
306
  {
247
307
    int status;
248
 
    ret = waitpid(splashy_command_pid, &status, 0);
 
308
    do {
 
309
      ret = waitpid(splashy_command_pid, &status, 0);
 
310
    } while(ret == -1 and errno == EINTR
 
311
            and not interrupted_by_signal);
 
312
    if(interrupted_by_signal){
 
313
      goto failure;
 
314
    }
249
315
    if(ret == -1){
250
 
      if(errno != EINTR){
251
 
        perror("waitpid");
252
 
      }
 
316
      perror("waitpid");
253
317
      if(errno == ECHILD){
254
318
        splashy_command_pid = 0;
255
319
      }
256
320
    } else {
257
321
      /* The child process has exited */
258
322
      splashy_command_pid = 0;
259
 
      if(not interrupted_by_signal and WIFEXITED(status)
260
 
         and WEXITSTATUS(status)==0){
 
323
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
261
324
        return EXIT_SUCCESS;
262
325
      }
263
326
    }
264
327
  }
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){
298
 
      perror("execl");
299
 
    }
300
 
    _exit(EXIT_FAILURE);
301
 
  }
302
 
  
303
 
  return EXIT_FAILURE;
 
328
  
 
329
 failure:
 
330
  
 
331
  free(prompt);
 
332
  
 
333
  if(proc_dir != NULL){
 
334
    TEMP_FAILURE_RETRY(closedir(proc_dir));
 
335
  }
 
336
  
 
337
  if(splashy_command_pid != 0){
 
338
    TEMP_FAILURE_RETRY(kill(splashy_command_pid, SIGTERM));
 
339
    
 
340
    TEMP_FAILURE_RETRY(kill(splashy_pid, SIGTERM));
 
341
    sleep(2);
 
342
    while(TEMP_FAILURE_RETRY(kill(splashy_pid, 0)) == 0){
 
343
      TEMP_FAILURE_RETRY(kill(splashy_pid, SIGKILL));
 
344
      sleep(1);
 
345
    }
 
346
    pid_t new_splashy_pid = (pid_t)TEMP_FAILURE_RETRY(fork());
 
347
    if(new_splashy_pid == 0){
 
348
      /* Child; will become new splashy process */
 
349
      
 
350
      /* Make the effective user ID (root) the only user ID instead of
 
351
         the real user ID (_mandos) */
 
352
      ret = setuid(geteuid());
 
353
      if(ret == -1){
 
354
        perror("setuid");
 
355
      }
 
356
      
 
357
      setsid();
 
358
      ret = chdir("/");
 
359
      if(ret == -1){
 
360
        perror("chdir");
 
361
      }
 
362
/*       if(fork() != 0){ */
 
363
/*      _exit(EXIT_SUCCESS); */
 
364
/*       } */
 
365
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
 
366
      if(ret == -1){
 
367
        perror("dup2");
 
368
        _exit(EX_OSERR);
 
369
      }
 
370
      
 
371
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
372
      {
 
373
        int e = errno;
 
374
        perror("execl");
 
375
        switch(e){
 
376
        case EACCES:
 
377
        case ENOENT:
 
378
        case ENOEXEC:
 
379
        default:
 
380
          _exit(EX_UNAVAILABLE);
 
381
        case ENAMETOOLONG:
 
382
        case E2BIG:
 
383
        case ENOMEM:
 
384
          _exit(EX_OSERR);
 
385
        case ENOTDIR:
 
386
        case ELOOP:
 
387
          _exit(EX_OSFILE);
 
388
        }
 
389
      }
 
390
    }
 
391
  }
 
392
  
 
393
  if(interrupted_by_signal){
 
394
    struct sigaction signal_action;
 
395
    sigemptyset(&signal_action.sa_mask);
 
396
    signal_action.sa_handler = SIG_DFL;
 
397
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
 
398
                                            &signal_action, NULL));
 
399
    if(ret == -1){
 
400
      perror("sigaction");
 
401
    }
 
402
    do {
 
403
      ret = raise(signal_received);
 
404
    } while(ret != 0 and errno == EINTR);
 
405
    if(ret != 0){
 
406
      perror("raise");
 
407
      abort();
 
408
    }
 
409
    TEMP_FAILURE_RETRY(pause());
 
410
  }
 
411
  
 
412
  return exitstatus;
304
413
}