/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

changed from using strtok to strsep

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
#include <errno.h>              /* errno, EBADF */
66
66
 
67
67
#define BUFFER_SIZE 256
68
 
#define CONFFILE "/conf/conf.d/mandos/client.conf"
69
68
 
70
69
const char *argp_program_version = "mandos-client 1.0";
71
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
186
185
  return true;
187
186
}
188
187
 
189
 
char ** addcustomargument(char **argv, int *argc, char *arg){
190
 
 
191
 
  if (argv == NULL){
192
 
    *argc = 1;
193
 
    argv = malloc(sizeof(char*) * 2);
194
 
    if(argv == NULL){
195
 
      return NULL;
196
 
    }
197
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
198
 
    argv[1] = NULL;
199
 
  }
200
 
  *argc += 1;
201
 
  argv = realloc(argv, sizeof(char *)
202
 
                  * ((unsigned int) *argc + 1));
203
 
  if(argv == NULL){
204
 
    return NULL;
205
 
  }
206
 
  argv[*argc-1] = arg;
207
 
  argv[*argc] = NULL;   
208
 
  return argv;
209
 
}
210
 
 
211
188
int main(int argc, char *argv[]){
212
189
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
213
 
  const char *conffile = CONFFILE;
214
 
  FILE *conffp;
215
190
  size_t d_name_len;
216
191
  DIR *dir = NULL;
217
192
  struct dirent *dirst;
225
200
  struct sigaction old_sigchld_action;
226
201
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
227
202
                                      .sa_flags = SA_NOCLDSTOP };
228
 
  char **custom_argv = NULL;
229
 
  int custom_argc = 0;
230
 
  
 
203
  char *plus_options = NULL;
 
204
  char **plus_argv = NULL;
 
205
 
231
206
  /* Establish a signal handler */
232
207
  sigemptyset(&sigchld_action.sa_mask);
233
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
275
250
      if (arg != NULL){
276
251
        char *p;
277
252
        while((p = strsep(&arg, ",")) != NULL){
278
 
          if(p[0] == '\0'){
 
253
          if(strcmp(p, "") == 0){
279
254
            continue;
280
255
          }
281
256
          addargument(getplugin(NULL, plugins), p);
285
260
    case 'o':
286
261
      if (arg != NULL){
287
262
        char *name = strsep(&arg, ":");
288
 
        if(name[0] == '\0'){
 
263
        if(strcmp(name, "") == 0){
289
264
          break;
290
265
        }
291
266
        char *opt = strsep(&arg, ":");
292
 
        if(opt[0] == '\0'){
 
267
        if(strcmp(opt, "") == 0){
293
268
          break;
294
269
        }
295
270
        if(opt != NULL){
296
271
          char *p;
297
272
          while((p = strsep(&opt, ",")) != NULL){
298
 
            if(p[0] == '\0'){
 
273
            if(strcmp(p, "") == 0){
299
274
              continue;
300
275
            }
301
276
            addargument(getplugin(name, plugins), p);
321
296
      debug = true;
322
297
      break;
323
298
    case ARGP_KEY_ARG:
324
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg); 
 
299
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
300
        argp_usage (state);
 
301
      }
 
302
      plus_options = arg;
325
303
      break;
326
304
    case ARGP_KEY_END:
327
305
      break;
343
321
    exitstatus = EXIT_FAILURE;
344
322
    goto end;
345
323
  }
346
 
 
347
 
  conffp = fopen(conffile, "r");
348
 
  if(conffp != NULL){
349
 
    char *org_line = NULL;
350
 
    size_t size = 0;
351
 
    ssize_t sret;
352
 
    char *p, *arg, *new_arg, *line;
353
 
    const char whitespace_delims[] = " \r\t\f\v\n";
354
 
    const char comment_delim[] = "#";
355
 
 
356
 
    while(true){
357
 
      sret = getline(&org_line, &size, conffp);
358
 
      if(sret == -1){
359
 
        break;
360
 
      }
361
 
 
362
 
      line = org_line;
363
 
      arg = strsep(&line, comment_delim);
364
 
      while((p = strsep(&arg, whitespace_delims)) != NULL){
365
 
        if(p[0] == '\0'){
366
 
          continue;
367
 
        }
368
 
        new_arg = strdup(p);
369
 
        custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
370
 
        if (custom_argv == NULL){
371
 
          perror("addcustomargument");
372
 
          exitstatus = EXIT_FAILURE;
373
 
          goto end;
374
 
        }
375
 
      }
376
 
    }
377
 
    free(org_line);
378
 
  } else{
379
 
    /* check for harmfull errors */
380
 
    if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
381
 
      perror("fopen");
 
324
  
 
325
  if(plus_options){
 
326
    /* This is a mangled argument in the form of
 
327
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
328
    /* Make new argc and argv vars, and call argp_parse() again. */
 
329
    plus_options++;             /* skip the first '+' character */
 
330
    const char delims[] = "+";
 
331
    char *arg;
 
332
    int new_argc = 1;
 
333
    plus_argv = malloc(sizeof(char*) * 2);
 
334
    if(plus_argv == NULL){
 
335
      perror("malloc");
382
336
      exitstatus = EXIT_FAILURE;
383
337
      goto end;
384
338
    }
385
 
  }
386
 
 
387
 
  if(custom_argv != NULL){
388
 
    custom_argv[0] = argv[0];
389
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
 
339
    plus_argv[0] = argv[0];
 
340
    plus_argv[1] = NULL;
 
341
 
 
342
    while((arg = strsep(&plus_options, delims)) != NULL){
 
343
      new_argc++;
 
344
      plus_argv = realloc(plus_argv, sizeof(char *)
 
345
                         * ((unsigned int) new_argc + 1));
 
346
      if(plus_argv == NULL){
 
347
        perror("realloc");
 
348
        exitstatus = EXIT_FAILURE;
 
349
        goto end;
 
350
      }
 
351
      plus_argv[new_argc-1] = arg;
 
352
      plus_argv[new_argc] = NULL;
 
353
    }
 
354
 
 
355
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
390
356
    if (ret == ARGP_ERR_UNKNOWN){
391
357
      fprintf(stderr, "Unknown error while parsing arguments\n");
392
358
      exitstatus = EXIT_FAILURE;
776
742
  /* Restore old signal handler */
777
743
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
778
744
  
779
 
  free(custom_argv);
 
745
  free(plus_argv);
780
746
  
781
747
  /* Free the plugin list */
782
748
  for(plugin *next; plugin_list != NULL; plugin_list = next){