/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

  • Committer: Teddy Hogeborn
  • Date: 2008-08-10 20:35:01 UTC
  • mfrom: (24.1.42 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080810203501-euh3qcf1nopilksm
Merge.

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>             /* strsep, strlen(), strcpy(),
 
54
#include <string.h>             /* strtok, strlen(), strcpy(),
55
55
                                   strcat() */
56
56
#include <errno.h>              /* errno */
57
57
#include <argp.h>               /* struct argp_option, struct
171
171
}
172
172
 
173
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){
 
174
  size_t ret;
 
175
  for(size_t written = 0; written < length; written += ret){
179
176
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
180
177
                                   length - written));
181
178
    if(ret < 0){
202
199
                                      .sa_flags = SA_NOCLDSTOP };
203
200
  char *plus_options = NULL;
204
201
  char **plus_argv = NULL;
205
 
 
 
202
  
206
203
  /* Establish a signal handler */
207
204
  sigemptyset(&sigchld_action.sa_mask);
208
205
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
248
245
    switch (key) {
249
246
    case 'g':
250
247
      if (arg != NULL){
251
 
        char *p;
252
 
        while((p = strsep(&arg, ",")) != NULL){
253
 
          if(strcmp(p, "") == 0){
254
 
            continue;
255
 
          }
 
248
        char *p = strtok(arg, ",");
 
249
        do{
256
250
          addargument(getplugin(NULL, plugins), p);
257
 
        }
 
251
          p = strtok(NULL, ",");
 
252
        } while (p != NULL);
258
253
      }
259
254
      break;
260
255
    case 'o':
261
256
      if (arg != NULL){
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
 
            }
 
257
        char *name = strtok(arg, ":");
 
258
        char *p = strtok(NULL, ":");
 
259
        if(p != NULL){
 
260
          p = strtok(p, ",");
 
261
          do{
276
262
            addargument(getplugin(name, plugins), p);
277
 
          }
 
263
            p = strtok(NULL, ",");
 
264
          } while (p != NULL);
278
265
        }
279
266
      }
280
267
      break;
317
304
  
318
305
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
319
306
  if (ret == ARGP_ERR_UNKNOWN){
320
 
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
307
    fprintf(stderr, "Unkown error while parsing arguments\n");
321
308
    exitstatus = EXIT_FAILURE;
322
309
    goto end;
323
310
  }
338
325
    }
339
326
    plus_argv[0] = argv[0];
340
327
    plus_argv[1] = NULL;
341
 
 
342
 
    while((arg = strsep(&plus_options, delims)) != NULL){
 
328
    arg = strtok(plus_options, delims); /* Get first argument */
 
329
    while(arg != NULL){
343
330
      new_argc++;
344
331
      plus_argv = realloc(plus_argv, sizeof(char *)
345
332
                         * ((unsigned int) new_argc + 1));
350
337
      }
351
338
      plus_argv[new_argc-1] = arg;
352
339
      plus_argv[new_argc] = NULL;
 
340
      arg = strtok(NULL, delims); /* Get next argument */
353
341
    }
354
 
 
355
342
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
356
343
    if (ret == ARGP_ERR_UNKNOWN){
357
 
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
344
      fprintf(stderr, "Unkown error while parsing arguments\n");
358
345
      exitstatus = EXIT_FAILURE;
359
346
      goto end;
360
347
    }
465
452
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
466
453
    if (filename == NULL){
467
454
      perror("malloc");
468
 
      continue;
 
455
      exitstatus = EXIT_FAILURE;
 
456
      goto end;
469
457
    }
470
458
    strcpy(filename, plugindir); /* Spurious warning */
471
459
    strcat(filename, "/");      /* Spurious warning */
474
462
    ret = stat(filename, &st);
475
463
    if (ret == -1){
476
464
      perror("stat");
477
 
      free(filename);
478
 
      continue;
 
465
      exitstatus = EXIT_FAILURE;
 
466
      goto end;
479
467
    }
480
468
    
481
469
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
530
518
    }
531
519
    // Starting a new process to be watched
532
520
    pid_t pid = fork();
533
 
    if(pid == -1){
534
 
      perror("fork");
535
 
      exitstatus = EXIT_FAILURE;
536
 
      goto end;
537
 
    }
538
521
    if(pid == 0){
539
522
      /* this is the child process */
540
523
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
722
705
      }
723
706
    }
724
707
  }
725
 
 
726
 
 
727
 
 end:
728
708
  
729
 
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
730
 
    /* Fallback if all plugins failed, none are found or an error occured */
 
709
  if(process_list == NULL){
731
710
    bool bret;
732
711
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
733
712
    char *passwordbuffer = getpass("Password: ");
737
716
      exitstatus = EXIT_FAILURE;
738
717
      goto end;
739
718
    }
 
719
    bret = print_out_password("\n", 1);
 
720
    if(not bret){
 
721
      perror("print_out_password");
 
722
      exitstatus = EXIT_FAILURE;
 
723
    }
740
724
  }
 
725
 
 
726
 end:
741
727
  
742
728
  /* Restore old signal handler */
743
729
  sigaction(SIGCHLD, &old_sigchld_action, NULL);