Fix a file descriptor leak when using an invitation.
[tinc] / src / invitation.c
index 9a082e3..0dfd9ea 100644 (file)
@@ -252,8 +252,8 @@ int cmd_invite(int argc, char *argv[]) {
        }
 
        // If a daemon is running, ensure no other nodes know about this name
-       bool found = false;
        if(connect_tincd(false)) {
+               bool found = false;
                sendline(fd, "%d %d", CONTROL, REQ_DUMP_NODES);
 
                while(recvline(fd, line, sizeof line)) {
@@ -607,6 +607,17 @@ make_names:
                return false;
        }
 
+       snprintf(filename, sizeof filename, "%s" SLASH "invitation-data", confbase);
+       FILE *finv = fopen(filename, "w");
+       if(!finv || fwrite(data, datalen, 1, finv) != 1) {
+               fprintf(stderr, "Could not create file %s: %s\n", filename, strerror(errno));
+               fclose(fh);
+               fclose(f);
+               fclose(finv);
+               return false;
+       }
+       fclose(finv);
+
        snprintf(filename, sizeof filename, "%s" SLASH "tinc-up.invitation", confbase);
        FILE *fup = fopen(filename, "w");
        if(!fup) {
@@ -686,7 +697,7 @@ make_names:
                }
 
                // Copy the safe variable to the right config file
-               fprintf(variables[i].type & VAR_HOST ? fh : f, "%s = %s\n", l, value);
+               fprintf((variables[i].type & VAR_HOST) ? fh : f, "%s = %s\n", l, value);
        }
 
        fclose(f);