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