4e63728b57755fffcece313d7ba26889615d86f8
[tinc] / src / protocol.c
1 /*
2     protocol.c -- handle the meta-protocol, basic functions
3     Copyright (C) 1999-2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4                   2000,2001 Guus Sliepen <guus@sliepen.warande.net>
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.124 2002/03/21 23:11: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)"), request_name[request], 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   return send_meta(c, buffer, len);
93 }
94
95 int receive_request(connection_t *c)
96 {
97   int request;
98 cp
99   if(sscanf(c->buffer, "%d", &request) == 1)
100     {
101       if((request < 0) || (request >= LAST) || (request_handlers[request] == NULL))
102         {
103           if(debug_lvl >= DEBUG_META)
104             syslog(LOG_DEBUG, _("Unknown request from %s (%s): %s"),
105                    c->name, c->hostname, c->buffer);
106           else
107             syslog(LOG_ERR, _("Unknown request from %s (%s)"),
108                    c->name, c->hostname);
109                    
110           return -1;
111         }
112       else
113         {
114           if(debug_lvl >= DEBUG_PROTOCOL)
115             {
116               if(debug_lvl >= DEBUG_META)
117                 syslog(LOG_DEBUG, _("Got %s from %s (%s): %s"),
118                        request_name[request], c->name, c->hostname, c->buffer);
119               else
120                 syslog(LOG_DEBUG, _("Got %s from %s (%s)"),
121                        request_name[request], c->name, c->hostname);
122             }
123         }
124
125       if((c->allow_request != ALL) && (c->allow_request != request))
126         {
127           syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), c->name, c->hostname);
128           return -1;
129         }
130
131       if(request_handlers[request](c))
132         /* Something went wrong. Probably scriptkiddies. Terminate. */
133         {
134           syslog(LOG_ERR, _("Error while processing %s from %s (%s)"),
135                  request_name[request], c->name, c->hostname);
136           return -1;
137         }
138     }
139   else
140     {
141       syslog(LOG_ERR, _("Bogus data received from %s (%s)"),
142              c->name, c->hostname);
143       return -1;
144     }
145 cp
146   return 0;
147 }
148
149 int request_compare(past_request_t *a, past_request_t *b)
150 {
151 cp
152   return strcmp(a->request, b->request);
153 }
154
155 void init_requests(void)
156 {
157 cp
158   past_request_tree = avl_alloc_tree((avl_compare_t)request_compare, (avl_action_t)free);
159 cp
160 }
161
162 void exit_request(void)
163 {
164 cp
165   avl_delete_tree(past_request_tree);
166 cp
167 }
168
169 int seen_request(char *request)
170 {
171   past_request_t p, *new;
172 cp
173   p.request = request;
174
175   if(avl_search(past_request_tree, &p))
176     return 1;
177   else
178     {
179       new = (past_request_t *)xmalloc(sizeof(*new));
180       new->request = xstrdup(request);
181       new->firstseen = now;
182       avl_insert(past_request_tree, new);
183       return 0;
184     }
185 cp  
186 }
187
188 void age_past_requests(void)
189 {
190   avl_node_t *node, *next;
191   past_request_t *p;
192   int left = 0, deleted = 0;
193 cp 
194   for(node = past_request_tree->head; node; node = next)
195     {
196       next = node->next;
197       p = (past_request_t *)node->data;
198       if(p->firstseen + pingtimeout < now)
199         avl_delete_node(past_request_tree, node), deleted++;
200       else
201         left++;
202     }
203
204   if(debug_lvl >= DEBUG_SCARY_THINGS && left + deleted)
205     syslog(LOG_DEBUG, _("Aging past requests: deleted %d, left %d\n"), deleted, left);
206 cp
207 }
208
209 /* Jumptable for the request handlers */
210
211 int (*request_handlers[])(connection_t*) = {
212   id_h, metakey_h, challenge_h, chal_reply_h, ack_h,
213   status_h, error_h, termreq_h,
214   ping_h, pong_h,
215 //  add_node_h, del_node_h,
216   add_subnet_h, del_subnet_h,
217   add_edge_h, del_edge_h,
218   key_changed_h, req_key_h, ans_key_h,
219   tcppacket_h,
220 };
221
222 /* Request names */
223
224 char (*request_name[]) = {
225   "ID", "METAKEY", "CHALLENGE", "CHAL_REPLY", "ACK",
226   "STATUS", "ERROR", "TERMREQ",
227   "PING", "PONG",
228 //  "ADD_NODE", "DEL_NODE",
229   "ADD_SUBNET", "DEL_SUBNET",
230   "ADD_EDGE", "DEL_EDGE",
231   "KEY_CHANGED", "REQ_KEY", "ANS_KEY",
232   "PACKET",
233 };