/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-16 23:28:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090916232839-3o7i8qmcdcz5j1ya
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
                 "$syslog", thanks to Petter Reinholdtsen
                 <pere@hungry.com> (Debian bug #546928).

* initramfs-tools-script: Removed erroneous comment.

* plugins.d/askpass-fifo.c: Removed TEMP_FAILURE_RETRY since it is
                            not needed.

* plugins.d/mandos-client.c (main): Bug fix: Initialize
                                    "old_sigterm_action".

* plugins.d/splashy.c (main): Bug fix: really check return value from
                              "sigaddset".  Fix some warnings on
                              64-bit systems.

* plugins.d/usplash.c (termination_handler, main): Save received
                                                   signal and
                                                   re-raise it on
                                                   exit.
  (usplash_write): Do not close FIFO, instead, take an additional file
                   descriptor pointer to it and open only when needed
                   (all callers changed).  Abort immediately on EINTR.
                   Bug fix:  Add NUL byte on single-word commands.
                   Ignore "interrupted_by_signal".
  (makeprompt, find_usplash): New; broken out from "main()".
  (find_usplash): Bug fix: close /proc/<pid>/cmdline FD on error.
  (main): Reorganized to jump to a new "failure" label on any error.
          Bug fix: check return values from sigaddset.
          New variable "usplash_accessed" to flag if usplash(8) needs
          to be killed and restarted.  Removed the "an_error_occured"
          variable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
                                   SIG_IGN, kill(), SIGKILL */
30
30
#include <stddef.h>             /* NULL */
31
31
#include <stdlib.h>             /* getenv() */
32
 
#include <stdio.h>              /* asprintf() */
 
32
#include <stdio.h>              /* asprintf(), perror() */
33
33
#include <stdlib.h>             /* EXIT_FAILURE, free(),
34
34
                                   EXIT_SUCCESS */
35
35
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
43
43
                                   STDOUT_FILENO, _exit(),
44
44
                                   pause() */
45
45
#include <string.h>             /* memcmp() */
46
 
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
47
 
                                   ENOENT, ENAMETOOLONG, EMFILE,
48
 
                                   ENFILE, ENOMEM, ENOEXEC, EINVAL,
49
 
                                   E2BIG, EFAULT, EIO, ETXTBSY,
50
 
                                   EISDIR, ELIBBAD, EPERM, EINTR,
51
 
                                   ECHILD */
52
 
#include <error.h>              /* error() */
 
46
#include <errno.h>              /* errno */
53
47
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
54
48
                                   WEXITSTATUS() */
55
 
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
56
 
                                   EX_UNAVAILABLE */
57
49
 
58
50
sig_atomic_t interrupted_by_signal = 0;
59
51
int signal_received;
73
65
  DIR *proc_dir = NULL;
74
66
  pid_t splashy_pid = 0;
75
67
  pid_t splashy_command_pid = 0;
76
 
  int exitstatus = EXIT_FAILURE;
77
68
  
78
69
  /* Create prompt string */
79
70
  {
99
90
    }
100
91
    if(ret == -1){
101
92
      prompt = NULL;
102
 
      exitstatus = EX_OSERR;
103
93
      goto failure;
104
94
    }
105
95
  }
109
99
    const char splashy_name[] = "/sbin/splashy";
110
100
    proc_dir = opendir("/proc");
111
101
    if(proc_dir == NULL){
112
 
      int e = errno;
113
 
      error(0, errno, "opendir");
114
 
      switch(e){
115
 
      case EACCES:
116
 
      case ENOTDIR:
117
 
      case ELOOP:
118
 
      case ENOENT:
119
 
      default:
120
 
        exitstatus = EX_OSFILE;
121
 
        break;
122
 
      case ENAMETOOLONG:
123
 
      case EMFILE:
124
 
      case ENFILE:
125
 
      case ENOMEM:
126
 
        exitstatus = EX_OSERR;
127
 
        break;
128
 
      }
 
102
      perror("opendir");
129
103
      goto failure;
130
104
    }
