]>
Commit | Line | Data |
---|---|---|
05114c74 | 1 | /*- |
2 | * Copyright (c) 2002 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. | |
09564242 | 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. | |
09564242 | 18 | * |
05114c74 | 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
a5c9cd31 | 30 | */ |
93c5ef94 | 31 | /* |
32 | * Copyright (c) 2003,2004 Damien Miller <djm@mindrot.org> | |
33 | * Copyright (c) 2003,2004 Darren Tucker <dtucker@zip.com.au> | |
34 | * | |
35 | * Permission to use, copy, modify, and distribute this software for any | |
36 | * purpose with or without fee is hereby granted, provided that the above | |
37 | * copyright notice and this permission notice appear in all copies. | |
38 | * | |
39 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
40 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
41 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
42 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
43 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
44 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
45 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
46 | */ | |
a5c9cd31 | 47 | |
5ff453c0 | 48 | /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */ |
a5c9cd31 | 49 | #include "includes.h" |
5ff453c0 | 50 | RCSID("$Id$"); |
a5c9cd31 | 51 | |
52 | #ifdef USE_PAM | |
2511d104 | 53 | #if defined(HAVE_SECURITY_PAM_APPL_H) |
05114c74 | 54 | #include <security/pam_appl.h> |
2511d104 | 55 | #elif defined (HAVE_PAM_PAM_APPL_H) |
56 | #include <pam/pam_appl.h> | |
57 | #endif | |
05114c74 | 58 | |
fde58bd4 | 59 | #include "auth.h" |
5c377b3b | 60 | #include "auth-pam.h" |
05114c74 | 61 | #include "buffer.h" |
62 | #include "bufaux.h" | |
42f11eb2 | 63 | #include "canohost.h" |
05114c74 | 64 | #include "log.h" |
65 | #include "monitor_wrap.h" | |
66 | #include "msg.h" | |
67 | #include "packet.h" | |
0f3ee929 | 68 | #include "misc.h" |
05114c74 | 69 | #include "servconf.h" |
70 | #include "ssh2.h" | |
71 | #include "xmalloc.h" | |
5b9e2464 | 72 | #include "auth-options.h" |
a5c9cd31 | 73 | |
7fceb20d | 74 | extern ServerOptions options; |
3eaf3960 | 75 | extern Buffer loginmsg; |
b3ef7fb7 | 76 | extern int compat20; |
1e08e787 | 77 | extern u_int utmp_len; |
7fceb20d | 78 | |
05114c74 | 79 | #ifdef USE_POSIX_THREADS |
80 | #include <pthread.h> | |
81 | /* | |
aff51935 | 82 | * Avoid namespace clash when *not* using pthreads for systems *with* |
83 | * pthreads, which unconditionally define pthread_t via sys/types.h | |
05114c74 | 84 | * (e.g. Linux) |
85 | */ | |
aff51935 | 86 | typedef pthread_t sp_pthread_t; |
05114c74 | 87 | #else |
e47e681f | 88 | typedef pid_t sp_pthread_t; |
89 | #endif | |
90 | ||
91 | struct pam_ctxt { | |
92 | sp_pthread_t pam_thread; | |
93 | int pam_psock; | |
94 | int pam_csock; | |
95 | int pam_done; | |
96 | }; | |
97 | ||
98 | static void sshpam_free_ctx(void *); | |
99 | static struct pam_ctxt *cleanup_ctxt; | |
100 | ||
101 | #ifndef USE_POSIX_THREADS | |
05114c74 | 102 | /* |
103 | * Simulate threads with processes. | |
104 | */ | |
a5c9cd31 | 105 | |
100e6910 | 106 | static int sshpam_thread_status = -1; |
107 | static mysig_t sshpam_oldsig; | |
108 | ||
109 | static void | |
110 | sshpam_sigchld_handler(int sig) | |
111 | { | |
8a956cda | 112 | signal(SIGCHLD, SIG_DFL); |
28b49ff8 | 113 | if (cleanup_ctxt == NULL) |
114 | return; /* handler called after PAM cleanup, shouldn't happen */ | |
8a956cda | 115 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, WNOHANG) |
930d0441 | 116 | <= 0) { |
8a956cda | 117 | /* PAM thread has not exitted, privsep slave must have */ |
118 | kill(cleanup_ctxt->pam_thread, SIGTERM); | |
119 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) | |
930d0441 | 120 | <= 0) |
8a956cda | 121 | return; /* could not wait */ |
122 | } | |
100e6910 | 123 | if (WIFSIGNALED(sshpam_thread_status) && |
124 | WTERMSIG(sshpam_thread_status) == SIGTERM) | |
125 | return; /* terminated by pthread_cancel */ | |
126 | if (!WIFEXITED(sshpam_thread_status)) | |
127 | fatal("PAM: authentication thread exited unexpectedly"); | |
128 | if (WEXITSTATUS(sshpam_thread_status) != 0) | |
129 | fatal("PAM: authentication thread exited uncleanly"); | |
130 | } | |
131 | ||
05114c74 | 132 | static void |
133 | pthread_exit(void *value __unused) | |
134 | { | |
135 | _exit(0); | |
136 | } | |
5daf7064 | 137 | |
05114c74 | 138 | static int |
139 | pthread_create(sp_pthread_t *thread, const void *attr __unused, | |
140 | void *(*thread_start)(void *), void *arg) | |
141 | { | |
142 | pid_t pid; | |
143 | ||
355fbf31 | 144 | sshpam_thread_status = -1; |
05114c74 | 145 | switch ((pid = fork())) { |
146 | case -1: | |
147 | error("fork(): %s", strerror(errno)); | |
148 | return (-1); | |
149 | case 0: | |
150 | thread_start(arg); | |
151 | _exit(1); | |
152 | default: | |
153 | *thread = pid; | |
100e6910 | 154 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
05114c74 | 155 | return (0); |
156 | } | |
157 | } | |
a5c9cd31 | 158 | |
05114c74 | 159 | static int |
160 | pthread_cancel(sp_pthread_t thread) | |
8c9fe09e | 161 | { |
28b49ff8 | 162 | signal(SIGCHLD, sshpam_oldsig); |
05114c74 | 163 | return (kill(thread, SIGTERM)); |
8c9fe09e | 164 | } |
165 | ||
05114c74 | 166 | static int |
167 | pthread_join(sp_pthread_t thread, void **value __unused) | |
8c9fe09e | 168 | { |
05114c74 | 169 | int status; |
170 | ||
100e6910 | 171 | if (sshpam_thread_status != -1) |
172 | return (sshpam_thread_status); | |
173 | signal(SIGCHLD, sshpam_oldsig); | |
05114c74 | 174 | waitpid(thread, &status, 0); |
175 | return (status); | |
8c9fe09e | 176 | } |
05114c74 | 177 | #endif |
178 | ||
179 | ||
0a23d79f | 180 | static pam_handle_t *sshpam_handle = NULL; |
181 | static int sshpam_err = 0; | |
182 | static int sshpam_authenticated = 0; | |
0a23d79f | 183 | static int sshpam_session_open = 0; |
184 | static int sshpam_cred_established = 0; | |
b3ef7fb7 | 185 | static int sshpam_account_status = -1; |
2212fc98 | 186 | static char **sshpam_env = NULL; |
d948154a | 187 | static Authctxt *sshpam_authctxt = NULL; |
5de92f17 | 188 | static const char *sshpam_password = NULL; |
6d51251f | 189 | static char badpw[] = "\b\n\r\177INCORRECT"; |
05114c74 | 190 | |
2212fc98 | 191 | /* Some PAM implementations don't implement this */ |
192 | #ifndef HAVE_PAM_GETENVLIST | |
193 | static char ** | |
194 | pam_getenvlist(pam_handle_t *pamh) | |
195 | { | |
196 | /* | |
aff51935 | 197 | * XXX - If necessary, we can still support envrionment passing |
2212fc98 | 198 | * for platforms without pam_getenvlist by searching for known |
199 | * env vars (e.g. KRB5CCNAME) from the PAM environment. | |
200 | */ | |
201 | return NULL; | |
202 | } | |
203 | #endif | |
204 | ||
059d3165 | 205 | /* |
206 | * Some platforms, notably Solaris, do not enforce password complexity | |
207 | * rules during pam_chauthtok() if the real uid of the calling process | |
208 | * is 0, on the assumption that it's being called by "passwd" run by root. | |
209 | * This wraps pam_chauthtok and sets/restore the real uid so PAM will do | |
210 | * the right thing. | |
211 | */ | |
212 | #ifdef SSHPAM_CHAUTHTOK_NEEDS_RUID | |
213 | static int | |
214 | sshpam_chauthtok_ruid(pam_handle_t *pamh, int flags) | |
215 | { | |
216 | int result; | |
217 | ||
218 | if (sshpam_authctxt == NULL) | |
219 | fatal("PAM: sshpam_authctxt not initialized"); | |
220 | if (setreuid(sshpam_authctxt->pw->pw_uid, -1) == -1) | |
221 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); | |
222 | result = pam_chauthtok(pamh, flags); | |
223 | if (setreuid(0, -1) == -1) | |
224 | fatal("%s: setreuid failed: %s", __func__, strerror(errno)); | |
225 | return result; | |
226 | } | |
227 | # define pam_chauthtok(a,b) (sshpam_chauthtok_ruid((a), (b))) | |
228 | #endif | |
229 | ||
b3ef7fb7 | 230 | void |
c10bb2ce | 231 | sshpam_password_change_required(int reqd) |
b3ef7fb7 | 232 | { |
90f3c272 | 233 | debug3("%s %d", __func__, reqd); |
d948154a | 234 | if (sshpam_authctxt == NULL) |
529d73ab | 235 | fatal("%s: PAM authctxt not initialized", __func__); |
d948154a | 236 | sshpam_authctxt->force_pwchange = reqd; |
b3ef7fb7 | 237 | if (reqd) { |
238 | no_port_forwarding_flag |= 2; | |
239 | no_agent_forwarding_flag |= 2; | |
240 | no_x11_forwarding_flag |= 2; | |
241 | } else { | |
242 | no_port_forwarding_flag &= ~2; | |
243 | no_agent_forwarding_flag &= ~2; | |
244 | no_x11_forwarding_flag &= ~2; | |
b3ef7fb7 | 245 | } |
246 | } | |
dd1fb864 | 247 | |
2212fc98 | 248 | /* Import regular and PAM environment from subprocess */ |
249 | static void | |
250 | import_environments(Buffer *b) | |
251 | { | |
252 | char *env; | |
253 | u_int i, num_env; | |
254 | int err; | |
255 | ||
90f3c272 | 256 | debug3("PAM: %s entering", __func__); |
257 | ||
a1e0095d | 258 | #ifndef USE_POSIX_THREADS |
b3ef7fb7 | 259 | /* Import variables set by do_pam_account */ |
260 | sshpam_account_status = buffer_get_int(b); | |
c10bb2ce | 261 | sshpam_password_change_required(buffer_get_int(b)); |
b3ef7fb7 | 262 | |
2212fc98 | 263 | /* Import environment from subprocess */ |
264 | num_env = buffer_get_int(b); | |
265 | sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env)); | |
266 | debug3("PAM: num env strings %d", num_env); | |
267 | for(i = 0; i < num_env; i++) | |
268 | sshpam_env[i] = buffer_get_string(b, NULL); | |
269 | ||
270 | sshpam_env[num_env] = NULL; | |
271 | ||
272 | /* Import PAM environment from subprocess */ | |
273 | num_env = buffer_get_int(b); | |
274 | debug("PAM: num PAM env strings %d", num_env); | |
275 | for(i = 0; i < num_env; i++) { | |
276 | env = buffer_get_string(b, NULL); | |
277 | ||
f79a6165 | 278 | #ifdef HAVE_PAM_PUTENV |
2212fc98 | 279 | /* Errors are not fatal here */ |
280 | if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { | |
281 | error("PAM: pam_putenv: %s", | |
282 | pam_strerror(sshpam_handle, sshpam_err)); | |
283 | } | |
f79a6165 | 284 | #endif |
2212fc98 | 285 | } |
a1e0095d | 286 | #endif |
2212fc98 | 287 | } |
288 | ||
ad55cd03 | 289 | /* |
05114c74 | 290 | * Conversation function for authentication thread. |
ad55cd03 | 291 | */ |
05114c74 | 292 | static int |
0943f13c | 293 | sshpam_thread_conv(int n, struct pam_message **msg, |
5b9e2464 | 294 | struct pam_response **resp, void *data) |
a5c9cd31 | 295 | { |
05114c74 | 296 | Buffer buffer; |
297 | struct pam_ctxt *ctxt; | |
0a23d79f | 298 | struct pam_response *reply; |
05114c74 | 299 | int i; |
300 | ||
60922169 | 301 | debug3("PAM: %s entering, %d messages", __func__, n); |
0a23d79f | 302 | *resp = NULL; |
303 | ||
d506e25f | 304 | if (data == NULL) { |
305 | error("PAM: conversation function passed a null context"); | |
306 | return (PAM_CONV_ERR); | |
307 | } | |
05114c74 | 308 | ctxt = data; |
309 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
310 | return (PAM_CONV_ERR); | |
0a23d79f | 311 | |
312 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
313 | return (PAM_CONV_ERR); | |
314 | memset(reply, 0, n * sizeof(*reply)); | |
315 | ||
05114c74 | 316 | buffer_init(&buffer); |
317 | for (i = 0; i < n; ++i) { | |
05114c74 | 318 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
319 | case PAM_PROMPT_ECHO_OFF: | |
aff51935 | 320 | buffer_put_cstring(&buffer, |
0a23d79f | 321 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 322 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 323 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
324 | goto fail; | |
aff51935 | 325 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
7b2a0de3 | 326 | goto fail; |
05114c74 | 327 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
328 | goto fail; | |
0a23d79f | 329 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 330 | break; |
331 | case PAM_PROMPT_ECHO_ON: | |
aff51935 | 332 | buffer_put_cstring(&buffer, |
0a23d79f | 333 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 334 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 335 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
336 | goto fail; | |
337 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
338 | goto fail; | |
05114c74 | 339 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
340 | goto fail; | |
0a23d79f | 341 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 342 | break; |
343 | case PAM_ERROR_MSG: | |
aff51935 | 344 | buffer_put_cstring(&buffer, |
0a23d79f | 345 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 346 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 347 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
348 | goto fail; | |
05114c74 | 349 | break; |
350 | case PAM_TEXT_INFO: | |
aff51935 | 351 | buffer_put_cstring(&buffer, |
0a23d79f | 352 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 353 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 354 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
355 | goto fail; | |
05114c74 | 356 | break; |
357 | default: | |
358 | goto fail; | |
a5c9cd31 | 359 | } |
05114c74 | 360 | buffer_clear(&buffer); |
a5c9cd31 | 361 | } |
05114c74 | 362 | buffer_free(&buffer); |
0a23d79f | 363 | *resp = reply; |
05114c74 | 364 | return (PAM_SUCCESS); |
0a23d79f | 365 | |
05114c74 | 366 | fail: |
0a23d79f | 367 | for(i = 0; i < n; i++) { |
368 | if (reply[i].resp != NULL) | |
369 | xfree(reply[i].resp); | |
370 | } | |
371 | xfree(reply); | |
05114c74 | 372 | buffer_free(&buffer); |
373 | return (PAM_CONV_ERR); | |
374 | } | |
a5c9cd31 | 375 | |
05114c74 | 376 | /* |
377 | * Authentication thread. | |
378 | */ | |
379 | static void * | |
380 | sshpam_thread(void *ctxtp) | |
381 | { | |
382 | struct pam_ctxt *ctxt = ctxtp; | |
383 | Buffer buffer; | |
c53917a9 | 384 | struct pam_conv sshpam_conv; |
e4472e7e | 385 | int flags = (options.permit_empty_passwd == 0 ? |
386 | PAM_DISALLOW_NULL_AUTHTOK : 0); | |
05114c74 | 387 | #ifndef USE_POSIX_THREADS |
2212fc98 | 388 | extern char **environ; |
389 | char **env_from_pam; | |
390 | u_int i; | |
05114c74 | 391 | const char *pam_user; |
392 | ||
0943f13c | 393 | pam_get_item(sshpam_handle, PAM_USER, (void **)&pam_user); |
2212fc98 | 394 | environ[0] = NULL; |
da97d54d | 395 | |
396 | if (sshpam_authctxt != NULL) { | |
397 | setproctitle("%s [pam]", | |
398 | sshpam_authctxt->valid ? pam_user : "unknown"); | |
399 | } | |
05114c74 | 400 | #endif |
a5c9cd31 | 401 | |
c53917a9 | 402 | sshpam_conv.conv = sshpam_thread_conv; |
403 | sshpam_conv.appdata_ptr = ctxt; | |
404 | ||
d948154a | 405 | if (sshpam_authctxt == NULL) |
529d73ab | 406 | fatal("%s: PAM authctxt not initialized", __func__); |
407 | ||
05114c74 | 408 | buffer_init(&buffer); |
409 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
410 | (const void *)&sshpam_conv); | |
411 | if (sshpam_err != PAM_SUCCESS) | |
412 | goto auth_fail; | |
e4472e7e | 413 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
05114c74 | 414 | if (sshpam_err != PAM_SUCCESS) |
415 | goto auth_fail; | |
b3ef7fb7 | 416 | |
2935db92 | 417 | if (compat20) { |
b3ef7fb7 | 418 | if (!do_pam_account()) |
419 | goto auth_fail; | |
d948154a | 420 | if (sshpam_authctxt->force_pwchange) { |
b3ef7fb7 | 421 | sshpam_err = pam_chauthtok(sshpam_handle, |
422 | PAM_CHANGE_EXPIRED_AUTHTOK); | |
423 | if (sshpam_err != PAM_SUCCESS) | |
424 | goto auth_fail; | |
c10bb2ce | 425 | sshpam_password_change_required(0); |
b3ef7fb7 | 426 | } |
2935db92 | 427 | } |
b3ef7fb7 | 428 | |
05114c74 | 429 | buffer_put_cstring(&buffer, "OK"); |
2212fc98 | 430 | |
431 | #ifndef USE_POSIX_THREADS | |
b3ef7fb7 | 432 | /* Export variables set by do_pam_account */ |
433 | buffer_put_int(&buffer, sshpam_account_status); | |
d948154a | 434 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
b3ef7fb7 | 435 | |
2212fc98 | 436 | /* Export any environment strings set in child */ |
437 | for(i = 0; environ[i] != NULL; i++) | |
438 | ; /* Count */ | |
439 | buffer_put_int(&buffer, i); | |
440 | for(i = 0; environ[i] != NULL; i++) | |
441 | buffer_put_cstring(&buffer, environ[i]); | |
442 | ||
443 | /* Export any environment strings set by PAM in child */ | |
444 | env_from_pam = pam_getenvlist(sshpam_handle); | |
445 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
446 | ; /* Count */ | |
447 | buffer_put_int(&buffer, i); | |
448 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
449 | buffer_put_cstring(&buffer, env_from_pam[i]); | |
450 | #endif /* USE_POSIX_THREADS */ | |
451 | ||
7b2a0de3 | 452 | /* XXX - can't do much about an error here */ |
05114c74 | 453 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
454 | buffer_free(&buffer); | |
455 | pthread_exit(NULL); | |
456 | ||
457 | auth_fail: | |
458 | buffer_put_cstring(&buffer, | |
459 | pam_strerror(sshpam_handle, sshpam_err)); | |
7b2a0de3 | 460 | /* XXX - can't do much about an error here */ |
05114c74 | 461 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
462 | buffer_free(&buffer); | |
463 | pthread_exit(NULL); | |
b6453d99 | 464 | |
05114c74 | 465 | return (NULL); /* Avoid warning for non-pthread case */ |
a5c9cd31 | 466 | } |
467 | ||
c6630044 | 468 | void |
469 | sshpam_thread_cleanup(void) | |
a5c9cd31 | 470 | { |
c6630044 | 471 | struct pam_ctxt *ctxt = cleanup_ctxt; |
472 | ||
90f3c272 | 473 | debug3("PAM: %s entering", __func__); |
c6630044 | 474 | if (ctxt != NULL && ctxt->pam_thread != 0) { |
475 | pthread_cancel(ctxt->pam_thread); | |
476 | pthread_join(ctxt->pam_thread, NULL); | |
477 | close(ctxt->pam_psock); | |
478 | close(ctxt->pam_csock); | |
479 | memset(ctxt, 0, sizeof(*ctxt)); | |
480 | cleanup_ctxt = NULL; | |
481 | } | |
05114c74 | 482 | } |
a5c9cd31 | 483 | |
05114c74 | 484 | static int |
0943f13c | 485 | sshpam_null_conv(int n, struct pam_message **msg, |
5b9e2464 | 486 | struct pam_response **resp, void *data) |
05114c74 | 487 | { |
60922169 | 488 | debug3("PAM: %s entering, %d messages", __func__, n); |
05114c74 | 489 | return (PAM_CONV_ERR); |
490 | } | |
a5c9cd31 | 491 | |
05114c74 | 492 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
493 | ||
c6630044 | 494 | void |
495 | sshpam_cleanup(void) | |
05114c74 | 496 | { |
05114c74 | 497 | debug("PAM: cleanup"); |
0a23d79f | 498 | if (sshpam_handle == NULL) |
499 | return; | |
05114c74 | 500 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
501 | if (sshpam_cred_established) { | |
502 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
503 | sshpam_cred_established = 0; | |
504 | } | |
505 | if (sshpam_session_open) { | |
506 | pam_close_session(sshpam_handle, PAM_SILENT); | |
507 | sshpam_session_open = 0; | |
a5c9cd31 | 508 | } |
dd1fb864 | 509 | sshpam_authenticated = 0; |
05114c74 | 510 | pam_end(sshpam_handle, sshpam_err); |
511 | sshpam_handle = NULL; | |
a5c9cd31 | 512 | } |
513 | ||
05114c74 | 514 | static int |
529d73ab | 515 | sshpam_init(Authctxt *authctxt) |
a5c9cd31 | 516 | { |
74678fb9 | 517 | extern char *__progname; |
529d73ab | 518 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
05114c74 | 519 | |
520 | if (sshpam_handle != NULL) { | |
521 | /* We already have a PAM context; check if the user matches */ | |
522 | sshpam_err = pam_get_item(sshpam_handle, | |
0943f13c | 523 | PAM_USER, (void **)&pam_user); |
05114c74 | 524 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) |
525 | return (0); | |
05114c74 | 526 | pam_end(sshpam_handle, sshpam_err); |
527 | sshpam_handle = NULL; | |
528 | } | |
529 | debug("PAM: initializing for \"%s\"", user); | |
01224183 | 530 | sshpam_err = |
6d51251f | 531 | pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle); |
d948154a | 532 | sshpam_authctxt = authctxt; |
529d73ab | 533 | |
5ff453c0 | 534 | if (sshpam_err != PAM_SUCCESS) { |
535 | pam_end(sshpam_handle, sshpam_err); | |
536 | sshpam_handle = NULL; | |
05114c74 | 537 | return (-1); |
5ff453c0 | 538 | } |
c5a7d788 | 539 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
05d62329 | 540 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
05114c74 | 541 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
5ff453c0 | 542 | if (sshpam_err != PAM_SUCCESS) { |
5b9e2464 | 543 | pam_end(sshpam_handle, sshpam_err); |
5ff453c0 | 544 | sshpam_handle = NULL; |
545 | return (-1); | |
546 | } | |
547 | #ifdef PAM_TTY_KLUDGE | |
aff51935 | 548 | /* |
549 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
550 | * sshd doesn't set the tty until too late in the auth process and | |
5ff453c0 | 551 | * may not even set one (for tty-less connections) |
aff51935 | 552 | */ |
5ff453c0 | 553 | debug("PAM: setting PAM_TTY to \"ssh\""); |
554 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
05114c74 | 555 | if (sshpam_err != PAM_SUCCESS) { |
556 | pam_end(sshpam_handle, sshpam_err); | |
557 | sshpam_handle = NULL; | |
558 | return (-1); | |
a5c9cd31 | 559 | } |
5ff453c0 | 560 | #endif |
05114c74 | 561 | return (0); |
a5c9cd31 | 562 | } |
563 | ||
05114c74 | 564 | static void * |
565 | sshpam_init_ctx(Authctxt *authctxt) | |
a5c9cd31 | 566 | { |
05114c74 | 567 | struct pam_ctxt *ctxt; |
568 | int socks[2]; | |
2b87da3b | 569 | |
90f3c272 | 570 | debug3("PAM: %s entering", __func__); |
7fceb20d | 571 | /* Refuse to start if we don't have PAM enabled */ |
572 | if (!options.use_pam) | |
573 | return NULL; | |
574 | ||
05114c74 | 575 | /* Initialize PAM */ |
529d73ab | 576 | if (sshpam_init(authctxt) == -1) { |
05114c74 | 577 | error("PAM: initialization failed"); |
578 | return (NULL); | |
579 | } | |
5c377b3b | 580 | |
05114c74 | 581 | ctxt = xmalloc(sizeof *ctxt); |
c6630044 | 582 | memset(ctxt, 0, sizeof(*ctxt)); |
05114c74 | 583 | |
584 | /* Start the authentication thread */ | |
585 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
586 | error("PAM: failed create sockets: %s", strerror(errno)); | |
587 | xfree(ctxt); | |
588 | return (NULL); | |
589 | } | |
590 | ctxt->pam_psock = socks[0]; | |
591 | ctxt->pam_csock = socks[1]; | |
592 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
593 | error("PAM: failed to start authentication thread: %s", | |
594 | strerror(errno)); | |
595 | close(socks[0]); | |
596 | close(socks[1]); | |
597 | xfree(ctxt); | |
598 | return (NULL); | |
a5c9cd31 | 599 | } |
c6630044 | 600 | cleanup_ctxt = ctxt; |
05114c74 | 601 | return (ctxt); |
602 | } | |
a5c9cd31 | 603 | |
05114c74 | 604 | static int |
605 | sshpam_query(void *ctx, char **name, char **info, | |
606 | u_int *num, char ***prompts, u_int **echo_on) | |
607 | { | |
608 | Buffer buffer; | |
609 | struct pam_ctxt *ctxt = ctx; | |
610 | size_t plen; | |
611 | u_char type; | |
612 | char *msg; | |
6d51251f | 613 | size_t len; |
05114c74 | 614 | |
90f3c272 | 615 | debug3("PAM: %s entering", __func__); |
05114c74 | 616 | buffer_init(&buffer); |
617 | *name = xstrdup(""); | |
618 | *info = xstrdup(""); | |
619 | *prompts = xmalloc(sizeof(char *)); | |
620 | **prompts = NULL; | |
621 | plen = 0; | |
622 | *echo_on = xmalloc(sizeof(u_int)); | |
623 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
624 | type = buffer_get_char(&buffer); | |
625 | msg = buffer_get_string(&buffer, NULL); | |
626 | switch (type) { | |
627 | case PAM_PROMPT_ECHO_ON: | |
628 | case PAM_PROMPT_ECHO_OFF: | |
629 | *num = 1; | |
6d51251f | 630 | len = plen + strlen(msg) + 1; |
cbdeccf3 | 631 | **prompts = xrealloc(**prompts, len); |
6d51251f | 632 | plen += snprintf(**prompts + plen, len, "%s", msg); |
05114c74 | 633 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
634 | xfree(msg); | |
635 | return (0); | |
636 | case PAM_ERROR_MSG: | |
637 | case PAM_TEXT_INFO: | |
638 | /* accumulate messages */ | |
6d51251f | 639 | len = plen + strlen(msg) + 2; |
cbdeccf3 | 640 | **prompts = xrealloc(**prompts, len); |
6d51251f | 641 | plen += snprintf(**prompts + plen, len, "%s\n", msg); |
05114c74 | 642 | xfree(msg); |
5daf7064 | 643 | break; |
05114c74 | 644 | case PAM_SUCCESS: |
645 | case PAM_AUTH_ERR: | |
646 | if (**prompts != NULL) { | |
647 | /* drain any accumulated messages */ | |
3eaf3960 | 648 | debug("PAM: %s", **prompts); |
649 | buffer_append(&loginmsg, **prompts, | |
650 | strlen(**prompts)); | |
05114c74 | 651 | xfree(**prompts); |
652 | **prompts = NULL; | |
653 | } | |
654 | if (type == PAM_SUCCESS) { | |
6d51251f | 655 | if (!sshpam_authctxt->valid || |
656 | (sshpam_authctxt->pw->pw_uid == 0 && | |
657 | options.permit_root_login != PERMIT_YES)) | |
658 | fatal("Internal error: PAM auth " | |
659 | "succeeded when it should have " | |
660 | "failed"); | |
2212fc98 | 661 | import_environments(&buffer); |
05114c74 | 662 | *num = 0; |
663 | **echo_on = 0; | |
664 | ctxt->pam_done = 1; | |
665 | xfree(msg); | |
666 | return (0); | |
667 | } | |
1e08e787 | 668 | error("PAM: %s for %s%.100s from %.100s", msg, |
669 | sshpam_authctxt->valid ? "" : "illegal user ", | |
670 | sshpam_authctxt->user, | |
671 | get_remote_name_or_ip(utmp_len, options.use_dns)); | |
e250770b | 672 | /* FALLTHROUGH */ |
5daf7064 | 673 | default: |
05114c74 | 674 | *num = 0; |
675 | **echo_on = 0; | |
676 | xfree(msg); | |
677 | ctxt->pam_done = -1; | |
678 | return (-1); | |
679 | } | |
a5c9cd31 | 680 | } |
05114c74 | 681 | return (-1); |
a5c9cd31 | 682 | } |
683 | ||
05114c74 | 684 | /* XXX - see also comment in auth-chall.c:verify_response */ |
685 | static int | |
686 | sshpam_respond(void *ctx, u_int num, char **resp) | |
a5c9cd31 | 687 | { |
05114c74 | 688 | Buffer buffer; |
689 | struct pam_ctxt *ctxt = ctx; | |
690 | ||
90f3c272 | 691 | debug2("PAM: %s entering, %d responses", __func__, num); |
05114c74 | 692 | switch (ctxt->pam_done) { |
693 | case 1: | |
694 | sshpam_authenticated = 1; | |
695 | return (0); | |
696 | case 0: | |
697 | break; | |
698 | default: | |
699 | return (-1); | |
a5c9cd31 | 700 | } |
05114c74 | 701 | if (num != 1) { |
702 | error("PAM: expected one response, got %u", num); | |
703 | return (-1); | |
704 | } | |
705 | buffer_init(&buffer); | |
6d51251f | 706 | if (sshpam_authctxt->valid && |
707 | (sshpam_authctxt->pw->pw_uid != 0 || | |
708 | options.permit_root_login == PERMIT_YES)) | |
709 | buffer_put_cstring(&buffer, *resp); | |
710 | else | |
711 | buffer_put_cstring(&buffer, badpw); | |
7b2a0de3 | 712 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
713 | buffer_free(&buffer); | |
714 | return (-1); | |
715 | } | |
05114c74 | 716 | buffer_free(&buffer); |
717 | return (1); | |
a5c9cd31 | 718 | } |
719 | ||
05114c74 | 720 | static void |
721 | sshpam_free_ctx(void *ctxtp) | |
a5c9cd31 | 722 | { |
05114c74 | 723 | struct pam_ctxt *ctxt = ctxtp; |
39ce53de | 724 | |
90f3c272 | 725 | debug3("PAM: %s entering", __func__); |
c6630044 | 726 | sshpam_thread_cleanup(); |
05114c74 | 727 | xfree(ctxt); |
728 | /* | |
729 | * We don't call sshpam_cleanup() here because we may need the PAM | |
730 | * handle at a later stage, e.g. when setting up a session. It's | |
731 | * still on the cleanup list, so pam_end() *will* be called before | |
732 | * the server process terminates. | |
733 | */ | |
ad55cd03 | 734 | } |
735 | ||
05114c74 | 736 | KbdintDevice sshpam_device = { |
737 | "pam", | |
738 | sshpam_init_ctx, | |
739 | sshpam_query, | |
740 | sshpam_respond, | |
741 | sshpam_free_ctx | |
742 | }; | |
743 | ||
744 | KbdintDevice mm_sshpam_device = { | |
745 | "pam", | |
746 | mm_sshpam_init_ctx, | |
747 | mm_sshpam_query, | |
748 | mm_sshpam_respond, | |
749 | mm_sshpam_free_ctx | |
750 | }; | |
2919e060 | 751 | |
2b87da3b | 752 | /* |
05114c74 | 753 | * This replaces auth-pam.c |
ad55cd03 | 754 | */ |
05114c74 | 755 | void |
529d73ab | 756 | start_pam(Authctxt *authctxt) |
ad55cd03 | 757 | { |
817e6d38 | 758 | if (!options.use_pam) |
759 | fatal("PAM: initialisation requested when UsePAM=no"); | |
760 | ||
529d73ab | 761 | if (sshpam_init(authctxt) == -1) |
05114c74 | 762 | fatal("PAM: initialisation failed"); |
a5c9cd31 | 763 | } |
764 | ||
05114c74 | 765 | void |
766 | finish_pam(void) | |
a5c9cd31 | 767 | { |
c6630044 | 768 | sshpam_cleanup(); |
a5c9cd31 | 769 | } |
770 | ||
5b9e2464 | 771 | u_int |
772 | do_pam_account(void) | |
a5c9cd31 | 773 | { |
b3ef7fb7 | 774 | if (sshpam_account_status != -1) |
775 | return (sshpam_account_status); | |
776 | ||
5b9e2464 | 777 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
6d51251f | 778 | debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err); |
b3ef7fb7 | 779 | |
780 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { | |
781 | sshpam_account_status = 0; | |
782 | return (sshpam_account_status); | |
5b9e2464 | 783 | } |
784 | ||
b3ef7fb7 | 785 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
c10bb2ce | 786 | sshpam_password_change_required(1); |
b3ef7fb7 | 787 | |
788 | sshpam_account_status = 1; | |
789 | return (sshpam_account_status); | |
05114c74 | 790 | } |
46c76c63 | 791 | |
49e82bb9 | 792 | void |
793 | do_pam_set_tty(const char *tty) | |
794 | { | |
26b3608b | 795 | if (tty != NULL) { |
796 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
797 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
798 | if (sshpam_err != PAM_SUCCESS) | |
799 | fatal("PAM: failed to set PAM_TTY: %s", | |
800 | pam_strerror(sshpam_handle, sshpam_err)); | |
801 | } | |
05114c74 | 802 | } |
cbd7492e | 803 | |
05114c74 | 804 | void |
805 | do_pam_setcred(int init) | |
806 | { | |
807 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
6d51251f | 808 | (const void *)&null_conv); |
05114c74 | 809 | if (sshpam_err != PAM_SUCCESS) |
810 | fatal("PAM: failed to set PAM_CONV: %s", | |
811 | pam_strerror(sshpam_handle, sshpam_err)); | |
812 | if (init) { | |
813 | debug("PAM: establishing credentials"); | |
814 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
815 | } else { | |
816 | debug("PAM: reinitializing credentials"); | |
817 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
818 | } | |
819 | if (sshpam_err == PAM_SUCCESS) { | |
820 | sshpam_cred_established = 1; | |
821 | return; | |
822 | } | |
823 | if (sshpam_authenticated) | |
824 | fatal("PAM: pam_setcred(): %s", | |
825 | pam_strerror(sshpam_handle, sshpam_err)); | |
826 | else | |
827 | debug("PAM: pam_setcred(): %s", | |
828 | pam_strerror(sshpam_handle, sshpam_err)); | |
a5c9cd31 | 829 | } |
830 | ||
05114c74 | 831 | static int |
0943f13c | 832 | sshpam_tty_conv(int n, struct pam_message **msg, |
5b9e2464 | 833 | struct pam_response **resp, void *data) |
ee48c949 | 834 | { |
05114c74 | 835 | char input[PAM_MAX_MSG_SIZE]; |
0a23d79f | 836 | struct pam_response *reply; |
ee48c949 | 837 | int i; |
838 | ||
60922169 | 839 | debug3("PAM: %s called with %d messages", __func__, n); |
840 | ||
0a23d79f | 841 | *resp = NULL; |
842 | ||
3eaf3960 | 843 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
05114c74 | 844 | return (PAM_CONV_ERR); |
0a23d79f | 845 | |
846 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
847 | return (PAM_CONV_ERR); | |
848 | memset(reply, 0, n * sizeof(*reply)); | |
849 | ||
05114c74 | 850 | for (i = 0; i < n; ++i) { |
851 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
852 | case PAM_PROMPT_ECHO_OFF: | |
0a23d79f | 853 | reply[i].resp = |
aff51935 | 854 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
05114c74 | 855 | RP_ALLOW_STDIN); |
0a23d79f | 856 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 857 | break; |
858 | case PAM_PROMPT_ECHO_ON: | |
74117b26 | 859 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
05114c74 | 860 | fgets(input, sizeof input, stdin); |
8936b151 | 861 | if ((reply[i].resp = strdup(input)) == NULL) |
862 | goto fail; | |
0a23d79f | 863 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 864 | break; |
865 | case PAM_ERROR_MSG: | |
866 | case PAM_TEXT_INFO: | |
74117b26 | 867 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
0a23d79f | 868 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 869 | break; |
870 | default: | |
871 | goto fail; | |
872 | } | |
ee48c949 | 873 | } |
0a23d79f | 874 | *resp = reply; |
05114c74 | 875 | return (PAM_SUCCESS); |
0a23d79f | 876 | |
05114c74 | 877 | fail: |
0a23d79f | 878 | for(i = 0; i < n; i++) { |
879 | if (reply[i].resp != NULL) | |
880 | xfree(reply[i].resp); | |
881 | } | |
882 | xfree(reply); | |
05114c74 | 883 | return (PAM_CONV_ERR); |
ee48c949 | 884 | } |
885 | ||
8600a4ab | 886 | static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; |
3eaf3960 | 887 | |
05114c74 | 888 | /* |
889 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
890 | * support that | |
891 | */ | |
892 | void | |
893 | do_pam_chauthtok(void) | |
a5c9cd31 | 894 | { |
05114c74 | 895 | if (use_privsep) |
5b9e2464 | 896 | fatal("Password expired (unable to change with privsep)"); |
05114c74 | 897 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
3eaf3960 | 898 | (const void *)&tty_conv); |
05114c74 | 899 | if (sshpam_err != PAM_SUCCESS) |
900 | fatal("PAM: failed to set PAM_CONV: %s", | |
901 | pam_strerror(sshpam_handle, sshpam_err)); | |
902 | debug("PAM: changing password"); | |
903 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
904 | if (sshpam_err != PAM_SUCCESS) | |
905 | fatal("PAM: pam_chauthtok(): %s", | |
906 | pam_strerror(sshpam_handle, sshpam_err)); | |
5daf7064 | 907 | } |
908 | ||
6d51251f | 909 | static int |
910 | sshpam_store_conv(int n, struct pam_message **msg, | |
911 | struct pam_response **resp, void *data) | |
912 | { | |
913 | struct pam_response *reply; | |
914 | int i; | |
915 | size_t len; | |
916 | ||
917 | debug3("PAM: %s called with %d messages", __func__, n); | |
918 | *resp = NULL; | |
919 | ||
920 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
921 | return (PAM_CONV_ERR); | |
922 | ||
923 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
924 | return (PAM_CONV_ERR); | |
925 | memset(reply, 0, n * sizeof(*reply)); | |
926 | ||
927 | for (i = 0; i < n; ++i) { | |
928 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
929 | case PAM_ERROR_MSG: | |
930 | case PAM_TEXT_INFO: | |
931 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
932 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); | |
933 | buffer_append(&loginmsg, "\n", 1 ); | |
934 | reply[i].resp_retcode = PAM_SUCCESS; | |
935 | break; | |
936 | default: | |
937 | goto fail; | |
938 | } | |
939 | } | |
940 | *resp = reply; | |
941 | return (PAM_SUCCESS); | |
942 | ||
943 | fail: | |
944 | for(i = 0; i < n; i++) { | |
945 | if (reply[i].resp != NULL) | |
946 | xfree(reply[i].resp); | |
947 | } | |
948 | xfree(reply); | |
949 | return (PAM_CONV_ERR); | |
950 | } | |
951 | ||
952 | static struct pam_conv store_conv = { sshpam_store_conv, NULL }; | |
953 | ||
3eaf3960 | 954 | void |
955 | do_pam_session(void) | |
956 | { | |
dd1fb864 | 957 | debug3("PAM: opening session"); |
aff51935 | 958 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
ef687c66 | 959 | (const void *)&store_conv); |
3eaf3960 | 960 | if (sshpam_err != PAM_SUCCESS) |
961 | fatal("PAM: failed to set PAM_CONV: %s", | |
962 | pam_strerror(sshpam_handle, sshpam_err)); | |
963 | sshpam_err = pam_open_session(sshpam_handle, 0); | |
6d51251f | 964 | if (sshpam_err != PAM_SUCCESS) |
965 | fatal("PAM: pam_open_session(): %s", | |
3eaf3960 | 966 | pam_strerror(sshpam_handle, sshpam_err)); |
6d51251f | 967 | sshpam_session_open = 1; |
3eaf3960 | 968 | } |
969 | ||
aff51935 | 970 | /* |
749560dd | 971 | * Set a PAM environment string. We need to do this so that the session |
972 | * modules can handle things like Kerberos/GSI credentials that appear | |
973 | * during the ssh authentication process. | |
974 | */ | |
749560dd | 975 | int |
aff51935 | 976 | do_pam_putenv(char *name, char *value) |
749560dd | 977 | { |
749560dd | 978 | int ret = 1; |
b6453d99 | 979 | #ifdef HAVE_PAM_PUTENV |
263c65df | 980 | char *compound; |
981 | size_t len; | |
982 | ||
983 | len = strlen(name) + strlen(value) + 2; | |
984 | compound = xmalloc(len); | |
985 | ||
986 | snprintf(compound, len, "%s=%s", name, value); | |
987 | ret = pam_putenv(sshpam_handle, compound); | |
988 | xfree(compound); | |
749560dd | 989 | #endif |
263c65df | 990 | |
749560dd | 991 | return (ret); |
992 | } | |
993 | ||
2212fc98 | 994 | char ** |
995 | fetch_pam_child_environment(void) | |
996 | { | |
997 | return sshpam_env; | |
998 | } | |
999 | ||
05114c74 | 1000 | char ** |
1001 | fetch_pam_environment(void) | |
1002 | { | |
05114c74 | 1003 | return (pam_getenvlist(sshpam_handle)); |
05114c74 | 1004 | } |
5c377b3b | 1005 | |
05114c74 | 1006 | void |
1007 | free_pam_environment(char **env) | |
1008 | { | |
1009 | char **envp; | |
5daf7064 | 1010 | |
0eb6370a | 1011 | if (env == NULL) |
1012 | return; | |
1013 | ||
05114c74 | 1014 | for (envp = env; *envp; envp++) |
1015 | xfree(*envp); | |
1016 | xfree(env); | |
a5c9cd31 | 1017 | } |
1018 | ||
5de92f17 | 1019 | /* |
1020 | * "Blind" conversation function for password authentication. Assumes that | |
1021 | * echo-off prompts are for the password and stores messages for later | |
1022 | * display. | |
1023 | */ | |
1024 | static int | |
0943f13c | 1025 | sshpam_passwd_conv(int n, struct pam_message **msg, |
5de92f17 | 1026 | struct pam_response **resp, void *data) |
1027 | { | |
1028 | struct pam_response *reply; | |
1029 | int i; | |
1030 | size_t len; | |
1031 | ||
1032 | debug3("PAM: %s called with %d messages", __func__, n); | |
1033 | ||
1034 | *resp = NULL; | |
1035 | ||
1036 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
1037 | return (PAM_CONV_ERR); | |
1038 | ||
1039 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
1040 | return (PAM_CONV_ERR); | |
1041 | memset(reply, 0, n * sizeof(*reply)); | |
1042 | ||
1043 | for (i = 0; i < n; ++i) { | |
1044 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
1045 | case PAM_PROMPT_ECHO_OFF: | |
1046 | if (sshpam_password == NULL) | |
1047 | goto fail; | |
8936b151 | 1048 | if ((reply[i].resp = strdup(sshpam_password)) == NULL) |
1049 | goto fail; | |
5de92f17 | 1050 | reply[i].resp_retcode = PAM_SUCCESS; |
1051 | break; | |
1052 | case PAM_ERROR_MSG: | |
1053 | case PAM_TEXT_INFO: | |
1054 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
1055 | if (len > 0) { | |
1056 | buffer_append(&loginmsg, | |
1057 | PAM_MSG_MEMBER(msg, i, msg), len); | |
1058 | buffer_append(&loginmsg, "\n", 1); | |
1059 | } | |
8936b151 | 1060 | if ((reply[i].resp = strdup("")) == NULL) |
1061 | goto fail; | |
5de92f17 | 1062 | reply[i].resp_retcode = PAM_SUCCESS; |
1063 | break; | |
1064 | default: | |
1065 | goto fail; | |
1066 | } | |
1067 | } | |
1068 | *resp = reply; | |
1069 | return (PAM_SUCCESS); | |
1070 | ||
1071 | fail: | |
1072 | for(i = 0; i < n; i++) { | |
1073 | if (reply[i].resp != NULL) | |
1074 | xfree(reply[i].resp); | |
1075 | } | |
1076 | xfree(reply); | |
1077 | return (PAM_CONV_ERR); | |
1078 | } | |
1079 | ||
1080 | static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; | |
1081 | ||
1082 | /* | |
1083 | * Attempt password authentication via PAM | |
1084 | */ | |
1085 | int | |
1086 | sshpam_auth_passwd(Authctxt *authctxt, const char *password) | |
1087 | { | |
1088 | int flags = (options.permit_empty_passwd == 0 ? | |
1089 | PAM_DISALLOW_NULL_AUTHTOK : 0); | |
1090 | ||
1091 | if (!options.use_pam || sshpam_handle == NULL) | |
1092 | fatal("PAM: %s called when PAM disabled or failed to " | |
1093 | "initialise.", __func__); | |
1094 | ||
1095 | sshpam_password = password; | |
1096 | sshpam_authctxt = authctxt; | |
1097 | ||
0e716148 | 1098 | /* |
1099 | * If the user logging in is invalid, or is root but is not permitted | |
1100 | * by PermitRootLogin, use an invalid password to prevent leaking | |
1101 | * information via timing (eg if the PAM config has a delay on fail). | |
1102 | */ | |
1103 | if (!authctxt->valid || (authctxt->pw->pw_uid == 0 && | |
1104 | options.permit_root_login != PERMIT_YES)) | |
1105 | sshpam_password = badpw; | |
1106 | ||
5de92f17 | 1107 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
1108 | (const void *)&passwd_conv); | |
1109 | if (sshpam_err != PAM_SUCCESS) | |
1110 | fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, | |
1111 | pam_strerror(sshpam_handle, sshpam_err)); | |
1112 | ||
1113 | sshpam_err = pam_authenticate(sshpam_handle, flags); | |
1114 | sshpam_password = NULL; | |
1115 | if (sshpam_err == PAM_SUCCESS && authctxt->valid) { | |
1116 | debug("PAM: password authentication accepted for %.100s", | |
1117 | authctxt->user); | |
1118 | return 1; | |
1119 | } else { | |
1120 | debug("PAM: password authentication failed for %.100s: %s", | |
1121 | authctxt->valid ? authctxt->user : "an illegal user", | |
1122 | pam_strerror(sshpam_handle, sshpam_err)); | |
1123 | return 0; | |
1124 | } | |
1125 | } | |
a5c9cd31 | 1126 | #endif /* USE_PAM */ |