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

📄 session.c

📁 OpenSSL Source code for SFTP, SSH, and many others
💻 C
📖 第 1 页 / 共 4 页
字号:
#ifdef _AIX	{		char *cp;		if ((cp = getenv("AUTHSTATE")) != NULL)			child_set_env(&env, &envsize, "AUTHSTATE", cp);		if ((cp = getenv("KRB5CCNAME")) != NULL)			child_set_env(&env, &envsize, "KRB5CCNAME", cp);		read_environment_file(&env, &envsize, "/etc/environment");	}#endif#ifdef KRB4	if (s->authctxt->krb4_ticket_file)		child_set_env(&env, &envsize, "KRBTKFILE",		    s->authctxt->krb4_ticket_file);#endif#ifdef KRB5	if (s->authctxt->krb5_ticket_file)		child_set_env(&env, &envsize, "KRB5CCNAME",		    s->authctxt->krb5_ticket_file);#endif#ifdef USE_PAM	/* Pull in any environment variables that may have been set by PAM. */	copy_environment(fetch_pam_environment(), &env, &envsize);#endif /* USE_PAM */	if (auth_sock_name != NULL)		child_set_env(&env, &envsize, SSH_AUTHSOCKET_ENV_NAME,		    auth_sock_name);	/* read $HOME/.ssh/environment. */	if (!options.use_login) {		snprintf(buf, sizeof buf, "%.200s/.ssh/environment",		    pw->pw_dir);		read_environment_file(&env, &envsize, buf);	}	if (debug_flag) {		/* dump the environment */		fprintf(stderr, "Environment:\n");		for (i = 0; env[i]; i++)			fprintf(stderr, "  %.200s\n", env[i]);	}	return env;}/* * Run $HOME/.ssh/rc, /etc/ssh/sshrc, or xauth (whichever is found * first in this order). */static voiddo_rc_files(Session *s, const char *shell){	FILE *f = NULL;	char cmd[1024];	int do_xauth;	struct stat st;	do_xauth =	    s->display != NULL && s->auth_proto != NULL && s->auth_data != NULL;	/* ignore _PATH_SSH_USER_RC for subsystems */	if (!s->is_subsystem && (stat(_PATH_SSH_USER_RC, &st) >= 0)) {		snprintf(cmd, sizeof cmd, "%s -c '%s %s'",		    shell, _PATH_BSHELL, _PATH_SSH_USER_RC);		if (debug_flag)			fprintf(stderr, "Running %s\n", cmd);		f = popen(cmd, "w");		if (f) {			if (do_xauth)				fprintf(f, "%s %s\n", s->auth_proto,				    s->auth_data);			pclose(f);		} else			fprintf(stderr, "Could not run %s\n",			    _PATH_SSH_USER_RC);	} else if (stat(_PATH_SSH_SYSTEM_RC, &st) >= 0) {		if (debug_flag)			fprintf(stderr, "Running %s %s\n", _PATH_BSHELL,			    _PATH_SSH_SYSTEM_RC);		f = popen(_PATH_BSHELL " " _PATH_SSH_SYSTEM_RC, "w");		if (f) {			if (do_xauth)				fprintf(f, "%s %s\n", s->auth_proto,				    s->auth_data);			pclose(f);		} else			fprintf(stderr, "Could not run %s\n",			    _PATH_SSH_SYSTEM_RC);	} else if (do_xauth && options.xauth_location != NULL) {		/* Add authority data to .Xauthority if appropriate. */		if (debug_flag) {			fprintf(stderr,			    "Running %.500s add "			    "%.100s %.100s %.100s\n",			    options.xauth_location, s->auth_display,			    s->auth_proto, s->auth_data);		}		snprintf(cmd, sizeof cmd, "%s -q -",		    options.xauth_location);		f = popen(cmd, "w");		if (f) {			fprintf(f, "add %s %s %s\n",			    s->auth_display, s->auth_proto,			    s->auth_data);			pclose(f);		} else {			fprintf(stderr, "Could not run %s\n",			    cmd);		}	}}static voiddo_nologin(struct passwd *pw){	FILE *f = NULL;	char buf[1024];#ifdef HAVE_LOGIN_CAP	if (!login_getcapbool(lc, "ignorenologin", 0) && pw->pw_uid)		f = fopen(login_getcapstr(lc, "nologin", _PATH_NOLOGIN,		    _PATH_NOLOGIN), "r");#else	if (pw->pw_uid)		f = fopen(_PATH_NOLOGIN, "r");#endif	if (f) {		/* /etc/nologin exists.  Print its contents and exit. */		while (fgets(buf, sizeof(buf), f))			fputs(buf, stderr);		fclose(f);		exit(254);	}}/* Set login name, uid, gid, and groups. */voiddo_setusercontext(struct passwd *pw){#ifdef HAVE_CYGWIN	if (is_winnt) {#else /* HAVE_CYGWIN */	if (getuid() == 0 || geteuid() == 0) {#endif /* HAVE_CYGWIN */#ifdef HAVE_SETPCRED		setpcred(pw->pw_name);#endif /* HAVE_SETPCRED */#ifdef HAVE_LOGIN_CAP		if (setusercontext(lc, pw, pw->pw_uid,		    (LOGIN_SETALL & ~LOGIN_SETPATH)) < 0) {			perror("unable to set user context");			exit(1);		}#else# if defined(HAVE_GETLUID) && defined(HAVE_SETLUID)		/* Sets login uid for accounting */		if (getluid() == -1 && setluid(pw->pw_uid) == -1)			error("setluid: %s", strerror(errno));# endif /* defined(HAVE_GETLUID) && defined(HAVE_SETLUID) */		if (setlogin(pw->pw_name) < 0)			error("setlogin failed: %s", strerror(errno));		if (setgid(pw->pw_gid) < 0) {			perror("setgid");			exit(1);		}		/* Initialize the group list. */		if (initgroups(pw->pw_name, pw->pw_gid) < 0) {			perror("initgroups");			exit(1);		}		endgrent();# ifdef USE_PAM		/*		 * PAM credentials may take the form of supplementary groups. 		 * These will have been wiped by the above initgroups() call.		 * Reestablish them here.		 */		do_pam_setcred(0);# endif /* USE_PAM */# if defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY)		irix_setusercontext(pw);#  endif /* defined(WITH_IRIX_PROJECT) || defined(WITH_IRIX_JOBS) || defined(WITH_IRIX_ARRAY) */		/* Permanently switch to the desired uid. */		permanently_set_uid(pw);#endif	}	if (getuid() != pw->pw_uid || geteuid() != pw->pw_uid)		fatal("Failed to set uids to %u.", (u_int) pw->pw_uid);}static voidlaunch_login(struct passwd *pw, const char *hostname){	/* Launch login(1). */	execl(LOGIN_PROGRAM, "login", "-h", hostname,#ifdef xxxLOGIN_NEEDS_TERM		    (s->term ? s->term : "unknown"),#endif /* LOGIN_NEEDS_TERM */#ifdef LOGIN_NO_ENDOPT	    "-p", "-f", pw->pw_name, (char *)NULL);#else	    "-p", "-f", "--", pw->pw_name, (char *)NULL);#endif	/* Login couldn't be executed, die. */	perror("login");	exit(1);}/* * Performs common processing for the child, such as setting up the * environment, closing extra file descriptors, setting the user and group * ids, and executing the command or shell. */voiddo_child(Session *s, const char *command){	extern char **environ;	char **env;	char *argv[10];	const char *shell, *shell0, *hostname = NULL;	struct passwd *pw = s->pw;	u_int i;	/* remove hostkey from the child's memory */	destroy_sensitive_data();	/* login(1) is only called if we execute the login shell */	if (options.use_login && command != NULL)		options.use_login = 0;	/*	 * Login(1) does this as well, and it needs uid 0 for the "-h"	 * switch, so we let login(1) to this for us.	 */	if (!options.use_login) {#ifdef HAVE_OSF_SIA		session_setup_sia(pw->pw_name, s->ttyfd == -1 ? NULL : s->tty);		if (!check_quietlogin(s, command))			do_motd();#else /* HAVE_OSF_SIA */		do_nologin(pw);# ifdef _AIX		aix_usrinfo(pw, s->tty, s->ttyfd);# endif /* _AIX */		do_setusercontext(pw);#endif /* HAVE_OSF_SIA */	}	/*	 * Get the shell from the password data.  An empty shell field is	 * legal, and means /bin/sh.	 */	shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;#ifdef HAVE_LOGIN_CAP	shell = login_getcapstr(lc, "shell", (char *)shell, (char *)shell);#endif	env = do_setup_env(s, shell);	/* we have to stash the hostname before we close our socket. */	if (options.use_login)		hostname = get_remote_name_or_ip(utmp_len,		    options.verify_reverse_mapping);	/*	 * Close the connection descriptors; note that this is the child, and	 * the server will still have the socket open, and it is important	 * that we do not shutdown it.  Note that the descriptors cannot be	 * closed before building the environment, as we call	 * get_remote_ipaddr there.	 */	if (packet_get_connection_in() == packet_get_connection_out())		close(packet_get_connection_in());	else {		close(packet_get_connection_in());		close(packet_get_connection_out());	}	/*	 * Close all descriptors related to channels.  They will still remain	 * open in the parent.	 */	/* XXX better use close-on-exec? -markus */	channel_close_all();	/*	 * Close any extra file descriptors.  Note that there may still be	 * descriptors left by system functions.  They will be closed later.	 */	endpwent();	/*	 * Close any extra open file descriptors so that we don\'t have them	 * hanging around in clients.  Note that we want to do this after	 * initgroups, because at least on Solaris 2.3 it leaves file	 * descriptors open.	 */	for (i = 3; i < 64; i++)		close(i);	/*	 * Must take new environment into use so that .ssh/rc,	 * /etc/ssh/sshrc and xauth are run in the proper environment.	 */	environ = env;#ifdef AFS	/* Try to get AFS tokens for the local cell. */	if (k_hasafs()) {		char cell[64];		if (k_afs_cell_of_file(pw->pw_dir, cell, sizeof(cell)) == 0)			krb_afslog(cell, 0);		krb_afslog(0, 0);	}#endif /* AFS */	/* Change current directory to the user\'s home directory. */	if (chdir(pw->pw_dir) < 0) {		fprintf(stderr, "Could not chdir to home directory %s: %s\n",		    pw->pw_dir, strerror(errno));#ifdef HAVE_LOGIN_CAP		if (login_getcapbool(lc, "requirehome", 0))			exit(1);#endif	}	if (!options.use_login)		do_rc_files(s, shell);	/* restore SIGPIPE for child */	signal(SIGPIPE,  SIG_DFL);	if (options.use_login) {		launch_login(pw, hostname);		/* NEVERREACHED */	}	/* Get the last component of the shell name. */	if ((shell0 = strrchr(shell, '/')) != NULL)		shell0++;	else		shell0 = shell;	/*	 * If we have no command, execute the shell.  In this case, the shell	 * name to be passed in argv[0] is preceded by '-' to indicate that	 * this is a login shell.	 */	if (!command) {		char argv0[256];		/* Start the shell.  Set initial character to '-'. */		argv0[0] = '-';		if (strlcpy(argv0 + 1, shell0, sizeof(argv0) - 1)		    >= sizeof(argv0) - 1) {			errno = EINVAL;			perror(shell);			exit(1);		}		/* Execute the shell. */		argv[0] = argv0;		argv[1] = NULL;		execve(shell, argv, env);		/* Executing the shell failed. */		perror(shell);		exit(1);	}	/*	 * Execute the command using the user's shell.  This uses the -c	 * option to execute the command.	 */	argv[0] = (char *) shell0;	argv[1] = "-c";	argv[2] = (char *) command;	argv[3] = NULL;	execve(shell, argv, env);	perror(shell);	exit(1);}Session *session_new(void){	int i;	static int did_init = 0;	if (!did_init) {		debug("session_new: init");		for (i = 0; i < MAX_SESSIONS; i++) {			sessions[i].used = 0;		}		did_init = 1;	}	for (i = 0; i < MAX_SESSIONS; i++) {		Session *s = &sessions[i];		if (! s->used) {			memset(s, 0, sizeof(*s));			s->chanid = -1;			s->ptyfd = -1;			s->ttyfd = -1;			s->used = 1;			s->self = i;			debug("session_new: session %d", i);			return s;		}	}	return NULL;}static voidsession_dump(void){	int i;	for (i = 0; i < MAX_SESSIONS; i++) {		Session *s = &sessions[i];		debug("dump: used %d session %d %p channel %d pid %ld",		    s->used,		    s->self,		    s,		    s->chanid,		    (long)s->pid);	}}intsession_open(Authctxt *authctxt, int chanid){	Session *s = session_new();	debug("session_open: channel %d", chanid);	if (s == NULL) {		error("no more sessions");		return 0;	}	s->authctxt = authctxt;	s->pw = authctxt->pw;	if (s->pw == NULL)		fatal("no user for session %d", s->self);	debug("session_open: session %d: link with channel %d", s->self, chanid);	s->chanid = chanid;	return 1;}Session *session_by_tty(char *tty){	int i;	for (i = 0; i < MAX_SESSIONS; i++) {		Session *s = &sessions[i];		if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {			debug("session_by_tty: session %d tty %s", i, tty);			return s;		}	}	debug("session_by_tty: unknown tty %.100s", tty);	session_dump();	return NULL;}static Session *session_by_channel(int id){	int i;	for (i = 0; i < MAX_SESSIONS; i++) {		Session *s = &sessions[i];		if (s->used && s->chanid == id) {			debug("session_by_channel: session %d channel %d", i, id);			return s;		}	}	debug("session_by_channel: unknown channel %d", id);	session_dump();	return NULL;}static Session *session_by_pid(pid_t pid){	int i;	debug("session_by_pid: pid %ld", (long)pid);	for (i = 0; i < MAX_SESSIONS; i++) {		Session *s = &sessions[i];		if (s->used && s->pid == pid)			return s;	}	error("session_by_pid: unknown pid %ld", (long)pid);	session_dump();	return NULL;}static intsession_window_change_req(Session *s){	s->col = packet_get_int();	s->row = packet_get_int();	s->xpixel = packet_get_int();	s->ypixel = packet_get_int();	packet_check_eom();	pty_change_window_size(s->ptyfd, s->row, s->col, s->xpixel, s->ypixel);	return 1;}

⌨️ 快捷键说明

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