📄 ldb.c
字号:
*ret_req = NULL; req = talloc(mem_ctx, struct ldb_request); if (req == NULL) { ldb_set_errstring(ldb, "Out of Memory"); return LDB_ERR_OPERATIONS_ERROR; } req->operation = LDB_EXTENDED; req->op.extended.oid = oid; req->op.extended.data = data; req->controls = controls; req->context = context; req->callback = callback; *ret_req = req; return LDB_SUCCESS;}int ldb_extended(struct ldb_context *ldb, const char *oid, void *data, struct ldb_result **_res){ struct ldb_request *req; int ret; struct ldb_result *res; *_res = NULL; res = talloc_zero(ldb, struct ldb_result); if (!res) { return LDB_ERR_OPERATIONS_ERROR; } ret = ldb_build_extended_req(&req, ldb, ldb, oid, data, NULL, res, ldb_extended_default_callback); if (ret != LDB_SUCCESS) goto done; ldb_set_timeout(ldb, req, 0); /* use default timeout */ ret = ldb_request(ldb, req); if (ret == LDB_SUCCESS) { ret = ldb_wait(req->handle, LDB_WAIT_ALL); } talloc_free(req);done: if (ret != LDB_SUCCESS) { talloc_free(res); } *_res = res; return ret;}/* note that ldb_search() will automatically replace a NULL 'base' value with the defaultNamingContext from the rootDSE if available.*/int ldb_search(struct ldb_context *ldb, struct ldb_dn *base, enum ldb_scope scope, const char *expression, const char * const *attrs, struct ldb_result **_res){ struct ldb_request *req; int ret; struct ldb_result *res; *_res = NULL; res = talloc_zero(ldb, struct ldb_result); if (!res) { return LDB_ERR_OPERATIONS_ERROR; } ret = ldb_build_search_req(&req, ldb, ldb, base?base:ldb_get_default_basedn(ldb), scope, expression, attrs, NULL, res, ldb_search_default_callback); if (ret != LDB_SUCCESS) goto done; ldb_set_timeout(ldb, req, 0); /* use default timeout */ ret = ldb_request(ldb, req); if (ret == LDB_SUCCESS) { ret = ldb_wait(req->handle, LDB_WAIT_ALL); } talloc_free(req);done: if (ret != LDB_SUCCESS) { talloc_free(res); } *_res = res; return ret;}/* a useful search function where you can easily define the expression and that takes a memory context where results are allocated*/int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result, struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs, const char *exp_fmt, ...){ struct ldb_result *res; char *expression; va_list ap; int ret; res = NULL; *result = NULL; va_start(ap, exp_fmt); expression = talloc_vasprintf(mem_ctx, exp_fmt, ap); va_end(ap); if ( ! expression) { return LDB_ERR_OPERATIONS_ERROR; } ret = ldb_search(ldb, base, scope, expression, attrs, &res); if (ret == LDB_SUCCESS) { talloc_steal(mem_ctx, res); *result = res; } talloc_free(expression); return ret;}/* add a record to the database. Will fail if a record with the given class and key already exists*/int ldb_add(struct ldb_context *ldb, const struct ldb_message *message){ struct ldb_request *req; int ret; ret = ldb_msg_sanity_check(ldb, message); if (ret != LDB_SUCCESS) { return ret; } ret = ldb_build_add_req(&req, ldb, ldb, message, NULL, NULL, NULL); if (ret != LDB_SUCCESS) return ret; ldb_set_timeout(ldb, req, 0); /* use default timeout */ /* do request and autostart a transaction */ ret = ldb_autotransaction_request(ldb, req); talloc_free(req); return ret;}/* modify the specified attributes of a record*/int ldb_modify(struct ldb_context *ldb, const struct ldb_message *message){ struct ldb_request *req; int ret; ret = ldb_msg_sanity_check(ldb, message); if (ret != LDB_SUCCESS) { return ret; } ret = ldb_build_mod_req(&req, ldb, ldb, message, NULL, NULL, NULL); if (ret != LDB_SUCCESS) return ret; ldb_set_timeout(ldb, req, 0); /* use default timeout */ /* do request and autostart a transaction */ ret = ldb_autotransaction_request(ldb, req); talloc_free(req); return ret;}/* delete a record from the database*/int ldb_delete(struct ldb_context *ldb, struct ldb_dn *dn){ struct ldb_request *req; int ret; ret = ldb_build_del_req(&req, ldb, ldb, dn, NULL, NULL, NULL); if (ret != LDB_SUCCESS) return ret; ldb_set_timeout(ldb, req, 0); /* use default timeout */ /* do request and autostart a transaction */ ret = ldb_autotransaction_request(ldb, req); talloc_free(req); return ret;}/* rename a record in the database*/int ldb_rename(struct ldb_context *ldb, struct ldb_dn *olddn, struct ldb_dn *newdn){ struct ldb_request *req; int ret; ret = ldb_build_rename_req(&req, ldb, ldb, olddn, newdn, NULL, NULL, NULL); if (ret != LDB_SUCCESS) return ret; ldb_set_timeout(ldb, req, 0); /* use default timeout */ /* do request and autostart a transaction */ ret = ldb_autotransaction_request(ldb, req); talloc_free(req); return ret;}/* return the global sequence number*/int ldb_sequence_number(struct ldb_context *ldb, enum ldb_sequence_type type, uint64_t *seq_num){ struct ldb_request *req; int ret; req = talloc(ldb, struct ldb_request); if (req == NULL) { ldb_set_errstring(ldb, "Out of Memory"); return LDB_ERR_OPERATIONS_ERROR; } req->operation = LDB_SEQUENCE_NUMBER; req->controls = NULL; req->context = NULL; req->callback = NULL; ldb_set_timeout(ldb, req, 0); /* use default timeout */ req->op.seq_num.type = type; /* do request and autostart a transaction */ ret = ldb_request(ldb, req); if (ret == LDB_SUCCESS) { *seq_num = req->op.seq_num.seq_num; } talloc_free(req); return ret;}/* return extended error information */const char *ldb_errstring(struct ldb_context *ldb){ if (ldb->err_string) { return ldb->err_string; } return NULL;}/* return a string explaining what a ldb error constant meancs*/const char *ldb_strerror(int ldb_err){ switch (ldb_err) { case LDB_SUCCESS: return "Success"; case LDB_ERR_OPERATIONS_ERROR: return "Operations error"; case LDB_ERR_PROTOCOL_ERROR: return "Protocol error"; case LDB_ERR_TIME_LIMIT_EXCEEDED: return "Time limit exceeded"; case LDB_ERR_SIZE_LIMIT_EXCEEDED: return "Size limit exceeded"; case LDB_ERR_COMPARE_FALSE: return "Compare false"; case LDB_ERR_COMPARE_TRUE: return "Compare true"; case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED: return "Auth method not supported"; case LDB_ERR_STRONG_AUTH_REQUIRED: return "Strong auth required";/* 9 RESERVED */ case LDB_ERR_REFERRAL: return "Referral error"; case LDB_ERR_ADMIN_LIMIT_EXCEEDED: return "Admin limit exceeded"; case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION: return "Unsupported critical extension"; case LDB_ERR_CONFIDENTIALITY_REQUIRED: return "Confidentiality required"; case LDB_ERR_SASL_BIND_IN_PROGRESS: return "SASL bind in progress"; case LDB_ERR_NO_SUCH_ATTRIBUTE: return "No such attribute"; case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE: return "Undefined attribute type"; case LDB_ERR_INAPPROPRIATE_MATCHING: return "Inappropriate matching"; case LDB_ERR_CONSTRAINT_VIOLATION: return "Constraint violation"; case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS: return "Attribute or value exists"; case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX: return "Invalid attribute syntax";/* 22-31 unused */ case LDB_ERR_NO_SUCH_OBJECT: return "No such object"; case LDB_ERR_ALIAS_PROBLEM: return "Alias problem"; case LDB_ERR_INVALID_DN_SYNTAX: return "Invalid DN syntax";/* 35 RESERVED */ case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM: return "Alias dereferencing problem";/* 37-47 unused */ case LDB_ERR_INAPPROPRIATE_AUTHENTICATION: return "Inappropriate authentication"; case LDB_ERR_INVALID_CREDENTIALS: return "Invalid credentials"; case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS: return "insufficient access rights"; case LDB_ERR_BUSY: return "Busy"; case LDB_ERR_UNAVAILABLE: return "Unavailable"; case LDB_ERR_UNWILLING_TO_PERFORM: return "Unwilling to perform"; case LDB_ERR_LOOP_DETECT: return "Loop detect";/* 55-63 unused */ case LDB_ERR_NAMING_VIOLATION: return "Naming violation"; case LDB_ERR_OBJECT_CLASS_VIOLATION: return "Object class violation"; case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF: return "Not allowed on non-leaf"; case LDB_ERR_NOT_ALLOWED_ON_RDN: return "Not allowed on RDN"; case LDB_ERR_ENTRY_ALREADY_EXISTS: return "Entry already exists"; case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED: return "Object class mods prohibited";/* 70 RESERVED FOR CLDAP */ case LDB_ERR_AFFECTS_MULTIPLE_DSAS: return "Affects multiple DSAs";/* 72-79 unused */ case LDB_ERR_OTHER: return "Other"; } return "Unknown error";}/* set backend specific opaque parameters*/int ldb_set_opaque(struct ldb_context *ldb, const char *name, void *value){ struct ldb_opaque *o; /* allow updating an existing value */ for (o=ldb->opaque;o;o=o->next) { if (strcmp(o->name, name) == 0) { o->value = value; return LDB_SUCCESS; } } o = talloc(ldb, struct ldb_opaque); if (o == NULL) { ldb_oom(ldb); return LDB_ERR_OTHER; } o->next = ldb->opaque; o->name = name; o->value = value; ldb->opaque = o; return LDB_SUCCESS;}/* get a previously set opaque value*/void *ldb_get_opaque(struct ldb_context *ldb, const char *name){ struct ldb_opaque *o; for (o=ldb->opaque;o;o=o->next) { if (strcmp(o->name, name) == 0) { return o->value; } } return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -