⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdb_nds.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************** Attempts to set the Universal Password**********************************************************************/static int nmasldap_set_password(	LDAP	 *ld,	const char     *objectDN,	const char     *pwd ){	int err = 0;	struct berval *requestBV = NULL;	char *replyOID = NULL;	struct berval *replyBV = NULL;	int serverVersion;	/* Validate char parameters. */	if(objectDN == NULL || (strlen(objectDN) == 0) || pwd == NULL || ld == NULL)	{		return LDAP_NO_SUCH_ATTRIBUTE;	}	err = berEncodePasswordData(&requestBV, objectDN, pwd, NULL);	if(err)	{		goto Cleanup;	}	/* Call the ldap_extended_operation (synchronously) */	if((err = ldap_extended_operation_s(ld, NMASLDAP_SET_PASSWORD_REQUEST, requestBV, NULL, NULL, &replyOID, &replyBV)))	{		goto Cleanup;	}	/* Make sure there is a return OID */	if(!replyOID)	{		err = LDAP_NOT_SUPPORTED;		goto Cleanup;	}	/* Is this what we were expecting to get back. */	if(strcmp(replyOID, NMASLDAP_SET_PASSWORD_RESPONSE))	{		err = LDAP_NOT_SUPPORTED;		goto Cleanup;	}	/* Do we have a good returned berval? */	if(!replyBV)	{		/* No; returned berval means we experienced a rather drastic error. */		/* Return operations error. */		err = LDAP_OPERATIONS_ERROR;		goto Cleanup;	}	err = berDecodeLoginData(replyBV, &serverVersion, NULL, NULL);	if(serverVersion != NMAS_LDAP_EXT_VERSION)	{		err = LDAP_OPERATIONS_ERROR;		goto Cleanup;	}Cleanup:	if(replyBV)	{		ber_bvfree(replyBV);	}	/* Free the return OID string if one was returned. */	if(replyOID)	{		ldap_memfree(replyOID);	}	/* Free memory allocated while building the request ber and berval. */	if(requestBV)	{		ber_bvfree(requestBV);	}	/* Return the appropriate error/success code. */	return err;}/********************************************************************** Attempts to get the Universal Password**********************************************************************/static int nmasldap_get_password(	LDAP	 *ld,	char     *objectDN,	size_t   *pwdSize,	/* in bytes */	unsigned char     *pwd ){	int err = 0;	struct berval *requestBV = NULL;	char *replyOID = NULL;	struct berval *replyBV = NULL;	int serverVersion;	char *pwdBuf;	size_t pwdBufLen, bufferLen;	/* Validate char parameters. */	if(objectDN == NULL || (strlen(objectDN) == 0) || pwdSize == NULL || ld == NULL)	{		return LDAP_NO_SUCH_ATTRIBUTE;	}	bufferLen = pwdBufLen = *pwdSize;	pwdBuf = SMB_MALLOC(pwdBufLen+2);	if(pwdBuf == NULL)	{		return LDAP_NO_MEMORY;	}	err = berEncodePasswordData(&requestBV, objectDN, NULL, NULL);	if(err)	{		goto Cleanup;	}	/* Call the ldap_extended_operation (synchronously) */	if((err = ldap_extended_operation_s(ld, NMASLDAP_GET_PASSWORD_REQUEST, requestBV, NULL, NULL, &replyOID, &replyBV)))	{		goto Cleanup;	}	/* Make sure there is a return OID */	if(!replyOID)	{		err = LDAP_NOT_SUPPORTED;		goto Cleanup;	}	/* Is this what we were expecting to get back. */	if(strcmp(replyOID, NMASLDAP_GET_PASSWORD_RESPONSE))	{		err = LDAP_NOT_SUPPORTED;		goto Cleanup;	}	/* Do we have a good returned berval? */	if(!replyBV)	{		/* No; returned berval means we experienced a rather drastic error. */		/* Return operations error. */		err = LDAP_OPERATIONS_ERROR;		goto Cleanup;	}	err = berDecodeLoginData(replyBV, &serverVersion, &pwdBufLen, pwdBuf);	if(serverVersion != NMAS_LDAP_EXT_VERSION)	{		err = LDAP_OPERATIONS_ERROR;		goto Cleanup;	}	if (!err && pwdBufLen != 0)	{		if (*pwdSize >= pwdBufLen+1 && pwd != NULL)		{			memcpy(pwd, pwdBuf, pwdBufLen);			pwd[pwdBufLen] = 0; /* add null termination */		}		*pwdSize = pwdBufLen; /* does not include null termination */	}Cleanup:	if(replyBV)	{		ber_bvfree(replyBV);	}	/* Free the return OID string if one was returned. */	if(replyOID)	{		ldap_memfree(replyOID);	}	/* Free memory allocated while building the request ber and berval. */	if(requestBV)	{		ber_bvfree(requestBV);	}	if (pwdBuf != NULL)	{		memset(pwdBuf, 0, bufferLen);		free(pwdBuf);	}	/* Return the appropriate error/success code. */	return err;}/********************************************************************** Get the user's password from NDS. *********************************************************************/int pdb_nds_get_password(	struct smbldap_state *ldap_state,	char *object_dn,	size_t *pwd_len,	char *pwd ){	LDAP *ld = ldap_state->ldap_struct;	int rc = -1;	rc = nmasldap_get_password(ld, object_dn, pwd_len, (unsigned char *)pwd);	if (rc == LDAP_SUCCESS) {#ifdef DEBUG_PASSWORD		DEBUG(100,("nmasldap_get_password returned %s for %s\n", pwd, object_dn));#endif    		DEBUG(5, ("NDS Universal Password retrieved for %s\n", object_dn));	} else {		DEBUG(3, ("NDS Universal Password NOT retrieved for %s\n", object_dn));	}	if (rc != LDAP_SUCCESS) {		rc = nmasldap_get_simple_pwd(ld, object_dn, *pwd_len, pwd);		if (rc == LDAP_SUCCESS) {#ifdef DEBUG_PASSWORD			DEBUG(100,("nmasldap_get_simple_pwd returned %s for %s\n", pwd, object_dn));#endif    			DEBUG(5, ("NDS Simple Password retrieved for %s\n", object_dn));		} else {			/* We couldn't get the password */			DEBUG(3, ("NDS Simple Password NOT retrieved for %s\n", object_dn));			return LDAP_INVALID_CREDENTIALS;		}	}	/* We got the password */	return LDAP_SUCCESS;}/********************************************************************** Set the users NDS, Universal and Simple passwords. ********************************************************************/int pdb_nds_set_password(	struct smbldap_state *ldap_state,	char *object_dn,	const char *pwd ){	LDAP *ld = ldap_state->ldap_struct;	int rc = -1;	LDAPMod **tmpmods = NULL;	rc = nmasldap_set_password(ld, object_dn, pwd);	if (rc == LDAP_SUCCESS) {		DEBUG(5,("NDS Universal Password changed for user %s\n", object_dn));	} else {		char *ld_error = NULL;		ldap_get_option(ld, LDAP_OPT_ERROR_STRING, &ld_error);				/* This will fail if Universal Password is not enabled for the user's context */		DEBUG(3,("NDS Universal Password could not be changed for user %s: %s (%s)\n",				 object_dn, ldap_err2string(rc), ld_error?ld_error:"unknown"));		SAFE_FREE(ld_error);	}	/* Set eDirectory Password */	smbldap_set_mod(&tmpmods, LDAP_MOD_REPLACE, "userPassword", pwd);	rc = smbldap_modify(ldap_state, object_dn, tmpmods);	return rc;}/********************************************************************** Allow ldap server to update internal login attempt counters by  performing a simple bind. If the samba authentication failed attempt  the bind with a bogus, randomly generated password to count the  failed attempt. If the bind fails even though samba authentication  succeeded, this would indicate that the user's account is disabled,  time restrictions are in place or some other password policy  violation.*********************************************************************/static NTSTATUS pdb_nds_update_login_attempts(struct pdb_methods *methods,					SAM_ACCOUNT *sam_acct, BOOL success){	struct ldapsam_privates *ldap_state;	if ((!methods) || (!sam_acct)) {		DEBUG(3,("pdb_nds_update_login_attempts: invalid parameter.\n"));		return NT_STATUS_MEMORY_NOT_ALLOCATED;	}	ldap_state = (struct ldapsam_privates *)methods->private_data;	if (ldap_state) {		/* Attempt simple bind with user credentials to update eDirectory		   password policy */		int rc = 0;		char *dn;		LDAPMessage *result = NULL;		LDAPMessage *entry = NULL;		const char **attr_list;		size_t pwd_len;		char clear_text_pw[512];		LDAP *ld = NULL;		const char *username = pdb_get_username(sam_acct);		BOOL got_clear_text_pw = False;		DEBUG(5,("pdb_nds_update_login_attempts: %s login for %s\n",				success ? "Successful" : "Failed", username));		result = pdb_get_backend_private_data(sam_acct, methods);		if (!result) {			attr_list = get_userattr_list(ldap_state->schema_ver);			rc = ldapsam_search_suffix_by_name(ldap_state, username, &result, attr_list );			free_attr_list( attr_list );			if (rc != LDAP_SUCCESS) {				return NT_STATUS_OBJECT_NAME_NOT_FOUND;			}			pdb_set_backend_private_data(sam_acct, result, private_data_free_fn, methods, PDB_CHANGED);		}		if (ldap_count_entries(ldap_state->smbldap_state->ldap_struct, result) == 0) {			DEBUG(0, ("pdb_nds_update_login_attempts: No user to modify!\n"));			return NT_STATUS_OBJECT_NAME_NOT_FOUND;		}		entry = ldap_first_entry(ldap_state->smbldap_state->ldap_struct, result);		dn = smbldap_get_dn(ldap_state->smbldap_state->ldap_struct, entry);		if (!dn) {			return NT_STATUS_OBJECT_NAME_NOT_FOUND;		}		DEBUG(3, ("pdb_nds_update_login_attempts: username %s found dn '%s'\n", username, dn));		pwd_len = sizeof(clear_text_pw);		if (success == True) {			if (pdb_nds_get_password(ldap_state->smbldap_state, dn, &pwd_len, clear_text_pw) == LDAP_SUCCESS) {				/* Got clear text password. Use simple ldap bind */				got_clear_text_pw = True;			}		} else {			generate_random_buffer((unsigned char *)clear_text_pw, 24);			clear_text_pw[24] = '\0';			DEBUG(5,("pdb_nds_update_login_attempts: using random password %s\n", clear_text_pw));		}		if((success != True) || (got_clear_text_pw == True)) {						rc = smb_ldap_setup_full_conn(&ld, ldap_state->location);			if (rc) {				return NT_STATUS_INVALID_CONNECTION;			}			/* Attempt simple bind with real or bogus password */			rc = ldap_simple_bind_s(ld, dn, clear_text_pw);			if (rc == LDAP_SUCCESS) {				DEBUG(5,("pdb_nds_update_login_attempts: ldap_simple_bind_s Successful for %s\n", username));				ldap_unbind_ext(ld, NULL, NULL);			} else {				NTSTATUS nt_status = NT_STATUS_ACCOUNT_RESTRICTION;				DEBUG(5,("pdb_nds_update_login_attempts: ldap_simple_bind_s Failed for %s\n", username));				switch(rc) {					case LDAP_INVALID_CREDENTIALS:						nt_status = NT_STATUS_WRONG_PASSWORD;						break;					case LDAP_UNWILLING_TO_PERFORM:						/* eDir returns this if the account was disabled. */						/* The problem is we don't know if the given						   password was correct for this account or						   not. We have to return more info than we						   should and tell the client NT_STATUS_ACCOUNT_DISABLED						   so they don't think the password was bad. JRA. */						nt_status = NT_STATUS_ACCOUNT_DISABLED;						break;					default:						break;				}				return nt_status;			}		}	}		return NT_STATUS_OK;}/********************************************************************** Intitalise the parts of the pdb_context that are common to NDS_ldapsam modes *********************************************************************/static NTSTATUS pdb_init_NDS_ldapsam_common(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location){	struct ldapsam_privates *ldap_state = (*pdb_method)->private_data;	/* Mark this as eDirectory ldap */	ldap_state->is_nds_ldap = True;	/* Add pdb_nds specific method for updating login attempts. */	(*pdb_method)->update_login_attempts = pdb_nds_update_login_attempts;	/* Save location for use in pdb_nds_update_login_attempts */	ldap_state->location = SMB_STRDUP(location);	return NT_STATUS_OK;}/********************************************************************** Initialise the 'nds compat' mode for pdb_ldap *********************************************************************/static NTSTATUS pdb_init_NDS_ldapsam_compat(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location){	NTSTATUS nt_status = pdb_init_ldapsam_compat(pdb_context, pdb_method, location);	(*pdb_method)->name = "NDS_ldapsam_compat";	pdb_init_NDS_ldapsam_common(pdb_context, pdb_method, location);	return nt_status;}/********************************************************************** Initialise the 'nds' normal mode for pdb_ldap *********************************************************************/static NTSTATUS pdb_init_NDS_ldapsam(PDB_CONTEXT *pdb_context, PDB_METHODS **pdb_method, const char *location){	NTSTATUS nt_status = pdb_init_ldapsam(pdb_context, pdb_method, location);	(*pdb_method)->name = "NDS_ldapsam";	pdb_init_NDS_ldapsam_common(pdb_context, pdb_method, location);	return nt_status;}NTSTATUS pdb_nds_init(void){	NTSTATUS nt_status;	if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "NDS_ldapsam", pdb_init_NDS_ldapsam)))		return nt_status;	if (!NT_STATUS_IS_OK(nt_status = smb_register_passdb(PASSDB_INTERFACE_VERSION, "NDS_ldapsam_compat", pdb_init_NDS_ldapsam_compat)))		return nt_status;	return NT_STATUS_OK;}

⌨️ 快捷键说明

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