]>
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 | |
05114c74 | 37 | #include <security/pam_appl.h> |
38 | ||
fde58bd4 | 39 | #include "auth.h" |
5c377b3b | 40 | #include "auth-pam.h" |
05114c74 | 41 | #include "buffer.h" |
42 | #include "bufaux.h" | |
42f11eb2 | 43 | #include "canohost.h" |
05114c74 | 44 | #include "log.h" |
45 | #include "monitor_wrap.h" | |
46 | #include "msg.h" | |
47 | #include "packet.h" | |
42f11eb2 | 48 | #include "readpass.h" |
05114c74 | 49 | #include "servconf.h" |
50 | #include "ssh2.h" | |
51 | #include "xmalloc.h" | |
5b9e2464 | 52 | #include "auth-options.h" |
a5c9cd31 | 53 | |
7fceb20d | 54 | extern ServerOptions options; |
55 | ||
05114c74 | 56 | #define __unused |
277f55cf | 57 | |
05114c74 | 58 | #ifdef USE_POSIX_THREADS |
59 | #include <pthread.h> | |
60 | /* | |
61 | * Avoid namespace clash when *not* using pthreads for systems *with* | |
62 | * pthreads, which unconditionally define pthread_t via sys/types.h | |
63 | * (e.g. Linux) | |
64 | */ | |
65 | typedef pthread_t sp_pthread_t; | |
66 | #else | |
67 | /* | |
68 | * Simulate threads with processes. | |
69 | */ | |
70 | typedef pid_t sp_pthread_t; | |
a5c9cd31 | 71 | |
05114c74 | 72 | static void |
73 | pthread_exit(void *value __unused) | |
74 | { | |
75 | _exit(0); | |
76 | } | |
5daf7064 | 77 | |
05114c74 | 78 | static int |
79 | pthread_create(sp_pthread_t *thread, const void *attr __unused, | |
80 | void *(*thread_start)(void *), void *arg) | |
81 | { | |
82 | pid_t pid; | |
83 | ||
84 | switch ((pid = fork())) { | |
85 | case -1: | |
86 | error("fork(): %s", strerror(errno)); | |
87 | return (-1); | |
88 | case 0: | |
89 | thread_start(arg); | |
90 | _exit(1); | |
91 | default: | |
92 | *thread = pid; | |
93 | return (0); | |
94 | } | |
95 | } | |
a5c9cd31 | 96 | |
05114c74 | 97 | static int |
98 | pthread_cancel(sp_pthread_t thread) | |
8c9fe09e | 99 | { |
05114c74 | 100 | return (kill(thread, SIGTERM)); |
8c9fe09e | 101 | } |
102 | ||
05114c74 | 103 | static int |
104 | pthread_join(sp_pthread_t thread, void **value __unused) | |
8c9fe09e | 105 | { |
05114c74 | 106 | int status; |
107 | ||
108 | waitpid(thread, &status, 0); | |
109 | return (status); | |
8c9fe09e | 110 | } |
05114c74 | 111 | #endif |
112 | ||
113 | ||
114 | static pam_handle_t *sshpam_handle; | |
115 | static int sshpam_err; | |
116 | static int sshpam_authenticated; | |
117 | static int sshpam_new_authtok_reqd; | |
118 | static int sshpam_session_open; | |
119 | static int sshpam_cred_established; | |
120 | ||
121 | struct pam_ctxt { | |
122 | sp_pthread_t pam_thread; | |
123 | int pam_psock; | |
124 | int pam_csock; | |
125 | int pam_done; | |
126 | }; | |
127 | ||
128 | static void sshpam_free_ctx(void *); | |
ad55cd03 | 129 | |
130 | /* | |
05114c74 | 131 | * Conversation function for authentication thread. |
ad55cd03 | 132 | */ |
05114c74 | 133 | static int |
5b9e2464 | 134 | sshpam_thread_conv(int n, const struct pam_message **msg, |
135 | struct pam_response **resp, void *data) | |
a5c9cd31 | 136 | { |
05114c74 | 137 | Buffer buffer; |
138 | struct pam_ctxt *ctxt; | |
139 | int i; | |
140 | ||
141 | ctxt = data; | |
142 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
143 | return (PAM_CONV_ERR); | |
144 | *resp = xmalloc(n * sizeof **resp); | |
145 | buffer_init(&buffer); | |
146 | for (i = 0; i < n; ++i) { | |
147 | resp[i]->resp_retcode = 0; | |
148 | resp[i]->resp = NULL; | |
149 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
150 | case PAM_PROMPT_ECHO_OFF: | |
151 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); | |
152 | ssh_msg_send(ctxt->pam_csock, | |
153 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
154 | ssh_msg_recv(ctxt->pam_csock, &buffer); | |
155 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) | |
156 | goto fail; | |
157 | resp[i]->resp = buffer_get_string(&buffer, NULL); | |
158 | break; | |
159 | case PAM_PROMPT_ECHO_ON: | |
160 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); | |
161 | ssh_msg_send(ctxt->pam_csock, | |
162 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
163 | ssh_msg_recv(ctxt->pam_csock, &buffer); | |
164 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) | |
165 | goto fail; | |
166 | resp[i]->resp = buffer_get_string(&buffer, NULL); | |
167 | break; | |
168 | case PAM_ERROR_MSG: | |
169 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); | |
170 | ssh_msg_send(ctxt->pam_csock, | |
171 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
172 | break; | |
173 | case PAM_TEXT_INFO: | |
174 | buffer_put_cstring(&buffer, PAM_MSG_MEMBER(msg, i, msg)); | |
175 | ssh_msg_send(ctxt->pam_csock, | |
176 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
177 | break; | |
178 | default: | |
179 | goto fail; | |
a5c9cd31 | 180 | } |
05114c74 | 181 | buffer_clear(&buffer); |
a5c9cd31 | 182 | } |
05114c74 | 183 | buffer_free(&buffer); |
184 | return (PAM_SUCCESS); | |
185 | fail: | |
186 | while (i) | |
187 | xfree(resp[--i]); | |
188 | xfree(*resp); | |
189 | *resp = NULL; | |
190 | buffer_free(&buffer); | |
191 | return (PAM_CONV_ERR); | |
192 | } | |
a5c9cd31 | 193 | |
05114c74 | 194 | /* |
195 | * Authentication thread. | |
196 | */ | |
197 | static void * | |
198 | sshpam_thread(void *ctxtp) | |
199 | { | |
200 | struct pam_ctxt *ctxt = ctxtp; | |
201 | Buffer buffer; | |
202 | struct pam_conv sshpam_conv = { sshpam_thread_conv, ctxt }; | |
203 | #ifndef USE_POSIX_THREADS | |
204 | const char *pam_user; | |
205 | ||
206 | pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user); | |
207 | setproctitle("%s [pam]", pam_user); | |
208 | #endif | |
a5c9cd31 | 209 | |
05114c74 | 210 | buffer_init(&buffer); |
211 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
212 | (const void *)&sshpam_conv); | |
213 | if (sshpam_err != PAM_SUCCESS) | |
214 | goto auth_fail; | |
215 | sshpam_err = pam_authenticate(sshpam_handle, 0); | |
216 | if (sshpam_err != PAM_SUCCESS) | |
217 | goto auth_fail; | |
05114c74 | 218 | buffer_put_cstring(&buffer, "OK"); |
219 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); | |
220 | buffer_free(&buffer); | |
221 | pthread_exit(NULL); | |
222 | ||
223 | auth_fail: | |
224 | buffer_put_cstring(&buffer, | |
225 | pam_strerror(sshpam_handle, sshpam_err)); | |
226 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); | |
227 | buffer_free(&buffer); | |
228 | pthread_exit(NULL); | |
229 | ||
230 | return (NULL); /* Avoid warning for non-pthread case */ | |
a5c9cd31 | 231 | } |
232 | ||
05114c74 | 233 | static void |
234 | sshpam_thread_cleanup(void *ctxtp) | |
a5c9cd31 | 235 | { |
05114c74 | 236 | struct pam_ctxt *ctxt = ctxtp; |
a5c9cd31 | 237 | |
05114c74 | 238 | pthread_cancel(ctxt->pam_thread); |
239 | pthread_join(ctxt->pam_thread, NULL); | |
240 | close(ctxt->pam_psock); | |
241 | close(ctxt->pam_csock); | |
242 | } | |
a5c9cd31 | 243 | |
05114c74 | 244 | static int |
5b9e2464 | 245 | sshpam_null_conv(int n, const struct pam_message **msg, |
246 | struct pam_response **resp, void *data) | |
05114c74 | 247 | { |
05114c74 | 248 | return (PAM_CONV_ERR); |
249 | } | |
a5c9cd31 | 250 | |
05114c74 | 251 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
252 | ||
253 | static void | |
254 | sshpam_cleanup(void *arg) | |
255 | { | |
256 | (void)arg; | |
257 | debug("PAM: cleanup"); | |
258 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); | |
259 | if (sshpam_cred_established) { | |
260 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
261 | sshpam_cred_established = 0; | |
262 | } | |
263 | if (sshpam_session_open) { | |
264 | pam_close_session(sshpam_handle, PAM_SILENT); | |
265 | sshpam_session_open = 0; | |
a5c9cd31 | 266 | } |
05114c74 | 267 | sshpam_authenticated = sshpam_new_authtok_reqd = 0; |
268 | pam_end(sshpam_handle, sshpam_err); | |
269 | sshpam_handle = NULL; | |
a5c9cd31 | 270 | } |
271 | ||
05114c74 | 272 | static int |
273 | sshpam_init(const char *user) | |
a5c9cd31 | 274 | { |
05114c74 | 275 | extern u_int utmp_len; |
276 | const char *pam_rhost, *pam_user; | |
277 | ||
278 | if (sshpam_handle != NULL) { | |
279 | /* We already have a PAM context; check if the user matches */ | |
280 | sshpam_err = pam_get_item(sshpam_handle, | |
281 | PAM_USER, (const void **)&pam_user); | |
282 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) | |
283 | return (0); | |
284 | fatal_remove_cleanup(sshpam_cleanup, NULL); | |
285 | pam_end(sshpam_handle, sshpam_err); | |
286 | sshpam_handle = NULL; | |
287 | } | |
288 | debug("PAM: initializing for \"%s\"", user); | |
289 | sshpam_err = pam_start("sshd", user, &null_conv, &sshpam_handle); | |
5ff453c0 | 290 | if (sshpam_err != PAM_SUCCESS) { |
291 | pam_end(sshpam_handle, sshpam_err); | |
292 | sshpam_handle = NULL; | |
05114c74 | 293 | return (-1); |
5ff453c0 | 294 | } |
c5a7d788 | 295 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
05d62329 | 296 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
05114c74 | 297 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
5ff453c0 | 298 | if (sshpam_err != PAM_SUCCESS) { |
5b9e2464 | 299 | pam_end(sshpam_handle, sshpam_err); |
5ff453c0 | 300 | sshpam_handle = NULL; |
301 | return (-1); | |
302 | } | |
303 | #ifdef PAM_TTY_KLUDGE | |
304 | /* | |
305 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
306 | * sshd doesn't set the tty until too late in the auth process and | |
307 | * may not even set one (for tty-less connections) | |
308 | */ | |
309 | debug("PAM: setting PAM_TTY to \"ssh\""); | |
310 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
05114c74 | 311 | if (sshpam_err != PAM_SUCCESS) { |
312 | pam_end(sshpam_handle, sshpam_err); | |
313 | sshpam_handle = NULL; | |
314 | return (-1); | |
a5c9cd31 | 315 | } |
5ff453c0 | 316 | #endif |
05114c74 | 317 | fatal_add_cleanup(sshpam_cleanup, NULL); |
318 | return (0); | |
a5c9cd31 | 319 | } |
320 | ||
05114c74 | 321 | static void * |
322 | sshpam_init_ctx(Authctxt *authctxt) | |
a5c9cd31 | 323 | { |
05114c74 | 324 | struct pam_ctxt *ctxt; |
325 | int socks[2]; | |
2b87da3b | 326 | |
7fceb20d | 327 | /* Refuse to start if we don't have PAM enabled */ |
328 | if (!options.use_pam) | |
329 | return NULL; | |
330 | ||
05114c74 | 331 | /* Initialize PAM */ |
332 | if (sshpam_init(authctxt->user) == -1) { | |
333 | error("PAM: initialization failed"); | |
334 | return (NULL); | |
335 | } | |
5c377b3b | 336 | |
05114c74 | 337 | ctxt = xmalloc(sizeof *ctxt); |
338 | ctxt->pam_done = 0; | |
339 | ||
340 | /* Start the authentication thread */ | |
341 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
342 | error("PAM: failed create sockets: %s", strerror(errno)); | |
343 | xfree(ctxt); | |
344 | return (NULL); | |
345 | } | |
346 | ctxt->pam_psock = socks[0]; | |
347 | ctxt->pam_csock = socks[1]; | |
348 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
349 | error("PAM: failed to start authentication thread: %s", | |
350 | strerror(errno)); | |
351 | close(socks[0]); | |
352 | close(socks[1]); | |
353 | xfree(ctxt); | |
354 | return (NULL); | |
a5c9cd31 | 355 | } |
05114c74 | 356 | fatal_add_cleanup(sshpam_thread_cleanup, ctxt); |
357 | return (ctxt); | |
358 | } | |
a5c9cd31 | 359 | |
05114c74 | 360 | static int |
361 | sshpam_query(void *ctx, char **name, char **info, | |
362 | u_int *num, char ***prompts, u_int **echo_on) | |
363 | { | |
364 | Buffer buffer; | |
365 | struct pam_ctxt *ctxt = ctx; | |
366 | size_t plen; | |
367 | u_char type; | |
368 | char *msg; | |
cbdeccf3 | 369 | size_t len; |
05114c74 | 370 | |
371 | buffer_init(&buffer); | |
372 | *name = xstrdup(""); | |
373 | *info = xstrdup(""); | |
374 | *prompts = xmalloc(sizeof(char *)); | |
375 | **prompts = NULL; | |
376 | plen = 0; | |
377 | *echo_on = xmalloc(sizeof(u_int)); | |
378 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
379 | type = buffer_get_char(&buffer); | |
380 | msg = buffer_get_string(&buffer, NULL); | |
381 | switch (type) { | |
382 | case PAM_PROMPT_ECHO_ON: | |
383 | case PAM_PROMPT_ECHO_OFF: | |
384 | *num = 1; | |
cbdeccf3 | 385 | len = plen + strlen(msg) + 1; |
386 | **prompts = xrealloc(**prompts, len); | |
387 | plen += snprintf(**prompts + plen, len, "%s", msg); | |
05114c74 | 388 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
389 | xfree(msg); | |
390 | return (0); | |
391 | case PAM_ERROR_MSG: | |
392 | case PAM_TEXT_INFO: | |
393 | /* accumulate messages */ | |
cbdeccf3 | 394 | len = plen + strlen(msg) + 1; |
395 | **prompts = xrealloc(**prompts, len); | |
396 | plen += snprintf(**prompts + plen, len, "%s", msg); | |
05114c74 | 397 | xfree(msg); |
5daf7064 | 398 | break; |
05114c74 | 399 | case PAM_SUCCESS: |
400 | case PAM_AUTH_ERR: | |
401 | if (**prompts != NULL) { | |
402 | /* drain any accumulated messages */ | |
403 | #if 0 /* XXX - not compatible with privsep */ | |
404 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
405 | packet_put_cstring(**prompts); | |
406 | packet_put_cstring(""); | |
407 | packet_send(); | |
408 | packet_write_wait(); | |
e108cd93 | 409 | #endif |
05114c74 | 410 | xfree(**prompts); |
411 | **prompts = NULL; | |
412 | } | |
413 | if (type == PAM_SUCCESS) { | |
414 | *num = 0; | |
415 | **echo_on = 0; | |
416 | ctxt->pam_done = 1; | |
417 | xfree(msg); | |
418 | return (0); | |
419 | } | |
420 | error("PAM: %s", msg); | |
5daf7064 | 421 | default: |
05114c74 | 422 | *num = 0; |
423 | **echo_on = 0; | |
424 | xfree(msg); | |
425 | ctxt->pam_done = -1; | |
426 | return (-1); | |
427 | } | |
a5c9cd31 | 428 | } |
05114c74 | 429 | return (-1); |
a5c9cd31 | 430 | } |
431 | ||
05114c74 | 432 | /* XXX - see also comment in auth-chall.c:verify_response */ |
433 | static int | |
434 | sshpam_respond(void *ctx, u_int num, char **resp) | |
a5c9cd31 | 435 | { |
05114c74 | 436 | Buffer buffer; |
437 | struct pam_ctxt *ctxt = ctx; | |
438 | ||
439 | debug2("PAM: %s", __func__); | |
440 | switch (ctxt->pam_done) { | |
441 | case 1: | |
442 | sshpam_authenticated = 1; | |
443 | return (0); | |
444 | case 0: | |
445 | break; | |
446 | default: | |
447 | return (-1); | |
a5c9cd31 | 448 | } |
05114c74 | 449 | if (num != 1) { |
450 | error("PAM: expected one response, got %u", num); | |
451 | return (-1); | |
452 | } | |
453 | buffer_init(&buffer); | |
454 | buffer_put_cstring(&buffer, *resp); | |
455 | ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer); | |
456 | buffer_free(&buffer); | |
457 | return (1); | |
a5c9cd31 | 458 | } |
459 | ||
05114c74 | 460 | static void |
461 | sshpam_free_ctx(void *ctxtp) | |
a5c9cd31 | 462 | { |
05114c74 | 463 | struct pam_ctxt *ctxt = ctxtp; |
39ce53de | 464 | |
05114c74 | 465 | fatal_remove_cleanup(sshpam_thread_cleanup, ctxt); |
466 | sshpam_thread_cleanup(ctxtp); | |
467 | xfree(ctxt); | |
468 | /* | |
469 | * We don't call sshpam_cleanup() here because we may need the PAM | |
470 | * handle at a later stage, e.g. when setting up a session. It's | |
471 | * still on the cleanup list, so pam_end() *will* be called before | |
472 | * the server process terminates. | |
473 | */ | |
ad55cd03 | 474 | } |
475 | ||
05114c74 | 476 | KbdintDevice sshpam_device = { |
477 | "pam", | |
478 | sshpam_init_ctx, | |
479 | sshpam_query, | |
480 | sshpam_respond, | |
481 | sshpam_free_ctx | |
482 | }; | |
483 | ||
484 | KbdintDevice mm_sshpam_device = { | |
485 | "pam", | |
486 | mm_sshpam_init_ctx, | |
487 | mm_sshpam_query, | |
488 | mm_sshpam_respond, | |
489 | mm_sshpam_free_ctx | |
490 | }; | |
2919e060 | 491 | |
2b87da3b | 492 | /* |
05114c74 | 493 | * This replaces auth-pam.c |
ad55cd03 | 494 | */ |
05114c74 | 495 | void |
496 | start_pam(const char *user) | |
ad55cd03 | 497 | { |
817e6d38 | 498 | if (!options.use_pam) |
499 | fatal("PAM: initialisation requested when UsePAM=no"); | |
500 | ||
05114c74 | 501 | if (sshpam_init(user) == -1) |
502 | fatal("PAM: initialisation failed"); | |
a5c9cd31 | 503 | } |
504 | ||
05114c74 | 505 | void |
506 | finish_pam(void) | |
a5c9cd31 | 507 | { |
05114c74 | 508 | fatal_remove_cleanup(sshpam_cleanup, NULL); |
509 | sshpam_cleanup(NULL); | |
a5c9cd31 | 510 | } |
511 | ||
5b9e2464 | 512 | u_int |
513 | do_pam_account(void) | |
a5c9cd31 | 514 | { |
5b9e2464 | 515 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
516 | debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err); | |
517 | ||
518 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) | |
519 | return (0); | |
520 | ||
521 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) { | |
522 | sshpam_new_authtok_reqd = 1; | |
523 | ||
524 | /* Prevent forwardings until password changed */ | |
525 | no_port_forwarding_flag |= 2; | |
526 | no_agent_forwarding_flag |= 2; | |
527 | no_x11_forwarding_flag |= 2; | |
528 | } | |
529 | ||
05114c74 | 530 | return (1); |
531 | } | |
46c76c63 | 532 | |
05114c74 | 533 | void |
534 | do_pam_session(const char *user, const char *tty) | |
535 | { | |
536 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
537 | (const void *)&null_conv); | |
538 | if (sshpam_err != PAM_SUCCESS) | |
539 | fatal("PAM: failed to set PAM_CONV: %s", | |
540 | pam_strerror(sshpam_handle, sshpam_err)); | |
26b3608b | 541 | if (tty != NULL) { |
542 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
543 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
544 | if (sshpam_err != PAM_SUCCESS) | |
545 | fatal("PAM: failed to set PAM_TTY: %s", | |
546 | pam_strerror(sshpam_handle, sshpam_err)); | |
547 | } | |
05114c74 | 548 | sshpam_err = pam_open_session(sshpam_handle, 0); |
549 | if (sshpam_err != PAM_SUCCESS) | |
550 | fatal("PAM: pam_open_session(): %s", | |
551 | pam_strerror(sshpam_handle, sshpam_err)); | |
552 | sshpam_session_open = 1; | |
553 | } | |
cbd7492e | 554 | |
05114c74 | 555 | void |
556 | do_pam_setcred(int init) | |
557 | { | |
558 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
559 | (const void *)&null_conv); | |
560 | if (sshpam_err != PAM_SUCCESS) | |
561 | fatal("PAM: failed to set PAM_CONV: %s", | |
562 | pam_strerror(sshpam_handle, sshpam_err)); | |
563 | if (init) { | |
564 | debug("PAM: establishing credentials"); | |
565 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
566 | } else { | |
567 | debug("PAM: reinitializing credentials"); | |
568 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
569 | } | |
570 | if (sshpam_err == PAM_SUCCESS) { | |
571 | sshpam_cred_established = 1; | |
572 | return; | |
573 | } | |
574 | if (sshpam_authenticated) | |
575 | fatal("PAM: pam_setcred(): %s", | |
576 | pam_strerror(sshpam_handle, sshpam_err)); | |
577 | else | |
578 | debug("PAM: pam_setcred(): %s", | |
579 | pam_strerror(sshpam_handle, sshpam_err)); | |
a5c9cd31 | 580 | } |
581 | ||
05114c74 | 582 | int |
583 | is_pam_password_change_required(void) | |
a5c9cd31 | 584 | { |
05114c74 | 585 | return (sshpam_new_authtok_reqd); |
a5c9cd31 | 586 | } |
587 | ||
05114c74 | 588 | static int |
5b9e2464 | 589 | pam_chauthtok_conv(int n, const struct pam_message **msg, |
590 | struct pam_response **resp, void *data) | |
ee48c949 | 591 | { |
05114c74 | 592 | char input[PAM_MAX_MSG_SIZE]; |
ee48c949 | 593 | int i; |
594 | ||
05114c74 | 595 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
596 | return (PAM_CONV_ERR); | |
597 | *resp = xmalloc(n * sizeof **resp); | |
598 | for (i = 0; i < n; ++i) { | |
599 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
600 | case PAM_PROMPT_ECHO_OFF: | |
601 | resp[i]->resp = | |
602 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), | |
603 | RP_ALLOW_STDIN); | |
604 | resp[i]->resp_retcode = PAM_SUCCESS; | |
605 | break; | |
606 | case PAM_PROMPT_ECHO_ON: | |
607 | fputs(PAM_MSG_MEMBER(msg, i, msg), stderr); | |
608 | fgets(input, sizeof input, stdin); | |
609 | resp[i]->resp = xstrdup(input); | |
610 | resp[i]->resp_retcode = PAM_SUCCESS; | |
611 | break; | |
612 | case PAM_ERROR_MSG: | |
613 | case PAM_TEXT_INFO: | |
614 | fputs(PAM_MSG_MEMBER(msg, i, msg), stderr); | |
615 | resp[i]->resp_retcode = PAM_SUCCESS; | |
616 | break; | |
617 | default: | |
618 | goto fail; | |
619 | } | |
ee48c949 | 620 | } |
05114c74 | 621 | return (PAM_SUCCESS); |
622 | fail: | |
623 | while (i) | |
624 | xfree(resp[--i]); | |
625 | xfree(*resp); | |
626 | *resp = NULL; | |
627 | return (PAM_CONV_ERR); | |
ee48c949 | 628 | } |
629 | ||
05114c74 | 630 | /* |
631 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
632 | * support that | |
633 | */ | |
634 | void | |
635 | do_pam_chauthtok(void) | |
a5c9cd31 | 636 | { |
05114c74 | 637 | struct pam_conv pam_conv = { pam_chauthtok_conv, NULL }; |
638 | ||
639 | if (use_privsep) | |
5b9e2464 | 640 | fatal("Password expired (unable to change with privsep)"); |
05114c74 | 641 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
642 | (const void *)&pam_conv); | |
643 | if (sshpam_err != PAM_SUCCESS) | |
644 | fatal("PAM: failed to set PAM_CONV: %s", | |
645 | pam_strerror(sshpam_handle, sshpam_err)); | |
646 | debug("PAM: changing password"); | |
647 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
648 | if (sshpam_err != PAM_SUCCESS) | |
649 | fatal("PAM: pam_chauthtok(): %s", | |
650 | pam_strerror(sshpam_handle, sshpam_err)); | |
5daf7064 | 651 | } |
652 | ||
05114c74 | 653 | void |
654 | print_pam_messages(void) | |
5daf7064 | 655 | { |
05114c74 | 656 | /* XXX */ |
657 | } | |
2b87da3b | 658 | |
05114c74 | 659 | char ** |
660 | fetch_pam_environment(void) | |
661 | { | |
662 | #ifdef HAVE_PAM_GETENVLIST | |
663 | debug("PAM: retrieving environment"); | |
664 | return (pam_getenvlist(sshpam_handle)); | |
665 | #else | |
666 | return (NULL); | |
667 | #endif | |
668 | } | |
5c377b3b | 669 | |
05114c74 | 670 | void |
671 | free_pam_environment(char **env) | |
672 | { | |
673 | char **envp; | |
5daf7064 | 674 | |
0eb6370a | 675 | if (env == NULL) |
676 | return; | |
677 | ||
05114c74 | 678 | for (envp = env; *envp; envp++) |
679 | xfree(*envp); | |
680 | xfree(env); | |
a5c9cd31 | 681 | } |
682 | ||
683 | #endif /* USE_PAM */ |