/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 plugbasedclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-07-31 19:48:05 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080731194805-mseis21dxwrdfqhk
* plugins.d/mandosclient.c (start_mandos_communication): Changed
                                                        "if_index" to
                                                        be of type
                                                        "AvahiIfIndex".
                                                        All callers
                                                        changed.
 (main): Add default values to "interface" and "if_index".  Only
         change if_index from default if "interface" was given.

* server.py (IPv6_TCPServer.server_bind): Bug fix: test if interface
                                          is empty, not if equal to
                                          avahi.IF_UNSPEC.
  (if_nametoindex): Bug fix; typo: assign to _func[0], not func[0].
  (main): Bug fix: Do not set service.interface unless the interface
          setting has been given.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
22
22
 */
23
23
 
24
 
#include <stdio.h>              /* popen(), fileno(), fprintf(),
25
 
                                   stderr, STDOUT_FILENO */
26
 
#include <iso646.h>             /* and, or, not */
27
 
#include <sys/types.h>         /* DIR, opendir(), stat(), struct stat,
28
 
                                  waitpid(), WIFEXITED(),
29
 
                                  WEXITSTATUS(), wait() */
30
 
#include <sys/wait.h>           /* wait() */
31
 
#include <dirent.h>             /* DIR, struct dirent, opendir(),
32
 
                                   readdir(), closedir() */
33
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
34
 
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
35
 
                                   fcntl() */
36
 
#include <fcntl.h>              /* fcntl() */
37
 
#include <stddef.h>             /* NULL */
38
 
#include <stdlib.h>             /* EXIT_FAILURE */
39
 
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
40
 
                                   FD_SET(), FD_ISSET() */
41
 
#include <string.h>             /* strlen(), strcpy(), strcat() */
42
 
#include <stdbool.h>            /* true */
43
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
44
 
                                   WEXITSTATUS() */
45
 
#include <errno.h>              /* errno */
46
 
#include <argp.h>               /* struct argp_option,
47
 
                                   struct argp_state, struct argp,
48
 
                                   argp_parse() */
 
24
#include <stdio.h>      /* popen, fileno */
 
25
#include <iso646.h>     /* and, or, not */
 
26
#include <sys/types.h>  /* DIR, opendir, stat, struct stat, waitpid,
 
27
                           WIFEXITED, WEXITSTATUS, wait */
 
28
#include <sys/wait.h>   /* wait */
 
29
#include <dirent.h>     /* DIR, opendir */
 
30
#include <sys/stat.h>   /* stat, struct stat */
 
31
#include <unistd.h>     /* stat, struct stat, chdir */
 
32
#include <stdlib.h>     /* EXIT_FAILURE */
 
33
#include <sys/select.h> /* fd_set, select, FD_ZERO, FD_SET,
 
34
                           FD_ISSET */
 
35
#include <string.h>     /* strlen, strcpy, strcat */
 
36
#include <stdbool.h>    /* true */
 
37
#include <sys/wait.h>   /* waitpid, WIFEXITED, WEXITSTATUS */
 
38
#include <errno.h>      /* errno */
 
39
#include <argp.h>       /* argp */
49
40
 
50
41
struct process;
51
42
 
59
50
} process;
60
51
 
61
52
typedef struct plugin{
62
 
  char *name;                   /* can be NULL or any plugin name */
 
53
  char *name;           /* can be "global" and any plugin name */
63
54
  char **argv;
64
55
  int argc;
65
 
  bool disabled;
66
56
  struct plugin *next;
67
57
} plugin;
68
58
 
88
78
  new_plugin->argv[0] = name;
89
79
  new_plugin->argv[1] = NULL;
90
80
  new_plugin->argc = 1;
91
 
  new_plugin->disabled = false;
 
81
  /* Append the new plugin to the list */
92
82
  new_plugin->next = *plugin_list;
93
 
  /* Append the new plugin to the list */
94
83
  *plugin_list = new_plugin;
95
84
  return new_plugin;
96
85
}
97
86
 
98
 
