/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-08-23 07:17:28 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080823071728-wo1k2mtbje5910np
* Makefile (MANPOST): Bug fix: corrected patterns.

* mandos-options.xml: Bug fix: also change root node tag name in
                      DOCTYPE declaration.
  (interface, address): Improved wording.

* overview.xml: Changed "host computer" to "computer".

Show diffs side-by-side

added added

removed removed

Lines of Context:
56
56
#include <argp.h>               /* struct argp_option, struct
57
57
                                   argp_state, struct argp,
58
58
                                   argp_parse(), ARGP_ERR_UNKNOWN,
59
 
                                   ARGP_KEY_END, ARGP_KEY_ARG,
60
 
                                   error_t */
 
59
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
61
60
#include <signal.h>             /* struct sigaction, sigemptyset(),
62
61
                                   sigaddset(), sigaction(),
63
62
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
79
78
  size_t buffer_size;
80
79
  size_t buffer_length;
81
80
  bool eof;
82
 
  volatile bool completed;
83
 
  volatile int status;
 
81
  bool completed;
 
82
  int status;
84
83
  struct process *next;
85
84
} process;
86
85
 
106
105
  if (new_plugin == NULL){
107
106
    return NULL;
108
107
  }
109
 
  char *copy_name = NULL;
110
 
  if(name != NULL){
111
 
    copy_name = strdup(name);
112
 
    if(copy_name == NULL){
113
 
      return NULL;
114
 
    }
 
108
  char *copy_name = strdup(name);
 
109
  if(copy_name == NULL){
 
110
    return NULL;
115
111
  }
116
112
  
117
113
  *new_plugin = (plugin) { .name = copy_name,
122
118
  
123
119
  new_plugin->argv = malloc(sizeof(char *) * 2);
124
120
  if (new_plugin->argv == NULL){
125
 
    free(copy_name);
126
121
    free(new_plugin);
127
122
    return NULL;
128
123
  }
131
126
 
132
127
  new_plugin->environ = malloc(sizeof(char *));
133
128
  if(new_plugin->environ == NULL){
134
 
    free(copy_name);
135
129
    free(new_plugin->argv);
136
130
    free(new_plugin);
137
131
    return NULL;
200
194
 
201
195
process *process_list = NULL;
202
196
 
203
 
/* Mark processes as completed when they exit, and save their exit
 
197
/* Mark a process as completed when it exits, and save its exit
204
198
   status. */
205
199
void handle_sigchld(__attribute__((unused)) int sig){
206
 
  while(true){
207
 
    process *proc = process_list;
208
 
    int status;
209
 
    pid_t pid = waitpid(-1, &status, WNOHANG);
210
 
    if(pid == 0){
211
 
      /* Only still running child processes */
212
 
      break;
213
 
    }
214
 
    if(pid == -1){
215
 
      if (errno != ECHILD){
216
 
        perror("waitpid");
217
 
      }
218
 
      /* No child processes */
219
 
      break;
220
 
    }
221
 
 
222
 
    /* A child exited, find it in process_list */
223
 
    while(proc != NULL and proc->pid != pid){
224
 
      proc = proc->next;
225
 
    }
226
 
    if(proc == NULL){
227
 
      /* Process not found in process list */
228
 
      continue;
229
 
    }
230
 
    proc->status = status;
231
 
    proc->completed = true;
232
 
  }
 
200
  process *proc = process_list;
 
201
  int status;
 
202
  pid_t pid = wait(&status);
 
203
  if(pid == -1){
 
204
    perror("wait");
 
205
    return;
 
206
  }
 
207
  while(proc != NULL and proc->pid != pid){
 
208
    proc = proc->next;
 
209
  }
 
210
  if(proc == NULL){
 
211
    /* Process not found in process list */
 
212
    return;
 
213
  }
 
214
  proc->status = status;
 
215
  proc->completed = true;
233
216
}
234
217
 
235
218
bool print_out_password(const char *buffer, size_t length){
254
237
    if(argv == NULL){
255
238
      return NULL;
256
239
    }
257
 
    argv[0] = NULL;     /* Will be set to argv[0] in main before
258
 
                           parsing */
 
240
    argv[0] = NULL;     /* Will be set to argv[0] in main before parsing */
259
241
    argv[1] = NULL;
260
242
  }
261
243
  *argc += 1;
265
247
    return NULL;
266
248
  }
267
249
  argv[*argc-1] = arg;
268
 
  argv[*argc] = NULL;
 
250
  argv[*argc] = NULL;   
269
251
  return argv;
270
252
}
271
253
 
272
254
static void free_plugin_list(plugin *plugin_list){
273
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
255
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
274
256
    next = plugin_list->next;
 
257
    free(plugin_list->name);
275
258
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
276
259
      free(*arg);
277
 
    }
 
260
    }    
278
261
    free(plugin_list->argv);
279
262
    for(char **env = plugin_list->environ; *env != NULL; env++){
280
263
      free(*env);
281
264
    }
282
265
    free(plugin_list->environ);
283
266
    free(plugin_list);
284
 
  }
 
267
  }  
285
268
}
286
269
 
287
270
int main(int argc, char *argv[]){
315
298
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
316
299
  if(ret == -1){
317
300
    perror("sigaction");
318
 
    exitstatus = EXIT_FAILURE;
 
301
    exitstatus = EXIT_FAILURE;    
319
302
    goto fallback;
320
303
  }
321
304
  
514
497
 
515
498
  if(custom_argv != NULL){
516
499
    custom_argv[0] = argv[0];
517
 
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0,
518
 
                      &plugin_list);
 
500
    ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
519
501
    if (ret == ARGP_ERR_UNKNOWN){
520
502
      fprintf(stderr, "Unknown error while parsing arguments\n");
521
503
      exitstatus = EXIT_FAILURE;
698
680
      }
699
681
    }
700
682
    
701
 
    int pipefd[2];
 
683
    int pipefd[2]; 
702
684
    ret = pipe(pipefd);
703
685
    if (ret == -1){
704
686
      perror("pipe");
805
787
  }
806
788
  
807
789
  free_plugin_list(plugin_list);
808
 
  plugin_list = NULL;
809
790
  
810
791
  closedir(dir);
811
792
  dir = NULL;
849
830
          /* Remove the plugin */
850
831
          FD_CLR(proc->fd, &rfds_all);
851
832
          /* Block signal while modifying process_list */
852
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
833
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
853
834
          if(ret < 0){
854
835
            perror("sigprocmask");
855
836
            exitstatus = EXIT_FAILURE;
883
864
        }
884
865
        /* This process exited nicely, so print its buffer */
885
866
 
886
 
        bool bret = print_out_password(proc->buffer,
887
 
                                       proc->buffer_length);
 
867
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
888
868
        if(not bret){
889
869
          perror("print_out_password");
890
870
          exitstatus = EXIT_FAILURE;
927
907
 fallback:
928
908
  
929
909
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
930
 
    /* Fallback if all plugins failed, none are found or an error
931
 
       occured */
 
910
    /* Fallback if all plugins failed, none are found or an error occured */
932
911
    bool bret;
933
912
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
934
913
    char *passwordbuffer = getpass("Password: ");