Initial Debian packaging; add src/include.mk
[debian/uanytun.git] / src / options.c
1 /*
2  *  uAnytun
3  *
4  *  uAnytun is a tiny implementation of SATP. Unlike Anytun which is a full
5  *  featured implementation uAnytun has no support for multiple connections
6  *  or synchronisation. It is a small single threaded implementation intended
7  *  to act as a client on small platforms.
8  *  The secure anycast tunneling protocol (satp) defines a protocol used
9  *  for communication between any combination of unicast and anycast
10  *  tunnel endpoints.  It has less protocol overhead than IPSec in Tunnel
11  *  mode and allows tunneling of every ETHER TYPE protocol (e.g.
12  *  ethernet, ip, arp ...). satp directly includes cryptography and
13  *  message authentication based on the methodes used by SRTP.  It is
14  *  intended to deliver a generic, scaleable and secure solution for
15  *  tunneling and relaying of packets of any protocol.
16  *  
17  *
18  *  Copyright (C) 2007-2008 Christian Pointner <equinox@anytun.org>
19  *
20  *  This file is part of uAnytun.
21  *
22  *  uAnytun is free software: you can redistribute it and/or modify
23  *  it under the terms of the GNU General Public License version 3 as
24  *  published by the Free Software Foundation.
25  *
26  *  uAnytun is distributed in the hope that it will be useful,
27  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
28  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29  *  GNU General Public License for more details.
30  *
31  *  You should have received a copy of the GNU General Public License
32  *  along with uAnytun. If not, see <http://www.gnu.org/licenses/>.
33  */
34
35 #include "datatypes.h"
36
37 #include "options.h"
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <ctype.h>
43
44 #include "log.h"
45
46 #ifndef NO_CRYPT
47 #include "auth_algo.h"
48 #endif
49
50 #define PARSE_BOOL_PARAM(SHORT, LONG, VALUE)             \
51     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
52       VALUE = 1;
53
54 #define PARSE_INVERSE_BOOL_PARAM(SHORT, LONG, VALUE)     \
55     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
56       VALUE = 0;
57
58 #define PARSE_INT_PARAM(SHORT, LONG, VALUE)              \
59     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
60     {                                                    \
61       if(argc < 1)                                       \
62         return i;                                        \
63       VALUE = atoi(argv[i+1]);                           \
64       argc--;                                            \
65       i++;                                               \
66     }
67
68 #define PARSE_STRING_PARAM(SHORT, LONG, VALUE)           \
69     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
70     {                                                    \
71       if(argc < 1 || argv[i+1][0] == '-')                \
72         return i;                                        \
73       if(VALUE) free(VALUE);                             \
74       VALUE = strdup(argv[i+1]);                         \
75       if(!VALUE)                                         \
76         return -2;                                       \
77       argc--;                                            \
78       i++;                                               \
79     }
80
81 #define PARSE_STRING_PARAM_SEC(SHORT, LONG, VALUE)       \
82     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
83     {                                                    \
84       if(argc < 1 || argv[i+1][0] == '-')                \
85         return i;                                        \
86       if(VALUE) free(VALUE);                             \
87       VALUE = strdup(argv[i+1]);                         \
88       if(!VALUE)                                         \
89         return -2;                                       \
90       size_t j;                                          \
91       for(j=0; j < strlen(argv[i+1]); ++j)               \
92         argv[i+1][j] = '#';                              \
93       argc--;                                            \
94       i++;                                               \
95     }
96
97 #define PARSE_IFCONFIG_PARAM(SHORT, LONG, VALUE)         \
98     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
99     {                                                    \
100       if(argc < 1 || argv[i+1][0] == '-')                \
101         return i;                                        \
102       int ret;                                           \
103       ret = options_parse_ifconfig(argv[i+1], &VALUE);   \
104       if(ret > 0)                                        \
105         return i+1;                                      \
106       if(ret < 0)                                        \
107         return ret;                                      \
108       argc--;                                            \
109       i++;                                               \
110     }
111
112 #define PARSE_HEXSTRING_PARAM_SEC(SHORT, LONG, VALUE)    \
113     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
114     {                                                    \
115       if(argc < 1 || argv[i+1][0] == '-')                \
116         return i;                                        \
117       int ret;                                           \
118       ret = options_parse_hex_string(argv[i+1], &VALUE); \
119       if(ret > 0)                                        \
120         return i+1;                                      \
121       else if(ret < 0)                                   \
122         return ret;                                      \
123       size_t j;                                          \
124       for(j=0; j < strlen(argv[i+1]); ++j)               \
125         argv[i+1][j] = '#';                              \
126       argc--;                                            \
127       i++;                                               \
128     }
129
130 #define PARSE_STRING_LIST(SHORT, LONG, LIST)             \
131     else if(!strcmp(str,SHORT) || !strcmp(str,LONG))     \
132     {                                                    \
133       if(argc < 1 || argv[i+1][0] == '-')                \
134         return i;                                        \
135       int ret = string_list_add(&LIST, argv[i+1]);       \
136       if(ret == -2)                                      \
137         return ret;                                      \
138       else if(ret)                                       \
139         return i+1;                                      \
140       argc--;                                            \
141       i++;                                               \
142     }
143
144 int options_parse_hex_string(const char* hex, buffer_t* buffer)
145 {
146   if(!hex || !buffer)
147     return -1;
148
149   u_int32_t hex_len = strlen(hex);
150   if(hex_len%2)
151     return 1;
152
153   if(buffer->buf_) 
154     free(buffer->buf_);
155   
156   buffer->length_ = hex_len/2;
157   buffer->buf_ = malloc(buffer->length_);
158   if(!buffer->buf_) {
159     buffer->length_ = 0;
160     return -2;
161   }
162
163   const char* ptr = hex;
164   int i;
165   for(i=0;i<buffer->length_;++i) {
166     u_int32_t tmp;
167     sscanf(ptr, "%2X", &tmp);
168     buffer->buf_[i] = (u_int8_t)tmp;
169     ptr += 2;
170   }
171
172   return 0;
173 }
174
175 int options_parse_ifconfig(const char* arg, ifconfig_param_t* ifcfg)
176 {
177   char* str = strdup(arg);
178   if(!str)
179     return -2;
180
181   char* ptr = str;
182   for(;*ptr;++ptr) {
183     if(*ptr == '/') {
184       *ptr = 0;
185       ptr++;
186       if(!(*ptr)) {
187         free(str);
188         return 1;
189       }
190       
191       ifcfg->prefix_length_ = atoi(ptr);
192       ifcfg->net_addr_ = strdup(str);
193       free(str);
194
195       if(!ifcfg->net_addr_)
196         return -2;
197
198       return 0;
199     }
200     if(!isdigit(*ptr) && *ptr != '.') {
201       free(str);
202       return 1;
203     }
204   }
205
206   free(str);
207   return 1;
208 }
209
210
211 int options_parse(options_t* opt, int argc, char* argv[])
212 {
213   if(!opt)
214     return -1;
215
216   options_default(opt);
217
218   if(opt->progname_)
219     free(opt->progname_);
220   opt->progname_ = strdup(argv[0]);
221   if(!opt->progname_)
222     return -2;
223
224   argc--;
225
226   char* role = NULL;
227   int i, ipv4_only = 0, ipv6_only = 0;
228   for(i=1; argc > 0; ++i)
229   {
230     char* str = argv[i];
231     argc--;
232
233     if(!strcmp(str,"-h") || !strcmp(str,"--help"))
234       return -1;
235     PARSE_INVERSE_BOOL_PARAM("-D","--nodaemonize", opt->daemonize_)
236     PARSE_STRING_PARAM("-u","--username", opt->username_)
237     PARSE_STRING_PARAM("-g","--groupname", opt->groupname_)
238     PARSE_STRING_PARAM("-C","--chroot", opt->chroot_dir_)
239     PARSE_STRING_PARAM("-P","--write-pid", opt->pid_file_)
240     PARSE_STRING_PARAM("-i","--interface", opt->local_addr_)
241     PARSE_STRING_PARAM("-p","--port", opt->local_port_)
242     PARSE_INT_PARAM("-s","--sender-id", opt->sender_id_)
243     PARSE_STRING_LIST("-L","--log", opt->log_targets_)
244     PARSE_STRING_PARAM("-r","--remote-host", opt->remote_addr_)
245     PARSE_STRING_PARAM("-o","--remote-port", opt->remote_port_)
246     PARSE_BOOL_PARAM("-4","--ipv4-only", ipv4_only)
247     PARSE_BOOL_PARAM("-6","--ipv6-only", ipv6_only)
248     PARSE_STRING_PARAM("-d","--dev", opt->dev_name_)
249     PARSE_STRING_PARAM("-t","--type", opt->dev_type_)
250     PARSE_IFCONFIG_PARAM("-n","--ifconfig", opt->ifconfig_param_)
251     PARSE_STRING_PARAM("-x","--post-up-script", opt->post_up_script_)
252     PARSE_INT_PARAM("-m","--mux", opt->mux_)
253     PARSE_INT_PARAM("-w","--window-size", opt->seq_window_size_)
254 #ifndef NO_CRYPT
255     PARSE_STRING_PARAM("-k","--kd-prf", opt->kd_prf_)
256 #ifndef NO_PASSPHRASE
257     PARSE_STRING_PARAM_SEC("-E","--passphrase", opt->passphrase_)
258 #endif
259     PARSE_STRING_PARAM("-e","--role", role)
260     PARSE_HEXSTRING_PARAM_SEC("-K","--key", opt->key_)
261     PARSE_HEXSTRING_PARAM_SEC("-A","--salt", opt->salt_)
262     PARSE_STRING_PARAM("-c","--cipher", opt->cipher_)
263     PARSE_STRING_PARAM("-a","--auth-algo", opt->auth_algo_)
264     PARSE_INT_PARAM("-b","--auth-tag-length", opt->auth_tag_length_)
265 #endif
266     else 
267       return i;
268   }
269   if(ipv4_only && ipv6_only)
270     return -3;
271   if(ipv4_only)
272     opt->resolv_addr_type_ = IPV4_ONLY;
273   if(ipv6_only)
274     opt->resolv_addr_type_ = IPV6_ONLY;
275
276   if(role) {
277     if(!strcmp(role, "alice") || !strcmp(role, "server") || !strcmp(role, "left"))
278       opt->role_ = ROLE_LEFT;
279     else if(!strcmp(role, "bob") || !strcmp(role, "client") || !strcmp(role, "right"))
280       opt->role_ = ROLE_RIGHT;
281     else {
282       free(role);
283       return -4;
284     }
285     free(role);
286   }
287   return 0;
288 }
289
290 void options_parse_post(options_t* opt)
291 {
292   if(!opt)
293     return;
294
295 #ifdef NO_V4MAPPED
296   if(opt->resolv_addr_type_ == ANY) {
297     opt->resolv_addr_type_ = IPV4_ONLY;
298     log_printf(WARNING, "No support for V4-mapped Adresses on this platform, defaulting to only use IPv4 addresses");
299   }
300 #endif
301
302 #ifndef NO_CRYPT
303   if(!strcmp(opt->cipher_, "null") && !strcmp(opt->auth_algo_, "null") && 
304      strcmp(opt->kd_prf_, "null")) {
305     if(opt->kd_prf_)
306       free(opt->kd_prf_);
307     opt->kd_prf_ = strdup("null");
308   }
309   if((strcmp(opt->cipher_, "null") || strcmp(opt->auth_algo_, "null")) && 
310      !strcmp(opt->kd_prf_, "null")) {
311     log_printf(WARNING, "using NULL key derivation with encryption and or authentication enabled!");
312   }
313
314   u_int32_t tag_len_max = auth_algo_get_max_length(opt->auth_algo_);
315   if(!tag_len_max) opt->auth_tag_length_ = 0;
316   else if(tag_len_max < opt->auth_tag_length_) {
317     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);
318     opt->auth_tag_length_ = tag_len_max;
319   }
320 #endif
321
322   if(!(opt->dev_name_) && !(opt->dev_type_))
323     opt->dev_type_ = strdup("tun");
324 }
325
326 void options_default(options_t* opt)
327 {
328   if(!opt)
329     return;
330
331   opt->progname_ = strdup("uanytun");
332   opt->daemonize_ = 1;
333   opt->username_ = NULL;
334   opt->groupname_ = NULL;
335   opt->chroot_dir_ = NULL;
336   opt->pid_file_ = NULL;
337   string_list_init(&opt->log_targets_);
338   opt->local_addr_ = NULL;
339   opt->local_port_ = strdup("4444");
340   opt->sender_id_ = 0;
341   opt->remote_addr_ = NULL;
342   opt->remote_port_ = strdup("4444");
343   opt->resolv_addr_type_ = ANY;
344   opt->dev_name_ = NULL;
345   opt->dev_type_ = NULL;
346   opt->ifconfig_param_.net_addr_ = NULL;
347   opt->ifconfig_param_.prefix_length_ = 0;
348   opt->post_up_script_ = NULL;
349   opt->mux_ = 0;
350   opt->seq_window_size_ = 0;
351 #ifndef NO_CRYPT
352   opt->kd_prf_ = strdup("aes-ctr");
353   opt->passphrase_ = NULL;
354   opt->role_ = ROLE_LEFT;
355   opt->cipher_ = strdup("aes-ctr");
356   opt->auth_algo_ = strdup("sha1");
357   opt->auth_tag_length_ = 10;
358 #else
359   opt->cipher_ = strdup("null");
360   opt->auth_tag_length_ = 0;
361 #endif
362   opt->key_.buf_ = NULL;
363   opt->key_.length_ = 0;
364   opt->salt_.buf_ = NULL;
365   opt->salt_.length_ = 0;
366 }
367
368 void options_clear(options_t* opt)
369 {
370   if(!opt)
371     return;
372
373   if(opt->progname_)
374     free(opt->progname_);
375   if(opt->username_)
376     free(opt->username_);
377   if(opt->groupname_)
378     free(opt->groupname_);
379   if(opt->chroot_dir_)
380     free(opt->chroot_dir_);
381   if(opt->pid_file_)
382     free(opt->pid_file_);
383   string_list_clear(&opt->log_targets_);
384   if(opt->local_addr_)
385     free(opt->local_addr_);
386   if(opt->local_port_)
387     free(opt->local_port_);
388   if(opt->remote_addr_)
389     free(opt->remote_addr_);
390   if(opt->remote_port_)
391     free(opt->remote_port_);
392   if(opt->dev_name_)
393     free(opt->dev_name_);
394   if(opt->dev_type_)
395     free(opt->dev_type_);
396   if(opt->ifconfig_param_.net_addr_)
397     free(opt->ifconfig_param_.net_addr_);
398   if(opt->post_up_script_)
399     free(opt->post_up_script_);
400   if(opt->cipher_)
401     free(opt->cipher_);
402 #ifndef NO_CRYPT
403   if(opt->auth_algo_)
404     free(opt->auth_algo_);
405   if(opt->kd_prf_)
406     free(opt->kd_prf_);
407   if(opt->passphrase_)
408     free(opt->passphrase_);
409 #endif
410   if(opt->key_.buf_)
411     free(opt->key_.buf_);
412   if(opt->salt_.buf_)
413     free(opt->salt_.buf_);
414 }
415
416 void options_print_usage()
417 {
418   printf("USAGE:\n");
419   printf("uanytun [-h|--help]                         prints this...\n");
420   printf("        [-D|--nodaemonize]                  don't run in background\n");
421   printf("        [-u|--username] <username>          change to this user\n");
422   printf("        [-g|--groupname] <groupname>        change to this group\n");
423   printf("        [-C|--chroot] <path>                chroot to this directory\n");
424   printf("        [-P|--write-pid] <path>             write pid to this file\n");
425   printf("        [-i|--interface] <ip-address>       local ip address to bind to\n");
426   printf("        [-p|--port] <port>                  local port to bind to\n");
427   printf("        [-s|--sender-id ] <sender id>       the sender id to use\n");
428   printf("        [-L|--log] <target>:<level>[,<param1>[,<param2>..]]\n");
429   printf("                                            add a log target, can be invoked several times\n");
430
431   printf("        [-r|--remote-host] <hostname|ip>    remote host\n");
432   printf("        [-o|--remote-port] <port>           remote port\n");
433   printf("        [-4|--ipv4-only]                    always resolv IPv4 addresses\n");
434   printf("        [-6|--ipv6-only]                    always resolv IPv6 addresses\n");
435   printf("        [-d|--dev] <name>                   device name\n");
436   printf("        [-t|--type] <tun|tap>               device type\n");
437
438   printf("        [-n|--ifconfig] <local>/<prefix>    the local address for the tun/tap device and the used prefix length\n");
439   printf("        [-x|--post-up-script] <script>      script gets called after interface is created\n");
440   printf("        [-m|--mux] <mux-id>                 the multiplex id to use\n");
441   printf("        [-w|--window-size] <window size>    seqence number window size\n");
442 #ifndef NO_CRYPT
443   printf("        [-k|--kd-prf] <kd-prf type>         key derivation pseudo random function\n");
444 #ifndef NO_PASSPHRASE
445   printf("        [-E|--passphrase] <pass phrase>     a passprhase to generate master key and salt from\n");
446 #endif
447   printf("        [-K|--key] <master key>             master key to use for encryption\n");
448   printf("        [-A|--salt] <master salt>           master salt to use for encryption\n");
449   printf("        [-e|--role] <role>                  left (alice) or right (bob)");
450   printf("        [-c|--cipher] <cipher type>         payload encryption algorithm\n");
451   printf("        [-a|--auth-algo] <algo type>        message authentication algorithm\n");
452   printf("        [-b|--auth-tag-length] <length>     length of the auth tag\n");
453 #endif
454 }
455
456 void options_print(options_t* opt)
457 {
458   if(!opt)
459     return;
460
461   printf("progname: '%s'\n", opt->progname_);
462   printf("daemonize: %d\n", opt->daemonize_);
463   printf("username: '%s'\n", opt->username_);
464   printf("groupname: '%s'\n", opt->groupname_);
465   printf("chroot_dir: '%s'\n", opt->chroot_dir_);
466   printf("pid_file: '%s'\n", opt->pid_file_);
467   printf("log_targets: \n");
468   string_list_print(&opt->log_targets_, "  '", "'\n");
469   printf("local_addr: '%s'\n", opt->local_addr_);
470   printf("local_port: '%s'\n", opt->local_port_);
471   printf("sender_id: %d\n", opt->sender_id_);
472   printf("remote_addr: '%s'\n", opt->remote_addr_);
473   printf("remote_port: '%s'\n", opt->remote_port_);
474   printf("resolv_addr_type: ");
475   switch(opt->resolv_addr_type_) {
476   case ANY: printf("any\n"); break;
477   case IPV4_ONLY: printf("ipv4-only\n"); break;
478   case IPV6_ONLY: printf("ipv6-only\n"); break;
479   default: printf("??\n"); break;
480   }
481   printf("dev_name: '%s'\n", opt->dev_name_);
482   printf("dev_type: '%s'\n", opt->dev_type_);
483   printf("ifconfig_net_addr: '%s'\n", opt->ifconfig_param_.net_addr_);
484   printf("ifconfig_prefix_length: %d\n", opt->ifconfig_param_.prefix_length_);
485   printf("post_up_script: '%s'\n", opt->post_up_script_);
486   printf("mux: %d\n", opt->mux_);
487   printf("seq_window_size: %d\n", opt->seq_window_size_);
488   printf("cipher: '%s'\n", opt->cipher_);
489 #ifndef NO_CRYPT
490   printf("auth_algo: '%s'\n", opt->auth_algo_);
491   printf("auth_tag_length: %d\n", opt->auth_tag_length_);
492   printf("kd_prf: '%s'\n", opt->kd_prf_);
493   printf("passphrase: '%s'\n", opt->passphrase_);
494   printf("role: ");
495   switch(opt->role_) {
496   case ROLE_LEFT: printf("left\n"); break;
497   case ROLE_RIGHT: printf("right\n"); break;
498   default: printf("??\n"); break;
499   }
500 #endif
501
502   u_int32_t i;
503   printf("key_[%d]: '", opt->key_.length_);
504   for(i=0; i<opt->key_.length_; ++i) printf("%02X", opt->key_.buf_[i]);
505   printf("'\n");
506
507   printf("salt_[%d]: '", opt->salt_.length_);
508   for(i=0; i<opt->salt_.length_; ++i) printf("%02X", opt->salt_.buf_[i]);
509   printf("'\n");
510 }