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

📄 ldb_msg.c

📁 samba最新软件
💻 C
📖 第 1 页 / 共 2 页
字号:
	msg2 = talloc(mem_ctx, struct ldb_message);	if (msg2 == NULL) return NULL;	*msg2 = *msg;	msg2->elements = talloc_array(msg2, struct ldb_message_element, 				      msg2->num_elements);	if (msg2->elements == NULL) goto failed;	for (i=0;i<msg2->num_elements;i++) {		msg2->elements[i] = msg->elements[i];	}	return msg2;failed:	talloc_free(msg2);	return NULL;}/*  copy a message, allocating new memory for all parts*/struct ldb_message *ldb_msg_copy(TALLOC_CTX *mem_ctx, 				 const struct ldb_message *msg){	struct ldb_message *msg2;	int i, j;	msg2 = ldb_msg_copy_shallow(mem_ctx, msg);	if (msg2 == NULL) return NULL;	msg2->dn = ldb_dn_copy(msg2, msg2->dn);	if (msg2->dn == NULL) goto failed;	for (i=0;i<msg2->num_elements;i++) {		struct ldb_message_element *el = &msg2->elements[i];		struct ldb_val *values = el->values;		el->name = talloc_strdup(msg2->elements, el->name);		if (el->name == NULL) goto failed;		el->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);		for (j=0;j<el->num_values;j++) {			el->values[j] = ldb_val_dup(el->values, &values[j]);			if (el->values[j].data == NULL && values[j].length != 0) {				goto failed;			}		}	}	return msg2;failed:	talloc_free(msg2);	return NULL;}/*  canonicalise a message, merging elements of the same name*/struct ldb_message *ldb_msg_canonicalize(struct ldb_context *ldb, 					 const struct ldb_message *msg){	int i;	struct ldb_message *msg2;	msg2 = ldb_msg_copy(ldb, msg);	if (msg2 == NULL) return NULL;	ldb_msg_sort_elements(msg2);	for (i=1;i<msg2->num_elements;i++) {		struct ldb_message_element *el1 = &msg2->elements[i-1];		struct ldb_message_element *el2 = &msg2->elements[i];		if (ldb_msg_element_compare_name(el1, el2) == 0) {			el1->values = talloc_realloc(msg2->elements, el1->values, struct ldb_val, 						       el1->num_values + el2->num_values);			if (el1->values == NULL) {				return NULL;			}			memcpy(el1->values + el1->num_values,			       el2->values,			       sizeof(struct ldb_val) * el2->num_values);			el1->num_values += el2->num_values;			talloc_free(discard_const_p(char, el2->name));			if (i+1<msg2->num_elements) {				memmove(el2, el2+1, sizeof(struct ldb_message_element) * 					(msg2->num_elements - (i+1)));			}			msg2->num_elements--;			i--;		}	}	return msg2;}/*  return a ldb_message representing the differences between msg1 and msg2. If you  then use this in a ldb_modify() call it can be used to save edits to a message*/struct ldb_message *ldb_msg_diff(struct ldb_context *ldb, 				 struct ldb_message *msg1,				 struct ldb_message *msg2){	struct ldb_message *mod;	struct ldb_message_element *el;	unsigned int i;	mod = ldb_msg_new(ldb);	mod->dn = msg1->dn;	mod->num_elements = 0;	mod->elements = NULL;	msg2 = ldb_msg_canonicalize(ldb, msg2);	if (msg2 == NULL) {		return NULL;	}		/* look in msg2 to find elements that need to be added	   or modified */	for (i=0;i<msg2->num_elements;i++) {		el = ldb_msg_find_element(msg1, msg2->elements[i].name);		if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {			continue;		}		if (ldb_msg_add(mod, 				&msg2->elements[i],				el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {			return NULL;		}	}	/* look in msg1 to find elements that need to be deleted */	for (i=0;i<msg1->num_elements;i++) {		el = ldb_msg_find_element(msg2, msg1->elements[i].name);		if (!el) {			if (ldb_msg_add_empty(mod, 					      msg1->elements[i].name,					      LDB_FLAG_MOD_DELETE, NULL) != 0) {				return NULL;			}		}	}	return mod;}int ldb_msg_sanity_check(struct ldb_context *ldb, 			 const struct ldb_message *msg){	int i, j;	/* basic check on DN */	if (msg->dn == NULL) {		/* TODO: return also an error string */		ldb_set_errstring(ldb, "ldb message lacks a DN!");		return LDB_ERR_INVALID_DN_SYNTAX;	}	/* basic syntax checks */	for (i = 0; i < msg->num_elements; i++) {		for (j = 0; j < msg->elements[i].num_values; j++) {			if (msg->elements[i].values[j].length == 0) {				TALLOC_CTX *mem_ctx = talloc_new(ldb);				/* an attribute cannot be empty */				/* TODO: return also an error string */				ldb_asprintf_errstring(ldb, "Element %s has empty attribute in ldb message (%s)!",							    msg->elements[i].name, 							    ldb_dn_get_linearized(msg->dn));				talloc_free(mem_ctx);				return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;			}		}	}	return LDB_SUCCESS;}/*  copy an attribute list. This only copies the array, not the elements  (ie. the elements are left as the same pointers)*/const char **ldb_attr_list_copy(TALLOC_CTX *mem_ctx, const char * const *attrs){	const char **ret;	int i;	for (i=0;attrs[i];i++) /* noop */ ;	ret = talloc_array(mem_ctx, const char *, i+1);	if (ret == NULL) {		return NULL;	}	for (i=0;attrs[i];i++) {		ret[i] = attrs[i];	}	ret[i] = attrs[i];	return ret;}/*  copy an attribute list. This only copies the array, not the elements  (ie. the elements are left as the same pointers).  The new attribute is added to the list.*/const char **ldb_attr_list_copy_add(TALLOC_CTX *mem_ctx, const char * const *attrs, const char *new_attr){	const char **ret;	int i;	bool found = false;	for (i=0;attrs[i];i++) {		if (ldb_attr_cmp(attrs[i], new_attr) == 0) {			found = true;		}	}	if (found) {		return ldb_attr_list_copy(mem_ctx, attrs);	}	ret = talloc_array(mem_ctx, const char *, i+2);	if (ret == NULL) {		return NULL;	}	for (i=0;attrs[i];i++) {		ret[i] = attrs[i];	}	ret[i] = new_attr;	ret[i+1] = NULL;	return ret;}/*  return 1 if an attribute is in a list of attributes, or 0 otherwise*/int ldb_attr_in_list(const char * const *attrs, const char *attr){	int i;	for (i=0;attrs && attrs[i];i++) {		if (ldb_attr_cmp(attrs[i], attr) == 0) {			return 1;		}	}	return 0;}/*  rename the specified attribute in a search result*/int ldb_msg_rename_attr(struct ldb_message *msg, const char *attr, const char *replace){	struct ldb_message_element *el = ldb_msg_find_element(msg, attr);	if (el == NULL) {		return LDB_SUCCESS;	}	el->name = talloc_strdup(msg->elements, replace);	if (el->name == NULL) {		return LDB_ERR_OPERATIONS_ERROR;	}	return LDB_SUCCESS;}/*  copy the specified attribute in a search result to a new attribute*/int ldb_msg_copy_attr(struct ldb_message *msg, const char *attr, const char *replace){	struct ldb_message_element *el = ldb_msg_find_element(msg, attr);	if (el == NULL) {		return LDB_SUCCESS;	}	if (ldb_msg_add(msg, el, 0) != 0) {		return LDB_ERR_OPERATIONS_ERROR;	}	return ldb_msg_rename_attr(msg, attr, replace);}/*  remove the specified element in a search result*/void ldb_msg_remove_element(struct ldb_message *msg, struct ldb_message_element *el){	int n = (el - msg->elements);	if (n != msg->num_elements-1) {		memmove(el, el+1, ((msg->num_elements-1) - n)*sizeof(*el));	}	msg->num_elements--;}/*  remove the specified attribute in a search result*/void ldb_msg_remove_attr(struct ldb_message *msg, const char *attr){	struct ldb_message_element *el = ldb_msg_find_element(msg, attr);	if (el) {		ldb_msg_remove_element(msg, el);	}}/*  return a LDAP formatted GeneralizedTime string*/char *ldb_timestring(TALLOC_CTX *mem_ctx, time_t t){	struct tm *tm = gmtime(&t);	char *ts;	int r;	if (!tm) {		return NULL;	}	/* we now excatly how long this string will be */	ts = talloc_array(mem_ctx, char, 18);	/* formatted like: 20040408072012.0Z */	r = snprintf(ts, 18,			"%04u%02u%02u%02u%02u%02u.0Z",			tm->tm_year+1900, tm->tm_mon+1,			tm->tm_mday, tm->tm_hour, tm->tm_min,			tm->tm_sec);	if (r != 17) {		talloc_free(ts);		return NULL;	}	return ts;}/*  convert a LDAP GeneralizedTime string to a time_t. Return 0 if unable to convert*/time_t ldb_string_to_time(const char *s){	struct tm tm;		if (s == NULL) return 0;		memset(&tm, 0, sizeof(tm));	if (sscanf(s, "%04u%02u%02u%02u%02u%02u", 		   &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 		   &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {		return 0;	}	tm.tm_year -= 1900;	tm.tm_mon -= 1;		return timegm(&tm);}/*  return a LDAP formatted UTCTime string*/char *ldb_timestring_utc(TALLOC_CTX *mem_ctx, time_t t){	struct tm *tm = gmtime(&t);	char *ts;	int r;	if (!tm) {		return NULL;	}	/* we now excatly how long this string will be */	ts = talloc_array(mem_ctx, char, 14);	/* formatted like: 20040408072012.0Z => 040408072012Z */	r = snprintf(ts, 14,			"%02u%02u%02u%02u%02u%02uZ",			(tm->tm_year+1900)%100, tm->tm_mon+1,			tm->tm_mday, tm->tm_hour, tm->tm_min,			tm->tm_sec);	if (r != 13) {		talloc_free(ts);		return NULL;	}	return ts;}/*  convert a LDAP UTCTime string to a time_t. Return 0 if unable to convert*/time_t ldb_string_utc_to_time(const char *s){	struct tm tm;		if (s == NULL) return 0;		memset(&tm, 0, sizeof(tm));	if (sscanf(s, "%02u%02u%02u%02u%02u%02u", 		   &tm.tm_year, &tm.tm_mon, &tm.tm_mday, 		   &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {		return 0;	}	if (tm.tm_year < 50) {		tm.tm_year += 100;	}	tm.tm_mon -= 1;		return timegm(&tm);}/*  dump a set of results to a file. Useful from within gdb*/void ldb_dump_results(struct ldb_context *ldb, struct ldb_result *result, FILE *f){	int i;	for (i = 0; i < result->count; i++) {		struct ldb_ldif ldif;		fprintf(f, "# record %d\n", i+1);		ldif.changetype = LDB_CHANGETYPE_NONE;		ldif.msg = result->msgs[i];		ldb_ldif_write_file(ldb, f, &ldif);	}}int ldb_msg_check_string_attribute(const struct ldb_message *msg, const char *name, const char *value){	struct ldb_message_element *el;	struct ldb_val val;		el = ldb_msg_find_element(msg, name);	if (el == NULL)		return 0;	val.data = discard_const_p(uint8_t, value);	val.length = strlen(value);	if (ldb_msg_find_val(el, &val))		return 1;	return 0;}

⌨️ 快捷键说明

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