]> git.meshlink.io Git - meshlink-tiny/blobdiff - src/protocol_auth.c
Ensure we exchange a session key for application data exchange.
[meshlink-tiny] / src / protocol_auth.c
index a0d350d5b9ff83700439b02802ad5882c739b668..573da14817126e6876bed2d15397f747adc2d815 100644 (file)
@@ -23,8 +23,6 @@
 #include "connection.h"
 #include "devtools.h"
 #include "ecdsa.h"
-#include "edge.h"
-#include "graph.h"
 #include "logger.h"
 #include "meshlink_internal.h"
 #include "meta.h"
 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
 
 bool send_id(meshlink_handle_t *mesh, connection_t *c) {
-       return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
-}
-
-static bool commit_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
-       // Check if the node is known
-       node_t *n = lookup_node(mesh, c->name);
-
-       if(n) {
-               if(n->status.blacklisted) {
-                       logger(mesh, MESHLINK_ERROR, "Invitee %s is blacklisted", c->name);
-               } else {
-                       logger(mesh, MESHLINK_ERROR, "Invitee %s already known", c->name);
-               }
-
-               return false;
-       }
-
-       // Create a new node
-       n = new_node();
-       n->name = xstrdup(c->name);
-       n->devclass = DEV_CLASS_UNKNOWN;
-       n->ecdsa = ecdsa_set_public_key(data);
-       n->submesh = c->submesh;
-
-       // Remember its current address
-       node_add_recent_address(mesh, n, &c->address);
-
-       if(!node_write_config(mesh, n, true) || !config_sync(mesh, "current")) {
-               logger(mesh, MESHLINK_ERROR, "Error writing configuration file for invited node %s!\n", c->name);
-               free_node(n);
-               return false;
-
-       }
-
-       node_add(mesh, n);
-
-       logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name);
-
-       //TODO: callback to application to inform of an accepted invitation
-
-       sptps_send_record(&c->sptps, 1, "", 0);
-
-       return true;
-}
-
-static bool process_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
-       // Recover the filename from the cookie and the key
-       char *fingerprint = ecdsa_get_base64_public_key(mesh->invitation_key);
-       char hash[64];
-       char hashbuf[18 + strlen(fingerprint)];
-       char cookie[25];
-       memcpy(hashbuf, data, 18);
-       memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18);
-       sha512(hashbuf, sizeof(hashbuf), hash);
-       b64encode_urlsafe(hash, cookie, 18);
-       free(fingerprint);
-
-       config_t config;
-
-       if(!invitation_read(mesh, "current", cookie, &config, mesh->config_key)) {
-               logger(mesh, MESHLINK_ERROR, "Error while trying to read invitation file\n");
-               return false;
-       }
-
-       // Read the new node's Name from the file
-       packmsg_input_t in = {config.buf, config.len};
-       packmsg_get_uint32(&in); // skip version
-       free(c->name);
-       c->name = packmsg_get_str_dup(&in);
-
-       // Check if the file contains Sub-Mesh information
-       char *submesh_name = packmsg_get_str_dup(&in);
-
-       if(!strcmp(submesh_name, CORE_MESH)) {
-               free(submesh_name);
-               c->submesh = NULL;
-       } else {
-               if(!check_id(submesh_name)) {
-                       logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie);
-                       free(submesh_name);
-                       return false;
-               }
-
-               c->submesh = lookup_or_create_submesh(mesh, submesh_name);
-               free(submesh_name);
-
-               if(!c->submesh) {
-                       logger(mesh, MESHLINK_ERROR, "Unknown submesh in invitation file %s\n", cookie);
-                       return false;
-               }
-       }
-
-       if(mesh->inviter_commits_first && !commit_invitation(mesh, c, (const char *)data + 18)) {
-               return false;
-       }
-
-       if(mesh->inviter_commits_first) {
-               devtool_set_inviter_commits_first(true);
-       }
-
-       // Send the node the contents of the invitation file
-       sptps_send_record(&c->sptps, 0, config.buf, config.len);
-
-       config_free(&config);
-
-       c->status.invitation_used = true;
-
-       logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name);
-       return true;
-}
-
-static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
-       connection_t *c = handle;
-       meshlink_handle_t *mesh = c->mesh;
-
-       // Extend the time for the invitation exchange upon receiving a valid message
-       c->last_ping_time = mesh->loop.now.tv_sec;
-
-       if(type == SPTPS_HANDSHAKE) {
-               // The peer should send its cookie first.
-               return true;
-       }
-
-       if(mesh->inviter_commits_first) {
-               if(type == 2 && len == 18 + 32 && !c->status.invitation_used) {
-                       return process_invitation(mesh, c, data);
-               }
-       } else {
-               if(type == 0 && len == 18 && !c->status.invitation_used) {
-                       return process_invitation(mesh, c, data);
-               } else if(type == 1 && len == 32 && c->status.invitation_used) {
-                       return commit_invitation(mesh, c, data);
-               }
-       }
-
-       return false;
+       return send_request(mesh, c, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
 }
 
 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
@@ -193,41 +56,6 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                return false;
        }
 
-       /* Check if this is an invitation  */
-
-       if(name[0] == '?') {
-               if(!mesh->invitation_key) {
-                       logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
-                       return false;
-               }
-
-               c->ecdsa = ecdsa_set_base64_public_key(name + 1);
-
-               if(!c->ecdsa) {
-                       logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
-                       return false;
-               }
-
-               c->status.invitation = true;
-               char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
-
-               if(!mykey) {
-                       return false;
-               }
-
-               if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
-                       return false;
-               }
-
-               free(mykey);
-
-               c->protocol_minor = 2;
-               c->allow_request = 1;
-               c->last_ping_time = mesh->loop.now.tv_sec;
-
-               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);
-       }
-
        /* Check if identity is a valid name */
 
        if(!check_id(name)) {
@@ -315,17 +143,7 @@ bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
        }
 
        c->last_ping_time = mesh->loop.now.tv_sec;
-       return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
-}
-
-static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
-       /* Send all known subnets and edges */
-
-       for splay_each(node_t, n, mesh->nodes) {
-               for inner_splay_each(edge_t, e, n->edge_tree) {
-                       send_add_edge(mesh, c, e, 0);
-               }
-       }
+       return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
 }
 
 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
@@ -355,27 +173,6 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                n = new_node();
                n->name = xstrdup(c->name);
                node_add(mesh, n);
-       } else {
-               if(n->connection) {
-                       /* Oh dear, we already have a connection to this node. */
-                       logger(mesh, MESHLINK_INFO, "Established a second connection with %s, closing old connection", n->connection->name);
-
-                       if(n->connection->outgoing) {
-                               if(c->outgoing) {
-                                       logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
-                               } else {
-                                       c->outgoing = n->connection->outgoing;
-                               }
-
-                               n->connection->outgoing = NULL;
-                       }
-
-                       /* Remove the edge before terminating the connection, to prevent a graph update. */
-                       edge_del(mesh, n->connection->edge);
-                       n->connection->edge = NULL;
-
-                       terminate_connection(mesh, n->connection, false);
-               }
        }
 
        n->devclass = devclass;
@@ -399,50 +196,9 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                mesh->meta_status_cb(mesh, (meshlink_node_t *)n, true);
        }
 
-       /*  Terminate any connections to this node that are not activated yet */
-
-       for list_each(connection_t, other, mesh->connections) {
-               if(!other->status.active && !strcmp(other->name, c->name)) {
-                       if(other->outgoing) {
-                               if(c->outgoing) {
-                                       logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
-                               } else {
-                                       c->outgoing = other->outgoing;
-                               }
-
-                               other->outgoing = NULL;
-                       }
-
-                       logger(mesh, MESHLINK_DEBUG, "Terminating pending second connection with %s", n->name);
-                       terminate_connection(mesh, other, false);
-               }
-       }
-
-       /* Send him everything we know */
-
-       send_everything(mesh, c);
-
-       /* Create an edge_t for this connection */
-
-       assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
-
-       c->edge = new_edge();
-       c->edge->from = mesh->self;
-       c->edge->to = n;
-       sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
-       c->edge->weight = mesh->dev_class_traits[devclass].edge_weight;
-       c->edge->connection = c;
-
-       node_add_recent_address(mesh, n, &c->address);
-       edge_add(mesh, c->edge);
-
-       /* Notify everyone of the new edge */
-
-       send_add_edge(mesh, mesh->everyone, c->edge, 0);
-
-       /* Run MST and SSSP algorithms */
-
-       graph(mesh);
+       send_add_edge(mesh, c, 0);
+       n->status.reachable = true;
+       update_node_status(mesh, c->node);
 
        /* Request a session key to jump start UDP traffic */