2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include "connection.h"
31 #include "meshlink_internal.h"
41 #include "ed25519/sha512.h"
43 ecdsa_t *invitation_key = NULL;
45 static bool send_proxyrequest(connection_t *c) {
51 sockaddr2str(&c->address, &host, &port);
52 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
58 if(c->address.sa.sa_family != AF_INET) {
59 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
62 char s4req[9 + (proxyuser ? strlen(proxyuser) : 0)];
65 memcpy(s4req + 2, &c->address.in.sin_port, 2);
66 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
68 memcpy(s4req + 8, proxyuser, strlen(proxyuser));
69 s4req[sizeof s4req - 1] = 0;
71 return send_meta(c, s4req, sizeof s4req);
74 int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
77 len += 3 + strlen(proxyuser) + strlen(proxypass);
85 s5req[i++] = strlen(proxyuser);
86 memcpy(s5req + i, proxyuser, strlen(proxyuser));
87 i += strlen(proxyuser);
88 s5req[i++] = strlen(proxypass);
89 memcpy(s5req + i, proxypass, strlen(proxypass));
90 i += strlen(proxypass);
98 if(c->address.sa.sa_family == AF_INET) {
100 memcpy(s5req + i, &c->address.in.sin_addr, 4);
102 memcpy(s5req + i, &c->address.in.sin_port, 2);
105 } else if(c->address.sa.sa_family == AF_INET6) {
107 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
109 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
113 logger(DEBUG_ALWAYS, LOG_ERR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
118 return send_meta(c, s5req, sizeof s5req);
121 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
126 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
131 bool send_id(connection_t *c) {
132 gettimeofday(&c->start, NULL);
134 int minor = mesh->self->connection->protocol_minor;
136 if(proxytype && c->outgoing)
137 if(!send_proxyrequest(c))
140 return send_request(c, "%d %s %d.%d", ID, mesh->self->connection->name, mesh->self->connection->protocol_major, minor);
143 static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
144 if(strchr(data, '\n')) {
145 logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
149 // Create a new host config file
150 char filename[PATH_MAX];
151 snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, c->name);
152 if(!access(filename, F_OK)) {
153 logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
157 FILE *f = fopen(filename, "w");
159 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
163 fprintf(f, "ECDSAPublicKey = %s\n", data);
166 logger(DEBUG_CONNECTIONS, LOG_INFO, "Key succesfully received from %s (%s)", c->name, c->hostname);
168 //TODO: callback to application to inform of an accepted invitation
170 sptps_send_record(&c->sptps, 2, data, 0);
174 static bool receive_invitation_sptps(void *handle, uint8_t type, const char *data, uint16_t len) {
175 connection_t *c = handle;
180 if(type == 1 && c->status.invitation_used)
181 return finalize_invitation(c, data, len);
183 if(type != 0 || len != 18 || c->status.invitation_used)
186 // Recover the filename from the cookie and the key
187 char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
189 char hashbuf[18 + strlen(fingerprint)];
191 memcpy(hashbuf, data, 18);
192 memcpy(hashbuf + 18, fingerprint, sizeof hashbuf - 18);
193 sha512(hashbuf, sizeof hashbuf, hash);
194 b64encode_urlsafe(hash, cookie, 18);
197 char filename[PATH_MAX], usedname[PATH_MAX];
198 snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookie);
199 snprintf(usedname, sizeof usedname, "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, cookie);
201 // Atomically rename the invitation file
202 if(rename(filename, usedname)) {
204 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
206 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
210 // Open the renamed file
211 FILE *f = fopen(usedname, "r");
213 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
217 // Read the new node's Name from the file
219 fgets(buf, sizeof buf, f);
221 buf[strlen(buf) - 1] = 0;
223 len = strcspn(buf, " \t=");
224 char *name = buf + len;
225 name += strspn(name, " \t");
228 name += strspn(name, " \t");
232 if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name)) {
233 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
239 c->name = xstrdup(name);
241 // Send the node the contents of the invitation file
244 while((result = fread(buf, 1, sizeof buf, f)))
245 sptps_send_record(&c->sptps, 0, buf, result);
246 sptps_send_record(&c->sptps, 1, buf, 0);
250 c->status.invitation_used = true;
252 logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s succesfully sent to %s (%s)", cookie, c->name, c->hostname);
256 bool id_h(connection_t *c, const char *request) {
257 char name[MAX_STRING_SIZE];
259 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
265 /* Check if this is an invitation */
268 if(!invitation_key) {
269 logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
273 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
275 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
279 c->status.invitation = true;
280 char *mykey = ecdsa_get_base64_public_key(invitation_key);
283 if(!send_request(c, "%d %s", ACK, mykey))
287 c->protocol_minor = 2;
289 return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
292 /* Check if identity is a valid name */
294 if(!check_id(name)) {
295 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
296 c->hostname, "invalid name");
300 /* If this is an outgoing connection, make sure we are connected to the right host */
303 if(strcmp(c->name, name)) {
304 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
311 c->name = xstrdup(name);
314 /* Check if version matches */
316 if(c->protocol_major != mesh->self->connection->protocol_major) {
317 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
318 c->name, c->hostname, c->protocol_major, c->protocol_minor);
322 if(bypass_security) {
324 init_configuration(&c->config_tree);
325 c->allow_request = ACK;
329 if(!c->config_tree) {
330 init_configuration(&c->config_tree);
332 if(!read_host_config(c->config_tree, c->name)) {
333 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
337 read_ecdsa_public_key(c);
339 if(c->protocol_minor && !ecdsa_active(c->ecdsa))
340 c->protocol_minor = 1;
343 /* Forbid version rollback for nodes whose ECDSA key we know */
345 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
346 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
347 c->name, c->hostname, c->protocol_major, c->protocol_minor);
351 c->allow_request = ACK;
352 char label[25 + strlen(mesh->self->name) + strlen(c->name)];
355 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", mesh->self->name, c->name);
357 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, mesh->self->name);
359 return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
362 bool send_ack(connection_t *c) {
363 /* ACK message contains rest of the information the other end needs
364 to create node_t and edge_t structures. */
369 /* Estimate weight */
371 gettimeofday(&now, NULL);
372 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
374 /* Check some options */
376 if(mesh->self->options & OPTION_PMTU_DISCOVERY)
377 c->options |= OPTION_PMTU_DISCOVERY;
379 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (PROT_MINOR << 24));
382 static void send_everything(connection_t *c) {
383 /* Send all known subnets and edges */
386 if(disablebuggypeers) {
389 char pad[MAXBUFSIZE - MAXSIZE];
392 memset(&zeropkt, 0, sizeof zeropkt);
393 zeropkt.pkt.len = MAXBUFSIZE;
394 send_tcppacket(c, &zeropkt.pkt);
397 for splay_each(node_t, n, mesh->nodes) {
398 for splay_each(edge_t, e, n->edge_tree)
403 bool ack_h(connection_t *c, const char *request) {
404 char hisport[MAX_STRING_SIZE];
411 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
412 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
417 /* Check if we already have a node_t for him */
419 n = lookup_node(c->name);
423 n->name = xstrdup(c->name);
427 /* Oh dear, we already have a connection to this node. */
428 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
430 if(n->connection->outgoing) {
432 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
434 c->outgoing = n->connection->outgoing;
436 n->connection->outgoing = NULL;
439 terminate_connection(n->connection, false);
440 /* Run graph algorithm to keep things in sync */
447 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
448 c->options &= ~OPTION_PMTU_DISCOVERY;
449 options &= ~OPTION_PMTU_DISCOVERY;
451 c->options |= options;
453 /* Activate this connection */
455 c->allow_request = ALL;
456 c->status.active = true;
458 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
461 /* Send him everything we know */
465 /* Create an edge_t for this connection */
467 c->edge = new_edge();
468 c->edge->from = mesh->self;
470 sockaddr2str(&c->address, &hisaddress, NULL);
471 c->edge->address = str2sockaddr(hisaddress, hisport);
473 c->edge->weight = (weight + c->estimated_weight) / 2;
474 c->edge->connection = c;
475 c->edge->options = c->options;
479 /* Notify everyone of the new edge */
481 send_add_edge(everyone, c->edge);
483 /* Run MST and SSSP algorithms */