/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 mandos-client.c

synopsis changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
25
25
 
26
 
#include <stdio.h>              /* popen(), fileno(), fprintf(),
27
 
                                   stderr, STDOUT_FILENO */
 
26
#include <stddef.h>             /* size_t, NULL */
 
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
 
28
                                   EXIT_SUCCESS, realloc() */
 
29
#include <stdbool.h>            /* bool, true, false */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
 
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
33
                                   stat, waitpid(), WIFEXITED(),
 
34
                                   WEXITSTATUS(), wait(), pid_t,
 
35
                                   uid_t, gid_t, getuid(), getgid(),
 
36
                                   dirfd() */
 
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
 
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
 
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
 
40
                                   WEXITSTATUS() */
 
41
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
28
42
#include <iso646.h>             /* and, or, not */
29
 
#include <sys/types.h>          /* DIR, opendir(), stat(),
30
 
                                   struct stat, waitpid(),
31
 
                                   WIFEXITED(), WEXITSTATUS(),
32
 
                                   wait() */
33
 
#include <sys/wait.h>           /* wait() */
34
43
#include <dirent.h>             /* DIR, struct dirent, opendir(),
35
 
                                   readdir(), closedir() */
36
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
44
                                   readdir(), closedir(), dirfd() */
37
45
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
38
 
                                   fcntl() */
39
 
#include <fcntl.h>              /* fcntl() */
40
 
#include <stddef.h>             /* NULL */
41
 
#include <stdlib.h>             /* EXIT_FAILURE */
42
 
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
43
 
                                   FD_SET(), FD_ISSET() */
44
 
#include <string.h>             /* strlen(), strcpy(), strcat() */
45
 
#include <stdbool.h>            /* true */
46
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
47
 
                                   WEXITSTATUS() */
 
46
                                   fcntl(), setuid(), setgid(),
 
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
 
48
                                   access(), pipe(), fork(), close()
 
49
                                   dup2, STDOUT_FILENO, _exit(),
 
50
                                   execv(), write(), read(),
 
51
                                   close() */
 
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
 
53
                                   FD_CLOEXEC */
 
54
#include <string.h>             /* strtok, strlen(), strcpy(),
 
55
                                   strcat() */
48
56
#include <errno.h>              /* errno */
49
 
#include <argp.h>               /* struct argp_option,
50
 
                                   struct argp_state, struct argp,
51
 
                                   argp_parse() */
 
57
#include <argp.h>               /* struct argp_option, struct
 
58
                                   argp_state, struct argp,
 
59
                                   argp_parse(), ARGP_ERR_UNKNOWN,
 
60
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
 
62
                                   sigaddset(), sigaction(),
 
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
 
64
                                   SIG_UNBLOCK, kill() */
 
65
#include <errno.h>              /* errno, EBADF */
 
66
 
 
67
#define BUFFER_SIZE 256
52
68
 
53
69
struct process;
54
70
 
72
88
  struct plugin *next;
73
89
} plugin;
74
90
 
75
 
