/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-22 01:59:47 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080722015947-y7o6keji020nvrhw
* Makefile (OPTIMIZE): New; optimize for size.
  (CFLAGS): Use $(OPTIMIZE).

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <stdbool.h>    /* true */
40
40
#include <sys/wait.h>   /* waitpid, WIFEXITED, WEXITSTATUS */
41
41
#include <errno.h>      /* errno */
42
 
#include <argp.h>       /* argp */
43
42
 
44
43
struct process;
45
44
 
52
51
  struct process *next;
53
52
} process;
54
53
 
55
 
typedef struct plugin{
56
 
  char *name;           /* can be "global" and any plugin name */
57
 
  char **argv;
58
 
  int argc;
59
 
  struct plugin *next;
60
 
} plugin;
61
 
 
62
 
plugin *getplugin(char *name, plugin **plugin_list){
63
 
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
64
 
    if ((p->name == name)
65
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
66
 
      return p;
67
 
    }
68
 
  }
69
 
  /* Create a new plugin */
70
 
  plugin *new_plugin = malloc(sizeof(plugin));
71
 
  if (new_plugin == NULL){
72
 
    perror("malloc");
73
 
    exit(EXIT_FAILURE);
74
 
  }
75
 
  new_plugin->name = name;
76
 
  new_plugin->argv = malloc(sizeof(char *) * 2);
77
 
  if (new_plugin->argv == NULL){
78
 
    perror("malloc");
79
 
    exit(EXIT_FAILURE);
80
 
  }
81
 
  new_plugin->argv[0] = name;
82
 
  new_plugin->argv[1] = NULL;
83
 
  new_plugin->argc = 1;
84
 
  /* Append the new plugin to the list */
85
 
  new_plugin->next = *plugin_list;
86
 
  *plugin_list = new_plugin;
87
 
  return new_plugin;
88
 
}
89
 
 
90
 
void addarguments(plugin *p, char *arg){
91
 
  p->argv[p->argc] = arg;
92
 
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
93
 
  if (p->argv == NULL){
94
 
    perror("malloc");
95
 
    exit(EXIT_FAILURE);
96
 
  }
97
 
  p->argc++;
98
 
  p->argv[p->argc] = NULL;
99
 
}
100
 
        
101
54
#define BUFFER_SIZE 256
102
55
 
103
 
const char *argp_program_version =
104
 
  "plugbasedclient 0.9";
105
 
const char *argp_program_bug_address =
106
 
  "<mandos@fukt.bsnet.se>";
107
 
static char doc[] =
108
 
  "Mandos plugin runner -- Run Mandos plugins";
109
 
/* A description of the arguments we accept. */
110
 
static char args_doc[] = "";
111
 
 
112
56
int main(int argc, char *argv[]){
113
57
  char plugindir[] = "plugins.d";
114
58
  size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
119
63
  int ret, maxfd = 0;
120
64
  process *process_list = NULL;
121
65
  
122
 
  /* The options we understand. */
123
 
  struct argp_option options[] = {
124
 
    { .name = "global-options", .key = 'g',
125
 
      .arg = "option[,option[,...]]", .flags = 0,
126
 
      .doc = "Options effecting all plugins" },
127
 
    { .name = "options-for", .key = 'o',
128
 
      .arg = "plugin:option[,option[,...]]", .flags = 0,
129
 
      .doc = "Options effecting only specified plugins" },
130
 
    { .name = NULL }
131
 
  };
132
 
  
133
 
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
134
 
       /* Get the INPUT argument from `argp_parse', which we
135
 
          know is a pointer to our arguments structure. */
136
 
    plugin **plugins = state->input;
137
 
    switch (key) {
138
 
    case 'g':
139
 
      if (arg != NULL){
140
 
        char *p = strtok(arg, ",");
141
 
        do{
142
 
          addarguments(getplugin(NULL, plugins), p);
143
 
          p = strtok(NULL, ",");
144
 
        } while (p);
145
 
      }
146
 
      break;
147
 
    case 'o':
148
 
      if (arg != NULL){
149
 
        char *name = strtok(arg, ":");
150
 
        char *p = strtok(NULL, ":");
151
 
        p = strtok(p, ",");
152
 
        do{
153
 
          addarguments(getplugin(name, plugins), p);
154
 
          p = strtok(NULL, ",");
155
 
        } while (p);
156
 
      }
157
 
      break;
158
 
    case ARGP_KEY_ARG:
159
 
      argp_usage (state);
160
 
      break;
161
 
    case ARGP_KEY_END:
162
 
      break;
163
 
    default:
164
 
      return ARGP_ERR_UNKNOWN;
165
 
    }
166
 
    return 0;
167
 
  }
