Replace Opaque and Strict options with a TunnelServer option.
[oweals/tinc.git] / src / protocol_subnet.c
1 /*
2     protocol_subnet.c -- handle the meta-protocol, subnets
3     Copyright (C) 1999-2003 Ivo Timmermans <ivo@o2w.nl>,
4                   2000-2003 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_subnet.c,v 1.1.4.17 2003/11/17 15:30:18 guus Exp $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "connection.h"
27 #include "logger.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "node.h"
31 #include "protocol.h"
32 #include "subnet.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 bool send_add_subnet(connection_t *c, const subnet_t *subnet)
37 {
38         bool x;
39         char *netstr;
40
41         cp();
42
43         x = send_request(c, "%d %lx %s %s", ADD_SUBNET, random(),
44                                          subnet->owner->name, netstr = net2str(subnet));
45
46         free(netstr);
47
48         return x;
49 }
50
51 bool add_subnet_h(connection_t *c)
52 {
53         char subnetstr[MAX_STRING_SIZE];
54         char name[MAX_STRING_SIZE];
55         node_t *owner;
56         subnet_t *s;
57
58         cp();
59
60         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
61                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ADD_SUBNET", c->name,
62                            c->hostname);
63                 return false;
64         }
65
66         /* Check if owner name is a valid */
67
68         if(!check_id(name)) {
69                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
70                            c->hostname, _("invalid name"));
71                 return false;
72         }
73
74         /* Check if subnet string is valid */
75
76         s = str2net(subnetstr);
77
78         if(!s) {
79                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ADD_SUBNET", c->name,
80                            c->hostname, _("invalid subnet string"));
81                 return false;
82         }
83
84         if(seen_request(c->buffer))
85                 return true;
86
87         /* Check if the owner of the new subnet is in the connection list */
88
89         owner = lookup_node(name);
90
91         if(!owner) {
92                 owner = new_node();
93                 owner->name = xstrdup(name);
94                 node_add(owner);
95         }
96
97         if(tunnelserver && owner != myself && owner != c->node)
98                 return false;
99
100         /* Check if we already know this subnet */
101
102         if(lookup_subnet(owner, s)) {
103                 free_subnet(s);
104                 return true;
105         }
106
107         /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
108
109         if(owner == myself) {
110                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
111                                    "ADD_SUBNET", c->name, c->hostname);
112                 s->owner = myself;
113                 send_del_subnet(c, s);
114                 return true;
115         }
116
117         /* In tunnel server mode, check if the subnet matches one in the config file of this node */
118
119         if(tunnelserver) {
120                 config_t *cfg;
121                 subnet_t *allowed;
122
123                 for(cfg = lookup_config(c->config_tree, "Subnet"); cfg; cfg = lookup_config_next(c->config_tree, cfg)) {
124                         if(!get_config_subnet(cfg, &allowed))
125                                 return false;
126
127                         if(!subnet_compare(s, allowed))
128                                 break;
129
130                         free_subnet(allowed);
131                 }
132
133                 if(!cfg)
134                         return false;
135
136                 free_subnet(allowed);
137         }
138
139         /* If everything is correct, add the subnet to the list of the owner */
140
141         subnet_add(owner, s);
142
143         /* Tell the rest */
144
145         if(!tunnelserver)
146                 forward_request(c);
147
148         return true;
149 }
150
151 bool send_del_subnet(connection_t *c, const subnet_t *s)
152 {
153         bool x;
154         char *netstr;
155
156         cp();
157
158         netstr = net2str(s);
159
160         x = send_request(c, "%d %lx %s %s", DEL_SUBNET, random(), s->owner->name, netstr);
161
162         free(netstr);
163
164         return x;
165 }
166
167 bool del_subnet_h(connection_t *c)
168 {
169         char subnetstr[MAX_STRING_SIZE];
170         char name[MAX_STRING_SIZE];
171         node_t *owner;
172         subnet_t *s, *find;
173
174         cp();
175
176         if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
177                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "DEL_SUBNET", c->name,
178                            c->hostname);
179                 return false;
180         }
181
182         /* Check if owner name is a valid */
183
184         if(!check_id(name)) {
185                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
186                            c->hostname, _("invalid name"));
187                 return false;
188         }
189
190         /* Check if the owner of the new subnet is in the connection list */
191
192         owner = lookup_node(name);
193
194         if(!owner) {
195                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which is not in our node tree"),
196                                    "DEL_SUBNET", c->name, c->hostname, name);
197                 return true;
198         }
199
200         if(tunnelserver && owner != myself && owner != c->node)
201                 return false;
202
203         /* Check if subnet string is valid */
204
205         s = str2net(subnetstr);
206
207         if(!s) {
208                 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "DEL_SUBNET", c->name,
209                            c->hostname, _("invalid subnet string"));
210                 return false;
211         }
212
213         if(seen_request(c->buffer))
214                 return true;
215
216         /* If everything is correct, delete the subnet from the list of the owner */
217
218         s->owner = owner;
219
220         find = lookup_subnet(owner, s);
221
222         free_subnet(s);
223
224         if(!find) {
225                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for %s which does not appear in his subnet tree"),
226                                    "DEL_SUBNET", c->name, c->hostname, name);
227                 return true;
228         }
229
230         /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
231
232         if(owner == myself) {
233                 ifdebug(PROTOCOL) logger(LOG_WARNING, _("Got %s from %s (%s) for ourself"),
234                                    "DEL_SUBNET", c->name, c->hostname);
235                 send_add_subnet(c, find);
236                 return true;
237         }
238
239         /* Tell the rest */
240
241         if(!tunnelserver)
242                 forward_request(c);
243
244         /* Finally, delete it. */
245
246         subnet_del(owner, find);
247
248         return true;
249 }