/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-08 04:41:37 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090908044137-4cxubotvn4etoxxl
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
                                    Bug fix: If taking down network
                                    interface, do not drop privileges
                                    completely; save them and reassert
                                    privileges when needed.

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,
31
30
#include <stddef.h>             /* NULL */
32
31
#include <stdlib.h>             /* getenv() */
33
32
#include <stdio.h>              /* asprintf(), perror() */
34
 
#include <stdlib.h>             /* EXIT_FAILURE, free(), strtoul(),
 
33
#include <stdlib.h>             /* EXIT_FAILURE, free(),
35
34
                                   EXIT_SUCCESS */
36
35
#include <sys/types.h>          /* pid_t, DIR, struct dirent,
37
36
                                   ssize_t */
38
37
#include <dirent.h>             /* opendir(), readdir(), closedir() */
 
38
#include <inttypes.h>           /* intmax_t, strtoimax() */
39
39
#include <sys/stat.h>           /* struct stat, lstat(), S_ISLNK */
40
40
#include <iso646.h>             /* not, or, and */
41
41
#include <unistd.h>             /* readlink(), fork(), execl(),
42
42
                                   sleep(), dup2() STDERR_FILENO,
43
 
                                   STDOUT_FILENO, _exit() */
 
43
                                   STDOUT_FILENO, _exit(),
 
44
                                   pause() */
44
45
#include <string.h>             /* memcmp() */
45
46
#include <errno.h>              /* errno */
46
47
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
47
48
                                   WEXITSTATUS() */
48
49
 
49
50
sig_atomic_t interrupted_by_signal = 0;
 
51
int signal_received;
50
52
 
51
 
static void termination_handler(__attribute__((unused))int signum){
 
53
static void termination_handler(int signum){
 
54
  if(interrupted_by_signal){
 
55
    return;
 
56
  }
52
57
  interrupted_by_signal = 1;
 
58
  signal_received = signum;
53
59
}
54
60
 
55
61
int main(__attribute__((unused))int argc,
56
62
         __attribute__((unused))char **argv){
57
63
  int ret = 0;
 
64
  char *prompt = NULL;
 
65
  DIR *proc_dir = NULL;
 
66
  pid_t splashy_pid = 0;
 
67
  pid_t splashy_command_pid = 0;
58
68
  
59
69
  /* Create prompt string */
60
 
  char *prompt = NULL;
61
70
  {
62
71
    const char *const cryptsource = getenv("cryptsource");
63
72
    const char *const crypttarget = getenv("crypttarget");
80
89
      }
81
90
    }
82
91
    if(ret == -1){
83
 
      return EXIT_FAILURE;
 
92
      prompt = NULL;
 
93
      goto failure;
84
94
    }
85
95
  }
86
96
  
87
97
  /* Find splashy process */
88
 
  pid_t splashy_pid = 0;
89
98
  {
90
99
    const char splashy_name[] = "/sbin/splashy";
91
 
    DIR *proc_dir = opendir("/proc");
 
100
    proc_dir = opendir("/proc");
92
101
    if(proc_dir == NULL){
93
 
      free(prompt);
94
102
      perror("opendir");
95
 
      return EXIT_FAILURE;
 
103
      goto failure;
96
104
    }
97
105
    for(struct dirent *proc_ent = readdir(proc_dir);
98
106
        proc_ent != NULL;
99
107
        proc_ent = readdir(proc_dir)){
100
 
      pid_t pid = (pid_t) strtoul(proc_ent->d_name, NULL, 10);
101
 
      if(pid == 0){
102
 
        /* Not a process */
103
 
        continue;
 
108
      pid_t pid;
 
109
      {
 
110
        intmax_t tmpmax;
 
111
        char *tmp;
 
112
        errno = 0;
 
113
        tmpmax = strtoimax(proc_ent->d_name, &tmp, 10);
 
114
        if(errno != 0 or tmp == proc_ent->d_name or *tmp != '\0'
 
115
           or tmpmax != (pid_t)tmpmax){
 
116
          /* Not a process */
 
117
          continue;
 
118
        }
 
119
        pid = (pid_t)tmpmax;
104
120
      }
105
121
      /* Find the executable name by doing readlink() on the
106
122
         /proc/<pid>/exe link */
111
127
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
112
128
        if(ret == -1){
113
129
          perror("asprintf");
114
 
          free(prompt);
115
 
          closedir(proc_dir);
116
 
          return EXIT_FAILURE;
 
130
          goto failure;
117
131
        }
118
132
        
119
133
        /* Check that it refers to a symlink owned by root:root */
120
134
        struct stat exe_stat;
121
135
        ret = lstat(exe_link, &exe_stat);
122
136
        if(ret == -1){
 
137
          if(errno == ENOENT){
 
138
            free(exe_link);
 
139
            continue;
 
140
          }
123
141
          perror("lstat");
124
142
          free(exe_link);
125
 
          free(prompt);
126
 
          closedir(proc_dir);
127
 
          return EXIT_FAILURE;
 
143
          goto failure;
128
144
        }
129
145
        if(not S_ISLNK(exe_stat.st_mode)
130
146
           or exe_stat.st_uid != 0
144
160
      }
145
161
    }
146
162
    closedir(proc_dir);
 
163
    proc_dir = NULL;
147
164
  }
148
165
  if(splashy_pid == 0){
149
 
    free(prompt);
150
 
    return EXIT_FAILURE;
 
166
    goto failure;
151
167
  }
152
168
  
153
169
  /* Set up the signal handler */
157
173
                     .sa_flags = 0 };
