]> git.meshlink.io Git - meshlink/blob - src/protocol_auth.c
Make the join commit order configurable.
[meshlink] / src / protocol_auth.c
1 /*
2     protocol_auth.c -- handle the meta-protocol, authentication
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "connection.h"
24 #include "ecdsa.h"
25 #include "edge.h"
26 #include "graph.h"
27 #include "logger.h"
28 #include "meshlink_internal.h"
29 #include "meta.h"
30 #include "net.h"
31 #include "netutl.h"
32 #include "node.h"
33 #include "packmsg.h"
34 #include "prf.h"
35 #include "protocol.h"
36 #include "sptps.h"
37 #include "utils.h"
38 #include "xalloc.h"
39 #include "ed25519/sha512.h"
40
41 #include <assert.h>
42
43 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
44
45 static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) {
46         switch(mesh->proxytype) {
47         case PROXY_HTTP: {
48                 char *host;
49                 char *port;
50
51                 sockaddr2str(&c->address, &host, &port);
52                 send_request(mesh, c, NULL, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
53                 free(host);
54                 free(port);
55                 return true;
56         }
57
58         case PROXY_SOCKS4: {
59                 if(c->address.sa.sa_family != AF_INET) {
60                         logger(mesh, MESHLINK_ERROR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
61                         return false;
62                 }
63
64                 char s4req[9 + (mesh->proxyuser ? strlen(mesh->proxyuser) : 0)];
65                 s4req[0] = 4;
66                 s4req[1] = 1;
67                 memcpy(s4req + 2, &c->address.in.sin_port, 2);
68                 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
69
70                 if(mesh->proxyuser) {
71                         memcpy(s4req + 8, mesh->proxyuser, strlen(mesh->proxyuser));
72                 }
73
74                 s4req[sizeof(s4req) - 1] = 0;
75                 c->tcplen = 8;
76                 return send_meta(mesh, c, s4req, sizeof(s4req));
77         }
78
79         case PROXY_SOCKS5: {
80                 int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
81                 c->tcplen = 2;
82
83                 if(mesh->proxypass) {
84                         len += 3 + strlen(mesh->proxyuser) + strlen(mesh->proxypass);
85                 }
86
87                 char s5req[len];
88                 int i = 0;
89                 s5req[i++] = 5;
90                 s5req[i++] = 1;
91
92                 if(mesh->proxypass) {
93                         s5req[i++] = 2;
94                         s5req[i++] = 1;
95                         s5req[i++] = strlen(mesh->proxyuser);
96                         memcpy(s5req + i, mesh->proxyuser, strlen(mesh->proxyuser));
97                         i += strlen(mesh->proxyuser);
98                         s5req[i++] = strlen(mesh->proxypass);
99                         memcpy(s5req + i, mesh->proxypass, strlen(mesh->proxypass));
100                         i += strlen(mesh->proxypass);
101                         c->tcplen += 2;
102                 } else {
103                         s5req[i++] = 0;
104                 }
105
106                 s5req[i++] = 5;
107                 s5req[i++] = 1;
108                 s5req[i++] = 0;
109
110                 if(c->address.sa.sa_family == AF_INET) {
111                         s5req[i++] = 1;
112                         memcpy(s5req + i, &c->address.in.sin_addr, 4);
113                         i += 4;
114                         memcpy(s5req + i, &c->address.in.sin_port, 2);
115                         i += 2;
116                         c->tcplen += 10;
117                 } else if(c->address.sa.sa_family == AF_INET6) {
118                         s5req[i++] = 3;
119                         memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
120                         i += 16;
121                         memcpy(s5req + i, &c->address.in6.sin6_port, 2);
122                         i += 2;
123                         c->tcplen += 22;
124                 } else {
125                         logger(mesh, MESHLINK_ERROR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
126                         return false;
127                 }
128
129                 if(i > len) {
130                         abort();
131                 }
132
133                 return send_meta(mesh, c, s5req, sizeof(s5req));
134         }
135
136         case PROXY_SOCKS4A:
137                 logger(mesh, MESHLINK_ERROR, "Proxy type not implemented yet");
138                 return false;
139
140         default:
141                 logger(mesh, MESHLINK_ERROR, "Unknown proxy type");
142                 return false;
143         }
144 }
145
146 bool send_id(meshlink_handle_t *mesh, connection_t *c) {
147         if(mesh->proxytype && c->outgoing)
148                 if(!send_proxyrequest(mesh, c)) {
149                         return false;
150                 }
151
152         return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
153 }
154
155 static bool commit_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
156         // Create a new node
157         node_t *n = new_node();
158         n->name = xstrdup(c->name);
159         n->devclass = DEV_CLASS_UNKNOWN;
160         n->ecdsa = ecdsa_set_public_key(data);
161         n->submesh = c->submesh;
162
163         // Remember its current address
164         node_add_recent_address(mesh, n, &c->address);
165
166         if(!node_write_config(mesh, n) || !config_sync(mesh, "current")) {
167                 logger(mesh, MESHLINK_ERROR, "Error writing configuration file for invited node %s!\n", c->name);
168                 free_node(n);
169                 return false;
170
171         }
172
173         node_add(mesh, n);
174
175         logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name);
176
177         //TODO: callback to application to inform of an accepted invitation
178
179         sptps_send_record(&c->sptps, 1, "", 0);
180
181         return true;
182 }
183
184 static bool process_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
185         // Recover the filename from the cookie and the key
186         char *fingerprint = ecdsa_get_base64_public_key(mesh->invitation_key);
187         char hash[64];
188         char hashbuf[18 + strlen(fingerprint)];
189         char cookie[25];
190         memcpy(hashbuf, data, 18);
191         memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18);
192         sha512(hashbuf, sizeof(hashbuf), hash);
193         b64encode_urlsafe(hash, cookie, 18);
194         free(fingerprint);
195
196         config_t config;
197
198         if(!invitation_read(mesh, "current", cookie, &config, mesh->config_key)) {
199                 logger(mesh, MESHLINK_ERROR, "Error while trying to read invitation file\n");
200                 return false;
201         }
202
203         // Read the new node's Name from the file
204         packmsg_input_t in = {config.buf, config.len};
205         packmsg_get_uint32(&in); // skip version
206         free(c->name);
207         c->name = packmsg_get_str_dup(&in);
208
209         // Check if the file contains Sub-Mesh information
210         char *submesh_name = packmsg_get_str_dup(&in);
211
212         if(!strcmp(submesh_name, CORE_MESH)) {
213                 free(submesh_name);
214                 c->submesh = NULL;
215         } else {
216                 if(!check_id(submesh_name)) {
217                         logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie);
218                         free(submesh_name);
219                         return false;
220                 }
221
222                 c->submesh = lookup_or_create_submesh(mesh, submesh_name);
223                 free(submesh_name);
224
225                 if(!c->submesh) {
226                         logger(mesh, MESHLINK_ERROR, "Unknown submesh in invitation file %s\n", cookie);
227                         return false;
228                 }
229         }
230
231         if(mesh->inviter_commits_first && !commit_invitation(mesh, c, (const char *)data + 18)) {
232                 return false;
233         }
234
235         // Send the node the contents of the invitation file
236         sptps_send_record(&c->sptps, 0, config.buf, config.len);
237
238         config_free(&config);
239
240         c->status.invitation_used = true;
241
242         logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name);
243         return true;
244 }
245
246 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
247         connection_t *c = handle;
248         meshlink_handle_t *mesh = c->mesh;
249
250         if(type == SPTPS_HANDSHAKE) {
251                 // The peer should send its cookie first.
252                 return true;
253         }
254
255         if(mesh->inviter_commits_first) {
256                 if(type == 2 && len == 18 + 32 && !c->status.invitation_used) {
257                         return process_invitation(mesh, c, data);
258                 }
259         } else {
260                 if(type == 0 && len == 18 && !c->status.invitation_used) {
261                         return process_invitation(mesh, c, data);
262                 } else if(type == 1 && len == 32 && c->status.invitation_used) {
263                         return commit_invitation(mesh, c, data);
264                 }
265         }
266
267         return false;
268 }
269
270 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
271         assert(request);
272         assert(*request);
273
274         char name[MAX_STRING_SIZE];
275
276         if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
277                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
278                 return false;
279         }
280
281         /* Check if this is an invitation  */
282
283         if(name[0] == '?') {
284                 if(!mesh->invitation_key) {
285                         logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
286                         return false;
287                 }
288
289                 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
290
291                 if(!c->ecdsa) {
292                         logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
293                         return false;
294                 }
295
296                 c->status.invitation = true;
297                 char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
298
299                 if(!mykey) {
300                         return false;
301                 }
302
303                 if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
304                         return false;
305                 }
306
307                 free(mykey);
308
309                 c->protocol_minor = 2;
310                 c->allow_request = 1;
311
312                 return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, meshlink_invitation_label, sizeof(meshlink_invitation_label), send_meta_sptps, receive_invitation_sptps);
313         }
314
315         /* Check if identity is a valid name */
316
317         if(!check_id(name)) {
318                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
319                 return false;
320         }
321
322         /* If this is an outgoing connection, make sure we are connected to the right host */
323
324         if(c->outgoing) {
325                 if(strcmp(c->name, name)) {
326                         logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
327                         return false;
328                 }
329         } else {
330                 if(c->name) {
331                         free(c->name);
332                 }
333
334                 c->name = xstrdup(name);
335         }
336
337         /* Check if version matches */
338
339         if(c->protocol_major != PROT_MAJOR) {
340                 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
341                        c->name, c->protocol_major, c->protocol_minor);
342                 return false;
343         }
344
345         /* Check if we know this node */
346
347         node_t *n = lookup_node(mesh, c->name);
348
349         if(!n) {
350                 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
351                 return false;
352         }
353
354         if(n->status.blacklisted) {
355                 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
356                 return false;
357         }
358
359         if(!node_read_public_key(mesh, n)) {
360                 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
361
362                 if(n->status.reachable && !n->status.waitingforkey) {
363                         logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
364                         send_req_key(mesh, n);
365                 }
366
367                 return false;
368         }
369
370         /* Forbid version rollback for nodes whose ECDSA key we know */
371
372         if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
373                 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
374                        c->name, c->protocol_major, c->protocol_minor);
375                 return false;
376         }
377
378         c->allow_request = ACK;
379         char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
380
381         if(c->outgoing) {
382                 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
383         } else {
384                 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
385         }
386
387         char buf1[1024], buf2[1024];
388         bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
389         bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
390         logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
391         return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
392 }
393
394 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
395         return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
396 }
397
398 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
399         /* Send all known subnets and edges */
400
401         for splay_each(node_t, n, mesh->nodes) {
402                 for splay_each(edge_t, e, n->edge_tree) {
403                         send_add_edge(mesh, c, e, 0);
404                 }
405         }
406 }
407
408 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
409         assert(request);
410         assert(*request);
411
412         char hisport[MAX_STRING_SIZE];
413         int devclass;
414         uint32_t options;
415         node_t *n;
416
417         if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
418                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
419                 return false;
420         }
421
422         if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
423                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
424                 return false;
425         }
426
427         /* Check if we already have a node_t for him */
428
429         n = lookup_node(mesh, c->name);
430
431         if(!n) {
432                 n = new_node();
433                 n->name = xstrdup(c->name);
434                 node_add(mesh, n);
435         } else {
436                 if(n->connection) {
437                         /* Oh dear, we already have a connection to this node. */
438                         logger(mesh, MESHLINK_DEBUG, "Established a second connection with %s, closing old connection", n->connection->name);
439
440                         if(n->connection->outgoing) {
441                                 if(c->outgoing) {
442                                         logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
443                                 } else {
444                                         c->outgoing = n->connection->outgoing;
445                                 }
446
447                                 n->connection->outgoing = NULL;
448                         }
449
450                         /* Remove the edge before terminating the connection, to prevent a graph update. */
451                         edge_del(mesh, n->connection->edge);
452                         n->connection->edge = NULL;
453
454                         terminate_connection(mesh, n->connection, false);
455                 }
456         }
457
458         n->devclass = devclass;
459         n->status.dirty = true;
460
461         n->last_successfull_connection = mesh->loop.now.tv_sec;
462
463         n->connection = c;
464         c->node = n;
465
466         /* Activate this connection */
467
468         c->allow_request = ALL;
469         c->status.active = true;
470
471         logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
472
473         /* Send him everything we know */
474
475         send_everything(mesh, c);
476
477         /* Create an edge_t for this connection */
478
479         assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
480
481         c->edge = new_edge();
482         c->edge->from = mesh->self;
483         c->edge->to = n;
484         sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
485         c->edge->weight = mesh->dev_class_traits[devclass].edge_weight;
486         c->edge->connection = c;
487
488         edge_add(mesh, c->edge);
489
490         /* Notify everyone of the new edge */
491
492         send_add_edge(mesh, mesh->everyone, c->edge, 0);
493
494         /* Run MST and SSSP algorithms */
495
496         graph(mesh);
497
498         return true;
499 }