/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

todo

Show diffs side-by-side

added added

removed removed

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