131
105
    for(struct dirent *proc_ent = readdir(proc_dir);
152
126
        char *exe_link;
153
127
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
154
128
        if(ret == -1){
155
 
          error(0, errno, "asprintf");
156
 
          exitstatus = EX_OSERR;
 
129
          perror("asprintf");
157
130
          goto failure;
158
131
        }
159
132
        
165
138
            free(exe_link);
166
139
            continue;
167
140
          }
168
 
          int e = errno;
169
 
          error(0, errno, "lstat");
 
141
          perror("lstat");
170
142
          free(exe_link);
171
 
          switch(e){
172
 
          case EACCES:
173
 
          case ENOTDIR:
174
 
          case ELOOP:
175
 
          default:
176
 
            exitstatus = EX_OSFILE;
177
 
            break;
178
 
          case ENAMETOOLONG:
179
 
            exitstatus = EX_OSERR;
180
 
            break;
181
 
          }
182
143
          goto failure;
183
144
        }
184
145
        if(not S_ISLNK(exe_stat.st_mode)
202
163
    proc_dir = NULL;
203
164
  }
204
165
  if(splashy_pid == 0){
205
 
    exitstatus = EX_UNAVAILABLE;
206
166
    goto failure;
207
167
  }
208
168
  
214
174
    sigemptyset(&new_action.sa_mask);
215
175
    ret = sigaddset(&new_action.sa_mask, SIGINT);
216
176
    if(ret == -1){
217
 
      error(0, errno, "sigaddset");
218
 
      exitstatus = EX_OSERR;
 
177
      perror("sigaddset");
219
178
      goto failure;
220
179
    }
221
180
    ret = sigaddset(&new_action.sa_mask, SIGHUP);
222
181
    if(ret == -1){
223
 
      error(0, errno, "sigaddset");
224
 
      exitstatus = EX_OSERR;
 
182
      perror("sigaddset");
225
183
      goto failure;
226
184
    }
227
185
    ret = sigaddset(&new_action.sa_mask, SIGTERM);
228
186
    if(ret == -1){
229
 
      error(0, errno, "sigaddset");
230
 
      exitstatus = EX_OSERR;
 
187
      perror("sigaddset");
231
188
      goto failure;
232
189
    }
233
190
    ret = sigaction(SIGINT, NULL, &old_action);
234
191
    if(ret == -1){
235
 
      error(0, errno, "sigaction");
236
 
      exitstatus = EX_OSERR;
 
192
      perror("sigaction");
237
193
      goto failure;
238
194
    }
239
195
    if(old_action.sa_handler != SIG_IGN){
240
196
      ret = sigaction(SIGINT, &new_action, NULL);
241
197
      if(ret == -1){
242
 
        error(0, errno, "sigaction");
243
 
        exitstatus = EX_OSERR;
 
198
        perror("sigaction");
244
199
        goto failure;
245
200
      }
246
201
    }
247
202
    ret = sigaction(SIGHUP, NULL, &old_action);
248
203
    if(ret == -1){
249
 
      error(0, errno, "sigaction");
250
 
      exitstatus = EX_OSERR;
 
204
      perror("sigaction");
251
205
      goto failure;
252
206
    }
253
207
    if(old_action.sa_handler != SIG_IGN){
254
208
      ret = sigaction(SIGHUP, &new_action, NULL);
255
209
      if(ret == -1){
256
 
        error(0, errno, "sigaction");
257
 
        exitstatus = EX_OSERR;
 
210
        perror("sigaction");
258
211
        goto failure;
259
212
      }
260
213
    }
261
214
    ret = sigaction(SIGTERM, NULL, &old_action);
262
215
    if(ret == -1){
263
 
      error(0, errno, "sigaction");
264
 
      exitstatus = EX_OSERR;
 
216
      perror("sigaction");
265
217
      goto failure;
266
218
    }
267
219
    if(old_action.sa_handler != SIG_IGN){
268
220
      ret = sigaction(SIGTERM, &new_action, NULL);
269
221
      if(ret == -1){
270
 
        error(0, errno, "sigaction");
271
 
        exitstatus = EX_OSERR;
 
222
        perror("sigaction");
272
223
        goto failure;
273
224
      }
274
225
    }
