From 39fd8ec76a516b503ddd499931f5905f7993cd34 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 30 Apr 2009 20:28:54 +0000 Subject: [PATCH] enabled options parser at new c-door_daemon --- door_daemon/Makefile | 3 +- door_daemon/door_daemon.c | 185 +++++++++++++------------- door_daemon/options.c | 315 +++++---------------------------------------- door_daemon/options.h | 64 ++------- 4 files changed, 133 insertions(+), 434 deletions(-) diff --git a/door_daemon/Makefile b/door_daemon/Makefile index adf4bfd..5198385 100644 --- a/door_daemon/Makefile +++ b/door_daemon/Makefile @@ -26,6 +26,8 @@ EXECUTABLE := door_daemon OBJ := log.o \ sig_handler.o \ + options.o \ + string_list.o \ door_daemon.o @@ -56,7 +58,6 @@ distclean: clean find . -name *.o -exec rm -f {} \; find . -name "*.\~*" -exec rm -rf {} \; rm -f include.mk - rm -f tun.c clean: rm -f *.o diff --git a/door_daemon/door_daemon.c b/door_daemon/door_daemon.c index b88aa00..4ad308c 100644 --- a/door_daemon/door_daemon.c +++ b/door_daemon/door_daemon.c @@ -29,7 +29,7 @@ #include "log.h" #include "sig_handler.h" -//#include "options.h" +#include "options.h" #include "daemon.h" #include "sysexec.h" @@ -56,6 +56,7 @@ int process_ttyusb(int ttyusb_fd) int ret = 0; do { ret = read(ttyusb_fd, buffer, sizeof(buffer)); + if(!ret) return -1; } while (ret == -1 && errno == EINTR); return 0; @@ -133,69 +134,61 @@ int main_loop(int ttyusb_fd, int cmd_listen_fd) int main(int argc, char* argv[]) { log_init(); - int ret = 0; -/* options_t opt; */ -/* int ret = options_parse(&opt, argc, argv); */ -/* if(ret) { */ -/* if(ret > 0) { */ -/* fprintf(stderr, "syntax error near: %s\n\n", argv[ret]); */ -/* } */ -/* if(ret == -2) { */ -/* fprintf(stderr, "memory error on options_parse, exitting\n"); */ -/* } */ -/* if(ret == -3) { */ -/* fprintf(stderr, "syntax error: -4 and -6 are mutual exclusive\n\n"); */ -/* } */ -/* if(ret == -4) { */ -/* fprintf(stderr, "syntax error: unknown role name\n\n"); */ -/* } */ - -/* if(ret != -2) */ -/* options_print_usage(); */ - -/* options_clear(&opt); */ -/* log_close(); */ -/* exit(ret); */ -/* } */ -/* string_list_element_t* tmp = opt.log_targets_.first_; */ -/* if(!tmp) { */ -/* log_add_target("syslog:3,door_daemon,daemon"); */ -/* } */ -/* else { */ -/* while(tmp) { */ -/* ret = log_add_target(tmp->string_); */ -/* if(ret) { */ -/* switch(ret) { */ -/* case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break; */ -/* case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", tmp->string_); break; */ -/* case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break; */ -/* default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break; */ -/* } */ + options_t opt; + int ret = options_parse(&opt, argc, argv); + if(ret) { + if(ret > 0) { + fprintf(stderr, "syntax error near: %s\n\n", argv[ret]); + } + if(ret == -2) { + fprintf(stderr, "memory error on options_parse, exitting\n"); + } + + if(ret != -2) + options_print_usage(); + + options_clear(&opt); + log_close(); + exit(ret); + } + string_list_element_t* tmp = opt.log_targets_.first_; + if(!tmp) { + log_add_target("syslog:3,door_daemon,daemon"); + } + else { + while(tmp) { + ret = log_add_target(tmp->string_); + if(ret) { + switch(ret) { + case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break; + case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", tmp->string_); break; + case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break; + default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break; + } -/* options_clear(&opt); */ -/* log_close(); */ -/* exit(ret); */ -/* } */ -/* tmp = tmp->next_; */ -/* } */ -/* } */ - log_add_target("stdout:5"); + options_clear(&opt); + log_close(); + exit(ret); + } + tmp = tmp->next_; + } + } log_printf(NOTICE, "just started..."); -/* options_parse_post(&opt); */ - -/* priv_info_t priv; */ -/* if(opt.username_) */ -/* if(priv_init(&priv, opt.username_, opt.groupname_)) { */ -/* options_clear(&opt); */ -/* log_close(); */ -/* exit(-1); */ -/* } */ + options_parse_post(&opt); + + priv_info_t priv; + if(opt.username_) + if(priv_init(&priv, opt.username_, opt.groupname_)) { + options_clear(&opt); + log_close(); + exit(-1); + } - int ttyusb_fd = open("/dev/ttyUSB0", O_RDWR); + int ttyusb_fd = open(opt.ttyusb_dev_, O_RDWR); if(ttyusb_fd < 0) { - log_printf(ERROR, "unable to open /dev/ttyUSB0: %s", strerror(errno)); -/* options_clear(&opt); */ + log_printf(ERROR, "unable to open %s: %s", opt.ttyusb_dev_, strerror(errno)); + options_clear(&opt); log_close(); exit(-1); } @@ -206,12 +199,12 @@ int main(int argc, char* argv[]) if(cmd_listen_fd < 0) { log_printf(ERROR, "unable to open socket: %s", strerror(errno)); close(ttyusb_fd); -/* options_clear(&opt); */ + options_clear(&opt); log_close(); exit(-1); } local.sun_family = AF_UNIX; - strcpy(local.sun_path, "sock"); + strcpy(local.sun_path, opt.command_sock_); // TODO: strlen ??? unlink(local.sun_path); len = SUN_LEN(&local); ret = bind(cmd_listen_fd, (struct sockaddr*)&local, len); @@ -219,7 +212,7 @@ int main(int argc, char* argv[]) log_printf(ERROR, "unable to bind to '%s': %s", local.sun_path, strerror(errno)); close(cmd_listen_fd); close(ttyusb_fd); -/* options_clear(&opt); */ + options_clear(&opt); log_close(); exit(-1); } @@ -229,49 +222,49 @@ int main(int argc, char* argv[]) log_printf(ERROR, "unable to listen on command socket: %s", local.sun_path, strerror(errno)); close(cmd_listen_fd); close(ttyusb_fd); -/* options_clear(&opt); */ + options_clear(&opt); log_close(); exit(-1); } -/* FILE* pid_file = NULL; */ -/* if(opt.pid_file_) { */ -/* pid_file = fopen(opt.pid_file_, "w"); */ -/* if(!pid_file) { */ -/* log_printf(WARNING, "unable to open pid file: %s", strerror(errno)); */ -/* } */ -/* } */ - -/* if(opt.chroot_dir_) */ -/* if(do_chroot(opt.chroot_dir_)) { */ -/* options_clear(&opt); */ -/* log_close(); */ -/* exit(-1); */ -/* } */ -/* if(opt.username_) */ -/* if(priv_drop(&priv)) { */ -/* options_clear(&opt); */ -/* log_close(); */ -/* exit(-1); */ -/* } */ - -/* if(opt.daemonize_) { */ -/* pid_t oldpid = getpid(); */ -/* daemonize(); */ -/* log_printf(INFO, "running in background now (old pid: %d)", oldpid); */ -/* } */ - -/* if(pid_file) { */ -/* pid_t pid = getpid(); */ -/* fprintf(pid_file, "%d", pid); */ -/* fclose(pid_file); */ -/* } */ + FILE* pid_file = NULL; + if(opt.pid_file_) { + pid_file = fopen(opt.pid_file_, "w"); + if(!pid_file) { + log_printf(WARNING, "unable to open pid file: %s", strerror(errno)); + } + } + + if(opt.chroot_dir_) + if(do_chroot(opt.chroot_dir_)) { + options_clear(&opt); + log_close(); + exit(-1); + } + if(opt.username_) + if(priv_drop(&priv)) { + options_clear(&opt); + log_close(); + exit(-1); + } + + if(opt.daemonize_) { + pid_t oldpid = getpid(); + daemonize(); + log_printf(INFO, "running in background now (old pid: %d)", oldpid); + } + + if(pid_file) { + pid_t pid = getpid(); + fprintf(pid_file, "%d", pid); + fclose(pid_file); + } ret = main_loop(ttyusb_fd, cmd_listen_fd); close(cmd_listen_fd); close(ttyusb_fd); -/* options_clear(&opt); */ + options_clear(&opt); if(!ret) log_printf(NOTICE, "normal shutdown"); diff --git a/door_daemon/options.c b/door_daemon/options.c index 2300794..e67706e 100644 --- a/door_daemon/options.c +++ b/door_daemon/options.c @@ -1,35 +1,21 @@ /* - * uAnytun + * door_daemon * - * uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full - * featured implementation uAnytun has no support for multiple connections - * or synchronisation. It is a small single threaded implementation intended - * to act as a client on small platforms. - * The secure anycast tunneling protocol (satp) defines a protocol used - * for communication between any combination of unicast and anycast - * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel - * mode and allows tunneling of every ETHER TYPE protocol (e.g. - * ethernet, ip, arp ...). satp directly includes cryptography and - * message authentication based on the methodes used by SRTP. It is - * intended to deliver a generic, scaleable and secure solution for - * tunneling and relaying of packets of any protocol. - * + * Copyright (C) 2009 Christian Pointner * - * Copyright (C) 2007-2008 Christian Pointner + * This file is part of door_daemon. * - * This file is part of uAnytun. - * - * uAnytun is free software: you can redistribute it and/or modify + * door_daemon is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * - * uAnytun is distributed in the hope that it will be useful, + * door_daemon 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 uAnytun. If not, see . + * along with door_daemon. If not, see . */ #include "datatypes.h" @@ -43,10 +29,6 @@ #include "log.h" -#ifndef NO_CRYPT -#include "auth_algo.h" -#endif - #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ VALUE = 1; @@ -94,21 +76,6 @@ i++; \ } -#define PARSE_IFCONFIG_PARAM(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_ifconfig(argv[i+1], &VALUE); \ - if(ret > 0) \ - return i+1; \ - if(ret < 0) \ - return ret; \ - argc--; \ - i++; \ - } - #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE) \ else if(!strcmp(str,SHORT) || !strcmp(str,LONG)) \ { \ @@ -172,40 +139,6 @@ int options_parse_hex_string(const char* hex, buffer_t* buffer) return 0; } -int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg) -{ - char* str = strdup(arg); - if(!str) - return -2; - - char* ptr = str; - for(;*ptr;++ptr) { - if(*ptr == '/') { - *ptr = 0; - ptr++; - if(!(*ptr)) { - free(str); - return 1; - } - - ifcfg->prefix_length_ = atoi(ptr); - ifcfg->net_addr_ = strdup(str); - free(str); - - if(!ifcfg->net_addr_) - return -2; - - return 0; - } - if(!isdigit(*ptr) && *ptr != '.') { - free(str); - return 1; - } - } - - free(str); - return 1; -} int options_parse(options_t* opt, int argc, char* argv[]) @@ -223,8 +156,7 @@ int options_parse(options_t* opt, int argc, char* argv[]) argc--; - char* role = NULL; - int i, ipv4_only = 0, ipv6_only = 0; + int i; for(i=1; argc > 0; ++i) { char* str = argv[i]; @@ -237,53 +169,13 @@ int options_parse(options_t* opt, int argc, char* argv[]) 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_PARAM("-i","--interface", opt->local_addr_) - PARSE_STRING_PARAM("-p","--port", opt->local_port_) - PARSE_INT_PARAM("-s","--sender-id", opt->sender_id_) PARSE_STRING_LIST("-L","--log", opt->log_targets_) - PARSE_STRING_PARAM("-r","--remote-host", opt->remote_addr_) - PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_) - PARSE_BOOL_PARAM("-4","--ipv4-only", ipv4_only) - PARSE_BOOL_PARAM("-6","--ipv6-only", ipv6_only) - PARSE_STRING_PARAM("-d","--dev", opt->dev_name_) - PARSE_STRING_PARAM("-t","--type", opt->dev_type_) - PARSE_IFCONFIG_PARAM("-n","--ifconfig", opt->ifconfig_param_) - PARSE_STRING_PARAM("-x","--post-up-script", opt->post_up_script_) - PARSE_INT_PARAM("-m","--mux", opt->mux_) - PARSE_INT_PARAM("-w","--window-size", opt->seq_window_size_) -#ifndef NO_CRYPT - PARSE_STRING_PARAM("-k","--kd-prf", opt->kd_prf_) -#ifndef NO_PASSPHRASE - PARSE_STRING_PARAM_SEC("-E","--passphrase", opt->passphrase_) -#endif - PARSE_STRING_PARAM("-e","--role", role) - PARSE_HEXSTRING_PARAM_SEC("-K","--key", opt->key_) - PARSE_HEXSTRING_PARAM_SEC("-A","--salt", opt->salt_) - PARSE_STRING_PARAM("-c","--cipher", opt->cipher_) - PARSE_STRING_PARAM("-a","--auth-algo", opt->auth_algo_) - PARSE_INT_PARAM("-b","--auth-tag-length", opt->auth_tag_length_) -#endif + PARSE_STRING_PARAM("-d","--device", opt->ttyusb_dev_) + PARSE_STRING_PARAM("-s","--socket", opt->command_sock_) else return i; } - if(ipv4_only && ipv6_only) - return -3; - if(ipv4_only) - opt->resolv_addr_type_ = IPV4_ONLY; - if(ipv6_only) - opt->resolv_addr_type_ = IPV6_ONLY; - - if(role) { - if(!strcmp(role, "alice") || !strcmp(role, "server") || !strcmp(role, "left")) - opt->role_ = ROLE_LEFT; - else if(!strcmp(role, "bob") || !strcmp(role, "client") || !strcmp(role, "right")) - opt->role_ = ROLE_RIGHT; - else { - free(role); - return -4; - } - free(role); - } + return 0; } @@ -291,36 +183,7 @@ void options_parse_post(options_t* opt) { if(!opt) return; - -#ifdef NO_V4MAPPED - if(opt->resolv_addr_type_ == ANY) { - opt->resolv_addr_type_ = IPV4_ONLY; - log_printf(WARNING, "No support for V4-mapped Adresses on this platform, defaulting to only use IPv4 addresses"); - } -#endif - -#ifndef NO_CRYPT - if(!strcmp(opt->cipher_, "null") && !strcmp(opt->auth_algo_, "null") && - strcmp(opt->kd_prf_, "null")) { - if(opt->kd_prf_) - free(opt->kd_prf_); - opt->kd_prf_ = strdup("null"); - } - if((strcmp(opt->cipher_, "null") || strcmp(opt->auth_algo_, "null")) && - !strcmp(opt->kd_prf_, "null")) { - log_printf(WARNING, "using NULL key derivation with encryption and or authentication enabled!"); - } - - u_int32_t tag_len_max = auth_algo_get_max_length(opt->auth_algo_); - if(!tag_len_max) opt->auth_tag_length_ = 0; - else if(tag_len_max < opt->auth_tag_length_) { - log_printf(WARNING, "%s auth algo can't generate tags of length %d, using maximum tag length(%d)", opt->auth_algo_, opt->auth_tag_length_, tag_len_max); - opt->auth_tag_length_ = tag_len_max; - } -#endif - - if(!(opt->dev_name_) && !(opt->dev_type_)) - opt->dev_type_ = strdup("tun"); +// nothing to do } void options_default(options_t* opt) @@ -328,41 +191,16 @@ void options_default(options_t* opt) if(!opt) return; - opt->progname_ = strdup("uanytun"); + opt->progname_ = strdup("door_daemon"); opt->daemonize_ = 1; opt->username_ = NULL; opt->groupname_ = NULL; opt->chroot_dir_ = NULL; opt->pid_file_ = NULL; string_list_init(&opt->log_targets_); - opt->local_addr_ = NULL; - opt->local_port_ = strdup("4444"); - opt->sender_id_ = 0; - opt->remote_addr_ = NULL; - opt->remote_port_ = strdup("4444"); - opt->resolv_addr_type_ = ANY; - opt->dev_name_ = NULL; - opt->dev_type_ = NULL; - opt->ifconfig_param_.net_addr_ = NULL; - opt->ifconfig_param_.prefix_length_ = 0; - opt->post_up_script_ = NULL; - opt->mux_ = 0; - opt->seq_window_size_ = 0; -#ifndef NO_CRYPT - opt->kd_prf_ = strdup("aes-ctr"); - opt->passphrase_ = NULL; - opt->role_ = ROLE_LEFT; - opt->cipher_ = strdup("aes-ctr"); - opt->auth_algo_ = strdup("sha1"); - opt->auth_tag_length_ = 10; -#else - opt->cipher_ = strdup("null"); - opt->auth_tag_length_ = 0; -#endif - opt->key_.buf_ = NULL; - opt->key_.length_ = 0; - opt->salt_.buf_ = NULL; - opt->salt_.length_ = 0; + + opt->ttyusb_dev_ = strdup("/dev/ttyUSB0"); + opt->command_sock_ = strdup("/var/run/door_daemon"); } void options_clear(options_t* opt) @@ -381,76 +219,27 @@ void options_clear(options_t* opt) if(opt->pid_file_) free(opt->pid_file_); string_list_clear(&opt->log_targets_); - if(opt->local_addr_) - free(opt->local_addr_); - if(opt->local_port_) - free(opt->local_port_); - if(opt->remote_addr_) - free(opt->remote_addr_); - if(opt->remote_port_) - free(opt->remote_port_); - if(opt->dev_name_) - free(opt->dev_name_); - if(opt->dev_type_) - free(opt->dev_type_); - if(opt->ifconfig_param_.net_addr_) - free(opt->ifconfig_param_.net_addr_); - if(opt->post_up_script_) - free(opt->post_up_script_); - if(opt->cipher_) - free(opt->cipher_); -#ifndef NO_CRYPT - if(opt->auth_algo_) - free(opt->auth_algo_); - if(opt->kd_prf_) - free(opt->kd_prf_); - if(opt->passphrase_) - free(opt->passphrase_); -#endif - if(opt->key_.buf_) - free(opt->key_.buf_); - if(opt->salt_.buf_) - free(opt->salt_.buf_); + + if(opt->ttyusb_dev_) + free(opt->ttyusb_dev_); + if(opt->command_sock_) + free(opt->command_sock_); } void options_print_usage() { printf("USAGE:\n"); - printf("uanytun [-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(" [-i|--interface] local ip address to bind to\n"); - printf(" [-p|--port] local port to bind to\n"); - printf(" [-s|--sender-id ] the sender id to use\n"); - printf(" [-L|--log] :[,[,..]]\n"); - printf(" add a log target, can be invoked several times\n"); - - printf(" [-r|--remote-host] remote host\n"); - printf(" [-o|--remote-port] remote port\n"); - printf(" [-4|--ipv4-only] always resolv IPv4 addresses\n"); - printf(" [-6|--ipv6-only] always resolv IPv6 addresses\n"); - printf(" [-d|--dev] device name\n"); - printf(" [-t|--type] device type\n"); - - printf(" [-n|--ifconfig] / the local address for the tun/tap device and the used prefix length\n"); - printf(" [-x|--post-up-script]