int main(int argc, char **argv) { int ch; char conf_path[MAXPATHLEN]; int conf=0; st_cn_t conn; conn = st_connection_alloc(); conn->st_config = conf_create_with_defaults(); (void)strncpy(conf_path, STUNEL_CONFIG, MAXPATHLEN-1); /* Parse command line args */ /* TODO: Add options for port, destination ip, key and user so using conf file is not necessary*/ while ((ch = getopt(argc, argv, "hqvDAf:p:d:i:u:")) != -1) { switch (ch) { case 'A': conf_set_authtype(conn->st_config, STUNEL_AUTH_AGENT); break; case 'f': if (argc == 3) { memset(conf_path, 0, MAXPATHLEN); (void)strncpy(conf_path, optarg, MAXPATHLEN-1); conf_path[MAXPATHLEN - 1]='\0'; conf=1; } break; case 'p': conf_set_port(conn->st_config, atoi(optarg)); break; case 'd': conf_set_address(conn->st_config, optarg); break; case 'i': conf_set_sshkey(conn->st_config, optarg); break; case 'u': conf_set_login(conn->st_config, optarg); break; case 'q': conf_set_log_level(conn->st_config, STUNEL_NORMAL); break; case 'v': conf_set_log_level(conn->st_config, STUNEL_VERBOSE); break; case 'D': conf_set_log_level(conn->st_config, STUNEL_DEBUG); break; case '?': case 'h': default: usage(); } } argc -= optind; argv += optind; if ( conf ) { printf("Reading configuration from file: %s\n", conf_path); conf_destroy(conn->st_config); conn->st_config = conf_get_file(conf_path); } printf("Config file: \n%s\n", conf_dump(conn->st_config)); if (conf_check(conn->st_config)) { st_connection_destroy(conn); exit(1); } printf("User is set to %s, port to %d\n", conf_get_login(conn->st_config), conf_get_port(conn->st_config)); printf("Connecting with libssh\n"); st_ssh_connect(conn); st_connection_destroy(conn); return 0; }
int main(int argc, char **argv) { int conf_err = 0; int option; int dflag = 0; conf_ruleset_s rset; // Set the default logger to syslog log_syslog(&logger); // Initialize the config and its rulesets conf_init(&conf); conf_rset_init(&rset); // Set the real path conf_set_realpath(&conf, argv[0]); conf_rset_add(&rset, conf_rule_create("root = %s", &conf.document_root)); conf_rset_add(&rset, conf_rule_create("handler = %s", &conf.method)); conf_rset_add(&rset, conf_rule_create("port = %hu", &conf.port)); conf_rset_add(&rset, conf_rule_create("backlog = %d", &conf.backlog)); conf_rset_add(&rset, conf_rule_create("mime = %s", &conf.mime)); conf_rset_add(&rset, conf_rule_create("logfile = %s", &conf.logfile)); if (conf_read(conf.real_path, ".lab3-config", rset) == -1) exit_fatal("The configuration file could not be read"); while((option = getopt(argc, argv, "hp:dl:s:")) != -1) { switch(option) { case 'p': conf_set_port(&conf, optarg); break; case 'd': dflag = 1; break; case 'l': conf_set_logfile(&conf, optarg); if (strncmp(optarg, "stdout", 6) == 0) log_stdout(&logger); else log_file(&logger); break; case 's': conf_set_method(&conf, optarg); break; case 'h': default: usage(argv[0]); exit(EXIT_SUCCESS); } } // Register signal handler for SIGINT signal(SIGINT, sigint_handler); if ((conf_err = conf_validate(conf)) < 0) conf_print_err(conf_err); // Daemonize the process if the dflag is set if (dflag) { log_syslog(&logger); daemonize(); } else { jail_proc(dflag); } server_create(); return 0; }