Bugzilla – Attachment 1596 Details for
Bug 1003
Support dynamic listen port allocation for remote forwards (-R0:...)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
revised revised patch
port0_2.diff (text/plain), 10.83 KB, created by
Damien Miller
on 2009-01-22 21:22:14 AEDT
(
hide
)
Description:
revised revised patch
Filename:
MIME Type:
Creator:
Damien Miller
Created:
2009-01-22 21:22:14 AEDT
Size:
10.83 KB
patch
obsolete
>Index: canohost.c >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/canohost.c,v >retrieving revision 1.63 >diff -u -p -r1.63 canohost.c >--- canohost.c 12 Jun 2008 00:03:49 -0000 1.63 >+++ canohost.c 22 Jan 2009 10:12:49 -0000 >@@ -300,7 +300,7 @@ get_remote_name_or_ip(u_int utmp_len, in > > /* Returns the local/remote port for the socket. */ > >-static int >+int > get_sock_port(int sock, int local) > { > struct sockaddr_storage from; >Index: canohost.h >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/canohost.h,v >retrieving revision 1.9 >diff -u -p -r1.9 canohost.h >--- canohost.h 25 Mar 2006 22:22:42 -0000 1.9 >+++ canohost.h 22 Jan 2009 10:12:49 -0000 >@@ -23,3 +23,5 @@ char *get_local_name(int); > > int get_remote_port(void); > int get_local_port(void); >+int get_sock_port(int, int); >+ >Index: channels.c >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/channels.c,v >retrieving revision 1.294 >diff -u -p -r1.294 channels.c >--- channels.c 22 Jan 2009 09:49:57 -0000 1.294 >+++ channels.c 22 Jan 2009 10:12:49 -0000 >@@ -2438,7 +2438,8 @@ channel_set_af(int af) > } > > static int >-channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port, >+channel_setup_fwd_listener(int type, const char *listen_addr, >+ u_short listen_port, int *allocated_listen_port, > const char *host_to_connect, u_short port_to_connect, int gateway_ports) > { > Channel *c; >@@ -2446,6 +2447,7 @@ channel_setup_fwd_listener(int type, con > struct addrinfo hints, *ai, *aitop; > const char *host, *addr; > char ntop[NI_MAXHOST], strport[NI_MAXSERV]; >+ in_port_t *lport_p; > > host = (type == SSH_CHANNEL_RPORT_LISTENER) ? > listen_addr : host_to_connect; >@@ -2514,10 +2516,29 @@ channel_setup_fwd_listener(int type, con > } > return 0; > } >- >+ if (allocated_listen_port != NULL) >+ *allocated_listen_port = 0; > for (ai = aitop; ai; ai = ai->ai_next) { >- if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) >+ switch (ai->ai_family) { >+ case AF_INET: >+ lport_p = &((struct sockaddr_in *)ai->ai_addr)-> >+ sin_port; >+ break; >+ case AF_INET6: >+ lport_p = &((struct sockaddr_in6 *)ai->ai_addr)-> >+ sin6_port; >+ break; >+ default: > continue; >+ } >+ /* >+ * If allocating a port for -R forwards, then use the >+ * same port for all address families. >+ */ >+ if (type == SSH_CHANNEL_RPORT_LISTENER && listen_port == 0 && >+ allocated_listen_port != NULL && *allocated_listen_port > 0) >+ *lport_p = htons(*allocated_listen_port); >+ > if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), > strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) { > error("channel_setup_fwd_listener: getnameinfo failed"); >@@ -2533,7 +2554,8 @@ channel_setup_fwd_listener(int type, con > > channel_set_reuseaddr(sock); > >- debug("Local forwarding listening on %s port %s.", ntop, strport); >+ debug("Local forwarding listening on %s port %s.", >+ ntop, strport); > > /* Bind the socket to the address. */ > if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { >@@ -2548,6 +2570,19 @@ channel_setup_fwd_listener(int type, con > close(sock); > continue; > } >+ >+ /* >+ * listen_port == 0 requests a dynamically allocated port - >+ * record what we got. >+ */ >+ if (type == SSH_CHANNEL_RPORT_LISTENER && listen_port == 0 && >+ allocated_listen_port != NULL && >+ *allocated_listen_port == 0) { >+ *allocated_listen_port = get_sock_port(sock, 1); >+ debug("Allocated listen port %d", >+ *allocated_listen_port); >+ } >+ > /* Allocate a channel number for the socket. */ > c = channel_new("port listener", type, sock, sock, -1, > CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, >@@ -2590,17 +2625,18 @@ channel_setup_local_fwd_listener(const c > const char *host_to_connect, u_short port_to_connect, int gateway_ports) > { > return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER, >- listen_host, listen_port, host_to_connect, port_to_connect, >+ listen_host, listen_port, NULL, host_to_connect, port_to_connect, > gateway_ports); > } > > /* protocol v2 remote port fwd, used by sshd */ > int > channel_setup_remote_fwd_listener(const char *listen_address, >- u_short listen_port, int gateway_ports) >+ u_short listen_port, int *allocated_listen_port, int gateway_ports) > { > return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER, >- listen_address, listen_port, NULL, 0, gateway_ports); >+ listen_address, listen_port, allocated_listen_port, >+ NULL, 0, gateway_ports); > } > > /* >Index: channels.h >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/channels.h,v >retrieving revision 1.97 >diff -u -p -r1.97 channels.h >--- channels.h 22 Jan 2009 09:46:01 -0000 1.97 >+++ channels.h 22 Jan 2009 10:12:49 -0000 >@@ -244,7 +244,7 @@ int channel_request_remote_forwarding(c > int channel_setup_local_fwd_listener(const char *, u_short, > const char *, u_short, int); > void channel_request_rforward_cancel(const char *host, u_short port); >-int channel_setup_remote_fwd_listener(const char *, u_short, int); >+int channel_setup_remote_fwd_listener(const char *, u_short, int *, int); > int channel_cancel_rport_listener(const char *, u_short); > > /* x11 forwarding */ >Index: clientloop.c >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/clientloop.c,v >retrieving revision 1.208 >diff -u -p -r1.208 clientloop.c >--- clientloop.c 22 Jan 2009 10:02:34 -0000 1.208 >+++ clientloop.c 22 Jan 2009 10:12:50 -0000 >@@ -840,7 +840,7 @@ process_cmdline(void) > } > channel_request_rforward_cancel(cancel_host, cancel_port); > } else { >- if (!parse_forward(&fwd, s, dynamic ? 1 : 0)) { >+ if (!parse_forward(&fwd, s, dynamic, remote)) { > logit("Bad forwarding specification."); > goto out; > } >Index: readconf.c >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/readconf.c,v >retrieving revision 1.175 >diff -u -p -r1.175 readconf.c >--- readconf.c 22 Jan 2009 10:02:34 -0000 1.175 >+++ readconf.c 22 Jan 2009 10:12:50 -0000 >@@ -730,7 +730,8 @@ parse_int: > } > > if (parse_forward(&fwd, fwdarg, >- opcode == oDynamicForward ? 1 : 0) == 0) >+ opcode == oDynamicForward ? 1 : 0, >+ opcode == oRemoteForward ? 1 : 0) == 0) > fatal("%.200s line %d: Bad forwarding specification.", > filename, linenum); > >@@ -1215,7 +1216,7 @@ fill_default_options(Options * options) > * returns number of arguments parsed or zero on error > */ > int >-parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd) >+parse_forward(Forward *fwd, const char *fwdspec, int dynamicfwd, int remotefwd) > { > int i; > char *p, *cp, *fwdarg[4]; >@@ -1278,12 +1279,16 @@ parse_forward(Forward *fwd, const char * > goto fail_free; > } > >- if (fwd->listen_port <= 0) >+ if (fwd->listen_port < 0 || (!remotefwd && fwd->listen_port == 0)) > goto fail_free; > > if (fwd->connect_host != NULL && > strlen(fwd->connect_host) >= NI_MAXHOST) > goto fail_free; >+ if (fwd->listen_host != NULL && >+ strlen(fwd->listen_host) >= NI_MAXHOST) >+ goto fail_free; >+ > > return (i); > >Index: readconf.h >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/readconf.h,v >retrieving revision 1.77 >diff -u -p -r1.77 readconf.h >--- readconf.h 22 Jan 2009 10:02:34 -0000 1.77 >+++ readconf.h 22 Jan 2009 10:12:50 -0000 >@@ -134,7 +134,7 @@ typedef struct { > void initialize_options(Options *); > void fill_default_options(Options *); > int read_config_file(const char *, const char *, Options *, int); >-int parse_forward(Forward *, const char *, int); >+int parse_forward(Forward *, const char *, int, int); > > int > process_config_line(Options *, const char *, char *, const char *, int, int *); >Index: serverloop.c >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/serverloop.c,v >retrieving revision 1.155 >diff -u -p -r1.155 serverloop.c >--- serverloop.c 22 Jan 2009 10:02:34 -0000 1.155 >+++ serverloop.c 22 Jan 2009 10:12:50 -0000 >@@ -1058,7 +1058,7 @@ server_input_global_request(int type, u_ > { > char *rtype; > int want_reply; >- int success = 0; >+ int success = 0, allocated_listen_port = 0; > > rtype = packet_get_string(NULL); > want_reply = packet_get_char(); >@@ -1081,13 +1081,15 @@ server_input_global_request(int type, u_ > /* check permissions */ > if (!options.allow_tcp_forwarding || > no_port_forwarding_flag || >- (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) { >+ (listen_port != 0 && listen_port < IPPORT_RESERVED && >+ pw->pw_uid != 0)) { > success = 0; > packet_send_debug("Server has disabled port forwarding."); > } else { > /* Start listening on the port */ > success = channel_setup_remote_fwd_listener( >- listen_address, listen_port, options.gateway_ports); >+ listen_address, listen_port, >+ &allocated_listen_port, options.gateway_ports); > } > xfree(listen_address); > } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { >@@ -1109,6 +1111,8 @@ server_input_global_request(int type, u_ > if (want_reply) { > packet_start(success ? > SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); >+ if (success && allocated_listen_port > 0) >+ packet_put_int(allocated_listen_port); > packet_send(); > packet_write_wait(); > } >Index: ssh.c >=================================================================== >RCS file: /cvs/src/usr.bin/ssh/ssh.c,v >retrieving revision 1.323 >diff -u -p -r1.323 ssh.c >--- ssh.c 22 Jan 2009 10:02:34 -0000 1.323 >+++ ssh.c 22 Jan 2009 10:12:51 -0000 >@@ -440,7 +440,7 @@ main(int ac, char **av) > break; > > case 'L': >- if (parse_forward(&fwd, optarg, 0)) >+ if (parse_forward(&fwd, optarg, 0, 0)) > add_local_forward(&options, &fwd); > else { > fprintf(stderr, >@@ -451,7 +451,7 @@ main(int ac, char **av) > break; > > case 'R': >- if (parse_forward(&fwd, optarg, 0)) { >+ if (parse_forward(&fwd, optarg, 0, 1)) { > add_remote_forward(&options, &fwd); > } else { > fprintf(stderr, >@@ -462,7 +462,7 @@ main(int ac, char **av) > break; > > case 'D': >- if (parse_forward(&fwd, optarg, 1)) { >+ if (parse_forward(&fwd, optarg, 1, 0)) { > add_local_forward(&options, &fwd); > } else { > fprintf(stderr, >@@ -818,9 +818,16 @@ ssh_confirm_remote_forward(int type, u_i > { > Forward *rfwd = (Forward *)ctxt; > >+ /* XXX verbose() on failure? */ > debug("remote forward %s for: listen %d, connect %s:%d", > type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure", > rfwd->listen_port, rfwd->connect_host, rfwd->connect_port); >+ if (type == SSH2_MSG_REQUEST_SUCCESS && rfwd->listen_port == 0) { >+ logit("Allocated port %u for remote forward to %s:%d", >+ packet_get_int(), >+ rfwd->connect_host, rfwd->connect_port); >+ } >+ > if (type == SSH2_MSG_REQUEST_FAILURE) { > if (options.exit_on_forward_failure) > fatal("Error: remote port forwarding failed for "
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 1003
:
858
|
879
|
1255
|
1595
| 1596