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