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

📄 ldap.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
		rc =  LDAP_NOT_SUPPORTED;		goto done;	}	cookie_be = ber_alloc_t(LBER_USE_DER);	if (cookie && *cookie) {		ber_printf(cookie_be, "{iO}", (ber_int_t) 1000, *cookie);		ber_bvfree(*cookie); /* don't need it from last time */		*cookie = NULL;	} else {		ber_printf(cookie_be, "{io}", (ber_int_t) 1000, "", 0);	}	ber_flatten(cookie_be, &cookie_bv);	PagedResults.ldctl_oid = CONST_DISCARD(char *, ADS_PAGE_CTL_OID);	PagedResults.ldctl_iscritical = (char) 1;	PagedResults.ldctl_value.bv_len = cookie_bv->bv_len;	PagedResults.ldctl_value.bv_val = cookie_bv->bv_val;	NoReferrals.ldctl_oid = CONST_DISCARD(char *, ADS_NO_REFERRALS_OID);	NoReferrals.ldctl_iscritical = (char) 0;	NoReferrals.ldctl_value.bv_len = 0;	NoReferrals.ldctl_value.bv_val = CONST_DISCARD(char *, "");	controls[0] = &NoReferrals;	controls[1] = &PagedResults;	controls[2] = NULL;	/* we need to disable referrals as the openldap libs don't	   handle them and paged results at the same time.  Using them	   together results in the result record containing the server 	   page control being removed from the result list (tridge/jmcd) 		   leaving this in despite the control that says don't generate	   referrals, in case the server doesn't support it (jmcd)	*/	ldap_set_option(ads->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);	rc = ldap_search_with_timeout(ads->ld, utf8_path, scope, utf8_expr, 				      search_attrs, 0, controls,				      NULL, LDAP_NO_LIMIT,				      (LDAPMessage **)res);	ber_free(cookie_be, 1);	ber_bvfree(cookie_bv);	if (rc) {		DEBUG(3,("ads_do_paged_search: ldap_search_with_timeout(%s) -> %s\n", expr,			 ldap_err2string(rc)));		goto done;	}	rc = ldap_parse_result(ads->ld, *res, NULL, NULL, NULL,					NULL, &rcontrols,  0);	if (!rcontrols) {		goto done;	}	for (i=0; rcontrols[i]; i++) {		if (strcmp(ADS_PAGE_CTL_OID, rcontrols[i]->ldctl_oid) == 0) {			cookie_be = ber_init(&rcontrols[i]->ldctl_value);			ber_scanf(cookie_be,"{iO}", (ber_int_t *) count,				  &cookie_bv);			/* the berval is the cookie, but must be freed when			   it is all done */			if (cookie_bv->bv_len) /* still more to do */				*cookie=ber_bvdup(cookie_bv);			else				*cookie=NULL;			ber_bvfree(cookie_bv);			ber_free(cookie_be, 1);			break;		}	}	ldap_controls_free(rcontrols);done:	talloc_destroy(ctx);	/* if/when we decide to utf8-encode attrs, take out this next line */	str_list_free(&search_attrs);	return ADS_ERROR(rc);}/** * Get all results for a search.  This uses ads_do_paged_search() to return  * all entries in a large search. * @param ads connection to ads server  * @param bind_path Base dn for the search * @param scope Scope of search (LDAP_SCOPE_BASE | LDAP_SCOPE_ONE | LDAP_SCOPE_SUBTREE) * @param expr Search expression * @param attrs Attributes to retrieve * @param res ** which will contain results - free res* with ads_msgfree() * @return status of search **/ADS_STATUS ads_do_search_all(ADS_STRUCT *ads, const char *bind_path,			     int scope, const char *expr,			     const char **attrs, void **res){	void *cookie = NULL;	int count = 0;	ADS_STATUS status;	*res = NULL;	status = ads_do_paged_search(ads, bind_path, scope, expr, attrs, res,				     &count, &cookie);	if (!ADS_ERR_OK(status)) 		return status;#ifdef HAVE_LDAP_ADD_RESULT_ENTRY	while (cookie) {		void *res2 = NULL;		ADS_STATUS status2;		LDAPMessage *msg, *next;		status2 = ads_do_paged_search(ads, bind_path, scope, expr, 					      attrs, &res2, &count, &cookie);		if (!ADS_ERR_OK(status2)) break;		/* this relies on the way that ldap_add_result_entry() works internally. I hope		   that this works on all ldap libs, but I have only tested with openldap */		for (msg = ads_first_entry(ads, res2); msg; msg = next) {			next = ads_next_entry(ads, msg);			ldap_add_result_entry((LDAPMessage **)res, msg);		}		/* note that we do not free res2, as the memory is now                   part of the main returned list */	}#else	DEBUG(0, ("no ldap_add_result_entry() support in LDAP libs!\n"));	status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);#endif	return status;}/** * Run a function on all results for a search.  Uses ads_do_paged_search() and *  runs the function as each page is returned, using ads_process_results() * @param ads connection to ads server * @param bind_path Base dn for the search * @param scope Scope of search (LDAP_SCOPE_BASE | LDAP_SCOPE_ONE | LDAP_SCOPE_SUBTREE) * @param expr Search expression - specified in local charset * @param attrs Attributes to retrieve - specified in UTF-8 or ascii * @param fn Function which takes attr name, values list, and data_area * @param data_area Pointer which is passed to function on each call * @return status of search **/ADS_STATUS ads_do_search_all_fn(ADS_STRUCT *ads, const char *bind_path,				int scope, const char *expr, const char **attrs,				BOOL(*fn)(char *, void **, void *), 				void *data_area){	void *cookie = NULL;	int count = 0;	ADS_STATUS status;	void *res;	status = ads_do_paged_search(ads, bind_path, scope, expr, attrs, &res,				     &count, &cookie);	if (!ADS_ERR_OK(status)) return status;	ads_process_results(ads, res, fn, data_area);	ads_msgfree(ads, res);	while (cookie) {		status = ads_do_paged_search(ads, bind_path, scope, expr, attrs,					     &res, &count, &cookie);		if (!ADS_ERR_OK(status)) break;				ads_process_results(ads, res, fn, data_area);		ads_msgfree(ads, res);	}	return status;}/** * Do a search with a timeout. * @param ads connection to ads server * @param bind_path Base dn for the search * @param scope Scope of search (LDAP_SCOPE_BASE | LDAP_SCOPE_ONE | LDAP_SCOPE_SUBTREE) * @param expr Search expression * @param attrs Attributes to retrieve * @param res ** which will contain results - free res* with ads_msgfree() * @return status of search **/ADS_STATUS ads_do_search(ADS_STRUCT *ads, const char *bind_path, int scope, 			 const char *expr,			 const char **attrs, void **res){	int rc;	char *utf8_expr, *utf8_path, **search_attrs = NULL;	TALLOC_CTX *ctx;	*res = NULL;	if (!(ctx = talloc_init("ads_do_search"))) {		DEBUG(1,("ads_do_search: talloc_init() failed!"));		return ADS_ERROR(LDAP_NO_MEMORY);	}	/* 0 means the conversion worked but the result was empty 	   so we only fail if it's negative.  In any case, it always 	   at least nulls out the dest */	if ((push_utf8_talloc(ctx, &utf8_expr, expr) == (size_t)-1) ||	    (push_utf8_talloc(ctx, &utf8_path, bind_path) == (size_t)-1)) {		DEBUG(1,("ads_do_search: push_utf8_talloc() failed!"));		rc = LDAP_NO_MEMORY;		goto done;	}	if (!attrs || !(*attrs))		search_attrs = NULL;	else {		/* This would be the utf8-encoded version...*/		/* if (!(search_attrs = ads_push_strvals(ctx, attrs)))  */		if (!(str_list_copy(&search_attrs, attrs)))		{			DEBUG(1,("ads_do_search: str_list_copy() failed!"));			rc = LDAP_NO_MEMORY;			goto done;		}	}	/* see the note in ads_do_paged_search - we *must* disable referrals */	ldap_set_option(ads->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);	rc = ldap_search_with_timeout(ads->ld, utf8_path, scope, utf8_expr,				      search_attrs, 0, NULL, NULL, 				      LDAP_NO_LIMIT,				      (LDAPMessage **)res);	if (rc == LDAP_SIZELIMIT_EXCEEDED) {		DEBUG(3,("Warning! sizelimit exceeded in ldap. Truncating.\n"));		rc = 0;	} done:	talloc_destroy(ctx);	/* if/when we decide to utf8-encode attrs, take out this next line */	str_list_free(&search_attrs);	return ADS_ERROR(rc);}/** * Do a general ADS search * @param ads connection to ads server * @param res ** which will contain results - free res* with ads_msgfree() * @param expr Search expression * @param attrs Attributes to retrieve * @return status of search **/ADS_STATUS ads_search(ADS_STRUCT *ads, void **res, 		      const char *expr, 		      const char **attrs){	return ads_do_search(ads, ads->config.bind_path, LDAP_SCOPE_SUBTREE, 			     expr, attrs, res);}/** * Do a search on a specific DistinguishedName * @param ads connection to ads server * @param res ** which will contain results - free res* with ads_msgfree() * @param dn DistinguishName to search * @param attrs Attributes to retrieve * @return status of search **/ADS_STATUS ads_search_dn(ADS_STRUCT *ads, void **res, 			 const char *dn, 			 const char **attrs){	return ads_do_search(ads, dn, LDAP_SCOPE_BASE, "(objectclass=*)", attrs, res);}/** * Free up memory from a ads_search * @param ads connection to ads server * @param msg Search results to free **/void ads_msgfree(ADS_STRUCT *ads, void *msg){	if (!msg) return;	ldap_msgfree(msg);}/** * Free up memory from various ads requests * @param ads connection to ads server * @param mem Area to free **/void ads_memfree(ADS_STRUCT *ads, void *mem){	SAFE_FREE(mem);}/** * Get a dn from search results * @param ads connection to ads server * @param msg Search result * @return dn string **/char *ads_get_dn(ADS_STRUCT *ads, void *msg){	char *utf8_dn, *unix_dn;	utf8_dn = ldap_get_dn(ads->ld, msg);	if (!utf8_dn) {		DEBUG (5, ("ads_get_dn: ldap_get_dn failed\n"));		return NULL;	}	if (pull_utf8_allocate(&unix_dn, utf8_dn) == (size_t)-1) {		DEBUG(0,("ads_get_dn: string conversion failure utf8 [%s]\n",			utf8_dn ));		return NULL;	}	ldap_memfree(utf8_dn);	return unix_dn;}/** * Find a machine account given a hostname * @param ads connection to ads server * @param res ** which will contain results - free res* with ads_msgfree() * @param host Hostname to search for * @return status of search **/ADS_STATUS ads_find_machine_acct(ADS_STRUCT *ads, void **res, const char *machine){	ADS_STATUS status;	char *expr;	const char *attrs[] = {"*", "nTSecurityDescriptor", NULL};	*res = NULL;	/* the easiest way to find a machine account anywhere in the tree	   is to look for hostname$ */	if (asprintf(&expr, "(samAccountName=%s$)", machine) == -1) {		DEBUG(1, ("asprintf failed!\n"));		return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);	}		status = ads_search(ads, res, expr, attrs);	SAFE_FREE(expr);	return status;}/** * Initialize a list of mods to be used in a modify request * @param ctx An initialized TALLOC_CTX * @return allocated ADS_MODLIST **/ADS_MODLIST ads_init_mods(TALLOC_CTX *ctx){#define ADS_MODLIST_ALLOC_SIZE 10	LDAPMod **mods;		if ((mods = TALLOC_ZERO_ARRAY(ctx, LDAPMod *, ADS_MODLIST_ALLOC_SIZE + 1)))		/* -1 is safety to make sure we don't go over the end.		   need to reset it to NULL before doing ldap modify */		mods[ADS_MODLIST_ALLOC_SIZE] = (LDAPMod *) -1;		return (ADS_MODLIST)mods;}/*  add an attribute to the list, with values list already constructed*/static ADS_STATUS ads_modlist_add(TALLOC_CTX *ctx, ADS_MODLIST *mods, 				  int mod_op, const char *name, 				  const void **invals){	int curmod;	LDAPMod **modlist = (LDAPMod **) *mods;	struct berval **ber_values = NULL;	char **char_values = NULL;	if (!invals) {		mod_op = LDAP_MOD_DELETE;	} else {		if (mod_op & LDAP_MOD_BVALUES)			ber_values = ads_dup_values(ctx, 						(const struct berval **)invals);		else			char_values = ads_push_strvals(ctx, 						  (const char **) invals);	}	/* find the first empty slot */	for (curmod=0; modlist[curmod] && modlist[curmod] != (LDAPMod *) -1;	     curmod++);	if (modlist[curmod] == (LDAPMod *) -1) {		if (!(modlist = TALLOC_REALLOC_ARRAY(ctx, modlist, LDAPMod *,				curmod+ADS_MODLIST_ALLOC_SIZE+1)))			return ADS_ERROR(LDAP_NO_MEMORY);		memset(&modlist[curmod], 0, 		       ADS_MODLIST_ALLOC_SIZE*sizeof(LDAPMod *));		modlist[curmod+ADS_MODLIST_ALLOC_SIZE] = (LDAPMod *) -1;		*mods = (ADS_MODLIST)modlist;	}			if (!(modlist[curmod] = TALLOC_ZERO_P(ctx, LDAPMod)))		return ADS_ERROR(LDAP_NO_MEMORY);	modlist[curmod]->mod_type = talloc_strdup(ctx, name);	if (mod_op & LDAP_MOD_BVALUES) {		modlist[curmod]->mod_bvalues = ber_values;	} else if (mod_op & LDAP_MOD_DELETE) {		modlist[curmod]->mod_values = NULL;	} else {		modlist[curmod]->mod_values = char_values;	}	modlist[curmod]->mod_op = mod_op;	return ADS_ERROR(LDAP_SUCCESS);}/** * Add a single string value to a mod list * @param ctx An initialized TALLOC_CTX * @param mods An initialized ADS_MODLIST * @param name The attribute name to add * @param val The value to add - NULL means DELETE * @return ADS STATUS indicating success of add **/ADS_STATUS ads_mod_str(TALLOC_CTX *ctx, ADS_MODLIST *mods, 		       const char *name, const char *val){	const char *values[2];	values[0] = val;	values[1] = NULL;	if (!val)		return ads_modlist_add(ctx, mods, LDAP_MOD_DELETE, name, NULL);	return ads_modlist_add(ctx, mods, LDAP_MOD_REPLACE, name, 			       (const void **) values);}/** * Add an array of string values to a mod list * @param ctx An initialized TALLOC_CTX * @param mods An initialized ADS_MODLIST * @param name The attribute name to add * @param vals The array of string values to add - NULL means DELETE * @return ADS STATUS indicating success of add **/ADS_STATUS ads_mod_strlist(TALLOC_CTX *ctx, ADS_MODLIST *mods,			   const char *name, const char **vals){	if (!vals)		return ads_modlist_add(ctx, mods, LDAP_MOD_DELETE, name, NULL);	return ads_modlist_add(ctx, mods, LDAP_MOD_REPLACE, 			       name, (const void **) vals);}/** * Add a single ber-encoded value to a mod list * @param ctx An initialized TALLOC_CTX * @param mods An initialized ADS_MODLIST * @param name The attribute name to add * @param val The value to add - NULL means DELETE * @return ADS STATUS indicating success of add **/static ADS_STATUS ads_mod_ber(TALLOC_CTX *ctx, ADS_MODLIST *mods, 			      const char *name, const struct berval *val){	const struct berval *values[2];	values[0] = val;	values[1] = NULL;

⌨️ 快捷键说明

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