284
235
    goto failure;
285
236
  }
286
237
  if(splashy_command_pid == -1){
287
 
    error(0, errno, "fork");
288
 
    exitstatus = EX_OSERR;
 
238
    perror("fork");
289
239
    goto failure;
290
240
  }
291
241
  /* Child */
293
243
    if(not interrupted_by_signal){
294
244
      const char splashy_command[] = "/sbin/splashy_update";
295
245
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
296
 
      int e = errno;
297
 
      error(0, errno, "execl");
298
 
      switch(e){
299
 
      case EACCES:
300
 
      case ENOENT:
301
 
      case ENOEXEC:
302
 
      case EINVAL:
303
 
        _exit(EX_UNAVAILABLE);
304
 
      case ENAMETOOLONG:
305
 
      case E2BIG:
306
 
      case ENOMEM:
307
 
      case EFAULT:
308
 
      case EIO:
309
 
      case EMFILE:
310
 
      case ENFILE:
311
 
      case ETXTBSY:
312
 
      default:
313
 
        _exit(EX_OSERR);
314
 
      case ENOTDIR:
315
 
      case ELOOP:
316
 
      case EISDIR:
317
 
      case ELIBBAD:
318
 
      case EPERM:
319
 
        _exit(EX_OSFILE);
320
 
      }
 
246
      perror("execl");
321
247
    }
322
248
    free(prompt);
323
249
    _exit(EXIT_FAILURE);
342
268
      goto failure;
343
269
    }
344
270
    if(ret == -1){
345
 
      error(0, errno, "waitpid");
 
271
      perror("waitpid");
346
272
      if(errno == ECHILD){
347
273
        splashy_command_pid = 0;
348
274
      }
380
306
         the real user ID (_mandos) */
381
307
      ret = setuid(geteuid());
382
308
      if(ret == -1){
383
 
        error(0, errno, "setuid");
 
309
        perror("setuid");
384
310
      }
385
311
      
386
312
      setsid();
387
313
      ret = chdir("/");
388
314
      if(ret == -1){
389
 
        error(0, errno, "chdir");
 
315
        perror("chdir");
390
316
      }
391
317
/*       if(fork() != 0){ */
392
318
/*      _exit(EXIT_SUCCESS); */
393
319
/*       } */
394
320
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
395
321
      if(ret == -1){
396
 
        error(0, errno, "dup2");
397
 
        _exit(EX_OSERR);
 
322
        perror("dup2");
 
323
        _exit(EXIT_FAILURE);
398
324
      }
399
 
      
 
325
    
400
326
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
401
 
      {
402
 
        int e = errno;
403
 
        error(0, errno, "execl");
404
 
        switch(e){
405
 
        case EACCES:
406
 
        case ENOENT:
407
 
        case ENOEXEC:
408
 
        default:
409
 
          _exit(EX_UNAVAILABLE);
410
 
        case ENAMETOOLONG:
411
 
        case E2BIG:
412
 
        case ENOMEM:
413
 
          _exit(EX_OSERR);
414
 
        case ENOTDIR:
415
 
        case ELOOP:
416
 
          _exit(EX_OSFILE);
417
 
        }
418
 
      }
 
327
      perror("execl");
 
328
      _exit(EXIT_FAILURE);
419
329
    }
420
330
  }
421
331
  
426
336
    ret = (int)TEMP_FAILURE_RETRY(sigaction(signal_received,
427
337
                                            &signal_action, NULL));
428
338
    if(ret == -1){
429
 
      error(0, errno, "sigaction");
 
339
      perror("sigaction");
430
340
    }
431
341
    do {
432
342
      ret = raise(signal_received);
433
343
    } while(ret != 0 and errno == EINTR);
434
344
    if(ret != 0){
435
 
      error(0, errno, "raise");
 
345
      perror("raise");
436
346
      abort();
437
347
    }
438
348
    TEMP_FAILURE_RETRY(pause());
439
349
  }
440
350
  
441
 
  return exitstatus;
 
351
  return EXIT_FAILURE;
442
352
}