1 /*
2 protocol.c -- handle the meta-protocol, basic functions
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "connection.h"
25 #include "logger.h"
26 #include "meta.h"
27 #include "protocol.h"
28 #include "utils.h"
29 #include "xalloc.h"
30
31 bool tunnelserver = false;
32 bool strictsubnets = false;
33 bool experimental = true;
34
35 /* Jumptable for the request handlers */
36
37 static bool (*request_handlers[])(connection_t *, const char *) = {
38 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
39 NULL, NULL, termreq_h,
40 ping_h, pong_h,
41 add_subnet_h, del_subnet_h,
42 add_edge_h, del_edge_h,
43 key_changed_h, req_key_h, ans_key_h, tcppacket_h, control_h,
44 NULL, NULL, /* Not "real" requests (yet) */
45 sptps_tcppacket_h,
46 udp_info_h, mtu_info_h,
47 };
48
49 /* Request names */
50
51 static char (*request_name[]) = {
52 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
53 "STATUS", "ERROR", "TERMREQ",
54 "PING", "PONG",
55 "ADD_SUBNET", "DEL_SUBNET",
56 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
57 "REQ_PUBKEY", "ANS_PUBKEY", "SPTPS_PACKET", "UDP_INFO", "MTU_INFO",
58 };
59
60 static splay_tree_t *past_request_tree;
61
62 /* Generic request routines - takes care of logging and error
63 detection as well */
64
send_request(connection_t * c,const char * format,...)65 bool send_request(connection_t *c, const char *format, ...) {
66 va_list args;
67 char request[MAXBUFSIZE];
68 int len;
69
70 /* Use vsnprintf instead of vxasprintf: faster, no memory
71 fragmentation, cleanup is automatic, and there is a limit on the
72 input buffer anyway */
73
74 va_start(args, format);
75 len = vsnprintf(request, sizeof(request), format, args);
76 request[sizeof(request) - 1] = 0;
77 va_end(args);
78
79 if(len < 0 || (size_t)len > sizeof(request) - 1) {
80 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
81 c->name, c->hostname);
82 return false;
83 }
84
85 int id = atoi(request);
86 logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[id], c->name, c->hostname, request);
87
88 request[len++] = '\n';
89
90 if(c == everyone) {
91 broadcast_meta(NULL, request, len);
92 return true;
93 } else {
94 if(id) {
95 return send_meta(c, request, len);
96 } else {
97 send_meta_raw(c, request, len);
98 return true;
99 }
100 }
101 }
102
forward_request(connection_t * from,const char * request)103 void forward_request(connection_t *from, const char *request) {
104 logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
105
106 // Create a temporary newline-terminated copy of the request
107 int len = strlen(request);
108 char tmp[len + 1];
109 memcpy(tmp, request, len);
110 tmp[len] = '\n';
111 broadcast_meta(from, tmp, sizeof(tmp));
112 }
113
receive_request(connection_t * c,const char * request)114 bool receive_request(connection_t *c, const char *request) {
115 if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
116 if(!request[0] || request[0] == '\r') {
117 return true;
118 }
119
120 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
121 if(!strncmp(request + 9, "200", 3)) {
122 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
123 return true;
124 } else {
125 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
126 return false;
127 }
128 }
129 }
130
131 int reqno = atoi(request);
132
133 if(reqno || *request == '0') {
134 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
135 logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
136 return false;
137 } else {
138 logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", request_name[reqno], c->name, c->hostname, request);
139 }
140
141 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
142 logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
143 return false;
144 }
145
146 if(!request_handlers[reqno](c, request)) {
147 /* Something went wrong. Probably scriptkiddies. Terminate. */
148
149 if(reqno != TERMREQ) {
150 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
151 }
152
153 return false;
154 }
155 } else {
156 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
157 return false;
158 }
159
160 return true;
161 }
162
past_request_compare(const past_request_t * a,const past_request_t * b)163 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
164 return strcmp(a->request, b->request);
165 }
166
free_past_request(past_request_t * r)167 static void free_past_request(past_request_t *r) {
168 free((char *)r->request);
169 free(r);
170 }
171
172 static timeout_t past_request_timeout;
173
age_past_requests(void * data)174 static void age_past_requests(void *data) {
175 (void)data;
176 int left = 0, deleted = 0;
177
178 for splay_each(past_request_t, p, past_request_tree) {
179 if(p->firstseen + pinginterval <= now.tv_sec) {
180 splay_delete_node(past_request_tree, node), deleted++;
181 } else {
182 left++;
183 }
184 }
185
186 if(left || deleted) {
187 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
188 }
189
190 if(left)
191 timeout_set(&past_request_timeout, &(struct timeval) {
192 10, rand() % 100000
193 });
194 }
195
seen_request(const char * request)196 bool seen_request(const char *request) {
197 past_request_t *new, p = {0};
198
199 p.request = request;
200
201 if(splay_search(past_request_tree, &p)) {
202 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
203 return true;
204 } else {
205 new = xmalloc(sizeof(*new));
206 new->request = xstrdup(request);
207 new->firstseen = now.tv_sec;
208 splay_insert(past_request_tree, new);
209 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval) {
210 10, rand() % 100000
211 });
212 return false;
213 }
214 }
215
init_requests(void)216 void init_requests(void) {
217 past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
218 }
219
exit_requests(void)220 void exit_requests(void) {
221 splay_delete_tree(past_request_tree);
222
223 timeout_del(&past_request_timeout);
224 }
225