a64854751d286e0f0af5272d0234d0bf80c2525c
[oweals/tinc.git] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2001 Ivo Timmermans <ivo@o2w.nl>,
4                   2000,2001 Guus Sliepen <guus@sliepen.eu.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: protocol.c,v 1.28.4.139 2003/07/06 23:16:28 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <errno.h>
32
33 #include <utils.h>
34 #include <xalloc.h>
35
36 #include "conf.h"
37 #include "protocol.h"
38 #include "meta.h"
39 #include "connection.h"
40 #include "logger.h"
41
42 #include "system.h"
43
44 /* Jumptable for the request handlers */
45
46 static int (*request_handlers[])(connection_t *) = {
47                 id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
48                 status_h, error_h, termreq_h,
49                 ping_h, pong_h,
50                 add_subnet_h, del_subnet_h,
51                 add_edge_h, del_edge_h,
52                 key_changed_h, req_key_h, ans_key_h, tcppacket_h,
53 };
54
55 /* Request names */
56
57 static char (*request_name[]) = {
58                 "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
59                 "STATUS", "ERROR", "TERMREQ",
60                 "PING", "PONG",
61                 "ADD_SUBNET", "DEL_SUBNET",
62                 "ADD_EDGE", "DEL_EDGE", "KEY_CHANGED", "REQ_KEY", "ANS_KEY", "PACKET",
63 };
64
65 static avl_tree_t *past_request_tree;
66
67 int check_id(char *id)
68 {
69         int i;
70
71         for(i = 0; i < strlen(id); i++)
72                 if(!isalnum(id[i]) && id[i] != '_')
73                         return -1;
74
75         return 0;
76 }
77
78 /* Generic request routines - takes care of logging and error
79    detection as well */
80
81 int send_request(connection_t *c, const char *format, ...)
82 {
83         va_list args;
84         char buffer[MAXBUFSIZE];
85         int len, request;
86
87         cp();
88
89         /* Use vsnprintf instead of vasprintf: faster, no memory
90            fragmentation, cleanup is automatic, and there is a limit on the
91            input buffer anyway */
92
93         va_start(args, format);
94         len = vsnprintf(buffer, MAXBUFSIZE, format, args);
95         va_end(args);
96
97         if(len < 0 || len > MAXBUFSIZE - 1) {
98                 logger(DEBUG_ALWAYS, LOG_ERR, _("Output buffer overflow while sending request to %s (%s)"),
99                            c->name, c->hostname);
100                 return -1;
101         }
102
103         if(debug_level >= DEBUG_PROTOCOL) {
104                 sscanf(buffer, "%d", &request);
105                 if(debug_level >= DEBUG_META)
106                         logger(DEBUG_ALWAYS, LOG_DEBUG, _("Sending %s to %s (%s): %s"),
107                                    request_name[request], c->name, c->hostname, buffer);
108                 else
109                         logger(DEBUG_ALWAYS, LOG_DEBUG, _("Sending %s to %s (%s)"), request_name[request],
110                                    c->name, c->hostname);
111         }
112
113         buffer[len++] = '\n';
114
115         if(c == broadcast)
116                 return broadcast_meta(NULL, buffer, len);
117         else
118                 return send_meta(c, buffer, len);
119 }
120
121 int forward_request(connection_t *from)
122 {
123         int request;
124         cp();
125
126         cp();
127
128         if(debug_level >= DEBUG_PROTOCOL) {
129                 sscanf(from->buffer, "%d", &request);
130                 if(debug_level >= DEBUG_META)
131                         logger(DEBUG_ALWAYS, LOG_DEBUG, _("Forwarding %s from %s (%s): %s"),
132                                    request_name[request], from->name, from->hostname,
133                                    from->buffer);
134                 else
135                         logger(DEBUG_ALWAYS, LOG_DEBUG, _("Forwarding %s from %s (%s)"),
136                                    request_name[request], from->name, from->hostname);
137         }
138
139         from->buffer[from->reqlen - 1] = '\n';
140
141         return broadcast_meta(from, from->buffer, from->reqlen);
142 }
143
144 int receive_request(connection_t *c)
145 {
146         int request;
147
148         cp();
149
150         if(sscanf(c->buffer, "%d", &request) == 1) {
151                 if((request < 0) || (request >= LAST) || !request_handlers[request]) {
152                         if(debug_level >= DEBUG_META)
153                                 logger(DEBUG_ALWAYS, LOG_DEBUG, _("Unknown request from %s (%s): %s"),
154                                            c->name, c->hostname, c->buffer);
155                         else
156                                 logger(DEBUG_ALWAYS, LOG_ERR, _("Unknown request from %s (%s)"),
157                                            c->name, c->hostname);
158
159                         return -1;
160                 } else {
161                         if(debug_level >= DEBUG_PROTOCOL) {
162                                 if(debug_level >= DEBUG_META)
163                                         logger(DEBUG_ALWAYS, LOG_DEBUG, _("Got %s from %s (%s): %s"),
164                                                    request_name[request], c->name, c->hostname,
165                                                    c->buffer);
166                                 else
167                                         logger(DEBUG_ALWAYS, LOG_DEBUG, _("Got %s from %s (%s)"),
168                                                    request_name[request], c->name, c->hostname);
169                         }
170                 }
171
172                 if((c->allow_request != ALL) && (c->allow_request != request)) {
173                         logger(DEBUG_ALWAYS, LOG_ERR, _("Unauthorized request from %s (%s)"), c->name,
174                                    c->hostname);
175                         return -1;
176                 }
177
178                 if(request_handlers[request] (c))
179                         /* Something went wrong. Probably scriptkiddies. Terminate. */
180                 {
181                         logger(DEBUG_ALWAYS, LOG_ERR, _("Error while processing %s from %s (%s)"),
182                                    request_name[request], c->name, c->hostname);
183                         return -1;
184                 }
185         } else {
186                 logger(DEBUG_ALWAYS, LOG_ERR, _("Bogus data received from %s (%s)"),
187                            c->name, c->hostname);
188                 return -1;
189         }
190
191         return 0;
192 }
193
194 static int past_request_compare(past_request_t *a, past_request_t *b)
195 {
196         return strcmp(a->request, b->request);
197 }
198
199 static void free_past_request(past_request_t *r)
200 {
201         cp();
202
203         if(r->request)
204                 free(r->request);
205
206         free(r);
207 }
208
209 void init_requests(void)
210 {
211         cp();
212
213         past_request_tree = avl_alloc_tree((avl_compare_t) past_request_compare, (avl_action_t) free_past_request);
214 }
215
216 void exit_requests(void)
217 {
218         cp();
219
220         avl_delete_tree(past_request_tree);
221 }
222
223 int seen_request(char *request)
224 {
225         past_request_t p, *new;
226
227         cp();
228
229         p.request = request;
230
231         if(avl_search(past_request_tree, &p)) {
232                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, _("Already seen request"));
233                 return 1;
234         } else {
235                 new = (past_request_t *) xmalloc(sizeof(*new));
236                 new->request = xstrdup(request);
237                 new->firstseen = now;
238                 avl_insert(past_request_tree, new);
239                 return 0;
240         }
241 }
242
243 void age_past_requests(void)
244 {
245         avl_node_t *node, *next;
246         past_request_t *p;
247         int left = 0, deleted = 0;
248
249         cp();
250
251         for(node = past_request_tree->head; node; node = next) {
252                 next = node->next;
253                 p = (past_request_t *) node->data;
254
255                 if(p->firstseen + pingtimeout < now)
256                         avl_delete_node(past_request_tree, node), deleted++;
257                 else
258                         left++;
259         }
260
261         if(left || deleted)
262                 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, _("Aging past requests: deleted %d, left %d\n"),
263                            deleted, left);
264 }