/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-24 06:17:02 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080824061702-zxrru4r1vxmx4tuq
* Makefile (PREFIX, CONFDIR, MANDIR): Use $(DESTDIR).
  (install-server, install-client): Use "install --directory" instead
                                    of mkdir.

* mandos-keygen: New options --subtype and --sublength.
  (trap): Added semicolons and backslashes.
  (gpg): Added "--enable-dsa2" to all invocations.

* mandos-keygen.xml: Changed single quotes to double quotes for
                     consistency.
  (/refentry/refentryinfo/copyright) Split copyright holders.
  (SYNOPSIS): Added "--subtype", "--sublength", "-s", and "-L".
  (OPTIONS): Document the subtype and sublength options.
  (SECURITY): Also note the "--subtype" and "--sublength" options.

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){
264
247
    return NULL;
265
248
  }
266
249
  argv[*argc-1] = arg;
267
 
  argv[*argc] = NULL;
 
250
  argv[*argc] = NULL;   
268
251
  return argv;
269
252
}
270
253
 
271
254
static void free_plugin_list(plugin *plugin_list){
272
 
  for(plugin *next; plugin_list != NULL; plugin_list = next){
 
255
  for(plugin *next = plugin_list; plugin_list != NULL; plugin_list = next){
273
256
    next = plugin_list->next;
 
257
    free(plugin_list->name);
274
258
    for(char **arg = plugin_list->argv; *arg != NULL; arg++){
275
259
      free(*arg);
276
 
    }
 
260
    }    
277
261
    free(plugin_list->argv);
278
262
    for(char **env = plugin_list->environ; *env != NULL; env++){
279
263
      free(*env);
280
264
    }
281
265
    free(plugin_list->environ);
282
266
    free(plugin_list);
283
 
  }
 
267
  }  
284
268
}
285
269
 
286
270
int main(int argc, char *argv[]){
314
298
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
315
299
  if(ret == -1){
316
300
    perror("sigaction");
317
 
    exitstatus = EXIT_FAILURE;
 
301
    exitstatus = EXIT_FAILURE;    
318
302
    goto fallback;
319
303
  }
320
304
  
696
680
      }
697
681
    }
698
682
    
699
 
    int pipefd[2];
 
683
    int pipefd[2]; 
700
684
    ret = pipe(pipefd);
701
685
    if (ret == -1){
702
686
      perror("pipe");
803
787
  }
804
788
  
805
789
  free_plugin_list(plugin_list);
806
 
  plugin_list = NULL;
807
790
  
808
791
  closedir(dir);
809
792
  dir = NULL;
847
830
          /* Remove the plugin */
848
831
          FD_CLR(proc->fd, &rfds_all);
849
832
          /* Block signal while modifying process_list */
850
 
          ret = sigprocmask(SIG_BLOCK, &sigchld_action.sa_mask, NULL);
 
833
          ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
851
834
          if(ret < 0){
852
835
            perror("sigprocmask");
853
836
            exitstatus = EXIT_FAILURE;
881
864
        }
882
865
        /* This process exited nicely, so print its buffer */
883
866
 
884
 
        bool bret = print_out_password(proc->buffer,
885
 
                                       proc->buffer_length);
 
867
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
886
868
        if(not bret){
887
869
          perror("print_out_password");
888
870
          exitstatus = EXIT_FAILURE;
925
907
 fallback:
926
908
  
927
909
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
928
 
    /* Fallback if all plugins failed, none are found or an error
929
 
       occured */
 
910
    /* Fallback if all plugins failed, none are found or an error occured */
930
911
    bool bret;
931
912
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
932
913
    char *passwordbuffer = getpass("Password: ");