2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) John H Terpsta 1999-2001
7 Copyright (C) Andrew Bartlett 2001
8 Copyright (C) Jeremy Allison 2001
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * This module provides PAM based functions for validation of
27 * username/password pairs, account managment, session and access control.
28 * Note: SMB password checking is done in smbpass.c
33 extern int DEBUGLEVEL;
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 {
54 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 /*******************************************************************
66 *********************************************************************/
68 static BOOL smb_pam_error_handler(pam_handle_t *pamh, int pam_error, 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)));
79 /*******************************************************************
80 This function is a sanity check, to make sure that we NEVER report
82 *********************************************************************/
84 static BOOL smb_pam_nt_status_error_handler(pam_handle_t *pamh, int pam_error,
85 char *msg, int dbglvl, uint32 *nt_status)
87 if (smb_pam_error_handler(pamh, pam_error, msg, dbglvl))
90 if (*nt_status == NT_STATUS_NOPROBLEMO) {
92 DEBUG(0, ("smb_pam_nt_status_error_handler: PAM: BUG: PAM and NT_STATUS \
93 error MISMATCH, forcing to NT_STATUS_LOGON_FAILURE"));
94 *nt_status = NT_STATUS_LOGON_FAILURE;
100 * PAM conversation function
101 * Here we assume (for now, at least) that echo on means login name, and
102 * echo off means password.
105 static int smb_pam_conv(int num_msg,
106 const struct pam_message **msg,
107 struct pam_response **resp,
111 struct pam_response *reply = NULL;
112 struct smb_pam_userdata *udp = (struct smb_pam_userdata *)appdata_ptr;
117 * Apparantly HPUX has a buggy PAM that doesn't support the
118 * appdata_ptr. Fail if this is the case. JRA.
122 DEBUG(0,("smb_pam_conv: PAM on this system is broken - appdata_ptr == NULL !\n"));
126 reply = malloc(sizeof(struct pam_response) * num_msg);
130 for (replies = 0; replies < num_msg; replies++) {
131 switch (msg[replies]->msg_style) {
132 case PAM_PROMPT_ECHO_ON:
133 reply[replies].resp_retcode = PAM_SUCCESS;
134 reply[replies].resp = COPY_STRING(udp->PAM_username);
138 case PAM_PROMPT_ECHO_OFF:
139 reply[replies].resp_retcode = PAM_SUCCESS;
140 reply[replies].resp = COPY_STRING(udp->PAM_password);
149 reply[replies].resp_retcode = PAM_SUCCESS;
150 reply[replies].resp = NULL;
154 /* Must be an error of some sort... */
165 * PAM password change conversation function
166 * Here we assume (for now, at least) that echo on means login name, and
167 * echo off means password.
170 static int smb_pam_passchange_conv(int num_msg,
171 const struct pam_message **msg,
172 struct pam_response **resp,
176 struct pam_response *reply = NULL;
177 fstring currentpw_prompt;
178 fstring newpw_prompt;
179 fstring repeatpw_prompt;
180 char *p = lp_passwd_chat();
181 struct smb_pam_userdata *udp = (struct smb_pam_userdata *)appdata_ptr;
186 * Apparantly HPUX has a buggy PAM that doesn't support the
187 * appdata_ptr. Fail if this is the case. JRA.
191 DEBUG(0,("smb_pam_passchange_conv: PAM on this system is broken - appdata_ptr == NULL !\n"));
195 /* Get the prompts... */
197 if (!next_token(&p, currentpw_prompt, NULL, sizeof(fstring)))
199 if (!next_token(&p, newpw_prompt, NULL, sizeof(fstring)))
201 if (!next_token(&p, repeatpw_prompt, NULL, sizeof(fstring)))
204 reply = malloc(sizeof(struct pam_response) * num_msg);
208 for (replies = 0; replies < num_msg; replies++) {
209 switch (msg[replies]->msg_style) {
210 case PAM_PROMPT_ECHO_ON:
211 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_ON: Replied: %s\n", msg[replies]->msg));
212 reply[replies].resp_retcode = PAM_SUCCESS;
213 reply[replies].resp = COPY_STRING(udp->PAM_username);
217 case PAM_PROMPT_ECHO_OFF:
218 reply[replies].resp_retcode = PAM_SUCCESS;
219 DEBUG(10,("smb_pam_passchange_conv: PAM_PROMPT_ECHO_OFF: Replied: %s\n", msg[replies]->msg));
220 if (strncmp(currentpw_prompt, msg[replies]->msg, strlen(currentpw_prompt)) == 0) {
221 reply[replies].resp = COPY_STRING(udp->PAM_password);
222 } else if (strncmp(newpw_prompt, msg[replies]->msg, strlen(newpw_prompt)) == 0) {
223 reply[replies].resp = COPY_STRING(udp->PAM_newpassword);
224 } else if (strncmp(repeatpw_prompt, msg[replies]->msg, strlen(repeatpw_prompt)) == 0) {
225 reply[replies].resp = COPY_STRING(udp->PAM_newpassword);
227 DEBUG(3,("smb_pam_passchange_conv: Could not find reply for PAM prompt: %s\n",msg[replies]->msg));
228 DEBUG(5,("smb_pam_passchange_conv: Prompts available:\n CurrentPW: \"%s\"\n NewPW: \"%s\"\n \
229 RepeatPW: \"%s\"\n",currentpw_prompt,newpw_prompt,repeatpw_prompt));
242 reply[replies].resp_retcode = PAM_SUCCESS;
243 reply[replies].resp = NULL;
247 /* Must be an error of some sort... */
259 /***************************************************************************
260 Free up a malloced pam_conv struct.
261 ****************************************************************************/
263 static void smb_free_pam_conv(struct pam_conv *pconv)
266 safe_free(pconv->appdata_ptr);
271 /***************************************************************************
272 Allocate a pam_conv struct.
273 ****************************************************************************/
275 static struct pam_conv *smb_setup_pam_conv(smb_pam_conv_fn smb_pam_conv_fnptr, char *user,
276 char *passwd, char *newpass)
278 struct pam_conv *pconv = (struct pam_conv *)malloc(sizeof(struct pam_conv));
279 struct smb_pam_userdata *udp = (struct smb_pam_userdata *)malloc(sizeof(struct smb_pam_userdata));
281 if (pconv == NULL || udp == NULL) {
287 udp->PAM_username = user;
288 udp->PAM_password = passwd;
289 udp->PAM_newpassword = newpass;
291 pconv->conv = smb_pam_conv_fnptr;
292 pconv->appdata_ptr = (void *)udp;
297 * PAM Closing out cleanup handler
300 static BOOL smb_pam_end(pam_handle_t *pamh, struct pam_conv *smb_pam_conv_ptr)
304 smb_free_pam_conv(smb_pam_conv_ptr);
307 pam_error = pam_end(pamh, 0);
308 if(smb_pam_error_handler(pamh, pam_error, "End Cleanup Failed", 2) == True) {
309 DEBUG(4, ("smb_pam_end: PAM: PAM_END OK.\n"));
313 DEBUG(2,("smb_pam_end: PAM: not initialised"));
318 * Start PAM authentication for specified account
321 static BOOL smb_pam_start(pam_handle_t **pamh, char *user, char *rhost, struct pam_conv *pconv)
325 *pamh = (pam_handle_t *)NULL;
327 DEBUG(4,("smb_pam_start: PAM: Init user: %s\n", user));
329 pam_error = pam_start("samba", user, pconv, pamh);
330 if( !smb_pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {
331 *pamh = (pam_handle_t *)NULL;
336 rhost = client_name();
337 if (strequal(rhost,"UNKNOWN"))
338 rhost = client_addr();
342 DEBUG(4,("smb_pam_start: PAM: setting rhost to: %s\n", rhost));
343 pam_error = pam_set_item(*pamh, PAM_RHOST, rhost);
344 if(!smb_pam_error_handler(*pamh, pam_error, "set rhost failed", 0)) {
345 smb_pam_end(*pamh, pconv);
346 *pamh = (pam_handle_t *)NULL;
351 DEBUG(4,("smb_pam_start: PAM: setting tty\n"));
352 pam_error = pam_set_item(*pamh, PAM_TTY, "samba");
353 if (!smb_pam_error_handler(*pamh, pam_error, "set tty failed", 0)) {
354 smb_pam_end(*pamh, pconv);
355 *pamh = (pam_handle_t *)NULL;
359 DEBUG(4,("smb_pam_start: PAM: Init passed for user: %s\n", user));
364 * PAM Authentication Handler
366 static uint32 smb_pam_auth(pam_handle_t *pamh, char *user)
369 uint32 nt_status = NT_STATUS_LOGON_FAILURE;
372 * To enable debugging set in /etc/pam.d/samba:
373 * auth required /lib/security/pam_pwdb.so nullok shadow audit
376 DEBUG(4,("smb_pam_auth: PAM: Authenticate User: %s\n", user));
377 pam_error = pam_authenticate(pamh, PAM_SILENT | lp_null_passwords() ? 0 : PAM_DISALLOW_NULL_AUTHTOK);
380 DEBUG(2, ("smb_pam_auth: PAM: Athentication Error for user %s\n", user));
381 nt_status = NT_STATUS_WRONG_PASSWORD;
383 case PAM_CRED_INSUFFICIENT:
384 DEBUG(2, ("smb_pam_auth: PAM: Insufficient Credentials for user %s\n", user));
385 nt_status = NT_STATUS_INSUFFICIENT_LOGON_INFO;
387 case PAM_AUTHINFO_UNAVAIL:
388 DEBUG(2, ("smb_pam_auth: PAM: Authentication Information Unavailable for user %s\n", user));
389 nt_status = NT_STATUS_LOGON_FAILURE;
391 case PAM_USER_UNKNOWN:
392 DEBUG(2, ("smb_pam_auth: PAM: Username %s NOT known to Authentication system\n", user));
393 nt_status = NT_STATUS_NO_SUCH_USER;
396 DEBUG(2, ("smb_pam_auth: PAM: One or more authentication modules reports user limit for user %s exceeeded\n", user));
397 nt_status = NT_STATUS_REMOTE_SESSION_LIMIT;
400 DEBUG(0, ("smb_pam_auth: PAM: One or more PAM modules failed to load for user %s\n", user));
401 nt_status = NT_STATUS_LOGON_FAILURE;
404 DEBUG(4, ("smb_pam_auth: PAM: User %s Authenticated OK\n", user));
405 nt_status = NT_STATUS_NOPROBLEMO;
408 DEBUG(0, ("smb_pam_auth: PAM: UNKNOWN ERROR while authenticating user %s\n", user));
409 nt_status = NT_STATUS_LOGON_FAILURE;
413 smb_pam_nt_status_error_handler(pamh, pam_error, "Authentication Failure", 2, &nt_status);
418 * PAM Account Handler
420 static uint32 smb_pam_account(pam_handle_t *pamh, char * user)
423 uint32 nt_status = NT_STATUS_ACCOUNT_DISABLED;
425 DEBUG(4,("smb_pam_account: PAM: Account Management for User: %s\n", user));
426 pam_error = pam_acct_mgmt(pamh, PAM_SILENT); /* Is user account enabled? */
427 switch( pam_error ) {
428 case PAM_AUTHTOK_EXPIRED:
429 DEBUG(2, ("smb_pam_account: PAM: User %s is valid but password is expired\n", user));
430 nt_status = NT_STATUS_PASSWORD_EXPIRED;
432 case PAM_ACCT_EXPIRED:
433 DEBUG(2, ("smb_pam_account: PAM: User %s no longer permitted to access system\n", user));
434 nt_status = NT_STATUS_ACCOUNT_EXPIRED;
437 DEBUG(2, ("smb_pam_account: PAM: There was an authentication error for user %s\n", user));
438 nt_status = NT_STATUS_LOGON_FAILURE;
440 case PAM_PERM_DENIED:
441 DEBUG(0, ("smb_pam_account: PAM: User %s is NOT permitted to access system at this time\n", user));
442 nt_status = NT_STATUS_ACCOUNT_RESTRICTION;
444 case PAM_USER_UNKNOWN:
445 DEBUG(0, ("smb_pam_account: PAM: User \"%s\" is NOT known to account management\n", user));
446 nt_status = NT_STATUS_NO_SUCH_USER;
449 DEBUG(4, ("smb_pam_account: PAM: Account OK for User: %s\n", user));
450 nt_status = NT_STATUS_NOPROBLEMO;
453 nt_status = NT_STATUS_ACCOUNT_DISABLED;
454 DEBUG(0, ("smb_pam_account: PAM: UNKNOWN PAM ERROR (%d) during Account Management for User: %s\n", pam_error, user));
458 smb_pam_nt_status_error_handler(pamh, pam_error, "Account Check Failed", 2, &nt_status);
463 * PAM Credential Setting
466 static uint32 smb_pam_setcred(pam_handle_t *pamh, char * user)
469 uint32 nt_status = NT_STATUS_NO_TOKEN;
472 * This will allow samba to aquire a kerberos token. And, when
473 * exporting an AFS cell, be able to /write/ to this cell.
476 DEBUG(4,("PAM: Account Management SetCredentials for User: %s\n", user));
477 pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED|PAM_SILENT));
478 switch( pam_error ) {
479 case PAM_CRED_UNAVAIL:
480 DEBUG(0, ("smb_pam_setcred: PAM: Credentials not found for user:%s\n", user ));
481 nt_status = NT_STATUS_NO_TOKEN;
483 case PAM_CRED_EXPIRED:
484 DEBUG(0, ("smb_pam_setcred: PAM: Credentials for user: \"%s\" EXPIRED!\n", user ));
485 nt_status = NT_STATUS_PASSWORD_EXPIRED;
487 case PAM_USER_UNKNOWN:
488 DEBUG(0, ("smb_pam_setcred: PAM: User: \"%s\" is NOT known so can not set credentials!\n", user ));
489 nt_status = NT_STATUS_NO_SUCH_USER;
492 DEBUG(0, ("smb_pam_setcred: PAM: Unknown setcredentials error - unable to set credentials for %s\n", user ));
493 nt_status = NT_STATUS_LOGON_FAILURE;
496 DEBUG(4, ("smb_pam_setcred: PAM: SetCredentials OK for User: %s\n", user));
497 nt_status = NT_STATUS_NOPROBLEMO;
500 DEBUG(0, ("smb_pam_setcred: PAM: UNKNOWN PAM ERROR (%d) during SetCredentials for User: %s\n", pam_error, user));
501 nt_status = NT_STATUS_NO_TOKEN;
505 smb_pam_nt_status_error_handler(pamh, pam_error, "Set Credential Failure", 2, &nt_status);
510 * PAM Internal Session Handler
512 static BOOL smb_internal_pam_session(pam_handle_t *pamh, char *user, char *tty, BOOL flag)
517 DEBUG(4,("smb_internal_pam_session: PAM: tty set to: %s\n", tty));
518 pam_error = pam_set_item(pamh, PAM_TTY, tty);
519 if (!smb_pam_error_handler(pamh, pam_error, "set tty failed", 0))
524 pam_error = pam_open_session(pamh, PAM_SILENT);
525 if (!smb_pam_error_handler(pamh, pam_error, "session setup failed", 0))
528 pam_setcred(pamh, (PAM_DELETE_CRED|PAM_SILENT)); /* We don't care if this fails */
529 pam_error = pam_close_session(pamh, PAM_SILENT); /* This will probably pick up the error anyway */
530 if (!smb_pam_error_handler(pamh, pam_error, "session close failed", 0))
537 * Internal PAM Password Changer.
540 static BOOL smb_pam_chauthtok(pam_handle_t *pamh, char * user)
544 DEBUG(4,("smb_pam_chauthtok: PAM: Password Change for User: %s\n", user));
546 pam_error = pam_chauthtok(pamh, PAM_SILENT); /* Change Password */
548 switch( pam_error ) {
549 case PAM_AUTHTOK_ERR:
550 DEBUG(2, ("PAM: unable to obtain the new authentication token - is password to weak?\n"));
553 /* This doesn't seem to be defined on Solaris. JRA */
554 #ifdef PAM_AUTHTOK_RECOVER_ERR
555 case PAM_AUTHTOK_RECOVER_ERR:
556 DEBUG(2, ("PAM: unable to obtain the old authentication token - was the old password wrong?.\n"));
560 case PAM_AUTHTOK_LOCK_BUSY:
561 DEBUG(2, ("PAM: unable to change the authentication token since it is currently locked.\n"));
563 case PAM_AUTHTOK_DISABLE_AGING:
564 DEBUG(2, ("PAM: Authentication token aging has been disabled.\n"));
566 case PAM_PERM_DENIED:
567 DEBUG(0, ("PAM: Permission denied.\n"));
570 DEBUG(0, ("PAM: Could not update all authentication token(s). No authentication tokens were updated.\n"));
572 case PAM_USER_UNKNOWN:
573 DEBUG(0, ("PAM: User not known to PAM\n"));
576 DEBUG(4, ("PAM: Account OK for User: %s\n", user));
579 DEBUG(0, ("PAM: UNKNOWN PAM ERROR (%d) for User: %s\n", pam_error, user));
582 if(!smb_pam_error_handler(pamh, pam_error, "Password Change Failed", 2)) {
586 /* If this point is reached, the password has changed. */
591 * PAM Externally accessible Session handler
594 BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
596 pam_handle_t *pamh = NULL;
597 struct pam_conv *pconv = NULL;
599 /* Ignore PAM if told to. */
601 if (!lp_obey_pam_restrictions())
604 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
607 if (!smb_pam_start(&pamh, user, rhost, pconv))
610 if (!smb_internal_pam_session(pamh, user, tty, True)) {
611 smb_pam_end(pamh, pconv);
615 return smb_pam_end(pamh, pconv);
619 * PAM Externally accessible Session handler
622 BOOL smb_pam_close_session(char *user, char *tty, char *rhost)
624 pam_handle_t *pamh = NULL;
625 struct pam_conv *pconv = NULL;
627 /* Ignore PAM if told to. */
629 if (!lp_obey_pam_restrictions())
632 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
635 if (!smb_pam_start(&pamh, user, rhost, pconv))
638 if (!smb_internal_pam_session(pamh, user, tty, False)) {
639 smb_pam_end(pamh, pconv);
643 return smb_pam_end(pamh, pconv);
647 * PAM Externally accessible Account handler
650 uint32 smb_pam_accountcheck(char * user)
652 uint32 nt_status = NT_STATUS_ACCOUNT_DISABLED;
653 pam_handle_t *pamh = NULL;
654 struct pam_conv *pconv = NULL;
656 /* Ignore PAM if told to. */
658 if (!lp_obey_pam_restrictions())
659 return NT_STATUS_NOPROBLEMO;
661 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)
664 if (!smb_pam_start(&pamh, user, NULL, pconv))
665 return NT_STATUS_ACCOUNT_DISABLED;
667 if ((nt_status = smb_pam_account(pamh, user)) != NT_STATUS_NOPROBLEMO)
668 DEBUG(0, ("smb_pam_accountcheck: PAM: Account Validation Failed - Rejecting User %s!\n", user));
670 smb_pam_end(pamh, pconv);
675 * PAM Password Validation Suite
678 uint32 smb_pam_passcheck(char * user, char * password)
680 pam_handle_t *pamh = NULL;
681 uint32 nt_status = NT_STATUS_LOGON_FAILURE;
682 struct pam_conv *pconv = NULL;
685 * Note we can't ignore PAM here as this is the only
686 * way of doing auths on plaintext passwords when
687 * compiled --with-pam.
690 if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, password, NULL)) == NULL)
691 return NT_STATUS_LOGON_FAILURE;
693 if (!smb_pam_start(&pamh, user, NULL, pconv))
694 return NT_STATUS_LOGON_FAILURE;
696 if ((nt_status = smb_pam_auth(pamh, user)) != NT_STATUS_NOPROBLEMO) {
697 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_auth failed - Rejecting User %s !\n", user));
698 smb_pam_end(pamh, pconv);
702 if ((nt_status = smb_pam_account(pamh, user)) != NT_STATUS_NOPROBLEMO) {
703 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_account failed - Rejecting User %s !\n", user));
704 smb_pam_end(pamh, pconv);
708 if ((nt_status = smb_pam_setcred(pamh, user)) != NT_STATUS_NOPROBLEMO) {
709 DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_setcred failed - Rejecting User %s !\n", user));
710 smb_pam_end(pamh, pconv);
714 smb_pam_end(pamh, pconv);
719 * PAM Password Change Suite
722 BOOL smb_pam_passchange(char * user, char * oldpassword, char * newpassword)
724 /* Appropriate quantities of root should be obtained BEFORE calling this function */
725 struct pam_conv *pconv = NULL;
726 pam_handle_t *pamh = NULL;
728 if ((pconv = smb_setup_pam_conv(smb_pam_passchange_conv, user, oldpassword, newpassword)) == NULL)
731 if(!smb_pam_start(&pamh, user, NULL, pconv))
734 if (!smb_pam_chauthtok(pamh, user)) {
735 DEBUG(0, ("smb_pam_passchange: PAM: Password Change Failed for user %s!\n", user));
736 smb_pam_end(pamh, pconv);
740 return smb_pam_end(pamh, pconv);
745 /* If PAM not used, no PAM restrictions on accounts. */
746 uint32 smb_pam_accountcheck(char * user)
748 return NT_STATUS_NOPROBLEMO;
751 /* If PAM not used, also no PAM restrictions on sessions. */
752 BOOL smb_pam_claim_session(char *user, char *tty, char *rhost)
757 /* If PAM not used, also no PAM restrictions on sessions. */
758 BOOL smb_pam_close_session(char *in_user, char *tty, char *rhost)
762 #endif /* WITH_PAM */