158
174
    sigemptyset(&new_action.sa_mask);
159
175
    sigaddset(&new_action.sa_mask, SIGINT);
 
176
    if(ret == -1){
 
177
      perror("sigaddset");
 
178
      goto failure;
 
179
    }
160
180
    sigaddset(&new_action.sa_mask, SIGHUP);
 
181
    if(ret == -1){
 
182
      perror("sigaddset");
 
183
      goto failure;
 
184
    }
161
185
    sigaddset(&new_action.sa_mask, SIGTERM);
 
186
    if(ret == -1){
 
187
      perror("sigaddset");
 
188
      goto failure;
 
189
    }
162
190
    ret = sigaction(SIGINT, NULL, &old_action);
163
191
    if(ret == -1){
164
192
      perror("sigaction");
165
 
      free(prompt);
166
 
      return EXIT_FAILURE;
 
193
      goto failure;
167
194
    }
168
195
    if(old_action.sa_handler != SIG_IGN){
169
196
      ret = sigaction(SIGINT, &new_action, NULL);
170
197
      if(ret == -1){
171
198
        perror("sigaction");
172
 
        free(prompt);
173
 
        return EXIT_FAILURE;
 
199
        goto failure;
174
200
      }
175
201
    }
176
202
    ret = sigaction(SIGHUP, NULL, &old_action);
177
203
    if(ret == -1){
178
204
      perror("sigaction");
179
 
      free(prompt);
180
 
      return EXIT_FAILURE;
 
205
      goto failure;
181
206
    }
182
207
    if(old_action.sa_handler != SIG_IGN){
183
208
      ret = sigaction(SIGHUP, &new_action, NULL);
184
209
      if(ret == -1){
185
210
        perror("sigaction");
186
 
        free(prompt);
187
 
        return EXIT_FAILURE;
 
211
        goto failure;
188
212
      }
189
213
    }
190
214
    ret = sigaction(SIGTERM, NULL, &old_action);
191
215
    if(ret == -1){
192
216
      perror("sigaction");
193
 
      free(prompt);
194
 
      return EXIT_FAILURE;
 
217
      goto failure;
195
218
    }
196
219
    if(old_action.sa_handler != SIG_IGN){
197
220
      ret = sigaction(SIGTERM, &new_action, NULL);
198
221
      if(ret == -1){
199
222
        perror("sigaction");
200
 
        free(prompt);
201
 
        return EXIT_FAILURE;
 
223
        goto failure;
202
224
      }
203
225
    }
204
226
  }
205
227
  
 
228
  if(interrupted_by_signal){
 
229
    goto failure;
 
230
  }
 
231
  
206
232
  /* Fork off the splashy command to prompt for password */
207
 
  pid_t splashy_command_pid = 0;
