90eca015ea5d5a009749afa27fa1e0e69161a71c
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2009 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 *, char *) = {
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, control_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", "CONTROL",
53 };
54
55 static splay_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 request[MAXBUFSIZE];
71         int len;
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(request, 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                 ifdebug(META)
89                         logger(LOG_DEBUG, "Sending %s to %s (%s): %s",
90                                    request_name[atoi(request)], c->name, c->hostname, request);
91                 else
92                         logger(LOG_DEBUG, "Sending %s to %s (%s)", request_name[atoi(request)],
93                                    c->name, c->hostname);
94         }
95
96         request[len++] = '\n';
97
98         if(c == broadcast) {
99                 broadcast_meta(NULL, request, len);
100                 return true;
101         } else
102                 return send_meta(c, request, len);
103 }
104
105 void forward_request(connection_t *from, char *request) {
106         ifdebug(PROTOCOL) {
107                 ifdebug(META)
108                         logger(LOG_DEBUG, "Forwarding %s from %s (%s): %s",
109                                    request_name[atoi(request)], from->name, from->hostname, request);
110                 else
111                         logger(LOG_DEBUG, "Forwarding %s from %s (%s)",
112                                    request_name[atoi(request)], from->name, from->hostname);
113         }
114
115         int len = strlen(request);
116         request[len] = '\n';
117         broadcast_meta(from, request, len);
118 }
119
120 bool receive_request(connection_t *c, char *request) {
121         int reqno = atoi(request);
122
123         if(reqno || *request == '0') {
124                 if((reqno < 0) || (reqno >= LAST) || !request_handlers[reqno]) {
125                         ifdebug(META)
126                                 logger(LOG_DEBUG, "Unknown request from %s (%s): %s",
127                                            c->name, c->hostname, request);
128                         else
129                                 logger(LOG_ERR, "Unknown request from %s (%s)",
130                                            c->name, c->hostname);
131
132                         return false;
133                 } else {
134                         ifdebug(PROTOCOL) {
135                                 ifdebug(META)
136                                         logger(LOG_DEBUG, "Got %s from %s (%s): %s",
137                                                    request_name[reqno], c->name, c->hostname, request);
138                                 else
139                                         logger(LOG_DEBUG, "Got %s from %s (%s)",
140                                                    request_name[reqno], c->name, c->hostname);
141                         }
142                 }
143
144                 if((c->allow_request != ALL) && (c->allow_request != reqno)) {
145                         logger(LOG_ERR, "Unauthorized request from %s (%s)", c->name,
146                                    c->hostname);
147                         return false;
148                 }
149
150                 if(!request_handlers[reqno](c, request)) {
151                         /* Something went wrong. Probably scriptkiddies. Terminate. */
152
153                         logger(LOG_ERR, "Error while processing %s from %s (%s)",
154                                    request_name[reqno], c->name, c->hostname);
155                         return false;
156                 }
157         } else {
158                 logger(LOG_ERR, "Bogus data received from %s (%s)",
159                            c->name, c->hostname);
160                 return false;
161         }
162
163         return true;
164 }
165
166 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
167         return strcmp(a->request, b->request);
168 }
169
170 static void free_past_request(past_request_t *r) {
171         if(r->request)
172                 free(r->request);
173
174         free(r);
175 }
176
177 static struct event past_request_event;
178
179 bool seen_request(char *request) {
180         past_request_t *new, p = {0};
181
182         p.request = request;
183
184         if(splay_search(past_request_tree, &p)) {
185                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Already seen request");
186                 return true;
187         } else {
188                 new = xmalloc(sizeof *new);
189                 new->request = xstrdup(request);
190                 new->firstseen = time(NULL);
191                 splay_insert(past_request_tree, new);
192                 event_add(&past_request_event, &(struct timeval){10, 0});
193                 return false;
194         }
195 }
196
197 void age_past_requests(int fd, short events, void *data) {
198         splay_node_t *node, *next;
199         past_request_t *p;
200         int left = 0, deleted = 0;
201         time_t now = time(NULL);
202
203         for(node = past_request_tree->head; node; node = next) {
204                 next = node->next;
205                 p = node->data;
206
207                 if(p->firstseen + pinginterval < now)
208                         splay_delete_node(past_request_tree, node), deleted++;
209                 else
210                         left++;
211         }
212
213         if(left || deleted)
214                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Aging past requests: deleted %d, left %d",
215                            deleted, left);
216
217         if(left)
218                 event_add(&past_request_event, &(struct timeval){10, 0});
219 }
220
221 void init_requests(void) {
222         past_request_tree = splay_alloc_tree((splay_compare_t) past_request_compare, (splay_action_t) free_past_request);
223
224         timeout_set(&past_request_event, age_past_requests, NULL);
225 }
226
227 void exit_requests(void) {
228         splay_delete_tree(past_request_tree);
229
230         event_del(&past_request_event);
231 }