/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-10-04 20:09:53 UTC
  • Revision ID: teddy@fukt.bsnet.se-20081004200953-7j0fjlq6fqsxs9t2
* Makefile (DOCS): Added "plugins.d/askpass-fifo.8mandos".
  (install-client-nokey): Also install
                          "plugins.d/askpass-fifo.8mandos".
  (uninstall-client): Also uninstall "plugins.d/askpass-fifo.8mandos".

* plugins.d/askpass-fifo.xml: New.

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