r292: removing some outdated files and directories (I love subversion!)
[Samba/gebeck_regimport.git] / source3 / auth / pampass.c
blob3239686a20d49629e42c89fec95d4ae9622d47b5
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) ? 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 = malloc(sizeof(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(char *p)
207 fstring prompt;
208 fstring reply;
209 struct chat_struct *list = NULL;
210 struct chat_struct *t;
211 struct chat_struct *tmp;
213 while (1) {
214 t = (struct chat_struct *)malloc(sizeof(*t));
215 if (!t) {
216 DEBUG(0,("make_pw_chat: malloc failed!\n"));
217 return NULL;
220 ZERO_STRUCTP(t);
222 DLIST_ADD_END(list, t, tmp);
224 if (!next_token(&p, prompt, NULL, sizeof(fstring)))
225 break;
227 if (strequal(prompt,"."))
228 fstrcpy(prompt,"*");
230 special_char_sub(prompt);
231 fstrcpy(t->prompt, prompt);
232 strlower_m(t->prompt);
233 trim_char(t->prompt, ' ', ' ');
235 if (!next_token(&p, reply, NULL, sizeof(fstring)))
236 break;
238 if (strequal(reply,"."))
239 fstrcpy(reply,"");
241 special_char_sub(reply);
242 fstrcpy(t->reply, reply);
243 strlower_m(t->reply);
244 trim_char(t->reply, ' ', ' ');
247 return list;
250 static void free_pw_chat(struct chat_struct *list)
252 while (list) {
253 struct chat_struct *old_head = list;
254 DLIST_REMOVE(list, list);
255 SAFE_FREE(old_head);
259 static int smb_pam_passchange_conv(int num_msg,
260 const struct pam_message **msg,
261 struct pam_response **resp,
262 void *appdata_ptr)
264 int replies = 0;
265 struct pam_response *reply = NULL;
266 fstring current_prompt;
267 fstring current_reply;
268 struct smb_pam_userdata *udp = (struct smb_pam_userdata *)appdata_ptr;
269 struct chat_struct *pw_chat= make_pw_chat(lp_passwd_chat());
270 struct chat_struct *t;
271 BOOL found;
272 *resp = NULL;
274 DEBUG(10,("smb_pam_passchange_conv: starting converstation for %d messages\n", num_msg));
276 if (num_msg <= 0)
277 return PAM_CONV_ERR;
279 if (pw_chat == NULL)
280 return PAM_CONV_ERR;
283 * Apparantly HPUX has a buggy PAM that doesn't support the
284 * appdata_ptr. Fail if this is the case. JRA.
287 if (udp == NULL) {
288 DEBUG(0,("smb_pam_passchange_conv: PAM on this system is broken - appdata_ptr == NULL !\n"));
289 free_pw_chat(pw_chat);
290 return PAM_CONV_ERR;
293 reply = malloc(sizeof(struct pam_response) * num_msg);
294 if (!reply) {
295 DEBUG(0,("smb_pam_passchange_conv: malloc for reply failed!\n"));
296 free_pw_chat(pw_chat);
297 return PAM_CONV_ERR;
300 for (replies = 0; replies < num_msg; replies++) {
301 found = False;
302 DEBUG(10,("smb_pam_passchange_conv: Processing message %d\n", replies));
303 switch (msg[replies]->msg_style) {
304 case PAM_PROMPT_ECHO_ON:
305 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: PAM said: %s\n", msg[replies]->msg));
306 fstrcpy(current_prompt, msg[replies]->msg);
307 trim_char(current_prompt, ' ', ' ');
308 for (t=pw_chat; t; t=t->next) {
310 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: trying to match |%s| to |%s|\n",
311 t->prompt, current_prompt ));
313 if (unix_wild_match(t->prompt, current_prompt) == 0) {
314 fstrcpy(current_reply, t->reply);
315 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: We sent: %s\n", current_reply));
316 pwd_sub(current_reply, udp->PAM_username, udp->PAM_password, udp->PAM_newpassword);
317 #ifdef DEBUG_PASSWORD
318 DEBUG(100,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: We actualy sent: %s\n", current_reply));
319 #endif
320 reply[replies].resp_retcode = PAM_SUCCESS;
321 reply[replies].resp = COPY_STRING(current_reply);
322 found = True;
323 break;
326 /* PAM frees resp */
327 if (!found) {
328 DEBUG(3,("smb_pam_passchange_conv: Could not find reply for PAM prompt: %s\n",msg[replies]->msg));
329 free_pw_chat(pw_chat);
330 SAFE_FREE(reply);
331 return PAM_CONV_ERR;
333 break;
335 case PAM_PROMPT_ECHO_OFF:
336 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: PAM said: %s\n", msg[replies]->msg));
337 fstrcpy(current_prompt, msg[replies]->msg);
338 trim_char(current_prompt, ' ', ' ');
339 for (t=pw_chat; t; t=t->next) {
341 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: trying to match |%s| to |%s|\n",
342 t->prompt, current_prompt ));
344 if (unix_wild_match(t->prompt, current_prompt) == 0) {
345 fstrcpy(current_reply, t->reply);
346 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: We sent: %s\n", current_reply));
347 pwd_sub(current_reply, udp->PAM_username, udp->PAM_password, udp->PAM_newpassword);
348 reply[replies].resp_retcode = PAM_SUCCESS;
349 reply[replies].resp = COPY_STRING(current_reply);
350 #ifdef DEBUG_PASSWORD
351 DEBUG(100,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: We actualy sent: %s\n", current_reply));
352 #endif
353 found = True;
354 break;
357 /* PAM frees resp */
359 if (!found) {
360 DEBUG(3,("smb_pam_passchange_conv: Could not find reply for PAM prompt: %s\n",msg[replies]->msg));
361 free_pw_chat(pw_chat);
362 SAFE_FREE(reply);
363 return PAM_CONV_ERR;
365 break;
367 case PAM_TEXT_INFO:
368 /* fall through */
370 case PAM_ERROR_MSG:
371 /* ignore it... */
372 reply[replies].resp_retcode = PAM_SUCCESS;
373 reply[replies].resp = NULL;
374 break;
376 default:
377 /* Must be an error of some sort... */
378 free_pw_chat(pw_chat);
379 SAFE_FREE(reply);
380 return PAM_CONV_ERR;
384 free_pw_chat(pw_chat);
385 if (reply)
386 *resp = reply;
387 return PAM_SUCCESS;
390 /***************************************************************************
391 Free up a malloced pam_conv struct.
392 ****************************************************************************/
394 static void smb_free_pam_conv(struct pam_conv *pconv)
396 if (pconv)
397 SAFE_FREE(pconv->appdata_ptr);
399 SAFE_FREE(pconv);
402 /***************************************************************************
403 Allocate a pam_conv struct.
404 ****************************************************************************/
406 static struct pam_conv *smb_setup_pam_conv(smb_pam_conv_fn smb_pam_conv_fnptr, const char *user,
407 const char *passwd, const char *newpass)
409 struct pam_conv *pconv = (struct pam_conv *)malloc(sizeof(struct pam_conv));
410 struct smb_pam_userdata *udp = (struct smb_pam_userdata *)malloc(sizeof(struct smb_pam_userdata));
412 if (pconv == NULL || udp == NULL) {
413 SAFE_FREE(pconv);
414 SAFE_FREE(udp);
415 return NULL;
418 udp->PAM_username = user;
419 udp->PAM_password = passwd;
420 udp->PAM_newpassword = newpass;
422 pconv->conv = smb_pam_conv_fnptr;
423 pconv->appdata_ptr = (void *)udp;
424 return pconv;
428 * PAM Closing out cleanup handler
431 static BOOL smb_pam_end(pam_handle_t *pamh, struct pam_conv *smb_pam_conv_ptr)
433 int pam_error;
435 smb_free_pam_conv(smb_pam_conv_ptr);
437 if( pamh != NULL ) {
438 pam_error = pam_end(pamh, 0);
439 if(smb_pam_error_handler(pamh, pam_error, "End Cleanup Failed", 2) == True) {
440 DEBUG(4, ("smb_pam_end: PAM: PAM_END OK.\n"));
441 return True;
444 DEBUG(2,("smb_pam_end: PAM: not initialised"));
445 return False;
449 * Start PAM authentication for specified account
452 static BOOL smb_pam_start(pam_handle_t **pamh, const char *user, const char *rhost, struct pam_conv *pconv)
454 int pam_error;
455 const char *our_rhost;
457 *pamh = (pam_handle_t *)NULL;
459 DEBUG(4,("smb_pam_start: PAM: Init user: %s\n", user));
461 pam_error = pam_start("samba", user, pconv, pamh);
462 if( !smb_pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {
463 *pamh = (pam_handle_t *)NULL;
464 return False;
467 if (rhost == NULL) {
468 our_rhost = client_name();
469 if (strequal(rhost,"UNKNOWN"))
470 our_rhost = client_addr();
471 } else {
472 our_rhost = rhost;
475 #ifdef PAM_RHOST
476 DEBUG(4,("smb_pam_start: PAM: setting rhost to: %s\n", our_rhost));
477 pam_error = pam_set_item(*pamh, PAM_RHOST, our_rhost);
478 if(!smb_pam_error_handler(*pamh, pam_error, "set rhost failed", 0)) {
479 smb_pam_end(*pamh, pconv);
480 *pamh = (pam_handle_t *)NULL;
481 return False;
483 #endif
484 #ifdef PAM_TTY
485 DEBUG(4,("smb_pam_start: PAM: setting tty\n"));
486 pam_error = pam_set_item(*pamh, PAM_TTY, "samba");
487 if (!smb_pam_error_handler(*pamh, pam_error, "set tty failed", 0)) {
488 smb_pam_end(*pamh, pconv);
489 *pamh = (pam_handle_t *)NULL;
490 return False;
492 #endif
493 DEBUG(4,("smb_pam_start: PAM: Init passed for user: %s\n", user));
494 return True;
498 * PAM Authentication Handler
500 static NTSTATUS smb_pam_auth(pam_handle_t *pamh, const char *user)
502 int pam_error;
503 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
506 * To enable debugging set in /etc/pam.d/samba:
507 * auth required /lib/security/pam_pwdb.so nullok shadow audit
510 DEBUG(4,("smb_pam_auth: PAM: Authenticate User: %s\n", user));
511 pam_error = pam_authenticate(pamh, PAM_SILENT | lp_null_passwords() ? 0 : PAM_DISALLOW_NULL_AUTHTOK);
512 switch( pam_error ){
513 case PAM_AUTH_ERR:
514 DEBUG(2, ("smb_pam_auth: PAM: Athentication Error for user %s\n", user));
515 break;
516 case PAM_CRED_INSUFFICIENT:
517 DEBUG(2, ("smb_pam_auth: PAM: Insufficient Credentials for user %s\n", user));
518 break;
519 case PAM_AUTHINFO_UNAVAIL:
520 DEBUG(2, ("smb_pam_auth: PAM: Authentication Information Unavailable for user %s\n", user));
521 break;
522 case PAM_USER_UNKNOWN:
523 DEBUG(2, ("smb_pam_auth: PAM: Username %s NOT known to Authentication system\n", user));
524 break;
525 case PAM_MAXTRIES:
526 DEBUG(2, ("smb_pam_auth: PAM: One or more authentication modules reports user limit for user %s exceeeded\n", user));
527 break;
528 case PAM_ABORT:
529 DEBUG(0, ("smb_pam_auth: PAM: One or more PAM modules failed to load for user %s\n", user));
530 break;
531 case PAM_SUCCESS:
532 DEBUG(4, ("smb_pam_auth: PAM: User %s Authenticated OK\n", user));
533 break;
534 default:
535 DEBUG(0, ("smb_pam_auth: PAM: UNKNOWN ERROR while authenticating user %s\n", user));
536 break;
539 smb_pam_nt_status_error_handler(pamh, pam_error, "Authentication Failure", 2, &nt_status);
540 return nt_status;
544 * PAM Account Handler
546 static NTSTATUS smb_pam_account(pam_handle_t *pamh, const char * user)
548 int pam_error;
549 NTSTATUS nt_status = NT_STATUS_ACCOUNT_DISABLED;
551 DEBUG(4,("smb_pam_account: PAM: Account Management for User: %s\n", user));
552 pam_error = pam_acct_mgmt(pamh, PAM_SILENT); /* Is user account enabled? */
553 switch( pam_error ) {
554 case PAM_AUTHTOK_EXPIRED:
555 DEBUG(2, ("smb_pam_account: PAM: User %s is valid but password is expired\n", user));
556 break;
557 case PAM_ACCT_EXPIRED:
558 DEBUG(2, ("smb_pam_account: PAM: User %s no longer permitted to access system\n", user));
559 break;
560 case PAM_AUTH_ERR:
561 DEBUG(2, ("smb_pam_account: PAM: There was an authentication error for user %s\n", user));
562 break;
563 case PAM_PERM_DENIED:
564 DEBUG(0, ("smb_pam_account: PAM: User %s is NOT permitted to access system at this time\n", user));
565 break;
566 case PAM_USER_UNKNOWN:
567 DEBUG(0, ("smb_pam_account: PAM: User \"%s\" is NOT known to account management\n", user));
568 break;
569 case PAM_SUCCESS:
570 DEBUG(4, ("smb_pam_account: PAM: Account OK for User: %s\n", user));
571 break;
572 default:
573 DEBUG(0, ("smb_pam_account: PAM: UNKNOWN PAM ERROR (%d) during Account Management for User: %s\n", pam_error, user));
574 break;
577 smb_pam_nt_status_error_handler(pamh, pam_error, "Account Check Failed", 2, &nt_status);
578 return nt_status;
582 * PAM Credential Setting
585 static NTSTATUS smb_pam_setcred(pam_handle_t *pamh, const char * user)
587 int pam_error;
588 NTSTATUS nt_status = NT_STATUS_NO_TOKEN;
591 * This will allow samba to aquire a kerberos token. And, when
592 * exporting an AFS cell, be able to /write/ to this cell.
595 DEBUG(4,("PAM: Account Management SetCredentials for User: %s\n", user));
596 pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED|PAM_SILENT));
597 switch( pam_error ) {
598 case PAM_CRED_UNAVAIL:
599 DEBUG(0, ("smb_pam_setcred: PAM: Credentials not found for user:%s\n", user ));
600 break;
601 case PAM_CRED_EXPIRED:
602 DEBUG(0, ("smb_pam_setcred: PAM: Credentials for user: \"%s\" EXPIRED!\n", user ));
603 break;
604 case PAM_USER_UNKNOWN:
605 DEBUG(0, ("smb_pam_setcred: PAM: User: \"%s\" is NOT known so can not set credentials!\n", user ));
606 break;
607 case PAM_CRED_ERR:
608 DEBUG(0, ("smb_pam_setcred: PAM: Unknown setcredentials error - unable to set credentials for %s\n", user ));
609 break;
610 case PAM_SUCCESS:
611 DEBUG(4, ("smb_pam_setcred: PAM: SetCredentials OK for User: %s\n", user));
612 break;
613 default:
614 DEBUG(0, ("smb_pam_setcred: PAM: UNKNOWN PAM ERROR (%d) during SetCredentials for User: %s\n", pam_error, user));
615 break;
618 smb_pam_nt_status_error_handler(pamh, pam_error, "Set Credential Failure", 2, &nt_status);
619 return nt_status;
623 * PAM Internal Session Handler
625 static BOOL smb_internal_pam_session(pam_handle_t *pamh, const char *user, const char *tty, BOOL flag)
627 int pam_error;
629 #ifdef PAM_TTY
630 DEBUG(4,("smb_internal_pam_session: PAM: tty set to: %s\n", tty));
631 pam_error = pam_set_item(pamh, PAM_TTY, tty);
632 if (!smb_pam_error_handler(pamh, pam_error, "set tty failed", 0))
633 return False;
634 #endif
636 if (flag) {
637 pam_error = pam_open_session(pamh, PAM_SILENT);
638 if (!smb_pam_error_handler(pamh, pam_error, "session setup failed", 0))
639 return False;
640 } else {
641 pam_setcred(pamh, (PAM_DELETE_CRED|PAM_SILENT)); /* We don't care if this fails */
642 pam_error = pam_close_session(pamh, PAM_SILENT); /* This will probably pick up the error anyway */
643 if (!smb_pam_error_handler(pamh, pam_error, "session close failed", 0))
644 return False;
646 return (True);
650 * Internal PAM Password Changer.
653 static BOOL smb_pam_chauthtok(pam_handle_t *pamh, const char * user)
655 int pam_error;
657 DEBUG(4,("smb_pam_chauthtok: PAM: Password Change for User: %s\n", user));
659 pam_error = pam_chauthtok(pamh, PAM_SILENT); /* Change Password */
661 switch( pam_error ) {
662 case PAM_AUTHTOK_ERR:
663 DEBUG(2, ("PAM: unable to obtain the new authentication token - is password to weak?\n"));
664 break;
666 /* This doesn't seem to be defined on Solaris. JRA */
667 #ifdef PAM_AUTHTOK_RECOVER_ERR
668 case PAM_AUTHTOK_RECOVER_ERR:
669 DEBUG(2, ("PAM: unable to obtain the old authentication token - was the old password wrong?.\n"));
670 break;
671 #endif
673 case PAM_AUTHTOK_LOCK_BUSY:
674 DEBUG(2, ("PAM: unable to change the authentication token since it is currently locked.\n"));
675 break;
676 case PAM_AUTHTOK_DISABLE_AGING:
677 DEBUG(2, ("PAM: Authentication token aging has been disabled.\n"));
678 break;
679 case PAM_PERM_DENIED:
680 DEBUG(0, ("PAM: Permission denied.\n"));
681 break;
682 case PAM_TRY_AGAIN:
683 DEBUG(0, ("PAM: Could not update all authentication token(s). No authentication tokens were updated.\n"));
684 break;
685 case PAM_USER_UNKNOWN:
686 DEBUG(0, ("PAM: User not known to PAM\n"));
687 break;
688 case PAM_SUCCESS:
689 DEBUG(4, ("PAM: Account OK for User: %s\n", user));
690 break;
691 default:
692 DEBUG(0, ("PAM: UNKNOWN PAM ERROR (%d) for User: %s\n", pam_error, user));
695 if(!smb_pam_error_handler(pamh, pam_error, "Password Change Failed", 2)) {
696 return False;
699 /* If this point is reached, the password has changed. */
700 return True;
704 * PAM Externally accessible Session handler
707 BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
709 pam_handle_t *pamh = NULL;
710 struct pam_conv *pconv = NULL;
712 /* Ignore PAM if told to. */
714 if (!lp_obey_pam_restrictions())
715 return True;
717 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
718 return False;
720 if (!smb_pam_start(&pamh, user, rhost, pconv))
721 return False;
723 if (!smb_internal_pam_session(pamh, user, tty, True)) {
724 smb_pam_end(pamh, pconv);
725 return False;
728 return smb_pam_end(pamh, pconv);
732 * PAM Externally accessible Session handler
735 BOOL smb_pam_close_session(char *user, char *tty, char *rhost)
737 pam_handle_t *pamh = NULL;
738 struct pam_conv *pconv = NULL;
740 /* Ignore PAM if told to. */
742 if (!lp_obey_pam_restrictions())
743 return True;
745 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
746 return False;
748 if (!smb_pam_start(&pamh, user, rhost, pconv))
749 return False;
751 if (!smb_internal_pam_session(pamh, user, tty, False)) {
752 smb_pam_end(pamh, pconv);
753 return False;
756 return smb_pam_end(pamh, pconv);
760 * PAM Externally accessible Account handler
763 NTSTATUS smb_pam_accountcheck(const char * user)
765 NTSTATUS nt_status = NT_STATUS_ACCOUNT_DISABLED;
766 pam_handle_t *pamh = NULL;
767 struct pam_conv *pconv = NULL;
769 /* Ignore PAM if told to. */
771 if (!lp_obey_pam_restrictions())
772 return NT_STATUS_OK;
774 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
775 return NT_STATUS_NO_MEMORY;
777 if (!smb_pam_start(&pamh, user, NULL, pconv))
778 return NT_STATUS_ACCOUNT_DISABLED;
780 if (!NT_STATUS_IS_OK(nt_status = smb_pam_account(pamh, user)))
781 DEBUG(0, ("smb_pam_accountcheck: PAM: Account Validation Failed - Rejecting User %s!\n", user));
783 smb_pam_end(pamh, pconv);
784 return nt_status;
788 * PAM Password Validation Suite
791 NTSTATUS smb_pam_passcheck(const char * user, const char * password)
793 pam_handle_t *pamh = NULL;
794 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
795 struct pam_conv *pconv = NULL;
798 * Note we can't ignore PAM here as this is the only
799 * way of doing auths on plaintext passwords when
800 * compiled --with-pam.
803 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, password, NULL)) == NULL)
804 return NT_STATUS_LOGON_FAILURE;
806 if (!smb_pam_start(&pamh, user, NULL, pconv))
807 return NT_STATUS_LOGON_FAILURE;
809 if (!NT_STATUS_IS_OK(nt_status = smb_pam_auth(pamh, user))) {
810 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_auth failed - Rejecting User %s !\n", user));
811 smb_pam_end(pamh, pconv);
812 return nt_status;
815 if (!NT_STATUS_IS_OK(nt_status = smb_pam_account(pamh, user))) {
816 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_account failed - Rejecting User %s !\n", user));
817 smb_pam_end(pamh, pconv);
818 return nt_status;
821 if (!NT_STATUS_IS_OK(nt_status = smb_pam_setcred(pamh, user))) {
822 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_setcred failed - Rejecting User %s !\n", user));
823 smb_pam_end(pamh, pconv);
824 return nt_status;
827 smb_pam_end(pamh, pconv);
828 return nt_status;
832 * PAM Password Change Suite
835 BOOL smb_pam_passchange(const char * user, const char * oldpassword, const char * newpassword)
837 /* Appropriate quantities of root should be obtained BEFORE calling this function */
838 struct pam_conv *pconv = NULL;
839 pam_handle_t *pamh = NULL;
841 if ((pconv = smb_setup_pam_conv(smb_pam_passchange_conv, user, oldpassword, newpassword)) == NULL)
842 return False;
844 if(!smb_pam_start(&pamh, user, NULL, pconv))
845 return False;
847 if (!smb_pam_chauthtok(pamh, user)) {
848 DEBUG(0, ("smb_pam_passchange: PAM: Password Change Failed for user %s!\n", user));
849 smb_pam_end(pamh, pconv);
850 return False;
853 return smb_pam_end(pamh, pconv);
856 #else
858 /* If PAM not used, no PAM restrictions on accounts. */
859 NTSTATUS smb_pam_accountcheck(const char * user)
861 return NT_STATUS_OK;
864 /* If PAM not used, also no PAM restrictions on sessions. */
865 BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
867 return True;
870 /* If PAM not used, also no PAM restrictions on sessions. */
871 BOOL smb_pam_close_session(char *in_user, char *tty, char *rhost)
873 return True;
875 #endif /* WITH_PAM */