forked from andys/VNCsnapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunnel.c
275 lines (230 loc) · 6.83 KB
/
tunnel.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (C) 2000 Const Kaplinsky. All Rights Reserved.
* Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
/*
* tunnel.c - tunneling support (e.g. for using standalone SSH installation)
*/
static const char *ID = "$Id: tunnel.c,v 1.5 2003/03/26 00:32:00 grmcdorman Exp $";
#ifndef WIN32
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include "vncsnapshot.h"
#ifdef WIN32
#define stat _stat
#ifndef S_ISREG
#define S_ISREG(x) (x & _S_IFREG)
#endif
#endif
/* If S_ISLNK is not defined, assume links don't exist */
#ifndef S_ISLNK
#define S_ISLNK(x) (0)
#endif
/* True if there was -tunnel or -via option in the command line. */
Bool tunnelSpecified = False;
/* True if it was -tunnel, not -via option. */
static Bool tunnelOption = False;
/* "Hostname:display" pair in the command line will be substituted
by this fake argument when tunneling is used. */
static char lastArgv[32];
static void processTunnelArgs(char **remoteHost,
int *remotePort, int localPort,
int *pargc, char **argv, int tunnelArgIndex);
static void processViaArgs(char **gatewayHost, char **remoteHost,
int *remotePort, int localPort,
int *pargc, char **argv, int tunnelArgIndex);
static char *getCmdPattern(void);
static Bool fillCmdPattern(char *result, char *pattern,
char *gatewayHost, char *remoteHost,
char *remotePort, char *localPort);
static Bool runCommand(char *cmd);
Bool
createTunnel(int *pargc, char **argv, int tunnelArgIndex)
{
char *pattern;
char cmd[1024];
int localPort, remotePort;
char localPortStr[8];
char remotePortStr[8];
char *gatewayHost = "";
char *remoteHost = "localhost";
tunnelSpecified = True;
if (strcmp(argv[tunnelArgIndex], "-tunnel") == 0)
tunnelOption = True;
pattern = getCmdPattern();
if (!pattern)
return False;
localPort = FindFreeTcpPort();
if (localPort == 0)
return False;
if (tunnelOption) {
processTunnelArgs(&remoteHost, &remotePort, localPort,
pargc, argv, tunnelArgIndex);
} else {
processViaArgs(&gatewayHost, &remoteHost, &remotePort, localPort,
pargc, argv, tunnelArgIndex);
}
sprintf(localPortStr, "%d", localPort);
sprintf(remotePortStr, "%d", remotePort);
if (!fillCmdPattern(cmd, pattern, gatewayHost, remoteHost,
remotePortStr, localPortStr))
return False;
if (!runCommand(cmd))
return False;
return True;
}
static void
processTunnelArgs(char **remoteHost, int *remotePort, int localPort,
int *pargc, char **argv, int tunnelArgIndex)
{
char *pdisplay;
if (tunnelArgIndex >= *pargc - 1)
usage();
pdisplay = strchr(argv[*pargc - 1], ':');
if (pdisplay == NULL || pdisplay == argv[*pargc - 1])
usage();
*pdisplay++ = '\0';
if (strspn(pdisplay, "-0123456789") != strlen(pdisplay))
usage();
*remotePort = atoi(pdisplay);
if (*remotePort < 100)
*remotePort += SERVER_PORT_OFFSET;
sprintf(lastArgv, "localhost:%d", localPort);
*remoteHost = argv[*pargc - 1];
argv[*pargc - 1] = lastArgv;
removeArgs(pargc, argv, tunnelArgIndex, 1);
}
static void
processViaArgs(char **gatewayHost, char **remoteHost,
int *remotePort, int localPort,
int *pargc, char **argv, int tunnelArgIndex)
{
char *colonPos;
int len, portOffset;
if (tunnelArgIndex >= *pargc - 2)
usage();
colonPos = strchr(argv[*pargc - 1], ':');
if (colonPos == NULL) {
/* No colon -- use default port number */
*remotePort = SERVER_PORT_OFFSET;
} else {
*colonPos++ = '\0';
len = strlen(colonPos);
portOffset = SERVER_PORT_OFFSET;
if (colonPos[0] == ':') {
/* Two colons -- interpret as a port number */
colonPos++;
len--;
portOffset = 0;
}
if (!len || strspn(colonPos, "-0123456789") != len) {
usage();
}
*remotePort = atoi(colonPos) + portOffset;
}
sprintf(lastArgv, "localhost::%d", localPort);
*gatewayHost = argv[tunnelArgIndex + 1];
if (argv[*pargc - 1][0] != '\0')
*remoteHost = argv[*pargc - 1];
argv[*pargc - 1] = lastArgv;
removeArgs(pargc, argv, tunnelArgIndex, 2);
}
static char *
getCmdPattern(void)
{
struct stat st;
char *pattern;
pattern = getenv((tunnelOption) ? "VNC_TUNNEL_CMD" : "VNC_VIA_CMD");
if (pattern == NULL) {
if ( stat(DEFAULT_SSH_CMD, &st) != 0 ||
!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) ) {
fprintf(stderr, "%s: Cannot establish SSH tunnel: missing %s binary.\n",
programName, DEFAULT_SSH_CMD);
return NULL;
}
pattern = (tunnelOption) ? DEFAULT_TUNNEL_CMD : DEFAULT_VIA_CMD;
}
return pattern;
}
/* Note: in fillCmdPattern() result points to a 1024-byte buffer */
static Bool
fillCmdPattern(char *result, char *pattern,
char *gatewayHost, char *remoteHost,
char *remotePort, char *localPort)
{
int i, j;
Bool H_found = False, G_found = False, R_found = False, L_found = False;
for (i=0, j=0; pattern[i] && j<1023; i++, j++) {
if (pattern[i] == '%') {
switch (pattern[++i]) {
case 'H':
strncpy(&result[j], remoteHost, 1024 - j);
j += strlen(remoteHost) - 1;
H_found = True;
continue;
case 'G':
strncpy(&result[j], gatewayHost, 1024 - j);
j += strlen(gatewayHost) - 1;
G_found = True;
continue;
case 'R':
strncpy(&result[j], remotePort, 1024 - j);
j += strlen(remotePort) - 1;
R_found = True;
continue;
case 'L':
strncpy(&result[j], localPort, 1024 - j);
j += strlen(localPort) - 1;
L_found = True;
continue;
case '\0':
i--;
continue;
}
}
result[j] = pattern[i];
}
if (pattern[i]) {
fprintf(stderr, "%s: Tunneling command is too long.\n", programName);
return False;
}
if (!H_found || !R_found || !L_found) {
fprintf(stderr, "%s: %%H, %%R or %%L absent in tunneling command.\n",
programName);
return False;
}
if (!tunnelOption && !G_found) {
fprintf(stderr, "%s: %%G pattern absent in tunneling command.\n",
programName);
return False;
}
result[j] = '\0';
return True;
}
static Bool
runCommand(char *cmd)
{
if (system(cmd) != 0) {
fprintf(stderr, "%s: Tunneling command failed: %s.\n",
programName, cmd);
return False;
}
return True;
}