]>
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; |
af0e5c2f | 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 | |
5d33c697 | 248 | /* Check ssh internal flags in addition to PAM */ |
249 | ||
250 | static int | |
251 | sshpam_login_allowed(Authctxt *ctxt) | |
252 | { | |
253 | if (ctxt->valid && (ctxt->pw->pw_uid != 0 || | |
254 | options.permit_root_login == PERMIT_YES)) | |
255 | return 1; | |
256 | return 0; | |
257 | } | |
258 | ||
2212fc98 | 259 | /* Import regular and PAM environment from subprocess */ |
260 | static void | |
261 | import_environments(Buffer *b) | |
262 | { | |
263 | char *env; | |
264 | u_int i, num_env; | |
265 | int err; | |
266 | ||
90f3c272 | 267 | debug3("PAM: %s entering", __func__); |
268 | ||
a1e0095d | 269 | #ifndef USE_POSIX_THREADS |
b3ef7fb7 | 270 | /* Import variables set by do_pam_account */ |
271 | sshpam_account_status = buffer_get_int(b); | |
c10bb2ce | 272 | sshpam_password_change_required(buffer_get_int(b)); |
b3ef7fb7 | 273 | |
2212fc98 | 274 | /* Import environment from subprocess */ |
275 | num_env = buffer_get_int(b); | |
276 | sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env)); | |
277 | debug3("PAM: num env strings %d", num_env); | |
278 | for(i = 0; i < num_env; i++) | |
279 | sshpam_env[i] = buffer_get_string(b, NULL); | |
280 | ||
281 | sshpam_env[num_env] = NULL; | |
282 | ||
283 | /* Import PAM environment from subprocess */ | |
284 | num_env = buffer_get_int(b); | |
285 | debug("PAM: num PAM env strings %d", num_env); | |
286 | for(i = 0; i < num_env; i++) { | |
287 | env = buffer_get_string(b, NULL); | |
288 | ||
f79a6165 | 289 | #ifdef HAVE_PAM_PUTENV |
2212fc98 | 290 | /* Errors are not fatal here */ |
291 | if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { | |
292 | error("PAM: pam_putenv: %s", | |
293 | pam_strerror(sshpam_handle, sshpam_err)); | |
294 | } | |
f79a6165 | 295 | #endif |
2212fc98 | 296 | } |
a1e0095d | 297 | #endif |
2212fc98 | 298 | } |
299 | ||
ad55cd03 | 300 | /* |
05114c74 | 301 | * Conversation function for authentication thread. |
ad55cd03 | 302 | */ |
05114c74 | 303 | static int |
0943f13c | 304 | sshpam_thread_conv(int n, struct pam_message **msg, |
5b9e2464 | 305 | struct pam_response **resp, void *data) |
a5c9cd31 | 306 | { |
05114c74 | 307 | Buffer buffer; |
308 | struct pam_ctxt *ctxt; | |
0a23d79f | 309 | struct pam_response *reply; |
05114c74 | 310 | int i; |
311 | ||
60922169 | 312 | debug3("PAM: %s entering, %d messages", __func__, n); |
0a23d79f | 313 | *resp = NULL; |
314 | ||
d506e25f | 315 | if (data == NULL) { |
316 | error("PAM: conversation function passed a null context"); | |
317 | return (PAM_CONV_ERR); | |
318 | } | |
05114c74 | 319 | ctxt = data; |
320 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
321 | return (PAM_CONV_ERR); | |
0a23d79f | 322 | |
323 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
324 | return (PAM_CONV_ERR); | |
325 | memset(reply, 0, n * sizeof(*reply)); | |
326 | ||
05114c74 | 327 | buffer_init(&buffer); |
328 | for (i = 0; i < n; ++i) { | |
05114c74 | 329 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
330 | case PAM_PROMPT_ECHO_OFF: | |
aff51935 | 331 | buffer_put_cstring(&buffer, |
0a23d79f | 332 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 333 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 334 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
335 | goto fail; | |
aff51935 | 336 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
7b2a0de3 | 337 | goto fail; |
05114c74 | 338 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
339 | goto fail; | |
0a23d79f | 340 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 341 | break; |
342 | case PAM_PROMPT_ECHO_ON: | |
aff51935 | 343 | buffer_put_cstring(&buffer, |
0a23d79f | 344 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 345 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 346 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
347 | goto fail; | |
348 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
349 | goto fail; | |
05114c74 | 350 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
351 | goto fail; | |
0a23d79f | 352 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 353 | break; |
354 | case PAM_ERROR_MSG: | |
aff51935 | 355 | buffer_put_cstring(&buffer, |
0a23d79f | 356 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 357 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 358 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
359 | goto fail; | |
05114c74 | 360 | break; |
361 | case PAM_TEXT_INFO: | |
aff51935 | 362 | buffer_put_cstring(&buffer, |
0a23d79f | 363 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 364 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 365 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
366 | goto fail; | |
05114c74 | 367 | break; |
368 | default: | |
369 | goto fail; | |
a5c9cd31 | 370 | } |
05114c74 | 371 | buffer_clear(&buffer); |
a5c9cd31 | 372 | } |
05114c74 | 373 | buffer_free(&buffer); |
0a23d79f | 374 | *resp = reply; |
05114c74 | 375 | return (PAM_SUCCESS); |
0a23d79f | 376 | |
05114c74 | 377 | fail: |
0a23d79f | 378 | for(i = 0; i < n; i++) { |
379 | if (reply[i].resp != NULL) | |
380 | xfree(reply[i].resp); | |
381 | } | |
382 | xfree(reply); | |
05114c74 | 383 | buffer_free(&buffer); |
384 | return (PAM_CONV_ERR); | |
385 | } | |
a5c9cd31 | 386 | |
05114c74 | 387 | /* |
388 | * Authentication thread. | |
389 | */ | |
390 | static void * | |
391 | sshpam_thread(void *ctxtp) | |
392 | { | |
393 | struct pam_ctxt *ctxt = ctxtp; | |
394 | Buffer buffer; | |
c53917a9 | 395 | struct pam_conv sshpam_conv; |
e4472e7e | 396 | int flags = (options.permit_empty_passwd == 0 ? |
397 | PAM_DISALLOW_NULL_AUTHTOK : 0); | |
05114c74 | 398 | #ifndef USE_POSIX_THREADS |
2212fc98 | 399 | extern char **environ; |
400 | char **env_from_pam; | |
401 | u_int i; | |
05114c74 | 402 | const char *pam_user; |
403 | ||
0943f13c | 404 | pam_get_item(sshpam_handle, PAM_USER, (void **)&pam_user); |
2212fc98 | 405 | environ[0] = NULL; |
da97d54d | 406 | |
407 | if (sshpam_authctxt != NULL) { | |
408 | setproctitle("%s [pam]", | |
409 | sshpam_authctxt->valid ? pam_user : "unknown"); | |
410 | } | |
05114c74 | 411 | #endif |
a5c9cd31 | 412 | |
c53917a9 | 413 | sshpam_conv.conv = sshpam_thread_conv; |
414 | sshpam_conv.appdata_ptr = ctxt; | |
415 | ||
d948154a | 416 | if (sshpam_authctxt == NULL) |
529d73ab | 417 | fatal("%s: PAM authctxt not initialized", __func__); |
418 | ||
05114c74 | 419 | buffer_init(&buffer); |
420 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
421 | (const void *)&sshpam_conv); | |
422 | if (sshpam_err != PAM_SUCCESS) | |
423 | goto auth_fail; | |
e4472e7e | 424 | sshpam_err = pam_authenticate(sshpam_handle, flags); |
05114c74 | 425 | if (sshpam_err != PAM_SUCCESS) |
426 | goto auth_fail; | |
b3ef7fb7 | 427 | |
2935db92 | 428 | if (compat20) { |
b3ef7fb7 | 429 | if (!do_pam_account()) |
430 | goto auth_fail; | |
d948154a | 431 | if (sshpam_authctxt->force_pwchange) { |
b3ef7fb7 | 432 | sshpam_err = pam_chauthtok(sshpam_handle, |
433 | PAM_CHANGE_EXPIRED_AUTHTOK); | |
434 | if (sshpam_err != PAM_SUCCESS) | |
435 | goto auth_fail; | |
c10bb2ce | 436 | sshpam_password_change_required(0); |
b3ef7fb7 | 437 | } |
2935db92 | 438 | } |
b3ef7fb7 | 439 | |
05114c74 | 440 | buffer_put_cstring(&buffer, "OK"); |
2212fc98 | 441 | |
442 | #ifndef USE_POSIX_THREADS | |
b3ef7fb7 | 443 | /* Export variables set by do_pam_account */ |
444 | buffer_put_int(&buffer, sshpam_account_status); | |
d948154a | 445 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
b3ef7fb7 | 446 | |
2212fc98 | 447 | /* Export any environment strings set in child */ |
448 | for(i = 0; environ[i] != NULL; i++) | |
449 | ; /* Count */ | |
450 | buffer_put_int(&buffer, i); | |
451 | for(i = 0; environ[i] != NULL; i++) | |
452 | buffer_put_cstring(&buffer, environ[i]); | |
453 | ||
454 | /* Export any environment strings set by PAM in child */ | |
455 | env_from_pam = pam_getenvlist(sshpam_handle); | |
456 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
457 | ; /* Count */ | |
458 | buffer_put_int(&buffer, i); | |
459 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
460 | buffer_put_cstring(&buffer, env_from_pam[i]); | |
461 | #endif /* USE_POSIX_THREADS */ | |
462 | ||
7b2a0de3 | 463 | /* XXX - can't do much about an error here */ |
05114c74 | 464 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
465 | buffer_free(&buffer); | |
466 | pthread_exit(NULL); | |
467 | ||
468 | auth_fail: | |
469 | buffer_put_cstring(&buffer, | |
470 | pam_strerror(sshpam_handle, sshpam_err)); | |
7b2a0de3 | 471 | /* XXX - can't do much about an error here */ |
05114c74 | 472 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
473 | buffer_free(&buffer); | |
474 | pthread_exit(NULL); | |
b6453d99 | 475 | |
05114c74 | 476 | return (NULL); /* Avoid warning for non-pthread case */ |
a5c9cd31 | 477 | } |
478 | ||
c6630044 | 479 | void |
480 | sshpam_thread_cleanup(void) | |
a5c9cd31 | 481 | { |
c6630044 | 482 | struct pam_ctxt *ctxt = cleanup_ctxt; |
483 | ||
90f3c272 | 484 | debug3("PAM: %s entering", __func__); |
c6630044 | 485 | if (ctxt != NULL && ctxt->pam_thread != 0) { |
486 | pthread_cancel(ctxt->pam_thread); | |
487 | pthread_join(ctxt->pam_thread, NULL); | |
488 | close(ctxt->pam_psock); | |
489 | close(ctxt->pam_csock); | |
490 | memset(ctxt, 0, sizeof(*ctxt)); | |
491 | cleanup_ctxt = NULL; | |
492 | } | |
05114c74 | 493 | } |
a5c9cd31 | 494 | |
05114c74 | 495 | static int |
0943f13c | 496 | sshpam_null_conv(int n, struct pam_message **msg, |
5b9e2464 | 497 | struct pam_response **resp, void *data) |
05114c74 | 498 | { |
60922169 | 499 | debug3("PAM: %s entering, %d messages", __func__, n); |
05114c74 | 500 | return (PAM_CONV_ERR); |
501 | } | |
a5c9cd31 | 502 | |
05114c74 | 503 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
504 | ||
ab17aac2 | 505 | static int |
506 | sshpam_store_conv(int n, struct pam_message **msg, | |
507 | struct pam_response **resp, void *data) | |
508 | { | |
509 | struct pam_response *reply; | |
510 | int i; | |
511 | size_t len; | |
512 | ||
513 | debug3("PAM: %s called with %d messages", __func__, n); | |
514 | *resp = NULL; | |
515 | ||
516 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
517 | return (PAM_CONV_ERR); | |
518 | ||
519 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
520 | return (PAM_CONV_ERR); | |
521 | memset(reply, 0, n * sizeof(*reply)); | |
522 | ||
523 | for (i = 0; i < n; ++i) { | |
524 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
525 | case PAM_ERROR_MSG: | |
526 | case PAM_TEXT_INFO: | |
527 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
528 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); | |
529 | buffer_append(&loginmsg, "\n", 1 ); | |
530 | reply[i].resp_retcode = PAM_SUCCESS; | |
531 | break; | |
532 | default: | |
533 | goto fail; | |
534 | } | |
535 | } | |
536 | *resp = reply; | |
537 | return (PAM_SUCCESS); | |
538 | ||
539 | fail: | |
540 | for(i = 0; i < n; i++) { | |
541 | if (reply[i].resp != NULL) | |
542 | xfree(reply[i].resp); | |
543 | } | |
544 | xfree(reply); | |
545 | return (PAM_CONV_ERR); | |
546 | } | |
547 | ||
548 | static struct pam_conv store_conv = { sshpam_store_conv, NULL }; | |
549 | ||
c6630044 | 550 | void |
551 | sshpam_cleanup(void) | |
05114c74 | 552 | { |
05114c74 | 553 | debug("PAM: cleanup"); |
0a23d79f | 554 | if (sshpam_handle == NULL) |
555 | return; | |
05114c74 | 556 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
557 | if (sshpam_cred_established) { | |
558 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
559 | sshpam_cred_established = 0; | |
560 | } | |
561 | if (sshpam_session_open) { | |
562 | pam_close_session(sshpam_handle, PAM_SILENT); | |
563 | sshpam_session_open = 0; | |
a5c9cd31 | 564 | } |
dd1fb864 | 565 | sshpam_authenticated = 0; |
05114c74 | 566 | pam_end(sshpam_handle, sshpam_err); |
567 | sshpam_handle = NULL; | |
a5c9cd31 | 568 | } |
569 | ||
05114c74 | 570 | static int |
529d73ab | 571 | sshpam_init(Authctxt *authctxt) |
a5c9cd31 | 572 | { |
74678fb9 | 573 | extern char *__progname; |
529d73ab | 574 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
05114c74 | 575 | |
576 | if (sshpam_handle != NULL) { | |
577 | /* We already have a PAM context; check if the user matches */ | |
578 | sshpam_err = pam_get_item(sshpam_handle, | |
0943f13c | 579 | PAM_USER, (void **)&pam_user); |
05114c74 | 580 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) |
581 | return (0); | |
05114c74 | 582 | pam_end(sshpam_handle, sshpam_err); |
583 | sshpam_handle = NULL; | |
584 | } | |
585 | debug("PAM: initializing for \"%s\"", user); | |
01224183 | 586 | sshpam_err = |
ba6dd90e | 587 | pam_start(SSHD_PAM_SERVICE, user, &store_conv, &sshpam_handle); |
d948154a | 588 | sshpam_authctxt = authctxt; |
529d73ab | 589 | |
5ff453c0 | 590 | if (sshpam_err != PAM_SUCCESS) { |
591 | pam_end(sshpam_handle, sshpam_err); | |
592 | sshpam_handle = NULL; | |
05114c74 | 593 | return (-1); |
5ff453c0 | 594 | } |
c5a7d788 | 595 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
05d62329 | 596 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
05114c74 | 597 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
5ff453c0 | 598 | if (sshpam_err != PAM_SUCCESS) { |
5b9e2464 | 599 | pam_end(sshpam_handle, sshpam_err); |
5ff453c0 | 600 | sshpam_handle = NULL; |
601 | return (-1); | |
602 | } | |
603 | #ifdef PAM_TTY_KLUDGE | |
aff51935 | 604 | /* |
605 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
606 | * sshd doesn't set the tty until too late in the auth process and | |
5ff453c0 | 607 | * may not even set one (for tty-less connections) |
aff51935 | 608 | */ |
5ff453c0 | 609 | debug("PAM: setting PAM_TTY to \"ssh\""); |
610 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
05114c74 | 611 | if (sshpam_err != PAM_SUCCESS) { |
612 | pam_end(sshpam_handle, sshpam_err); | |
613 | sshpam_handle = NULL; | |
614 | return (-1); | |
a5c9cd31 | 615 | } |
5ff453c0 | 616 | #endif |
05114c74 | 617 | return (0); |
a5c9cd31 | 618 | } |
619 | ||
05114c74 | 620 | static void * |
621 | sshpam_init_ctx(Authctxt *authctxt) | |
a5c9cd31 | 622 | { |
05114c74 | 623 | struct pam_ctxt *ctxt; |
624 | int socks[2]; | |
2b87da3b | 625 | |
90f3c272 | 626 | debug3("PAM: %s entering", __func__); |
7fceb20d | 627 | /* Refuse to start if we don't have PAM enabled */ |
628 | if (!options.use_pam) | |
629 | return NULL; | |
630 | ||
05114c74 | 631 | /* Initialize PAM */ |
529d73ab | 632 | if (sshpam_init(authctxt) == -1) { |
05114c74 | 633 | error("PAM: initialization failed"); |
634 | return (NULL); | |
635 | } | |
5c377b3b | 636 | |
05114c74 | 637 | ctxt = xmalloc(sizeof *ctxt); |
c6630044 | 638 | memset(ctxt, 0, sizeof(*ctxt)); |
05114c74 | 639 | |
640 | /* Start the authentication thread */ | |
641 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
642 | error("PAM: failed create sockets: %s", strerror(errno)); | |
643 | xfree(ctxt); | |
644 | return (NULL); | |
645 | } | |
646 | ctxt->pam_psock = socks[0]; | |
647 | ctxt->pam_csock = socks[1]; | |
648 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
649 | error("PAM: failed to start authentication thread: %s", | |
650 | strerror(errno)); | |
651 | close(socks[0]); | |
652 | close(socks[1]); | |
653 | xfree(ctxt); | |
654 | return (NULL); | |
a5c9cd31 | 655 | } |
c6630044 | 656 | cleanup_ctxt = ctxt; |
05114c74 | 657 | return (ctxt); |
658 | } | |
a5c9cd31 | 659 | |
05114c74 | 660 | static int |
661 | sshpam_query(void *ctx, char **name, char **info, | |
662 | u_int *num, char ***prompts, u_int **echo_on) | |
663 | { | |
664 | Buffer buffer; | |
665 | struct pam_ctxt *ctxt = ctx; | |
666 | size_t plen; | |
667 | u_char type; | |
668 | char *msg; | |
bbe58934 | 669 | size_t len, mlen; |
05114c74 | 670 | |
90f3c272 | 671 | debug3("PAM: %s entering", __func__); |
05114c74 | 672 | buffer_init(&buffer); |
673 | *name = xstrdup(""); | |
674 | *info = xstrdup(""); | |
675 | *prompts = xmalloc(sizeof(char *)); | |
676 | **prompts = NULL; | |
677 | plen = 0; | |
678 | *echo_on = xmalloc(sizeof(u_int)); | |
679 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
680 | type = buffer_get_char(&buffer); | |
681 | msg = buffer_get_string(&buffer, NULL); | |
bbe58934 | 682 | mlen = strlen(msg); |
05114c74 | 683 | switch (type) { |
684 | case PAM_PROMPT_ECHO_ON: | |
685 | case PAM_PROMPT_ECHO_OFF: | |
686 | *num = 1; | |
bbe58934 | 687 | len = plen + mlen + 1; |
cbdeccf3 | 688 | **prompts = xrealloc(**prompts, len); |
bbe58934 | 689 | strlcpy(**prompts + plen, msg, len - plen); |
690 | plen += mlen; | |
05114c74 | 691 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
692 | xfree(msg); | |
693 | return (0); | |
694 | case PAM_ERROR_MSG: | |
695 | case PAM_TEXT_INFO: | |
696 | /* accumulate messages */ | |
bbe58934 | 697 | len = plen + mlen + 2; |
cbdeccf3 | 698 | **prompts = xrealloc(**prompts, len); |
bbe58934 | 699 | strlcpy(**prompts + plen, msg, len - plen); |
700 | plen += mlen; | |
701 | strlcat(**prompts + plen, "\n", len - plen); | |
702 | plen++; | |
05114c74 | 703 | xfree(msg); |
5daf7064 | 704 | break; |
05114c74 | 705 | case PAM_SUCCESS: |
706 | case PAM_AUTH_ERR: | |
707 | if (**prompts != NULL) { | |
708 | /* drain any accumulated messages */ | |
3eaf3960 | 709 | debug("PAM: %s", **prompts); |
710 | buffer_append(&loginmsg, **prompts, | |
711 | strlen(**prompts)); | |
05114c74 | 712 | xfree(**prompts); |
713 | **prompts = NULL; | |
714 | } | |
715 | if (type == PAM_SUCCESS) { | |
5d33c697 | 716 | if (!sshpam_login_allowed(sshpam_authctxt)) |
af0e5c2f | 717 | fatal("Internal error: PAM auth " |
718 | "succeeded when it should have " | |
719 | "failed"); | |
2212fc98 | 720 | import_environments(&buffer); |
05114c74 | 721 | *num = 0; |
722 | **echo_on = 0; | |
723 | ctxt->pam_done = 1; | |
724 | xfree(msg); | |
725 | return (0); | |
726 | } | |
1e08e787 | 727 | error("PAM: %s for %s%.100s from %.100s", msg, |
728 | sshpam_authctxt->valid ? "" : "illegal user ", | |
729 | sshpam_authctxt->user, | |
730 | get_remote_name_or_ip(utmp_len, options.use_dns)); | |
e250770b | 731 | /* FALLTHROUGH */ |
5daf7064 | 732 | default: |
05114c74 | 733 | *num = 0; |
734 | **echo_on = 0; | |
735 | xfree(msg); | |
736 | ctxt->pam_done = -1; | |
737 | return (-1); | |
738 | } | |
a5c9cd31 | 739 | } |
05114c74 | 740 | return (-1); |
a5c9cd31 | 741 | } |
742 | ||
05114c74 | 743 | /* XXX - see also comment in auth-chall.c:verify_response */ |
744 | static int | |
745 | sshpam_respond(void *ctx, u_int num, char **resp) | |
a5c9cd31 | 746 | { |
05114c74 | 747 | Buffer buffer; |
748 | struct pam_ctxt *ctxt = ctx; | |
749 | ||
90f3c272 | 750 | debug2("PAM: %s entering, %d responses", __func__, num); |
05114c74 | 751 | switch (ctxt->pam_done) { |
752 | case 1: | |
753 | sshpam_authenticated = 1; | |
754 | return (0); | |
755 | case 0: | |
756 | break; | |
757 | default: | |
758 | return (-1); | |
a5c9cd31 | 759 | } |
05114c74 | 760 | if (num != 1) { |
761 | error("PAM: expected one response, got %u", num); | |
762 | return (-1); | |
763 | } | |
764 | buffer_init(&buffer); | |
5d33c697 | 765 | if (sshpam_login_allowed(sshpam_authctxt)) |
af0e5c2f | 766 | buffer_put_cstring(&buffer, *resp); |
767 | else | |
768 | buffer_put_cstring(&buffer, badpw); | |
7b2a0de3 | 769 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
770 | buffer_free(&buffer); | |
771 | return (-1); | |
772 | } | |
05114c74 | 773 | buffer_free(&buffer); |
774 | return (1); | |
a5c9cd31 | 775 | } |
776 | ||
05114c74 | 777 | static void |
778 | sshpam_free_ctx(void *ctxtp) | |
a5c9cd31 | 779 | { |
05114c74 | 780 | struct pam_ctxt *ctxt = ctxtp; |
39ce53de | 781 | |
90f3c272 | 782 | debug3("PAM: %s entering", __func__); |
c6630044 | 783 | sshpam_thread_cleanup(); |
05114c74 | 784 | xfree(ctxt); |
785 | /* | |
786 | * We don't call sshpam_cleanup() here because we may need the PAM | |
787 | * handle at a later stage, e.g. when setting up a session. It's | |
788 | * still on the cleanup list, so pam_end() *will* be called before | |
789 | * the server process terminates. | |
790 | */ | |
ad55cd03 | 791 | } |
792 | ||
05114c74 | 793 | KbdintDevice sshpam_device = { |
794 | "pam", | |
795 | sshpam_init_ctx, | |
796 | sshpam_query, | |
797 | sshpam_respond, | |
798 | sshpam_free_ctx | |
799 | }; | |
800 | ||
801 | KbdintDevice mm_sshpam_device = { | |
802 | "pam", | |
803 | mm_sshpam_init_ctx, | |
804 | mm_sshpam_query, | |
805 | mm_sshpam_respond, | |
806 | mm_sshpam_free_ctx | |
807 | }; | |
2919e060 | 808 | |
2b87da3b | 809 | /* |
05114c74 | 810 | * This replaces auth-pam.c |
ad55cd03 | 811 | */ |
05114c74 | 812 | void |
529d73ab | 813 | start_pam(Authctxt *authctxt) |
ad55cd03 | 814 | { |
817e6d38 | 815 | if (!options.use_pam) |
816 | fatal("PAM: initialisation requested when UsePAM=no"); | |
817 | ||
529d73ab | 818 | if (sshpam_init(authctxt) == -1) |
05114c74 | 819 | fatal("PAM: initialisation failed"); |
a5c9cd31 | 820 | } |
821 | ||
05114c74 | 822 | void |
823 | finish_pam(void) | |
a5c9cd31 | 824 | { |
c6630044 | 825 | sshpam_cleanup(); |
a5c9cd31 | 826 | } |
827 | ||
5b9e2464 | 828 | u_int |
829 | do_pam_account(void) | |
a5c9cd31 | 830 | { |
ba6dd90e | 831 | debug("%s: called", __func__); |
b3ef7fb7 | 832 | if (sshpam_account_status != -1) |
833 | return (sshpam_account_status); | |
834 | ||
5b9e2464 | 835 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
ba6dd90e | 836 | debug3("PAM: %s pam_acct_mgmt = %d (%s)", __func__, sshpam_err, |
837 | pam_strerror(sshpam_handle, sshpam_err)); | |
b3ef7fb7 | 838 | |
839 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { | |
840 | sshpam_account_status = 0; | |
841 | return (sshpam_account_status); | |
5b9e2464 | 842 | } |
843 | ||
b3ef7fb7 | 844 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
c10bb2ce | 845 | sshpam_password_change_required(1); |
b3ef7fb7 | 846 | |
847 | sshpam_account_status = 1; | |
848 | return (sshpam_account_status); | |
05114c74 | 849 | } |
46c76c63 | 850 | |
49e82bb9 | 851 | void |
852 | do_pam_set_tty(const char *tty) | |
853 | { | |
26b3608b | 854 | if (tty != NULL) { |
855 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
856 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
857 | if (sshpam_err != PAM_SUCCESS) | |
858 | fatal("PAM: failed to set PAM_TTY: %s", | |
859 | pam_strerror(sshpam_handle, sshpam_err)); | |
860 | } | |
05114c74 | 861 | } |
cbd7492e | 862 | |
05114c74 | 863 | void |
864 | do_pam_setcred(int init) | |
865 | { | |
866 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
ba6dd90e | 867 | (const void *)&store_conv); |
05114c74 | 868 | if (sshpam_err != PAM_SUCCESS) |
869 | fatal("PAM: failed to set PAM_CONV: %s", | |
870 | pam_strerror(sshpam_handle, sshpam_err)); | |
871 | if (init) { | |
872 | debug("PAM: establishing credentials"); | |
873 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
874 | } else { | |
875 | debug("PAM: reinitializing credentials"); | |
876 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
877 | } | |
878 | if (sshpam_err == PAM_SUCCESS) { | |
879 | sshpam_cred_established = 1; | |
880 | return; | |
881 | } | |
882 | if (sshpam_authenticated) | |
883 | fatal("PAM: pam_setcred(): %s", | |
884 | pam_strerror(sshpam_handle, sshpam_err)); | |
885 | else | |
886 | debug("PAM: pam_setcred(): %s", | |
887 | pam_strerror(sshpam_handle, sshpam_err)); | |
a5c9cd31 | 888 | } |
889 | ||
05114c74 | 890 | static int |
0943f13c | 891 | sshpam_tty_conv(int n, struct pam_message **msg, |
5b9e2464 | 892 | struct pam_response **resp, void *data) |
ee48c949 | 893 | { |
05114c74 | 894 | char input[PAM_MAX_MSG_SIZE]; |
0a23d79f | 895 | struct pam_response *reply; |
ee48c949 | 896 | int i; |
897 | ||
60922169 | 898 | debug3("PAM: %s called with %d messages", __func__, n); |
899 | ||
0a23d79f | 900 | *resp = NULL; |
901 | ||
3eaf3960 | 902 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
05114c74 | 903 | return (PAM_CONV_ERR); |
0a23d79f | 904 | |
905 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
906 | return (PAM_CONV_ERR); | |
907 | memset(reply, 0, n * sizeof(*reply)); | |
908 | ||
05114c74 | 909 | for (i = 0; i < n; ++i) { |
910 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
911 | case PAM_PROMPT_ECHO_OFF: | |
0a23d79f | 912 | reply[i].resp = |
aff51935 | 913 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
05114c74 | 914 | RP_ALLOW_STDIN); |
0a23d79f | 915 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 916 | break; |
917 | case PAM_PROMPT_ECHO_ON: | |
74117b26 | 918 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
05114c74 | 919 | fgets(input, sizeof input, stdin); |
8936b151 | 920 | if ((reply[i].resp = strdup(input)) == NULL) |
921 | goto fail; | |
0a23d79f | 922 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 923 | break; |
924 | case PAM_ERROR_MSG: | |
925 | case PAM_TEXT_INFO: | |
74117b26 | 926 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
0a23d79f | 927 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 928 | break; |
929 | default: | |
930 | goto fail; | |
931 | } | |
ee48c949 | 932 | } |
0a23d79f | 933 | *resp = reply; |
05114c74 | 934 | return (PAM_SUCCESS); |
0a23d79f | 935 | |
05114c74 | 936 | fail: |
0a23d79f | 937 | for(i = 0; i < n; i++) { |
938 | if (reply[i].resp != NULL) | |
939 | xfree(reply[i].resp); | |
940 | } | |
941 | xfree(reply); | |
05114c74 | 942 | return (PAM_CONV_ERR); |
ee48c949 | 943 | } |
944 | ||
8600a4ab | 945 | static struct pam_conv tty_conv = { sshpam_tty_conv, NULL }; |
3eaf3960 | 946 | |
05114c74 | 947 | /* |
948 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
949 | * support that | |
950 | */ | |
951 | void | |
952 | do_pam_chauthtok(void) | |
a5c9cd31 | 953 | { |
05114c74 | 954 | if (use_privsep) |
5b9e2464 | 955 | fatal("Password expired (unable to change with privsep)"); |
05114c74 | 956 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
3eaf3960 | 957 | (const void *)&tty_conv); |
05114c74 | 958 | if (sshpam_err != PAM_SUCCESS) |
959 | fatal("PAM: failed to set PAM_CONV: %s", | |
960 | pam_strerror(sshpam_handle, sshpam_err)); | |
961 | debug("PAM: changing password"); | |
962 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
963 | if (sshpam_err != PAM_SUCCESS) | |
964 | fatal("PAM: pam_chauthtok(): %s", | |
965 | pam_strerror(sshpam_handle, sshpam_err)); | |
5daf7064 | 966 | } |
967 | ||
3eaf3960 | 968 | void |
969 | do_pam_session(void) | |
970 | { | |
dd1fb864 | 971 | debug3("PAM: opening session"); |
aff51935 | 972 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
ef687c66 | 973 | (const void *)&store_conv); |
3eaf3960 | 974 | if (sshpam_err != PAM_SUCCESS) |
975 | fatal("PAM: failed to set PAM_CONV: %s", | |
976 | pam_strerror(sshpam_handle, sshpam_err)); | |
977 | sshpam_err = pam_open_session(sshpam_handle, 0); | |
abdec250 | 978 | if (sshpam_err == PAM_SUCCESS) |
979 | sshpam_session_open = 1; | |
980 | else { | |
981 | sshpam_session_open = 0; | |
982 | disable_forwarding(); | |
983 | error("PAM: pam_open_session(): %s", | |
3eaf3960 | 984 | pam_strerror(sshpam_handle, sshpam_err)); |
abdec250 | 985 | } |
986 | ||
987 | } | |
988 | ||
989 | int | |
990 | is_pam_session_open(void) | |
991 | { | |
992 | return sshpam_session_open; | |
3eaf3960 | 993 | } |
994 | ||
aff51935 | 995 | /* |
749560dd | 996 | * Set a PAM environment string. We need to do this so that the session |
997 | * modules can handle things like Kerberos/GSI credentials that appear | |
998 | * during the ssh authentication process. | |
999 | */ | |
749560dd | 1000 | int |
aff51935 | 1001 | do_pam_putenv(char *name, char *value) |
749560dd | 1002 | { |
749560dd | 1003 | int ret = 1; |
b6453d99 | 1004 | #ifdef HAVE_PAM_PUTENV |
263c65df | 1005 | char *compound; |
1006 | size_t len; | |
1007 | ||
1008 | len = strlen(name) + strlen(value) + 2; | |
1009 | compound = xmalloc(len); | |
1010 | ||
1011 | snprintf(compound, len, "%s=%s", name, value); | |
1012 | ret = pam_putenv(sshpam_handle, compound); | |
1013 | xfree(compound); | |
749560dd | 1014 | #endif |
263c65df | 1015 | |
749560dd | 1016 | return (ret); |
1017 | } | |
1018 | ||
2212fc98 | 1019 | char ** |
1020 | fetch_pam_child_environment(void) | |
1021 | { | |
1022 | return sshpam_env; | |
1023 | } | |
1024 | ||
05114c74 | 1025 | char ** |
1026 | fetch_pam_environment(void) | |
1027 | { | |
05114c74 | 1028 | return (pam_getenvlist(sshpam_handle)); |
05114c74 | 1029 | } |
5c377b3b | 1030 | |
05114c74 | 1031 | void |
1032 | free_pam_environment(char **env) | |
1033 | { | |
1034 | char **envp; | |
5daf7064 | 1035 | |
0eb6370a | 1036 | if (env == NULL) |
1037 | return; | |
1038 | ||
05114c74 | 1039 | for (envp = env; *envp; envp++) |
1040 | xfree(*envp); | |
1041 | xfree(env); | |
a5c9cd31 | 1042 | } |
1043 | ||
5de92f17 | 1044 | /* |
1045 | * "Blind" conversation function for password authentication. Assumes that | |
1046 | * echo-off prompts are for the password and stores messages for later | |
1047 | * display. | |
1048 | */ | |
1049 | static int | |
0943f13c | 1050 | sshpam_passwd_conv(int n, struct pam_message **msg, |
5de92f17 | 1051 | struct pam_response **resp, void *data) |
1052 | { | |
1053 | struct pam_response *reply; | |
1054 | int i; | |
1055 | size_t len; | |
1056 | ||
1057 | debug3("PAM: %s called with %d messages", __func__, n); | |
1058 | ||
1059 | *resp = NULL; | |
1060 | ||
1061 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
1062 | return (PAM_CONV_ERR); | |
1063 | ||
1064 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
1065 | return (PAM_CONV_ERR); | |
1066 | memset(reply, 0, n * sizeof(*reply)); | |
1067 | ||
1068 | for (i = 0; i < n; ++i) { | |
1069 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
1070 | case PAM_PROMPT_ECHO_OFF: | |
1071 | if (sshpam_password == NULL) | |
1072 | goto fail; | |
8936b151 | 1073 | if ((reply[i].resp = strdup(sshpam_password)) == NULL) |
1074 | goto fail; | |
5de92f17 | 1075 | reply[i].resp_retcode = PAM_SUCCESS; |
1076 | break; | |
1077 | case PAM_ERROR_MSG: | |
1078 | case PAM_TEXT_INFO: | |
1079 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
1080 | if (len > 0) { | |
1081 | buffer_append(&loginmsg, | |
1082 | PAM_MSG_MEMBER(msg, i, msg), len); | |
1083 | buffer_append(&loginmsg, "\n", 1); | |
1084 | } | |
8936b151 | 1085 | if ((reply[i].resp = strdup("")) == NULL) |
1086 | goto fail; | |
5de92f17 | 1087 | reply[i].resp_retcode = PAM_SUCCESS; |
1088 | break; | |
1089 | default: | |
1090 | goto fail; | |
1091 | } | |
1092 | } | |
1093 | *resp = reply; | |
1094 | return (PAM_SUCCESS); | |
1095 | ||
1096 | fail: | |
1097 | for(i = 0; i < n; i++) { | |
1098 | if (reply[i].resp != NULL) | |
1099 | xfree(reply[i].resp); | |
1100 | } | |
1101 | xfree(reply); | |
1102 | return (PAM_CONV_ERR); | |
1103 | } | |
1104 | ||
1105 | static struct pam_conv passwd_conv = { sshpam_passwd_conv, NULL }; | |
1106 | ||
1107 | /* | |
1108 | * Attempt password authentication via PAM | |
1109 | */ | |
1110 | int | |
1111 | sshpam_auth_passwd(Authctxt *authctxt, const char *password) | |
1112 | { | |
1113 | int flags = (options.permit_empty_passwd == 0 ? | |
1114 | PAM_DISALLOW_NULL_AUTHTOK : 0); | |
1115 | ||
1116 | if (!options.use_pam || sshpam_handle == NULL) | |
1117 | fatal("PAM: %s called when PAM disabled or failed to " | |
1118 | "initialise.", __func__); | |
1119 | ||
1120 | sshpam_password = password; | |
1121 | sshpam_authctxt = authctxt; | |
1122 | ||
0e716148 | 1123 | /* |
1124 | * If the user logging in is invalid, or is root but is not permitted | |
1125 | * by PermitRootLogin, use an invalid password to prevent leaking | |
1126 | * information via timing (eg if the PAM config has a delay on fail). | |
1127 | */ | |
5d33c697 | 1128 | if (!sshpam_login_allowed(authctxt)) |
0e716148 | 1129 | sshpam_password = badpw; |
1130 | ||
5de92f17 | 1131 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
1132 | (const void *)&passwd_conv); | |
1133 | if (sshpam_err != PAM_SUCCESS) | |
1134 | fatal("PAM: %s: failed to set PAM_CONV: %s", __func__, | |
1135 | pam_strerror(sshpam_handle, sshpam_err)); | |
1136 | ||
1137 | sshpam_err = pam_authenticate(sshpam_handle, flags); | |
1138 | sshpam_password = NULL; | |
5d33c697 | 1139 | if (sshpam_err == PAM_SUCCESS && sshpam_login_allowed(authctxt)) { |
5de92f17 | 1140 | debug("PAM: password authentication accepted for %.100s", |
1141 | authctxt->user); | |
1142 | return 1; | |
1143 | } else { | |
1144 | debug("PAM: password authentication failed for %.100s: %s", | |
1145 | authctxt->valid ? authctxt->user : "an illegal user", | |
1146 | pam_strerror(sshpam_handle, sshpam_err)); | |
1147 | return 0; | |
1148 | } | |
1149 | } | |
a5c9cd31 | 1150 | #endif /* USE_PAM */ |