]>
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 | ||
0a23d79f | 114 | static pam_handle_t *sshpam_handle = NULL; |
115 | static int sshpam_err = 0; | |
116 | static int sshpam_authenticated = 0; | |
117 | static int sshpam_new_authtok_reqd = 0; | |
118 | static int sshpam_session_open = 0; | |
119 | static int sshpam_cred_established = 0; | |
05114c74 | 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; | |
0a23d79f | 139 | struct pam_response *reply; |
05114c74 | 140 | int i; |
141 | ||
0a23d79f | 142 | *resp = NULL; |
143 | ||
05114c74 | 144 | ctxt = data; |
145 | if (n <= 0 || n > PAM_MAX_NUM_MSG) | |
146 | return (PAM_CONV_ERR); | |
0a23d79f | 147 | |
148 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
149 | return (PAM_CONV_ERR); | |
150 | memset(reply, 0, n * sizeof(*reply)); | |
151 | ||
05114c74 | 152 | buffer_init(&buffer); |
153 | for (i = 0; i < n; ++i) { | |
05114c74 | 154 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { |
155 | case PAM_PROMPT_ECHO_OFF: | |
0a23d79f | 156 | buffer_put_cstring(&buffer, |
157 | PAM_MSG_MEMBER(msg, i, msg)); | |
05114c74 | 158 | ssh_msg_send(ctxt->pam_csock, |
159 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
160 | ssh_msg_recv(ctxt->pam_csock, &buffer); | |
161 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) | |
162 | goto fail; | |
0a23d79f | 163 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 164 | break; |
165 | case PAM_PROMPT_ECHO_ON: | |
0a23d79f | 166 | buffer_put_cstring(&buffer, |
167 | PAM_MSG_MEMBER(msg, i, msg)); | |
05114c74 | 168 | ssh_msg_send(ctxt->pam_csock, |
169 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
170 | ssh_msg_recv(ctxt->pam_csock, &buffer); | |
171 | if (buffer_get_char(&buffer) != PAM_AUTHTOK) | |
172 | goto fail; | |
0a23d79f | 173 | reply[i].resp = buffer_get_string(&buffer, NULL); |
05114c74 | 174 | break; |
175 | case PAM_ERROR_MSG: | |
0a23d79f | 176 | buffer_put_cstring(&buffer, |
177 | PAM_MSG_MEMBER(msg, i, msg)); | |
05114c74 | 178 | ssh_msg_send(ctxt->pam_csock, |
179 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
180 | break; | |
181 | case PAM_TEXT_INFO: | |
0a23d79f | 182 | buffer_put_cstring(&buffer, |
183 | PAM_MSG_MEMBER(msg, i, msg)); | |
05114c74 | 184 | ssh_msg_send(ctxt->pam_csock, |
185 | PAM_MSG_MEMBER(msg, i, msg_style), &buffer); | |
186 | break; | |
187 | default: | |
188 | goto fail; | |
a5c9cd31 | 189 | } |
05114c74 | 190 | buffer_clear(&buffer); |
a5c9cd31 | 191 | } |
05114c74 | 192 | buffer_free(&buffer); |
0a23d79f | 193 | *resp = reply; |
05114c74 | 194 | return (PAM_SUCCESS); |
0a23d79f | 195 | |
05114c74 | 196 | fail: |
0a23d79f | 197 | for(i = 0; i < n; i++) { |
198 | if (reply[i].resp != NULL) | |
199 | xfree(reply[i].resp); | |
200 | } | |
201 | xfree(reply); | |
05114c74 | 202 | buffer_free(&buffer); |
203 | return (PAM_CONV_ERR); | |
204 | } | |
a5c9cd31 | 205 | |
05114c74 | 206 | /* |
207 | * Authentication thread. | |
208 | */ | |
209 | static void * | |
210 | sshpam_thread(void *ctxtp) | |
211 | { | |
212 | struct pam_ctxt *ctxt = ctxtp; | |
213 | Buffer buffer; | |
c53917a9 | 214 | struct pam_conv sshpam_conv; |
05114c74 | 215 | #ifndef USE_POSIX_THREADS |
216 | const char *pam_user; | |
217 | ||
218 | pam_get_item(sshpam_handle, PAM_USER, (const void **)&pam_user); | |
219 | setproctitle("%s [pam]", pam_user); | |
220 | #endif | |
a5c9cd31 | 221 | |
c53917a9 | 222 | sshpam_conv.conv = sshpam_thread_conv; |
223 | sshpam_conv.appdata_ptr = ctxt; | |
224 | ||
05114c74 | 225 | buffer_init(&buffer); |
226 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
227 | (const void *)&sshpam_conv); | |
228 | if (sshpam_err != PAM_SUCCESS) | |
229 | goto auth_fail; | |
230 | sshpam_err = pam_authenticate(sshpam_handle, 0); | |
231 | if (sshpam_err != PAM_SUCCESS) | |
232 | goto auth_fail; | |
05114c74 | 233 | buffer_put_cstring(&buffer, "OK"); |
234 | ssh_msg_send(ctxt->pam_csock, sshpam_err, &buffer); | |
235 | buffer_free(&buffer); | |
236 | pthread_exit(NULL); | |
237 | ||
238 | auth_fail: | |
239 | buffer_put_cstring(&buffer, | |
240 | pam_strerror(sshpam_handle, sshpam_err)); | |
241 | ssh_msg_send(ctxt->pam_csock, PAM_AUTH_ERR, &buffer); | |
242 | buffer_free(&buffer); | |
243 | pthread_exit(NULL); | |
244 | ||
245 | return (NULL); /* Avoid warning for non-pthread case */ | |
a5c9cd31 | 246 | } |
247 | ||
05114c74 | 248 | static void |
249 | sshpam_thread_cleanup(void *ctxtp) | |
a5c9cd31 | 250 | { |
05114c74 | 251 | struct pam_ctxt *ctxt = ctxtp; |
a5c9cd31 | 252 | |
05114c74 | 253 | pthread_cancel(ctxt->pam_thread); |
254 | pthread_join(ctxt->pam_thread, NULL); | |
255 | close(ctxt->pam_psock); | |
256 | close(ctxt->pam_csock); | |
257 | } | |
a5c9cd31 | 258 | |
05114c74 | 259 | static int |
5b9e2464 | 260 | sshpam_null_conv(int n, const struct pam_message **msg, |
261 | struct pam_response **resp, void *data) | |
05114c74 | 262 | { |
05114c74 | 263 | return (PAM_CONV_ERR); |
264 | } | |
a5c9cd31 | 265 | |
05114c74 | 266 | static struct pam_conv null_conv = { sshpam_null_conv, NULL }; |
267 | ||
268 | static void | |
269 | sshpam_cleanup(void *arg) | |
270 | { | |
271 | (void)arg; | |
272 | debug("PAM: cleanup"); | |
0a23d79f | 273 | if (sshpam_handle == NULL) |
274 | return; | |
05114c74 | 275 | pam_set_item(sshpam_handle, PAM_CONV, (const void *)&null_conv); |
276 | if (sshpam_cred_established) { | |
277 | pam_setcred(sshpam_handle, PAM_DELETE_CRED); | |
278 | sshpam_cred_established = 0; | |
279 | } | |
280 | if (sshpam_session_open) { | |
281 | pam_close_session(sshpam_handle, PAM_SILENT); | |
282 | sshpam_session_open = 0; | |
a5c9cd31 | 283 | } |
05114c74 | 284 | sshpam_authenticated = sshpam_new_authtok_reqd = 0; |
285 | pam_end(sshpam_handle, sshpam_err); | |
286 | sshpam_handle = NULL; | |
a5c9cd31 | 287 | } |
288 | ||
05114c74 | 289 | static int |
290 | sshpam_init(const char *user) | |
a5c9cd31 | 291 | { |
05114c74 | 292 | extern u_int utmp_len; |
74678fb9 | 293 | extern char *__progname; |
05114c74 | 294 | const char *pam_rhost, *pam_user; |
295 | ||
296 | if (sshpam_handle != NULL) { | |
297 | /* We already have a PAM context; check if the user matches */ | |
298 | sshpam_err = pam_get_item(sshpam_handle, | |
299 | PAM_USER, (const void **)&pam_user); | |
300 | if (sshpam_err == PAM_SUCCESS && strcmp(user, pam_user) == 0) | |
301 | return (0); | |
302 | fatal_remove_cleanup(sshpam_cleanup, NULL); | |
303 | pam_end(sshpam_handle, sshpam_err); | |
304 | sshpam_handle = NULL; | |
305 | } | |
306 | debug("PAM: initializing for \"%s\"", user); | |
01224183 | 307 | sshpam_err = |
308 | pam_start(SSHD_PAM_SERVICE, user, &null_conv, &sshpam_handle); | |
5ff453c0 | 309 | if (sshpam_err != PAM_SUCCESS) { |
310 | pam_end(sshpam_handle, sshpam_err); | |
311 | sshpam_handle = NULL; | |
05114c74 | 312 | return (-1); |
5ff453c0 | 313 | } |
c5a7d788 | 314 | pam_rhost = get_remote_name_or_ip(utmp_len, options.use_dns); |
05d62329 | 315 | debug("PAM: setting PAM_RHOST to \"%s\"", pam_rhost); |
05114c74 | 316 | sshpam_err = pam_set_item(sshpam_handle, PAM_RHOST, pam_rhost); |
5ff453c0 | 317 | if (sshpam_err != PAM_SUCCESS) { |
5b9e2464 | 318 | pam_end(sshpam_handle, sshpam_err); |
5ff453c0 | 319 | sshpam_handle = NULL; |
320 | return (-1); | |
321 | } | |
322 | #ifdef PAM_TTY_KLUDGE | |
323 | /* | |
324 | * Some silly PAM modules (e.g. pam_time) require a TTY to operate. | |
325 | * sshd doesn't set the tty until too late in the auth process and | |
326 | * may not even set one (for tty-less connections) | |
327 | */ | |
328 | debug("PAM: setting PAM_TTY to \"ssh\""); | |
329 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, "ssh"); | |
05114c74 | 330 | if (sshpam_err != PAM_SUCCESS) { |
331 | pam_end(sshpam_handle, sshpam_err); | |
332 | sshpam_handle = NULL; | |
333 | return (-1); | |
a5c9cd31 | 334 | } |
5ff453c0 | 335 | #endif |
05114c74 | 336 | fatal_add_cleanup(sshpam_cleanup, NULL); |
337 | return (0); | |
a5c9cd31 | 338 | } |
339 | ||
05114c74 | 340 | static void * |
341 | sshpam_init_ctx(Authctxt *authctxt) | |
a5c9cd31 | 342 | { |
05114c74 | 343 | struct pam_ctxt *ctxt; |
344 | int socks[2]; | |
2b87da3b | 345 | |
7fceb20d | 346 | /* Refuse to start if we don't have PAM enabled */ |
347 | if (!options.use_pam) | |
348 | return NULL; | |
349 | ||
05114c74 | 350 | /* Initialize PAM */ |
351 | if (sshpam_init(authctxt->user) == -1) { | |
352 | error("PAM: initialization failed"); | |
353 | return (NULL); | |
354 | } | |
5c377b3b | 355 | |
05114c74 | 356 | ctxt = xmalloc(sizeof *ctxt); |
357 | ctxt->pam_done = 0; | |
358 | ||
359 | /* Start the authentication thread */ | |
360 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, socks) == -1) { | |
361 | error("PAM: failed create sockets: %s", strerror(errno)); | |
362 | xfree(ctxt); | |
363 | return (NULL); | |
364 | } | |
365 | ctxt->pam_psock = socks[0]; | |
366 | ctxt->pam_csock = socks[1]; | |
367 | if (pthread_create(&ctxt->pam_thread, NULL, sshpam_thread, ctxt) == -1) { | |
368 | error("PAM: failed to start authentication thread: %s", | |
369 | strerror(errno)); | |
370 | close(socks[0]); | |
371 | close(socks[1]); | |
372 | xfree(ctxt); | |
373 | return (NULL); | |
a5c9cd31 | 374 | } |
05114c74 | 375 | fatal_add_cleanup(sshpam_thread_cleanup, ctxt); |
376 | return (ctxt); | |
377 | } | |
a5c9cd31 | 378 | |
05114c74 | 379 | static int |
380 | sshpam_query(void *ctx, char **name, char **info, | |
381 | u_int *num, char ***prompts, u_int **echo_on) | |
382 | { | |
383 | Buffer buffer; | |
384 | struct pam_ctxt *ctxt = ctx; | |
385 | size_t plen; | |
386 | u_char type; | |
387 | char *msg; | |
cbdeccf3 | 388 | size_t len; |
05114c74 | 389 | |
390 | buffer_init(&buffer); | |
391 | *name = xstrdup(""); | |
392 | *info = xstrdup(""); | |
393 | *prompts = xmalloc(sizeof(char *)); | |
394 | **prompts = NULL; | |
395 | plen = 0; | |
396 | *echo_on = xmalloc(sizeof(u_int)); | |
397 | while (ssh_msg_recv(ctxt->pam_psock, &buffer) == 0) { | |
398 | type = buffer_get_char(&buffer); | |
399 | msg = buffer_get_string(&buffer, NULL); | |
400 | switch (type) { | |
401 | case PAM_PROMPT_ECHO_ON: | |
402 | case PAM_PROMPT_ECHO_OFF: | |
403 | *num = 1; | |
cbdeccf3 | 404 | len = plen + strlen(msg) + 1; |
405 | **prompts = xrealloc(**prompts, len); | |
406 | plen += snprintf(**prompts + plen, len, "%s", msg); | |
05114c74 | 407 | **echo_on = (type == PAM_PROMPT_ECHO_ON); |
408 | xfree(msg); | |
409 | return (0); | |
410 | case PAM_ERROR_MSG: | |
411 | case PAM_TEXT_INFO: | |
412 | /* accumulate messages */ | |
cbdeccf3 | 413 | len = plen + strlen(msg) + 1; |
414 | **prompts = xrealloc(**prompts, len); | |
415 | plen += snprintf(**prompts + plen, len, "%s", msg); | |
05114c74 | 416 | xfree(msg); |
5daf7064 | 417 | break; |
05114c74 | 418 | case PAM_SUCCESS: |
419 | case PAM_AUTH_ERR: | |
420 | if (**prompts != NULL) { | |
421 | /* drain any accumulated messages */ | |
422 | #if 0 /* XXX - not compatible with privsep */ | |
423 | packet_start(SSH2_MSG_USERAUTH_BANNER); | |
424 | packet_put_cstring(**prompts); | |
425 | packet_put_cstring(""); | |
426 | packet_send(); | |
427 | packet_write_wait(); | |
e108cd93 | 428 | #endif |
05114c74 | 429 | xfree(**prompts); |
430 | **prompts = NULL; | |
431 | } | |
432 | if (type == PAM_SUCCESS) { | |
433 | *num = 0; | |
434 | **echo_on = 0; | |
435 | ctxt->pam_done = 1; | |
436 | xfree(msg); | |
437 | return (0); | |
438 | } | |
439 | error("PAM: %s", msg); | |
5daf7064 | 440 | default: |
05114c74 | 441 | *num = 0; |
442 | **echo_on = 0; | |
443 | xfree(msg); | |
444 | ctxt->pam_done = -1; | |
445 | return (-1); | |
446 | } | |
a5c9cd31 | 447 | } |
05114c74 | 448 | return (-1); |
a5c9cd31 | 449 | } |
450 | ||
05114c74 | 451 | /* XXX - see also comment in auth-chall.c:verify_response */ |
452 | static int | |
453 | sshpam_respond(void *ctx, u_int num, char **resp) | |
a5c9cd31 | 454 | { |
05114c74 | 455 | Buffer buffer; |
456 | struct pam_ctxt *ctxt = ctx; | |
457 | ||
458 | debug2("PAM: %s", __func__); | |
459 | switch (ctxt->pam_done) { | |
460 | case 1: | |
461 | sshpam_authenticated = 1; | |
462 | return (0); | |
463 | case 0: | |
464 | break; | |
465 | default: | |
466 | return (-1); | |
a5c9cd31 | 467 | } |
05114c74 | 468 | if (num != 1) { |
469 | error("PAM: expected one response, got %u", num); | |
470 | return (-1); | |
471 | } | |
472 | buffer_init(&buffer); | |
473 | buffer_put_cstring(&buffer, *resp); | |
474 | ssh_msg_send(ctxt->pam_psock, PAM_AUTHTOK, &buffer); | |
475 | buffer_free(&buffer); | |
476 | return (1); | |
a5c9cd31 | 477 | } |
478 | ||
05114c74 | 479 | static void |
480 | sshpam_free_ctx(void *ctxtp) | |
a5c9cd31 | 481 | { |
05114c74 | 482 | struct pam_ctxt *ctxt = ctxtp; |
39ce53de | 483 | |
05114c74 | 484 | fatal_remove_cleanup(sshpam_thread_cleanup, ctxt); |
485 | sshpam_thread_cleanup(ctxtp); | |
486 | xfree(ctxt); | |
487 | /* | |
488 | * We don't call sshpam_cleanup() here because we may need the PAM | |
489 | * handle at a later stage, e.g. when setting up a session. It's | |
490 | * still on the cleanup list, so pam_end() *will* be called before | |
491 | * the server process terminates. | |
492 | */ | |
ad55cd03 | 493 | } |
494 | ||
05114c74 | 495 | KbdintDevice sshpam_device = { |
496 | "pam", | |
497 | sshpam_init_ctx, | |
498 | sshpam_query, | |
499 | sshpam_respond, | |
500 | sshpam_free_ctx | |
501 | }; | |
502 | ||
503 | KbdintDevice mm_sshpam_device = { | |
504 | "pam", | |
505 | mm_sshpam_init_ctx, | |
506 | mm_sshpam_query, | |
507 | mm_sshpam_respond, | |
508 | mm_sshpam_free_ctx | |
509 | }; | |
2919e060 | 510 | |
2b87da3b | 511 | /* |
05114c74 | 512 | * This replaces auth-pam.c |
ad55cd03 | 513 | */ |
05114c74 | 514 | void |
515 | start_pam(const char *user) | |
ad55cd03 | 516 | { |
817e6d38 | 517 | if (!options.use_pam) |
518 | fatal("PAM: initialisation requested when UsePAM=no"); | |
519 | ||
05114c74 | 520 | if (sshpam_init(user) == -1) |
521 | fatal("PAM: initialisation failed"); | |
a5c9cd31 | 522 | } |
523 | ||
05114c74 | 524 | void |
525 | finish_pam(void) | |
a5c9cd31 | 526 | { |
05114c74 | 527 | fatal_remove_cleanup(sshpam_cleanup, NULL); |
528 | sshpam_cleanup(NULL); | |
a5c9cd31 | 529 | } |
530 | ||
5b9e2464 | 531 | u_int |
532 | do_pam_account(void) | |
a5c9cd31 | 533 | { |
5b9e2464 | 534 | sshpam_err = pam_acct_mgmt(sshpam_handle, 0); |
535 | debug3("%s: pam_acct_mgmt = %d", __func__, sshpam_err); | |
536 | ||
537 | if (sshpam_err != PAM_SUCCESS && sshpam_err != PAM_NEW_AUTHTOK_REQD) | |
538 | return (0); | |
539 | ||
540 | if (sshpam_err == PAM_NEW_AUTHTOK_REQD) { | |
541 | sshpam_new_authtok_reqd = 1; | |
542 | ||
543 | /* Prevent forwardings until password changed */ | |
544 | no_port_forwarding_flag |= 2; | |
545 | no_agent_forwarding_flag |= 2; | |
546 | no_x11_forwarding_flag |= 2; | |
547 | } | |
548 | ||
05114c74 | 549 | return (1); |
550 | } | |
46c76c63 | 551 | |
05114c74 | 552 | void |
49e82bb9 | 553 | do_pam_session(void) |
05114c74 | 554 | { |
555 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
556 | (const void *)&null_conv); | |
557 | if (sshpam_err != PAM_SUCCESS) | |
558 | fatal("PAM: failed to set PAM_CONV: %s", | |
559 | pam_strerror(sshpam_handle, sshpam_err)); | |
49e82bb9 | 560 | sshpam_err = pam_open_session(sshpam_handle, 0); |
561 | if (sshpam_err != PAM_SUCCESS) | |
562 | fatal("PAM: pam_open_session(): %s", | |
563 | pam_strerror(sshpam_handle, sshpam_err)); | |
564 | sshpam_session_open = 1; | |
565 | } | |
566 | ||
567 | void | |
568 | do_pam_set_tty(const char *tty) | |
569 | { | |
26b3608b | 570 | if (tty != NULL) { |
571 | debug("PAM: setting PAM_TTY to \"%s\"", tty); | |
572 | sshpam_err = pam_set_item(sshpam_handle, PAM_TTY, tty); | |
573 | if (sshpam_err != PAM_SUCCESS) | |
574 | fatal("PAM: failed to set PAM_TTY: %s", | |
575 | pam_strerror(sshpam_handle, sshpam_err)); | |
576 | } | |
05114c74 | 577 | } |
cbd7492e | 578 | |
05114c74 | 579 | void |
580 | do_pam_setcred(int init) | |
581 | { | |
582 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, | |
583 | (const void *)&null_conv); | |
584 | if (sshpam_err != PAM_SUCCESS) | |
585 | fatal("PAM: failed to set PAM_CONV: %s", | |
586 | pam_strerror(sshpam_handle, sshpam_err)); | |
587 | if (init) { | |
588 | debug("PAM: establishing credentials"); | |
589 | sshpam_err = pam_setcred(sshpam_handle, PAM_ESTABLISH_CRED); | |
590 | } else { | |
591 | debug("PAM: reinitializing credentials"); | |
592 | sshpam_err = pam_setcred(sshpam_handle, PAM_REINITIALIZE_CRED); | |
593 | } | |
594 | if (sshpam_err == PAM_SUCCESS) { | |
595 | sshpam_cred_established = 1; | |
596 | return; | |
597 | } | |
598 | if (sshpam_authenticated) | |
599 | fatal("PAM: pam_setcred(): %s", | |
600 | pam_strerror(sshpam_handle, sshpam_err)); | |
601 | else | |
602 | debug("PAM: pam_setcred(): %s", | |
603 | pam_strerror(sshpam_handle, sshpam_err)); | |
a5c9cd31 | 604 | } |
605 | ||
05114c74 | 606 | int |
607 | is_pam_password_change_required(void) | |
a5c9cd31 | 608 | { |
05114c74 | 609 | return (sshpam_new_authtok_reqd); |
a5c9cd31 | 610 | } |
611 | ||
05114c74 | 612 | static int |
5b9e2464 | 613 | pam_chauthtok_conv(int n, const struct pam_message **msg, |
614 | struct pam_response **resp, void *data) | |
ee48c949 | 615 | { |
05114c74 | 616 | char input[PAM_MAX_MSG_SIZE]; |
0a23d79f | 617 | struct pam_response *reply; |
ee48c949 | 618 | int i; |
619 | ||
0a23d79f | 620 | *resp = NULL; |
621 | ||
05114c74 | 622 | if (n <= 0 || n > PAM_MAX_NUM_MSG) |
623 | return (PAM_CONV_ERR); | |
0a23d79f | 624 | |
625 | if ((reply = malloc(n * sizeof(*reply))) == NULL) | |
626 | return (PAM_CONV_ERR); | |
627 | memset(reply, 0, n * sizeof(*reply)); | |
628 | ||
05114c74 | 629 | for (i = 0; i < n; ++i) { |
630 | switch (PAM_MSG_MEMBER(msg, i, msg_style)) { | |
631 | case PAM_PROMPT_ECHO_OFF: | |
0a23d79f | 632 | reply[i].resp = |
05114c74 | 633 | read_passphrase(PAM_MSG_MEMBER(msg, i, msg), |
634 | RP_ALLOW_STDIN); | |
0a23d79f | 635 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 636 | break; |
637 | case PAM_PROMPT_ECHO_ON: | |
638 | fputs(PAM_MSG_MEMBER(msg, i, msg), stderr); | |
639 | fgets(input, sizeof input, stdin); | |
0a23d79f | 640 | reply[i].resp = xstrdup(input); |
641 | reply[i].resp_retcode = PAM_SUCCESS; | |
05114c74 | 642 | break; |
643 | case PAM_ERROR_MSG: | |
644 | case PAM_TEXT_INFO: | |
645 | fputs(PAM_MSG_MEMBER(msg, i, msg), stderr); | |
0a23d79f | 646 | reply[i].resp_retcode = PAM_SUCCESS; |
05114c74 | 647 | break; |
648 | default: | |
649 | goto fail; | |
650 | } | |
ee48c949 | 651 | } |
0a23d79f | 652 | *resp = reply; |
05114c74 | 653 | return (PAM_SUCCESS); |
0a23d79f | 654 | |
05114c74 | 655 | fail: |
0a23d79f | 656 | for(i = 0; i < n; i++) { |
657 | if (reply[i].resp != NULL) | |
658 | xfree(reply[i].resp); | |
659 | } | |
660 | xfree(reply); | |
05114c74 | 661 | return (PAM_CONV_ERR); |
ee48c949 | 662 | } |
663 | ||
05114c74 | 664 | /* |
665 | * XXX this should be done in the authentication phase, but ssh1 doesn't | |
666 | * support that | |
667 | */ | |
668 | void | |
669 | do_pam_chauthtok(void) | |
a5c9cd31 | 670 | { |
c53917a9 | 671 | struct pam_conv pam_conv; |
672 | ||
673 | pam_conv.conv = pam_chauthtok_conv; | |
674 | pam_conv.appdata_ptr = NULL; | |
05114c74 | 675 | |
676 | if (use_privsep) | |
5b9e2464 | 677 | fatal("Password expired (unable to change with privsep)"); |
05114c74 | 678 | sshpam_err = pam_set_item(sshpam_handle, PAM_CONV, |
679 | (const void *)&pam_conv); | |
680 | if (sshpam_err != PAM_SUCCESS) | |
681 | fatal("PAM: failed to set PAM_CONV: %s", | |
682 | pam_strerror(sshpam_handle, sshpam_err)); | |
683 | debug("PAM: changing password"); | |
684 | sshpam_err = pam_chauthtok(sshpam_handle, PAM_CHANGE_EXPIRED_AUTHTOK); | |
685 | if (sshpam_err != PAM_SUCCESS) | |
686 | fatal("PAM: pam_chauthtok(): %s", | |
687 | pam_strerror(sshpam_handle, sshpam_err)); | |
5daf7064 | 688 | } |
689 | ||
749560dd | 690 | /* |
691 | * Set a PAM environment string. We need to do this so that the session | |
692 | * modules can handle things like Kerberos/GSI credentials that appear | |
693 | * during the ssh authentication process. | |
694 | */ | |
695 | ||
696 | int | |
697 | do_pam_putenv(char *name, char *value) | |
698 | { | |
749560dd | 699 | int ret = 1; |
749560dd | 700 | #ifdef HAVE_PAM_PUTENV |
263c65df | 701 | char *compound; |
702 | size_t len; | |
703 | ||
704 | len = strlen(name) + strlen(value) + 2; | |
705 | compound = xmalloc(len); | |
706 | ||
707 | snprintf(compound, len, "%s=%s", name, value); | |
708 | ret = pam_putenv(sshpam_handle, compound); | |
709 | xfree(compound); | |
749560dd | 710 | #endif |
263c65df | 711 | |
749560dd | 712 | return (ret); |
713 | } | |
714 | ||
05114c74 | 715 | void |
716 | print_pam_messages(void) | |
5daf7064 | 717 | { |
05114c74 | 718 | /* XXX */ |
719 | } | |
2b87da3b | 720 | |
05114c74 | 721 | char ** |
722 | fetch_pam_environment(void) | |
723 | { | |
724 | #ifdef HAVE_PAM_GETENVLIST | |
725 | debug("PAM: retrieving environment"); | |
726 | return (pam_getenvlist(sshpam_handle)); | |
727 | #else | |
728 | return (NULL); | |
729 | #endif | |
730 | } | |
5c377b3b | 731 | |
05114c74 | 732 | void |
733 | free_pam_environment(char **env) | |
734 | { | |
735 | char **envp; | |
5daf7064 | 736 | |
0eb6370a | 737 | if (env == NULL) |
738 | return; | |
739 | ||
05114c74 | 740 | for (envp = env; *envp; envp++) |
741 | xfree(*envp); | |
742 | xfree(env); | |
a5c9cd31 | 743 | } |
744 | ||
745 | #endif /* USE_PAM */ |