Home | History | Annotate | Line # | Download | only in pam_ssh
pam_ssh.c revision 1.1.1.1
      1  1.1  christos /*-
      2  1.1  christos  * Copyright (c) 2003 Networks Associates Technology, Inc.
      3  1.1  christos  * All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * This software was developed for the FreeBSD Project by ThinkSec AS and
      6  1.1  christos  * NAI Labs, the Security Research Division of Network Associates, Inc.
      7  1.1  christos  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
      8  1.1  christos  * DARPA CHATS research program.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  * 3. The name of the author may not be used to endorse or promote
     19  1.1  christos  *    products derived from this software without specific prior written
     20  1.1  christos  *    permission.
     21  1.1  christos  *
     22  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     23  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     26  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  christos  * SUCH DAMAGE.
     33  1.1  christos  */
     34  1.1  christos 
     35  1.1  christos #include <sys/cdefs.h>
     36  1.1  christos __FBSDID("$FreeBSD: src/lib/libpam/modules/pam_ssh/pam_ssh.c,v 1.40 2004/02/10 10:13:21 des Exp $");
     37  1.1  christos 
     38  1.1  christos #include <sys/param.h>
     39  1.1  christos #include <sys/wait.h>
     40  1.1  christos 
     41  1.1  christos #include <errno.h>
     42  1.1  christos #include <fcntl.h>
     43  1.1  christos #include <paths.h>
     44  1.1  christos #include <pwd.h>
     45  1.1  christos #include <signal.h>
     46  1.1  christos #include <stdio.h>
     47  1.1  christos #include <string.h>
     48  1.1  christos #include <unistd.h>
     49  1.1  christos 
     50  1.1  christos #define PAM_SM_AUTH
     51  1.1  christos #define PAM_SM_SESSION
     52  1.1  christos 
     53  1.1  christos #include <security/pam_appl.h>
     54  1.1  christos #include <security/pam_modules.h>
     55  1.1  christos #include <security/openpam.h>
     56  1.1  christos 
     57  1.1  christos #include <openssl/evp.h>
     58  1.1  christos 
     59  1.1  christos #include "key.h"
     60  1.1  christos #include "authfd.h"
     61  1.1  christos #include "authfile.h"
     62  1.1  christos 
     63  1.1  christos extern char **environ;
     64  1.1  christos 
     65  1.1  christos struct pam_ssh_key {
     66  1.1  christos 	Key	*key;
     67  1.1  christos 	char	*comment;
     68  1.1  christos };
     69  1.1  christos 
     70  1.1  christos static const char *pam_ssh_prompt = "SSH passphrase: ";
     71  1.1  christos static const char *pam_ssh_have_keys = "pam_ssh_have_keys";
     72  1.1  christos 
     73  1.1  christos static const char *pam_ssh_keyfiles[] = {
     74  1.1  christos 	".ssh/identity",	/* SSH1 RSA key */
     75  1.1  christos 	".ssh/id_rsa",		/* SSH2 RSA key */
     76  1.1  christos 	".ssh/id_dsa",		/* SSH2 DSA key */
     77  1.1  christos 	NULL
     78  1.1  christos };
     79  1.1  christos 
     80  1.1  christos static const char *pam_ssh_agent = "/usr/bin/ssh-agent";
     81  1.1  christos static char *const pam_ssh_agent_argv[] = { "ssh_agent", "-s", NULL };
     82  1.1  christos static char *const pam_ssh_agent_envp[] = { NULL };
     83  1.1  christos 
     84  1.1  christos /*
     85  1.1  christos  * Attempts to load a private key from the specified file in the specified
     86  1.1  christos  * directory, using the specified passphrase.  If successful, returns a
     87  1.1  christos  * struct pam_ssh_key containing the key and its comment.
     88  1.1  christos  */
     89  1.1  christos static struct pam_ssh_key *
     90  1.1  christos pam_ssh_load_key(const char *dir, const char *kfn, const char *passphrase)
     91  1.1  christos {
     92  1.1  christos 	struct pam_ssh_key *psk;
     93  1.1  christos 	char fn[PATH_MAX];
     94  1.1  christos 	char *comment;
     95  1.1  christos 	Key *key;
     96  1.1  christos 
     97  1.1  christos 	if (snprintf(fn, sizeof(fn), "%s/%s", dir, kfn) > (int)sizeof(fn))
     98  1.1  christos 		return (NULL);
     99  1.1  christos 	comment = NULL;
    100  1.1  christos 	key = key_load_private(fn, passphrase, &comment);
    101  1.1  christos 	if (key == NULL) {
    102  1.1  christos 		openpam_log(PAM_LOG_DEBUG, "failed to load key from %s\n", fn);
    103  1.1  christos 		return (NULL);
    104  1.1  christos 	}
    105  1.1  christos 
    106  1.1  christos 	openpam_log(PAM_LOG_DEBUG, "loaded '%s' from %s\n", comment, fn);
    107  1.1  christos 	if ((psk = malloc(sizeof(*psk))) == NULL) {
    108  1.1  christos 		key_free(key);
    109  1.1  christos 		free(comment);
    110  1.1  christos 		return (NULL);
    111  1.1  christos 	}
    112  1.1  christos 	psk->key = key;
    113  1.1  christos 	psk->comment = comment;
    114  1.1  christos 	return (psk);
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos /*
    118  1.1  christos  * Wipes a private key and frees the associated resources.
    119  1.1  christos  */
    120  1.1  christos static void
    121  1.1  christos pam_ssh_free_key(pam_handle_t *pamh __unused,
    122  1.1  christos     void *data, int pam_err __unused)
    123  1.1  christos {
    124  1.1  christos 	struct pam_ssh_key *psk;
    125  1.1  christos 
    126  1.1  christos 	psk = data;
    127  1.1  christos 	key_free(psk->key);
    128  1.1  christos 	free(psk->comment);
    129  1.1  christos 	free(psk);
    130  1.1  christos }
    131  1.1  christos 
    132  1.1  christos PAM_EXTERN int
    133  1.1  christos pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
    134  1.1  christos     int argc __unused, const char *argv[] __unused)
    135  1.1  christos {
    136  1.1  christos 	const char **kfn, *passphrase, *user;
    137  1.1  christos 	struct passwd *pwd;
    138  1.1  christos 	struct pam_ssh_key *psk;
    139  1.1  christos 	int nkeys, pam_err, pass;
    140  1.1  christos 
    141  1.1  christos 	/* PEM is not loaded by default */
    142  1.1  christos 	OpenSSL_add_all_algorithms();
    143  1.1  christos 
    144  1.1  christos 	/* get user name and home directory */
    145  1.1  christos 	pam_err = pam_get_user(pamh, &user, NULL);
    146  1.1  christos 	if (pam_err != PAM_SUCCESS)
    147  1.1  christos 		return (pam_err);
    148  1.1  christos 	pwd = getpwnam(user);
    149  1.1  christos 	if (pwd == NULL)
    150  1.1  christos 		return (PAM_USER_UNKNOWN);
    151  1.1  christos 	if (pwd->pw_dir == NULL)
    152  1.1  christos 		return (PAM_AUTH_ERR);
    153  1.1  christos 
    154  1.1  christos 	/* switch to user credentials */
    155  1.1  christos 	pam_err = openpam_borrow_cred(pamh, pwd);
    156  1.1  christos 	if (pam_err != PAM_SUCCESS)
    157  1.1  christos 		return (pam_err);
    158  1.1  christos 
    159  1.1  christos 	pass = (pam_get_item(pamh, PAM_AUTHTOK,
    160  1.1  christos 	    (const void **)&passphrase) == PAM_SUCCESS);
    161  1.1  christos  load_keys:
    162  1.1  christos 	/* get passphrase */
    163  1.1  christos 	pam_err = pam_get_authtok(pamh, PAM_AUTHTOK,
    164  1.1  christos 	    &passphrase, pam_ssh_prompt);
    165  1.1  christos 	if (pam_err != PAM_SUCCESS) {
    166  1.1  christos 		openpam_restore_cred(pamh);
    167  1.1  christos 		return (pam_err);
    168  1.1  christos 	}
    169  1.1  christos 
    170  1.1  christos 	/* try to load keys from all keyfiles we know of */
    171  1.1  christos 	nkeys = 0;
    172  1.1  christos 	for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
    173  1.1  christos 		psk = pam_ssh_load_key(pwd->pw_dir, *kfn, passphrase);
    174  1.1  christos 		if (psk != NULL) {
    175  1.1  christos 			pam_set_data(pamh, *kfn, psk, pam_ssh_free_key);
    176  1.1  christos 			++nkeys;
    177  1.1  christos 		}
    178  1.1  christos 	}
    179  1.1  christos 
    180  1.1  christos 	/*
    181  1.1  christos 	 * If we tried an old token and didn't get anything, and
    182  1.1  christos 	 * try_first_pass was specified, try again after prompting the
    183  1.1  christos 	 * user for a new passphrase.
    184  1.1  christos 	 */
    185  1.1  christos 	if (nkeys == 0 && pass == 1 &&
    186  1.1  christos 	    openpam_get_option(pamh, "try_first_pass") != NULL) {
    187  1.1  christos 		pam_set_item(pamh, PAM_AUTHTOK, NULL);
    188  1.1  christos 		pass = 0;
    189  1.1  christos 		goto load_keys;
    190  1.1  christos 	}
    191  1.1  christos 
    192  1.1  christos 	/* switch back to arbitrator credentials before returning */
    193  1.1  christos 	openpam_restore_cred(pamh);
    194  1.1  christos 
    195  1.1  christos 	/* no keys? */
    196  1.1  christos 	if (nkeys == 0)
    197  1.1  christos 		return (PAM_AUTH_ERR);
    198  1.1  christos 
    199  1.1  christos 	pam_set_data(pamh, pam_ssh_have_keys, NULL, NULL);
    200  1.1  christos 	return (PAM_SUCCESS);
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos PAM_EXTERN int
    204  1.1  christos pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
    205  1.1  christos     int argc __unused, const char *argv[] __unused)
    206  1.1  christos {
    207  1.1  christos 
    208  1.1  christos 	return (PAM_SUCCESS);
    209  1.1  christos }
    210  1.1  christos 
    211  1.1  christos /*
    212  1.1  christos  * Parses a line from ssh-agent's output.
    213  1.1  christos  */
    214  1.1  christos static void
    215  1.1  christos pam_ssh_process_agent_output(pam_handle_t *pamh, FILE *f)
    216  1.1  christos {
    217  1.1  christos 	char *line, *p, *key, *val;
    218  1.1  christos 	size_t len;
    219  1.1  christos 
    220  1.1  christos 	while ((line = fgetln(f, &len)) != NULL) {
    221  1.1  christos 		if (len < 4 || strncmp(line, "SSH_", 4) != 0)
    222  1.1  christos 			continue;
    223  1.1  christos 
    224  1.1  christos 		/* find equal sign at end of key */
    225  1.1  christos 		for (p = key = line; p < line + len; ++p)
    226  1.1  christos 			if (*p == '=')
    227  1.1  christos 				break;
    228  1.1  christos 		if (p == line + len || *p != '=')
    229  1.1  christos 			continue;
    230  1.1  christos 		*p = '\0';
    231  1.1  christos 
    232  1.1  christos 		/* find semicolon at end of value */
    233  1.1  christos 		for (val = ++p; p < line + len; ++p)
    234  1.1  christos 			if (*p == ';')
    235  1.1  christos 				break;
    236  1.1  christos 		if (p == line + len || *p != ';')
    237  1.1  christos 			continue;
    238  1.1  christos 		*p = '\0';
    239  1.1  christos 
    240  1.1  christos 		/* store key-value pair in environment */
    241  1.1  christos 		openpam_log(PAM_LOG_DEBUG, "got %s: %s", key, val);
    242  1.1  christos 		pam_setenv(pamh, key, val, 1);
    243  1.1  christos 	}
    244  1.1  christos }
    245  1.1  christos 
    246  1.1  christos /*
    247  1.1  christos  * Starts an ssh agent and stores the environment variables derived from
    248  1.1  christos  * its output.
    249  1.1  christos  */
    250  1.1  christos static int
    251  1.1  christos pam_ssh_start_agent(pam_handle_t *pamh)
    252  1.1  christos {
    253  1.1  christos 	int agent_pipe[2];
    254  1.1  christos 	pid_t pid;
    255  1.1  christos 	FILE *f;
    256  1.1  christos 
    257  1.1  christos 	/* get a pipe which we will use to read the agent's output */
    258  1.1  christos 	if (pipe(agent_pipe) == -1) {
    259  1.1  christos 		openpam_restore_cred(pamh);
    260  1.1  christos 		return (PAM_SYSTEM_ERR);
    261  1.1  christos 	}
    262  1.1  christos 
    263  1.1  christos 	/* start the agent */
    264  1.1  christos 	openpam_log(PAM_LOG_DEBUG, "starting an ssh agent");
    265  1.1  christos 	pid = fork();
    266  1.1  christos 	if (pid == (pid_t)-1) {
    267  1.1  christos 		/* failed */
    268  1.1  christos 		close(agent_pipe[0]);
    269  1.1  christos 		close(agent_pipe[1]);
    270  1.1  christos 		return (PAM_SYSTEM_ERR);
    271  1.1  christos 	}
    272  1.1  christos 	if (pid == 0) {
    273  1.1  christos 		int fd;
    274  1.1  christos 
    275  1.1  christos 		/* child: drop privs, close fds and start agent */
    276  1.1  christos 		setgid(getegid());
    277  1.1  christos 		setuid(geteuid());
    278  1.1  christos 		close(STDIN_FILENO);
    279  1.1  christos 		open(_PATH_DEVNULL, O_RDONLY);
    280  1.1  christos 		dup2(agent_pipe[1], STDOUT_FILENO);
    281  1.1  christos 		dup2(agent_pipe[1], STDERR_FILENO);
    282  1.1  christos 		for (fd = 3; fd < getdtablesize(); ++fd)
    283  1.1  christos 			close(fd);
    284  1.1  christos 		execve(pam_ssh_agent, pam_ssh_agent_argv, pam_ssh_agent_envp);
    285  1.1  christos 		_exit(127);
    286  1.1  christos 	}
    287  1.1  christos 
    288  1.1  christos 	/* parent */
    289  1.1  christos 	close(agent_pipe[1]);
    290  1.1  christos 	if ((f = fdopen(agent_pipe[0], "r")) == NULL)
    291  1.1  christos 		return (PAM_SYSTEM_ERR);
    292  1.1  christos 	pam_ssh_process_agent_output(pamh, f);
    293  1.1  christos 	fclose(f);
    294  1.1  christos 
    295  1.1  christos 	return (PAM_SUCCESS);
    296  1.1  christos }
    297  1.1  christos 
    298  1.1  christos /*
    299  1.1  christos  * Adds previously stored keys to a running agent.
    300  1.1  christos  */
    301  1.1  christos static int
    302  1.1  christos pam_ssh_add_keys_to_agent(pam_handle_t *pamh)
    303  1.1  christos {
    304  1.1  christos 	AuthenticationConnection *ac;
    305  1.1  christos 	struct pam_ssh_key *psk;
    306  1.1  christos 	const char **kfn;
    307  1.1  christos 	char **envlist, **env;
    308  1.1  christos 	int pam_err;
    309  1.1  christos 
    310  1.1  christos 	/* switch to PAM environment */
    311  1.1  christos 	envlist = environ;
    312  1.1  christos 	if ((environ = pam_getenvlist(pamh)) == NULL) {
    313  1.1  christos 		environ = envlist;
    314  1.1  christos 		return (PAM_SYSTEM_ERR);
    315  1.1  christos 	}
    316  1.1  christos 
    317  1.1  christos 	/* get a connection to the agent */
    318  1.1  christos 	if ((ac = ssh_get_authentication_connection()) == NULL) {
    319  1.1  christos 		pam_err = PAM_SYSTEM_ERR;
    320  1.1  christos 		goto end;
    321  1.1  christos 	}
    322  1.1  christos 
    323  1.1  christos 	/* look for keys to add to it */
    324  1.1  christos 	for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
    325  1.1  christos 		pam_err = pam_get_data(pamh, *kfn, (void **)&psk);
    326  1.1  christos 		if (pam_err == PAM_SUCCESS && psk != NULL) {
    327  1.1  christos 			if (ssh_add_identity(ac, psk->key, psk->comment))
    328  1.1  christos 				openpam_log(PAM_LOG_DEBUG,
    329  1.1  christos 				    "added %s to ssh agent", psk->comment);
    330  1.1  christos 			else
    331  1.1  christos 				openpam_log(PAM_LOG_DEBUG, "failed "
    332  1.1  christos 				    "to add %s to ssh agent", psk->comment);
    333  1.1  christos 			/* we won't need the key again, so wipe it */
    334  1.1  christos 			pam_set_data(pamh, *kfn, NULL, NULL);
    335  1.1  christos 		}
    336  1.1  christos 	}
    337  1.1  christos 	pam_err = PAM_SUCCESS;
    338  1.1  christos  end:
    339  1.1  christos 	/* disconnect from agent */
    340  1.1  christos 	if (ac != NULL)
    341  1.1  christos 		ssh_close_authentication_connection(ac);
    342  1.1  christos 
    343  1.1  christos 	/* switch back to original environment */
    344  1.1  christos 	for (env = environ; *env != NULL; ++env)
    345  1.1  christos 		free(*env);
    346  1.1  christos 	free(environ);
    347  1.1  christos 	environ = envlist;
    348  1.1  christos 
    349  1.1  christos 	return (pam_err);
    350  1.1  christos }
    351  1.1  christos 
    352  1.1  christos PAM_EXTERN int
    353  1.1  christos pam_sm_open_session(pam_handle_t *pamh, int flags __unused,
    354  1.1  christos     int argc __unused, const char *argv[] __unused)
    355  1.1  christos {
    356  1.1  christos 	struct passwd *pwd;
    357  1.1  christos 	const char *user;
    358  1.1  christos 	void *data;
    359  1.1  christos 	int pam_err;
    360  1.1  christos 
    361  1.1  christos 	/* no keys, no work */
    362  1.1  christos 	if (pam_get_data(pamh, pam_ssh_have_keys, &data) != PAM_SUCCESS &&
    363  1.1  christos 	    openpam_get_option(pamh, "want_agent") == NULL)
    364  1.1  christos 		return (PAM_SUCCESS);
    365  1.1  christos 
    366  1.1  christos 	/* switch to user credentials */
    367  1.1  christos 	pam_err = pam_get_user(pamh, &user, NULL);
    368  1.1  christos 	if (pam_err != PAM_SUCCESS)
    369  1.1  christos 		return (pam_err);
    370  1.1  christos 	pwd = getpwnam(user);
    371  1.1  christos 	if (pwd == NULL)
    372  1.1  christos 		return (PAM_USER_UNKNOWN);
    373  1.1  christos 	pam_err = openpam_borrow_cred(pamh, pwd);
    374  1.1  christos 	if (pam_err != PAM_SUCCESS)
    375  1.1  christos 		return (pam_err);
    376  1.1  christos 
    377  1.1  christos 	/* start the agent */
    378  1.1  christos 	pam_err = pam_ssh_start_agent(pamh);
    379  1.1  christos 	if (pam_err != PAM_SUCCESS) {
    380  1.1  christos 		openpam_restore_cred(pamh);
    381  1.1  christos 		return (pam_err);
    382  1.1  christos 	}
    383  1.1  christos 
    384  1.1  christos 	/* we have an agent, see if we can add any keys to it */
    385  1.1  christos 	pam_err = pam_ssh_add_keys_to_agent(pamh);
    386  1.1  christos 	if (pam_err != PAM_SUCCESS) {
    387  1.1  christos 		/* XXX ignore failures */
    388  1.1  christos 	}
    389  1.1  christos 
    390  1.1  christos 	openpam_restore_cred(pamh);
    391  1.1  christos 	return (PAM_SUCCESS);
    392  1.1  christos }
    393  1.1  christos 
    394  1.1  christos PAM_EXTERN int
    395  1.1  christos pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
    396  1.1  christos     int argc __unused, const char *argv[] __unused)
    397  1.1  christos {
    398  1.1  christos 	const char *ssh_agent_pid;
    399  1.1  christos 	char *end;
    400  1.1  christos 	int status;
    401  1.1  christos 	pid_t pid;
    402  1.1  christos 
    403  1.1  christos 	if ((ssh_agent_pid = pam_getenv(pamh, "SSH_AGENT_PID")) == NULL) {
    404  1.1  christos 		openpam_log(PAM_LOG_DEBUG, "no ssh agent");
    405  1.1  christos 		return (PAM_SUCCESS);
    406  1.1  christos 	}
    407  1.1  christos 	pid = (pid_t)strtol(ssh_agent_pid, &end, 10);
    408  1.1  christos 	if (*ssh_agent_pid == '\0' || *end != '\0') {
    409  1.1  christos 		openpam_log(PAM_LOG_DEBUG, "invalid ssh agent pid");
    410  1.1  christos 		return (PAM_SESSION_ERR);
    411  1.1  christos 	}
    412  1.1  christos 	openpam_log(PAM_LOG_DEBUG, "killing ssh agent %d", (int)pid);
    413  1.1  christos 	if (kill(pid, SIGTERM) == -1 ||
    414  1.1  christos 	    (waitpid(pid, &status, 0) == -1 && errno != ECHILD))
    415  1.1  christos 		return (PAM_SYSTEM_ERR);
    416  1.1  christos 	return (PAM_SUCCESS);
    417  1.1  christos }
    418  1.1  christos 
    419  1.1  christos PAM_MODULE_ENTRY("pam_ssh");
    420