/* * dropnroll * * Copyright (C) 2009-2015 Christian Pointner * * This file is part of dropnroll. * * dropnroll is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * dropnroll is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with dropnroll. If not, see . */ #include "datatypes.h" #include "options.h" #include #include #include #include #include #include "log.h" #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ VALUE = 1; #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ VALUE = 0; #define PARSE_INT_PARAM(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ if(argc < 1) \ return i; \ VALUE = atoi(argv[i+1]); \ argc--; \ i++; \ } #define PARSE_STRING_PARAM(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ if(argc < 1 || argv[i+1][0] == '-') \ return i; \ if(VALUE) free(VALUE); \ VALUE = strdup(argv[i+1]); \ if(!VALUE) \ return -2; \ argc--; \ i++; \ } #define PARSE_STRING_PARAM_SEC(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ if(argc < 1 || argv[i+1][0] == '-') \ return i; \ if(VALUE) free(VALUE); \ VALUE = strdup(argv[i+1]); \ if(!VALUE) \ return -2; \ size_t j; \ for(j=0; j < strlen(argv[i+1]); ++j) \ argv[i+1][j] = '#'; \ argc--; \ i++; \ } #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ if(argc < 1 || argv[i+1][0] == '-') \ return i; \ int ret; \ ret = options_parse_hex_string(argv[i+1], &VALUE); \ if(ret > 0) \ return i+1; \ else if(ret < 0) \ return ret; \ size_t j; \ for(j=0; j < strlen(argv[i+1]); ++j) \ argv[i+1][j] = '#'; \ argc--; \ i++; \ } #define PARSE_STRING_LIST(SHORT, LONG, LIST) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ if(argc < 1 || argv[i+1][0] == '-') \ return i; \ int ret = string_list_add(&LIST, argv[i+1]); \ if(ret == -2) \ return ret; \ else if(ret) \ return i+1; \ argc--; \ i++; \ } int options_parse_hex_string(const char* hex, buffer_t* buffer) { if(!hex || !buffer) return -1; u_int32_t hex_len = strlen(hex); if(hex_len%2) return 1; if(buffer->buf_) free(buffer->buf_); buffer->length_ = hex_len/2; buffer->buf_ = malloc(buffer->length_); if(!buffer->buf_) { buffer->length_ = 0; return -2; } const char* ptr = hex; int i; for(i=0;ilength_;++i) { u_int32_t tmp; sscanf(ptr, "%2X", &tmp); buffer->buf_[i] = (u_int8_t)tmp; ptr += 2; } return 0; } int options_parse(options_t* opt, int argc, char* argv[]) { if(!opt) return -1; options_default(opt); if(opt->progname_) free(opt->progname_); opt->progname_ = strdup(argv[0]); if(!opt->progname_) return -2; argc--; char* children_policy = NULL; int i; for(i=1; argc > 0; ++i) { char* str = argv[i]; argc--; if(!strcmp(str,"-h") || !strcmp(str,"--help")) return -1; PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", opt->daemonize_) PARSE_STRING_PARAM("-u","--username", opt->username_) PARSE_STRING_PARAM("-g","--groupname", opt->groupname_) PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_) PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_) PARSE_STRING_LIST("-L","--log", opt->log_targets_) PARSE_BOOL_PARAM("-U", "--debug", opt->debug_) PARSE_STRING_PARAM("-s","--command-sock", opt->command_sock_) PARSE_STRING_PARAM("-x","--script", opt->script_) PARSE_INT_PARAM("-m","--max-children", opt->max_children_) PARSE_STRING_PARAM("-p","--children-policy", children_policy) PARSE_INT_PARAM("-r","--children-min-runtime", opt->min_child_runtime_) PARSE_STRING_LIST("-d","--dir", opt->dirs_) else return i; } if(opt->debug_) { string_list_add(&opt->log_targets_, "stdout:5"); opt->daemonize_ = 0; } if(children_policy) { if(!strcmp(children_policy, "defer")) opt->children_policy_ = DEFER; else if(!strcmp(children_policy, "drop")) opt->children_policy_ = DROP; else if(!strcmp(children_policy, "kill-oldest")) opt->children_policy_ = KILL_OLDEST; else return -3; } if(!opt->log_targets_.first_) string_list_add(&opt->log_targets_, "syslog:3,dropnroll,daemon"); if(!opt->dirs_.first_ && !opt->command_sock_) opt->command_sock_ = strdup("/var/run/dropnroll/cmd.sock"); return 0; } int options_parse_post(options_t* opt) { if(!opt) return -1; if(opt->max_children_ < 0) { log_printf(ERROR, "-m|--max-children invalid value %d", opt->max_children_); return 1; } if(opt->max_children_ == 0) log_printf(WARNING, "disabling child limit, this may be harmful to your system"); return 0; } void options_default(options_t* opt) { if(!opt) return; opt->progname_ = strdup("dropnroll"); opt->daemonize_ = 1; opt->username_ = NULL; opt->groupname_ = NULL; opt->chroot_dir_ = NULL; opt->pid_file_ = NULL; string_list_init(&opt->log_targets_); opt->debug_ = 0; opt->command_sock_ = NULL; opt->script_ = strdup("newfile.sh"); opt->max_children_ = 8; opt->children_policy_ = DEFER; opt->min_child_runtime_ = -1; string_list_init(&opt->dirs_); } void options_clear(options_t* opt) { if(!opt) return; if(opt->progname_) free(opt->progname_); if(opt->username_) free(opt->username_); if(opt->groupname_) free(opt->groupname_); if(opt->chroot_dir_) free(opt->chroot_dir_); if(opt->pid_file_) free(opt->pid_file_); string_list_clear(&opt->log_targets_); if(opt->command_sock_) free(opt->command_sock_); if(opt->script_) free(opt->script_); string_list_clear(&opt->dirs_); } void options_print_usage() { printf("USAGE:\n"); printf("dropnroll\n"); printf(" [-h|--help] prints this...\n"); printf(" [-D|--nodaemonize] don't run in background\n"); printf(" [-u|--username] change to this user\n"); printf(" [-g|--groupname] change to this group\n"); printf(" [-C|--chroot] chroot to this directory\n"); printf(" [-P|--write-pid] write pid to this file\n"); printf(" [-L|--log] :[,[,..]]\n"); printf(" add a log target, can be invoked several times\n"); printf(" [-U|--debug] don't daemonize and log to stdout with maximum log level\n"); printf(" [-s|--command-sock] the command socket e.g. /var/run/dropnroll/cmd.sock\n"); printf(" [-x|--script]