|
Lines 206-224
static struct {
Link Here
|
| 206 |
*/ |
206 |
*/ |
| 207 |
|
207 |
|
| 208 |
void |
208 |
void |
| 209 |
add_local_forward(Options *options, u_short port, const char *host, |
209 |
add_local_forward(Options *options, const char *listen_host, |
| 210 |
u_short host_port) |
210 |
u_short listen_port, const char *connect_host, u_short connect_port) |
| 211 |
{ |
211 |
{ |
| 212 |
Forward *fwd; |
212 |
Forward *fwd; |
| 213 |
extern uid_t original_real_uid; |
213 |
extern uid_t original_real_uid; |
| 214 |
if (port < IPPORT_RESERVED && original_real_uid != 0) |
214 |
if (listen_port < IPPORT_RESERVED && original_real_uid != 0) |
| 215 |
fatal("Privileged ports can only be forwarded by root."); |
215 |
fatal("Privileged ports can only be forwarded by root."); |
| 216 |
if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
216 |
if (options->num_local_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 217 |
fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION); |
217 |
fatal("Too many local forwards (max %d).", SSH_MAX_FORWARDS_PER_DIRECTION); |
| 218 |
fwd = &options->local_forwards[options->num_local_forwards++]; |
218 |
fwd = &options->local_forwards[options->num_local_forwards++]; |
| 219 |
fwd->port = port; |
219 |
if (listen_host == NULL) |
| 220 |
fwd->host = xstrdup(host); |
220 |
fwd->listen_host = NULL; |
| 221 |
fwd->host_port = host_port; |
221 |
else |
|
|
222 |
fwd->listen_host = xstrdup(listen_host); |
| 223 |
fwd->listen_port = listen_port; |
| 224 |
fwd->connect_host = xstrdup(connect_host); |
| 225 |
fwd->connect_port = connect_port; |
| 222 |
} |
226 |
} |
| 223 |
|
227 |
|
| 224 |
/* |
228 |
/* |
|
Lines 227-243
add_local_forward(Options *options, u_sh
Link Here
|
| 227 |
*/ |
231 |
*/ |
| 228 |
|
232 |
|
| 229 |
void |
233 |
void |
| 230 |
add_remote_forward(Options *options, u_short port, const char *host, |
234 |
add_remote_forward(Options *options, const char *listen_host, |
| 231 |
u_short host_port) |
235 |
u_short listen_port, const char *connect_host, u_short connect_port) |
| 232 |
{ |
236 |
{ |
| 233 |
Forward *fwd; |
237 |
Forward *fwd; |
| 234 |
if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
238 |
if (options->num_remote_forwards >= SSH_MAX_FORWARDS_PER_DIRECTION) |
| 235 |
fatal("Too many remote forwards (max %d).", |
239 |
fatal("Too many remote forwards (max %d).", |
| 236 |
SSH_MAX_FORWARDS_PER_DIRECTION); |
240 |
SSH_MAX_FORWARDS_PER_DIRECTION); |
| 237 |
fwd = &options->remote_forwards[options->num_remote_forwards++]; |
241 |
fwd = &options->remote_forwards[options->num_remote_forwards++]; |
| 238 |
fwd->port = port; |
242 |
if (listen_host == NULL) |
| 239 |
fwd->host = xstrdup(host); |
243 |
fwd->listen_host = NULL; |
| 240 |
fwd->host_port = host_port; |
244 |
else |
|
|
245 |
fwd->listen_host = xstrdup(listen_host); |
| 246 |
fwd->listen_port = listen_port; |
| 247 |
fwd->connect_host = xstrdup(connect_host); |
| 248 |
fwd->connect_port = connect_port; |
| 241 |
} |
249 |
} |
| 242 |
|
250 |
|
| 243 |
static void |
251 |
static void |
|
Lines 245-255
clear_forwardings(Options *options)
Link Here
|
| 245 |
{ |
253 |
{ |
| 246 |
int i; |
254 |
int i; |
| 247 |
|
255 |
|
| 248 |
for (i = 0; i < options->num_local_forwards; i++) |
256 |
for (i = 0; i < options->num_local_forwards; i++) { |
| 249 |
xfree(options->local_forwards[i].host); |
257 |
xfree(options->local_forwards[i].listen_host); |
|
|
258 |
xfree(options->local_forwards[i].connect_host); |
| 259 |
} |
| 250 |
options->num_local_forwards = 0; |
260 |
options->num_local_forwards = 0; |
| 251 |
for (i = 0; i < options->num_remote_forwards; i++) |
261 |
for (i = 0; i < options->num_remote_forwards; i++) { |
| 252 |
xfree(options->remote_forwards[i].host); |
262 |
xfree(options->remote_forwards[i].listen_host); |
|
|
263 |
xfree(options->remote_forwards[i].connect_host); |
| 264 |
} |
| 253 |
options->num_remote_forwards = 0; |
265 |
options->num_remote_forwards = 0; |
| 254 |
} |
266 |
} |
| 255 |
|
267 |
|
|
Lines 282-292
process_config_line(Options *options, co
Link Here
|
| 282 |
char *line, const char *filename, int linenum, |
294 |
char *line, const char *filename, int linenum, |
| 283 |
int *activep) |
295 |
int *activep) |
| 284 |
{ |
296 |
{ |
| 285 |
char buf[256], *s, **charptr, *endofnumber, *keyword, *arg; |
297 |
char *s, **charptr, *endofnumber, *keyword, *arg, *arg2, fwdarg[256]; |
| 286 |
int opcode, *intptr, value; |
298 |
int opcode, *intptr, value; |
| 287 |
size_t len; |
299 |
size_t len; |
| 288 |
u_short fwd_port, fwd_host_port; |
300 |
Forward fwd; |
| 289 |
char sfwd_host_port[6]; |
|
|
| 290 |
|
301 |
|
| 291 |
/* Strip trailing whitespace */ |
302 |
/* Strip trailing whitespace */ |
| 292 |
for(len = strlen(line) - 1; len > 0; len--) { |
303 |
for(len = strlen(line) - 1; len > 0; len--) { |
|
Lines 643-672
parse_int:
Link Here
|
| 643 |
case oLocalForward: |
654 |
case oLocalForward: |
| 644 |
case oRemoteForward: |
655 |
case oRemoteForward: |
| 645 |
arg = strdelim(&s); |
656 |
arg = strdelim(&s); |
| 646 |
if (!arg || *arg == '\0') |
657 |
if (arg == NULL || *arg == '\0') |
| 647 |
fatal("%.200s line %d: Missing port argument.", |
658 |
fatal("%.200s line %d: Missing port argument.", |
| 648 |
filename, linenum); |
659 |
filename, linenum); |
| 649 |
if ((fwd_port = a2port(arg)) == 0) |
660 |
arg2 = strdelim(&s); |
| 650 |
fatal("%.200s line %d: Bad listen port.", |
661 |
if (arg2 == NULL || *arg2 == '\0') |
| 651 |
filename, linenum); |
662 |
fatal("%.200s line %d: Missing target argument.", |
| 652 |
arg = strdelim(&s); |
|
|
| 653 |
if (!arg || *arg == '\0') |
| 654 |
fatal("%.200s line %d: Missing second argument.", |
| 655 |
filename, linenum); |
663 |
filename, linenum); |
| 656 |
if (sscanf(arg, "%255[^:]:%5[0-9]", buf, sfwd_host_port) != 2 && |
664 |
|
| 657 |
sscanf(arg, "%255[^/]/%5[0-9]", buf, sfwd_host_port) != 2) |
665 |
/* construct a string for parse_forward */ |
|
|
666 |
snprintf(fwdarg, sizeof(fwdarg), "%s:%s", arg, arg2); |
| 667 |
|
| 668 |
if (parse_forward(&fwd, fwdarg) == 0) |
| 658 |
fatal("%.200s line %d: Bad forwarding specification.", |
669 |
fatal("%.200s line %d: Bad forwarding specification.", |
| 659 |
filename, linenum); |
670 |
filename, linenum); |
| 660 |
if ((fwd_host_port = a2port(sfwd_host_port)) == 0) |
671 |
|
| 661 |
fatal("%.200s line %d: Bad forwarding port.", |
|
|
| 662 |
filename, linenum); |
| 663 |
if (*activep) { |
672 |
if (*activep) { |
| 664 |
if (opcode == oLocalForward) |
673 |
if (opcode == oLocalForward) |
| 665 |
add_local_forward(options, fwd_port, buf, |
674 |
add_local_forward(options, |
| 666 |
fwd_host_port); |
675 |
fwd.listen_host, fwd.listen_port, |
|
|
676 |
fwd.connect_host, fwd.connect_port); |
| 667 |
else if (opcode == oRemoteForward) |
677 |
else if (opcode == oRemoteForward) |
| 668 |
add_remote_forward(options, fwd_port, buf, |
678 |
add_remote_forward(options, |
| 669 |
fwd_host_port); |
679 |
fwd.listen_host, fwd.listen_port, |
|
|
680 |
fwd.connect_host, fwd.connect_port); |
| 670 |
} |
681 |
} |
| 671 |
break; |
682 |
break; |
| 672 |
|
683 |
|
|
Lines 675-686
parse_int:
Link Here
|
| 675 |
if (!arg || *arg == '\0') |
686 |
if (!arg || *arg == '\0') |
| 676 |
fatal("%.200s line %d: Missing port argument.", |
687 |
fatal("%.200s line %d: Missing port argument.", |
| 677 |
filename, linenum); |
688 |
filename, linenum); |
| 678 |
fwd_port = a2port(arg); |
689 |
fwd.listen_port = 0; |
| 679 |
if (fwd_port == 0) |
690 |
fwd.listen_host = hpdelim(&arg); |
|
|
691 |
if (fwd.listen_host == NULL || |
| 692 |
strlen(fwd.listen_host) >= NI_MAXHOST) |
| 693 |
fatal("%.200s line %d: Bad forwarding specification.", |
| 694 |
filename, linenum); |
| 695 |
if (arg) { |
| 696 |
fwd.listen_port = a2port(arg); |
| 697 |
fwd.listen_host = cleanhostname(fwd.listen_host); |
| 698 |
} else { |
| 699 |
fwd.listen_port = a2port(fwd.listen_host); |
| 700 |
fwd.listen_host = ""; |
| 701 |
} |
| 702 |
if (fwd.listen_port == 0) |
| 680 |
fatal("%.200s line %d: Badly formatted port number.", |
703 |
fatal("%.200s line %d: Badly formatted port number.", |
| 681 |
filename, linenum); |
704 |
filename, linenum); |
| 682 |
if (*activep) |
705 |
if (*activep) |
| 683 |
add_local_forward(options, fwd_port, "socks", 0); |
706 |
add_local_forward(options, fwd.listen_host, |
|
|
707 |
fwd.listen_port, "socks", 0); |
| 684 |
break; |
708 |
break; |
| 685 |
|
709 |
|
| 686 |
case oClearAllForwardings: |
710 |
case oClearAllForwardings: |
|
Lines 1042-1045
fill_default_options(Options * options)
Link Here
|
| 1042 |
/* options->hostname will be set in the main program if appropriate */ |
1066 |
/* options->hostname will be set in the main program if appropriate */ |
| 1043 |
/* options->host_key_alias should not be set by default */ |
1067 |
/* options->host_key_alias should not be set by default */ |
| 1044 |
/* options->preferred_authentications will be set in ssh */ |
1068 |
/* options->preferred_authentications will be set in ssh */ |
|
|
1069 |
} |
| 1070 |
|
| 1071 |
/* |
| 1072 |
* parse_forward |
| 1073 |
* parses a string containing a port forwarding specification of the form: |
| 1074 |
* [listenhost:]listenport:connecthost:connectport |
| 1075 |
* returns number of arguments parsed or zero on error |
| 1076 |
*/ |
| 1077 |
int |
| 1078 |
parse_forward(Forward *fwd, const char *fwdspec) |
| 1079 |
{ |
| 1080 |
int i; |
| 1081 |
char *p, *cp, *fwdarg[4]; |
| 1082 |
|
| 1083 |
memset(fwd, '\0', sizeof(*fwd)); |
| 1084 |
|
| 1085 |
cp = p = xstrdup(fwdspec); |
| 1086 |
|
| 1087 |
/* skip leading spaces */ |
| 1088 |
while (*cp && isspace(*cp)) |
| 1089 |
cp++; |
| 1090 |
|
| 1091 |
for (i = 0; i < 4; ++i) |
| 1092 |
if ((fwdarg[i] = hpdelim(&cp)) == NULL) |
| 1093 |
break; |
| 1094 |
|
| 1095 |
/* Check for trailing garbage in 4-arg case*/ |
| 1096 |
if (cp != NULL) |
| 1097 |
i = 0; /* failure */ |
| 1098 |
|
| 1099 |
switch (i) { |
| 1100 |
case 3: |
| 1101 |
fwd->listen_host = NULL; |
| 1102 |
fwd->listen_port = a2port(fwdarg[0]); |
| 1103 |
fwd->connect_host = xstrdup(cleanhostname(fwdarg[1])); |
| 1104 |
fwd->connect_port = a2port(fwdarg[2]); |
| 1105 |
break; |
| 1106 |
|
| 1107 |
case 4: |
| 1108 |
fwd->listen_host = xstrdup(cleanhostname(fwdarg[0])); |
| 1109 |
fwd->listen_port = a2port(fwdarg[1]); |
| 1110 |
fwd->connect_host = xstrdup(cleanhostname(fwdarg[2])); |
| 1111 |
fwd->connect_port = a2port(fwdarg[3]); |
| 1112 |
break; |
| 1113 |
default: |
| 1114 |
i = 0; /* failure */ |
| 1115 |
} |
| 1116 |
|
| 1117 |
xfree(p); |
| 1118 |
|
| 1119 |
if (fwd->listen_port == 0 && fwd->connect_port == 0) |
| 1120 |
goto fail_free; |
| 1121 |
|
| 1122 |
if (fwd->connect_host != NULL && |
| 1123 |
strlen(fwd->connect_host) >= NI_MAXHOST) |
| 1124 |
goto fail_free; |
| 1125 |
|
| 1126 |
return (i); |
| 1127 |
|
| 1128 |
fail_free: |
| 1129 |
if (fwd->connect_host != NULL) |
| 1130 |
xfree(fwd->connect_host); |
| 1131 |
if (fwd->listen_host != NULL) |
| 1132 |
xfree(fwd->listen_host); |
| 1133 |
return (0); |
| 1045 |
} |
1134 |
} |