|
Lines 205-225
Link Here
|
| 205 |
*/ |
205 |
*/ |
| 206 |
|
206 |
|
| 207 |
void |
207 |
void |
| 208 |
add_local_forward(Options *options, u_short port, const char *host, |
208 |
add_local_forward(Options *options, const char *listen_host, |
| 209 |
u_short host_port) |
209 |
u_short listen_port, const char *connect_host, |
|
|
210 |
u_short connect_port) |
| 210 |
{ |
211 |
{ |
| 211 |
Forward *fwd; |
212 |
Forward *fwd; |
| 212 |
#ifndef NO_IPPORT_RESERVED_CONCEPT |
213 |
#ifndef NO_IPPORT_RESERVED_CONCEPT |
| 213 |
extern uid_t original_real_uid; |
214 |
extern uid_t original_real_uid; |
| 214 |
if (port < IPPORT_RESERVED && original_real_uid != 0) |
215 |
if (listen_port < IPPORT_RESERVED && original_real_uid != 0) |
| 215 |
fatal("Privileged ports can only be forwarded by root."); |
216 |
fatal("Privileged ports can only be forwarded by root."); |
| 216 |
#endif |
217 |
#endif |
| 217 |
if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
218 |
if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 218 |
fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION); |
219 |
fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION); |
| 219 |
fwd = &options->local_forwards[options->num_local_forwards++]; |
220 |
fwd = &options->local_forwards[options->num_local_forwards++]; |
| 220 |
fwd->port = port; |
221 |
if (listen_host == NULL) |
| 221 |
fwd->host = xstrdup(host); |
222 |
fwd->listen_host = NULL; |
| 222 |
fwd->host_port = host_port; |
223 |
else |
|
|
224 |
fwd->listen_host = xstrdup(listen_host); |
| 225 |
fwd->listen_port = listen_port; |
| 226 |
fwd->connect_host = xstrdup(connect_host); |
| 227 |
fwd->connect_port = connect_port; |
| 223 |
} |
228 |
} |
| 224 |
|
229 |
|
| 225 |
/* |
230 |
/* |
|
Lines 228-244
Link Here
|
| 228 |
*/ |
233 |
*/ |
| 229 |
|
234 |
|
| 230 |
void |
235 |
void |
| 231 |
add_remote_forward(Options *options, u_short port, const char *host, |
236 |
add_remote_forward(Options *options, const char *listen_host, |
| 232 |
u_short host_port) |
237 |
u_short listen_port, const char *connect_host, |
|
|
238 |
u_short connect_port) |
| 233 |
{ |
239 |
{ |
| 234 |
Forward *fwd; |
240 |
Forward *fwd; |
| 235 |
if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
241 |
if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 236 |
fatal("Too many remote forwards (max %d).", |
242 |
fatal("Too many remote forwards (max %d).", |
| 237 |
SSH_MAX_FORWARDS_PER_DIRECTION); |
243 |
SSH_MAX_FORWARDS_PER_DIRECTION); |
| 238 |
fwd = &options->remote_forwards[options->num_remote_forwards++]; |
244 |
fwd = &options->remote_forwards[options->num_remote_forwards++]; |
| 239 |
fwd->port = port; |
245 |
if (listen_host == NULL) |
| 240 |
fwd->host = xstrdup(host); |
246 |
fwd->listen_host = NULL; |
| 241 |
fwd->host_port = host_port; |
247 |
else |
|
|
248 |
fwd->listen_host = xstrdup(listen_host); |
| 249 |
fwd->listen_port = listen_port; |
| 250 |
fwd->connect_host = xstrdup(connect_host); |
| 251 |
fwd->connect_port = connect_port; |
| 242 |
} |
252 |
} |
| 243 |
|
253 |
|
| 244 |
static void |
254 |
static void |
|
Lines 246-256
Link Here
|
| 246 |
{ |
256 |
{ |
| 247 |
int i; |
257 |
int i; |
| 248 |
|
258 |
|
| 249 |
for (i = 0; i < options->num_local_forwards; i++) |
259 |
for (i = 0; i < options->num_local_forwards; i++) { |
| 250 |
xfree(options->local_forwards[i].host); |
260 |
xfree(options->local_forwards[i].listen_host); |
|
|
261 |
xfree(options->local_forwards[i].connect_host); |
| 262 |
} |
| 251 |
options->num_local_forwards = 0; |
263 |
options->num_local_forwards = 0; |
| 252 |
for (i = 0; i < options->num_remote_forwards; i++) |
264 |
for (i = 0; i < options->num_remote_forwards; i++) { |
| 253 |
xfree(options->remote_forwards[i].host); |
265 |
xfree(options->remote_forwards[i].listen_host); |
|
|
266 |
xfree(options->remote_forwards[i].connect_host); |
| 267 |
} |
| 254 |
options->num_remote_forwards = 0; |
268 |
options->num_remote_forwards = 0; |
| 255 |
} |
269 |
} |
| 256 |
|
270 |
|
|
Lines 283-294
Link Here
|
| 283 |
char *line, const char *filename, int linenum, |
297 |
char *line, const char *filename, int linenum, |
| 284 |
int *activep) |
298 |
int *activep) |
| 285 |
{ |
299 |
{ |
| 286 |
char buf[256], *s, **charptr, *endofnumber, *keyword, *arg; |
300 |
char *s, **charptr, *endofnumber, *keyword, *arg, |
|
|
301 |
*arg2, fwdarg[256]; |
| 287 |
int opcode, *intptr, value; |
302 |
int opcode, *intptr, value; |
| 288 |
size_t len; |
303 |
size_t len; |
| 289 |
u_short fwd_port, fwd_host_port; |
|
|
| 290 |
char sfwd_host_port[6]; |
| 291 |
extern int IPv4or6; |
304 |
extern int IPv4or6; |
|
|
305 |
Forward fwd; |
| 292 |
|
306 |
|
| 293 |
/* Strip trailing whitespace */ |
307 |
/* Strip trailing whitespace */ |
| 294 |
for(len = strlen(line) - 1; len > 0; len--) { |
308 |
for(len = strlen(line) - 1; len > 0; len--) { |
|
Lines 649-675
Link Here
|
| 649 |
if (!arg || *arg == '\0') |
663 |
if (!arg || *arg == '\0') |
| 650 |
fatal("%.200s line %d: Missing port argument.", |
664 |
fatal("%.200s line %d: Missing port argument.", |
| 651 |
filename, linenum); |
665 |
filename, linenum); |
| 652 |
if ((fwd_port = a2port(arg)) == 0) |
666 |
arg2 = strdelim(&s); |
| 653 |
fatal("%.200s line %d: Bad listen port.", |
667 |
if (!arg2 || *arg2 == '\0') |
| 654 |
filename, linenum); |
|
|
| 655 |
arg = strdelim(&s); |
| 656 |
if (!arg || *arg == '\0') |
| 657 |
fatal("%.200s line %d: Missing second argument.", |
668 |
fatal("%.200s line %d: Missing second argument.", |
| 658 |
filename, linenum); |
669 |
filename, linenum); |
| 659 |
if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 && |
670 |
|
| 660 |
sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2) |
671 |
/* construct a string for parse_forward */ |
|
|
672 |
snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2); |
| 673 |
|
| 674 |
if (parse_forward(&fwd, fwdarg) == 0) |
| 661 |
fatal("%.200s line %d: Bad forwarding specification.", |
675 |
fatal("%.200s line %d: Bad forwarding specification.", |
| 662 |
filename, linenum); |
676 |
filename, linenum); |
| 663 |
if ((fwd_host_port = a2port(sfwd_host_port)) == 0) |
677 |
if (fwd.listen_port == 0) |
|
|
678 |
fatal("%.200s line %d: Bad listen port.", |
| 679 |
filename, linenum); |
| 680 |
if (fwd.connect_port == 0) |
| 664 |
fatal("%.200s line %d: Bad forwarding port.", |
681 |
fatal("%.200s line %d: Bad forwarding port.", |
| 665 |
filename, linenum); |
682 |
filename, linenum); |
|
|
683 |
|
| 666 |
if (*activep) { |
684 |
if (*activep) { |
| 667 |
if (opcode == oLocalForward) |
685 |
if (opcode == oLocalForward) |
| 668 |
add_local_forward(options, fwd_port, buf, |
686 |
add_local_forward(options, |
| 669 |
fwd_host_port); |
687 |
fwd.listen_host, fwd.listen_port, |
|
|
688 |
fwd.connect_host, fwd.connect_port); |
| 670 |
else if (opcode == oRemoteForward) |
689 |
else if (opcode == oRemoteForward) |
| 671 |
add_remote_forward(options, fwd_port, buf, |
690 |
add_remote_forward(options, |
| 672 |
fwd_host_port); |
691 |
fwd.listen_host, fwd.listen_port, |
|
|
692 |
fwd.connect_host, fwd.connect_port); |
| 673 |
} |
693 |
} |
| 674 |
break; |
694 |
break; |
| 675 |
|
695 |
|
|
Lines 678-689
Link Here
|
| 678 |
if (!arg || *arg == '\0') |
698 |
if (!arg || *arg == '\0') |
| 679 |
fatal("%.200s line %d: Missing port argument.", |
699 |
fatal("%.200s line %d: Missing port argument.", |
| 680 |
filename, linenum); |
700 |
filename, linenum); |
| 681 |
fwd_port = a2port(arg); |
701 |
fwd.listen_port = 0; |
| 682 |
if (fwd_port == 0) |
702 |
fwd.listen_host = hpdelim(&arg); |
|
|
703 |
if (!fwd.listen_host) |
| 704 |
fatal("%.200s line %d: Bad forwarding specification.", |
| 705 |
filename, linenum); |
| 706 |
if (arg) { |
| 707 |
fwd.listen_port = a2port(arg); |
| 708 |
fwd.listen_host = cleanhostname(fwd.listen_host); |
| 709 |
} else { |
| 710 |
fwd.listen_port = a2port(fwd.listen_host); |
| 711 |
fwd.listen_host = ""; |
| 712 |
} |
| 713 |
if (fwd.listen_port == 0) |
| 683 |
fatal("%.200s line %d: Badly formatted port number.", |
714 |
fatal("%.200s line %d: Badly formatted port number.", |
| 684 |
filename, linenum); |
715 |
filename, linenum); |
| 685 |
if (*activep) |
716 |
if (*activep) |
| 686 |
add_local_forward(options, fwd_port, "socks4", 0); |
717 |
add_local_forward(options, fwd.listen_host, |
|
|
718 |
fwd.listen_port, "socks4", 0); |
| 687 |
break; |
719 |
break; |
| 688 |
|
720 |
|
| 689 |
case oClearAllForwardings: |
721 |
case oClearAllForwardings: |
|
Lines 985-988
Link Here
|
| 985 |
/* options->hostname will be set in the main program if appropriate */ |
1017 |
/* options->hostname will be set in the main program if appropriate */ |
| 986 |
/* options->host_key_alias should not be set by default */ |
1018 |
/* options->host_key_alias should not be set by default */ |
| 987 |
/* options->preferred_authentications will be set in ssh */ |
1019 |
/* options->preferred_authentications will be set in ssh */ |
|
|
1020 |
} |
| 1021 |
|
| 1022 |
/* |
| 1023 |
* parse_forward |
| 1024 |
* parses a string containing a port forwarding specification of the form: |
| 1025 |
* [listenhost:]listenport:connecthost:connectport |
| 1026 |
* returns number of arguments parsed or zero on error |
| 1027 |
*/ |
| 1028 |
int |
| 1029 |
parse_forward(Forward *fwd, const char *fwdspec) |
| 1030 |
{ |
| 1031 |
int i; |
| 1032 |
char *p, *cp, *fwdarg[4]; |
| 1033 |
|
| 1034 |
cp = p = xstrdup(fwdspec); |
| 1035 |
|
| 1036 |
/* skip leading spaces */ |
| 1037 |
while (*cp && isspace(*cp)) |
| 1038 |
cp++; |
| 1039 |
|
| 1040 |
for (i = 0; i < 4; ++i) |
| 1041 |
if ( !(fwdarg[i] = hpdelim(&cp)) ) |
| 1042 |
break; |
| 1043 |
switch(i) { |
| 1044 |
case 3: |
| 1045 |
fwd->listen_host = NULL; |
| 1046 |
fwd->listen_port = a2port(fwdarg[0]); |
| 1047 |
fwd->connect_host = xstrdup(cleanhostname(fwdarg[1])); |
| 1048 |
fwd->connect_port = a2port(fwdarg[2]); |
| 1049 |
break; |
| 1050 |
|
| 1051 |
case 4: |
| 1052 |
fwd->listen_host = xstrdup(cleanhostname(fwdarg[0])); |
| 1053 |
fwd->listen_port = a2port(fwdarg[1]); |
| 1054 |
fwd->connect_host = xstrdup(cleanhostname(fwdarg[2])); |
| 1055 |
fwd->connect_port = a2port(fwdarg[3]); |
| 1056 |
break; |
| 1057 |
default: |
| 1058 |
i = 0; /* failure */ |
| 1059 |
} |
| 1060 |
|
| 1061 |
xfree(p); |
| 1062 |
|
| 1063 |
if (fwd->listen_port == 0 && fwd->connect_port == 0) |
| 1064 |
i = 0; /* failure */ |
| 1065 |
|
| 1066 |
return(i); |
| 988 |
} |
1067 |
} |