208
 
  if(not interrupted_by_signal){
209
 
    splashy_command_pid = fork();
210
 
    if(splashy_command_pid == -1){
211
 
      if(not interrupted_by_signal){
212
 
        perror("fork");
213
 
      }
214
 
      return EXIT_FAILURE;
215
 
    }
216
 
    /* Child */
217
 
    if(splashy_command_pid == 0){
 
233
  splashy_command_pid = fork();
 
234
  if(splashy_command_pid != 0 and interrupted_by_signal){
 
235
    goto failure;
 
236
  }
 
237
  if(splashy_command_pid == -1){
 
238
    perror("fork");
 
239
    goto failure;
 
240
  }
 
241
  /* Child */
 
242
  if(splashy_command_pid == 0){
 
243
    if(not interrupted_by_signal){
218
244
      const char splashy_command[] = "/sbin/splashy_update";
219
 
      ret = execl(splashy_command, splashy_command, prompt,
220
 
                  (char *)NULL);
221
 
      if(not interrupted_by_signal){
222
 
        perror("execl");
223
 
      }
224
 
      free(prompt);
225
 
      _exit(EXIT_FAILURE);
 
245
      execl(splashy_command, splashy_command, prompt, (char *)NULL);
 
246
      perror("execl");
226
247
    }
 
248
    free(prompt);
 
249
    _exit(EXIT_FAILURE);
227
250
  }
228
251
  
229
252
  /* Parent */
230
253
  free(prompt);
 
254
  prompt = NULL;
 
255
  
 
256
  if(interrupted_by_signal){
 
257
    goto failure;
 
258
  }
231
259
  
232
260
  /* Wait for command to complete */
233
 
  if(not interrupted_by_signal and splashy_command_pid != 0){
 
261
  {
234
262
    int status;
235
 
    ret = waitpid(splashy_command_pid, &status, 0);
 
263
    do {
 
264
      ret = waitpid(splashy_command_pid, &status, 0);
 
265
    } while(ret == -1 and errno == EINTR
 
266
            and not interrupted_by_signal);
 
267
    if(interrupted_by_signal){
 
268
      goto failure;
 
269
    }
236
270
    if(ret == -1){
237
 
      if(errno != EINTR){
238
 
        perror("waitpid");
239
 
      }
 
271
      perror("waitpid");
240
272
      if(errno == ECHILD){
241
273
        splashy_command_pid = 0;
242
274
      }
243
275
    } else {
244
276
      /* The child process has exited */
245
277
      splashy_command_pid = 0;
246
 
      if(not interrupted_by_signal and WIFEXITED(status)
247
 
         and WEXITSTATUS(status)==0){
 
278
      if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
248
279
        return EXIT_SUCCESS;
249
280
      }
250
281
    }
251
282
  }
252
 
  kill(splashy_pid, SIGTERM);
253
 
  if(interrupted_by_signal and splashy_command_pid != 0){
254
 
    kill(splashy_command_pid, SIGTERM);
255
 
  }
256
 
  sleep(2);
257
 
  while(kill(splashy_pid, 0) == 0){
258
 
    kill(splashy_pid, SIGKILL);
259
 
    sleep(1);
260
 
  }
261
 
  pid_t new_splashy_pid = fork();
262
 
  if(new_splashy_pid == 0){
263
 
    /* Child; will become new splashy process */
 
283
  
 
284
 failure:
 
285
  
 
286
  free(prompt);
 
287
  
 
288
  if(proc_dir != NULL){
 
289
    TEMP_FAILURE_RETRY(closedir(proc_dir));
 
290
  }
 
291
  
 
292
  if(splashy_command_pid != 0){
 
293
    TEMP_FAILURE_RETRY(kill(splashy_command_pid, SIGTERM));
264
294
    
265
 
    /* Make the effective user ID (root) the only user ID instead of
266
 
       the real user ID (mandos) */
267
 
    ret = setuid(geteuid());
268
 
    if(ret == -1){
269
 
      perror("setuid");
 
295
    TEMP_FAILURE_RETRY(kill(splashy_pid, SIGTERM));
 
296
    sleep(2);
 
297
    while(TEMP_FAILURE_RETRY(kill(splashy_pid, 0)) == 0){
 
298
      TEMP_FAILURE_RETRY(kill(splashy_pid, SIGKILL));
 
299
      sleep(1);
270
300
    }
 
301
    pid_t new_splashy_pid = TEMP_FAILURE_RETRY(fork());
 
302
    if(new_splashy_pid == 0){
 
303
      /* Child; will become new splashy process */
 
304
      
 
305
      /* Make the effective user ID (root) the only user ID instead of
 
306
         the real user ID (_mandos) */
 
307
      ret = setuid(geteuid());
 
308
      if(ret == -1){
 
309
        perror("setuid");
 
310
      }
 
311
      
 
312
      setsid();
 
313
      ret = chdir("/");
 
314
      if(ret == -1){
 
315
        perror("chdir");
 
316
      }
 
317
/*       if(fork() != 0){ */
 
318
/*      _exit(EXIT_SUCCESS); */
 
319
/*       } */
 
320
      ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace stdout */
 
321
      if(ret == -1){
 
322
        perror("dup2");
 
323
        _exit(EXIT_FAILURE);
 
324
      }
271
325
    
272
 
    setsid();
273
 
    ret = chdir("/");
274
 
/*     if(fork() != 0){ */
275
 
/*       _exit(EXIT_SUCCESS); */
276
 
/*     } */
277
 
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
278
 
    if(ret == -1){
279
 
      perror("dup2");
 
326
      execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
 
327
      perror("execl");
280
328
      _exit(EXIT_FAILURE);
281
329
    }
282
 
    
283
 
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
284
 
    if(not interrupted_by_signal){
285
 
      perror("execl");
286
 
    }
287
 
    _exit(EXIT_FAILURE);
 
330
  }
 
331
  
 
332
  if(interrupted_by_signal){
 
333
    struct sigaction signal_action;
 
334
    sigemptyset(&signal_action.sa_mask);
 
335
    signal_action.sa_handler = SIG_DFL;
 
336
    ret = TEMP_FAILURE_RETRY(sigaction(signal_received,
 
337
                                       &signal_action, NULL));
 
338
    if(ret == -1){
 
339
      perror("sigaction");
 
340
    }
 
341
    do {
 
342
      ret = raise(signal_received);
 
343
    } while(ret != 0 and errno == EINTR);
 
344
    if(ret != 0){
 
345
      perror("raise");
 
346
      abort();
 
347
    }
 
348
    TEMP_FAILURE_RETRY(pause());
288
349
  }
289
350
  
290
351
  return EXIT_FAILURE;