168
 
 
169
 
  plugin *plugin_list = NULL;
170
 
  
171
 
  struct argp argp = { .options = options, .parser = parse_opt,
172
 
                       .args_doc = args_doc, .doc = doc };
173
 
 
174
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
175
 
 
176
 
/*   for(plugin *p = plugin_list; p != NULL; p=p->next){ */
177
 
/*     fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
178
 
/*     for(char **a = p->argv + 1; *a != NULL; a++){ */
179
 
/*       fprintf(stderr, "\tArg: %s\n", *a); */
180
 
/*     } */
181
 
/*   } */
182
 
  
183
 
/*   return 0; */
184
 
  
185
66
  dir = opendir(plugindir);
186
 
  
 
67
 
187
68
  if(dir == NULL){
188
69
    fprintf(stderr, "Can not open directory\n");
189
70
    return EXIT_FAILURE;
218
99
      // Starting a new process to be watched
219
100
      process *new_process = malloc(sizeof(process));
220
101
      int pipefd[2];
221
 
      ret = pipe(pipefd);
222
 
      if (ret == -1){
223
 
        perror(argv[0]);
224
 
        goto end;
225
 
      }
 
102
      pipe(pipefd);
226
103
      new_process->pid = fork();
227
104
      if(new_process->pid == 0){
228
105
        /* this is the child process */
229
106
        closedir(dir);
230
107
        close(pipefd[0]);       /* close unused read end of pipe */
231
108
        dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
232
 
        char *basename;
233
 
        basename = strrchr(filename, '/');
234
 
        if (basename == NULL){
235
 
          basename = filename;
236
 
        } else {
237
 
          basename++;
238
 
        }
239
 
        plugin *p = getplugin(basename, &plugin_list);
240
 
 
241
 
        plugin *g = getplugin(NULL, &plugin_list);
242
 
        for(char **a = g->argv + 1; *a != NULL; a++){
243
 
          addarguments(p, *a);
244
 
        }
245
 
        if(execv(filename, p->argv) < 0){
 
109
        /* create a new modified argument list */
 
110
        char **new_argv = malloc(sizeof(char *)
 
111
                                 * ((unsigned int) argc + 1));
 
112
        new_argv[0] = filename;
 
113
        for(int i = 1; i < argc; i++){
 
114
          new_argv[i] = argv[i];
 
115
        }
 
116
        new_argv[argc] = NULL;
 
117
        if(execv(filename, new_argv) < 0){
246
118
          perror(argv[0]);
247
119
          close(pipefd[1]);
248
120
          exit(EXIT_FAILURE);
307
179
              int status;
308
180
              waitpid(process_itr->pid, &status, 0);
309
181
              if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
310
 
                for(size_t written = 0;
311
 
                    written < process_itr->buffer_length;){
312
 
                  ret = write(STDOUT_FILENO,
313
 
                              process_itr->buffer + written,
314
 
                              process_itr->buffer_length - written);
315
 
                  if(ret < 0){
316
 
                    perror(argv[0]);
317
 
                    goto end;
318
 
                  }
319
 
                  written += (size_t)ret;
320
 
                }
 
182
                write(STDOUT_FILENO, process_itr->buffer,
 
183
                      process_itr->buffer_length);
321
184
                goto end;
322
185
              } else {
323
186
                FD_CLR(process_itr->fd, &rfds_orig);