⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssh.c

📁 OpenSSL Source code for SFTP, SSH, and many others
💻 C
📖 第 1 页 / 共 3 页
字号:
		debug("Connections to local port %d forwarded to remote address %.200s:%d",		    options.local_forwards[i].port,		    options.local_forwards[i].host,		    options.local_forwards[i].host_port);		success += channel_setup_local_fwd_listener(		    options.local_forwards[i].port,		    options.local_forwards[i].host,		    options.local_forwards[i].host_port,		    options.gateway_ports);	}	if (i > 0 && success == 0)		error("Could not request local forwarding.");	/* Initiate remote TCP/IP port forwardings. */	for (i = 0; i < options.num_remote_forwards; i++) {		debug("Connections to remote port %d forwarded to local address %.200s:%d",		    options.remote_forwards[i].port,		    options.remote_forwards[i].host,		    options.remote_forwards[i].host_port);		channel_request_remote_forwarding(		    options.remote_forwards[i].port,		    options.remote_forwards[i].host,		    options.remote_forwards[i].host_port);	}}static voidcheck_agent_present(void){	if (options.forward_agent) {		/* Clear agent forwarding if we don\'t have an agent. */		int authfd = ssh_get_authentication_socket();		if (authfd < 0)			options.forward_agent = 0;		else			ssh_close_authentication_socket(authfd);	}}static intssh_session(void){	int type;	int interactive = 0;	int have_tty = 0;	struct winsize ws;	char *cp;	/* Enable compression if requested. */	if (options.compression) {		debug("Requesting compression at level %d.", options.compression_level);		if (options.compression_level < 1 || options.compression_level > 9)			fatal("Compression level must be from 1 (fast) to 9 (slow, best).");		/* Send the request. */		packet_start(SSH_CMSG_REQUEST_COMPRESSION);		packet_put_int(options.compression_level);		packet_send();		packet_write_wait();		type = packet_read();		if (type == SSH_SMSG_SUCCESS)			packet_start_compression(options.compression_level);		else if (type == SSH_SMSG_FAILURE)			log("Warning: Remote host refused compression.");		else			packet_disconnect("Protocol error waiting for compression response.");	}	/* Allocate a pseudo tty if appropriate. */	if (tty_flag) {		debug("Requesting pty.");		/* Start the packet. */		packet_start(SSH_CMSG_REQUEST_PTY);		/* Store TERM in the packet.  There is no limit on the		   length of the string. */		cp = getenv("TERM");		if (!cp)			cp = "";		packet_put_cstring(cp);		/* Store window size in the packet. */		if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)			memset(&ws, 0, sizeof(ws));		packet_put_int(ws.ws_row);		packet_put_int(ws.ws_col);		packet_put_int(ws.ws_xpixel);		packet_put_int(ws.ws_ypixel);		/* Store tty modes in the packet. */		tty_make_modes(fileno(stdin), NULL);		/* Send the packet, and wait for it to leave. */		packet_send();		packet_write_wait();		/* Read response from the server. */		type = packet_read();		if (type == SSH_SMSG_SUCCESS) {			interactive = 1;			have_tty = 1;		} else if (type == SSH_SMSG_FAILURE)			log("Warning: Remote host failed or refused to allocate a pseudo tty.");		else			packet_disconnect("Protocol error waiting for pty request response.");	}	/* Request X11 forwarding if enabled and DISPLAY is set. */	if (options.forward_x11 && getenv("DISPLAY") != NULL) {		char *proto, *data;		/* Get reasonable local authentication information. */		x11_get_proto(&proto, &data);		/* Request forwarding with authentication spoofing. */		debug("Requesting X11 forwarding with authentication spoofing.");		x11_request_forwarding_with_spoofing(0, proto, data);		/* Read response from the server. */		type = packet_read();		if (type == SSH_SMSG_SUCCESS) {			interactive = 1;		} else if (type == SSH_SMSG_FAILURE) {			log("Warning: Remote host denied X11 forwarding.");		} else {			packet_disconnect("Protocol error waiting for X11 forwarding");		}	}	/* Tell the packet module whether this is an interactive session. */	packet_set_interactive(interactive);	/* Request authentication agent forwarding if appropriate. */	check_agent_present();	if (options.forward_agent) {		debug("Requesting authentication agent forwarding.");		auth_request_forwarding();		/* Read response from the server. */		type = packet_read();		packet_check_eom();		if (type != SSH_SMSG_SUCCESS)			log("Warning: Remote host denied authentication agent forwarding.");	}	/* Initiate port forwardings. */	ssh_init_forwarding();	/* If requested, let ssh continue in the background. */	if (fork_after_authentication_flag)		if (daemon(1, 1) < 0)			fatal("daemon() failed: %.200s", strerror(errno));	/*	 * If a command was specified on the command line, execute the	 * command now. Otherwise request the server to start a shell.	 */	if (buffer_len(&command) > 0) {		int len = buffer_len(&command);		if (len > 900)			len = 900;		debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));		packet_start(SSH_CMSG_EXEC_CMD);		packet_put_string(buffer_ptr(&command), buffer_len(&command));		packet_send();		packet_write_wait();	} else {		debug("Requesting shell.");		packet_start(SSH_CMSG_EXEC_SHELL);		packet_send();		packet_write_wait();	}	/* Enter the interactive session. */	return client_loop(have_tty, tty_flag ?	    options.escape_char : SSH_ESCAPECHAR_NONE, 0);}static voidclient_subsystem_reply(int type, u_int32_t seq, void *ctxt){	int id, len;	id = packet_get_int();	len = buffer_len(&command);	if (len > 900)		len = 900;	packet_check_eom();	if (type == SSH2_MSG_CHANNEL_FAILURE)		fatal("Request for subsystem '%.*s' failed on channel %d",		    len, (u_char *)buffer_ptr(&command), id);}voidclient_global_request_reply(int type, u_int32_t seq, void *ctxt){	int i;	i = client_global_request_id++;	if (i >= options.num_remote_forwards) {		debug("client_global_request_reply: too many replies %d > %d",		    i, options.num_remote_forwards);		return;	}	debug("remote forward %s for: listen %d, connect %s:%d",	    type == SSH2_MSG_REQUEST_SUCCESS ? "success" : "failure",	    options.remote_forwards[i].port,	    options.remote_forwards[i].host,	    options.remote_forwards[i].host_port);	if (type == SSH2_MSG_REQUEST_FAILURE)		log("Warning: remote port forwarding failed for listen port %d",		    options.remote_forwards[i].port);}/* request pty/x11/agent/tcpfwd/shell for channel */static voidssh_session2_setup(int id, void *arg){	int len;	int interactive = 0;	struct termios tio;	debug("ssh_session2_setup: id %d", id);	if (tty_flag) {		struct winsize ws;		char *cp;		cp = getenv("TERM");		if (!cp)			cp = "";		/* Store window size in the packet. */		if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)			memset(&ws, 0, sizeof(ws));		channel_request_start(id, "pty-req", 0);		packet_put_cstring(cp);		packet_put_int(ws.ws_col);		packet_put_int(ws.ws_row);		packet_put_int(ws.ws_xpixel);		packet_put_int(ws.ws_ypixel);		tio = get_saved_tio();		tty_make_modes(/*ignored*/ 0, &tio);		packet_send();		interactive = 1;		/* XXX wait for reply */	}	if (options.forward_x11 &&	    getenv("DISPLAY") != NULL) {		char *proto, *data;		/* Get reasonable local authentication information. */		x11_get_proto(&proto, &data);		/* Request forwarding with authentication spoofing. */		debug("Requesting X11 forwarding with authentication spoofing.");		x11_request_forwarding_with_spoofing(id, proto, data);		interactive = 1;		/* XXX wait for reply */	}	check_agent_present();	if (options.forward_agent) {		debug("Requesting authentication agent forwarding.");		channel_request_start(id, "auth-agent-req@openssh.com", 0);		packet_send();	}	len = buffer_len(&command);	if (len > 0) {		if (len > 900)			len = 900;		if (subsystem_flag) {			debug("Sending subsystem: %.*s", len, (u_char *)buffer_ptr(&command));			channel_request_start(id, "subsystem", /*want reply*/ 1);			/* register callback for reply */			/* XXX we assume that client_loop has already been called */			dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &client_subsystem_reply);			dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &client_subsystem_reply);		} else {			debug("Sending command: %.*s", len, (u_char *)buffer_ptr(&command));			channel_request_start(id, "exec", 0);		}		packet_put_string(buffer_ptr(&command), buffer_len(&command));		packet_send();	} else {		channel_request_start(id, "shell", 0);		packet_send();	}	packet_set_interactive(interactive);}/* open new channel for a session */static intssh_session2_open(void){	Channel *c;	int window, packetmax, in, out, err;	if (stdin_null_flag) {		in = open(_PATH_DEVNULL, O_RDONLY);	} else {		in = dup(STDIN_FILENO);	}	out = dup(STDOUT_FILENO);	err = dup(STDERR_FILENO);	if (in < 0 || out < 0 || err < 0)		fatal("dup() in/out/err failed");	/* enable nonblocking unless tty */	if (!isatty(in))		set_nonblock(in);	if (!isatty(out))		set_nonblock(out);	if (!isatty(err))		set_nonblock(err);	window = CHAN_SES_WINDOW_DEFAULT;	packetmax = CHAN_SES_PACKET_DEFAULT;	if (tty_flag) {		window >>= 1;		packetmax >>= 1;	}	c = channel_new(	    "session", SSH_CHANNEL_OPENING, in, out, err,	    window, packetmax, CHAN_EXTENDED_WRITE,	    xstrdup("client-session"), /*nonblock*/0);	debug3("ssh_session2_open: channel_new: %d", c->self);	channel_send_open(c->self);	if (!no_shell_flag)		channel_register_confirm(c->self, ssh_session2_setup);	return c->self;}static intssh_session2(void){	int id = -1;	/* XXX should be pre-session */	ssh_init_forwarding();	if (!no_shell_flag || (datafellows & SSH_BUG_DUMMYCHAN))		id = ssh_session2_open();	/* If requested, let ssh continue in the background. */	if (fork_after_authentication_flag)		if (daemon(1, 1) < 0)			fatal("daemon() failed: %.200s", strerror(errno));	return client_loop(tty_flag, tty_flag ?	    options.escape_char : SSH_ESCAPECHAR_NONE, id);}static voidload_public_identity_files(void){	char *filename;	int i = 0;	Key *public;#ifdef SMARTCARD	Key **keys;	if (options.smartcard_device != NULL &&	    options.num_identity_files < SSH_MAX_IDENTITY_FILES &&	    (keys = sc_get_keys(options.smartcard_device, NULL)) != NULL ) {		int count = 0;		for (i = 0; keys[i] != NULL; i++) {			count++;			memmove(&options.identity_files[1], &options.identity_files[0],			    sizeof(char *) * (SSH_MAX_IDENTITY_FILES - 1));			memmove(&options.identity_keys[1], &options.identity_keys[0],			    sizeof(Key *) * (SSH_MAX_IDENTITY_FILES - 1));			options.num_identity_files++;			options.identity_keys[0] = keys[i];			options.identity_files[0] = xstrdup("smartcard key");;		}		if (options.num_identity_files > SSH_MAX_IDENTITY_FILES)			options.num_identity_files = SSH_MAX_IDENTITY_FILES;		i = count;		xfree(keys);	}#endif /* SMARTCARD */	for (; i < options.num_identity_files; i++) {		filename = tilde_expand_filename(options.identity_files[i],		    original_real_uid);		public = key_load_public(filename, NULL);		debug("identity file %s type %d", filename,		    public ? public->type : -1);		xfree(options.identity_files[i]);		options.identity_files[i] = filename;		options.identity_keys[i] = public;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -