]> git.meshlink.io Git - meshlink/blobdiff - src/protocol_auth.c
Fix spelling errors.
[meshlink] / src / protocol_auth.c
index b11acaef7c5e4238dcba37af577bb8077da2fef3..f88aaf4b9e7bf430c08cffb02542fe1b9170dbc6 100644 (file)
@@ -48,7 +48,7 @@ static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) {
                char *port;
 
                sockaddr2str(&c->address, &host, &port);
-               send_request(mesh, c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
+               send_request(mesh, c, NULL, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
                free(host);
                free(port);
                return true;
@@ -151,7 +151,7 @@ bool send_id(meshlink_handle_t *mesh, connection_t *c) {
                        return false;
                }
 
-       return send_request(mesh, c, "%d %s %d.%d", ID, mesh->self->connection->name, mesh->self->connection->protocol_major, minor);
+       return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->connection->name, mesh->self->connection->protocol_major, minor, mesh->appname);
 }
 
 static bool finalize_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data, uint16_t len) {
@@ -179,9 +179,14 @@ static bool finalize_invitation(meshlink_handle_t *mesh, connection_t *c, const
        }
 
        fprintf(f, "ECDSAPublicKey = %s\n", (const char *)data);
+
+       if(c->submesh) {
+               fprintf(f, "SubMesh = %s\n", c->submesh->name);
+       }
+
        fclose(f);
 
-       logger(mesh, MESHLINK_INFO, "Key succesfully received from %s", c->name);
+       logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name);
 
        //TODO: callback to application to inform of an accepted invitation
 
@@ -239,6 +244,24 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat
 
        if(!f) {
                logger(mesh, MESHLINK_ERROR, "Error trying to open invitation %s\n", cookie);
+               unlink(usedname);
+               return false;
+       }
+
+       // Check the timestamp
+       struct stat st;
+
+       if(fstat(fileno(f), &st)) {
+               logger(mesh, MESHLINK_ERROR, "Could not stat invitation file %s\n", usedname);
+               fclose(f);
+               unlink(usedname);
+               return false;
+       }
+
+       if(time(NULL) > st.st_mtime + mesh->invitation_timeout) {
+               logger(mesh, MESHLINK_ERROR, "Peer %s tried to use an outdated invitation file %s\n", c->name, usedname);
+               fclose(f);
+               unlink(usedname);
                return false;
        }
 
@@ -270,6 +293,38 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat
        free(c->name);
        c->name = xstrdup(name);
 
+       // Check if the file contains Sub-Mesh information
+       buf[0] = 0;
+       fgets(buf, sizeof(buf), f);
+
+       if(*buf) {
+               buf[strlen(buf) - 1] = 0;
+       }
+
+       if(!strncmp(buf, "SubMesh", 7)) {
+               len = strcspn(buf, " \t=");
+               char *submesh_name = buf + len;
+               submesh_name += strspn(submesh_name, " \t");
+
+               if(*submesh_name == '=') {
+                       submesh_name++;
+                       submesh_name += strspn(submesh_name, " \t");
+               }
+
+               if(!check_id(submesh_name)) {
+                       logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie);
+                       fclose(f);
+                       return false;
+               }
+
+               c->submesh = NULL;
+               c->submesh = lookup_or_create_submesh(mesh, submesh_name);
+
+               if(!c->submesh) {
+                       return false;
+               }
+       }
+
        // Send the node the contents of the invitation file
        rewind(f);
        size_t result;
@@ -284,7 +339,7 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat
 
        c->status.invitation_used = true;
 
-       logger(mesh, MESHLINK_INFO, "Invitation %s succesfully sent to %s", cookie, c->name);
+       logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name);
        return true;
 }
 
@@ -318,7 +373,7 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                        return false;
                }
 
-               if(!send_request(mesh, c, "%d %s", ACK, mykey)) {
+               if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
                        return false;
                }
 
@@ -369,6 +424,14 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
                }
        }
 
+       bool blacklisted = false;
+       get_config_bool(lookup_config(c->config_tree, "blacklisted"), &blacklisted);
+
+       if(blacklisted) {
+               logger(mesh, MESHLINK_EPEER, "Peer %s is blacklisted", c->name);
+               return false;
+       }
+
        read_ecdsa_public_key(mesh, c);
 
        if(!ecdsa_active(c->ecdsa)) {
@@ -376,7 +439,7 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
 
                node_t *n = lookup_node(mesh, c->name);
 
-               if(n && !n->status.waitingforkey) {
+               if(n && n->status.reachable && !n->status.waitingforkey) {
                        logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
                        send_req_key(mesh, n);
                }
@@ -412,7 +475,7 @@ bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
                c->options |= OPTION_PMTU_DISCOVERY;
        }
 
-       return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, (c->options & 0xffffff) | (PROT_MINOR << 24));
+       return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, (c->options & 0xffffff) | (PROT_MINOR << 24));
 }
 
 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
@@ -420,7 +483,7 @@ static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
 
        for splay_each(node_t, n, mesh->nodes) {
                for splay_each(edge_t, e, n->edge_tree) {
-                       send_add_edge(mesh, c, e);
+                       send_add_edge(mesh, c, e, 0);
                }
        }
 }
@@ -515,7 +578,7 @@ bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
 
        /* Notify everyone of the new edge */
 
-       send_add_edge(mesh, mesh->everyone, c->edge);
+       send_add_edge(mesh, mesh->everyone, c->edge, 0);
 
        /* Run MST and SSSP algorithms */