/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« 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(),
4
 
                                   sigaction, SIGINT, SIG_IGN, SIGHUP,
5
 
                                   SIGTERM, kill(), SIGKILL */
 
3
                                   sigemptyset(), sigaddset(), SIGINT,
 
4
                                   SIGHUP, SIGTERM, sigaction,
 
5
                                   SIG_IGN, 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, EXIT_SUCCESS,
10
 
                                   strtoul(), free() */
 
9
#include <stdlib.h>             /* EXIT_FAILURE, free(), strtoul(),
 
10
                                   EXIT_SUCCESS */
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 */
14
16
#include <unistd.h>             /* readlink(), fork(), execl(),
15
 
                                   _exit */
 
17
                                   sleep(), dup2() STDERR_FILENO,
 
18
                                   STDOUT_FILENO, _exit() */
16
19
#include <string.h>             /* memcmp() */
17
 
#include <iso646.h>             /* and */
18
20
#include <errno.h>              /* errno */
19
21
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
20
22
                                   WEXITSTATUS() */
88
90
          closedir(proc_dir);
89
91
          return EXIT_FAILURE;
90
92
        }
 
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
        
91
111
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
92
112
        free(exe_link);
93
113
      }
120
140
      free(prompt);
121
141
      return EXIT_FAILURE;
122
142
    }
123
 
    if (old_action.sa_handler != SIG_IGN){
 
143
    if(old_action.sa_handler != SIG_IGN){
124
144
      ret = sigaction(SIGINT, &new_action, NULL);
125
145
      if(ret == -1){
126
146
        perror("sigaction");
134
154
      free(prompt);
135
155
      return EXIT_FAILURE;
136
156
    }
137
 
    if (old_action.sa_handler != SIG_IGN){
 
157
    if(old_action.sa_handler != SIG_IGN){
138
158
      ret = sigaction(SIGHUP, &new_action, NULL);
139
159
      if(ret == -1){
140
160
        perror("sigaction");
148
168
      free(prompt);
149
169
      return EXIT_FAILURE;
150
170
    }
151
 
    if (old_action.sa_handler != SIG_IGN){
 
171
    if(old_action.sa_handler != SIG_IGN){
152
172
      ret = sigaction(SIGTERM, &new_action, NULL);
153
173
      if(ret == -1){
154
174
        perror("sigaction");
173
193
      const char splashy_command[] = "/sbin/splashy_update";
174
194
      ret = execl(splashy_command, splashy_command, prompt,
175
195
                  (char *)NULL);
176
 
      if(not interrupted_by_signal and errno != ENOENT){
177
 
        /* Don't report "File not found", since splashy might not be
178
 
           installed. */
 
196
      if(not interrupted_by_signal){
179
197
        perror("execl");
180
198
      }
181
199
      free(prompt);
182
 
      return EXIT_FAILURE;
 
200
      _exit(EXIT_FAILURE);
183
201
    }
184
202
  }
185
203
  
187
205
  free(prompt);
188
206
  
189
207
  /* Wait for command to complete */
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;
 
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
      }
196
225
    }
197
226
  }
198
227
  kill(splashy_pid, SIGTERM);
199
 
  if(interrupted_by_signal){
 
228
  if(interrupted_by_signal and splashy_command_pid != 0){
200
229
    kill(splashy_command_pid, SIGTERM);
201
230
  }
202
 
  
 
231
  sleep(2);
 
232
  while(kill(splashy_pid, 0) == 0){
 
233
    kill(splashy_pid, SIGKILL);
 
234
    sleep(1);
 
235
  }
203
236
  pid_t new_splashy_pid = fork();
204
237
  if(new_splashy_pid == 0){
205
238
    /* Child; will become new splashy process */
206
 
    while(kill(splashy_pid, 0)){
207
 
      sleep(2);
208
 
      kill(splashy_pid, SIGKILL);
209
 
      sleep(1);
 
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");
210
245
    }
 
246
    
 
247
    setsid();
 
248
    ret = chdir("/");
 
249
/*     if(fork() != 0){ */
 
250
/*       _exit(EXIT_SUCCESS); */
 
251
/*     } */
211
252
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
212
253
    if(ret == -1){
213
254
      perror("dup2");
214
255
      _exit(EXIT_FAILURE);
215
256
    }
 
257
    
216
258
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
259
    if(not interrupted_by_signal){
 
260
      perror("execl");
 
261
    }
 
262
    _exit(EXIT_FAILURE);
217
263
  }
218
264
  
219
265
  return EXIT_FAILURE;