void addargument(plugin *p, char *arg){
 
87
void addarguments(plugin *p, char *arg){
99
88
  p->argv[p->argc] = arg;
100
89
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
101
90
  if (p->argv == NULL){
105
94
  p->argc++;
106
95
  p->argv[p->argc] = NULL;
107
96
}
108
 
 
 
97
        
109
98
#define BUFFER_SIZE 256
110
99
 
111
100
const char *argp_program_version =
112
101
  "plugbasedclient 0.9";
113
102
const char *argp_program_bug_address =
114
103
  "<mandos@fukt.bsnet.se>";
 
104
static char doc[] =
 
105
  "Mandos plugin runner -- Run Mandos plugins";
 
106
/* A description of the arguments we accept. */
 
107
static char args_doc[] = "";
115
108
 
116
109
int main(int argc, char *argv[]){
117
 
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
118
 
  size_t d_name_len;
 
110
  char plugindir[] = "plugins.d";
 
111
  size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
119
112
  DIR *dir;
120
113
  struct dirent *dirst;
121
114
  struct stat st;
122
 
  fd_set rfds_all;
 
115
  fd_set rfds_orig;
123
116
  int ret, maxfd = 0;
124
117
  process *process_list = NULL;
125
 
  bool debug = false;
126
 
  int exitstatus = EXIT_SUCCESS;
127
118
  
128
119
  /* The options we understand. */
129
120
  struct argp_option options[] = {
130
121
    { .name = "global-options", .key = 'g',
131
 
      .arg = "OPTION[,OPTION[,...]]",
132
 
      .doc = "Options passed to all plugins" },
 
122
      .arg = "option[,option[,...]]", .flags = 0,
 
123
      .doc = "Options effecting all plugins" },
133
124
    { .name = "options-for", .key = 'o',
134
 
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
135
 
      .doc = "Options passed only to specified plugin" },
136
 
    { .name = "disable", .key = 'd',
137
 
      .arg = "PLUGIN",
138
 
      .doc = "Disable a specific plugin", .group = 1 },
139
 
    { .name = "plugin-dir", .key = 128,
140
 
      .arg = "DIRECTORY",
141
 
      .doc = "Specify a different plugin directory", .group = 2 },
142
 
    { .name = "debug", .key = 129,
143
 
      .doc = "Debug mode", .group = 3 },
 
125
      .arg = "plugin:option[,option[,...]]", .flags = 0,
 
126
      .doc = "Options effecting only specified plugins" },
144
127
    { .name = NULL }
145
128
  };
146
129
  
147
130
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
148
131
       /* Get the INPUT argument from `argp_parse', which we
149
 
          know is a pointer to our plugin list pointer. */
 
132
          know is a pointer to our arguments structure. */
150
133
    plugin **plugins = state->input;
151
134
    switch (key) {
152
135
    case 'g':
153
136
      if (arg != NULL){
154
137
        char *p = strtok(arg, ",");
155
138
        do{
156
 
          addargument(getplugin(NULL, plugins), p);
 
139
          addarguments(getplugin(NULL, plugins), p);
157
140
          p = strtok(NULL, ",");
158
141
        } while (p);
159
142
      }
162
145
      if (arg != NULL){
163
146
        char *name = strtok(arg, ":");
164
147
        char *p = strtok(NULL, ":");
165
 
        if(p){
166
 
          p = strtok(p, ",");
167
 
          do{
168
 
            addargument(getplugin(name, plugins), p);
169
 
            p = strtok(NULL, ",");
170
 
          } while (p);
171
 
        }
172
 
      }
173
 
      break;
174
 
    case 'd':
175
 
      if (arg != NULL){
176
 
        getplugin(arg, plugins)->disabled = true;
177
 
      }
178
 
      break;
179
 
    case 128:
180
 
      plugindir = arg;
181
 
      break;
182
 
    case 129:
183
 
      debug = true;
 
148
        p = strtok(p, ",");
 
149
        do{
 
150
          addarguments(getplugin(name, plugins), p);
 
151
          p = strtok(NULL, ",");
 
152
        } while (p);
 
153
      }
184
154
      break;
185
155
    case ARGP_KEY_ARG:
186
156
      argp_usage (state);
192
162
    }
193
163
    return 0;
194
164
  }
195
 
  
 
165
 
196
166
  plugin *plugin_list = NULL;
197
167
  
198
168
  struct argp argp = { .options = options, .parser = parse_opt,
199
 
                       .args_doc = "",
200
 
                       .doc = "Mandos plugin runner -- Run plugins" };
201
 
  
 
169
                       .args_doc = args_doc, .doc = doc };
 
170
 
202
171
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
172
 
 
173
/*   for(plugin *p = plugin_list; p != NULL; p=p->next){ */
 
174
/*     fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
 
175
/*     for(char **a = p->argv + 1; *a != NULL; a++){ */
 
176
/*       fprintf(stderr, "\tArg: %s\n", *a); */
 
177
/*     } */
 
178
/*   } */
203
179
  
