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