r21219: Speed up the initial startup time of smbd on systems with loaded disk
[Samba.git] / source / auth / pampass.c
blobba11d2e8fc2b52b747831746849a96d23472e625
1 /*
2 Unix SMB/CIFS implementation.
3 PAM Password checking
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) John H Terpsta 1999-2001
6 Copyright (C) Andrew Bartlett 2001
7 Copyright (C) Jeremy Allison 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * This module provides PAM based functions for validation of
26 * username/password pairs, account managment, session and access control.
27 * Note: SMB password checking is done in smbpass.c
30 #include "includes.h"
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_AUTH
35 #ifdef WITH_PAM
37 /*******************************************************************
38 * Handle PAM authentication
39 * - Access, Authentication, Session, Password
40 * Note: See PAM Documentation and refer to local system PAM implementation
41 * which determines what actions/limitations/allowances become affected.
42 *********************************************************************/
44 #include <security/pam_appl.h>
47 * Structure used to communicate between the conversation function
48 * and the server_login/change password functions.
51 struct smb_pam_userdata {
52 const char *PAM_username;
53 const char *PAM_password;
54 const char *PAM_newpassword;
57 typedef int (*smb_pam_conv_fn)(int, const struct pam_message **, struct pam_response **, void *appdata_ptr);
60 * Macros to help make life easy
62 #define COPY_STRING(s) (s) ? SMB_STRDUP(s) : NULL
64 /*******************************************************************
65 PAM error handler.
66 *********************************************************************/
68 static BOOL smb_pam_error_handler(pam_handle_t *pamh, int pam_error, const char *msg, int dbglvl)
71 if( pam_error != PAM_SUCCESS) {
72 DEBUG(dbglvl, ("smb_pam_error_handler: PAM: %s : %s\n",
73 msg, pam_strerror(pamh, pam_error)));
75 return False;
77 return True;
80 /*******************************************************************
81 This function is a sanity check, to make sure that we NEVER report
82 failure as sucess.
83 *********************************************************************/
85 static BOOL smb_pam_nt_status_error_handler(pam_handle_t *pamh, int pam_error,
86 const char *msg, int dbglvl,
87 NTSTATUS *nt_status)
89 *nt_status = pam_to_nt_status(pam_error);
91 if (smb_pam_error_handler(pamh, pam_error, msg, dbglvl))
92 return True;
94 if (NT_STATUS_IS_OK(*nt_status)) {
95 /* Complain LOUDLY */
96 DEBUG(0, ("smb_pam_nt_status_error_handler: PAM: BUG: PAM and NT_STATUS \
97 error MISMATCH, forcing to NT_STATUS_LOGON_FAILURE"));
98 *nt_status = NT_STATUS_LOGON_FAILURE;
100 return False;
104 * PAM conversation function
105 * Here we assume (for now, at least) that echo on means login name, and
106 * echo off means password.
109 static int smb_pam_conv(int num_msg,
110 const struct pam_message **msg,
111 struct pam_response **resp,
112 void *appdata_ptr)
114 int replies = 0;
115 struct pam_response *reply = NULL;
116 struct smb_pam_userdata *udp = (struct smb_pam_userdata *)appdata_ptr;
118 *resp = NULL;
120 if (num_msg <= 0)
121 return PAM_CONV_ERR;
124 * Apparantly HPUX has a buggy PAM that doesn't support the
125 * appdata_ptr. Fail if this is the case. JRA.
128 if (udp == NULL) {
129 DEBUG(0,("smb_pam_conv: PAM on this system is broken - appdata_ptr == NULL !\n"));
130 return PAM_CONV_ERR;
133 reply = SMB_MALLOC_ARRAY(struct pam_response, num_msg);
134 if (!reply)
135 return PAM_CONV_ERR;
137 memset(reply, '\0', sizeof(struct pam_response) * num_msg);
139 for (replies = 0; replies < num_msg; replies++) {
140 switch (msg[replies]->msg_style) {
141 case PAM_PROMPT_ECHO_ON:
142 reply[replies].resp_retcode = PAM_SUCCESS;
143 reply[replies].resp = COPY_STRING(udp->PAM_username);
144 /* PAM frees resp */
145 break;
147 case PAM_PROMPT_ECHO_OFF:
148 reply[replies].resp_retcode = PAM_SUCCESS;
149 reply[replies].resp = COPY_STRING(udp->PAM_password);
150 /* PAM frees resp */
151 break;
153 case PAM_TEXT_INFO:
154 /* fall through */
156 case PAM_ERROR_MSG:
157 /* ignore it... */
158 reply[replies].resp_retcode = PAM_SUCCESS;
159 reply[replies].resp = NULL;
160 break;
162 default:
163 /* Must be an error of some sort... */
164 SAFE_FREE(reply);
165 return PAM_CONV_ERR;
168 if (reply)
169 *resp = reply;
170 return PAM_SUCCESS;
174 * PAM password change conversation function
175 * Here we assume (for now, at least) that echo on means login name, and
176 * echo off means password.
179 static void special_char_sub(char *buf)
181 all_string_sub(buf, "\\n", "", 0);
182 all_string_sub(buf, "\\r", "", 0);
183 all_string_sub(buf, "\\s", " ", 0);
184 all_string_sub(buf, "\\t", "\t", 0);
187 static void pwd_sub(char *buf, const char *username, const char *oldpass, const char *newpass)
189 fstring_sub(buf, "%u", username);
190 all_string_sub(buf, "%o", oldpass, sizeof(fstring));
191 all_string_sub(buf, "%n", newpass, sizeof(fstring));
195 struct chat_struct {
196 struct chat_struct *next, *prev;
197 fstring prompt;
198 fstring reply;
201 /**************************************************************
202 Create a linked list containing chat data.
203 ***************************************************************/
205 static struct chat_struct *make_pw_chat(const char *p)
207 fstring prompt;
208 fstring reply;
209 struct chat_struct *list = NULL;
210 struct chat_struct *t;
212 while (1) {
213 t = SMB_MALLOC_P(struct chat_struct);
214 if (!t) {
215 DEBUG(0,("make_pw_chat: malloc failed!\n"));
216 return NULL;
219 ZERO_STRUCTP(t);
221 DLIST_ADD_END(list, t, struct chat_struct*);
223 if (!next_token(&p, prompt, NULL, sizeof(fstring)))
224 break;
226 if (strequal(prompt,"."))
227 fstrcpy(prompt,"*");
229 special_char_sub(prompt);
230 fstrcpy(t->prompt, prompt);
231 strlower_m(t->prompt);
232 trim_char(t->prompt, ' ', ' ');
234 if (!next_token(&p, reply, NULL, sizeof(fstring)))
235 break;
237 if (strequal(reply,"."))
238 fstrcpy(reply,"");
240 special_char_sub(reply);
241 fstrcpy(t->reply, reply);
242 strlower_m(t->reply);
243 trim_char(t->reply, ' ', ' ');
246 return list;
249 static void free_pw_chat(struct chat_struct *list)
251 while (list) {
252 struct chat_struct *old_head = list;
253 DLIST_REMOVE(list, list);
254 SAFE_FREE(old_head);
258 static int smb_pam_passchange_conv(int num_msg,
259 const struct pam_message **msg,
260 struct pam_response **resp,
261 void *appdata_ptr)
263 int replies = 0;
264 struct pam_response *reply = NULL;
265 fstring current_prompt;
266 fstring current_reply;
267 struct smb_pam_userdata *udp = (struct smb_pam_userdata *)appdata_ptr;
268 struct chat_struct *pw_chat= make_pw_chat(lp_passwd_chat());
269 struct chat_struct *t;
270 BOOL found;
271 *resp = NULL;
273 DEBUG(10,("smb_pam_passchange_conv: starting converstation for %d messages\n", num_msg));
275 if (num_msg <= 0)
276 return PAM_CONV_ERR;
278 if (pw_chat == NULL)
279 return PAM_CONV_ERR;
282 * Apparantly HPUX has a buggy PAM that doesn't support the
283 * appdata_ptr. Fail if this is the case. JRA.
286 if (udp == NULL) {
287 DEBUG(0,("smb_pam_passchange_conv: PAM on this system is broken - appdata_ptr == NULL !\n"));
288 free_pw_chat(pw_chat);
289 return PAM_CONV_ERR;
292 reply = SMB_MALLOC_ARRAY(struct pam_response, num_msg);
293 if (!reply) {
294 DEBUG(0,("smb_pam_passchange_conv: malloc for reply failed!\n"));
295 free_pw_chat(pw_chat);
296 return PAM_CONV_ERR;
299 for (replies = 0; replies < num_msg; replies++) {
300 found = False;
301 DEBUG(10,("smb_pam_passchange_conv: Processing message %d\n", replies));
302 switch (msg[replies]->msg_style) {
303 case PAM_PROMPT_ECHO_ON:
304 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: PAM said: %s\n", msg[replies]->msg));
305 fstrcpy(current_prompt, msg[replies]->msg);
306 trim_char(current_prompt, ' ', ' ');
307 for (t=pw_chat; t; t=t->next) {
309 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: trying to match |%s| to |%s|\n",
310 t->prompt, current_prompt ));
312 if (unix_wild_match(t->prompt, current_prompt)) {
313 fstrcpy(current_reply, t->reply);
314 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: We sent: %s\n", current_reply));
315 pwd_sub(current_reply, udp->PAM_username, udp->PAM_password, udp->PAM_newpassword);
316 #ifdef DEBUG_PASSWORD
317 DEBUG(100,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: We actualy sent: %s\n", current_reply));
318 #endif
319 reply[replies].resp_retcode = PAM_SUCCESS;
320 reply[replies].resp = COPY_STRING(current_reply);
321 found = True;
322 break;
325 /* PAM frees resp */
326 if (!found) {
327 DEBUG(3,("smb_pam_passchange_conv: Could not find reply for PAM prompt: %s\n",msg[replies]->msg));
328 free_pw_chat(pw_chat);
329 SAFE_FREE(reply);
330 return PAM_CONV_ERR;
332 break;
334 case PAM_PROMPT_ECHO_OFF:
335 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: PAM said: %s\n", msg[replies]->msg));
336 fstrcpy(current_prompt, msg[replies]->msg);
337 trim_char(current_prompt, ' ', ' ');
338 for (t=pw_chat; t; t=t->next) {
340 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: trying to match |%s| to |%s|\n",
341 t->prompt, current_prompt ));
343 if (unix_wild_match(t->prompt, current_prompt)) {
344 fstrcpy(current_reply, t->reply);
345 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: We sent: %s\n", current_reply));
346 pwd_sub(current_reply, udp->PAM_username, udp->PAM_password, udp->PAM_newpassword);
347 reply[replies].resp_retcode = PAM_SUCCESS;
348 reply[replies].resp = COPY_STRING(current_reply);
349 #ifdef DEBUG_PASSWORD
350 DEBUG(100,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: We actualy sent: %s\n", current_reply));
351 #endif
352 found = True;
353 break;
356 /* PAM frees resp */
358 if (!found) {
359 DEBUG(3,("smb_pam_passchange_conv: Could not find reply for PAM prompt: %s\n",msg[replies]->msg));
360 free_pw_chat(pw_chat);
361 SAFE_FREE(reply);
362 return PAM_CONV_ERR;
364 break;
366 case PAM_TEXT_INFO:
367 /* fall through */
369 case PAM_ERROR_MSG:
370 /* ignore it... */
371 reply[replies].resp_retcode = PAM_SUCCESS;
372 reply[replies].resp = NULL;
373 break;
375 default:
376 /* Must be an error of some sort... */
377 free_pw_chat(pw_chat);
378 SAFE_FREE(reply);
379 return PAM_CONV_ERR;
383 free_pw_chat(pw_chat);
384 if (reply)
385 *resp = reply;
386 return PAM_SUCCESS;
389 /***************************************************************************
390 Free up a malloced pam_conv struct.
391 ****************************************************************************/
393 static void smb_free_pam_conv(struct pam_conv *pconv)
395 if (pconv)
396 SAFE_FREE(pconv->appdata_ptr);
398 SAFE_FREE(pconv);
401 /***************************************************************************
402 Allocate a pam_conv struct.
403 ****************************************************************************/
405 static struct pam_conv *smb_setup_pam_conv(smb_pam_conv_fn smb_pam_conv_fnptr, const char *user,
406 const char *passwd, const char *newpass)
408 struct pam_conv *pconv = SMB_MALLOC_P(struct pam_conv);
409 struct smb_pam_userdata *udp = SMB_MALLOC_P(struct smb_pam_userdata);
411 if (pconv == NULL || udp == NULL) {
412 SAFE_FREE(pconv);
413 SAFE_FREE(udp);
414 return NULL;
417 udp->PAM_username = user;
418 udp->PAM_password = passwd;
419 udp->PAM_newpassword = newpass;
421 pconv->conv = smb_pam_conv_fnptr;
422 pconv->appdata_ptr = (void *)udp;
423 return pconv;
427 * PAM Closing out cleanup handler
430 static BOOL smb_pam_end(pam_handle_t *pamh, struct pam_conv *smb_pam_conv_ptr)
432 int pam_error;
434 smb_free_pam_conv(smb_pam_conv_ptr);
436 if( pamh != NULL ) {
437 pam_error = pam_end(pamh, 0);
438 if(smb_pam_error_handler(pamh, pam_error, "End Cleanup Failed", 2) == True) {
439 DEBUG(4, ("smb_pam_end: PAM: PAM_END OK.\n"));
440 return True;
443 DEBUG(2,("smb_pam_end: PAM: not initialised"));
444 return False;
448 * Start PAM authentication for specified account
451 static BOOL smb_pam_start(pam_handle_t **pamh, const char *user, const char *rhost, struct pam_conv *pconv)
453 int pam_error;
454 const char *our_rhost;
456 *pamh = (pam_handle_t *)NULL;
458 DEBUG(4,("smb_pam_start: PAM: Init user: %s\n", user));
460 pam_error = pam_start("samba", user, pconv, pamh);
461 if( !smb_pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {
462 *pamh = (pam_handle_t *)NULL;
463 return False;
466 if (rhost == NULL) {
467 our_rhost = client_name();
468 if (strequal(our_rhost,"UNKNOWN"))
469 our_rhost = client_addr();
470 } else {
471 our_rhost = rhost;
474 #ifdef PAM_RHOST
475 DEBUG(4,("smb_pam_start: PAM: setting rhost to: %s\n", our_rhost));
476 pam_error = pam_set_item(*pamh, PAM_RHOST, our_rhost);
477 if(!smb_pam_error_handler(*pamh, pam_error, "set rhost failed", 0)) {
478 smb_pam_end(*pamh, pconv);
479 *pamh = (pam_handle_t *)NULL;
480 return False;
482 #endif
483 #ifdef PAM_TTY
484 DEBUG(4,("smb_pam_start: PAM: setting tty\n"));
485 pam_error = pam_set_item(*pamh, PAM_TTY, "samba");
486 if (!smb_pam_error_handler(*pamh, pam_error, "set tty failed", 0)) {
487 smb_pam_end(*pamh, pconv);
488 *pamh = (pam_handle_t *)NULL;
489 return False;
491 #endif
492 DEBUG(4,("smb_pam_start: PAM: Init passed for user: %s\n", user));
493 return True;
497 * PAM Authentication Handler
499 static NTSTATUS smb_pam_auth(pam_handle_t *pamh, const char *user)
501 int pam_error;
502 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
505 * To enable debugging set in /etc/pam.d/samba:
506 * auth required /lib/security/pam_pwdb.so nullok shadow audit
509 DEBUG(4,("smb_pam_auth: PAM: Authenticate User: %s\n", user));
510 pam_error = pam_authenticate(pamh, PAM_SILENT | lp_null_passwords() ? 0 : PAM_DISALLOW_NULL_AUTHTOK);
511 switch( pam_error ){
512 case PAM_AUTH_ERR:
513 DEBUG(2, ("smb_pam_auth: PAM: Authentication Error for user %s\n", user));
514 break;
515 case PAM_CRED_INSUFFICIENT:
516 DEBUG(2, ("smb_pam_auth: PAM: Insufficient Credentials for user %s\n", user));
517 break;
518 case PAM_AUTHINFO_UNAVAIL:
519 DEBUG(2, ("smb_pam_auth: PAM: Authentication Information Unavailable for user %s\n", user));
520 break;
521 case PAM_USER_UNKNOWN:
522 DEBUG(2, ("smb_pam_auth: PAM: Username %s NOT known to Authentication system\n", user));
523 break;
524 case PAM_MAXTRIES:
525 DEBUG(2, ("smb_pam_auth: PAM: One or more authentication modules reports user limit for user %s exceeeded\n", user));
526 break;
527 case PAM_ABORT:
528 DEBUG(0, ("smb_pam_auth: PAM: One or more PAM modules failed to load for user %s\n", user));
529 break;
530 case PAM_SUCCESS:
531 DEBUG(4, ("smb_pam_auth: PAM: User %s Authenticated OK\n", user));
532 break;
533 default:
534 DEBUG(0, ("smb_pam_auth: PAM: UNKNOWN ERROR while authenticating user %s\n", user));
535 break;
538 smb_pam_nt_status_error_handler(pamh, pam_error, "Authentication Failure", 2, &nt_status);
539 return nt_status;
543 * PAM Account Handler
545 static NTSTATUS smb_pam_account(pam_handle_t *pamh, const char * user)
547 int pam_error;
548 NTSTATUS nt_status = NT_STATUS_ACCOUNT_DISABLED;
550 DEBUG(4,("smb_pam_account: PAM: Account Management for User: %s\n", user));
551 pam_error = pam_acct_mgmt(pamh, PAM_SILENT); /* Is user account enabled? */
552 switch( pam_error ) {
553 case PAM_AUTHTOK_EXPIRED:
554 DEBUG(2, ("smb_pam_account: PAM: User %s is valid but password is expired\n", user));
555 break;
556 case PAM_ACCT_EXPIRED:
557 DEBUG(2, ("smb_pam_account: PAM: User %s no longer permitted to access system\n", user));
558 break;
559 case PAM_AUTH_ERR:
560 DEBUG(2, ("smb_pam_account: PAM: There was an authentication error for user %s\n", user));
561 break;
562 case PAM_PERM_DENIED:
563 DEBUG(0, ("smb_pam_account: PAM: User %s is NOT permitted to access system at this time\n", user));
564 break;
565 case PAM_USER_UNKNOWN:
566 DEBUG(0, ("smb_pam_account: PAM: User \"%s\" is NOT known to account management\n", user));
567 break;
568 case PAM_SUCCESS:
569 DEBUG(4, ("smb_pam_account: PAM: Account OK for User: %s\n", user));
570 break;
571 default:
572 DEBUG(0, ("smb_pam_account: PAM: UNKNOWN PAM ERROR (%d) during Account Management for User: %s\n", pam_error, user));
573 break;
576 smb_pam_nt_status_error_handler(pamh, pam_error, "Account Check Failed", 2, &nt_status);
577 return nt_status;
581 * PAM Credential Setting
584 static NTSTATUS smb_pam_setcred(pam_handle_t *pamh, const char * user)
586 int pam_error;
587 NTSTATUS nt_status = NT_STATUS_NO_TOKEN;
590 * This will allow samba to aquire a kerberos token. And, when
591 * exporting an AFS cell, be able to /write/ to this cell.
594 DEBUG(4,("PAM: Account Management SetCredentials for User: %s\n", user));
595 pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED|PAM_SILENT));
596 switch( pam_error ) {
597 case PAM_CRED_UNAVAIL:
598 DEBUG(0, ("smb_pam_setcred: PAM: Credentials not found for user:%s\n", user ));
599 break;
600 case PAM_CRED_EXPIRED:
601 DEBUG(0, ("smb_pam_setcred: PAM: Credentials for user: \"%s\" EXPIRED!\n", user ));
602 break;
603 case PAM_USER_UNKNOWN:
604 DEBUG(0, ("smb_pam_setcred: PAM: User: \"%s\" is NOT known so can not set credentials!\n", user ));
605 break;
606 case PAM_CRED_ERR:
607 DEBUG(0, ("smb_pam_setcred: PAM: Unknown setcredentials error - unable to set credentials for %s\n", user ));
608 break;
609 case PAM_SUCCESS:
610 DEBUG(4, ("smb_pam_setcred: PAM: SetCredentials OK for User: %s\n", user));
611 break;
612 default:
613 DEBUG(0, ("smb_pam_setcred: PAM: UNKNOWN PAM ERROR (%d) during SetCredentials for User: %s\n", pam_error, user));
614 break;
617 smb_pam_nt_status_error_handler(pamh, pam_error, "Set Credential Failure", 2, &nt_status);
618 return nt_status;
622 * PAM Internal Session Handler
624 static BOOL smb_internal_pam_session(pam_handle_t *pamh, const char *user, const char *tty, BOOL flag)
626 int pam_error;
628 #ifdef PAM_TTY
629 DEBUG(4,("smb_internal_pam_session: PAM: tty set to: %s\n", tty));
630 pam_error = pam_set_item(pamh, PAM_TTY, tty);
631 if (!smb_pam_error_handler(pamh, pam_error, "set tty failed", 0))
632 return False;
633 #endif
635 if (flag) {
636 pam_error = pam_open_session(pamh, PAM_SILENT);
637 if (!smb_pam_error_handler(pamh, pam_error, "session setup failed", 0))
638 return False;
639 } else {
640 pam_setcred(pamh, (PAM_DELETE_CRED|PAM_SILENT)); /* We don't care if this fails */
641 pam_error = pam_close_session(pamh, PAM_SILENT); /* This will probably pick up the error anyway */
642 if (!smb_pam_error_handler(pamh, pam_error, "session close failed", 0))
643 return False;
645 return (True);
649 * Internal PAM Password Changer.
652 static BOOL smb_pam_chauthtok(pam_handle_t *pamh, const char * user)
654 int pam_error;
656 DEBUG(4,("smb_pam_chauthtok: PAM: Password Change for User: %s\n", user));
658 pam_error = pam_chauthtok(pamh, PAM_SILENT); /* Change Password */
660 switch( pam_error ) {
661 case PAM_AUTHTOK_ERR:
662 DEBUG(2, ("PAM: unable to obtain the new authentication token - is password to weak?\n"));
663 break;
665 /* This doesn't seem to be defined on Solaris. JRA */
666 #ifdef PAM_AUTHTOK_RECOVER_ERR
667 case PAM_AUTHTOK_RECOVER_ERR:
668 DEBUG(2, ("PAM: unable to obtain the old authentication token - was the old password wrong?.\n"));
669 break;
670 #endif
672 case PAM_AUTHTOK_LOCK_BUSY:
673 DEBUG(2, ("PAM: unable to change the authentication token since it is currently locked.\n"));
674 break;
675 case PAM_AUTHTOK_DISABLE_AGING:
676 DEBUG(2, ("PAM: Authentication token aging has been disabled.\n"));
677 break;
678 case PAM_PERM_DENIED:
679 DEBUG(0, ("PAM: Permission denied.\n"));
680 break;
681 case PAM_TRY_AGAIN:
682 DEBUG(0, ("PAM: Could not update all authentication token(s). No authentication tokens were updated.\n"));
683 break;
684 case PAM_USER_UNKNOWN:
685 DEBUG(0, ("PAM: User not known to PAM\n"));
686 break;
687 case PAM_SUCCESS:
688 DEBUG(4, ("PAM: Account OK for User: %s\n", user));
689 break;
690 default:
691 DEBUG(0, ("PAM: UNKNOWN PAM ERROR (%d) for User: %s\n", pam_error, user));
694 if(!smb_pam_error_handler(pamh, pam_error, "Password Change Failed", 2)) {
695 return False;
698 /* If this point is reached, the password has changed. */
699 return True;
703 * PAM Externally accessible Session handler
706 BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
708 pam_handle_t *pamh = NULL;
709 struct pam_conv *pconv = NULL;
711 /* Ignore PAM if told to. */
713 if (!lp_obey_pam_restrictions())
714 return True;
716 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
717 return False;
719 if (!smb_pam_start(&pamh, user, rhost, pconv))
720 return False;
722 if (!smb_internal_pam_session(pamh, user, tty, True)) {
723 smb_pam_end(pamh, pconv);
724 return False;
727 return smb_pam_end(pamh, pconv);
731 * PAM Externally accessible Session handler
734 BOOL smb_pam_close_session(char *user, char *tty, char *rhost)
736 pam_handle_t *pamh = NULL;
737 struct pam_conv *pconv = NULL;
739 /* Ignore PAM if told to. */
741 if (!lp_obey_pam_restrictions())
742 return True;
744 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
745 return False;
747 if (!smb_pam_start(&pamh, user, rhost, pconv))
748 return False;
750 if (!smb_internal_pam_session(pamh, user, tty, False)) {
751 smb_pam_end(pamh, pconv);
752 return False;
755 return smb_pam_end(pamh, pconv);
759 * PAM Externally accessible Account handler
762 NTSTATUS smb_pam_accountcheck(const char * user)
764 NTSTATUS nt_status = NT_STATUS_ACCOUNT_DISABLED;
765 pam_handle_t *pamh = NULL;
766 struct pam_conv *pconv = NULL;
768 /* Ignore PAM if told to. */
770 if (!lp_obey_pam_restrictions())
771 return NT_STATUS_OK;
773 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
774 return NT_STATUS_NO_MEMORY;
776 if (!smb_pam_start(&pamh, user, NULL, pconv))
777 return NT_STATUS_ACCOUNT_DISABLED;
779 if (!NT_STATUS_IS_OK(nt_status = smb_pam_account(pamh, user)))
780 DEBUG(0, ("smb_pam_accountcheck: PAM: Account Validation Failed - Rejecting User %s!\n", user));
782 smb_pam_end(pamh, pconv);
783 return nt_status;
787 * PAM Password Validation Suite
790 NTSTATUS smb_pam_passcheck(const char * user, const char * password)
792 pam_handle_t *pamh = NULL;
793 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
794 struct pam_conv *pconv = NULL;
797 * Note we can't ignore PAM here as this is the only
798 * way of doing auths on plaintext passwords when
799 * compiled --with-pam.
802 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, password, NULL)) == NULL)
803 return NT_STATUS_LOGON_FAILURE;
805 if (!smb_pam_start(&pamh, user, NULL, pconv))
806 return NT_STATUS_LOGON_FAILURE;
808 if (!NT_STATUS_IS_OK(nt_status = smb_pam_auth(pamh, user))) {
809 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_auth failed - Rejecting User %s !\n", user));
810 smb_pam_end(pamh, pconv);
811 return nt_status;
814 if (!NT_STATUS_IS_OK(nt_status = smb_pam_account(pamh, user))) {
815 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_account failed - Rejecting User %s !\n", user));
816 smb_pam_end(pamh, pconv);
817 return nt_status;
820 if (!NT_STATUS_IS_OK(nt_status = smb_pam_setcred(pamh, user))) {
821 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_setcred failed - Rejecting User %s !\n", user));
822 smb_pam_end(pamh, pconv);
823 return nt_status;
826 smb_pam_end(pamh, pconv);
827 return nt_status;
831 * PAM Password Change Suite
834 BOOL smb_pam_passchange(const char * user, const char * oldpassword, const char * newpassword)
836 /* Appropriate quantities of root should be obtained BEFORE calling this function */
837 struct pam_conv *pconv = NULL;
838 pam_handle_t *pamh = NULL;
840 if ((pconv = smb_setup_pam_conv(smb_pam_passchange_conv, user, oldpassword, newpassword)) == NULL)
841 return False;
843 if(!smb_pam_start(&pamh, user, NULL, pconv))
844 return False;
846 if (!smb_pam_chauthtok(pamh, user)) {
847 DEBUG(0, ("smb_pam_passchange: PAM: Password Change Failed for user %s!\n", user));
848 smb_pam_end(pamh, pconv);
849 return False;
852 return smb_pam_end(pamh, pconv);
855 #else
857 /* If PAM not used, no PAM restrictions on accounts. */
858 NTSTATUS smb_pam_accountcheck(const char * user)
860 return NT_STATUS_OK;
863 /* If PAM not used, also no PAM restrictions on sessions. */
864 BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
866 return True;
869 /* If PAM not used, also no PAM restrictions on sessions. */
870 BOOL smb_pam_close_session(char *in_user, char *tty, char *rhost)
872 return True;
874 #endif /* WITH_PAM */