Update android build instructions. Disable PIE as this is not supported on some devices.
[tinc] / src / protocol.c
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
34 /* Jumptable for the request handlers */
35
36 static bool (*request_handlers[])(connection_t *) = {
37                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
38                 status_h, error_h, termreq_h,
39                 ping_h, pong_h,
40                 add_subnet_h, del_subnet_h,
41                 add_edge_h, del_edge_h,
42                 key_changed_h, req_key_h, ans_key_h, tcppacket_h,
43 };
44
45 /* Request names */
46
47 static char (*request_name[]) = {
48                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
49                 "STATUS", "ERROR", "TERMREQ",
50                 "PING", "PONG",
51                 "ADD_SUBNET", "DEL_SUBNET",
52                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET",
53 };
54
55 static avl_tree_t *past_request_tree;
56
57 bool check_id(const char *id) {
58         for(; *id; id++)
59                 if(!isalnum(*id) && *id != '_')
60                         return false;
61
62         return true;
63 }
64
65 /* Generic request routines - takes care of logging and error
66    detection as well */
67
68 bool send_request(connection_t *c, const char *format, ...) {
69         va_list args;
70         char buffer[MAXBUFSIZE];
71         int len, request = 0;
72
73         /* Use vsnprintf instead of vxasprintf: faster, no memory
74            fragmentation, cleanup is automatic, and there is a limit on the
75            input buffer anyway */
76
77         va_start(args, format);
78         len = vsnprintf(buffer, MAXBUFSIZE, format, args);
79         va_end(args);
80
81         if(len < 0 || len > MAXBUFSIZE - 1) {
82                 logger(LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
83                            c->name, c->hostname);
84                 return false;
85         }
86
87         ifdebug(PROTOCOL) {
88                 sscanf(buffer, "%d", &request);
89                 ifdebug(META)
90                         logger(LOG_DEBUG, "Sending %s to %s (%s): %s",
91                                    request_name[request], c->name, c->hostname, buffer);
92                 else
93                         logger(LOG_DEBUG, "Sending %s to %s (%s)", request_name[request],
94                                    c->name, c->hostname);
95         }
96
97         buffer[len++] = '\n';
98
99         if(c == everyone) {
100                 broadcast_meta(NULL, buffer, len);
101                 return true;
102         } else
103                 return send_meta(c, buffer, len);
104 }
105
106 void forward_request(connection_t *from) {
107         int request;
108
109         ifdebug(PROTOCOL) {
110                 sscanf(from->buffer, "%d", &request);
111                 ifdebug(META)
112                         logger(LOG_DEBUG, "Forwarding %s from %s (%s): %s",
113                                    request_name[request], from->name, from->hostname,
114                                    from->buffer);
115                 else
116                         logger(LOG_DEBUG, "Forwarding %s from %s (%s)",
117                                    request_name[request], from->name, from->hostname);
118         }
119
120         from->buffer[from->reqlen - 1] = '\n';
121
122         broadcast_meta(from, from->buffer, from->reqlen);
123 }
124
125 bool receive_request(connection_t *c) {
126         int request;
127
128         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
129                 if(!c->buffer[0] || c->buffer[0] == '\r')
130                         return true;
131                 if(!strncasecmp(c->buffer, "HTTP/1.1 ", 9)) {
132                         if(!strncmp(c->buffer + 9, "200", 3)) {
133                                 logger(LOG_DEBUG, "Proxy request granted");
134                                 return true;
135                         } else {
136                                 logger(LOG_DEBUG, "Proxy request rejected: %s", c->buffer + 9);
137                                 return false;
138                         }
139                 }
140         }
141
142         if(sscanf(c->buffer, "%d", &request) == 1) {
143                 if((request < 0) || (request >= LAST) || !request_handlers[request]) {
144                         ifdebug(META)
145                                 logger(LOG_DEBUG, "Unknown request from %s (%s): %s",
146                                            c->name, c->hostname, c->buffer);
147                         else
148                                 logger(LOG_ERR, "Unknown request from %s (%s)",
149                                            c->name, c->hostname);
150
151                         return false;
152                 } else {
153                         ifdebug(PROTOCOL) {
154                                 ifdebug(META)
155                                         logger(LOG_DEBUG, "Got %s from %s (%s): %s",
156                                                    request_name[request], c->name, c->hostname,
157                                                    c->buffer);
158                                 else
159                                         logger(LOG_DEBUG, "Got %s from %s (%s)",
160                                                    request_name[request], c->name, c->hostname);
161                         }
162                 }
163
164                 if((c->allow_request != ALL) && (c->allow_request != request)) {
165                         logger(LOG_ERR, "Unauthorized request from %s (%s)", c->name,
166                                    c->hostname);
167                         return false;
168                 }
169
170                 if(!request_handlers[request](c)) {
171                         /* Something went wrong. Probably scriptkiddies. Terminate. */
172
173                         logger(LOG_ERR, "Error while processing %s from %s (%s)",
174                                    request_name[request], c->name, c->hostname);
175                         return false;
176                 }
177         } else {
178                 logger(LOG_ERR, "Bogus data received from %s (%s)",
179                            c->name, c->hostname);
180                 return false;
181         }
182
183         return true;
184 }
185
186 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
187         return strcmp(a->request, b->request);
188 }
189
190 static void free_past_request(past_request_t *r) {
191         if(r->request)
192                 free(r->request);
193
194         free(r);
195 }
196
197 void init_requests(void) {
198         past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
199 }
200
201 void exit_requests(void) {
202         avl_delete_tree(past_request_tree);
203 }
204
205 bool seen_request(char *request) {
206         past_request_t *new, p = {NULL};
207
208         p.request = request;
209
210         if(avl_search(past_request_tree, &p)) {
211                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Already seen request");
212                 return true;
213         } else {
214                 new = xmalloc(sizeof(*new));
215                 new->request = xstrdup(request);
216                 new->firstseen = now;
217                 avl_insert(past_request_tree, new);
218                 return false;
219         }
220 }
221
222 void age_past_requests(void) {
223         avl_node_t *node, *next;
224         past_request_t *p;
225         int left = 0, deleted = 0;
226
227         for(node = past_request_tree->head; node; node = next) {
228                 next = node->next;
229                 p = node->data;
230
231                 if(p->firstseen + pinginterval <= now)
232                         avl_delete_node(past_request_tree, node), deleted++;
233                 else
234                         left++;
235         }
236
237         if(left || deleted)
238                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Aging past requests: deleted %d, left %d",
239                            deleted, left);
240 }