FreeBSD/lib/libpam/modules/pam_ssh/pam_ssh.c
/*
 * Starts an ssh agent and stores the environment variables derived from
 * its output.
 */
Starts an ssh agent and stores the environment variables derived from
its output.
Starts an ssh agent and stores the environment variables derived from its output .
static int
pam_ssh_start_agent(pam_handle_t *pamh)
{
	int agent_pipe[2];
	pid_t pid;
	FILE *f;
	if (pipe(agent_pipe) == -1)
		return (PAM_SYSTEM_ERR);
	openpam_log(PAM_LOG_DEBUG, "starting an ssh agent");
	pid = fork();
	if (pid == (pid_t)-1) {
		close(agent_pipe[0]);
		close(agent_pipe[1]);
		return (PAM_SYSTEM_ERR);
	}
	if (pid == 0) {
		int fd;
		setgid(getegid());
		setuid(geteuid());
		close(STDIN_FILENO);
		open(_PATH_DEVNULL, O_RDONLY);
		dup2(agent_pipe[1], STDOUT_FILENO);
		dup2(agent_pipe[1], STDERR_FILENO);
		for (fd = 3; fd < getdtablesize(); ++fd)
			close(fd);
		execve(pam_ssh_agent, pam_ssh_agent_argv, pam_ssh_agent_envp);
		_exit(127);
	}
	close(agent_pipe[1]);
	if ((f = fdopen(agent_pipe[0], "r")) == NULL)
		return (PAM_SYSTEM_ERR);
	pam_ssh_process_agent_output(pamh, f);
	fclose(f);
	return (PAM_SUCCESS);
}
static int pam_ssh_start_agent ( pam_handle_t * pamh ) { int agent_pipe [ 2 ] ; pid_t pid ; FILE * f ; if ( pipe ( agent_pipe ) == -1 ) return ( PAM_SYSTEM_ERR ) ; openpam_log ( PAM_LOG_DEBUG , " " ) ; pid = fork ( ) ; if ( pid == ( pid_t ) - 1 ) { close ( agent_pipe [ 0 ] ) ; close ( agent_pipe [ 1 ] ) ; return ( PAM_SYSTEM_ERR ) ; } if ( pid == 0 ) { int fd ; setgid ( getegid ( ) ) ; setuid ( geteuid ( ) ) ; close ( STDIN_FILENO ) ; open ( _PATH_DEVNULL , O_RDONLY ) ; dup2 ( agent_pipe [ 1 ] , STDOUT_FILENO ) ; dup2 ( agent_pipe [ 1 ] , STDERR_FILENO ) ; for ( fd = 3 ; fd < getdtablesize ( ) ; ++ fd ) close ( fd ) ; execve ( pam_ssh_agent , pam_ssh_agent_argv , pam_ssh_agent_envp ) ; _exit ( 127 ) ; } close ( agent_pipe [ 1 ] ) ; if ( ( f = fdopen ( agent_pipe [ 0 ] , " " ) ) == NULL ) return ( PAM_SYSTEM_ERR ) ; pam_ssh_process_agent_output ( pamh , f ) ; fclose ( f ) ; return ( PAM_SUCCESS ) ; }
Starts an ssh agent and stores the environment variables derived from
 its output.
Starts an ssh agent and stores the environment variables derived from its output .
/* get a pipe which we will use to read the agent's output */ /* start the agent */ /* failed */ /* child: drop privs, close fds and start agent */ /* parent */