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