/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

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#define _GNU_SOURCE             /* asprintf() */
2
2
#include <signal.h>             /* sig_atomic_t, struct sigaction,
3
 
                                   sigemptyset(), sigaddset(), SIGINT,
4
 
                                   SIGHUP, SIGTERM, sigaction,
5
 
                                   SIG_IGN, kill(), SIGKILL */
 
3
                                   sigemptyset(), sigaddset(),
 
4
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
 
5
                                   SIGTERM, kill(), SIGKILL */
6
6
#include <stddef.h>             /* NULL */
7
7
#include <stdlib.h>             /* getenv() */
8
8
#include <stdio.h>              /* asprintf(), perror() */
9
 
#include <stdlib.h>             /* EXIT_FAILURE, free(), strtoul(),
10
 
                                   EXIT_SUCCESS */
 
9
#include <stdlib.h>             /* EXIT_FAILURE, EXIT_SUCCESS,
 
10
                                   strtoul(), free() */
11
11
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
12
12
                                   ssize_t */
13
13
#include <dirent.h>             /* opendir(), readdir(), closedir() */
14
 
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
15
 
#include <iso646.h>             /* not, or, and */
16
14
#include <unistd.h>             /* readlink(), fork(), execl(),
17
 
                                   sleep(), dup2() STDERR_FILENO,
18
 
                                   STDOUT_FILENO, _exit() */
 
15
                                   _exit */
19
16
#include <string.h>             /* memcmp() */
 
17
#include <iso646.h>             /* and */
20
18
#include <errno.h>              /* errno */
21
19
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
22
20
                                   WEXITSTATUS() */
90
88
          closedir(proc_dir);
91
89
          return EXIT_FAILURE;
92
90
        }
93
 
        
94
 
        /* Check that it refers to a symlink owned by root:root */
95
 
        struct stat exe_stat;
96
 
        ret = lstat(exe_link, &exe_stat);
97
 
        if(ret == -1){
98
 
          perror("lstat");
99
 
          free(exe_link);
100
 
          free(prompt);
101
 
          closedir(proc_dir);
102
 
          return EXIT_FAILURE;
103
 
        }
104
 
        if(not S_ISLNK(exe_stat.st_mode)
105
 
           or exe_stat.st_uid != 0
106
 
           or exe_stat.st_gid != 0){
107
 
          free(exe_link);
108
 
          continue;
109
 
        }
110
 
        
111
91
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
112
92
        free(exe_link);
113
93
      }
140
120
      free(prompt);
141
121
      return EXIT_FAILURE;
142
122
    }
143
 
    if(old_action.sa_handler != SIG_IGN){
 
123
    if (old_action.sa_handler != SIG_IGN){
144
124
      ret = sigaction(SIGINT, &new_action, NULL);
145
125
      if(ret == -1){
146
126
        perror("sigaction");
154
134
      free(prompt);
155
135
      return EXIT_FAILURE;
156
136
    }
157
 
    if(old_action.sa_handler != SIG_IGN){
 
137
    if (old_action.sa_handler != SIG_IGN){
158
138
      ret = sigaction(SIGHUP, &new_action, NULL);
159
139
      if(ret == -1){
160
140
        perror("sigaction");
168
148
      free(prompt);
169
149
      return EXIT_FAILURE;
170
150
    }
171
 
    if(old_action.sa_handler != SIG_IGN){
 
151
    if (old_action.sa_handler != SIG_IGN){
172
152
      ret = sigaction(SIGTERM, &new_action, NULL);
173
153
      if(ret == -1){
174
154
        perror("sigaction");
193
173
      const char splashy_command[] = "/sbin/splashy_update";
194
174
      ret = execl(splashy_command, splashy_command, prompt,
195
175
                  (char *)NULL);
196
 
      if(not interrupted_by_signal){
 
176
      if(not interrupted_by_signal and errno != ENOENT){
 
177
        /* Don't report "File not found", since splashy might not be
 
178
           installed. */
197
179
        perror("execl");
198
180
      }
199
181
      free(prompt);
200
 
      _exit(EXIT_FAILURE);
 
182
      return EXIT_FAILURE;
201
183
    }
202
184
  }
203
185
  
205
187
  free(prompt);
206
188
  
207
189
  /* Wait for command to complete */
208
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
209
 
    int status;
210
 
    ret = waitpid(splashy_command_pid, &status, 0);
211
 
    if(ret == -1){
212
 
      if(errno != EINTR){
213
 
        perror("waitpid");
214
 
      }
215
 
      if(errno == ECHILD){
216
 
        splashy_command_pid = 0;
217
 
      }
218
 
    } else {
219
 
      /* The child process has exited */
220
 
      splashy_command_pid = 0;
221
 
      if(not interrupted_by_signal and WIFEXITED(status)
222
 
         and WEXITSTATUS(status)==0){
223
 
        return EXIT_SUCCESS;
224
 
      }
 
190
  int status;
 
191
  while(not interrupted_by_signal){
 
192
    waitpid(splashy_command_pid, &status, 0);
 
193
    if(not interrupted_by_signal
 
194
       and WIFEXITED(status) and WEXITSTATUS(status)==0){
 
195
      return EXIT_SUCCESS;
225
196
    }
226
197
  }
227
198
  kill(splashy_pid, SIGTERM);
228
 
  if(interrupted_by_signal and splashy_command_pid != 0){
 
199
  if(interrupted_by_signal){
229
200
    kill(splashy_command_pid, SIGTERM);
230
201
  }
231
 
  sleep(2);
232
 
  while(kill(splashy_pid, 0) == 0){
233
 
    kill(splashy_pid, SIGKILL);
234
 
    sleep(1);
235
 
  }
 
202
  
236
203
  pid_t new_splashy_pid = fork();
237
204
  if(new_splashy_pid == 0){
238
205
    /* Child; will become new splashy process */
239
 
    
240
 
    /* Make the effective user ID (root) the only user ID instead of
241
 
       the real user ID (mandos) */
242
 
    ret = setuid(geteuid());
243
 
    if(ret == -1){
244
 
      perror("setuid");
 
206
    while(kill(splashy_pid, 0)){
 
207
      sleep(2);
 
208
      kill(splashy_pid, SIGKILL);
 
209
      sleep(1);
245
210
    }
246
 
    
247
 
    setsid();
248
 
    ret = chdir("/");
249
 
/*     if(fork() != 0){ */
250
 
/*       _exit(EXIT_SUCCESS); */
251
 
/*     } */
252
211
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
253
212
    if(ret == -1){
254
213
      perror("dup2");
255
214
      _exit(EXIT_FAILURE);
256
215
    }
257
 
    
258
216
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
259
 
    if(not interrupted_by_signal){
260
 
      perror("execl");
261
 
    }
262
 
    _exit(EXIT_FAILURE);
263
217
  }
264
218
  
265
219
  return EXIT_FAILURE;