/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:
51
51
                                   close() */
52
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
53
53
                                   FD_CLOEXEC */
54
 
#include <string.h>             /* strtok, strlen(), strcpy(),
 
54
#include <string.h>             /* strsep, strlen(), strcpy(),
55
55
                                   strcat() */
56
56
#include <errno.h>              /* errno */
57
57
#include <argp.h>               /* struct argp_option, struct
66
66
 
67
67
#define BUFFER_SIZE 256
68
68
 
69
 
const char *argp_program_version = "plugbasedclient 1.0";
 
69
const char *argp_program_version = "mandos-client 1.0";
70
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
71
 
72
72
struct process;
170
170
  proc->completed = true;
171
171
}
172
172
 
 
173
bool print_out_password(const char *buffer, size_t length){
 
174
  ssize_t ret;
 
175
  if(length>0 and buffer[length-1] == '\n'){
 
176
    length--;
 
177
  }
 
178
  for(size_t written = 0; written < length; written += (size_t)ret){
 
179
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
180
                                   length - written));
 
181
    if(ret < 0){
 
182
      return false;
 
183
    }
 
184
  }
 
185
  return true;
 
186
}
 
187
 
173
188
int main(int argc, char *argv[]){
174
189
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
175
190
  size_t d_name_len;
187
202
                                      .sa_flags = SA_NOCLDSTOP };
188
203
  char *plus_options = NULL;
189
204
  char **plus_argv = NULL;
190
 
  
 
205
 
191
206
  /* Establish a signal handler */
192
207
  sigemptyset(&sigchld_action.sa_mask);
193
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
233
248
    switch (key) {
234
249
    case 'g':
235
250
      if (arg != NULL){
236
 
        char *p = strtok(arg, ",");
237
 
        do{
 
251
        char *p;
 
252
        while((p = strsep(&arg, ",")) != NULL){
 
253
          if(strcmp(p, "") == 0){
 
254
            continue;
 
255
          }
238
256
          addargument(getplugin(NULL, plugins), p);
239
 
          p = strtok(NULL, ",");
240
 
        } while (p != NULL);
 
257
        }
241
258
      }
242
259
      break;
243
260
    case 'o':
244
261
      if (arg != NULL){
245
 
        char *name = strtok(arg, ":");
246
 
        char *p = strtok(NULL, ":");
247
 
        if(p != NULL){
248
 
          p = strtok(p, ",");
249
 
          do{
 
262
        char *name = strsep(&arg, ":");
 
263
        if(strcmp(name, "") == 0){
 
264
          break;
 
265
        }
 
266
        char *opt = strsep(&arg, ":");
 
267
        if(strcmp(opt, "") == 0){
 
268
          break;
 
269
        }
 
270
        if(opt != NULL){
 
271
          char *p;
 
272
          while((p = strsep(&opt, ",")) != NULL){
 
273
            if(strcmp(p, "") == 0){
 
274
              continue;
 
275
            }
250
276
            addargument(getplugin(name, plugins), p);
251
 
            p = strtok(NULL, ",");
252
 
          } while (p != NULL);
 
277
          }
253
278
        }
254
279
      }
255
280
      break;
292
317
  
293
318
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
294
319
  if (ret == ARGP_ERR_UNKNOWN){
295
 
    fprintf(stderr, "Unkown error while parsing arguments\n");
 
320
    fprintf(stderr, "Unknown error while parsing arguments\n");
296
321
    exitstatus = EXIT_FAILURE;
297
322
    goto end;
298
323
  }
313
338
    }
314
339
    plus_argv[0] = argv[0];
315
340
    plus_argv[1] = NULL;
316
 
    arg = strtok(plus_options, delims); /* Get first argument */
317
 
    while(arg != NULL){
 
341
 
 
342
    while((arg = strsep(&plus_options, delims)) != NULL){
318
343
      new_argc++;
319
344
      plus_argv = realloc(plus_argv, sizeof(char *)
320
345
                         * ((unsigned int) new_argc + 1));
325
350
      }
326
351
      plus_argv[new_argc-1] = arg;
327
352
      plus_argv[new_argc] = NULL;
328
 
      arg = strtok(NULL, delims); /* Get next argument */
329
353
    }
 
354
 
330
355
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
331
356
    if (ret == ARGP_ERR_UNKNOWN){
332
 
      fprintf(stderr, "Unkown error while parsing arguments\n");
 
357
      fprintf(stderr, "Unknown error while parsing arguments\n");
333
358
      exitstatus = EXIT_FAILURE;
334
359
      goto end;
335
360
    }
440
465
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
441
466
    if (filename == NULL){
442
467
      perror("malloc");
443
 
      exitstatus = EXIT_FAILURE;
444
 
      goto end;
 
468
      continue;
445
469
    }
446
470
    strcpy(filename, plugindir); /* Spurious warning */
447
471
    strcat(filename, "/");      /* Spurious warning */
450
474
    ret = stat(filename, &st);
451
475
    if (ret == -1){
452
476
      perror("stat");
453
 
      exitstatus = EXIT_FAILURE;
454
 
      goto end;
 
477
      free(filename);
 
478
      continue;
455
479
    }
456
480
    
457
481
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
506
530
    }
