summaryrefslogtreecommitdiffstats
path: root/src/shell.c
blob: 7acfa48118bc97ca0809479aec4b826d2aab9b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
  Copyright (c) 2012-2014, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "peer.h"

#include <arpa/inet.h>
#include <net/if.h>
#include <sys/wait.h>


bool fastd_shell_exec(fastd_context_t *ctx, const char *command, const char *dir, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, int *ret) {
	int result = -1;
	bool ok = false;
	char *cwd = get_current_dir_name();

	if(!chdir(dir)) {
		/* both INET6_ADDRSTRLEN and IFNAMESIZE already include space for the zero termination, so there is no need to add space for the '%' here. */
		char buf[INET6_ADDRSTRLEN+IF_NAMESIZE];

		snprintf(buf, sizeof(buf), "%u", (unsigned)getpid());
		setenv("FASTD_PID", buf, 1);

		if (ctx->ifname) {
			setenv("INTERFACE", ctx->ifname, 1);
		}
		else if (ctx->conf->ifname) {
			char ifname[IF_NAMESIZE];

			strncpy(ifname, ctx->conf->ifname, sizeof(ifname)-1);
			ifname[sizeof(ifname)-1] = 0;

			setenv("INTERFACE", ifname, 1);
		}
		else {
			unsetenv("INTERFACE");
		}

		snprintf(buf, sizeof(buf), "%u", ctx->conf->mtu);
		setenv("INTERFACE_MTU", buf, 1);

		if (peer && peer->config && peer->config->name)
			setenv("PEER_NAME", peer->config->name, 1);
		else
			unsetenv("PEER_NAME");

		switch(local_addr ? local_addr->sa.sa_family : AF_UNSPEC) {
		case AF_INET:
			inet_ntop(AF_INET, &local_addr->in.sin_addr, buf, sizeof(buf));
			setenv("LOCAL_ADDRESS", buf, 1);

			snprintf(buf, sizeof(buf), "%u", ntohs(local_addr->in.sin_port));
			setenv("LOCAL_PORT", buf, 1);

			break;

		case AF_INET6:
			inet_ntop(AF_INET6, &local_addr->in6.sin6_addr, buf, sizeof(buf));

			if (IN6_IS_ADDR_LINKLOCAL(&local_addr->in6.sin6_addr)) {
				if (if_indextoname(local_addr->in6.sin6_scope_id, buf+strlen(buf)+1))
					buf[strlen(buf)] = '%';
			}

			setenv("LOCAL_ADDRESS", buf, 1);

			snprintf(buf, sizeof(buf), "%u", ntohs(local_addr->in6.sin6_port));
			setenv("LOCAL_PORT", buf, 1);

			break;

		default:
			unsetenv("LOCAL_ADDRESS");
			unsetenv("LOCAL_PORT");
		}

		switch(peer_addr ? peer_addr->sa.sa_family : AF_UNSPEC) {
		case AF_INET:
			inet_ntop(AF_INET, &peer_addr->in.sin_addr, buf, sizeof(buf));
			setenv("PEER_ADDRESS", buf, 1);

			snprintf(buf, sizeof(buf), "%u", ntohs(peer_addr->in.sin_port));
			setenv("PEER_PORT", buf, 1);

			break;

		case AF_INET6:
			inet_ntop(AF_INET6, &peer_addr->in6.sin6_addr, buf, sizeof(buf));

			if (IN6_IS_ADDR_LINKLOCAL(&peer_addr->in6.sin6_addr)) {
				if (if_indextoname(peer_addr->in6.sin6_scope_id, buf+strlen(buf)+1))
					buf[strlen(buf)] = '%';
			}

			setenv("PEER_ADDRESS", buf, 1);

			snprintf(buf, sizeof(buf), "%u", ntohs(peer_addr->in6.sin6_port));
			setenv("PEER_PORT", buf, 1);

			break;

		default:
			unsetenv("PEER_ADDRESS");
			unsetenv("PEER_PORT");
		}

		ctx->conf->protocol->set_shell_env(ctx, peer);

		result = system(command);

		if (ret) {
			*ret = result;
			ok = true;
		}
		else {
			if (WIFSIGNALED(result))
				pr_error(ctx, "command exited with signal %i", WTERMSIG(result));
			else if (WEXITSTATUS(result))
				pr_warn(ctx, "command exited with status %i", WEXITSTATUS(result));
			else
				ok = true;
		}

		if(chdir(cwd))
			pr_error(ctx, "can't chdir to `%s': %s", cwd, strerror(errno));
	}
	else {
		pr_error(ctx, "can't chdir to `%s': %s", dir, strerror(errno));
	}

	free(cwd);

	return ok;
}