204
 
  if(debug){
205
 
    for(plugin *p = plugin_list; p != NULL; p=p->next){
206
 
      fprintf(stderr, "Plugin: %s has %d arguments\n",
207
 
              p->name ? p->name : "Global", p->argc);
208
 
      for(char **a = p->argv; *a != NULL; a++){
209
 
        fprintf(stderr, "\tArg: %s\n", *a);
210
 
      }
211
 
    }
212
 
  }
 
180
/*   return 0; */
213
181
  
214
182
  dir = opendir(plugindir);
215
 
  {
216
 
    /* Set the FD_CLOEXEC flag on the directory */
217
 
    int fd = dirfd(dir);
218
 
    ret = fcntl (fd, F_GETFD, 0);
219
 
    if(ret < 0){
220
 
      perror("fcntl F_GETFD");
221
 
      goto end;
222
 
    }
223
 
    ret = fcntl(fd, F_SETFD, FD_CLOEXEC | ret);
224
 
    if(ret < 0){
225
 
      perror("fcntl F_SETFD");
226
 
      goto end;
227
 
    }
228
 
  }
229
183
  
230
184
  if(dir == NULL){
231
185
    fprintf(stderr, "Can not open directory\n");
232
186
    return EXIT_FAILURE;
233
187
  }
234
188
  
235
 
  FD_ZERO(&rfds_all);
 
189
  FD_ZERO(&rfds_orig);
236
190
  
237
191
  while(true){
238
192
    dirst = readdir(dir);
244
198
    
245
199
    d_name_len = strlen(dirst->d_name);
246
200
    
247
 
    // Ignore dotfiles, backup files and other junk
248
 
    {
249
 
      bool bad_name = false;
250
 
      
251
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
252
 
      
253
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
254
 
                                           ".dpkg-old",
255
 
                                           ".dpkg-divert", NULL };
256
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
257
 
        size_t pre_len = strlen(*pre);
258
 
        if((d_name_len >= pre_len)
259
 
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
260
 
          if(debug){
261
 
            fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
262
 
                    " with bad prefix %s\n", dirst->d_name, *pre);
263
 
          }
264
 
          bad_name = true;
265
 
          break;
266
 
        }
267
 
      }
268
 
      
269
 
      if(bad_name){
270
 
        continue;
271
 
      }
272
 
      
273
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
274
 
        size_t suf_len = strlen(*suf);
275
 
        if((d_name_len >= suf_len)
276
 
           and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
277
 
                == 0)){
278
 
          if(debug){
279
 
            fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
280
 
                    " with bad suffix %s\n", dirst->d_name, *suf);
281
 
          }
282
 
          bad_name = true;
283
 
          break;
284
 
        }
285
 
      }
286
 
      
287
 
      if(bad_name){
288
 
        continue;
289
 
      }
290
 
    }
291
 
    
292
 
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
293
 
    if (filename == NULL){
294
 
      perror("malloc");
295
 
      exitstatus =EXIT_FAILURE;
296
 
      goto end;
297
 
    }
 
201
    // Ignore dotfiles and backup files
 
202
    if (dirst->d_name[0] == '.'
 
203
        or dirst->d_name[d_name_len - 1] == '~'){
 
204
      continue;
 
205
    }
 
206
 
 
207
    char *filename = malloc(d_name_len + plugindir_len + 2);
298
208
    strcpy(filename, plugindir);
299
209
    strcat(filename, "/");
300
210
    strcat(filename, dirst->d_name);    
301
211
 
302
212
    stat(filename, &st);
303
213
 
304
 
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
305
 
      if(debug){
306
 
        fprintf(stderr, "Ignoring plugin dir entry name \"%s\""
307
 
                " with bad type or mode\n", filename);
308
 
      }
309
 
      continue;
310
 
    }
311
 
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
312
 
      if(debug){
313
 
        fprintf(stderr, "Ignoring disabled plugin \"%s\"",
314
 
                dirst->d_name);
315
 
      }
316
 
      continue;
317
 
    }
318
 
    // Starting a new process to be watched
319
 
    int pipefd[2]; 
320
 
    ret = pipe(pipefd);
321
 
    if (ret == -1){
322
 
      perror(argv[0]);
323
 
      goto end;
324
 
    }
325
 
    pid_t pid = fork();
