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