2 * Copyright (c) 2003 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * DARPA CHATS research program.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior written
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $FreeBSD: src/lib/libpam/modules/pam_ssh/pam_ssh.c,v 1.45 2007/12/21 12:00:15 des Exp $
35 * $DragonFly: src/lib/pam_module/pam_ssh/pam_ssh.c,v 1.2 2006/09/29 06:35:03 corecode 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>
65 extern char **environ
;
72 static const char *pam_ssh_prompt
= "SSH passphrase: ";
73 static const char *pam_ssh_have_keys
= "pam_ssh_have_keys";
75 static const char *pam_ssh_keyfiles
[] = {
76 ".ssh/identity", /* SSH1 RSA key */
77 ".ssh/id_rsa", /* SSH2 RSA key */
78 ".ssh/id_dsa", /* SSH2 DSA key */
82 static const char *pam_ssh_agent
= "/usr/bin/ssh-agent";
83 static const char *pam_ssh_agent_argv
[] = { "ssh_agent", "-s", NULL
};
84 static char *const pam_ssh_agent_envp
[] = { NULL
};
87 * Attempts to load a private key from the specified file in the specified
88 * directory, using the specified passphrase. If successful, returns a
89 * struct pam_ssh_key containing the key and its comment.
91 static struct pam_ssh_key
*
92 pam_ssh_load_key(const char *dir
, const char *kfn
, const char *passphrase
)
94 struct pam_ssh_key
*psk
;
99 if (snprintf(fn
, sizeof(fn
), "%s/%s", dir
, kfn
) > (int)sizeof(fn
))
102 key
= key_load_private(fn
, passphrase
, &comment
);
104 openpam_log(PAM_LOG_DEBUG
, "failed to load key from %s\n", fn
);
108 openpam_log(PAM_LOG_DEBUG
, "loaded '%s' from %s\n", comment
, fn
);
109 if ((psk
= malloc(sizeof(*psk
))) == NULL
) {
115 psk
->comment
= comment
;
120 * Wipes a private key and frees the associated resources.
123 pam_ssh_free_key(pam_handle_t
*pamh __unused
,
124 void *data
, int pam_err __unused
)
126 struct pam_ssh_key
*psk
;
135 pam_sm_authenticate(pam_handle_t
*pamh
, int flags __unused
,
136 int argc __unused
, const char *argv
[] __unused
)
138 const char **kfn
, *passphrase
, *user
;
141 struct pam_ssh_key
*psk
;
142 int nkeys
, nullok
, pam_err
, pass
;
144 nullok
= (openpam_get_option(pamh
, "nullok") != NULL
);
146 /* PEM is not loaded by default */
147 OpenSSL_add_all_algorithms();
149 /* get user name and home directory */
150 pam_err
= pam_get_user(pamh
, &user
, NULL
);
151 if (pam_err
!= PAM_SUCCESS
)
153 pwd
= getpwnam(user
);
155 return (PAM_USER_UNKNOWN
);
156 if (pwd
->pw_dir
== NULL
)
157 return (PAM_AUTH_ERR
);
160 pass
= (pam_get_item(pamh
, PAM_AUTHTOK
, &item
) == PAM_SUCCESS
&&
164 pam_err
= pam_get_authtok(pamh
, PAM_AUTHTOK
,
165 &passphrase
, pam_ssh_prompt
);
166 if (pam_err
!= PAM_SUCCESS
)
169 if (*passphrase
== '\0' && !nullok
)
172 /* switch to user credentials */
173 pam_err
= openpam_borrow_cred(pamh
, pwd
);
174 if (pam_err
!= PAM_SUCCESS
)
177 /* try to load keys from all keyfiles we know of */
178 for (kfn
= pam_ssh_keyfiles
; *kfn
!= NULL
; ++kfn
) {
179 psk
= pam_ssh_load_key(pwd
->pw_dir
, *kfn
, passphrase
);
181 pam_set_data(pamh
, *kfn
, psk
, pam_ssh_free_key
);
186 /* switch back to arbitrator credentials */
187 openpam_restore_cred(pamh
);
191 * If we tried an old token and didn't get anything, and
192 * try_first_pass was specified, try again after prompting the
193 * user for a new passphrase.
195 if (nkeys
== 0 && pass
== 1 &&
196 openpam_get_option(pamh
, "try_first_pass") != NULL
) {
197 pam_set_item(pamh
, PAM_AUTHTOK
, NULL
);
204 return (PAM_AUTH_ERR
);
206 pam_set_data(pamh
, pam_ssh_have_keys
, NULL
, NULL
);
207 return (PAM_SUCCESS
);
211 pam_sm_setcred(pam_handle_t
*pamh __unused
, int flags __unused
,
212 int argc __unused
, const char *argv
[] __unused
)
215 return (PAM_SUCCESS
);
219 * Parses a line from ssh-agent's output.
222 pam_ssh_process_agent_output(pam_handle_t
*pamh
, FILE *f
)
224 char *line
, *p
, *key
, *val
;
227 while ((line
= fgetln(f
, &len
)) != NULL
) {
228 if (len
< 4 || strncmp(line
, "SSH_", 4) != 0)
231 /* find equal sign at end of key */
232 for (p
= key
= line
; p
< line
+ len
; ++p
)
235 if (p
== line
+ len
|| *p
!= '=')
239 /* find semicolon at end of value */
240 for (val
= ++p
; p
< line
+ len
; ++p
)
243 if (p
== line
+ len
|| *p
!= ';')
247 /* store key-value pair in environment */
248 openpam_log(PAM_LOG_DEBUG
, "got %s: %s", key
, val
);
249 pam_setenv(pamh
, key
, val
, 1);
254 * Starts an ssh agent and stores the environment variables derived from
258 pam_ssh_start_agent(pam_handle_t
*pamh
)
264 /* get a pipe which we will use to read the agent's output */
265 if (pipe(agent_pipe
) == -1)
266 return (PAM_SYSTEM_ERR
);
268 /* start the agent */
269 openpam_log(PAM_LOG_DEBUG
, "starting an ssh agent");
271 if (pid
== (pid_t
)-1) {
273 close(agent_pipe
[0]);
274 close(agent_pipe
[1]);
275 return (PAM_SYSTEM_ERR
);
280 /* child: drop privs, close fds and start agent */
284 open(_PATH_DEVNULL
, O_RDONLY
);
285 dup2(agent_pipe
[1], STDOUT_FILENO
);
286 dup2(agent_pipe
[1], STDERR_FILENO
);
287 for (fd
= 3; fd
< getdtablesize(); ++fd
)
289 execve(pam_ssh_agent
,
290 __DECONST(char * const *, pam_ssh_agent_argv
),
296 close(agent_pipe
[1]);
297 if ((f
= fdopen(agent_pipe
[0], "r")) == NULL
)
298 return (PAM_SYSTEM_ERR
);
299 pam_ssh_process_agent_output(pamh
, f
);
302 return (PAM_SUCCESS
);
306 * Adds previously stored keys to a running agent.
309 pam_ssh_add_keys_to_agent(pam_handle_t
*pamh
)
311 AuthenticationConnection
*ac
;
312 const struct pam_ssh_key
*psk
;
314 char **envlist
, **env
;
317 /* switch to PAM environment */
319 if ((environ
= pam_getenvlist(pamh
)) == NULL
) {
321 return (PAM_SYSTEM_ERR
);
324 /* get a connection to the agent */
325 if ((ac
= ssh_get_authentication_connection()) == NULL
) {
326 pam_err
= PAM_SYSTEM_ERR
;
330 /* look for keys to add to it */
331 for (kfn
= pam_ssh_keyfiles
; *kfn
!= NULL
; ++kfn
) {
333 pam_err
= pam_get_data(pamh
, *kfn
, &vp
);
335 if (pam_err
== PAM_SUCCESS
&& psk
!= NULL
) {
336 if (ssh_add_identity(ac
, psk
->key
, psk
->comment
))
337 openpam_log(PAM_LOG_DEBUG
,
338 "added %s to ssh agent", psk
->comment
);
340 openpam_log(PAM_LOG_DEBUG
, "failed "
341 "to add %s to ssh agent", psk
->comment
);
342 /* we won't need the key again, so wipe it */
343 pam_set_data(pamh
, *kfn
, NULL
, NULL
);
346 pam_err
= PAM_SUCCESS
;
348 /* disconnect from agent */
350 ssh_close_authentication_connection(ac
);
352 /* switch back to original environment */
353 for (env
= environ
; *env
!= NULL
; ++env
)
362 pam_sm_open_session(pam_handle_t
*pamh
, int flags __unused
,
363 int argc __unused
, const char *argv
[] __unused
)
370 /* no keys, no work */
371 if (pam_get_data(pamh
, pam_ssh_have_keys
, &data
) != PAM_SUCCESS
&&
372 openpam_get_option(pamh
, "want_agent") == NULL
)
373 return (PAM_SUCCESS
);
375 /* switch to user credentials */
376 pam_err
= pam_get_user(pamh
, &user
, NULL
);
377 if (pam_err
!= PAM_SUCCESS
)
379 pwd
= getpwnam(user
);
381 return (PAM_USER_UNKNOWN
);
382 pam_err
= openpam_borrow_cred(pamh
, pwd
);
383 if (pam_err
!= PAM_SUCCESS
)
386 /* start the agent */
387 pam_err
= pam_ssh_start_agent(pamh
);
388 if (pam_err
!= PAM_SUCCESS
) {
389 openpam_restore_cred(pamh
);
393 /* we have an agent, see if we can add any keys to it */
394 pam_err
= pam_ssh_add_keys_to_agent(pamh
);
395 if (pam_err
!= PAM_SUCCESS
) {
396 /* XXX ignore failures */
399 openpam_restore_cred(pamh
);
400 return (PAM_SUCCESS
);
404 pam_sm_close_session(pam_handle_t
*pamh
, int flags __unused
,
405 int argc __unused
, const char *argv
[] __unused
)
407 const char *ssh_agent_pid
;
412 if ((ssh_agent_pid
= pam_getenv(pamh
, "SSH_AGENT_PID")) == NULL
) {
413 openpam_log(PAM_LOG_DEBUG
, "no ssh agent");
414 return (PAM_SUCCESS
);
416 pid
= (pid_t
)strtol(ssh_agent_pid
, &end
, 10);
417 if (*ssh_agent_pid
== '\0' || *end
!= '\0') {
418 openpam_log(PAM_LOG_DEBUG
, "invalid ssh agent pid");
419 return (PAM_SESSION_ERR
);
421 openpam_log(PAM_LOG_DEBUG
, "killing ssh agent %d", (int)pid
);
422 if (kill(pid
, SIGTERM
) == -1 ||
423 (waitpid(pid
, &status
, 0) == -1 && errno
!= ECHILD
))
424 return (PAM_SYSTEM_ERR
);
425 return (PAM_SUCCESS
);
428 PAM_MODULE_ENTRY("pam_ssh");