326
 
    if(pid == 0){
327
 
      /* this is the child process */
328
 
      closedir(dir);
329
 
      close(pipefd[0]); /* close unused read end of pipe */
330
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
331
 
      
332
 
      plugin *p = getplugin(dirst->d_name, &plugin_list);
333
 
      plugin *g = getplugin(NULL, &plugin_list);
334
 
      for(char **a = g->argv + 1; *a != NULL; a++){
335
 
        addargument(p, *a);
336
 
      }
337
 
      if(execv(filename, p->argv) < 0){
338
 
        perror(argv[0]);
339
 
        close(pipefd[1]);
340
 
        _exit(EXIT_FAILURE);
341
 
      }
342
 
      /* no return */
343
 
    }
344
 
    close(pipefd[1]);           /* close unused write end of pipe */
345
 
    process *new_process = malloc(sizeof(process));
346
 
    if (new_process == NULL){
347
 
      perror("malloc");
348
 
      exitstatus = EXIT_FAILURE;
349
 
      goto end;
350
 
    }
351
 
    
352
 
    new_process->fd = pipefd[0];
353
 
    new_process->buffer = malloc(BUFFER_SIZE);
354
 
    if (new_process->buffer == NULL){
355
 
      perror("malloc");
356
 
      exitstatus = EXIT_FAILURE;
357
 
      goto end;
358
 
    }
359
 
    new_process->buffer_size = BUFFER_SIZE;
360
 
    new_process->buffer_length = 0;
361
 
    FD_SET(new_process->fd, &rfds_all);
362
 
      
363
 
    if (maxfd < new_process->fd){
364
 
      maxfd = new_process->fd;
365
 
    }
366
 
    
367
 
    //List handling
368
 
    new_process->next = process_list;
369
 
    process_list = new_process;
 
214
    if (S_ISREG(st.st_mode) and (access(filename, X_OK) == 0)){
 
215
      // Starting a new process to be watched
 
216
      process *new_process = malloc(sizeof(process));
 
217
      int pipefd[2];
 
218
      ret = pipe(pipefd);
 
219
      if (ret == -1){
 
220
        perror(argv[0]);
 
221
        goto end;
 
222
      }
 
223
      new_process->pid = fork();
 
224
      if(new_process->pid == 0){
 
225
        /* this is the child process */
 
226
        closedir(dir);
 
227
        close(pipefd[0]);       /* close unused read end of pipe */
 
228
        dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
229
        char *basename;
 
230
        basename = strrchr(filename, '/');
 
231
        if (basename == NULL){
 
232
          basename = filename;
 
233
        } else {
 
234
          basename++;
 
235
        }
 
236
        plugin *p = getplugin(basename, &plugin_list);
 
237
 
 
238
        plugin *g = getplugin(NULL, &plugin_list);
 
239
        for(char **a = g->argv + 1; *a != NULL; a++){
 
240
          addarguments(p, *a);
 
241
        }
 
242
        if(execv(filename, p->argv) < 0){
 
243
          perror(argv[0]);
 
244
          close(pipefd[1]);
 
245
          exit(EXIT_FAILURE);
 
246
        }
 
247
        /* no return */
 
248
      }
 
249
      close(pipefd[1]);         /* close unused write end of pipe */
 
250
      new_process->fd = pipefd[0];
 
251
      new_process->buffer = malloc(BUFFER_SIZE);
 
252
      if (new_process->buffer == NULL){
 
253
        perror(argv[0]);
 
254
        goto end;
 
255
      }
 
256
      new_process->buffer_size = BUFFER_SIZE;
 
257
      new_process->buffer_length = 0;
 
258
      FD_SET(new_process->fd, &rfds_orig);
 
259
      
 
260
      if (maxfd < new_process->fd){
 
261
        maxfd = new_process->fd;
 
262
      }
 
263
      
 
264
      //List handling
 
265
      new_process->next = process_list;
 
266
      process_list = new_process;
 
267
    }
370
268
  }
371
269
  
372
270
  closedir(dir);
373
271
  
374
272
  if (process_list != NULL){
375
273
    while(true){
376
 
      fd_set rfds = rfds_all;
 
274
      fd_set rfds = rfds_orig;
377
275
      int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
378
276
      if (select_ret == -1){
379
277
        perror(argv[0]);
419
317
                }
420
318
                goto end;
421
319
              } else {
422
 
                FD_CLR(process_itr->fd, &rfds_all);
 
320
                FD_CLR(process_itr->fd, &rfds_orig);
423
321
              }
424
322
            }
425
323
          }
446
344
      break;
447
345
    }
448
346
  }  
449
 
  return exitstatus;
 
347
  return EXIT_SUCCESS;
450
348
}