1999-11-29 07:09:44 +00:00
|
|
|
/*-
|
2003-02-09 21:20:44 +00:00
|
|
|
* Copyright (c) 2003 Networks Associates Technology, Inc.
|
2001-12-05 16:06:35 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2003-02-09 21:20:44 +00:00
|
|
|
* This software was developed for the FreeBSD Project by ThinkSec AS and
|
|
|
|
* NAI Labs, the Security Research Division of Network Associates, Inc.
|
|
|
|
* under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
|
|
|
|
* DARPA CHATS research program.
|
1999-11-29 07:09:44 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-02-09 21:20:44 +00:00
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
1999-11-29 07:09:44 +00:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-09-30 22:11:06 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1999-11-29 07:09:44 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2001-07-29 18:31:09 +00:00
|
|
|
#include <sys/wait.h>
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2003-09-19 11:33:03 +00:00
|
|
|
#include <errno.h>
|
2002-04-04 18:45:21 +00:00
|
|
|
#include <fcntl.h>
|
2003-02-09 21:20:44 +00:00
|
|
|
#include <paths.h>
|
1999-11-29 07:09:44 +00:00
|
|
|
#include <pwd.h>
|
2001-07-29 18:31:09 +00:00
|
|
|
#include <signal.h>
|
1999-11-29 07:09:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2002-04-04 18:45:21 +00:00
|
|
|
#define PAM_SM_AUTH
|
2001-12-05 16:06:35 +00:00
|
|
|
#define PAM_SM_SESSION
|
|
|
|
|
2002-02-05 06:08:26 +00:00
|
|
|
#include <security/pam_appl.h>
|
1999-11-29 07:09:44 +00:00
|
|
|
#include <security/pam_modules.h>
|
2002-04-08 12:38:50 +00:00
|
|
|
#include <security/openpam.h>
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2001-07-29 18:31:09 +00:00
|
|
|
#include <openssl/evp.h>
|
2000-05-30 09:03:15 +00:00
|
|
|
|
|
|
|
#include "key.h"
|
1999-11-29 07:09:44 +00:00
|
|
|
#include "authfd.h"
|
2000-05-30 09:03:15 +00:00
|
|
|
#include "authfile.h"
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
extern char **environ;
|
2002-04-06 19:30:04 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
struct pam_ssh_key {
|
|
|
|
Key *key;
|
|
|
|
char *comment;
|
|
|
|
};
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
static const char *pam_ssh_prompt = "SSH passphrase: ";
|
|
|
|
static const char *pam_ssh_have_keys = "pam_ssh_have_keys";
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
static const char *pam_ssh_keyfiles[] = {
|
|
|
|
".ssh/identity", /* SSH1 RSA key */
|
|
|
|
".ssh/id_rsa", /* SSH2 RSA key */
|
|
|
|
".ssh/id_dsa", /* SSH2 DSA key */
|
|
|
|
NULL
|
|
|
|
};
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
static const char *pam_ssh_agent = "/usr/bin/ssh-agent";
|
2004-02-10 10:13:21 +00:00
|
|
|
static char *const pam_ssh_agent_argv[] = { "ssh_agent", "-s", NULL };
|
|
|
|
static char *const pam_ssh_agent_envp[] = { NULL };
|
1999-11-29 07:09:44 +00:00
|
|
|
|
1999-12-28 05:32:54 +00:00
|
|
|
/*
|
2003-02-09 21:20:44 +00:00
|
|
|
* Attempts to load a private key from the specified file in the specified
|
|
|
|
* directory, using the specified passphrase. If successful, returns a
|
|
|
|
* struct pam_ssh_key containing the key and its comment.
|
1999-12-28 05:32:54 +00:00
|
|
|
*/
|
2003-02-09 21:20:44 +00:00
|
|
|
static struct pam_ssh_key *
|
|
|
|
pam_ssh_load_key(const char *dir, const char *kfn, const char *passphrase)
|
1999-12-28 05:32:54 +00:00
|
|
|
{
|
2003-02-09 21:20:44 +00:00
|
|
|
struct pam_ssh_key *psk;
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
char *comment;
|
|
|
|
Key *key;
|
2002-04-04 18:45:21 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
if (snprintf(fn, sizeof(fn), "%s/%s", dir, kfn) > (int)sizeof(fn))
|
|
|
|
return (NULL);
|
2002-04-04 18:45:21 +00:00
|
|
|
comment = NULL;
|
2003-02-09 21:20:44 +00:00
|
|
|
key = key_load_private(fn, passphrase, &comment);
|
|
|
|
if (key == NULL) {
|
|
|
|
openpam_log(PAM_LOG_DEBUG, "failed to load key from %s\n", fn);
|
|
|
|
return (NULL);
|
2002-04-04 18:45:21 +00:00
|
|
|
}
|
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
openpam_log(PAM_LOG_DEBUG, "loaded '%s' from %s\n", comment, fn);
|
|
|
|
if ((psk = malloc(sizeof(*psk))) == NULL) {
|
2001-07-29 18:31:09 +00:00
|
|
|
key_free(key);
|
|
|
|
free(comment);
|
2003-02-09 21:20:44 +00:00
|
|
|
return (NULL);
|
2001-07-29 18:31:09 +00:00
|
|
|
}
|
2003-02-09 21:20:44 +00:00
|
|
|
psk->key = key;
|
|
|
|
psk->comment = comment;
|
|
|
|
return (psk);
|
1999-12-28 05:32:54 +00:00
|
|
|
}
|
|
|
|
|
2002-04-04 18:45:21 +00:00
|
|
|
/*
|
2003-02-09 21:20:44 +00:00
|
|
|
* Wipes a private key and frees the associated resources.
|
2002-04-04 18:45:21 +00:00
|
|
|
*/
|
2003-02-09 21:20:44 +00:00
|
|
|
static void
|
|
|
|
pam_ssh_free_key(pam_handle_t *pamh __unused,
|
|
|
|
void *data, int pam_err __unused)
|
2002-04-04 18:45:21 +00:00
|
|
|
{
|
2003-02-09 21:20:44 +00:00
|
|
|
struct pam_ssh_key *psk;
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
psk = data;
|
|
|
|
key_free(psk->key);
|
|
|
|
free(psk->comment);
|
|
|
|
free(psk);
|
1999-11-29 07:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PAM_EXTERN int
|
2002-04-06 19:30:04 +00:00
|
|
|
pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
|
2002-04-12 22:27:25 +00:00
|
|
|
int argc __unused, const char *argv[] __unused)
|
1999-11-29 07:09:44 +00:00
|
|
|
{
|
2003-02-09 21:20:44 +00:00
|
|
|
const char **kfn, *passphrase, *user;
|
2005-09-26 20:33:53 +00:00
|
|
|
const void *item;
|
2003-02-09 21:20:44 +00:00
|
|
|
struct passwd *pwd;
|
|
|
|
struct pam_ssh_key *psk;
|
2005-09-22 05:35:24 +00:00
|
|
|
int nkeys, nullok, pam_err, pass;
|
|
|
|
|
|
|
|
nullok = (openpam_get_option(pamh, "nullok") != NULL);
|
2003-02-09 21:20:44 +00:00
|
|
|
|
|
|
|
/* PEM is not loaded by default */
|
|
|
|
OpenSSL_add_all_algorithms();
|
|
|
|
|
|
|
|
/* get user name and home directory */
|
2003-02-10 18:59:20 +00:00
|
|
|
pam_err = pam_get_user(pamh, &user, NULL);
|
2003-02-09 21:20:44 +00:00
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
return (pam_err);
|
|
|
|
pwd = getpwnam(user);
|
|
|
|
if (pwd == NULL)
|
|
|
|
return (PAM_USER_UNKNOWN);
|
|
|
|
if (pwd->pw_dir == NULL)
|
2002-04-12 22:27:25 +00:00
|
|
|
return (PAM_AUTH_ERR);
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2005-09-22 05:35:24 +00:00
|
|
|
nkeys = 0;
|
2005-09-26 20:33:53 +00:00
|
|
|
pass = (pam_get_item(pamh, PAM_AUTHTOK, &item) == PAM_SUCCESS &&
|
|
|
|
item != NULL);
|
2003-02-09 21:20:44 +00:00
|
|
|
load_keys:
|
|
|
|
/* get passphrase */
|
|
|
|
pam_err = pam_get_authtok(pamh, PAM_AUTHTOK,
|
|
|
|
&passphrase, pam_ssh_prompt);
|
2005-09-21 16:08:40 +00:00
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
return (pam_err);
|
|
|
|
|
2005-09-22 05:35:24 +00:00
|
|
|
if (*passphrase == '\0' && !nullok)
|
|
|
|
goto skip_keys;
|
|
|
|
|
2005-09-21 16:08:40 +00:00
|
|
|
/* switch to user credentials */
|
|
|
|
pam_err = openpam_borrow_cred(pamh, pwd);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
2003-02-09 21:20:44 +00:00
|
|
|
return (pam_err);
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* try to load keys from all keyfiles we know of */
|
|
|
|
for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
|
|
|
|
psk = pam_ssh_load_key(pwd->pw_dir, *kfn, passphrase);
|
|
|
|
if (psk != NULL) {
|
|
|
|
pam_set_data(pamh, *kfn, psk, pam_ssh_free_key);
|
|
|
|
++nkeys;
|
|
|
|
}
|
|
|
|
}
|
2001-12-05 16:06:35 +00:00
|
|
|
|
2005-09-21 16:08:40 +00:00
|
|
|
/* switch back to arbitrator credentials */
|
|
|
|
openpam_restore_cred(pamh);
|
|
|
|
|
2005-09-22 05:35:24 +00:00
|
|
|
skip_keys:
|
2003-02-09 21:20:44 +00:00
|
|
|
/*
|
|
|
|
* If we tried an old token and didn't get anything, and
|
|
|
|
* try_first_pass was specified, try again after prompting the
|
|
|
|
* user for a new passphrase.
|
|
|
|
*/
|
|
|
|
if (nkeys == 0 && pass == 1 &&
|
|
|
|
openpam_get_option(pamh, "try_first_pass") != NULL) {
|
|
|
|
pam_set_item(pamh, PAM_AUTHTOK, NULL);
|
|
|
|
pass = 0;
|
|
|
|
goto load_keys;
|
2002-04-04 18:45:21 +00:00
|
|
|
}
|
2001-12-05 16:06:35 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* no keys? */
|
|
|
|
if (nkeys == 0)
|
|
|
|
return (PAM_AUTH_ERR);
|
2002-04-04 18:45:21 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
pam_set_data(pamh, pam_ssh_have_keys, NULL, NULL);
|
2002-04-12 22:27:25 +00:00
|
|
|
return (PAM_SUCCESS);
|
2001-12-05 16:06:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PAM_EXTERN int
|
2002-04-06 19:30:04 +00:00
|
|
|
pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
|
2002-04-12 22:27:25 +00:00
|
|
|
int argc __unused, const char *argv[] __unused)
|
2001-12-05 16:06:35 +00:00
|
|
|
{
|
2003-02-09 21:20:44 +00:00
|
|
|
|
2002-04-12 22:27:25 +00:00
|
|
|
return (PAM_SUCCESS);
|
2001-12-05 16:06:35 +00:00
|
|
|
}
|
1999-11-29 07:09:44 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/*
|
|
|
|
* Parses a line from ssh-agent's output.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
pam_ssh_process_agent_output(pam_handle_t *pamh, FILE *f)
|
1999-11-29 07:09:44 +00:00
|
|
|
{
|
2003-02-09 21:20:44 +00:00
|
|
|
char *line, *p, *key, *val;
|
|
|
|
size_t len;
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
while ((line = fgetln(f, &len)) != NULL) {
|
|
|
|
if (len < 4 || strncmp(line, "SSH_", 4) != 0)
|
|
|
|
continue;
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* find equal sign at end of key */
|
|
|
|
for (p = key = line; p < line + len; ++p)
|
|
|
|
if (*p == '=')
|
|
|
|
break;
|
|
|
|
if (p == line + len || *p != '=')
|
|
|
|
continue;
|
|
|
|
*p = '\0';
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* find semicolon at end of value */
|
|
|
|
for (val = ++p; p < line + len; ++p)
|
|
|
|
if (*p == ';')
|
|
|
|
break;
|
|
|
|
if (p == line + len || *p != ';')
|
|
|
|
continue;
|
|
|
|
*p = '\0';
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* store key-value pair in environment */
|
|
|
|
openpam_log(PAM_LOG_DEBUG, "got %s: %s", key, val);
|
|
|
|
pam_setenv(pamh, key, val, 1);
|
1999-11-29 07:09:44 +00:00
|
|
|
}
|
2003-02-09 21:20:44 +00:00
|
|
|
}
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2002-04-04 18:45:21 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* get a pipe which we will use to read the agent's output */
|
2005-09-21 16:08:40 +00:00
|
|
|
if (pipe(agent_pipe) == -1)
|
2003-02-09 21:20:44 +00:00
|
|
|
return (PAM_SYSTEM_ERR);
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* start the agent */
|
|
|
|
openpam_log(PAM_LOG_DEBUG, "starting an ssh agent");
|
|
|
|
pid = fork();
|
|
|
|
if (pid == (pid_t)-1) {
|
|
|
|
/* failed */
|
|
|
|
close(agent_pipe[0]);
|
|
|
|
close(agent_pipe[1]);
|
|
|
|
return (PAM_SYSTEM_ERR);
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* child: drop privs, close fds and start agent */
|
|
|
|
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);
|
|
|
|
}
|
2001-08-10 19:21:45 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* parent */
|
|
|
|
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);
|
2002-04-04 18:45:21 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
return (PAM_SUCCESS);
|
|
|
|
}
|
2001-08-10 19:21:45 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/*
|
|
|
|
* Adds previously stored keys to a running agent.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pam_ssh_add_keys_to_agent(pam_handle_t *pamh)
|
|
|
|
{
|
|
|
|
AuthenticationConnection *ac;
|
|
|
|
struct pam_ssh_key *psk;
|
|
|
|
const char **kfn;
|
2005-09-26 20:33:53 +00:00
|
|
|
void *item;
|
2003-02-09 21:20:44 +00:00
|
|
|
char **envlist, **env;
|
|
|
|
int pam_err;
|
|
|
|
|
|
|
|
/* switch to PAM environment */
|
|
|
|
envlist = environ;
|
|
|
|
if ((environ = pam_getenvlist(pamh)) == NULL) {
|
|
|
|
environ = envlist;
|
|
|
|
return (PAM_SYSTEM_ERR);
|
|
|
|
}
|
2002-04-04 18:45:21 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* get a connection to the agent */
|
|
|
|
if ((ac = ssh_get_authentication_connection()) == NULL) {
|
|
|
|
pam_err = PAM_SYSTEM_ERR;
|
|
|
|
goto end;
|
|
|
|
}
|
2002-04-04 18:45:21 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* look for keys to add to it */
|
|
|
|
for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
|
2005-09-26 20:33:53 +00:00
|
|
|
pam_err = pam_get_data(pamh, *kfn, &item);
|
|
|
|
if (pam_err == PAM_SUCCESS && item != NULL) {
|
|
|
|
psk = item;
|
2003-02-09 21:20:44 +00:00
|
|
|
if (ssh_add_identity(ac, psk->key, psk->comment))
|
|
|
|
openpam_log(PAM_LOG_DEBUG,
|
|
|
|
"added %s to ssh agent", psk->comment);
|
2002-04-04 18:45:21 +00:00
|
|
|
else
|
2003-02-09 21:20:44 +00:00
|
|
|
openpam_log(PAM_LOG_DEBUG, "failed "
|
|
|
|
"to add %s to ssh agent", psk->comment);
|
|
|
|
/* we won't need the key again, so wipe it */
|
|
|
|
pam_set_data(pamh, *kfn, NULL, NULL);
|
2001-07-29 18:31:09 +00:00
|
|
|
}
|
2002-04-04 18:45:21 +00:00
|
|
|
}
|
2003-02-09 21:20:44 +00:00
|
|
|
pam_err = PAM_SUCCESS;
|
|
|
|
end:
|
|
|
|
/* disconnect from agent */
|
|
|
|
if (ac != NULL)
|
|
|
|
ssh_close_authentication_connection(ac);
|
|
|
|
|
|
|
|
/* switch back to original environment */
|
|
|
|
for (env = environ; *env != NULL; ++env)
|
|
|
|
free(*env);
|
|
|
|
free(environ);
|
|
|
|
environ = envlist;
|
|
|
|
|
|
|
|
return (pam_err);
|
|
|
|
}
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
PAM_EXTERN int
|
|
|
|
pam_sm_open_session(pam_handle_t *pamh, int flags __unused,
|
|
|
|
int argc __unused, const char *argv[] __unused)
|
|
|
|
{
|
|
|
|
struct passwd *pwd;
|
|
|
|
const char *user;
|
2004-02-10 10:13:21 +00:00
|
|
|
void *data;
|
2003-02-09 21:20:44 +00:00
|
|
|
int pam_err;
|
|
|
|
|
|
|
|
/* no keys, no work */
|
|
|
|
if (pam_get_data(pamh, pam_ssh_have_keys, &data) != PAM_SUCCESS &&
|
|
|
|
openpam_get_option(pamh, "want_agent") == NULL)
|
2002-04-12 22:27:25 +00:00
|
|
|
return (PAM_SUCCESS);
|
2002-04-04 18:45:21 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* switch to user credentials */
|
2003-02-10 18:59:20 +00:00
|
|
|
pam_err = pam_get_user(pamh, &user, NULL);
|
2003-02-09 21:20:44 +00:00
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
return (pam_err);
|
|
|
|
pwd = getpwnam(user);
|
|
|
|
if (pwd == NULL)
|
|
|
|
return (PAM_USER_UNKNOWN);
|
|
|
|
pam_err = openpam_borrow_cred(pamh, pwd);
|
|
|
|
if (pam_err != PAM_SUCCESS)
|
|
|
|
return (pam_err);
|
|
|
|
|
|
|
|
/* start the agent */
|
|
|
|
pam_err = pam_ssh_start_agent(pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
openpam_restore_cred(pamh);
|
|
|
|
return (pam_err);
|
1999-12-28 05:32:54 +00:00
|
|
|
}
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
/* we have an agent, see if we can add any keys to it */
|
|
|
|
pam_err = pam_ssh_add_keys_to_agent(pamh);
|
|
|
|
if (pam_err != PAM_SUCCESS) {
|
|
|
|
/* XXX ignore failures */
|
1999-11-29 07:09:44 +00:00
|
|
|
}
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
openpam_restore_cred(pamh);
|
2002-04-12 22:27:25 +00:00
|
|
|
return (PAM_SUCCESS);
|
1999-11-29 07:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PAM_EXTERN int
|
2002-04-06 19:30:04 +00:00
|
|
|
pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
|
2002-04-12 22:27:25 +00:00
|
|
|
int argc __unused, const char *argv[] __unused)
|
1999-11-29 07:09:44 +00:00
|
|
|
{
|
2003-02-09 21:20:44 +00:00
|
|
|
const char *ssh_agent_pid;
|
|
|
|
char *end;
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
2001-07-29 18:31:09 +00:00
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
if ((ssh_agent_pid = pam_getenv(pamh, "SSH_AGENT_PID")) == NULL) {
|
|
|
|
openpam_log(PAM_LOG_DEBUG, "no ssh agent");
|
|
|
|
return (PAM_SUCCESS);
|
|
|
|
}
|
|
|
|
pid = (pid_t)strtol(ssh_agent_pid, &end, 10);
|
|
|
|
if (*ssh_agent_pid == '\0' || *end != '\0') {
|
|
|
|
openpam_log(PAM_LOG_DEBUG, "invalid ssh agent pid");
|
2002-04-12 22:27:25 +00:00
|
|
|
return (PAM_SESSION_ERR);
|
2001-07-29 18:31:09 +00:00
|
|
|
}
|
2003-02-09 21:20:44 +00:00
|
|
|
openpam_log(PAM_LOG_DEBUG, "killing ssh agent %d", (int)pid);
|
|
|
|
if (kill(pid, SIGTERM) == -1 ||
|
2003-09-19 11:33:03 +00:00
|
|
|
(waitpid(pid, &status, 0) == -1 && errno != ECHILD))
|
2003-02-09 21:20:44 +00:00
|
|
|
return (PAM_SYSTEM_ERR);
|
2002-04-12 22:27:25 +00:00
|
|
|
return (PAM_SUCCESS);
|
1999-11-29 07:09:44 +00:00
|
|
|
}
|
|
|
|
|
2003-02-09 21:20:44 +00:00
|
|
|
PAM_MODULE_ENTRY("pam_ssh");
|