52
52
#include <fcntl.h> /* fcntl(), F_GETFD, F_SETFD,
54
#include <string.h> /* strsep, strlen(), strcpy(),
54
#include <string.h> /* strtok, strlen(), strcpy(),
56
56
#include <errno.h> /* errno */
57
57
#include <argp.h> /* struct argp_option, struct
171
170
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));
211
173
int main(int argc, char *argv[]){
212
const char *plugindir = "/lib/mandos/plugins.d";
213
const char *argfile = ARGFILE;
174
const char *plugindir = "/conf/conf.d/mandos/plugins.d";
215
175
size_t d_name_len;
217
177
struct dirent *dirst;
275
235
if (arg != NULL){
277
while((p = strsep(&arg, ",")) != NULL){
236
char *p = strtok(arg, ",");
281
238
addargument(getplugin(NULL, plugins), p);
239
p = strtok(NULL, ",");
286
244
if (arg != NULL){
287
char *name = strsep(&arg, ":");
291
char *opt = strsep(&arg, ":");
297
while((p = strsep(&opt, ",")) != NULL){
245
char *name = strtok(arg, ":");
246
char *p = strtok(NULL, ":");
301
250
addargument(getplugin(name, plugins), p);
251
p = strtok(NULL, ",");
340
293
ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
341
294
if (ret == ARGP_ERR_UNKNOWN){
342
fprintf(stderr, "Unknown error while parsing arguments\n");
295
fprintf(stderr, "Unkown error while parsing arguments\n");
343
296
exitstatus = EXIT_FAILURE;
347
conffp = fopen(argfile, "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 harmful errors and go to fallback. Other errors might
380
not affect opening plugins */
381
if (errno == EMFILE or errno == ENFILE or errno == ENOMEM){
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){
383
311
exitstatus = EXIT_FAILURE;
388
if(custom_argv != NULL){
389
custom_argv[0] = argv[0];
390
ret = argp_parse (&argp, custom_argc, custom_argv, 0, 0, &plugin_list);
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);
391
331
if (ret == ARGP_ERR_UNKNOWN){
392
fprintf(stderr, "Unknown error while parsing arguments\n");
332
fprintf(stderr, "Unkown error while parsing arguments\n");
393
333
exitstatus = EXIT_FAILURE;
720
656
/* This process exited nicely, so print its buffer */
722
bool bret = print_out_password(proc->buffer, proc->buffer_length);
724
perror("print_out_password");
725
exitstatus = EXIT_FAILURE;
657
for(size_t written = 0; written < proc->buffer_length;
658
written += (size_t)ret){
659
ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
660
proc->buffer + written,
665
exitstatus = EXIT_FAILURE;
702
if(process_list == NULL){
703
fprintf(stderr, "All plugin processes failed, exiting\n");
704
exitstatus = EXIT_FAILURE;
764
if(process_list == NULL or exitstatus != EXIT_SUCCESS){
765
/* Fallback if all plugins failed, none are found or an error occured */
767
fprintf(stderr, "Going to fallback mode using getpass(3)\n");
768
char *passwordbuffer = getpass("Password: ");
769
bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
771
perror("print_out_password");
772
exitstatus = EXIT_FAILURE;
777
708
/* Restore old signal handler */
778
709
sigaction(SIGCHLD, &old_sigchld_action, NULL);
782
713
/* Free the plugin list */
783
714
for(plugin *next; plugin_list != NULL; plugin_list = next){