/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

  • Committer: Teddy Hogeborn
  • Date: 2008-09-23 18:52:33 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080923185233-a6zir2neltxk2qvd
* debian/mandos-client.lintian-overrides: Ignore setuid
                                          "plugins.d/splashy".

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() */
27
25
  interrupted_by_signal = 1;
28
26
}
29
27
 
30
 
int main(__attribute__((unused))int argc,
31
 
         __attribute__((unused))char **argv){
 
28
int main(__attribute__((unused))int argc, char **argv){
32
29
  int ret = 0;
33
30
  
34
31
  /* Create prompt string */
79
76
      }
80
77
      /* Find the executable name by doing readlink() on the
81
78
         /proc/<pid>/exe link */
 
79
      char *exe_link;
 
80
      ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
 
81
      if(ret == -1){
 
82
        perror("asprintf");
 
83
        free(prompt);
 
84
        closedir(proc_dir);
 
85
        return EXIT_FAILURE;
 
86
      }
82
87
      char exe_target[sizeof(splashy_name)];
83
 
      ssize_t sret;
84
 
      {
85
 
        char *exe_link;
86
 
        ret = asprintf(&exe_link, "/proc/%s/exe", proc_ent->d_name);
87
 
        if(ret == -1){
88
 
          perror("asprintf");
89
 
          free(prompt);
90
 
          closedir(proc_dir);
91
 
          return EXIT_FAILURE;
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
 
        
111
 
        sret = readlink(exe_link, exe_target, sizeof(exe_target));
112
 
        free(exe_link);
113
 
      }
 
88
      ssize_t sret = readlink(exe_link, exe_target,
 
89
                              sizeof(exe_target));
114
90
      if((sret == ((ssize_t)sizeof(exe_target)-1))
115
91
         and (memcmp(splashy_name, exe_target,
116
92
                     sizeof(exe_target)-1) == 0)){
140
116
      free(prompt);
141
117
      return EXIT_FAILURE;
142
118
    }
143
 
    if(old_action.sa_handler != SIG_IGN){
 
119
    if (old_action.sa_handler != SIG_IGN){
144
120
      ret = sigaction(SIGINT, &new_action, NULL);
145
121
      if(ret == -1){
146
122
        perror("sigaction");
154
130
      free(prompt);
155
131
      return EXIT_FAILURE;
156
132
    }
157
 
    if(old_action.sa_handler != SIG_IGN){
 
133
    if (old_action.sa_handler != SIG_IGN){
158
134
      ret = sigaction(SIGHUP, &new_action, NULL);
159
135
      if(ret == -1){
160
136
        perror("sigaction");
168
144
      free(prompt);
169
145
      return EXIT_FAILURE;
170
146
    }
171
 
    if(old_action.sa_handler != SIG_IGN){
 
147
    if (old_action.sa_handler != SIG_IGN){
172
148
      ret = sigaction(SIGTERM, &new_action, NULL);
173
149
      if(ret == -1){
174
150
        perror("sigaction");
193
169
      const char splashy_command[] = "/sbin/splashy_update";
194
170
      ret = execl(splashy_command, splashy_command, prompt,
195
171
                  (char *)NULL);
196
 
      if(not interrupted_by_signal){
 
172
      if(not interrupted_by_signal and errno != ENOENT){
 
173
        /* Don't report "File not found", since splashy might not be
 
174
           installed. */
197
175
        perror("execl");
198
176
      }
199
177
      free(prompt);
200
 
      _exit(EXIT_FAILURE);
 
178
      return EXIT_FAILURE;
201
179
    }
202
180
  }
203
181
  
205
183
  free(prompt);
206
184
  
207
185
  /* 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
 
      }
 
186
  int status;
 
187
  while(not interrupted_by_signal){
 
188
    waitpid(splashy_command_pid, &status, 0);
 
189
    if(not interrupted_by_signal
 
190
       and WIFEXITED(status) and WEXITSTATUS(status)==0){
 
191
      return EXIT_SUCCESS;
225
192
    }
226
193
  }
227
194
  kill(splashy_pid, SIGTERM);
228
 
  if(interrupted_by_signal and splashy_command_pid != 0){
 
195
  if(interrupted_by_signal){
229
196
    kill(splashy_command_pid, SIGTERM);
230
197
  }
231
 
  sleep(2);
232
 
  while(kill(splashy_pid, 0) == 0){
233
 
    kill(splashy_pid, SIGKILL);
234
 
    sleep(1);
235
 
  }
 
198
 
236
199
  pid_t new_splashy_pid = fork();
237
200
  if(new_splashy_pid == 0){
238
 
    /* 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");
 
201
    while(kill(splashy_pid, 0)){
 
202
      sleep(2);
 
203
      kill(splashy_pid, SIGKILL);
 
204
      sleep(1);
245
205
    }
246
 
    
247
 
    setsid();
248
 
    ret = chdir("/");
249
 
/*     if(fork() != 0){ */
250
 
/*       _exit(EXIT_SUCCESS); */
251
 
/*     } */
252
206
    ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
253
207
    if(ret == -1){
254
208
      perror("dup2");
255
209
      _exit(EXIT_FAILURE);
256
210
    }
257
 
    
258
211
    execl("/sbin/splashy", "/sbin/splashy", "boot", (char *)NULL);
259
 
    if(not interrupted_by_signal){
260
 
      perror("execl");
261
 
    }
262
 
    _exit(EXIT_FAILURE);
263
212
  }
264
213
  
265
214
  return EXIT_FAILURE;