summaryrefslogtreecommitdiff
path: root/session.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2019-01-19 21:41:53 +0000
committerDamien Miller <djm@mindrot.org>2019-01-20 09:45:18 +1100
commita5e2ad88acff2b7d131ee6d5dc5d339b0f8c6a6d (patch)
tree977a18b418ad0879a86bc97a43645eac12d7b919 /session.c
parent3a00a921590d4c4b7e96df11bb10e6f9253ad45e (diff)
upstream: convert session.c to new packet API
with & ok markus@ OpenBSD-Commit-ID: fae817207e23099ddd248960c984f7b7f26ea68e
Diffstat (limited to 'session.c')
-rw-r--r--session.c151
1 files changed, 87 insertions, 64 deletions
diff --git a/session.c b/session.c
index 26ab6f6a0..b5a382473 100644
--- a/session.c
+++ b/session.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: session.c,v 1.311 2019/01/19 21:41:18 djm Exp $ */ 1/* $OpenBSD: session.c,v 1.312 2019/01/19 21:41:53 djm Exp $ */
2/* 2/*
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved 4 * All rights reserved
@@ -105,9 +105,6 @@
105#include <selinux/selinux.h> 105#include <selinux/selinux.h>
106#endif 106#endif
107 107
108#include "opacket.h" /* XXX */
109extern struct ssh *active_state; /* XXX */
110
111#define IS_INTERNAL_SFTP(c) \ 108#define IS_INTERNAL_SFTP(c) \
112 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \ 109 (!strncmp(c, INTERNAL_SFTP_NAME, sizeof(INTERNAL_SFTP_NAME) - 1) && \
113 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \ 110 (c[sizeof(INTERNAL_SFTP_NAME) - 1] == '\0' || \
@@ -207,7 +204,7 @@ auth_input_request_forwarding(struct ssh *ssh, struct passwd * pw)
207 204
208 /* Create private directory for socket */ 205 /* Create private directory for socket */
209 if (mkdtemp(auth_sock_dir) == NULL) { 206 if (mkdtemp(auth_sock_dir) == NULL) {
210 packet_send_debug("Agent forwarding disabled: " 207 ssh_packet_send_debug(ssh, "Agent forwarding disabled: "
211 "mkdtemp() failed: %.100s", strerror(errno)); 208 "mkdtemp() failed: %.100s", strerror(errno));
212 restore_uid(); 209 restore_uid();
213 free(auth_sock_dir); 210 free(auth_sock_dir);
@@ -524,7 +521,7 @@ do_exec_no_pty(struct ssh *ssh, Session *s, const char *command)
524 521
525 s->pid = pid; 522 s->pid = pid;
526 /* Set interactive/non-interactive mode. */ 523 /* Set interactive/non-interactive mode. */
527 packet_set_interactive(s->display != NULL, 524 ssh_packet_set_interactive(ssh, s->display != NULL,
528 options.ip_qos_interactive, options.ip_qos_bulk); 525 options.ip_qos_interactive, options.ip_qos_bulk);
529 526
530 /* 527 /*
@@ -653,7 +650,7 @@ do_exec_pty(struct ssh *ssh, Session *s, const char *command)
653 650
654 /* Enter interactive session. */ 651 /* Enter interactive session. */
655 s->ptymaster = ptymaster; 652 s->ptymaster = ptymaster;
656 packet_set_interactive(1, 653 ssh_packet_set_interactive(ssh, 1,
657 options.ip_qos_interactive, options.ip_qos_bulk); 654 options.ip_qos_interactive, options.ip_qos_bulk);
658 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1); 655 session_set_fds(ssh, s, ptyfd, fdout, -1, 1, 1);
659 return 0; 656 return 0;
@@ -786,8 +783,8 @@ do_login(struct ssh *ssh, Session *s, const char *command)
786 */ 783 */
787 memset(&from, 0, sizeof(from)); 784 memset(&from, 0, sizeof(from));
788 fromlen = sizeof(from); 785 fromlen = sizeof(from);
789 if (packet_connection_is_on_socket()) { 786 if (ssh_packet_connection_is_on_socket(ssh)) {
790 if (getpeername(packet_get_connection_in(), 787 if (getpeername(ssh_packet_get_connection_in(ssh),
791 (struct sockaddr *)&from, &fromlen) < 0) { 788 (struct sockaddr *)&from, &fromlen) < 0) {
792 debug("getpeername: %.100s", strerror(errno)); 789 debug("getpeername: %.100s", strerror(errno));
793 cleanup_exit(255); 790 cleanup_exit(255);
@@ -1198,7 +1195,7 @@ do_setup_env(struct ssh *ssh, Session *s, const char *shell)
1198 ssh_local_port(ssh)); 1195 ssh_local_port(ssh));
1199 child_set_env(&env, &envsize, "SSH_CLIENT", buf); 1196 child_set_env(&env, &envsize, "SSH_CLIENT", buf);
1200 1197
1201 laddr = get_local_ipaddr(packet_get_connection_in()); 1198 laddr = get_local_ipaddr(ssh_packet_get_connection_in(ssh));
1202 snprintf(buf, sizeof buf, "%.50s %d %.50s %d", 1199 snprintf(buf, sizeof buf, "%.50s %d %.50s %d",
1203 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 1200 ssh_remote_ipaddr(ssh), ssh_remote_port(ssh),
1204 laddr, ssh_local_port(ssh)); 1201 laddr, ssh_local_port(ssh));
@@ -1365,7 +1362,7 @@ safely_chroot(const char *path, uid_t uid)
1365 component, strerror(errno)); 1362 component, strerror(errno));
1366 if (st.st_uid != 0 || (st.st_mode & 022) != 0) 1363 if (st.st_uid != 0 || (st.st_mode & 022) != 0)
1367 fatal("bad ownership or modes for chroot " 1364 fatal("bad ownership or modes for chroot "
1368 "directory %s\"%s\"", 1365 "directory %s\"%s\"",
1369 cp == NULL ? "" : "component ", component); 1366 cp == NULL ? "" : "component ", component);
1370 if (!S_ISDIR(st.st_mode)) 1367 if (!S_ISDIR(st.st_mode))
1371 fatal("chroot path %s\"%s\" is not a directory", 1368 fatal("chroot path %s\"%s\" is not a directory",
@@ -1503,11 +1500,12 @@ child_close_fds(struct ssh *ssh)
1503 auth_sock = -1; 1500 auth_sock = -1;
1504 } 1501 }
1505 1502
1506 if (packet_get_connection_in() == packet_get_connection_out()) 1503 if (ssh_packet_get_connection_in(ssh) ==
1507 close(packet_get_connection_in()); 1504 ssh_packet_get_connection_out(ssh))
1505 close(ssh_packet_get_connection_in(ssh));
1508 else { 1506 else {
1509 close(packet_get_connection_in()); 1507 close(ssh_packet_get_connection_in(ssh));
1510 close(packet_get_connection_out()); 1508 close(ssh_packet_get_connection_out(ssh));
1511 } 1509 }
1512 /* 1510 /*
1513 * Close all descriptors related to channels. They will still remain 1511 * Close all descriptors related to channels. They will still remain
@@ -1549,7 +1547,7 @@ do_child(struct ssh *ssh, Session *s, const char *command)
1549 1547
1550 /* remove hostkey from the child's memory */ 1548 /* remove hostkey from the child's memory */
1551 destroy_sensitive_data(); 1549 destroy_sensitive_data();
1552 packet_clear_keys(); 1550 ssh_packet_clear_keys(ssh);
1553 1551
1554 /* Force a password change */ 1552 /* Force a password change */
1555 if (s->authctxt->force_pwchange) { 1553 if (s->authctxt->force_pwchange) {
@@ -1911,11 +1909,14 @@ session_by_pid(pid_t pid)
1911static int 1909static int
1912session_window_change_req(struct ssh *ssh, Session *s) 1910session_window_change_req(struct ssh *ssh, Session *s)
1913{ 1911{
1914 s->col = packet_get_int(); 1912 int r;
1915 s->row = packet_get_int(); 1913
1916 s->xpixel = packet_get_int(); 1914 if ((r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
1917 s->ypixel = packet_get_int(); 1915 (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
1918 packet_check_eom(); 1916 (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
1917 (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0 ||
1918 (r = sshpkt_get_end(ssh)) != 0)
1919 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
1919 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); 1920 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1920 return 1; 1921 return 1;
1921} 1922}
@@ -1923,22 +1924,23 @@ session_window_change_req(struct ssh *ssh, Session *s)
1923static int 1924static int
1924session_pty_req(struct ssh *ssh, Session *s) 1925session_pty_req(struct ssh *ssh, Session *s)
1925{ 1926{
1926 u_int len; 1927 int r;
1927 1928
1928 if (!auth_opts->permit_pty_flag || !options.permit_tty) { 1929 if (!auth_opts->permit_pty_flag || !options.permit_tty) {
1929 debug("Allocating a pty not permitted for this connection."); 1930 debug("Allocating a pty not permitted for this connection.");
1930 return 0; 1931 return 0;
1931 } 1932 }
1932 if (s->ttyfd != -1) { 1933 if (s->ttyfd != -1) {
1933 packet_disconnect("Protocol error: you already have a pty."); 1934 ssh_packet_disconnect(ssh, "Protocol error: you already have a pty.");
1934 return 0; 1935 return 0;
1935 } 1936 }
1936 1937
1937 s->term = packet_get_string(&len); 1938 if ((r = sshpkt_get_cstring(ssh, &s->term, NULL)) != 0 ||
1938 s->col = packet_get_int(); 1939 (r = sshpkt_get_u32(ssh, &s->col)) != 0 ||
1939 s->row = packet_get_int(); 1940 (r = sshpkt_get_u32(ssh, &s->row)) != 0 ||
1940 s->xpixel = packet_get_int(); 1941 (r = sshpkt_get_u32(ssh, &s->xpixel)) != 0 ||
1941 s->ypixel = packet_get_int(); 1942 (r = sshpkt_get_u32(ssh, &s->ypixel)) != 0)
1943 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
1942 1944
1943 if (strcmp(s->term, "") == 0) { 1945 if (strcmp(s->term, "") == 0) {
1944 free(s->term); 1946 free(s->term);
@@ -1960,13 +1962,15 @@ session_pty_req(struct ssh *ssh, Session *s)
1960 1962
1961 ssh_tty_parse_modes(ssh, s->ttyfd); 1963 ssh_tty_parse_modes(ssh, s->ttyfd);
1962 1964
1965 if ((r = sshpkt_get_end(ssh)) != 0)
1966 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
1967
1963 if (!use_privsep) 1968 if (!use_privsep)
1964 pty_setowner(s->pw, s->tty); 1969 pty_setowner(s->pw, s->tty);
1965 1970
1966 /* Set window size from the packet. */ 1971 /* Set window size from the packet. */
1967 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel); 1972 pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);
1968 1973
1969 packet_check_eom();
1970 session_proctitle(s); 1974 session_proctitle(s);
1971 return 1; 1975 return 1;
1972} 1976}
@@ -1975,13 +1979,13 @@ static int
1975session_subsystem_req(struct ssh *ssh, Session *s) 1979session_subsystem_req(struct ssh *ssh, Session *s)
1976{ 1980{
1977 struct stat st; 1981 struct stat st;
1978 u_int len; 1982 int r, success = 0;
1979 int success = 0;
1980 char *prog, *cmd; 1983 char *prog, *cmd;
1981 u_int i; 1984 u_int i;
1982 1985
1983 s->subsys = packet_get_string(&len); 1986 if ((r = sshpkt_get_cstring(ssh, &s->subsys, NULL)) != 0 ||
1984 packet_check_eom(); 1987 (r = sshpkt_get_end(ssh)) != 0)
1988 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
1985 debug2("subsystem request for %.100s by user %s", s->subsys, 1989 debug2("subsystem request for %.100s by user %s", s->subsys,
1986 s->pw->pw_name); 1990 s->pw->pw_name);
1987 1991
@@ -2014,18 +2018,22 @@ session_subsystem_req(struct ssh *ssh, Session *s)
2014static int 2018static int
2015session_x11_req(struct ssh *ssh, Session *s) 2019session_x11_req(struct ssh *ssh, Session *s)
2016{ 2020{
2017 int success; 2021 int r, success;
2022 u_char single_connection = 0;
2018 2023
2019 if (s->auth_proto != NULL || s->auth_data != NULL) { 2024 if (s->auth_proto != NULL || s->auth_data != NULL) {
2020 error("session_x11_req: session %d: " 2025 error("session_x11_req: session %d: "
2021 "x11 forwarding already active", s->self); 2026 "x11 forwarding already active", s->self);
2022 return 0; 2027 return 0;
2023 } 2028 }
2024 s->single_connection = packet_get_char(); 2029 if ((r = sshpkt_get_u8(ssh, &single_connection)) != 0 ||
2025 s->auth_proto = packet_get_string(NULL); 2030 (r = sshpkt_get_cstring(ssh, &s->auth_proto, NULL)) != 0 ||
2026 s->auth_data = packet_get_string(NULL); 2031 (r = sshpkt_get_cstring(ssh, &s->auth_data, NULL)) != 0 ||
2027 s->screen = packet_get_int(); 2032 (r = sshpkt_get_u32(ssh, &s->screen)) != 0 ||
2028 packet_check_eom(); 2033 (r = sshpkt_get_end(ssh)) != 0)
2034 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2035
2036 s->single_connection = single_connection;
2029 2037
2030 if (xauth_valid_string(s->auth_proto) && 2038 if (xauth_valid_string(s->auth_proto) &&
2031 xauth_valid_string(s->auth_data)) 2039 xauth_valid_string(s->auth_data))
@@ -2046,17 +2054,24 @@ session_x11_req(struct ssh *ssh, Session *s)
2046static int 2054static int
2047session_shell_req(struct ssh *ssh, Session *s) 2055session_shell_req(struct ssh *ssh, Session *s)
2048{ 2056{
2049 packet_check_eom(); 2057 int r;
2058
2059 if ((r = sshpkt_get_end(ssh)) != 0)
2060 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2050 return do_exec(ssh, s, NULL) == 0; 2061 return do_exec(ssh, s, NULL) == 0;
2051} 2062}
2052 2063
2053static int 2064static int
2054session_exec_req(struct ssh *ssh, Session *s) 2065session_exec_req(struct ssh *ssh, Session *s)
2055{ 2066{
2056 u_int len, success; 2067 u_int success;
2068 int r;
2069 char *command = NULL;
2070
2071 if ((r = sshpkt_get_cstring(ssh, &command, NULL)) != 0 ||
2072 (r = sshpkt_get_end(ssh)) != 0)
2073 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2057 2074
2058 char *command = packet_get_string(&len);
2059 packet_check_eom();
2060 success = do_exec(ssh, s, command) == 0; 2075 success = do_exec(ssh, s, command) == 0;
2061 free(command); 2076 free(command);
2062 return success; 2077 return success;
@@ -2065,9 +2080,11 @@ session_exec_req(struct ssh *ssh, Session *s)
2065static int 2080static int
2066session_break_req(struct ssh *ssh, Session *s) 2081session_break_req(struct ssh *ssh, Session *s)
2067{ 2082{
2083 int r;
2068 2084
2069 packet_get_int(); /* ignored */ 2085 if ((r = sshpkt_get_u32(ssh, NULL)) != 0 || /* ignore */
2070 packet_check_eom(); 2086 (r = sshpkt_get_end(ssh)) != 0)
2087 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2071 2088
2072 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0) 2089 if (s->ptymaster == -1 || tcsendbreak(s->ptymaster, 0) < 0)
2073 return 0; 2090 return 0;
@@ -2078,11 +2095,13 @@ static int
2078session_env_req(struct ssh *ssh, Session *s) 2095session_env_req(struct ssh *ssh, Session *s)
2079{ 2096{
2080 char *name, *val; 2097 char *name, *val;
2081 u_int name_len, val_len, i; 2098 u_int i;
2099 int r;
2082 2100
2083 name = packet_get_cstring(&name_len); 2101 if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
2084 val = packet_get_cstring(&val_len); 2102 (r = sshpkt_get_cstring(ssh, &val, NULL)) != 0 ||
2085 packet_check_eom(); 2103 (r = sshpkt_get_end(ssh)) != 0)
2104 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2086 2105
2087 /* Don't set too many environment variables */ 2106 /* Don't set too many environment variables */
2088 if (s->num_env > 128) { 2107 if (s->num_env > 128) {
@@ -2185,8 +2204,10 @@ static int
2185session_auth_agent_req(struct ssh *ssh, Session *s) 2204session_auth_agent_req(struct ssh *ssh, Session *s)
2186{ 2205{
2187 static int called = 0; 2206 static int called = 0;
2207 int r;
2188 2208
2189 packet_check_eom(); 2209 if ((r = sshpkt_get_end(ssh)) != 0)
2210 sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
2190 if (!auth_opts->permit_agent_forwarding_flag || 2211 if (!auth_opts->permit_agent_forwarding_flag ||
2191 !options.allow_agent_forwarding) { 2212 !options.allow_agent_forwarding) {
2192 debug("%s: agent forwarding disabled", __func__); 2213 debug("%s: agent forwarding disabled", __func__);
@@ -2376,6 +2397,7 @@ static void
2376session_exit_message(struct ssh *ssh, Session *s, int status) 2397session_exit_message(struct ssh *ssh, Session *s, int status)
2377{ 2398{
2378 Channel *c; 2399 Channel *c;
2400 int r;
2379 2401
2380 if ((c = channel_lookup(ssh, s->chanid)) == NULL) 2402 if ((c = channel_lookup(ssh, s->chanid)) == NULL)
2381 fatal("%s: session %d: no channel %d", 2403 fatal("%s: session %d: no channel %d",
@@ -2385,22 +2407,23 @@ session_exit_message(struct ssh *ssh, Session *s, int status)
2385 2407
2386 if (WIFEXITED(status)) { 2408 if (WIFEXITED(status)) {
2387 channel_request_start(ssh, s->chanid, "exit-status", 0); 2409 channel_request_start(ssh, s->chanid, "exit-status", 0);
2388 packet_put_int(WEXITSTATUS(status)); 2410 if ((r = sshpkt_put_u32(ssh, WEXITSTATUS(status))) != 0 ||
2389 packet_send(); 2411 (r = sshpkt_send(ssh)) != 0)
2412 sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
2390 } else if (WIFSIGNALED(status)) { 2413 } else if (WIFSIGNALED(status)) {
2391 channel_request_start(ssh, s->chanid, "exit-signal", 0); 2414 channel_request_start(ssh, s->chanid, "exit-signal", 0);
2392 packet_put_cstring(sig2name(WTERMSIG(status))); 2415#ifndef WCOREDUMP
2393#ifdef WCOREDUMP 2416# define WCOREDUMP(x) (0)
2394 packet_put_char(WCOREDUMP(status)? 1 : 0); 2417#endif
2395#else /* WCOREDUMP */ 2418 if ((r = sshpkt_put_cstring(ssh, sig2name(WTERMSIG(status)))) != 0 ||
2396 packet_put_char(0); 2419 (r = sshpkt_put_u8(ssh, WCOREDUMP(status)? 1 : 0)) != 0 ||
2397#endif /* WCOREDUMP */ 2420 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2398 packet_put_cstring(""); 2421 (r = sshpkt_put_cstring(ssh, "")) != 0 ||
2399 packet_put_cstring(""); 2422 (r = sshpkt_send(ssh)) != 0)
2400 packet_send(); 2423 sshpkt_fatal(ssh, r, "%s: exit reply", __func__);
2401 } else { 2424 } else {
2402 /* Some weird exit cause. Just exit. */ 2425 /* Some weird exit cause. Just exit. */
2403 packet_disconnect("wait returned status %04x.", status); 2426 ssh_packet_disconnect(ssh, "wait returned status %04x.", status);
2404 } 2427 }
2405 2428
2406 /* disconnect channel */ 2429 /* disconnect channel */
@@ -2571,7 +2594,7 @@ session_setup_x11fwd(struct ssh *ssh, Session *s)
2571 u_int i; 2594 u_int i;
2572 2595
2573 if (!auth_opts->permit_x11_forwarding_flag) { 2596 if (!auth_opts->permit_x11_forwarding_flag) {
2574 packet_send_debug("X11 forwarding disabled by key options."); 2597 ssh_packet_send_debug(ssh, "X11 forwarding disabled by key options.");
2575 return 0; 2598 return 0;
2576 } 2599 }
2577 if (!options.x11_forwarding) { 2600 if (!options.x11_forwarding) {
@@ -2580,7 +2603,7 @@ session_setup_x11fwd(struct ssh *ssh, Session *s)
2580 } 2603 }
2581 if (options.xauth_location == NULL || 2604 if (options.xauth_location == NULL ||
2582 (stat(options.xauth_location, &st) == -1)) { 2605 (stat(options.xauth_location, &st) == -1)) {
2583 packet_send_debug("No xauth program; cannot forward X11."); 2606 ssh_packet_send_debug(ssh, "No xauth program; cannot forward X11.");
2584 return 0; 2607 return 0;
2585 } 2608 }
2586 if (s->display != NULL) { 2609 if (s->display != NULL) {