2 * Copyright (c) 2003 Networks Associates Technology, Inc.
3 * Copyright (c) 2004-2011 Dag-Erling Smørgrav
6 * This software was developed for the FreeBSD Project by ThinkSec AS and
7 * NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $FreeBSD: src/lib/libpam/modules/pam_ssh/pam_ssh.c,v 1.49 2011/11/20 15:18:49 des Exp $
38 #include <sys/param.h>
51 #define PAM_SM_SESSION
53 #include <security/pam_appl.h>
54 #include <security/pam_modules.h>
55 #include <security/openpam.h>
57 #include <openssl/evp.h>
64 #define ssh_add_identity(auth, key, comment) \
65 ssh_add_identity_constrained(auth, key, comment, 0, 0)
67 extern char **environ;
74 static const char *pam_ssh_prompt = "SSH passphrase: ";
75 static const char *pam_ssh_have_keys = "pam_ssh_have_keys";
77 static const char *pam_ssh_keyfiles[] = {
78 ".ssh/identity", /* SSH1 RSA key */
79 ".ssh/id_rsa", /* SSH2 RSA key */
80 ".ssh/id_dsa", /* SSH2 DSA key */
81 ".ssh/id_ecdsa", /* SSH2 ECDSA key */
85 static const char *pam_ssh_agent = "/usr/bin/ssh-agent";
86 static const char *pam_ssh_agent_argv[] = { "ssh_agent", "-s", NULL };
87 static char *const pam_ssh_agent_envp[] = { NULL };
90 * Attempts to load a private key from the specified file in the specified
91 * directory, using the specified passphrase. If successful, returns a
92 * struct pam_ssh_key containing the key and its comment.
94 static struct pam_ssh_key *
95 pam_ssh_load_key(const char *dir, const char *kfn, const char *passphrase,
98 struct pam_ssh_key *psk;
103 if (snprintf(fn, sizeof(fn), "%s/%s", dir, kfn) > (int)sizeof(fn))
107 * If the key is unencrypted, OpenSSL ignores the passphrase, so
108 * it will seem like the user typed in the right one. This allows
109 * a user to circumvent nullok by providing a dummy passphrase.
110 * Verify that the key really *is* encrypted by trying to load it
111 * with an empty passphrase, and if the key is not encrypted,
112 * accept only an empty passphrase.
114 key = key_load_private(fn, NULL, &comment);
115 if (key != NULL && !(*passphrase == '\0' && nullok)) {
120 key = key_load_private(fn, passphrase, &comment);
122 openpam_log(PAM_LOG_DEBUG, "failed to load key from %s", fn);
126 openpam_log(PAM_LOG_DEBUG, "loaded '%s' from %s", comment, fn);
127 if ((psk = malloc(sizeof(*psk))) == NULL) {
133 psk->comment = comment;
138 * Wipes a private key and frees the associated resources.
141 pam_ssh_free_key(pam_handle_t *pamh __unused,
142 void *data, int pam_err __unused)
144 struct pam_ssh_key *psk;
153 pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
154 int argc __unused, const char *argv[] __unused)
156 const char **kfn, *passphrase, *user;
159 struct pam_ssh_key *psk;
160 int nkeys, nullok, pam_err, pass;
162 nullok = (openpam_get_option(pamh, "nullok") != NULL);
164 /* PEM is not loaded by default */
165 OpenSSL_add_all_algorithms();
167 /* get user name and home directory */
168 pam_err = pam_get_user(pamh, &user, NULL);
169 if (pam_err != PAM_SUCCESS)
171 pwd = getpwnam(user);
173 return (PAM_USER_UNKNOWN);
174 if (pwd->pw_dir == NULL)
175 return (PAM_AUTH_ERR);
178 pass = (pam_get_item(pamh, PAM_AUTHTOK, &item) == PAM_SUCCESS &&
182 pam_err = pam_get_authtok(pamh, PAM_AUTHTOK,
183 &passphrase, pam_ssh_prompt);
184 if (pam_err != PAM_SUCCESS)
187 /* switch to user credentials */
188 pam_err = openpam_borrow_cred(pamh, pwd);
189 if (pam_err != PAM_SUCCESS)
192 /* try to load keys from all keyfiles we know of */
193 for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
194 psk = pam_ssh_load_key(pwd->pw_dir, *kfn, passphrase, nullok);
196 pam_set_data(pamh, *kfn, psk, pam_ssh_free_key);
201 /* switch back to arbitrator credentials */
202 openpam_restore_cred(pamh);
205 * If we tried an old token and didn't get anything, and
206 * try_first_pass was specified, try again after prompting the
207 * user for a new passphrase.
209 if (nkeys == 0 && pass == 1 &&
210 openpam_get_option(pamh, "try_first_pass") != NULL) {
211 pam_set_item(pamh, PAM_AUTHTOK, NULL);
218 return (PAM_AUTH_ERR);
220 pam_set_data(pamh, pam_ssh_have_keys, NULL, NULL);
221 return (PAM_SUCCESS);
225 pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
226 int argc __unused, const char *argv[] __unused)
229 return (PAM_SUCCESS);
233 * Parses a line from ssh-agent's output.
236 pam_ssh_process_agent_output(pam_handle_t *pamh, FILE *f)
238 char *line, *p, *key, *val;
241 while ((line = fgetln(f, &len)) != NULL) {
242 if (len < 4 || strncmp(line, "SSH_", 4) != 0)
245 /* find equal sign at end of key */
246 for (p = key = line; p < line + len; ++p)
249 if (p == line + len || *p != '=')
253 /* find semicolon at end of value */
254 for (val = ++p; p < line + len; ++p)
257 if (p == line + len || *p != ';')
261 /* store key-value pair in environment */
262 openpam_log(PAM_LOG_DEBUG, "got %s: %s", key, val);
263 pam_setenv(pamh, key, val, 1);
268 * Starts an ssh agent and stores the environment variables derived from
272 pam_ssh_start_agent(pam_handle_t *pamh)
278 /* get a pipe which we will use to read the agent's output */
279 if (pipe(agent_pipe) == -1)
280 return (PAM_SYSTEM_ERR);
282 /* start the agent */
283 openpam_log(PAM_LOG_DEBUG, "starting an ssh agent");
285 if (pid == (pid_t)-1) {
287 close(agent_pipe[0]);
288 close(agent_pipe[1]);
289 return (PAM_SYSTEM_ERR);
294 /* child: drop privs, close fds and start agent */
298 open(_PATH_DEVNULL, O_RDONLY);
299 dup2(agent_pipe[1], STDOUT_FILENO);
300 dup2(agent_pipe[1], STDERR_FILENO);
301 for (fd = 3; fd < getdtablesize(); ++fd)
303 execve(pam_ssh_agent,
304 __DECONST(char * const *, pam_ssh_agent_argv),
310 close(agent_pipe[1]);
311 if ((f = fdopen(agent_pipe[0], "r")) == NULL)
312 return (PAM_SYSTEM_ERR);
313 pam_ssh_process_agent_output(pamh, f);
316 return (PAM_SUCCESS);
320 * Adds previously stored keys to a running agent.
323 pam_ssh_add_keys_to_agent(pam_handle_t *pamh)
325 AuthenticationConnection *ac;
326 const struct pam_ssh_key *psk;
329 char **envlist, **env;
332 /* switch to PAM environment */
334 if ((environ = pam_getenvlist(pamh)) == NULL) {
336 return (PAM_SYSTEM_ERR);
339 /* get a connection to the agent */
340 if ((ac = ssh_get_authentication_connection()) == NULL) {
341 openpam_log(PAM_LOG_DEBUG, "failed to connect to the agent");
342 pam_err = PAM_SYSTEM_ERR;
346 /* look for keys to add to it */
347 for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
348 pam_err = pam_get_data(pamh, *kfn, &item);
349 if (pam_err == PAM_SUCCESS && item != NULL) {
351 if (ssh_add_identity(ac, psk->key, psk->comment))
352 openpam_log(PAM_LOG_DEBUG,
353 "added %s to ssh agent", psk->comment);
355 openpam_log(PAM_LOG_DEBUG, "failed "
356 "to add %s to ssh agent", psk->comment);
357 /* we won't need the key again, so wipe it */
358 pam_set_data(pamh, *kfn, NULL, NULL);
361 pam_err = PAM_SUCCESS;
363 /* disconnect from agent */
365 ssh_close_authentication_connection(ac);
367 /* switch back to original environment */
368 for (env = environ; *env != NULL; ++env)
377 pam_sm_open_session(pam_handle_t *pamh, int flags __unused,
378 int argc __unused, const char *argv[] __unused)
385 /* no keys, no work */
386 if (pam_get_data(pamh, pam_ssh_have_keys, &data) != PAM_SUCCESS &&
387 openpam_get_option(pamh, "want_agent") == NULL)
388 return (PAM_SUCCESS);
390 /* switch to user credentials */
391 pam_err = pam_get_user(pamh, &user, NULL);
392 if (pam_err != PAM_SUCCESS)
394 pwd = getpwnam(user);
396 return (PAM_USER_UNKNOWN);
397 pam_err = openpam_borrow_cred(pamh, pwd);
398 if (pam_err != PAM_SUCCESS)
401 /* start the agent */
402 pam_err = pam_ssh_start_agent(pamh);
403 if (pam_err != PAM_SUCCESS) {
404 openpam_restore_cred(pamh);
408 /* we have an agent, see if we can add any keys to it */
409 pam_err = pam_ssh_add_keys_to_agent(pamh);
410 if (pam_err != PAM_SUCCESS) {
411 /* XXX ignore failures */
414 openpam_restore_cred(pamh);
415 return (PAM_SUCCESS);
419 pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
420 int argc __unused, const char *argv[] __unused)
422 const char *ssh_agent_pid;
427 if ((ssh_agent_pid = pam_getenv(pamh, "SSH_AGENT_PID")) == NULL) {
428 openpam_log(PAM_LOG_DEBUG, "no ssh agent");
429 return (PAM_SUCCESS);
431 pid = (pid_t)strtol(ssh_agent_pid, &end, 10);
432 if (*ssh_agent_pid == '\0' || *end != '\0') {
433 openpam_log(PAM_LOG_DEBUG, "invalid ssh agent pid");
434 return (PAM_SESSION_ERR);
436 openpam_log(PAM_LOG_DEBUG, "killing ssh agent %d", (int)pid);
437 if (kill(pid, SIGTERM) == -1 ||
438 (waitpid(pid, &status, 0) == -1 && errno != ECHILD))
439 return (PAM_SYSTEM_ERR);
440 return (PAM_SUCCESS);
443 PAM_MODULE_ENTRY("pam_ssh");