]>
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 | */ |
31 | ||
5ff453c0 | 32 | /* Based on $FreeBSD: src/crypto/openssh/auth2-pam-freebsd.c,v 1.11 2003/03/31 13:48:18 des Exp $ */ |
a5c9cd31 | 33 | #include "includes.h" |
5ff453c0 | 34 | RCSID("$Id$"); |
a5c9cd31 | 35 | |
36 | #ifdef USE_PAM | |
2511d104 | 37 | #if defined(HAVE_SECURITY_PAM_APPL_H) |
05114c74 | 38 | #include <security/pam_appl.h> |
2511d104 | 39 | #elif defined (HAVE_PAM_PAM_APPL_H) |
40 | #include <pam/pam_appl.h> | |
41 | #endif | |
05114c74 | 42 | |
fde58bd4 | 43 | #include "auth.h" |
5c377b3b | 44 | #include "auth-pam.h" |
05114c74 | 45 | #include "buffer.h" |
46 | #include "bufaux.h" | |
42f11eb2 | 47 | #include "canohost.h" |
05114c74 | 48 | #include "log.h" |
49 | #include "monitor_wrap.h" | |
50 | #include "msg.h" | |
51 | #include "packet.h" | |
42f11eb2 | 52 | #include "readpass.h" |
05114c74 | 53 | #include "servconf.h" |
54 | #include "ssh2.h" | |
55 | #include "xmalloc.h" | |
5b9e2464 | 56 | #include "auth-options.h" |
a5c9cd31 | 57 | |
7fceb20d | 58 | extern ServerOptions options; |
3eaf3960 | 59 | extern Buffer loginmsg; |
b3ef7fb7 | 60 | extern int compat20; |
7fceb20d | 61 | |
05114c74 | 62 | #ifdef USE_POSIX_THREADS |
63 | #include <pthread.h> | |
64 | /* | |
aff51935 | 65 | * Avoid namespace clash when *not* using pthreads for systems *with* |
66 | * pthreads, which unconditionally define pthread_t via sys/types.h | |
05114c74 | 67 | * (e.g. Linux) |
68 | */ | |
aff51935 | 69 | typedef pthread_t sp_pthread_t; |
05114c74 | 70 | #else |
e47e681f | 71 | typedef pid_t sp_pthread_t; |
72 | #endif | |
73 | ||
74 | struct pam_ctxt { | |
75 | sp_pthread_t pam_thread; | |
76 | int pam_psock; | |
77 | int pam_csock; | |
78 | int pam_done; | |
79 | }; | |
80 | ||
81 | static void sshpam_free_ctx(void *); | |
82 | static struct pam_ctxt *cleanup_ctxt; | |
83 | ||
84 | #ifndef USE_POSIX_THREADS | |
05114c74 | 85 | /* |
86 | * Simulate threads with processes. | |
87 | */ | |
a5c9cd31 | 88 | |
100e6910 | 89 | static int sshpam_thread_status = -1; |
90 | static mysig_t sshpam_oldsig; | |
91 | ||
92 | static void | |
93 | sshpam_sigchld_handler(int sig) | |
94 | { | |
28b49ff8 | 95 | if (cleanup_ctxt == NULL) |
96 | return; /* handler called after PAM cleanup, shouldn't happen */ | |
100e6910 | 97 | if (waitpid(cleanup_ctxt->pam_thread, &sshpam_thread_status, 0) == -1) |
98 | return; /* couldn't wait for process */ | |
99 | if (WIFSIGNALED(sshpam_thread_status) && | |
100 | WTERMSIG(sshpam_thread_status) == SIGTERM) | |
101 | return; /* terminated by pthread_cancel */ | |
102 | if (!WIFEXITED(sshpam_thread_status)) | |
103 | fatal("PAM: authentication thread exited unexpectedly"); | |
104 | if (WEXITSTATUS(sshpam_thread_status) != 0) | |
105 | fatal("PAM: authentication thread exited uncleanly"); | |
106 | } | |
107 | ||
05114c74 | 108 | static void |
109 | pthread_exit(void *value __unused) | |
110 | { | |
111 | _exit(0); | |
112 | } | |
5daf7064 | 113 | |
05114c74 | 114 | static int |
115 | pthread_create(sp_pthread_t *thread, const void *attr __unused, | |
116 | void *(*thread_start)(void *), void *arg) | |
117 | { | |
118 | pid_t pid; | |
119 | ||
355fbf31 | 120 | sshpam_thread_status = -1; |
05114c74 | 121 | switch ((pid = fork())) { |
122 | case -1: | |
123 | error("fork(): %s", strerror(errno)); | |
124 | return (-1); | |
125 | case 0: | |
126 | thread_start(arg); | |
127 | _exit(1); | |
128 | default: | |
129 | *thread = pid; | |
100e6910 | 130 | sshpam_oldsig = signal(SIGCHLD, sshpam_sigchld_handler); |
05114c74 | 131 | return (0); |
132 | } | |
133 | } | |
a5c9cd31 | 134 | |
05114c74 | 135 | static int |
136 | pthread_cancel(sp_pthread_t thread) | |
8c9fe09e | 137 | { |
28b49ff8 | 138 | signal(SIGCHLD, sshpam_oldsig); |
05114c74 | 139 | return (kill(thread, SIGTERM)); |
8c9fe09e | 140 | } |
141 | ||
05114c74 | 142 | static int |
143 | pthread_join(sp_pthread_t thread, void **value __unused) | |
8c9fe09e | 144 | { |
05114c74 | 145 | int status; |
146 | ||
100e6910 | 147 | if (sshpam_thread_status != -1) |
148 | return (sshpam_thread_status); | |
149 | signal(SIGCHLD, sshpam_oldsig); | |
05114c74 | 150 | waitpid(thread, &status, 0); |
151 | return (status); | |
8c9fe09e | 152 | } |
05114c74 | 153 | #endif |
154 | ||
155 | ||
0a23d79f | 156 | static pam_handle_t *sshpam_handle = NULL; |
157 | static int sshpam_err = 0; | |
158 | static int sshpam_authenticated = 0; | |
0a23d79f | 159 | static int sshpam_session_open = 0; |
160 | static int sshpam_cred_established = 0; | |
b3ef7fb7 | 161 | static int sshpam_account_status = -1; |
2212fc98 | 162 | static char **sshpam_env = NULL; |
d948154a | 163 | static Authctxt *sshpam_authctxt = NULL; |
05114c74 | 164 | |
2212fc98 | 165 | /* Some PAM implementations don't implement this */ |
166 | #ifndef HAVE_PAM_GETENVLIST | |
167 | static char ** | |
168 | pam_getenvlist(pam_handle_t *pamh) | |
169 | { | |
170 | /* | |
aff51935 | 171 | * XXX - If necessary, we can still support envrionment passing |
2212fc98 | 172 | * for platforms without pam_getenvlist by searching for known |
173 | * env vars (e.g. KRB5CCNAME) from the PAM environment. | |
174 | */ | |
175 | return NULL; | |
176 | } | |
177 | #endif | |
178 | ||
b3ef7fb7 | 179 | void |
180 | pam_password_change_required(int reqd) | |
181 | { | |
90f3c272 | 182 | debug3("%s %d", __func__, reqd); |
d948154a | 183 | if (sshpam_authctxt == NULL) |
529d73ab | 184 | fatal("%s: PAM authctxt not initialized", __func__); |
d948154a | 185 | sshpam_authctxt->force_pwchange = reqd; |
b3ef7fb7 | 186 | if (reqd) { |
187 | no_port_forwarding_flag |= 2; | |
188 | no_agent_forwarding_flag |= 2; | |
189 | no_x11_forwarding_flag |= 2; | |
190 | } else { | |
191 | no_port_forwarding_flag &= ~2; | |
192 | no_agent_forwarding_flag &= ~2; | |
193 | no_x11_forwarding_flag &= ~2; | |
b3ef7fb7 | 194 | } |
195 | } | |
dd1fb864 | 196 | |
2212fc98 | 197 | /* Import regular and PAM environment from subprocess */ |
198 | static void | |
199 | import_environments(Buffer *b) | |
200 | { | |
201 | char *env; | |
202 | u_int i, num_env; | |
203 | int err; | |
204 | ||
90f3c272 | 205 | debug3("PAM: %s entering", __func__); |
206 | ||
a1e0095d | 207 | #ifndef USE_POSIX_THREADS |
b3ef7fb7 | 208 | /* Import variables set by do_pam_account */ |
209 | sshpam_account_status = buffer_get_int(b); | |
210 | pam_password_change_required(buffer_get_int(b)); | |
211 | ||
2212fc98 | 212 | /* Import environment from subprocess */ |
213 | num_env = buffer_get_int(b); | |
214 | sshpam_env = xmalloc((num_env + 1) * sizeof(*sshpam_env)); | |
215 | debug3("PAM: num env strings %d", num_env); | |
216 | for(i = 0; i < num_env; i++) | |
217 | sshpam_env[i] = buffer_get_string(b, NULL); | |
218 | ||
219 | sshpam_env[num_env] = NULL; | |
220 | ||
221 | /* Import PAM environment from subprocess */ | |
222 | num_env = buffer_get_int(b); | |
223 | debug("PAM: num PAM env strings %d", num_env); | |
224 | for(i = 0; i < num_env; i++) { | |
225 | env = buffer_get_string(b, NULL); | |
226 | ||
f79a6165 | 227 | #ifdef HAVE_PAM_PUTENV |
2212fc98 | 228 | /* Errors are not fatal here */ |
229 | if ((err = pam_putenv(sshpam_handle, env)) != PAM_SUCCESS) { | |
230 | error("PAM: pam_putenv: %s", | |
231 | pam_strerror(sshpam_handle, sshpam_err)); | |
232 | } | |
f79a6165 | 233 | #endif |
2212fc98 | 234 | } |
a1e0095d | 235 | #endif |
2212fc98 | 236 | } |
237 | ||
ad55cd03 | 238 | /* |
05114c74 | 239 | * Conversation function for authentication thread. |
ad55cd03 | 240 | */ |
05114c74 | 241 | static int |
5b9e2464 | 242 | sshpam_thread_conv(int n, const struct pam_message **msg, |
243 | struct pam_response **resp, void *data) | |
a5c9cd31 | 244 | { |
05114c74 | 245 | Buffer buffer; |
246 | struct pam_ctxt *ctxt; | |
0a23d79f | 247 | struct pam_response *reply; |
05114c74 | 248 | int i; |
249 | ||
60922169 | 250 | debug3("PAM: %s entering, %d messages", __func__, n); |
0a23d79f | 251 | *resp = NULL; |
252 | ||
05114c74 | 253 | ctxt = data; |
254 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
255 | return (PAM_CONV_ERR); | |
0a23d79f | 256 | |
257 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
258 | return (PAM_CONV_ERR); | |
259 | memset(reply, 0, n * sizeof(*reply)); | |
260 | ||
05114c74 | 261 | buffer_init(&buffer); |
262 | for (i = 0; i < n; ++i) { | |
05114c74 | 263 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
264 | case PAM_PROMPT_ECHO_OFF: | |
aff51935 | 265 | buffer_put_cstring(&buffer, |
0a23d79f | 266 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 267 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 268 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
269 | goto fail; | |
aff51935 | 270 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) |
7b2a0de3 | 271 | goto fail; |
05114c74 | 272 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
273 | goto fail; | |
0a23d79f | 274 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 275 | break; |
276 | case PAM_PROMPT_ECHO_ON: | |
aff51935 | 277 | buffer_put_cstring(&buffer, |
0a23d79f | 278 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 279 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 280 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
281 | goto fail; | |
282 | if (ssh_msg_recv(ctxt->pam_csock, &buffer) == -1) | |
283 | goto fail; | |
05114c74 | 284 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) |
285 | goto fail; | |
0a23d79f | 286 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 287 | break; |
288 | case PAM_ERROR_MSG: | |
aff51935 | 289 | buffer_put_cstring(&buffer, |
0a23d79f | 290 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 291 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 292 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
293 | goto fail; | |
05114c74 | 294 | break; |
295 | case PAM_TEXT_INFO: | |
aff51935 | 296 | buffer_put_cstring(&buffer, |
0a23d79f | 297 | PAM_MSG_MEMBER(msg, i, msg)); |
aff51935 | 298 | if (ssh_msg_send(ctxt->pam_csock, |
7b2a0de3 | 299 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer) == -1) |
300 | goto fail; | |
05114c74 | 301 | break; |
302 | default: | |
303 | goto fail; | |
a5c9cd31 | 304 | } |
05114c74 | 305 | buffer_clear(&buffer); |
a5c9cd31 | 306 | } |
05114c74 | 307 | buffer_free(&buffer); |
0a23d79f | 308 | *resp = reply; |
05114c74 | 309 | return (PAM_SUCCESS); |
0a23d79f | 310 | |
05114c74 | 311 | fail: |
0a23d79f | 312 | for(i = 0; i < n; i++) { |
313 | if (reply[i].resp != NULL) | |
314 | xfree(reply[i].resp); | |
315 | } | |
316 | xfree(reply); | |
05114c74 | 317 | buffer_free(&buffer); |
318 | return (PAM_CONV_ERR); | |
319 | } | |
a5c9cd31 | 320 | |
05114c74 | 321 | /* |
322 | * Authentication thread. | |
323 | */ | |
324 | static void * | |
325 | sshpam_thread(void *ctxtp) | |
326 | { | |
327 | struct pam_ctxt *ctxt = ctxtp; | |
328 | Buffer buffer; | |
c53917a9 | 329 | struct pam_conv sshpam_conv; |
05114c74 | 330 | #ifndef USE_POSIX_THREADS |
2212fc98 | 331 | extern char **environ; |
332 | char **env_from_pam; | |
333 | u_int i; | |
05114c74 | 334 | const char *pam_user; |
335 | ||
336 | pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user); | |
337 | setproctitle("%s [pam]", pam_user); | |
2212fc98 | 338 | environ[0] = NULL; |
05114c74 | 339 | #endif |
a5c9cd31 | 340 | |
c53917a9 | 341 | sshpam_conv.conv = sshpam_thread_conv; |
342 | sshpam_conv.appdata_ptr = ctxt; | |
343 | ||
d948154a | 344 | if (sshpam_authctxt == NULL) |
529d73ab | 345 | fatal("%s: PAM authctxt not initialized", __func__); |
346 | ||
05114c74 | 347 | buffer_init(&buffer); |
348 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
349 | (const void *)&sshpam_conv); | |
350 | if (sshpam_err != PAM_SUCCESS) | |
351 | goto auth_fail; | |
352 | sshpam_err = pam_authenticate(sshpam_handle, 0); | |
353 | if (sshpam_err != PAM_SUCCESS) | |
354 | goto auth_fail; | |
b3ef7fb7 | 355 | |
2935db92 | 356 | if (compat20) { |
b3ef7fb7 | 357 | if (!do_pam_account()) |
358 | goto auth_fail; | |
d948154a | 359 | if (sshpam_authctxt->force_pwchange) { |
b3ef7fb7 | 360 | sshpam_err = pam_chauthtok(sshpam_handle, |
361 | PAM_CHANGE_EXPIRED_AUTHTOK); | |
362 | if (sshpam_err != PAM_SUCCESS) | |
363 | goto auth_fail; | |
364 | pam_password_change_required(0); | |
365 | } | |
2935db92 | 366 | } |
b3ef7fb7 | 367 | |
05114c74 | 368 | buffer_put_cstring(&buffer, "OK"); |
2212fc98 | 369 | |
370 | #ifndef USE_POSIX_THREADS | |
b3ef7fb7 | 371 | /* Export variables set by do_pam_account */ |
372 | buffer_put_int(&buffer, sshpam_account_status); | |
d948154a | 373 | buffer_put_int(&buffer, sshpam_authctxt->force_pwchange); |
b3ef7fb7 | 374 | |
2212fc98 | 375 | /* Export any environment strings set in child */ |
376 | for(i = 0; environ[i] != NULL; i++) | |
377 | ; /* Count */ | |
378 | buffer_put_int(&buffer, i); | |
379 | for(i = 0; environ[i] != NULL; i++) | |
380 | buffer_put_cstring(&buffer, environ[i]); | |
381 | ||
382 | /* Export any environment strings set by PAM in child */ | |
383 | env_from_pam = pam_getenvlist(sshpam_handle); | |
384 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
385 | ; /* Count */ | |
386 | buffer_put_int(&buffer, i); | |
387 | for(i = 0; env_from_pam != NULL && env_from_pam[i] != NULL; i++) | |
388 | buffer_put_cstring(&buffer, env_from_pam[i]); | |
389 | #endif /* USE_POSIX_THREADS */ | |
390 | ||
7b2a0de3 | 391 | /* XXX - can't do much about an error here */ |
05114c74 | 392 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); |
393 | buffer_free(&buffer); | |
394 | pthread_exit(NULL); | |
395 | ||
396 | auth_fail: | |
397 | buffer_put_cstring(&buffer, | |
398 | pam_strerror(sshpam_handle, sshpam_err)); | |
7b2a0de3 | 399 | /* XXX - can't do much about an error here */ |
05114c74 | 400 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); |
401 | buffer_free(&buffer); | |
402 | pthread_exit(NULL); | |
b6453d99 | 403 | |
05114c74 | 404 | return (NULL); /* Avoid warning for non-pthread case */ |
a5c9cd31 | 405 | } |
406 | ||
c6630044 | 407 | void |
408 | sshpam_thread_cleanup(void) | |
a5c9cd31 | 409 | { |
c6630044 | 410 | struct pam_ctxt *ctxt = cleanup_ctxt; |
411 | ||
90f3c272 | 412 | debug3("PAM: %s entering", __func__); |
c6630044 | 413 | if (ctxt != NULL && ctxt->pam_thread != 0) { |
414 | pthread_cancel(ctxt->pam_thread); | |
415 | pthread_join(ctxt->pam_thread, NULL); | |
416 | close(ctxt->pam_psock); | |
417 | close(ctxt->pam_csock); | |
418 | memset(ctxt, 0, sizeof(*ctxt)); | |
419 | cleanup_ctxt = NULL; | |
420 | } | |
05114c74 | 421 | } |
a5c9cd31 | 422 | |
05114c74 | 423 | static int |
5b9e2464 | 424 | sshpam_null_conv(int n, const struct pam_message **msg, |
425 | struct pam_response **resp, void *data) | |
05114c74 | 426 | { |
60922169 | 427 | debug3("PAM: %s entering, %d messages", __func__, n); |
05114c74 | 428 | return (PAM_CONV_ERR); |
429 | } | |
a5c9cd31 | 430 | |
05114c74 | 431 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
432 | ||
c6630044 | 433 | void |
434 | sshpam_cleanup(void) | |
05114c74 | 435 | { |
05114c74 | 436 | debug("PAM: cleanup"); |
0a23d79f | 437 | if (sshpam_handle == NULL) |
438 | return; | |
05114c74 | 439 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
440 | if (sshpam_cred_established) { | |
441 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
442 | sshpam_cred_established = 0; | |
443 | } | |
444 | if (sshpam_session_open) { | |
445 | pam_close_session(sshpam_handle, PAM_SILENT); | |
446 | sshpam_session_open = 0; | |
a5c9cd31 | 447 | } |
dd1fb864 | 448 | sshpam_authenticated = 0; |
05114c74 | 449 | pam_end(sshpam_handle, sshpam_err); |
450 | sshpam_handle = NULL; | |
a5c9cd31 | 451 | } |
452 | ||
05114c74 | 453 | static int |
529d73ab | 454 | sshpam_init(Authctxt *authctxt) |
a5c9cd31 | 455 | { |
05114c74 | 456 | extern u_int utmp_len; |
74678fb9 | 457 | extern char *__progname; |
529d73ab | 458 | const char *pam_rhost, *pam_user, *user = authctxt->user; |
05114c74 | 459 | |
460 | if (sshpam_handle != NULL) { | |
461 | /* We already have a PAM context; check if the user matches */ | |
462 | sshpam_err = pam_get_item(sshpam_handle, | |
463 | PAM_USER, (const void **)&pam_user); | |
464 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) | |
465 | return (0); | |
05114c74 | 466 | pam_end(sshpam_handle, sshpam_err); |
467 | sshpam_handle = NULL; | |
468 | } | |
469 | debug("PAM: initializing for \"%s\"", user); | |
01224183 | 470 | sshpam_err = |
471 | pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle); | |
d948154a | 472 | sshpam_authctxt = authctxt; |
529d73ab | 473 | |
5ff453c0 | 474 | if (sshpam_err != PAM_SUCCESS) { |
475 | pam_end(sshpam_handle, sshpam_err); | |
476 | sshpam_handle = NULL; | |
05114c74 | 477 | return (-1); |
5ff453c0 | 478 | } |
c5a7d788 | 479 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
05d62329 | 480 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
05114c74 | 481 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
5ff453c0 | 482 | if (sshpam_err != PAM_SUCCESS) { |
5b9e2464 | 483 | pam_end(sshpam_handle, sshpam_err); |
5ff453c0 | 484 | sshpam_handle = NULL; |
485 | return (-1); | |
486 | } | |
487 | #ifdef PAM_TTY_KLUDGE | |
aff51935 | 488 | /* |
489 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
490 | * sshd doesn't set the tty until too late in the auth process and | |
5ff453c0 | 491 | * may not even set one (for tty-less connections) |
aff51935 | 492 | */ |
5ff453c0 | 493 | debug("PAM: setting PAM_TTY to \"ssh\""); |
494 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
05114c74 | 495 | if (sshpam_err != PAM_SUCCESS) { |
496 | pam_end(sshpam_handle, sshpam_err); | |
497 | sshpam_handle = NULL; | |
498 | return (-1); | |
a5c9cd31 | 499 | } |
5ff453c0 | 500 | #endif |
05114c74 | 501 | return (0); |
a5c9cd31 | 502 | } |
503 | ||
05114c74 | 504 | static void * |
505 | sshpam_init_ctx(Authctxt *authctxt) | |
a5c9cd31 | 506 | { |
05114c74 | 507 | struct pam_ctxt *ctxt; |
508 | int socks[2]; | |
2b87da3b | 509 | |
90f3c272 | 510 | debug3("PAM: %s entering", __func__); |
7fceb20d | 511 | /* Refuse to start if we don't have PAM enabled */ |
512 | if (!options.use_pam) | |
513 | return NULL; | |
514 | ||
05114c74 | 515 | /* Initialize PAM */ |
529d73ab | 516 | if (sshpam_init(authctxt) == -1) { |
05114c74 | 517 | error("PAM: initialization failed"); |
518 | return (NULL); | |
519 | } | |
5c377b3b | 520 | |
05114c74 | 521 | ctxt = xmalloc(sizeof *ctxt); |
c6630044 | 522 | memset(ctxt, 0, sizeof(*ctxt)); |
05114c74 | 523 | |
524 | /* Start the authentication thread */ | |
525 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
526 | error("PAM: failed create sockets: %s", strerror(errno)); | |
527 | xfree(ctxt); | |
528 | return (NULL); | |
529 | } | |
530 | ctxt->pam_psock = socks[0]; | |
531 | ctxt->pam_csock = socks[1]; | |
532 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
533 | error("PAM: failed to start authentication thread: %s", | |
534 | strerror(errno)); | |
535 | close(socks[0]); | |
536 | close(socks[1]); | |
537 | xfree(ctxt); | |
538 | return (NULL); | |
a5c9cd31 | 539 | } |
c6630044 | 540 | cleanup_ctxt = ctxt; |
05114c74 | 541 | return (ctxt); |
542 | } | |
a5c9cd31 | 543 | |
05114c74 | 544 | static int |
545 | sshpam_query(void *ctx, char **name, char **info, | |
546 | u_int *num, char ***prompts, u_int **echo_on) | |
547 | { | |
548 | Buffer buffer; | |
549 | struct pam_ctxt *ctxt = ctx; | |
550 | size_t plen; | |
551 | u_char type; | |
552 | char *msg; | |
cbdeccf3 | 553 | size_t len; |
05114c74 | 554 | |
90f3c272 | 555 | debug3("PAM: %s entering", __func__); |
05114c74 | 556 | buffer_init(&buffer); |
557 | *name = xstrdup(""); | |
558 | *info = xstrdup(""); | |
559 | *prompts = xmalloc(sizeof(char *)); | |
560 | **prompts = NULL; | |
561 | plen = 0; | |
562 | *echo_on = xmalloc(sizeof(u_int)); | |
563 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
564 | type = buffer_get_char(&buffer); | |
565 | msg = buffer_get_string(&buffer, NULL); | |
566 | switch (type) { | |
567 | case PAM_PROMPT_ECHO_ON: | |
568 | case PAM_PROMPT_ECHO_OFF: | |
569 | *num = 1; | |
cbdeccf3 | 570 | len = plen + strlen(msg) + 1; |
571 | **prompts = xrealloc(**prompts, len); | |
572 | plen += snprintf(**prompts + plen, len, "%s", msg); | |
05114c74 | 573 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
574 | xfree(msg); | |
575 | return (0); | |
576 | case PAM_ERROR_MSG: | |
577 | case PAM_TEXT_INFO: | |
578 | /* accumulate messages */ | |
4f1b45b4 | 579 | len = plen + strlen(msg) + 2; |
cbdeccf3 | 580 | **prompts = xrealloc(**prompts, len); |
4f1b45b4 | 581 | plen += snprintf(**prompts + plen, len, "%s\n", msg); |
05114c74 | 582 | xfree(msg); |
5daf7064 | 583 | break; |
05114c74 | 584 | case PAM_SUCCESS: |
585 | case PAM_AUTH_ERR: | |
586 | if (**prompts != NULL) { | |
587 | /* drain any accumulated messages */ | |
3eaf3960 | 588 | debug("PAM: %s", **prompts); |
589 | buffer_append(&loginmsg, **prompts, | |
590 | strlen(**prompts)); | |
05114c74 | 591 | xfree(**prompts); |
592 | **prompts = NULL; | |
593 | } | |
594 | if (type == PAM_SUCCESS) { | |
2212fc98 | 595 | import_environments(&buffer); |
05114c74 | 596 | *num = 0; |
597 | **echo_on = 0; | |
598 | ctxt->pam_done = 1; | |
599 | xfree(msg); | |
600 | return (0); | |
601 | } | |
602 | error("PAM: %s", msg); | |
e250770b | 603 | /* FALLTHROUGH */ |
5daf7064 | 604 | default: |
05114c74 | 605 | *num = 0; |
606 | **echo_on = 0; | |
607 | xfree(msg); | |
608 | ctxt->pam_done = -1; | |
609 | return (-1); | |
610 | } | |
a5c9cd31 | 611 | } |
05114c74 | 612 | return (-1); |
a5c9cd31 | 613 | } |
614 | ||
05114c74 | 615 | /* XXX - see also comment in auth-chall.c:verify_response */ |
616 | static int | |
617 | sshpam_respond(void *ctx, u_int num, char **resp) | |
a5c9cd31 | 618 | { |
05114c74 | 619 | Buffer buffer; |
620 | struct pam_ctxt *ctxt = ctx; | |
621 | ||
90f3c272 | 622 | debug2("PAM: %s entering, %d responses", __func__, num); |
05114c74 | 623 | switch (ctxt->pam_done) { |
624 | case 1: | |
625 | sshpam_authenticated = 1; | |
626 | return (0); | |
627 | case 0: | |
628 | break; | |
629 | default: | |
630 | return (-1); | |
a5c9cd31 | 631 | } |
05114c74 | 632 | if (num != 1) { |
633 | error("PAM: expected one response, got %u", num); | |
634 | return (-1); | |
635 | } | |
636 | buffer_init(&buffer); | |
637 | buffer_put_cstring(&buffer, *resp); | |
7b2a0de3 | 638 | if (ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer) == -1) { |
639 | buffer_free(&buffer); | |
640 | return (-1); | |
641 | } | |
05114c74 | 642 | buffer_free(&buffer); |
643 | return (1); | |
a5c9cd31 | 644 | } |
645 | ||
05114c74 | 646 | static void |
647 | sshpam_free_ctx(void *ctxtp) | |
a5c9cd31 | 648 | { |
05114c74 | 649 | struct pam_ctxt *ctxt = ctxtp; |
39ce53de | 650 | |
90f3c272 | 651 | debug3("PAM: %s entering", __func__); |
c6630044 | 652 | sshpam_thread_cleanup(); |
05114c74 | 653 | xfree(ctxt); |
654 | /* | |
655 | * We don't call sshpam_cleanup() here because we may need the PAM | |
656 | * handle at a later stage, e.g. when setting up a session. It's | |
657 | * still on the cleanup list, so pam_end() *will* be called before | |
658 | * the server process terminates. | |
659 | */ | |
ad55cd03 | 660 | } |
661 | ||
05114c74 | 662 | KbdintDevice sshpam_device = { |
663 | "pam", | |
664 | sshpam_init_ctx, | |
665 | sshpam_query, | |
666 | sshpam_respond, | |
667 | sshpam_free_ctx | |
668 | }; | |
669 | ||
670 | KbdintDevice mm_sshpam_device = { | |
671 | "pam", | |
672 | mm_sshpam_init_ctx, | |
673 | mm_sshpam_query, | |
674 | mm_sshpam_respond, | |
675 | mm_sshpam_free_ctx | |
676 | }; | |
2919e060 | 677 | |
2b87da3b | 678 | /* |
05114c74 | 679 | * This replaces auth-pam.c |
ad55cd03 | 680 | */ |
05114c74 | 681 | void |
529d73ab | 682 | start_pam(Authctxt *authctxt) |
ad55cd03 | 683 | { |
817e6d38 | 684 | if (!options.use_pam) |
685 | fatal("PAM: initialisation requested when UsePAM=no"); | |
686 | ||
529d73ab | 687 | if (sshpam_init(authctxt) == -1) |
05114c74 | 688 | fatal("PAM: initialisation failed"); |
a5c9cd31 | 689 | } |
690 | ||
05114c74 | 691 | void |
692 | finish_pam(void) | |
a5c9cd31 | 693 | { |
c6630044 | 694 | sshpam_cleanup(); |
a5c9cd31 | 695 | } |
696 | ||
5b9e2464 | 697 | u_int |
698 | do_pam_account(void) | |
a5c9cd31 | 699 | { |
b3ef7fb7 | 700 | if (sshpam_account_status != -1) |
701 | return (sshpam_account_status); | |
702 | ||
5b9e2464 | 703 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
90f3c272 | 704 | debug3("PAM: %s pam_acct_mgmt = %d", __func__, sshpam_err); |
b3ef7fb7 | 705 | |
706 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) { | |
707 | sshpam_account_status = 0; | |
708 | return (sshpam_account_status); | |
5b9e2464 | 709 | } |
710 | ||
b3ef7fb7 | 711 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) |
712 | pam_password_change_required(1); | |
713 | ||
714 | sshpam_account_status = 1; | |
715 | return (sshpam_account_status); | |
05114c74 | 716 | } |
46c76c63 | 717 | |
49e82bb9 | 718 | void |
719 | do_pam_set_tty(const char *tty) | |
720 | { | |
26b3608b | 721 | if (tty != NULL) { |
722 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
723 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
724 | if (sshpam_err != PAM_SUCCESS) | |
725 | fatal("PAM: failed to set PAM_TTY: %s", | |
726 | pam_strerror(sshpam_handle, sshpam_err)); | |
727 | } | |
05114c74 | 728 | } |
cbd7492e | 729 | |
05114c74 | 730 | void |
731 | do_pam_setcred(int init) | |
732 | { | |
733 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
734 | (const void *)&null_conv); | |
735 | if (sshpam_err != PAM_SUCCESS) | |
736 | fatal("PAM: failed to set PAM_CONV: %s", | |
737 | pam_strerror(sshpam_handle, sshpam_err)); | |
738 | if (init) { | |
739 | debug("PAM: establishing credentials"); | |
740 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
741 | } else { | |
742 | debug("PAM: reinitializing credentials"); | |
743 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
744 | } | |
745 | if (sshpam_err == PAM_SUCCESS) { | |
746 | sshpam_cred_established = 1; | |
747 | return; | |
748 | } | |
749 | if (sshpam_authenticated) | |
750 | fatal("PAM: pam_setcred(): %s", | |
751 | pam_strerror(sshpam_handle, sshpam_err)); | |
752 | else | |
753 | debug("PAM: pam_setcred(): %s", | |
754 | pam_strerror(sshpam_handle, sshpam_err)); | |
a5c9cd31 | 755 | } |
756 | ||
05114c74 | 757 | static int |
3eaf3960 | 758 | pam_tty_conv(int n, const struct pam_message **msg, |
5b9e2464 | 759 | struct pam_response **resp, void *data) |
ee48c949 | 760 | { |
05114c74 | 761 | char input[PAM_MAX_MSG_SIZE]; |
0a23d79f | 762 | struct pam_response *reply; |
ee48c949 | 763 | int i; |
764 | ||
60922169 | 765 | debug3("PAM: %s called with %d messages", __func__, n); |
766 | ||
0a23d79f | 767 | *resp = NULL; |
768 | ||
3eaf3960 | 769 | if (n <= 0 || n > PAM_MAX_NUM_MSG || !isatty(STDIN_FILENO)) |
05114c74 | 770 | return (PAM_CONV_ERR); |
0a23d79f | 771 | |
772 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
773 | return (PAM_CONV_ERR); | |
774 | memset(reply, 0, n * sizeof(*reply)); | |
775 | ||
05114c74 | 776 | for (i = 0; i < n; ++i) { |
777 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
778 | case PAM_PROMPT_ECHO_OFF: | |
0a23d79f | 779 | reply[i].resp = |
aff51935 | 780 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
05114c74 | 781 | RP_ALLOW_STDIN); |
0a23d79f | 782 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 783 | break; |
784 | case PAM_PROMPT_ECHO_ON: | |
74117b26 | 785 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
05114c74 | 786 | fgets(input, sizeof input, stdin); |
0a23d79f | 787 | reply[i].resp = xstrdup(input); |
788 | reply[i].resp_retcode = PAM_SUCCESS; | |
05114c74 | 789 | break; |
790 | case PAM_ERROR_MSG: | |
791 | case PAM_TEXT_INFO: | |
74117b26 | 792 | fprintf(stderr, "%s\n", PAM_MSG_MEMBER(msg, i, msg)); |
0a23d79f | 793 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 794 | break; |
795 | default: | |
796 | goto fail; | |
797 | } | |
ee48c949 | 798 | } |
0a23d79f | 799 | *resp = reply; |
05114c74 | 800 | return (PAM_SUCCESS); |
0a23d79f | 801 | |
05114c74 | 802 | fail: |
0a23d79f | 803 | for(i = 0; i < n; i++) { |
804 | if (reply[i].resp != NULL) | |
805 | xfree(reply[i].resp); | |
806 | } | |
807 | xfree(reply); | |
05114c74 | 808 | return (PAM_CONV_ERR); |
ee48c949 | 809 | } |
810 | ||
3eaf3960 | 811 | static struct pam_conv tty_conv = { pam_tty_conv, NULL }; |
812 | ||
05114c74 | 813 | /* |
814 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
815 | * support that | |
816 | */ | |
817 | void | |
818 | do_pam_chauthtok(void) | |
a5c9cd31 | 819 | { |
05114c74 | 820 | if (use_privsep) |
5b9e2464 | 821 | fatal("Password expired (unable to change with privsep)"); |
05114c74 | 822 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
3eaf3960 | 823 | (const void *)&tty_conv); |
05114c74 | 824 | if (sshpam_err != PAM_SUCCESS) |
825 | fatal("PAM: failed to set PAM_CONV: %s", | |
826 | pam_strerror(sshpam_handle, sshpam_err)); | |
827 | debug("PAM: changing password"); | |
828 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
829 | if (sshpam_err != PAM_SUCCESS) | |
830 | fatal("PAM: pam_chauthtok(): %s", | |
831 | pam_strerror(sshpam_handle, sshpam_err)); | |
5daf7064 | 832 | } |
833 | ||
ef687c66 | 834 | static int |
835 | pam_store_conv(int n, const struct pam_message **msg, | |
836 | struct pam_response **resp, void *data) | |
837 | { | |
838 | struct pam_response *reply; | |
839 | int i; | |
840 | size_t len; | |
841 | ||
842 | debug3("PAM: %s called with %d messages", __func__, n); | |
843 | *resp = NULL; | |
844 | ||
845 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
846 | return (PAM_CONV_ERR); | |
847 | ||
848 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
849 | return (PAM_CONV_ERR); | |
850 | memset(reply, 0, n * sizeof(*reply)); | |
851 | ||
852 | for (i = 0; i < n; ++i) { | |
853 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
854 | case PAM_ERROR_MSG: | |
855 | case PAM_TEXT_INFO: | |
856 | len = strlen(PAM_MSG_MEMBER(msg, i, msg)); | |
857 | buffer_append(&loginmsg, PAM_MSG_MEMBER(msg, i, msg), len); | |
858 | buffer_append(&loginmsg, "\n", 1 ); | |
859 | reply[i].resp_retcode = PAM_SUCCESS; | |
860 | break; | |
861 | default: | |
862 | goto fail; | |
863 | } | |
864 | } | |
865 | *resp = reply; | |
866 | return (PAM_SUCCESS); | |
867 | ||
868 | fail: | |
869 | for(i = 0; i < n; i++) { | |
870 | if (reply[i].resp != NULL) | |
871 | xfree(reply[i].resp); | |
872 | } | |
873 | xfree(reply); | |
874 | return (PAM_CONV_ERR); | |
875 | } | |
876 | ||
877 | static struct pam_conv store_conv = { pam_store_conv, NULL }; | |
878 | ||
3eaf3960 | 879 | void |
880 | do_pam_session(void) | |
881 | { | |
dd1fb864 | 882 | debug3("PAM: opening session"); |
aff51935 | 883 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
ef687c66 | 884 | (const void *)&store_conv); |
3eaf3960 | 885 | if (sshpam_err != PAM_SUCCESS) |
886 | fatal("PAM: failed to set PAM_CONV: %s", | |
887 | pam_strerror(sshpam_handle, sshpam_err)); | |
888 | sshpam_err = pam_open_session(sshpam_handle, 0); | |
889 | if (sshpam_err != PAM_SUCCESS) | |
890 | fatal("PAM: pam_open_session(): %s", | |
891 | pam_strerror(sshpam_handle, sshpam_err)); | |
892 | sshpam_session_open = 1; | |
893 | } | |
894 | ||
aff51935 | 895 | /* |
749560dd | 896 | * Set a PAM environment string. We need to do this so that the session |
897 | * modules can handle things like Kerberos/GSI credentials that appear | |
898 | * during the ssh authentication process. | |
899 | */ | |
749560dd | 900 | int |
aff51935 | 901 | do_pam_putenv(char *name, char *value) |
749560dd | 902 | { |
749560dd | 903 | int ret = 1; |
b6453d99 | 904 | #ifdef HAVE_PAM_PUTENV |
263c65df | 905 | char *compound; |
906 | size_t len; | |
907 | ||
908 | len = strlen(name) + strlen(value) + 2; | |
909 | compound = xmalloc(len); | |
910 | ||
911 | snprintf(compound, len, "%s=%s", name, value); | |
912 | ret = pam_putenv(sshpam_handle, compound); | |
913 | xfree(compound); | |
749560dd | 914 | #endif |
263c65df | 915 | |
749560dd | 916 | return (ret); |
917 | } | |
918 | ||
2212fc98 | 919 | char ** |
920 | fetch_pam_child_environment(void) | |
921 | { | |
922 | return sshpam_env; | |
923 | } | |
924 | ||
05114c74 | 925 | char ** |
926 | fetch_pam_environment(void) | |
927 | { | |
05114c74 | 928 | return (pam_getenvlist(sshpam_handle)); |
05114c74 | 929 | } |
5c377b3b | 930 | |
05114c74 | 931 | void |
932 | free_pam_environment(char **env) | |
933 | { | |
934 | char **envp; | |
5daf7064 | 935 | |
0eb6370a | 936 | if (env == NULL) |
937 | return; | |
938 | ||
05114c74 | 939 | for (envp = env; *envp; envp++) |
940 | xfree(*envp); | |
941 | xfree(env); | |
a5c9cd31 | 942 | } |
943 | ||
944 | #endif /* USE_PAM */ |