/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 plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-09-21 12:04:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080921120402-mgd2jl8xo634jw18
* Makefile: Put the init script before avahi-daemon.

* debian/mandos.prerm: Bug fix: stop mandos, not ssh.

* debian/rules (install-indep): Put the init script before
                                avahi-daemon.

* init.d/mandos: Require "avahi-daemon".

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
const char *argp_program_version = "plugin-runner 1.0";
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
75
 
struct plugin;
76
 
 
77
75
typedef struct plugin{
78
76
  char *name;                   /* can be NULL or any plugin name */
79
77
  char **argv;
96
94
 
97
95
static plugin *plugin_list = NULL;
98
96
 
99
 
/* Gets a existing plugin based on name,
 
97
/* Gets an existing plugin based on name,
100
98
   or if none is found, creates a new one */
101
99
static plugin *getplugin(char *name){
102
100
  /* Check for exiting plugin with that name */
118
116
      return NULL;
119
117
    }
120
118
  }
121
 
 
 
119
  
122
120
  *new_plugin = (plugin) { .name = copy_name,
123
121
                           .argc = 1,
124
122
                           .disabled = false,
187
185
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
188
186
  /* Search for this environment variable */
189
187
  for(char **e = p->environ; *e != NULL; e++){
190
 
    if(strncmp(*e, def, namelen+1) == 0){
 
188
    if(strncmp(*e, def, namelen + 1) == 0){
191
189
      /* It already exists */
192
190
      if(replace){
193
 
        char *new = realloc(*e, strlen(def));
 
191
        char *new = realloc(*e, strlen(def) + 1);
194
192
        if(new == NULL){
195
193
          return false;
196
194
        }
208
206
 * Descriptor Flags".
209
207
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
210
208
 */
211
 
static int set_cloexec_flag(int fd)
212
 
{
 
209
static int set_cloexec_flag(int fd){
213
210
  int ret = fcntl(fd, F_GETFD, 0);
214
211
  /* If reading the flags failed, return error indication now. */
215
212
  if(ret < 0){
222
219
 
223
220
/* Mark processes as completed when they exit, and save their exit
224
221
   status. */
225
 
void handle_sigchld(__attribute__((unused)) int sig){
 
222
static void handle_sigchld(__attribute__((unused)) int sig){
226
223
  while(true){
227
224
    plugin *proc = plugin_list;
228
225
    int status;
238
235
      /* No child processes */
239
236
      break;
240
237
    }
241
 
 
 
238
    
242
239
    /* A child exited, find it in process_list */
243
240
    while(proc != NULL and proc->pid != pid){
244
241
      proc = proc->next;
253
250
}
254
251
 
255
252
/* Prints out a password to stdout */
256
 
bool print_out_password(const char *buffer, size_t length){
 
253
static bool print_out_password(const char *buffer, size_t length){
257
254
  ssize_t ret;
258
 
  if(length>0 and buffer[length-1] == '\n'){
259
 
    length--;
260
 
  }
261
255
  for(size_t written = 0; written < length; written += (size_t)ret){
262
256
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
263
257
                                   length - written));
397
391
      if(arg == NULL){
398
392
        break;
399
393
      }
400
 
      {
401
 
        char *envdef = strdup(arg);
402
 
        if(envdef == NULL){
403
 
          break;
404
 
        }
405
 
        if(not add_environment(getplugin(NULL), envdef, true)){
406
 
          perror("add_environment");
407
 
        }
 
394
      if(not add_environment(getplugin(NULL), arg, true)){
 
395
        perror("add_environment");
408
396
      }
409
397
      break;
410
398
    case 'o':                   /* --options-for */
411
399
      if (arg != NULL){
412
400
        char *p_name = strsep(&arg, ":");
413
 
        if(p_name[0] == '\0'){
 
401
        if(p_name[0] == '\0' or arg == NULL){
414
402
          break;
415
403
        }
416
404
        char *opt = strsep(&arg, ":");
417
 
        if(opt[0] == '\0'){
 
405
        if(opt[0] == '\0' or opt == NULL){
418
406
          break;
419
407
        }
420
 
        if(opt != NULL){
421
 
          char *p;
422
 
          while((p = strsep(&opt, ",")) != NULL){
423
 
            if(p[0] == '\0'){
424
 
              continue;
425
 
            }
426
 
            if(not add_argument(getplugin(p_name), p)){
427
 
              perror("add_argument");
428
 
              return ARGP_ERR_UNKNOWN;
429
 
            }
 
408
        char *p;
 
409
        while((p = strsep(&opt, ",")) != NULL){
 
410
          if(p[0] == '\0'){
 
411
            continue;
 
412
          }
 
413
          if(not add_argument(getplugin(p_name), p)){
 
414
            perror("add_argument");
 
415
            return ARGP_ERR_UNKNOWN;
430
416
          }
431
417
        }
432
418
      }
440
426
        if(envdef == NULL){
441
427
          break;
442
428
        }
443
 
        char *p_name = strndup(arg, (size_t) (envdef-arg));
444
 
        if(p_name == NULL){
445
 
          break;
446
 
        }
447
 
        envdef++;
448
 
        if(not add_environment(getplugin(p_name), envdef, true)){
 
429
        *envdef = '\0';
 
430
        if(not add_environment(getplugin(arg), envdef+1, true)){
449
431
          perror("add_environment");
450
432
        }
451
433
      }
469
451
      }
470
452
      break;
471
453
    case 128:                   /* --plugin-dir */
 
454
      free(plugindir);
472
455
      plugindir = strdup(arg);
473
456
      if(plugindir == NULL){
474
457
        perror("strdup");
487
470
      debug = true;
488
471
      break;
489
472
    case ARGP_KEY_ARG:
490
 
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
473
      /* Cryptsetup always passes an argument, which is an empty
 
474
         string if "none" was specified in /etc/crypttab.  So if
 
475
         argument was empty, we ignore it silently. */
 
476
      if(arg[0] != '\0'){
 
477
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
 
478
      }
491
479
      break;
492
480
    case ARGP_KEY_END:
493
481
      break;
512
500
    case 128:                   /* --plugin-dir */
513
501
      break;
514
502
    case 129:                   /* --config-file */
 
503
      free(argfile);
515
504
      argfile = strdup(arg);
516
505
      if(argfile == NULL){
517
506
        perror("strdup");
747
736
    }
748
737
 
749
738
    char *filename;
750
 
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
739
    if(plugindir == NULL){
 
740
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
 
741
    } else {
 
742
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
 
743
    }
751
744
    if(ret < 0){
752
745
      perror("asprintf");
753
746
      continue;
942
935
    }
943
936
    /* OK, now either a process completed, or something can be read
944
937
       from one of them */
945
 
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
 
938
    for(plugin *proc = plugin_list; proc != NULL;){
946
939
      /* Is this process completely done? */
947
940
      if(proc->eof and proc->completed){
948
941
        /* Only accept the plugin output if it exited cleanly */
975
968
            exitstatus = EXIT_FAILURE;
976
969
            goto fallback;
977
970
          }
978
 
          free_plugin(proc);
 
971
          
979
972
          /* We are done modifying process list, so unblock signal */
980
973
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
981
974
                             NULL);
988
981
          if(plugin_list == NULL){
989
982
            break;
990
983
          }
 
984
          
 
985
          plugin *next_plugin = proc->next;
 
986
          free_plugin(proc);
 
987
          proc = next_plugin;
991
988
          continue;
992
989
        }
993
990
        
1005
1002
      /* This process has not completed.  Does it have any output? */
1006
1003
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1007
1004
        /* This process had nothing to say at this time */
 
1005
        proc = proc->next;
1008
1006
        continue;
1009
1007
      }
1010
1008
      /* Before reading, make the process' data buffer large enough */
1023
1021
                 BUFFER_SIZE);
1024
1022
      if(ret < 0){
1025
1023
        /* Read error from this process; ignore the error */
 
1024
        proc = proc->next;
1026
1025
        continue;
1027
1026
      }
1028
1027
      if(ret == 0){
1043
1042
    bool bret;
1044
1043
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1045
1044
    char *passwordbuffer = getpass("Password: ");
1046
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
1045
    size_t len = strlen(passwordbuffer);
 
1046
    /* Strip trailing newline */
 
1047
    if(len > 0 and passwordbuffer[len-1] == '\n'){
 
1048
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
 
1049
      len--;
 
1050
    }
 
1051
    bret = print_out_password(passwordbuffer, len);
1047
1052
    if(not bret){
1048
1053
      perror("print_out_password");
1049
1054
      exitstatus = EXIT_FAILURE;