1 /* Copyright 2008-2019 Bernhard R. Fischer, Daniel Haslinger.
2  *
3  * This file is part of OnionCat.
4  *
5  * OnionCat is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3 of the License.
8  *
9  * OnionCat is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with OnionCat. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*! \file ocatsetup.c
19  *  This file contains the global settings structure.
20  *
21  *  @author Bernhard Fischer <rahra _at_ cypherpunk at>
22  *  \date 2019/09/08
23  */
24 
25 
26 #include "ocat.h"
27 #include "ocat_netdesc.h"
28 #include "ocathosts.h"
29 
30 
31 static struct sockaddr_in6 socks_dst6_;
32 static struct sockaddr_in ctrl_listen_;
33 static struct sockaddr_in6 ctrl_listen6_;
34 static struct sockaddr *ctrl_listen_ptr_[] =
35 {
36    (struct sockaddr*) &ctrl_listen_,
37 #ifndef __CYGWIN__
38    (struct sockaddr*) &ctrl_listen6_,
39 #endif
40       NULL};
41 static int ctrl_fd_[2] = {-1, -1};
42 
43 struct OcatSetup setup_ =
44 {
45    // fhd_keys
46    {0, 0},
47    // fhd_key_len
48    sizeof(uint32_t),
49    //TOR_SOCKS_PORT,
50    //OCAT_LISTEN_PORT,
51    0, 0,
52    //! default tunfd is stdin/stdout
53    {0, 1},
54    1, // ipconfig
55    //! default debug level
56    LOG_DEBUG,
57    OCAT_UNAME, {0}, {0}, {{{0}}}, 0,
58 #ifndef __ANDROID__
59    0,                                     // run as root (dont drop privs)
60 #else
61    1,                                     // currently require for Android (cause theres now APK...)
62 #endif
63    1,                                     // enable controller interface
64    OCAT_DIR,
65    TUN_DEV,
66    0,                                     // enable SOCKS5
67    16,                                    // l_hs_namelen
68    {'\0'},                                // tunname
69    0,
70    //ADDR4_PREFIX, ADDR4_MASK
71    {0}, 0,
72    // config_file, config_read, config_failed
73    NULL, 0, 0,
74    NULL,                                  // ifup
75 #ifdef __CYGWIN__
76    1,
77 #else
78    0,                                      // use_tap
79 #endif
80    {0x00, 0x00, 0x6c, 0x00, 0x00, 0x00},   // ocat_hwaddr (OnionCat MAC address)
81    PID_FILE,                               // pid_file
82    0,                                      // create_pid_file
83    NULL, NULL,                             // logfile
84    0,                                      // use_syslog
85 #ifdef __CYGWIN__
86    0,
87 #else
88    1,                                      // daemon
89 #endif
90    0,
91    "/dev/urandom",
92    {(struct sockaddr_in*) &socks_dst6_},
93    // oc_listen
94    NULL,
95    // oc_listen_fd
96    NULL,
97    // oc_listen_cnt
98    0,
99    //! rand_addr
100    0,
101    {0},
102    sizeof(struct OcatSetup),
103    //! sig_term, term_req
104    0, 0,
105    PTHREAD_MUTEX_INITIALIZER,
106    // ctrl_listen
107    ctrl_listen_ptr_,
108    // oc_listen_fd
109    ctrl_fd_,
110    // oc_listen_cnt
111 #if defined(__CYGWIN__)
112    1
113 #else
114    2
115 #endif
116    ,
117    // socksfd
118    {-1, -1},
119    // net_type
120    NTYPE_TOR,
121    // max_ctrl, ctrl_active
122    MAX_DEF_CTRL_SESS, 0,
123    // pid_fd
124    {-1, -1},
125    // sig_usr1, clear_stats
126    0, 0,
127    // unidirectional
128    1,
129    // hosts_lookup
130    0,
131    // hosts_path
132    NULL,
133    // domain
134    ""
135 };
136 
137 
138 #define IADDRSTRLEN 128
139 
140 
init_setup(void)141 void init_setup(void)
142 {
143    struct timeval tv;
144    const uint32_t loop_ = htonl(INADDR_LOOPBACK);
145 
146    // seeding PRNG rand()
147    if (gettimeofday(&tv, NULL) == -1)
148       log_msg(LOG_WARNING, "could gettimeofday(): \"%s\"", strerror(errno));
149    srand(tv.tv_sec ^ tv.tv_usec);
150 
151    //setup_.logf = stderr;
152    setup_.uptime = time(NULL);
153    memset(&socks_dst6_, 0, sizeof(socks_dst6_));
154    setup_.socks_dst->sin_family = AF_INET;
155    //setup_.socks_dst->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
156    memcpy(&setup_.socks_dst->sin_addr, &loop_, sizeof(setup_.socks_dst->sin_addr));
157 #ifdef HAVE_SIN_LEN
158    setup_.socks_dst->sin_len = SOCKADDR_SIZE(setup_.socks_dst);
159 #endif
160 
161    memset(&ctrl_listen_, 0, sizeof(ctrl_listen_));
162    memset(&ctrl_listen6_, 0, sizeof(ctrl_listen6_));
163 
164 #ifdef __linux__
165    CNF(fhd_key[IPV6_KEY]) = htonl(ETHERTYPE_IPV6);
166    CNF(fhd_key[IPV4_KEY]) = htonl(ETHERTYPE_IP);
167 #else
168    CNF(fhd_key[IPV6_KEY]) = htonl(AF_INET6);
169    CNF(fhd_key[IPV4_KEY]) = htonl(AF_INET);
170 #endif
171 }
172 
173 
post_init_setup(void)174 void post_init_setup(void)
175 {
176    size_t l;
177    const uint32_t loop_ = htonl(INADDR_LOOPBACK);
178 
179    setup_.ocat_addr4 = NDESC(prefix4);
180    setup_.ocat_addr4_mask = NDESC(addr4_mask);
181    setup_.ocat_dest_port = NDESC(vdest_port);
182    setup_.ocat_ctrl_port = NDESC(ctrl_port);
183    setup_.hosts_lookup = NDESC(hosts_lookup);
184    setup_.domain = NDESC(domain);
185    setup_.l_hs_namelen = NDESC(l_hs_namelen);
186    hosts_init(NDESC(domain));
187 
188    l = strlen(SYSCONFDIR) + strlen(NDESC(config_file)) + 2;
189    if ((setup_.config_file = malloc(l)) != NULL)
190       snprintf(setup_.config_file, l, "%s/%s", SYSCONFDIR, NDESC(config_file));
191    else
192       log_msg(LOG_WARNING, "could not get memory for config file string: \"%s\"", strerror(errno));
193 
194    if (!setup_.socks_dst->sin_port)
195       setup_.socks_dst->sin_port = htons(NDESC(socks_port));
196 
197    ctrl_listen_.sin_family = AF_INET;
198    ctrl_listen_.sin_port = htons(setup_.ocat_ctrl_port);
199    //ctrl_listen_.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
200    memcpy(&ctrl_listen_.sin_addr, &loop_, sizeof(ctrl_listen_.sin_addr));
201 #ifdef HAVE_SIN_LEN
202    ctrl_listen_.sin_len = sizeof(ctrl_listen_);
203 #endif
204 
205    ctrl_listen6_.sin6_family = AF_INET6;
206    ctrl_listen6_.sin6_port = htons(setup_.ocat_ctrl_port);
207    ctrl_listen6_.sin6_addr = in6addr_loopback; //IN6ADDR_LOOPBACK_INIT;
208 #ifdef HAVE_SIN_LEN
209    ctrl_listen6_.sin6_len = sizeof(ctrl_listen6_);
210 #endif
211 
212    snprintf(setup_.version, VERSION_STRING_LEN, "%s (c) %s (%s mode)", PACKAGE_STRING, OCAT_AUTHOR, setup_.net_type == NTYPE_TOR ? "OnionCat" : setup_.net_type == NTYPE_I2P ? "GarliCat" : "unknown");
213 
214    // You may comment-in the following lines for debugging purpose. Code was
215    // removed due to request of Debian package maintainer.
216 #if 0
217    snprintf(&setup_.version[strlen(setup_.version)], VERSION_STRING_LEN - strlen(setup_.version), " -- compiled %s %s", __DATE__, __TIME__);
218 #endif
219 
220    setup_.pid_file = NDESC(pid_file);
221    setup_.oc_vdns = NDESC(prefix);
222    setup_.oc_vdns.s6_addr[15] = 1;
223 }
224 
225 
226 #define SBUF 100
227 
228 
print_setup_struct(FILE * f)229 void print_setup_struct(FILE *f)
230 {
231    char *c, ip[SBUF], nm[SBUF], ip6[SBUF], logf[SBUF], hw[SBUF];
232    int i, t;
233    struct sockaddr_str sas;
234 
235    inet_ntop(AF_INET, &setup_.ocat_addr4, ip, SBUF);
236    inet_ntop(AF_INET, &setup_.ocat_addr4_mask, nm, SBUF);
237    inet_ntop(AF_INET6, &setup_.ocat_addr, ip6, SBUF);
238    ether_ntoa_r((struct ether_addr*) setup_.ocat_hwaddr, hw);
239 
240    if (setup_.logf == stderr)
241       strlcpy(logf, "stderr", sizeof(logf));
242    else
243       snprintf(logf, sizeof(logf), "%p", setup_.logf);
244 
245    t = time(NULL) - setup_.uptime;
246 
247 
248    fprintf(f,
249          "fhd_key[IPV4(%d)]       = 0x%04x\n"
250          "fhd_key[IPV6(%d)]       = 0x%04x\n"
251          "fhd_key_len            = %d\n"
252          //"tor_socks_port    = %d\n"
253          //"ocat_listen_port       = %d\n"
254          "ocat_dest_port         = %d\n"
255          "ocat_ctrl_port         = %d\n"
256          "tunfd[0]               = %d\n"
257          "tunfd[1]               = %d\n"
258          "ipconfig               = %d\n"
259          "debug_level            = %d\n"
260          "usrname                = \"%s\"\n"
261          "onion_url              = \"%s\"\n"
262          "onion3_url             = \"%s\"\n"
263          "ocat_addr              = %s\n"
264          "create_clog            = %d\n"
265          "runasroot              = %d\n"
266          "controller             = %d\n"
267          "ocat_dir               = \"%s\"\n"
268          "tun_dev                = \"%s\"\n"
269          "socks5                 = %d\n"
270          "l_hs_namelen           = %d\n"
271          "tunname                = \"%s\"\n"
272          "ipv4_enable            = %d\n"
273          "ocat_addr4             = %s\n"
274          "ocat_addr4_mask        = %s\n"
275          "config_file            = \"%s\"\n"
276          "config_read            = %d\n"
277          "use_tap                = %d\n"
278          "ocat_hwaddr            = %s\n"
279          "pid_file               = \"%s\"\n"
280          "logfn                  = \"%s\"\n"
281          "logf                   = %s\n"
282          "daemon                 = %d\n"
283          "uptime                 = %d days, %d:%02d\n"
284          "version[%3d+1/%3d]     = \"%s\"\n"
285          "sizeof_setup           = %d\n"
286          "term_req               = %d\n"
287          "net_type               = %d (%s)\n"
288          "max_ctrl               = %d\n"
289          "ctrl_active            = %d\n"
290          "pid_fd[2]              = {%d, %d}\n"
291          "clear_stats            = %d\n"
292          "ctrl_listen_cnt        = %d\n"
293          "unidirectional         = %d\n"
294          "hosts_lookup           = %d\n"
295          "hosts_path             = %s\n"
296          "domain                 = \"%s\"\n"
297          "----------------------\n"
298          ,
299          IPV4_KEY, ntohl(setup_.fhd_key[IPV4_KEY]), IPV6_KEY, ntohl(setup_.fhd_key[IPV6_KEY]),
300          setup_.fhd_key_len,
301          //setup_.tor_socks_port,
302          //setup_.ocat_listen_port,
303          setup_.ocat_dest_port,
304          setup_.ocat_ctrl_port,
305          setup_.tunfd[0], setup_.tunfd[1],
306          setup_.ipconfig,
307          setup_.debug_level,
308          setup_.usrname,
309          setup_.onion_url,
310          setup_.onion3_url,
311          ip6,
312          setup_.create_clog,
313          setup_.runasroot,
314          setup_.controller,
315          setup_.ocat_dir,
316          setup_.tun_dev,
317          setup_.socks5,
318          setup_.l_hs_namelen,
319          setup_.tunname,
320          setup_.ipv4_enable,
321          ip,
322          nm,
323          setup_.config_file,
324          setup_.config_read,
325          setup_.use_tap,
326          hw,
327          setup_.pid_file,
328          SSTR(setup_.logfn),
329          logf,
330          setup_.daemon,
331          t / (3600 * 24), t / 3600 % 24, t / 60 % 60,
332          (int) strlen(setup_.version), VERSION_STRING_LEN, setup_.version,
333          setup_.sizeof_setup,
334          setup_.term_req,
335          setup_.net_type, setup_.net_type == NTYPE_TOR ? "NTYPE_TOR" : setup_.net_type == NTYPE_I2P ? "NTYPE_I2P" : "unknown",
336          setup_.max_ctrl, setup_.ctrl_active,
337          setup_.pid_fd[0], setup_.pid_fd[1],
338          setup_.clear_stats,
339          setup_.ctrl_listen_cnt,
340          setup_.unidirectional,
341          setup_.hosts_lookup,
342          SSTR(setup_.hosts_path),
343          setup_.domain
344          );
345 
346    if (inet_ntops((struct sockaddr*) setup_.socks_dst, &sas))
347    {
348       c = sas.sstr_family == AF_INET6 ? "6" : "";
349       fprintf(f,
350          "socks_dst%s.sin_family   = 0x%04x\n"
351          "socks_dst%s.sin_port     = %d\n"
352          "socks_dst%s.sin_addr     = %s\n",
353          c, sas.sstr_family,
354          c, ntohs(sas.sstr_port),
355          c, sas.sstr_addr);
356    }
357    else
358       log_msg(LOG_WARNING, "could not convert struct sockaddr: \"%s\"", strerror(errno));
359 
360    for (i = 0; i < CNF(oc_listen_cnt); i++)
361    {
362       if (inet_ntops(CNF(oc_listen)[i], &sas))
363          fprintf(f, "oc_listen[%d]           = %s:%d\n", i, sas.sstr_addr, ntohs(sas.sstr_port));
364       else
365          log_msg(LOG_WARNING, "could not convert struct sockaddr: \"%s\"", strerror(errno));
366       fprintf(f, "oc_listen_fd[%d]        = %d\n", i, CNF(oc_listen_fd)[i]);
367    }
368 
369    for (i = 0; i < CNF(ctrl_listen_cnt); i++)
370    {
371       if (inet_ntops(ctrl_listen_ptr_[i], &sas))
372          fprintf(f, "ctrl_listen_ptr_[%d]    = %s:%d (0x%04x)\n", i, sas.sstr_addr, ntohs(sas.sstr_port), sas.sstr_family);
373       else
374          log_msg(LOG_WARNING, "could not convert struct sockaddr: \"%s\"", strerror(errno));
375    }
376 
377    inet_ntop(AF_INET6, &setup_.oc_vdns, ip6, SBUF);
378    fprintf(f, "oc_vdns                = %s\n", ip6);
379 }
380 
381 
lock_setup(void)382 void lock_setup(void)
383 {
384    pthread_mutex_lock(&setup_.mutex);
385 }
386 
387 
unlock_setup(void)388 void unlock_setup(void)
389 {
390    pthread_mutex_unlock(&setup_.mutex);
391 }
392 
393