K&R style braces
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2006 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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "meta.h"
29 #include "protocol.h"
30 #include "utils.h"
31 #include "xalloc.h"
32
33 bool tunnelserver = false;
34
35 /* Jumptable for the request handlers */
36
37 static bool (*request_handlers[])(connection_t *) = {
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,
44 };
45
46 /* Request names */
47
48 static char (*request_name[]) = {
49                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
50                 "STATUS", "ERROR", "TERMREQ",
51                 "PING", "PONG",
52                 "ADD_SUBNET", "DEL_SUBNET",
53                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET",
54 };
55
56 static avl_tree_t *past_request_tree;
57
58 bool check_id(const char *id) {
59         for(; *id; id++)
60                 if(!isalnum(*id) && *id != '_')
61                         return false;
62
63         return true;
64 }
65
66 /* Generic request routines - takes care of logging and error
67    detection as well */
68
69 bool send_request(connection_t *c, const char *format, ...) {
70         va_list args;
71         char buffer[MAXBUFSIZE];
72         int len, request;
73
74         cp();
75
76         /* Use vsnprintf instead of vasprintf: faster, no memory
77            fragmentation, cleanup is automatic, and there is a limit on the
78            input buffer anyway */
79
80         va_start(args, format);
81         len = vsnprintf(buffer, MAXBUFSIZE, format, args);
82         va_end(args);
83
84         if(len < 0 || len > MAXBUFSIZE - 1) {
85                 logger(LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"),
86                            c->name, c->hostname);
87                 return false;
88         }
89
90         ifdebug(PROTOCOL) {
91                 sscanf(buffer, "%d", &request);
92                 ifdebug(META)
93                         logger(LOG_DEBUG, _("Sending %s to %s (%s): %s"),
94                                    request_name[request], c->name, c->hostname, buffer);
95                 else
96                         logger(LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request],
97                                    c->name, c->hostname);
98         }
99
100         buffer[len++] = '\n';
101
102         if(c == broadcast) {
103                 broadcast_meta(NULL, buffer, len);
104                 return true;
105         } else
106                 return send_meta(c, buffer, len);
107 }
108
109 void forward_request(connection_t *from) {
110         int request;
111
112         cp();
113
114         ifdebug(PROTOCOL) {
115                 sscanf(from->buffer, "%d", &request);
116                 ifdebug(META)
117                         logger(LOG_DEBUG, _("Forwarding %s from %s (%s): %s"),
118                                    request_name[request], from->name, from->hostname,
119                                    from->buffer);
120                 else
121                         logger(LOG_DEBUG, _("Forwarding %s from %s (%s)"),
122                                    request_name[request], from->name, from->hostname);
123         }
124
125         from->buffer[from->reqlen - 1] = '\n';
126
127         broadcast_meta(from, from->buffer, from->reqlen);
128 }
129
130 bool receive_request(connection_t *c) {
131         int request;
132
133         cp();
134
135         if(sscanf(c->buffer, "%d", &request) == 1) {
136                 if((request < 0) || (request >= LAST) || !request_handlers[request]) {
137                         ifdebug(META)
138                                 logger(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
139                                            c->name, c->hostname, c->buffer);
140                         else
141                                 logger(LOG_ERR, _("Unknown request from %s (%s)"),
142                                            c->name, c->hostname);
143
144                         return false;
145                 } else {
146                         ifdebug(PROTOCOL) {
147                                 ifdebug(META)
148                                         logger(LOG_DEBUG, _("Got %s from %s (%s): %s"),
149                                                    request_name[request], c->name, c->hostname,
150                                                    c->buffer);
151                                 else
152                                         logger(LOG_DEBUG, _("Got %s from %s (%s)"),
153                                                    request_name[request], c->name, c->hostname);
154                         }
155                 }
156
157                 if((c->allow_request != ALL) && (c->allow_request != request)) {
158                         logger(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
159                                    c->hostname);
160                         return false;
161                 }
162
163                 if(!request_handlers[request](c)) {
164                         /* Something went wrong. Probably scriptkiddies. Terminate. */
165
166                         logger(LOG_ERR, _("Error while processing %s from %s (%s)"),
167                                    request_name[request], c->name, c->hostname);
168                         return false;
169                 }
170         } else {
171                 logger(LOG_ERR, _("Bogus data received from %s (%s)"),
172                            c->name, c->hostname);
173                 return false;
174         }
175
176         return true;
177 }
178
179 static int past_request_compare(const past_request_t *a, const past_request_t *b) {
180         return strcmp(a->request, b->request);
181 }
182
183 static void free_past_request(past_request_t *r) {
184         cp();
185
186         if(r->request)
187                 free(r->request);
188
189         free(r);
190 }
191
192 static struct event past_request_event;
193
194 bool seen_request(char *request) {
195         past_request_t *new, p = {0};
196
197         cp();
198
199         p.request = request;
200
201         if(avl_search(past_request_tree, &p)) {
202                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Already seen request"));
203                 return true;
204         } else {
205                 new = xmalloc(sizeof(*new));
206                 new->request = xstrdup(request);
207                 new->firstseen = time(NULL);
208                 avl_insert(past_request_tree, new);
209                 event_add(&past_request_event, &(struct timeval){10, 0});
210                 return false;
211         }
212 }
213
214 void age_past_requests(int fd, short events, void *data) {
215         avl_node_t *node, *next;
216         past_request_t *p;
217         int left = 0, deleted = 0;
218         time_t now = time(NULL);
219
220         cp();
221
222         for(node = past_request_tree->head; node; node = next) {
223                 next = node->next;
224                 p = node->data;
225
226                 if(p->firstseen + pinginterval < now)
227                         avl_delete_node(past_request_tree, node), deleted++;
228                 else
229                         left++;
230         }
231
232         if(left || deleted)
233                 ifdebug(SCARY_THINGS) logger(LOG_DEBUG, _("Aging past requests: deleted %d, left %d"),
234                            deleted, left);
235
236         if(left)
237                 event_add(&past_request_event, &(struct timeval){10, 0});
238 }
239
240 void init_requests(void) {
241         cp();
242
243         past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
244
245         timeout_set(&past_request_event, age_past_requests, NULL);
246 }
247
248 void exit_requests(void) {
249         cp();
250
251         avl_delete_tree(past_request_tree);
252
253         event_del(&past_request_event);
254 }