plugin *getplugin(char *name, plugin **plugin_list){
 
91
static plugin *getplugin(char *name, plugin **plugin_list){
76
92
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
77
93
    if ((p->name == name)
78
94
        or (p->name and name and (strcmp(p->name, name) == 0))){
101
117
  return new_plugin;
102
118
}
103
119
 
104
 
void addargument(plugin *p, char *arg){
 
120
static void addargument(plugin *p, char *arg){
105
121
  p->argv[p->argc] = arg;
106
122
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
107
123
  if (p->argv == NULL){
117
133
 * Descriptor Flags".
118
134
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
119
135
 */
120
 
int set_cloexec_flag(int fd)
 
136
static int set_cloexec_flag(int fd)
121
137
{
122
138
  int ret = fcntl(fd, F_GETFD, 0);
123
139
  /* If reading the flags failed, return error indication now. */
128
144
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
129
145
}
130
146
 
131
 
#define BUFFER_SIZE 256
132
 
 
133
147
const char *argp_program_version = "plugbasedclient 0.9";
134
148
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
135
149
 
141
155
  process *proc = process_list;
142
156
  int status;
143
157
  pid_t pid = wait(&status);
 
158
  if(pid == -1){
 
159
    perror("wait");
 
160
    return;
 
161
  }
144
162
  while(proc != NULL and proc->pid != pid){
145
 
    proc = proc->next;    
 
163
    proc = proc->next;
146
164
  }
147
165
  if(proc == NULL){
148
166
    /* Process not found in process list */
167
185
  struct sigaction old_sigchld_action;
168
186
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
169
187
                                      .sa_flags = SA_NOCLDSTOP };
170
 
 
 
188
  char *plus_options = NULL;
 
189
  char **plus_argv = NULL;
 
190
  
171
191
  /* Establish a signal handler */
172
192
  sigemptyset(&sigchld_action.sa_mask);
173
193
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
217
237
        do{
218
238
          addargument(getplugin(NULL, plugins), p);
219
239
          p = strtok(NULL, ",");
220
 
        } while (p);
 
240
        } while (p != NULL);
221
241
      }
222
242
      break;
223
243
    case 'o':
224
244
      if (arg != NULL){
225
245
        char *name = strtok(arg, ":");
226
246
        char *p = strtok(NULL, ":");
227
 
        if(p){
 
247
        if(p != NULL){
228
248
          p = strtok(p, ",");
229
249
          do{
230
250
            addargument(getplugin(name, plugins), p);
231
251
            p = strtok(NULL, ",");
232
 
          } while (p);
 
252
          } while (p != NULL);
233
253
        }
234
254
      }
235
255
      break;
251
271
      debug = true;
252
272
      break;
253
273
    case ARGP_KEY_ARG:
254
 
      argp_usage (state);
 
274
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
275
        argp_usage (state);
 
276
      }
 
277
      plus_options = arg;
255
278
      break;
256
279
    case ARGP_KEY_END:
257
280
      break;
264
287
  plugin *plugin_list = NULL;
265
288
  
266
289
  struct argp argp = { .options = options, .parser = parse_opt,
267
 
                       .args_doc = "",
 
290
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
268
291
                       .doc = "Mandos plugin runner -- Run plugins" };
269
292
  
270
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
271
 
 
 
293
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
294
  if (ret == ARGP_ERR_UNKNOWN){
 
295
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
296
    exitstatus = EXIT_FAILURE;
 
297
    goto end;
 
298
  }
 
299
  
 
300
  if(plus_options){
 
301
    /* This is a mangled argument in the form of
 
302
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
303
    /* Make new argc and argv vars, and call argp_parse() again. */
 
304
    plus_options++;             /* skip the first '+' character */
 
305
    const char delims[] = "+";
 
306
    char *arg;
 
307
    int new_argc = 1;
 
308
    plus_argv = malloc(sizeof(char*) * 2);
 
309
    if(plus_argv == NULL){
 
310
      perror("malloc");
 
311
      exitstatus = EXIT_FAILURE;
 
312
      goto end;
 
313
    }
 
314
    plus_argv[0] = argv[0];
 
315
    plus_argv[1] = NULL;
 
316
    arg = strtok(plus_options, delims); /* Get first argument */
 
317
    while(arg != NULL){
 
318
      new_argc++;
 
319
      plus_argv = realloc(plus_argv, sizeof(char *)
 
320
                         * ((unsigned int) new_argc + 1));
 
321
      if(plus_argv == NULL){
 
322
        perror("realloc");
 
323
        exitstatus = EXIT_FAILURE;
 
324
        goto end;
 
325
      }
 
326
      plus_argv[new_argc-1] = arg;
 
327
      plus_argv[new_argc] = NULL;
 
328
      arg = strtok(NULL, delims); /* Get next argument */
 
329
    }
 
330
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
331
    if (ret == ARGP_ERR_UNKNOWN){
 
332
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
333
      exitstatus = EXIT_FAILURE;
 
334
      goto end;
 
335
    }
 
336
  }
 
337
  
272
338
  if(debug){
273
339
    for(plugin *p = plugin_list; p != NULL; p=p->next){
274
340
      fprintf(stderr, "Plugin: %s has %d arguments\n",
278
344
      }
279
345
    }
280
346
  }
281
 
 
 
347
  
282
348
  ret = setuid(uid);
283
349
  if (ret == -1){
284
350
    perror("setuid");
286
352
  
287
353
  setgid(gid);
288
354
  if (ret == -1){
289
 
    perror("setuid");
 
355
    perror("setgid");
290
356
  }
291
357
  
292
358
  dir = opendir(plugindir);
316
382
    
317
383
    // All directory entries have been processed
318
384
    if(dirst == NULL){
 
385
      if (errno == EBADF){
 
386
        perror("readdir");
 
387
        exitstatus = EXIT_FAILURE;
 
388
        goto end;
 
389
      }
319
390
      break;
320
391
    }
321
392
    
376
447
    strcat(filename, "/");      /* Spurious warning */
377
448
    strcat(filename, dirst->d_name); /* Spurious warning */
378
449
    
379
 
    stat(filename, &st);
 
450
    ret = stat(filename, &st);
 
451
    if (ret == -1){
 
452
      perror("stat");
 
453
      exitstatus = EXIT_FAILURE;
 
454
      goto end;
 
455
    }
380
456
    
381
457
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
382
458
      if(debug){
442
518
        perror("sigprocmask");
443
519
        _exit(EXIT_FAILURE);
444
520
      }
445
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
521
 
 
522
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
523
      if(ret == -1){
 
524
        perror("dup2");
 
525
        _exit(EXIT_FAILURE);
 
526
      }
446
527
      
447
528
      if(dirfd(dir) < 0){
448
529
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
500
581
  
501
582
  closedir(dir);
502
583
  dir = NULL;
503
 
  
 
584
    
504
585
  if (process_list == NULL){
505
586
    fprintf(stderr, "No plugin processes started, exiting\n");
506
587
    exitstatus = EXIT_FAILURE;
624
705
  /* Restore old signal handler */
625
706
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
626
707
  
 
708
  free(plus_argv);
 
709
  
627
710
  /* Free the plugin list */
628
711
  for(plugin *next; plugin_list != NULL; plugin_list = next){
629
712
    next = plugin_list->next;
639
722
  for(process *next; process_list != NULL; process_list = next){
640
723
    next = process_list->next;
641
724
    close(process_list->fd);
642
 
    kill(process_list->pid, SIGTERM);
 
725
    ret = kill(process_list->pid, SIGTERM);
 
726
    if(ret == -1 and errno != ESRCH){
 
727
      /* set-uid proccesses migth not get closed */
 
728
      perror("kill");
 
729
    }
643
730
    free(process_list->buffer);
644
731
    free(process_list);
645
732
  }