From: Guus Sliepen Date: Tue, 20 Aug 2013 22:24:55 +0000 (+0200) Subject: Execute scripts when invitations are created or accepted. X-Git-Tag: release-1.1pre9~29 X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=21184674b38ea1da87588de97dab076c9b9e4a81;p=tinc Execute scripts when invitations are created or accepted. --- diff --git a/doc/tinc.conf.5.in b/doc/tinc.conf.5.in index 69deace9..1d5aa4e0 100644 --- a/doc/tinc.conf.5.in +++ b/doc/tinc.conf.5.in @@ -580,7 +580,9 @@ UDP is possible or not. Apart from reading the server and host configuration files, tinc can also run scripts at certain moments. Under Windows (not Cygwin), the scripts should have the extension -.Pa .bat . +.Pa .bat +or +.Pa cmd . .Bl -tag -width indent .It Pa @sysconfdir@/tinc/ Ns Ar NETNAME Ns Pa /tinc-up This is the most important script. @@ -607,6 +609,10 @@ This script is started when a Subnet becomes reachable. The Subnet and the node it belongs to are passed in environment variables. .It Pa @sysconfdir@/tinc/ Ns Ar NETNAME Ns Pa /subnet-down This script is started when a Subnet becomes unreachable. +.It Pa @sysconfdir@/tinc/ Ns Ar NETNAME Ns Pa /invitation-created +This script is started when a new invitation has been created. +.It Pa @sysconfdir@/tinc/ Ns Ar NETNAME Ns Pa /invitation-accepted +This script is started when an invitation has been used. .El .Pp The scripts are started without command line arguments, but can make use of certain environment variables. @@ -615,6 +621,8 @@ Under UNIX like operating systems the names of environment variables must be pre in scripts. Under Windows, in .Pa .bat +or +.Pa .cmd files, they have to be put between .Li % signs. @@ -640,6 +648,14 @@ When a host becomes (un)reachable, this is set to the port number it uses for co When a subnet becomes (un)reachable, this is set to the subnet. .It Ev WEIGHT When a subnet becomes (un)reachable, this is set to the subnet weight. +.It Ev INVITATION_FILE +When the +.Pa invitation-created +script is called, this is set to the file where the invitation details will be stored. +.It Ev INVITATION_URL +When the +.Pa invitation-created +script is called, this is set to the invitation URL that has been created. .El .Pp Do not forget that under UNIX operating systems, you have to make the scripts executable, using the command diff --git a/doc/tinc.texi b/doc/tinc.texi index 6f128f81..acbee946 100644 --- a/doc/tinc.texi +++ b/doc/tinc.texi @@ -1380,6 +1380,13 @@ The Subnet and the node it belongs to are passed in environment variables. @item @value{sysconfdir}/tinc/@var{netname}/subnet-down This script is started when a Subnet becomes unreachable. + +@item @value{sysconfdir}/tinc/@var{netname}/invitation-created +This script is started when a new invitation has been created. + +@item @value{sysconfdir}/tinc/@var{netname}/invitation-accepted +This script is started when an invitation has been used. + @end table @cindex environment variables @@ -1424,8 +1431,24 @@ this is set to the port number it uses for communication with other tinc daemons @item SUBNET When a subnet becomes (un)reachable, this is set to the subnet. +@cindex WEIGHT +@item WEIGHT +When a subnet becomes (un)reachable, this is set to the subnet weight. + +@cindex INVITATION_FILE +@item INVITATION_FILE +When the @file{invitation-created} script is called, +this is set to the file where the invitation details will be stored. + +@cindex INVITATION_URL +@item INVITATION_URL +When the @file{invitation-created} script is called, +this is set to the invitation URL that has been created. @end table +Do not forget that under UNIX operating systems, +you have to make the scripts executable, using the command @samp{chmod a+x script}. + @c ================================================================== @node How to configure diff --git a/src/invitation.c b/src/invitation.c index 5175ba92..e6567bae 100644 --- a/src/invitation.c +++ b/src/invitation.c @@ -368,7 +368,6 @@ int cmd_invite(int argc, char *argv[]) { free(filename); return 1; } - free(filename); f = fdopen(ifd, "w"); if(!f) abort(); @@ -385,12 +384,31 @@ int cmd_invite(int argc, char *argv[]) { fprintf(f, "#---------------------------------------------------------------#\n"); fprintf(f, "Name = %s\n", myname); - xasprintf(&filename, "%s" SLASH "hosts" SLASH "%s", confbase, myname); - fcopy(f, filename); + char *filename2; + xasprintf(&filename2, "%s" SLASH "hosts" SLASH "%s", confbase, myname); + fcopy(f, filename2); fclose(f); + free(filename2); // Create an URL from the local address, key hash and cookie - printf("%s/%s%s\n", address, hash, cookie); + char *url; + xasprintf(&url, "%s/%s%s", address, hash, cookie); + + // Call the inviation-created script + setenv("NAME", myname, true); + setenv("NETNAME", netname, true); + setenv("NODE", argv[1], true); + setenv("INVITATION_FILE", filename, true); + setenv("INVITATION_URL", url, true); + char *scriptname; + xasprintf(&scriptname, "\"%s" SLASH "invitation-created\"", confbase); + system(scriptname); + free(scriptname); + unsetenv("NODE"); + unsetenv("INVITATION"); + + puts(url); + free(url); free(filename); free(address); diff --git a/src/protocol_auth.c b/src/protocol_auth.c index d69c8ab7..f309a40d 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -26,6 +26,7 @@ #include "control_common.h" #include "cipher.h" #include "crypto.h" +#include "device.h" #include "digest.h" #include "ecdsa.h" #include "edge.h" @@ -37,6 +38,7 @@ #include "netutl.h" #include "node.h" #include "prf.h" +#include "process.h" #include "protocol.h" #include "rsa.h" #include "sptps.h" @@ -174,6 +176,24 @@ static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) fclose(f); logger(DEBUG_CONNECTIONS, LOG_INFO, "Key succesfully received from %s (%s)", c->name, c->hostname); + + // Call invitation-accepted script + char *envp[7] = {NULL}; + char *address, *port; + + xasprintf(&envp[0], "NETNAME=%s", netname ? : ""); + xasprintf(&envp[1], "DEVICE=%s", device ? : ""); + xasprintf(&envp[2], "INTERFACE=%s", iface ? : ""); + xasprintf(&envp[3], "NODE=%s", c->name); + sockaddr2str(&c->address, &address, &port); + xasprintf(&envp[4], "REMOTEADDRESS=%s", address); + xasprintf(&envp[5], "NAME=%s", myself->name); + + execute_script("invitation-accepted", envp); + + for(int i = 0; envp[i] && i < 7; i++) + free(envp[i]); + sptps_send_record(&c->sptps, 2, data, 0); return true; }