pam_ssh: pam_ssh: Don't allow a bogus passphrase for unencrypted keys.
[dragonfly.git] / lib / pam_module / pam_ssh / pam_ssh.c
CommitLineData
f8bdfa2d
JS
1/*-
2 * Copyright (c) 2003 Networks Associates Technology, Inc.
3 * All rights reserved.
4 *
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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
20 * permission.
21 *
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
32 * SUCH DAMAGE.
33 *
6a3bc796 34 * $FreeBSD: src/lib/libpam/modules/pam_ssh/pam_ssh.c,v 1.45 2007/12/21 12:00:15 des Exp $
fc401d71 35 * $DragonFly: src/lib/pam_module/pam_ssh/pam_ssh.c,v 1.2 2006/09/29 06:35:03 corecode Exp $
f8bdfa2d
JS
36 */
37
38#include <sys/param.h>
39#include <sys/wait.h>
40
41#include <errno.h>
42#include <fcntl.h>
43#include <paths.h>
44#include <pwd.h>
45#include <signal.h>
46#include <stdio.h>
47#include <string.h>
48#include <unistd.h>
49
50#define PAM_SM_AUTH
51#define PAM_SM_SESSION
52
53#include <security/pam_appl.h>
54#include <security/pam_modules.h>
55#include <security/openpam.h>
56
57#include <openssl/evp.h>
58
fc401d71 59#include "buffer.h"
f8bdfa2d 60#include "key.h"
6a3bc796 61#include "buffer.h"
f8bdfa2d
JS
62#include "authfd.h"
63#include "authfile.h"
64
4d25bb71
PA
65#define ssh_add_identity(auth, key, comment) \
66 ssh_add_identity_constrained(auth, key, comment, 0, 0)
67
f8bdfa2d
JS
68extern char **environ;
69
70struct pam_ssh_key {
71 Key *key;
72 char *comment;
73};
74
75static const char *pam_ssh_prompt = "SSH passphrase: ";
76static const char *pam_ssh_have_keys = "pam_ssh_have_keys";
77
78static const char *pam_ssh_keyfiles[] = {
79 ".ssh/identity", /* SSH1 RSA key */
80 ".ssh/id_rsa", /* SSH2 RSA key */
81 ".ssh/id_dsa", /* SSH2 DSA key */
82 NULL
83};
84
85static const char *pam_ssh_agent = "/usr/bin/ssh-agent";
86static const char *pam_ssh_agent_argv[] = { "ssh_agent", "-s", NULL };
87static char *const pam_ssh_agent_envp[] = { NULL };
88
89/*
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.
93 */
94static struct pam_ssh_key *
abe58d8a
PA
95pam_ssh_load_key(const char *dir, const char *kfn, const char *passphrase,
96 int nullok)
f8bdfa2d
JS
97{
98 struct pam_ssh_key *psk;
99 char fn[PATH_MAX];
100 char *comment;
101 Key *key;
102
103 if (snprintf(fn, sizeof(fn), "%s/%s", dir, kfn) > (int)sizeof(fn))
104 return (NULL);
105 comment = NULL;
abe58d8a
PA
106 /*
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.
113 */
114 key = key_load_private(fn, NULL, &comment);
115 if (key != NULL && !(*passphrase == '\0' && nullok)) {
116 key_free(key);
117 return (NULL);
118 }
119 if (key == NULL)
120 key = key_load_private(fn, passphrase, &comment);
f8bdfa2d
JS
121 if (key == NULL) {
122 openpam_log(PAM_LOG_DEBUG, "failed to load key from %s\n", fn);
123 return (NULL);
124 }
125
126 openpam_log(PAM_LOG_DEBUG, "loaded '%s' from %s\n", comment, fn);
127 if ((psk = malloc(sizeof(*psk))) == NULL) {
128 key_free(key);
129 free(comment);
130 return (NULL);
131 }
132 psk->key = key;
133 psk->comment = comment;
134 return (psk);
135}
136
137/*
138 * Wipes a private key and frees the associated resources.
139 */
140static void
141pam_ssh_free_key(pam_handle_t *pamh __unused,
142 void *data, int pam_err __unused)
143{
144 struct pam_ssh_key *psk;
145
146 psk = data;
147 key_free(psk->key);
148 free(psk->comment);
149 free(psk);
150}
151
152PAM_EXTERN int
153pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
154 int argc __unused, const char *argv[] __unused)
155{
156 const char **kfn, *passphrase, *user;
6a3bc796 157 const void *item;
f8bdfa2d
JS
158 struct passwd *pwd;
159 struct pam_ssh_key *psk;
6a3bc796
PA
160 int nkeys, nullok, pam_err, pass;
161
162 nullok = (openpam_get_option(pamh, "nullok") != NULL);
f8bdfa2d
JS
163
164 /* PEM is not loaded by default */
165 OpenSSL_add_all_algorithms();
166
167 /* get user name and home directory */
168 pam_err = pam_get_user(pamh, &user, NULL);
169 if (pam_err != PAM_SUCCESS)
170 return (pam_err);
171 pwd = getpwnam(user);
172 if (pwd == NULL)
173 return (PAM_USER_UNKNOWN);
174 if (pwd->pw_dir == NULL)
175 return (PAM_AUTH_ERR);
176
6a3bc796
PA
177 nkeys = 0;
178 pass = (pam_get_item(pamh, PAM_AUTHTOK, &item) == PAM_SUCCESS &&
179 item != NULL);
f8bdfa2d
JS
180 load_keys:
181 /* get passphrase */
182 pam_err = pam_get_authtok(pamh, PAM_AUTHTOK,
183 &passphrase, pam_ssh_prompt);
6a3bc796
PA
184 if (pam_err != PAM_SUCCESS)
185 return (pam_err);
186
6a3bc796
PA
187 /* switch to user credentials */
188 pam_err = openpam_borrow_cred(pamh, pwd);
189 if (pam_err != PAM_SUCCESS)
f8bdfa2d 190 return (pam_err);
f8bdfa2d
JS
191
192 /* try to load keys from all keyfiles we know of */
f8bdfa2d 193 for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
abe58d8a 194 psk = pam_ssh_load_key(pwd->pw_dir, *kfn, passphrase, nullok);
f8bdfa2d
JS
195 if (psk != NULL) {
196 pam_set_data(pamh, *kfn, psk, pam_ssh_free_key);
197 ++nkeys;
198 }
199 }
200
6a3bc796
PA
201 /* switch back to arbitrator credentials */
202 openpam_restore_cred(pamh);
203
f8bdfa2d
JS
204 /*
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.
208 */
209 if (nkeys == 0 && pass == 1 &&
210 openpam_get_option(pamh, "try_first_pass") != NULL) {
211 pam_set_item(pamh, PAM_AUTHTOK, NULL);
212 pass = 0;
213 goto load_keys;
214 }
215
f8bdfa2d
JS
216 /* no keys? */
217 if (nkeys == 0)
218 return (PAM_AUTH_ERR);
219
220 pam_set_data(pamh, pam_ssh_have_keys, NULL, NULL);
221 return (PAM_SUCCESS);
222}
223
224PAM_EXTERN int
225pam_sm_setcred(pam_handle_t *pamh __unused, int flags __unused,
226 int argc __unused, const char *argv[] __unused)
227{
228
229 return (PAM_SUCCESS);
230}
231
232/*
233 * Parses a line from ssh-agent's output.
234 */
235static void
236pam_ssh_process_agent_output(pam_handle_t *pamh, FILE *f)
237{
238 char *line, *p, *key, *val;
239 size_t len;
240
241 while ((line = fgetln(f, &len)) != NULL) {
242 if (len < 4 || strncmp(line, "SSH_", 4) != 0)
243 continue;
244
245 /* find equal sign at end of key */
246 for (p = key = line; p < line + len; ++p)
247 if (*p == '=')
248 break;
249 if (p == line + len || *p != '=')
250 continue;
251 *p = '\0';
252
253 /* find semicolon at end of value */
254 for (val = ++p; p < line + len; ++p)
255 if (*p == ';')
256 break;
257 if (p == line + len || *p != ';')
258 continue;
259 *p = '\0';
260
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);
264 }
265}
266
267/*
268 * Starts an ssh agent and stores the environment variables derived from
269 * its output.
270 */
271static int
272pam_ssh_start_agent(pam_handle_t *pamh)
273{
274 int agent_pipe[2];
275 pid_t pid;
276 FILE *f;
277
278 /* get a pipe which we will use to read the agent's output */
6a3bc796 279 if (pipe(agent_pipe) == -1)
f8bdfa2d 280 return (PAM_SYSTEM_ERR);
f8bdfa2d
JS
281
282 /* start the agent */
283 openpam_log(PAM_LOG_DEBUG, "starting an ssh agent");
284 pid = fork();
285 if (pid == (pid_t)-1) {
286 /* failed */
287 close(agent_pipe[0]);
288 close(agent_pipe[1]);
289 return (PAM_SYSTEM_ERR);
290 }
291 if (pid == 0) {
292 int fd;
293
294 /* child: drop privs, close fds and start agent */
295 setgid(getegid());
296 setuid(geteuid());
297 close(STDIN_FILENO);
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)
302 close(fd);
303 execve(pam_ssh_agent,
304 __DECONST(char * const *, pam_ssh_agent_argv),
305 pam_ssh_agent_envp);
306 _exit(127);
307 }
308
309 /* parent */
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);
314 fclose(f);
315
316 return (PAM_SUCCESS);
317}
318
319/*
320 * Adds previously stored keys to a running agent.
321 */
322static int
323pam_ssh_add_keys_to_agent(pam_handle_t *pamh)
324{
325 AuthenticationConnection *ac;
210bf96c 326 const struct pam_ssh_key *psk;
f8bdfa2d
JS
327 const char **kfn;
328 char **envlist, **env;
329 int pam_err;
330
331 /* switch to PAM environment */
332 envlist = environ;
333 if ((environ = pam_getenvlist(pamh)) == NULL) {
334 environ = envlist;
335 return (PAM_SYSTEM_ERR);
336 }
337
338 /* get a connection to the agent */
339 if ((ac = ssh_get_authentication_connection()) == NULL) {
340 pam_err = PAM_SYSTEM_ERR;
341 goto end;
342 }
343
344 /* look for keys to add to it */
345 for (kfn = pam_ssh_keyfiles; *kfn != NULL; ++kfn) {
0234afdd
SW
346 const void *vp;
347 pam_err = pam_get_data(pamh, *kfn, &vp);
348 psk = vp;
f8bdfa2d
JS
349 if (pam_err == PAM_SUCCESS && psk != NULL) {
350 if (ssh_add_identity(ac, psk->key, psk->comment))
351 openpam_log(PAM_LOG_DEBUG,
352 "added %s to ssh agent", psk->comment);
353 else
354 openpam_log(PAM_LOG_DEBUG, "failed "
355 "to add %s to ssh agent", psk->comment);
356 /* we won't need the key again, so wipe it */
357 pam_set_data(pamh, *kfn, NULL, NULL);
358 }
359 }
360 pam_err = PAM_SUCCESS;
361 end:
362 /* disconnect from agent */
363 if (ac != NULL)
364 ssh_close_authentication_connection(ac);
365
366 /* switch back to original environment */
367 for (env = environ; *env != NULL; ++env)
368 free(*env);
369 free(environ);
370 environ = envlist;
371
372 return (pam_err);
373}
374
375PAM_EXTERN int
376pam_sm_open_session(pam_handle_t *pamh, int flags __unused,
377 int argc __unused, const char *argv[] __unused)
378{
379 struct passwd *pwd;
380 const char *user;
210bf96c 381 const void *data;
f8bdfa2d
JS
382 int pam_err;
383
384 /* no keys, no work */
385 if (pam_get_data(pamh, pam_ssh_have_keys, &data) != PAM_SUCCESS &&
386 openpam_get_option(pamh, "want_agent") == NULL)
387 return (PAM_SUCCESS);
388
389 /* switch to user credentials */
390 pam_err = pam_get_user(pamh, &user, NULL);
391 if (pam_err != PAM_SUCCESS)
392 return (pam_err);
393 pwd = getpwnam(user);
394 if (pwd == NULL)
395 return (PAM_USER_UNKNOWN);
396 pam_err = openpam_borrow_cred(pamh, pwd);
397 if (pam_err != PAM_SUCCESS)
398 return (pam_err);
399
400 /* start the agent */
401 pam_err = pam_ssh_start_agent(pamh);
402 if (pam_err != PAM_SUCCESS) {
403 openpam_restore_cred(pamh);
404 return (pam_err);
405 }
406
407 /* we have an agent, see if we can add any keys to it */
408 pam_err = pam_ssh_add_keys_to_agent(pamh);
409 if (pam_err != PAM_SUCCESS) {
410 /* XXX ignore failures */
411 }
412
413 openpam_restore_cred(pamh);
414 return (PAM_SUCCESS);
415}
416
417PAM_EXTERN int
418pam_sm_close_session(pam_handle_t *pamh, int flags __unused,
419 int argc __unused, const char *argv[] __unused)
420{
421 const char *ssh_agent_pid;
422 char *end;
423 int status;
424 pid_t pid;
425
426 if ((ssh_agent_pid = pam_getenv(pamh, "SSH_AGENT_PID")) == NULL) {
427 openpam_log(PAM_LOG_DEBUG, "no ssh agent");
428 return (PAM_SUCCESS);
429 }
430 pid = (pid_t)strtol(ssh_agent_pid, &end, 10);
431 if (*ssh_agent_pid == '\0' || *end != '\0') {
432 openpam_log(PAM_LOG_DEBUG, "invalid ssh agent pid");
433 return (PAM_SESSION_ERR);
434 }
435 openpam_log(PAM_LOG_DEBUG, "killing ssh agent %d", (int)pid);
436 if (kill(pid, SIGTERM) == -1 ||
437 (waitpid(pid, &status, 0) == -1 && errno != ECHILD))
438 return (PAM_SYSTEM_ERR);
439 return (PAM_SUCCESS);
440}
441
442PAM_MODULE_ENTRY("pam_ssh");