pampass.c

来自「samba-3.0.22.tar.gz 编译smb服务器的源码」· C语言 代码 · 共 876 行 · 第 1/2 页

C
876
字号
		if(smb_pam_error_handler(pamh, pam_error, "End Cleanup Failed", 2) == True) {			DEBUG(4, ("smb_pam_end: PAM: PAM_END OK.\n"));			return True;		}	}	DEBUG(2,("smb_pam_end: PAM: not initialised"));	return False;}/* * Start PAM authentication for specified account */static BOOL smb_pam_start(pam_handle_t **pamh, const char *user, const char *rhost, struct pam_conv *pconv){	int pam_error;	const char *our_rhost;	*pamh = (pam_handle_t *)NULL;	DEBUG(4,("smb_pam_start: PAM: Init user: %s\n", user));	pam_error = pam_start("samba", user, pconv, pamh);	if( !smb_pam_error_handler(*pamh, pam_error, "Init Failed", 0)) {		*pamh = (pam_handle_t *)NULL;		return False;	}	if (rhost == NULL) {		our_rhost = client_name();		if (strequal(our_rhost,"UNKNOWN"))			our_rhost = client_addr();	} else {		our_rhost = rhost;	}#ifdef PAM_RHOST	DEBUG(4,("smb_pam_start: PAM: setting rhost to: %s\n", our_rhost));	pam_error = pam_set_item(*pamh, PAM_RHOST, our_rhost);	if(!smb_pam_error_handler(*pamh, pam_error, "set rhost failed", 0)) {		smb_pam_end(*pamh, pconv);		*pamh = (pam_handle_t *)NULL;		return False;	}#endif#ifdef PAM_TTY	DEBUG(4,("smb_pam_start: PAM: setting tty\n"));	pam_error = pam_set_item(*pamh, PAM_TTY, "samba");	if (!smb_pam_error_handler(*pamh, pam_error, "set tty failed", 0)) {		smb_pam_end(*pamh, pconv);		*pamh = (pam_handle_t *)NULL;		return False;	}#endif	DEBUG(4,("smb_pam_start: PAM: Init passed for user: %s\n", user));	return True;}/* * PAM Authentication Handler */static NTSTATUS smb_pam_auth(pam_handle_t *pamh, const char *user){	int pam_error;	NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;	/*	 * To enable debugging set in /etc/pam.d/samba:	 *	auth required /lib/security/pam_pwdb.so nullok shadow audit	 */		DEBUG(4,("smb_pam_auth: PAM: Authenticate User: %s\n", user));	pam_error = pam_authenticate(pamh, PAM_SILENT | lp_null_passwords() ? 0 : PAM_DISALLOW_NULL_AUTHTOK);	switch( pam_error ){		case PAM_AUTH_ERR:			DEBUG(2, ("smb_pam_auth: PAM: Athentication Error for user %s\n", user));			break;		case PAM_CRED_INSUFFICIENT:			DEBUG(2, ("smb_pam_auth: PAM: Insufficient Credentials for user %s\n", user));			break;		case PAM_AUTHINFO_UNAVAIL:			DEBUG(2, ("smb_pam_auth: PAM: Authentication Information Unavailable for user %s\n", user));			break;		case PAM_USER_UNKNOWN:			DEBUG(2, ("smb_pam_auth: PAM: Username %s NOT known to Authentication system\n", user));			break;		case PAM_MAXTRIES:			DEBUG(2, ("smb_pam_auth: PAM: One or more authentication modules reports user limit for user %s exceeeded\n", user));			break;		case PAM_ABORT:			DEBUG(0, ("smb_pam_auth: PAM: One or more PAM modules failed to load for user %s\n", user));			break;		case PAM_SUCCESS:			DEBUG(4, ("smb_pam_auth: PAM: User %s Authenticated OK\n", user));			break;		default:			DEBUG(0, ("smb_pam_auth: PAM: UNKNOWN ERROR while authenticating user %s\n", user));			break;	}	smb_pam_nt_status_error_handler(pamh, pam_error, "Authentication Failure", 2, &nt_status);	return nt_status;}/*  * PAM Account Handler */static NTSTATUS smb_pam_account(pam_handle_t *pamh, const char * user){	int pam_error;	NTSTATUS nt_status = NT_STATUS_ACCOUNT_DISABLED;	DEBUG(4,("smb_pam_account: PAM: Account Management for User: %s\n", user));	pam_error = pam_acct_mgmt(pamh, PAM_SILENT); /* Is user account enabled? */	switch( pam_error ) {		case PAM_AUTHTOK_EXPIRED:			DEBUG(2, ("smb_pam_account: PAM: User %s is valid but password is expired\n", user));			break;		case PAM_ACCT_EXPIRED:			DEBUG(2, ("smb_pam_account: PAM: User %s no longer permitted to access system\n", user));			break;		case PAM_AUTH_ERR:			DEBUG(2, ("smb_pam_account: PAM: There was an authentication error for user %s\n", user));			break;		case PAM_PERM_DENIED:			DEBUG(0, ("smb_pam_account: PAM: User %s is NOT permitted to access system at this time\n", user));			break;		case PAM_USER_UNKNOWN:			DEBUG(0, ("smb_pam_account: PAM: User \"%s\" is NOT known to account management\n", user));			break;		case PAM_SUCCESS:			DEBUG(4, ("smb_pam_account: PAM: Account OK for User: %s\n", user));			break;		default:			DEBUG(0, ("smb_pam_account: PAM: UNKNOWN PAM ERROR (%d) during Account Management for User: %s\n", pam_error, user));			break;	}	smb_pam_nt_status_error_handler(pamh, pam_error, "Account Check Failed", 2, &nt_status);	return nt_status;}/* * PAM Credential Setting */static NTSTATUS smb_pam_setcred(pam_handle_t *pamh, const char * user){	int pam_error;	NTSTATUS nt_status = NT_STATUS_NO_TOKEN;	/*	 * This will allow samba to aquire a kerberos token. And, when	 * exporting an AFS cell, be able to /write/ to this cell.	 */	DEBUG(4,("PAM: Account Management SetCredentials for User: %s\n", user));	pam_error = pam_setcred(pamh, (PAM_ESTABLISH_CRED|PAM_SILENT)); 	switch( pam_error ) {		case PAM_CRED_UNAVAIL:			DEBUG(0, ("smb_pam_setcred: PAM: Credentials not found for user:%s\n", user ));			break;		case PAM_CRED_EXPIRED:			DEBUG(0, ("smb_pam_setcred: PAM: Credentials for user: \"%s\" EXPIRED!\n", user ));			break;		case PAM_USER_UNKNOWN:			DEBUG(0, ("smb_pam_setcred: PAM: User: \"%s\" is NOT known so can not set credentials!\n", user ));			break;		case PAM_CRED_ERR:			DEBUG(0, ("smb_pam_setcred: PAM: Unknown setcredentials error - unable to set credentials for %s\n", user ));			break;		case PAM_SUCCESS:			DEBUG(4, ("smb_pam_setcred: PAM: SetCredentials OK for User: %s\n", user));			break;		default:			DEBUG(0, ("smb_pam_setcred: PAM: UNKNOWN PAM ERROR (%d) during SetCredentials for User: %s\n", pam_error, user));			break;	}	smb_pam_nt_status_error_handler(pamh, pam_error, "Set Credential Failure", 2, &nt_status);	return nt_status;}/* * PAM Internal Session Handler */static BOOL smb_internal_pam_session(pam_handle_t *pamh, const char *user, const char *tty, BOOL flag){	int pam_error;#ifdef PAM_TTY	DEBUG(4,("smb_internal_pam_session: PAM: tty set to: %s\n", tty));	pam_error = pam_set_item(pamh, PAM_TTY, tty);	if (!smb_pam_error_handler(pamh, pam_error, "set tty failed", 0))		return False;#endif	if (flag) {		pam_error = pam_open_session(pamh, PAM_SILENT);		if (!smb_pam_error_handler(pamh, pam_error, "session setup failed", 0))			return False;	} else {		pam_setcred(pamh, (PAM_DELETE_CRED|PAM_SILENT)); /* We don't care if this fails */		pam_error = pam_close_session(pamh, PAM_SILENT); /* This will probably pick up the error anyway */		if (!smb_pam_error_handler(pamh, pam_error, "session close failed", 0))			return False;	}	return (True);}/* * Internal PAM Password Changer. */static BOOL smb_pam_chauthtok(pam_handle_t *pamh, const char * user){	int pam_error;	DEBUG(4,("smb_pam_chauthtok: PAM: Password Change for User: %s\n", user));	pam_error = pam_chauthtok(pamh, PAM_SILENT); /* Change Password */	switch( pam_error ) {	case PAM_AUTHTOK_ERR:		DEBUG(2, ("PAM: unable to obtain the new authentication token - is password to weak?\n"));		break;	/* This doesn't seem to be defined on Solaris. JRA */#ifdef PAM_AUTHTOK_RECOVER_ERR	case PAM_AUTHTOK_RECOVER_ERR:		DEBUG(2, ("PAM: unable to obtain the old authentication token - was the old password wrong?.\n"));		break;#endif	case PAM_AUTHTOK_LOCK_BUSY:		DEBUG(2, ("PAM: unable to change the authentication token since it is currently locked.\n"));		break;	case PAM_AUTHTOK_DISABLE_AGING:		DEBUG(2, ("PAM: Authentication token aging has been disabled.\n"));		break;	case PAM_PERM_DENIED:		DEBUG(0, ("PAM: Permission denied.\n"));		break;	case PAM_TRY_AGAIN:		DEBUG(0, ("PAM: Could not update all authentication token(s). No authentication tokens were updated.\n"));		break;	case PAM_USER_UNKNOWN:		DEBUG(0, ("PAM: User not known to PAM\n"));		break;	case PAM_SUCCESS:		DEBUG(4, ("PAM: Account OK for User: %s\n", user));		break;	default:		DEBUG(0, ("PAM: UNKNOWN PAM ERROR (%d) for User: %s\n", pam_error, user));	} 	if(!smb_pam_error_handler(pamh, pam_error, "Password Change Failed", 2)) {		return False;	}	/* If this point is reached, the password has changed. */	return True;}/* * PAM Externally accessible Session handler */BOOL smb_pam_claim_session(char *user, char *tty, char *rhost){	pam_handle_t *pamh = NULL;	struct pam_conv *pconv = NULL;	/* Ignore PAM if told to. */	if (!lp_obey_pam_restrictions())		return True;	if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)		return False;	if (!smb_pam_start(&pamh, user, rhost, pconv))		return False;	if (!smb_internal_pam_session(pamh, user, tty, True)) {		smb_pam_end(pamh, pconv);		return False;	}	return smb_pam_end(pamh, pconv);}/* * PAM Externally accessible Session handler */BOOL smb_pam_close_session(char *user, char *tty, char *rhost){	pam_handle_t *pamh = NULL;	struct pam_conv *pconv = NULL;	/* Ignore PAM if told to. */	if (!lp_obey_pam_restrictions())		return True;	if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)		return False;	if (!smb_pam_start(&pamh, user, rhost, pconv))		return False;	if (!smb_internal_pam_session(pamh, user, tty, False)) {		smb_pam_end(pamh, pconv);		return False;	}	return smb_pam_end(pamh, pconv);}/* * PAM Externally accessible Account handler */NTSTATUS smb_pam_accountcheck(const char * user){	NTSTATUS nt_status = NT_STATUS_ACCOUNT_DISABLED;	pam_handle_t *pamh = NULL;	struct pam_conv *pconv = NULL;	/* Ignore PAM if told to. */	if (!lp_obey_pam_restrictions())		return NT_STATUS_OK;	if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, NULL, NULL)) == NULL)		return NT_STATUS_NO_MEMORY;	if (!smb_pam_start(&pamh, user, NULL, pconv))		return NT_STATUS_ACCOUNT_DISABLED;	if (!NT_STATUS_IS_OK(nt_status = smb_pam_account(pamh, user)))		DEBUG(0, ("smb_pam_accountcheck: PAM: Account Validation Failed - Rejecting User %s!\n", user));	smb_pam_end(pamh, pconv);	return nt_status;}/* * PAM Password Validation Suite */NTSTATUS smb_pam_passcheck(const char * user, const char * password){	pam_handle_t *pamh = NULL;	NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;	struct pam_conv *pconv = NULL;	/*	 * Note we can't ignore PAM here as this is the only	 * way of doing auths on plaintext passwords when	 * compiled --with-pam.	 */	if ((pconv = smb_setup_pam_conv(smb_pam_conv, user, password, NULL)) == NULL)		return NT_STATUS_LOGON_FAILURE;	if (!smb_pam_start(&pamh, user, NULL, pconv))		return NT_STATUS_LOGON_FAILURE;	if (!NT_STATUS_IS_OK(nt_status = smb_pam_auth(pamh, user))) {		DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_auth failed - Rejecting User %s !\n", user));		smb_pam_end(pamh, pconv);		return nt_status;	}	if (!NT_STATUS_IS_OK(nt_status = smb_pam_account(pamh, user))) {		DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_account failed - Rejecting User %s !\n", user));		smb_pam_end(pamh, pconv);		return nt_status;	}	if (!NT_STATUS_IS_OK(nt_status = smb_pam_setcred(pamh, user))) {		DEBUG(0, ("smb_pam_passcheck: PAM: smb_pam_setcred failed - Rejecting User %s !\n", user));		smb_pam_end(pamh, pconv);		return nt_status;	}	smb_pam_end(pamh, pconv);	return nt_status;}/* * PAM Password Change Suite */BOOL smb_pam_passchange(const char * user, const char * oldpassword, const char * newpassword){	/* Appropriate quantities of root should be obtained BEFORE calling this function */	struct pam_conv *pconv = NULL;	pam_handle_t *pamh = NULL;	if ((pconv = smb_setup_pam_conv(smb_pam_passchange_conv, user, oldpassword, newpassword)) == NULL)		return False;	if(!smb_pam_start(&pamh, user, NULL, pconv))		return False;	if (!smb_pam_chauthtok(pamh, user)) {		DEBUG(0, ("smb_pam_passchange: PAM: Password Change Failed for user %s!\n", user));		smb_pam_end(pamh, pconv);		return False;	}	return smb_pam_end(pamh, pconv);}#else/* If PAM not used, no PAM restrictions on accounts. */NTSTATUS smb_pam_accountcheck(const char * user){	return NT_STATUS_OK;}/* If PAM not used, also no PAM restrictions on sessions. */BOOL smb_pam_claim_session(char *user, char *tty, char *rhost){	return True;}/* If PAM not used, also no PAM restrictions on sessions. */BOOL smb_pam_close_session(char *in_user, char *tty, char *rhost){	return True;}#endif /* WITH_PAM */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?