52
52
#include <fcntl.h> /* fcntl(), F_GETFD, F_SETFD,
54
#include <string.h> /* strtok, strlen(), strcpy(),
54
#include <string.h> /* strsep, strlen(), strcpy(),
56
56
#include <errno.h> /* errno */
57
57
#include <argp.h> /* struct argp_option, struct
170
171
proc->completed = true;
174
bool print_out_password(const char *buffer, size_t length){
176
if(length>0 and buffer[length-1] == '\n'){
179
for(size_t written = 0; written < length; written += (size_t)ret){
180
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
189
char ** addcustomargument(char **argv, int *argc, char *arg){
193
argv = malloc(sizeof(char*) * 2);
197
argv[0] = NULL; /* Will be set to argv[0] in main before parsing */
201
argv = realloc(argv, sizeof(char *)
202
* ((unsigned int) *argc + 1));
173
211
int main(int argc, char *argv[]){
174
212
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
213
const char *conffile = CONFFILE;
175
215
size_t d_name_len;
177
217
struct dirent *dirst;
185
225
struct sigaction old_sigchld_action;
186
226
struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
187
227
.sa_flags = SA_NOCLDSTOP };
188
char *plus_options = NULL;
189
char **plus_argv = NULL;
228
char **custom_argv = NULL;
191
231
/* Establish a signal handler */
192
232
sigemptyset(&sigchld_action.sa_mask);
235
275
if (arg != NULL){
236
char *p = strtok(arg, ",");
277
while((p = strsep(&arg, ",")) != NULL){
238
281
addargument(getplugin(NULL, plugins), p);
239
p = strtok(NULL, ",");
244
286
if (arg != NULL){
245
char *name = strtok(arg, ":");
246
char *p = strtok(NULL, ":");
287
char *name = strsep(&arg, ":");
291
char *opt = strsep(&arg, ":");
297
while((p = strsep(&opt, ",")) != NULL){
250
301
addargument(getplugin(name, plugins), p);
251
p = strtok(NULL, ",");
293
340
ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
294
341
if (ret == ARGP_ERR_UNKNOWN){
295
perror("argp_parse");
342
fprintf(stderr, "Unknown error while parsing arguments\n");
296
343
exitstatus = EXIT_FAILURE;
301
/* This is a mangled argument in the form of
302
"+--option+--other-option=parameter+--yet-another-option", etc */
303
/* Make new argc and argv vars, and call argp_parse() again. */
304
plus_options++; /* skip the first '+' character */
305
const char delims[] = "+";
308
plus_argv = malloc(sizeof(char*) * 2);
309
if(plus_argv == NULL){
347
conffp = fopen(conffile, "r");
349
char *org_line = NULL;
352
char *p, *arg, *new_arg, *line;
353
const char whitespace_delims[] = " \r\t\f\v\n";
354
const char comment_delim[] = "#";
357
sret = getline(&org_line, &size, conffp);
363
arg = strsep(&line, comment_delim);
364
while((p = strsep(&arg, whitespace_delims)) != NULL){
369
custom_argv = addcustomargument(custom_argv, &custom_argc, new_arg);
370
if (custom_argv == NULL){
371
perror("addcustomargument");
372
exitstatus = EXIT_FAILURE;
379
/* check for harmfull errors */
380
if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
311
382
exitstatus = EXIT_FAILURE;
314
plus_argv[0] = argv[0];
316
arg = strtok(plus_options, delims); /* Get first argument */
319
plus_argv = realloc(plus_argv, sizeof(char *)
320
* ((unsigned int) new_argc + 1));
321
if(plus_argv == NULL){
323
exitstatus = EXIT_FAILURE;
326
plus_argv[new_argc-1] = arg;
327
plus_argv[new_argc] = NULL;
328
arg = strtok(NULL, delims); /* Get next argument */
330
ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
387
if(custom_argv != NULL){
388
custom_argv[0] = argv[0];
389
ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
331
390
if (ret == ARGP_ERR_UNKNOWN){
332
perror("argp_parse");
391
fprintf(stderr, "Unknown error while parsing arguments\n");
333
392
exitstatus = EXIT_FAILURE;
606
669
/* Bad exit by plugin */
608
671
if(WIFEXITED(proc->status)){
609
fprintf(stderr, "Plugin %d exited with status %d\n",
610
proc->pid, WEXITSTATUS(proc->status));
672
fprintf(stderr, "Plugin %u exited with status %d\n",
673
(unsigned int) (proc->pid),
674
WEXITSTATUS(proc->status));
611
675
} else if(WIFSIGNALED(proc->status)) {
612
fprintf(stderr, "Plugin %d killed by signal %d\n",
613
proc->pid, WTERMSIG(proc->status));
676
fprintf(stderr, "Plugin %u killed by signal %d\n",
677
(unsigned int) (proc->pid),
678
WTERMSIG(proc->status));
614
679
} else if(WCOREDUMP(proc->status)){
615
fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
680
fprintf(stderr, "Plugin %d dumped core\n",
681
(unsigned int) (proc->pid));
618
684
/* Remove the plugin */
653
719
/* 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,
662
exitstatus = EXIT_FAILURE;
721
bool bret = print_out_password(proc->buffer, proc->buffer_length);
723
perror("print_out_password");
724
exitstatus = EXIT_FAILURE;
699
if(process_list == NULL){
700
fprintf(stderr, "All plugin processes failed, exiting\n");
701
exitstatus = EXIT_FAILURE;
763
if(process_list == NULL or exitstatus != EXIT_SUCCESS){
764
/* Fallback if all plugins failed, none are found or an error occured */
766
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
767
char *passwordbuffer = getpass("Password: ");
768
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
770
perror("print_out_password");
771
exitstatus = EXIT_FAILURE;
705
776
/* Restore old signal handler */
706
777
sigaction(SIGCHLD, &old_sigchld_action, NULL);
710
781
/* Free the plugin list */
711
782
for(plugin *next; plugin_list != NULL; plugin_list = next){