Lines Matching defs:ssh
14 * called by a name other than "ssh" or "Secure Shell".
67 #include "ssh.h"
95 typedef void chan_fn(struct ssh *, Channel *c);
210 static void port_open_helper(struct ssh *ssh, Channel *c, char *rtype);
216 static Channel *rdynamic_connect_prepare(struct ssh *, char *, char *);
217 static int rdynamic_connect_finish(struct ssh *, Channel *);
225 channel_init_channels(struct ssh *ssh)
236 ssh->chanctxt = sc;
240 channel_by_id(struct ssh *ssh, int id)
244 if (id < 0 || (u_int)id >= ssh->chanctxt->channels_alloc) {
248 c = ssh->chanctxt->channels[id];
257 channel_by_remote_id(struct ssh *ssh, u_int remote_id)
262 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
263 c = ssh->chanctxt->channels[i];
275 channel_lookup(struct ssh *ssh, int id)
279 if ((c = channel_by_id(ssh, id)) == NULL)
304 channel_add_timeout(struct ssh *ssh, const char *type_pattern,
307 struct ssh_channels *sc = ssh->chanctxt;
325 channel_clear_timeouts(struct ssh *ssh)
327 struct ssh_channels *sc = ssh->chanctxt;
339 lookup_timeout(struct ssh *ssh, const char *type)
341 struct ssh_channels *sc = ssh->chanctxt;
359 channel_set_xtype(struct ssh *ssh, int id, const char *xctype)
363 if ((c = channel_by_id(ssh, id)) == NULL)
369 c->inactive_deadline = lookup_timeout(ssh, c->xctype);
379 channel_set_used_time(struct ssh *ssh, Channel *c)
381 ssh->chanctxt->lastused = monotime();
383 c->lastused = ssh->chanctxt->lastused;
391 channel_get_expiry(struct ssh *ssh, Channel *c)
393 struct ssh_channels *sc = ssh->chanctxt;
411 channel_register_fds(struct ssh *ssh, Channel *c, int rfd, int wfd, int efd,
468 channel_set_used_time(ssh, NULL);
475 channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
479 struct ssh_channels *sc = ssh->chanctxt;
516 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, 0);
526 c->inactive_deadline = lookup_timeout(ssh, c->ctype);
534 channel_close_fd(struct ssh *ssh, Channel *c, int *fdp)
584 channel_close_fds(struct ssh *ssh, Channel *c)
588 channel_close_fd(ssh, c, &c->sock);
590 channel_close_fd(ssh, c, &c->rfd);
592 channel_close_fd(ssh, c, &c->wfd);
594 channel_close_fd(ssh, c, &c->efd);
626 permission_set_get(struct ssh *ssh, int where)
628 struct ssh_channels *sc = ssh->chanctxt;
644 permission_set_get_array(struct ssh *ssh, int who, int where,
647 struct permission_set *pset = permission_set_get(ssh, where);
665 permission_set_add(struct ssh *ssh, int who, int where,
673 permission_set_get_array(ssh, who, where, &permp, &npermp);
692 mux_remove_remote_forwardings(struct ssh *ssh, Channel *c)
694 struct ssh_channels *sc = ssh->chanctxt;
708 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
709 (r = sshpkt_put_cstring(ssh,
711 (r = sshpkt_put_u8(ssh, 0)) != 0 ||
712 (r = sshpkt_put_cstring(ssh,
714 (r = sshpkt_put_u32(ssh, perm->listen_port)) != 0 ||
715 (r = sshpkt_send(ssh)) != 0) {
724 channel_free(struct ssh *ssh, Channel *c)
726 struct ssh_channels *sc = ssh->chanctxt;
750 mux_remove_remote_forwardings(ssh, c);
759 s = channel_open_message(ssh);
764 channel_close_fds(ssh, c);
779 cc->abandon_cb(ssh, c, cc->ctx);
784 c->filter_cleanup(ssh, c->self, c->filter_ctx);
790 channel_free_all(struct ssh *ssh)
793 struct ssh_channels *sc = ssh->chanctxt;
797 channel_free(ssh, sc->channels[i]);
823 channel_close_all(struct ssh *ssh)
827 for (i = 0; i < ssh->chanctxt->channels_alloc; i++)
828 if (ssh->chanctxt->channels[i] != NULL)
829 channel_close_fds(ssh, ssh->chanctxt->channels[i]);
836 channel_stop_listening(struct ssh *ssh)
841 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
842 c = ssh->chanctxt->channels[i];
851 channel_close_fd(ssh, c, &c->sock);
852 channel_free(ssh, c);
864 channel_not_very_much_buffered_data(struct ssh *ssh)
867 u_int maxsize = ssh_packet_get_maxsize(ssh);
870 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
871 c = ssh->chanctxt->channels[i];
885 channel_still_open(struct ssh *ssh)
890 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
891 c = ssh->chanctxt->channels[i];
928 channel_tty_open(struct ssh *ssh)
933 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
934 c = ssh->chanctxt->channels[i];
945 channel_find_open(struct ssh *ssh)
950 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
951 c = ssh->chanctxt->channels[i];
1029 channel_open_message(struct ssh *ssh)
1042 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
1043 c = ssh->chanctxt->channels[i];
1088 open_preamble(struct ssh *ssh, const char *where, Channel *c, const char *type)
1092 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1093 (r = sshpkt_put_cstring(ssh, type)) != 0 ||
1094 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1095 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1096 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0) {
1102 channel_send_open(struct ssh *ssh, int id)
1104 Channel *c = channel_lookup(ssh, id);
1112 open_preamble(ssh, __func__, c, c->ctype);
1113 if ((r = sshpkt_send(ssh)) != 0)
1118 channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
1120 Channel *c = channel_lookup(ssh, id);
1131 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
1132 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1133 (r = sshpkt_put_cstring(ssh, service)) != 0 ||
1134 (r = sshpkt_put_u8(ssh, wantconfirm)) != 0) {
1140 channel_register_status_confirm(struct ssh *ssh, int id,
1146 if ((c = channel_lookup(ssh, id)) == NULL)
1157 channel_register_open_confirm(struct ssh *ssh, int id,
1160 Channel *c = channel_lookup(ssh, id);
1171 channel_register_cleanup(struct ssh *ssh, int id,
1174 Channel *c = channel_by_id(ssh, id);
1185 channel_cancel_cleanup(struct ssh *ssh, int id)
1187 Channel *c = channel_by_id(ssh, id);
1198 channel_register_filter(struct ssh *ssh, int id, channel_infilter_fn *ifn,
1201 Channel *c = channel_lookup(ssh, id);
1214 channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
1217 Channel *c = channel_lookup(ssh, id);
1225 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
1227 channel_set_used_time(ssh, c);
1230 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1231 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1232 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1233 (r = sshpkt_send(ssh)) != 0)
1238 channel_pre_listener(struct ssh *ssh, Channel *c)
1244 channel_pre_connecting(struct ssh *ssh, Channel *c)
1251 channel_pre_open(struct ssh *ssh, Channel *c)
1269 chan_obuf_empty(ssh, c);
1297 x11_open_helper(struct ssh *ssh, struct sshbuf *b)
1299 struct ssh_channels *sc = ssh->chanctxt;
1364 channel_force_close(struct ssh *ssh, Channel *c, int abandon)
1368 chan_read_failed(ssh, c);
1371 chan_ibuf_empty(ssh, c);
1376 chan_write_failed(ssh, c);
1379 c->detach_user(ssh, c->self, 1, NULL);
1381 channel_close_fd(ssh, c, &c->efd);
1390 channel_pre_x11_open(struct ssh *ssh, Channel *c)
1392 int ret = x11_open_helper(ssh, c->output);
1398 channel_set_used_time(ssh, c);
1399 channel_pre_open(ssh, c);
1405 channel_force_close(ssh, c, 0);
1410 channel_pre_mux_client(struct ssh *ssh, Channel *c)
1419 chan_ibuf_empty(ssh, c);
1421 chan_rcvd_oclose(ssh, c);
1428 chan_obuf_empty(ssh, c);
1680 channel_connect_stdio_fwd(struct ssh *ssh,
1688 c = channel_new(ssh, "stdio-forward", SSH_CHANNEL_OPENING, in, out,
1697 channel_register_fds(ssh, c, in, out, -1, 0, 1, 0);
1698 port_open_helper(ssh, c, port_to_connect == PORT_STREAMLOCAL ?
1706 channel_pre_dynamic(struct ssh *ssh, Channel *c)
1737 chan_mark_dead(ssh, c);
1747 port_open_helper(ssh, c, "direct-tcpip");
1753 rdynamic_close(struct ssh *ssh, Channel *c)
1756 channel_force_close(ssh, c, 0);
1761 channel_before_prepare_io_rdynamic(struct ssh *ssh, Channel *c)
1774 rdynamic_close(ssh, c);
1795 rdynamic_close(ssh, c);
1801 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
1802 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1803 (r = sshpkt_put_stringb(ssh, c->input)) != 0 ||
1804 (r = sshpkt_send(ssh)) != 0) {
1811 } else if (rdynamic_connect_finish(ssh, c) < 0) {
1813 rdynamic_close(ssh, c);
1819 channel_post_x11_listener(struct ssh *ssh, Channel *c)
1836 channel_close_fd(ssh, c, &c->sock);
1837 chan_mark_dead(ssh, c);
1854 nc = channel_new(ssh, "x11-connection",
1857 open_preamble(ssh, __func__, nc, "x11");
1858 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1859 (r = sshpkt_put_u32(ssh, remote_port)) != 0) {
1862 if ((r = sshpkt_send(ssh)) != 0)
1868 port_open_helper(struct ssh *ssh, Channel *c, char *rtype)
1890 open_preamble(ssh, __func__, c, rtype);
1893 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1894 (r = sshpkt_put_u32(ssh, c->host_port)) != 0)
1898 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0)
1902 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0)
1906 if ((r = sshpkt_put_cstring(ssh, c->path)) != 0 ||
1907 (r = sshpkt_put_u32(ssh, local_port)) != 0)
1912 if ((r = sshpkt_put_cstring(ssh, "")) != 0)
1916 if ((r = sshpkt_put_cstring(ssh, remote_ipaddr)) != 0 ||
1917 (r = sshpkt_put_u32(ssh, (u_int)remote_port)) != 0)
1920 if ((r = sshpkt_send(ssh)) != 0)
1927 channel_set_x11_refuse_time(struct ssh *ssh, time_t refuse_time)
1929 ssh->chanctxt->x11_refuse_time = refuse_time;
1936 channel_post_port_listener(struct ssh *ssh, Channel *c)
1979 nc = channel_new(ssh, rtype, nextstate, newsock, newsock, -1,
1987 port_open_helper(ssh, nc, rtype);
1995 channel_post_auth_listener(struct ssh *ssh, Channel *c)
2013 nc = channel_new(ssh, "agent-connection",
2017 open_preamble(ssh, __func__, nc, "auth-agent@openssh.com");
2018 if ((r = sshpkt_send(ssh)) != 0)
2023 channel_post_connecting(struct ssh *ssh, Channel *c)
2046 channel_set_used_time(ssh, c);
2050 if ((r = sshpkt_start(ssh,
2052 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2053 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
2054 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
2055 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
2056 (r = sshpkt_send(ssh)) != 0)
2074 rdynamic_close(ssh, c);
2076 if ((r = sshpkt_start(ssh,
2078 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2079 (r = sshpkt_put_u32(ssh,
2081 (r = sshpkt_put_cstring(ssh, strerror(err))) != 0 ||
2082 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2083 (r = sshpkt_send(ssh)) != 0)
2085 chan_mark_dead(ssh, c);
2095 channel_handle_rfd(struct ssh *ssh, Channel *c)
2129 channel_set_used_time(ssh, c);
2143 chan_mark_dead(ssh, c);
2146 chan_read_failed(ssh, c);
2150 channel_set_used_time(ssh, c);
2152 if (c->input_filter(ssh, c, buf, len) == -1) {
2154 chan_read_failed(ssh, c);
2164 channel_handle_wfd(struct ssh *ssh, Channel *c)
2179 if ((buf = c->output_filter(ssh, c, &data, &dlen)) == NULL) {
2182 chan_mark_dead(ssh, c);
2184 chan_write_failed(ssh, c);
2214 chan_mark_dead(ssh, c);
2217 chan_write_failed(ssh, c);
2221 channel_set_used_time(ssh, c);
2231 if ((r = sshpkt_msg_ignore(ssh, 4+len)) != 0 ||
2232 (r = sshpkt_send(ssh)) != 0)
2245 channel_handle_efd_write(struct ssh *ssh, Channel *c)
2262 channel_close_fd(ssh, c, &c->efd);
2267 channel_set_used_time(ssh, c);
2273 channel_handle_efd_read(struct ssh *ssh, Channel *c)
2288 channel_close_fd(ssh, c, &c->efd);
2291 channel_set_used_time(ssh, c);
2300 channel_handle_efd(struct ssh *ssh, Channel *c)
2308 return channel_handle_efd_write(ssh, c);
2311 return channel_handle_efd_read(ssh, c);
2317 channel_check_window(struct ssh *ssh, Channel *c)
2329 if ((r = sshpkt_start(ssh,
2331 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2332 (r = sshpkt_put_u32(ssh, c->local_consumed)) != 0 ||
2333 (r = sshpkt_send(ssh)) != 0) {
2345 channel_post_open(struct ssh *ssh, Channel *c)
2347 channel_handle_rfd(ssh, c);
2348 channel_handle_wfd(ssh, c);
2349 channel_handle_efd(ssh, c);
2350 channel_check_window(ssh, c);
2354 read_mux(struct ssh *ssh, Channel *c, u_int need)
2369 chan_read_failed(ssh, c);
2378 channel_post_mux_client_read(struct ssh *ssh, Channel *c)
2393 if (read_mux(ssh, c, 4) < 4) /* read header */
2401 chan_rcvd_oclose(ssh, c);
2404 if (read_mux(ssh, c, need + 4) < need + 4) /* read body */
2406 if (c->mux_rcb(ssh, c) != 0) {
2408 chan_mark_dead(ssh, c);
2414 channel_post_mux_client_write(struct ssh *ssh, Channel *c)
2428 chan_mark_dead(ssh, c);
2436 channel_post_mux_client(struct ssh *ssh, Channel *c)
2438 channel_post_mux_client_read(ssh, c);
2439 channel_post_mux_client_write(ssh, c);
2443 channel_post_mux_listener(struct ssh *ssh, Channel *c)
2481 nc = channel_new(ssh, "mux-control", SSH_CHANNEL_MUX_CLIENT,
2487 nc->mux_rcb(ssh, nc);
2534 channel_garbage_collect(struct ssh *ssh, Channel *c)
2539 if (!chan_is_dead(ssh, c, c->detach_close))
2543 c->detach_user(ssh, c->self, 0, NULL);
2549 if (!chan_is_dead(ssh, c, 1))
2552 channel_free(ssh, c);
2558 channel_handler(struct ssh *ssh, int table, struct timespec *timeout)
2560 struct ssh_channels *sc = ssh->chanctxt;
2572 if (ssh_packet_is_rekeying(ssh) && c->type != SSH_CHANNEL_OPEN)
2582 channel_get_expiry(ssh, c) != 0 &&
2583 now >= channel_get_expiry(ssh, c)) {
2588 channel_force_close(ssh, c, 1);
2591 (*ftab[c->type])(ssh, c);
2595 channel_get_expiry(ssh, c) != 0) {
2597 channel_get_expiry(ssh, c));
2608 channel_garbage_collect(ssh, c);
2619 channel_before_prepare_io(struct ssh *ssh)
2621 struct ssh_channels *sc = ssh->chanctxt;
2630 channel_before_prepare_io_rdynamic(ssh, c);
2754 channel_prepare_poll(struct ssh *ssh, struct pollfd **pfdp, u_int *npfd_allocp,
2757 struct ssh_channels *sc = ssh->chanctxt;
2760 channel_before_prepare_io(ssh); /* might create a new channel */
2779 channel_handler(ssh, CHAN_PRE, timeout);
2823 channel_after_poll(struct ssh *ssh, struct pollfd *pfd, u_int npfd)
2825 struct ssh_channels *sc = ssh->chanctxt;
2900 channel_handler(ssh, CHAN_POST, NULL);
2908 channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2928 chan_ibuf_empty(ssh, c);
2950 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2951 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2952 (r = sshpkt_put_string(ssh, pkt, plen)) != 0 ||
2953 (r = sshpkt_send(ssh)) != 0)
2966 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_DATA)) != 0 ||
2967 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2968 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->input), len)) != 0 ||
2969 (r = sshpkt_send(ssh)) != 0)
2982 channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
3000 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
3001 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
3002 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
3003 (r = sshpkt_put_string(ssh, sshbuf_ptr(c->extended), len)) != 0 ||
3004 (r = sshpkt_send(ssh)) != 0)
3018 channel_output_poll(struct ssh *ssh)
3020 struct ssh_channels *sc = ssh->chanctxt;
3046 ret |= channel_output_poll_input_open(ssh, c);
3050 ret |= channel_output_poll_extended_read(ssh, c);
3060 * from the ssh server:
3096 channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
3137 c = channel_new(ssh, "mux-proxy", SSH_CHANNEL_MUX_PROXY,
3145 channel_free(ssh, c);
3164 c = channel_new(ssh, "mux-proxy", SSH_CHANNEL_MUX_PROXY,
3174 channel_free(ssh, c);
3203 permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL, "<mux>",
3210 if ((c = channel_by_remote_id(ssh, remote_id)) != NULL) {
3212 channel_free(ssh, c);
3219 if ((r = sshpkt_start(ssh, type)) != 0 ||
3220 (r = sshpkt_putb(ssh, modified)) != 0 ||
3221 (r = sshpkt_send(ssh)) != 0) {
3226 if ((r = sshpkt_start(ssh, type)) != 0 ||
3227 (r = sshpkt_put(ssh, cp, have)) != 0 ||
3228 (r = sshpkt_send(ssh)) != 0) {
3249 channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
3288 cp = sshpkt_ptr(ssh, &len);
3317 channel_free(ssh, c);
3330 channel_parse_id(struct ssh *ssh, const char *where, const char *what)
3335 if ((r = sshpkt_get_u32(ssh, &id)) != 0) {
3337 ssh_packet_disconnect(ssh, "Invalid %s message", what);
3341 ssh_packet_disconnect(ssh, "Invalid %s channel id", what);
3348 channel_from_packet_id(struct ssh *ssh, const char *where, const char *what)
3350 int id = channel_parse_id(ssh, where, what);
3353 if ((c = channel_lookup(ssh, id)) == NULL) {
3354 ssh_packet_disconnect(ssh,
3361 channel_input_data(int type, u_int32_t seq, struct ssh *ssh)
3365 Channel *c = channel_from_packet_id(ssh, __func__, "data");
3368 if (channel_proxy_upstream(c, type, seq, ssh))
3379 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3380 (r = sshpkt_get_end(ssh)) != 0)
3411 ssh_packet_disconnect(ssh, "channel %d: peer ignored "
3429 channel_input_extended_data(int type, u_int32_t seq, struct ssh *ssh)
3434 Channel *c = channel_from_packet_id(ssh, __func__, "extended data");
3437 if (channel_proxy_upstream(c, type, seq, ssh))
3444 if (ssh->compat & SSH_BUG_EXTEOF)
3448 ssh_packet_disconnect(ssh, "Received extended_data "
3452 if ((r = sshpkt_get_u32(ssh, &tcode)) != 0) {
3454 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3462 if ((r = sshpkt_get_string_direct(ssh, &data, &data_len)) != 0 ||
3463 (r = sshpkt_get_end(ssh)) != 0) {
3465 ssh_packet_disconnect(ssh, "Invalid extended_data message");
3482 channel_input_ieof(int type, u_int32_t seq, struct ssh *ssh)
3484 Channel *c = channel_from_packet_id(ssh, __func__, "ieof");
3487 if ((r = sshpkt_get_end(ssh)) != 0) {
3489 ssh_packet_disconnect(ssh, "Invalid ieof message");
3492 if (channel_proxy_upstream(c, type, seq, ssh))
3494 chan_rcvd_ieof(ssh, c);
3501 chan_ibuf_empty(ssh, c);
3507 channel_input_oclose(int type, u_int32_t seq, struct ssh *ssh)
3509 Channel *c = channel_from_packet_id(ssh, __func__, "oclose");
3512 if (channel_proxy_upstream(c, type, seq, ssh))
3514 if ((r = sshpkt_get_end(ssh)) != 0) {
3516 ssh_packet_disconnect(ssh, "Invalid oclose message");
3518 chan_rcvd_oclose(ssh, c);
3523 channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
3525 Channel *c = channel_from_packet_id(ssh, __func__, "open confirmation");
3529 if (channel_proxy_upstream(c, type, seq, ssh))
3532 ssh_packet_disconnect(ssh, "Received open confirmation for "
3538 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
3539 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
3540 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0 ||
3541 (r = sshpkt_get_end(ssh)) != 0) {
3543 ssh_packet_disconnect(ssh, "Invalid open confirmation message");
3552 c->open_confirm(ssh, c->self, 1, c->open_confirm_ctx);
3555 channel_set_used_time(ssh, c);
3578 channel_input_open_failure(int type, u_int32_t seq, struct ssh *ssh)
3580 Channel *c = channel_from_packet_id(ssh, __func__, "open failure");
3585 if (channel_proxy_upstream(c, type, seq, ssh))
3588 ssh_packet_disconnect(ssh, "Received open failure for "
3590 if ((r = sshpkt_get_u32(ssh, &reason)) != 0) {
3592 ssh_packet_disconnect(ssh, "Invalid open failure message");
3595 if ((r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
3596 (r = sshpkt_get_string_direct(ssh, NULL, NULL)) != 0 ||
3597 (r = sshpkt_get_end(ssh)) != 0) {
3599 ssh_packet_disconnect(ssh, "Invalid open failure message");
3606 c->open_confirm(ssh, c->self, 0, c->open_confirm_ctx);
3610 chan_mark_dead(ssh, c);
3615 channel_input_window_adjust(int type, u_int32_t seq, struct ssh *ssh)
3617 int id = channel_parse_id(ssh, __func__, "window adjust");
3623 if ((c = channel_lookup(ssh, id)) == NULL) {
3628 if (channel_proxy_upstream(c, type, seq, ssh))
3630 if ((r = sshpkt_get_u32(ssh, &adjust)) != 0 ||
3631 (r = sshpkt_get_end(ssh)) != 0) {
3633 ssh_packet_disconnect(ssh, "Invalid window adjust message");
3645 channel_input_status_confirm(int type, u_int32_t seq, struct ssh *ssh)
3647 int id = channel_parse_id(ssh, __func__, "status confirm");
3652 ssh_packet_set_alive_timeouts(ssh, 0);
3656 if ((c = channel_lookup(ssh, id)) == NULL) {
3660 if (channel_proxy_upstream(c, type, seq, ssh))
3662 if (sshpkt_get_end(ssh) != 0)
3663 ssh_packet_disconnect(ssh, "Invalid status confirm message");
3666 cc->cb(ssh, type, c, cc->ctx);
3675 channel_set_af(struct ssh *ssh, int af)
3677 ssh->chanctxt->IPv4or6 = af;
3697 channel_fwd_bind_addr(struct ssh *ssh, const char *listen_addr, int *wildcardp,
3708 if (((ssh->compat & SSH_OLD_FORWARD_ADDR) &&
3720 ssh_packet_send_debug(ssh,
3751 channel_setup_fwd_listener_tcpip(struct ssh *ssh, int type,
3780 addr = channel_fwd_bind_addr(ssh, fwd->listen_host, &wildcard,
3790 hints.ai_family = ssh->chanctxt->IPv4or6;
3797 ssh_packet_disconnect(ssh, "getaddrinfo: fatal error: %s",
3882 c = channel_new(ssh, "port-listener", type, sock, sock, -1,
3889 !(ssh->compat & SSH_BUG_DYNAMIC_RPORT))
3902 channel_setup_fwd_listener_streamlocal(struct ssh *ssh, int type,
3965 c = channel_new(ssh, "unix-listener", type, sock, sock, -1,
3976 channel_cancel_rport_listener_tcpip(struct ssh *ssh,
3982 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
3983 Channel *c = ssh->chanctxt->channels[i];
3988 channel_free(ssh, c);
3997 channel_cancel_rport_listener_streamlocal(struct ssh *ssh, const char *path)
4002 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
4003 Channel *c = ssh->chanctxt->channels[i];
4010 channel_free(ssh, c);
4019 channel_cancel_rport_listener(struct ssh *ssh, struct Forward *fwd)
4022 return channel_cancel_rport_listener_streamlocal(ssh,
4025 return channel_cancel_rport_listener_tcpip(ssh,
4031 channel_cancel_lport_listener_tcpip(struct ssh *ssh,
4037 const char *addr = channel_fwd_bind_addr(ssh, lhost, NULL, 1, fwd_opts);
4039 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
4040 Channel *c = ssh->chanctxt->channels[i];
4058 channel_free(ssh, c);
4067 channel_cancel_lport_listener_streamlocal(struct ssh *ssh, const char *path)
4077 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
4078 Channel *c = ssh->chanctxt->channels[i];
4085 channel_free(ssh, c);
4094 channel_cancel_lport_listener(struct ssh *ssh,
4098 return channel_cancel_lport_listener_streamlocal(ssh,
4101 return channel_cancel_lport_listener_tcpip(ssh,
4106 /* protocol local port fwd, used by ssh */
4108 channel_setup_local_fwd_listener(struct ssh *ssh,
4112 return channel_setup_fwd_listener_streamlocal(ssh,
4115 return channel_setup_fwd_listener_tcpip(ssh,
4149 check_rfwd_permission(struct ssh *ssh, struct Forward *fwd)
4151 struct ssh_channels *sc = ssh->chanctxt;
4185 channel_setup_remote_fwd_listener(struct ssh *ssh, struct Forward *fwd,
4188 if (!check_rfwd_permission(ssh, fwd)) {
4189 ssh_packet_send_debug(ssh, "port forwarding refused");
4195 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
4201 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
4206 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
4210 return channel_setup_fwd_listener_streamlocal(ssh,
4213 return channel_setup_fwd_listener_tcpip(ssh,
4241 channel_request_remote_forwarding(struct ssh *ssh, struct Forward *fwd)
4249 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4250 (r = sshpkt_put_cstring(ssh,
4252 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
4253 (r = sshpkt_put_cstring(ssh, fwd->listen_path)) != 0 ||
4254 (r = sshpkt_send(ssh)) != 0 ||
4255 (r = ssh_packet_write_wait(ssh)) != 0)
4258 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4259 (r = sshpkt_put_cstring(ssh, "tcpip-forward")) != 0 ||
4260 (r = sshpkt_put_u8(ssh, 1)) != 0 || /* want reply */
4261 (r = sshpkt_put_cstring(ssh,
4263 (r = sshpkt_put_u32(ssh, fwd->listen_port)) != 0 ||
4264 (r = sshpkt_send(ssh)) != 0 ||
4265 (r = ssh_packet_write_wait(ssh)) != 0)
4288 idx = permission_set_add(ssh, FORWARD_USER, FORWARD_LOCAL,
4357 channel_request_rforward_cancel_tcpip(struct ssh *ssh,
4360 struct ssh_channels *sc = ssh->chanctxt;
4376 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4377 (r = sshpkt_put_cstring(ssh, "cancel-tcpip-forward")) != 0 ||
4378 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4379 (r = sshpkt_put_cstring(ssh, channel_rfwd_bind_host(host))) != 0 ||
4380 (r = sshpkt_put_u32(ssh, port)) != 0 ||
4381 (r = sshpkt_send(ssh)) != 0)
4394 channel_request_rforward_cancel_streamlocal(struct ssh *ssh, const char *path)
4396 struct ssh_channels *sc = ssh->chanctxt;
4412 if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
4413 (r = sshpkt_put_cstring(ssh,
4415 (r = sshpkt_put_u8(ssh, 0)) != 0 || /* want reply */
4416 (r = sshpkt_put_cstring(ssh, path)) != 0 ||
4417 (r = sshpkt_send(ssh)) != 0)
4429 channel_request_rforward_cancel(struct ssh *ssh, struct Forward *fwd)
4432 return channel_request_rforward_cancel_streamlocal(ssh,
4435 return channel_request_rforward_cancel_tcpip(ssh,
4447 channel_permit_all(struct ssh *ssh, int where)
4449 struct permission_set *pset = permission_set_get(ssh, where);
4459 channel_add_permission(struct ssh *ssh, int who, int where,
4463 struct permission_set *pset = permission_set_get(ssh, where);
4471 permission_set_add(ssh, who, where,
4481 channel_disable_admin(struct ssh *ssh, int where)
4483 channel_clear_permission(ssh, FORWARD_ADM, where);
4484 permission_set_add(ssh, FORWARD_ADM, where,
4492 channel_clear_permission(struct ssh *ssh, int who, int where)
4497 permission_set_get_array(ssh, who, where, &permp, &npermp);
4508 channel_update_permission(struct ssh *ssh, int idx, int newport)
4510 struct permission_set *pset = &ssh->chanctxt->local_perms;
4526 (ssh->compat & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
4608 connect_to_helper(struct ssh *ssh, const char *name, int port, int socktype,
4644 hints.ai_family = ssh->chanctxt->IPv4or6;
4674 connect_to(struct ssh *ssh, const char *host, int port,
4682 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4688 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4702 channel_connect_by_listen_address(struct ssh *ssh, const char *listen_host,
4705 struct ssh_channels *sc = ssh->chanctxt;
4717 return rdynamic_connect_prepare(ssh,
4719 return connect_to(ssh,
4730 channel_connect_by_listen_path(struct ssh *ssh, const char *path,
4733 struct ssh_channels *sc = ssh->chanctxt;
4741 return connect_to(ssh,
4753 channel_connect_to_port(struct ssh *ssh, const char *host, u_short port,
4756 struct ssh_channels *sc = ssh->chanctxt;
4789 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), host, port);
4796 sock = connect_to_helper(ssh, host, port, SOCK_STREAM, ctype, rname,
4803 c = channel_new(ssh, ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4814 channel_connect_to_path(struct ssh *ssh, const char *path,
4817 struct ssh_channels *sc = ssh->chanctxt;
4849 return connect_to(ssh, path, PORT_STREAMLOCAL, ctype, rname);
4853 channel_send_window_changes(struct ssh *ssh)
4855 struct ssh_channels *sc = ssh->chanctxt;
4866 channel_request_start(ssh, i, "window-change", 0);
4867 if ((r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
4868 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
4869 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
4870 (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0 ||
4871 (r = sshpkt_send(ssh)) != 0)
4878 rdynamic_connect_prepare(struct ssh *ssh, char *ctype, char *rname)
4883 c = channel_new(ssh, ctype, SSH_CHANNEL_RDYNAMIC_OPEN, -1, -1, -1,
4892 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
4893 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
4894 (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
4895 (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
4896 (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0)
4903 rdynamic_connect_finish(struct ssh *ssh, Channel *c)
4905 struct ssh_channels *sc = ssh->chanctxt;
4928 sock = connect_to_helper(ssh, c->path, c->host_port, SOCK_STREAM, NULL,
4936 channel_register_fds(ssh, c, sock, sock, -1, 0, 1, 0);
4949 x11_create_display_inet(struct ssh *ssh, int x11_display_offset,
4968 hints.ai_family = ssh->chanctxt->IPv4or6;
5024 nc = channel_new(ssh, "x11-listener",
5058 x11_connect_display(struct ssh *ssh)
5123 hints.ai_family = ssh->chanctxt->IPv4or6;
5164 x11_request_forwarding_with_spoofing(struct ssh *ssh, int client_session_id,
5167 struct ssh_channels *sc = ssh->chanctxt;
5215 channel_request_start(ssh, client_session_id, "x11-req", want_reply);
5216 if ((r = sshpkt_put_u8(ssh, 0)) != 0 || /* bool: single connection */
5217 (r = sshpkt_put_cstring(ssh, proto)) != 0 ||
5218 (r = sshpkt_put_cstring(ssh, new_data)) != 0 ||
5219 (r = sshpkt_put_u32(ssh, screen_number)) != 0 ||
5220 (r = sshpkt_send(ssh)) != 0 ||
5221 (r = ssh_packet_write_wait(ssh)) != 0)
5230 x11_channel_used_recently(struct ssh *ssh) {
5235 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
5236 c = ssh->chanctxt->channels[i];