Revert to edge and graph stuff. This time, use a directed graph.
[tinc] / src / protocol_key.c
1 /*
2     protocol_key.c -- handle the meta-protocol, key exchange
3     Copyright (C) 1999-2002 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2002 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_key.c,v 1.1.4.9 2002/09/04 08:02:33 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <errno.h>
31
32 #include <utils.h>
33 #include <xalloc.h>
34 #include <avl_tree.h>
35
36 #include "conf.h"
37 #include "net.h"
38 #include "netutl.h"
39 #include "protocol.h"
40 #include "meta.h"
41 #include "connection.h"
42 #include "node.h"
43
44 #include "system.h"
45
46 int mykeyused = 0;
47
48 int send_key_changed(connection_t *c, node_t *n)
49 {
50 cp
51   /* Only send this message if some other daemon requested our key previously.
52      This reduces unnecessary key_changed broadcasts.
53   */
54
55   if(n == myself && !mykeyused)
56     return 0;
57
58   send_request(NULL, "%d %lx %s", KEY_CHANGED, random(), n->name);
59 cp
60   return 0;
61 }
62
63 int key_changed_h(connection_t *c)
64 {
65   char name[MAX_STRING_SIZE];
66   avl_node_t *node;
67   connection_t *other;
68   node_t *n;
69 cp
70   if(sscanf(c->buffer, "%*d %*x "MAX_STRING, name) != 1)
71     {
72       syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "KEY_CHANGED",
73              c->name, c->hostname);
74       return -1;
75     }
76
77   if(seen_request(c->buffer))
78     return 0;
79
80   n = lookup_node(name);
81
82   if(!n)
83     {
84       syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist"), "KEY_CHANGED",
85              c->name, c->hostname, name);
86       return -1;
87     }
88
89   n->status.validkey = 0;
90   n->status.waitingforkey = 0;
91
92   /* Tell the others */
93
94   for(node = connection_tree->head; node; node = node->next)
95     {
96       other = (connection_t *)node->data;
97       if(other->status.active && other != c)
98         send_request(other, "%s", c->buffer);
99     }
100 cp
101   return 0;
102 }
103
104 int send_req_key(connection_t *c, node_t *from, node_t *to)
105 {
106 cp
107   return send_request(c, "%d %s %s", REQ_KEY,
108                       from->name, to->name);
109 }
110
111 int req_key_h(connection_t *c)
112 {
113   char from_name[MAX_STRING_SIZE];
114   char to_name[MAX_STRING_SIZE];
115   node_t *from, *to;
116 cp
117   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING, from_name, to_name) != 2)
118     {
119        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "REQ_KEY",
120               c->name, c->hostname);
121        return -1;
122     }
123
124   from = lookup_node(from_name);
125
126   if(!from)
127     {
128       syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "REQ_KEY",
129              c->name, c->hostname, from_name);
130       return -1;
131     }
132
133   to = lookup_node(to_name);
134   
135   if(!to)
136     {
137       syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "REQ_KEY",
138              c->name, c->hostname, to_name);
139       return -1;
140     }
141
142   /* Check if this key request is for us */
143
144   if(to == myself)      /* Yes, send our own key back */
145     {
146       mykeyused = 1;
147       from->sent_seqno = 0;
148       send_ans_key(c, myself, from);
149     }
150   else
151     {
152 /* Proxy keys
153       if(to->status.validkey)
154         {
155           send_ans_key(c, to, from);
156         }
157       else
158 */
159         send_req_key(to->nexthop->connection, from, to);
160     }
161
162 cp
163   return 0;
164 }
165
166 int send_ans_key(connection_t *c, node_t *from, node_t *to)
167 {
168   char key[MAX_STRING_SIZE];
169 cp
170   bin2hex(from->key, key, from->keylength);
171   key[from->keylength * 2] = '\0';
172 cp
173   return send_request(c, "%d %s %s %s %d %d %d %d", ANS_KEY,
174                       from->name, to->name, key, from->cipher?from->cipher->nid:0, from->digest?from->digest->type:0, from->maclength, from->compression);
175 }
176
177 int ans_key_h(connection_t *c)
178 {
179   char from_name[MAX_STRING_SIZE];
180   char to_name[MAX_STRING_SIZE];
181   char key[MAX_STRING_SIZE];
182   int cipher, digest, maclength, compression;
183   node_t *from, *to;
184 cp
185   if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d", from_name, to_name, key, &cipher, &digest, &maclength, &compression) != 7)
186     {
187        syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY",
188               c->name, c->hostname);
189        return -1;
190     }
191
192   from = lookup_node(from_name);
193
194   if(!from)
195     {
196       syslog(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"), "ANS_KEY",
197              c->name, c->hostname, from_name);
198       return -1;
199     }
200
201   to = lookup_node(to_name);
202
203   if(!to)
204     {
205       syslog(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"), "ANS_KEY",
206              c->name, c->hostname, to_name);
207       return -1;
208     }
209
210   /* Forward it if necessary */
211
212   if(to != myself)
213     {
214       return send_request(to->nexthop->connection, "%s", c->buffer);
215     }
216
217   /* Update our copy of the origin's packet key */
218
219   if(from->key)
220     free(from->key);
221
222   from->key = xstrdup(key);
223   from->keylength = strlen(key) / 2;
224   hex2bin(from->key, from->key, from->keylength);
225   from->key[from->keylength] = '\0';
226
227   from->status.validkey = 1;
228   from->status.waitingforkey = 0;
229   from->received_seqno = 0;
230     
231   /* Check and lookup cipher and digest algorithms */
232
233   if(cipher)
234     {
235       from->cipher = EVP_get_cipherbynid(cipher);
236       if(!from->cipher)
237         {
238           syslog(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname);
239           return -1;
240         }
241       if(from->keylength != from->cipher->key_len + from->cipher->iv_len)
242         {
243           syslog(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname);
244           return -1;
245         }
246     }
247   else
248     {
249       from->cipher = NULL;
250     }
251
252   from->maclength = maclength;
253
254   if(digest)
255     {
256       from->digest = EVP_get_digestbynid(digest);
257       if(!from->digest)
258         {
259           syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
260           return -1;
261         }
262       if(from->maclength > from->digest->md_size || from->maclength < 0)
263         {
264           syslog(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
265           return -1;
266         }
267     }
268   else
269     {
270       from->digest = NULL;
271     }
272
273   from->compression = compression;
274   
275   flush_queue(from);
276 cp
277   return 0;
278 }