507
531
    // Starting a new process to be watched
508
532
    pid_t pid = fork();
 
533
    if(pid == -1){
 
534
      perror("fork");
 
535
      exitstatus = EXIT_FAILURE;
 
536
      goto end;
 
537
    }
509
538
    if(pid == 0){
510
539
      /* this is the child process */
511
540
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
583
612
  dir = NULL;
584
613
    
585
614
  if (process_list == NULL){
586
 
    fprintf(stderr, "No plugin processes started, exiting\n");
587
 
    exitstatus = EXIT_FAILURE;
588
 
    goto end;
 
615
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
616
            " directory?\n");
 
617
    process_list = NULL;
589
618
  }
590
619
  while(process_list){
591
620
    fd_set rfds = rfds_all;
606
635
          /* Bad exit by plugin */
607
636
          if(debug){
608
637
            if(WIFEXITED(proc->status)){
609
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
610
 
                      proc->pid, WEXITSTATUS(proc->status));
 
638
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
639
                      (unsigned int) (proc->pid),
 
640
                      WEXITSTATUS(proc->status));
611
641
            } else if(WIFSIGNALED(proc->status)) {
612
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
613
 
                      proc->pid, WTERMSIG(proc->status));
 
642
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
643
                      (unsigned int) (proc->pid),
 
644
                      WTERMSIG(proc->status));
614
645
            } else if(WCOREDUMP(proc->status)){
615
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
646
              fprintf(stderr, "Plugin %d dumped core\n",
 
647
                      (unsigned int) (proc->pid));
616
648
            }
617
649
          }
618
650
          /* Remove the plugin */
651
683
          break;
652
684
        }
653
685
        /* This process exited nicely, so print its buffer */
654
 
        for(size_t written = 0; written < proc->buffer_length;
655
 
            written += (size_t)ret){
656
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
657
 
                                         proc->buffer + written,
658
 
                                         proc->buffer_length
659
 
                                         - written));
660
 
          if(ret < 0){
661
 
            perror("write");
662
 
            exitstatus = EXIT_FAILURE;
663
 
            goto end;
664
 
          }
 
686
 
 
687
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
688
        if(not bret){
 
689
          perror("print_out_password");
 
690
          exitstatus = EXIT_FAILURE;
665
691
        }
666
692
        goto end;
667
693
      }
696
722
      }
697
723
    }
698
724
  }
699
 
  if(process_list == NULL){
700
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
701
 
    exitstatus = EXIT_FAILURE;
702
 
  }
703
 
  
 
725
 
 
726
 
704
727
 end:
 
728
  
 
729
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
730
    /* Fallback if all plugins failed, none are found or an error occured */
 
731
    bool bret;
 
732
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
733
    char *passwordbuffer = getpass("Password: ");
 
734
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
735
    if(not bret){
 
736
      perror("print_out_password");
 
737
      exitstatus = EXIT_FAILURE;
 
738
      goto end;
 
739
    }
 
740
  }
 
741
  
705
742
  /* Restore old signal handler */
706
743
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
707
744