Remove "release-" from displayed git version.
[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 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                 status_h, error_h, 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, NULL, /* Not "real" requests (yet) */
45                 udp_info_h, mtu_info_h,
46 };
47
48 /* Request names */
49
50 static char (*request_name[]) = {
51                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
52                 "STATUS", "ERROR", "TERMREQ",
53                 "PING", "PONG",
54                 "ADD_SUBNET", "DEL_SUBNET",
55                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET", "CONTROL",
56                 "REQ_PUBKEY", "ANS_PUBKEY", "REQ_SPTPS", "UDP_INFO", "MTU_INFO",
57 };
58
59 static splay_tree_t *past_request_tree;
60
61 /* Generic request routines - takes care of logging and error
62    detection as well */
63
64 bool send_request(connection_t *c, const char *format, ...) {
65         va_list args;
66         char request[MAXBUFSIZE];
67         int len;
68
69         /* Use vsnprintf instead of vxasprintf: faster, no memory
70            fragmentation, cleanup is automatic, and there is a limit on the
71            input buffer anyway */
72
73         va_start(args, format);
74         len = vsnprintf(request, MAXBUFSIZE, format, args);
75         va_end(args);
76
77         if(len < 0 || len > MAXBUFSIZE - 1) {
78                 logger(DEBUG_ALWAYS, LOG_ERR, "Output buffer overflow while sending request to %s (%s)",
79                            c->name, c->hostname);
80                 return false;
81         }
82
83         logger(DEBUG_META, LOG_DEBUG, "Sending %s to %s (%s): %s", request_name[atoi(request)], c->name, c->hostname, request);
84
85         request[len++] = '\n';
86
87         if(c == everyone) {
88                 broadcast_meta(NULL, request, len);
89                 return true;
90         } else
91                 return send_meta(c, request, len);
92 }
93
94 void forward_request(connection_t *from, const char *request) {
95         logger(DEBUG_META, LOG_DEBUG, "Forwarding %s from %s (%s): %s", request_name[atoi(request)], from->name, from->hostname, request);
96
97         // Create a temporary newline-terminated copy of the request
98         int len = strlen(request);
99         char tmp[len + 1];
100         memcpy(tmp, request, len);
101         tmp[len] = '\n';
102         broadcast_meta(from, tmp, sizeof tmp);
103 }
104
105 bool receive_request(connection_t *c, const char *request) {
106         if(c->outgoing && proxytype == PROXY_HTTP && c->allow_request == ID) {
107                 if(!request[0] || request[0] == '\r')
108                         return true;
109                 if(!strncasecmp(request, "HTTP/1.1 ", 9)) {
110                         if(!strncmp(request + 9, "200", 3)) {
111                                 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Proxy request granted");
112                                 return true;
113                         } else {
114                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Proxy request rejected: %s", request + 9);
115                                 return false;
116                         }
117                 }
118         }
119
120         int reqno = atoi(request);
121
122         if(reqno || *request == '0') {
123                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
124                         logger(DEBUG_META, LOG_DEBUG, "Unknown request from %s (%s): %s", c->name, c->hostname, request);
125                         return false;
126                 } else {
127                         logger(DEBUG_META, LOG_DEBUG, "Got %s from %s (%s): %s", request_name[reqno], c->name, c->hostname, request);
128                 }
129
130                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
131                         logger(DEBUG_ALWAYS, LOG_ERR, "Unauthorized request from %s (%s)", c->name, c->hostname);
132                         return false;
133                 }
134
135                 if(!request_handlers[reqno](c, request)) {
136                         /* Something went wrong. Probably scriptkiddies. Terminate. */
137
138                         if(reqno != TERMREQ)
139                                 logger(DEBUG_ALWAYS, LOG_ERR, "Error while processing %s from %s (%s)", request_name[reqno], c->name, c->hostname);
140                         return false;
141                 }
142         } else {
143                 logger(DEBUG_ALWAYS, LOG_ERR, "Bogus data received from %s (%s)", c->name, c->hostname);
144                 return false;
145         }
146
147         return true;
148 }
149
150 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
151         return strcmp(a->request, b->request);
152 }
153
154 static void free_past_request(past_request_t *r) {
155         if(r->request)
156                 free((char *)r->request);
157
158         free(r);
159 }
160
161 static timeout_t past_request_timeout;
162
163 static void age_past_requests(void *data) {
164         int left = 0, deleted = 0;
165
166         for splay_each(past_request_t, p, past_request_tree) {
167                 if(p->firstseen + pinginterval <= now.tv_sec)
168                         splay_delete_node(past_request_tree, node), deleted++;
169                 else
170                         left++;
171         }
172
173         if(left || deleted)
174                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Aging past requests: deleted %d, left %d", deleted, left);
175
176         if(left)
177                 timeout_set(&past_request_timeout, &(struct timeval){10, rand() % 100000});
178 }
179
180 bool seen_request(const char *request) {
181         past_request_t *new, p = {NULL};
182
183         p.request = request;
184
185         if(splay_search(past_request_tree, &p)) {
186                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Already seen request");
187                 return true;
188         } else {
189                 new = xmalloc(sizeof *new);
190                 new->request = xstrdup(request);
191                 new->firstseen = now.tv_sec;
192                 splay_insert(past_request_tree, new);
193                 timeout_add(&past_request_timeout, age_past_requests, NULL, &(struct timeval){10, rand() % 100000});
194                 return false;
195         }
196 }
197
198 void init_requests(void) {
199         past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
200 }
201
202 void exit_requests(void) {
203         splay_delete_tree(past_request_tree);
204
205         timeout_del(&past_request_timeout);
206 }