📄 ldif.c
字号:
entry_free(entry); if(path.bv_val != NULL) SLAP_FREE(path.bv_val); rs->sr_text = NULL; ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr); send_ldap_result(op, rs); slap_graduate_commit_csn( op ); return 0;}static int ldif_back_delete(Operation *op, SlapReply *rs) { struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private; struct berval path = BER_BVNULL; int res = 0; if ( BER_BVISEMPTY( &op->o_csn )) { struct berval csn; char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE]; csn.bv_val = csnbuf; csn.bv_len = sizeof( csnbuf ); slap_get_csn( op, &csn, 1 ); } ldap_pvt_thread_rdwr_wlock(&ni->li_rdwr); dn2path(&op->o_req_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path, &path); path.bv_val[path.bv_len - STRLENOF(LDIF)] = '\0'; res = rmdir(path.bv_val); path.bv_val[path.bv_len - STRLENOF(LDIF)] = '.'; if ( res && errno != ENOENT ) { rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF; } else { res = unlink(path.bv_val); } if(res == -1) { if(errno == ENOENT) rs->sr_err = LDAP_NO_SUCH_OBJECT; else rs->sr_err = LDAP_UNWILLING_TO_PERFORM; } else rs->sr_err = LDAP_SUCCESS; SLAP_FREE(path.bv_val); ldap_pvt_thread_rdwr_wunlock(&ni->li_rdwr); send_ldap_result(op, rs); slap_graduate_commit_csn( op ); return 0;}static int move_entry(Entry * entry, struct berval * ndn, struct berval * newndn, struct berval * suffixdn, struct berval * base_path) { int res; int exists_res; struct berval path; struct berval newpath; dn2path(ndn, suffixdn, base_path, &path); dn2path(newndn, suffixdn, base_path, &newpath); if((entry == NULL || path.bv_val == NULL) || newpath.bv_val == NULL) { /* some object doesn't exist */ res = LDAP_NO_SUCH_OBJECT; } else { /* do the modrdn */ exists_res = open(newpath.bv_val, O_RDONLY); if(exists_res == -1 && errno == ENOENT) { ldap_pvt_thread_mutex_lock( &entry2str_mutex ); res = spew_entry(entry, &newpath); if(res != -1) { /* if this fails we should log something bad */ res = unlink(path.bv_val); res = LDAP_SUCCESS; } else { if(errno == ENOENT) res = LDAP_NO_SUCH_OBJECT; else res = LDAP_UNWILLING_TO_PERFORM; unlink(newpath.bv_val); /* in case file was created */ } ldap_pvt_thread_mutex_unlock( &entry2str_mutex ); } else if(exists_res) { int close_res = close(exists_res); res = LDAP_ALREADY_EXISTS; if(close_res == -1) { /* log heinous error */ } } else { res = LDAP_UNWILLING_TO_PERFORM; } } if(newpath.bv_val != NULL) SLAP_FREE(newpath.bv_val); if(path.bv_val != NULL) SLAP_FREE(path.bv_val); return res;}static int ldif_back_modrdn(Operation *op, SlapReply *rs) { struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private; struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL; struct berval p_dn, bv = BER_BVNULL; Entry * entry = NULL; LDAPRDN new_rdn = NULL; LDAPRDN old_rdn = NULL; Modifications * mods = NULL; int res; ldap_pvt_thread_rdwr_wlock( &ni->li_rdwr ); entry = (Entry *) get_entry(op, &ni->li_base_path); /* build the mods to the entry */ if(entry != NULL) { if(ldap_bv2rdn(&op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) { rs->sr_err = LDAP_INVALID_DN_SYNTAX; } else if(op->oq_modrdn.rs_deleteoldrdn && ldap_bv2rdn(&op->o_req_dn, &old_rdn, (char **)&rs->sr_text, LDAP_DN_FORMAT_LDAP)) { rs->sr_err = LDAP_OTHER; } else { /* got both rdns successfully, ready to build mods */ if(slap_modrdn2mods(op, rs, entry, old_rdn, new_rdn, &mods) != LDAP_SUCCESS) { rs->sr_err = LDAP_UNWILLING_TO_PERFORM; } else { /* built mods successfully */ /* build new dn, and new ndn for the entry */ if(op->oq_modrdn.rs_newSup != NULL) { struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn; Entry *np; /* new superior */ p_dn = *op->oq_modrdn.rs_newSup; op->o_req_dn = *op->oq_modrdn.rs_newSup; op->o_req_ndn = *op->oq_modrdn.rs_nnewSup; np = (Entry *)get_entry( op, &ni->li_base_path ); op->o_req_dn = op_dn; op->o_req_ndn = op_ndn; if ( np == NULL ) { goto no_such_object; } entry_free( np ); } else { dnParent(&entry->e_name, &p_dn); } build_new_dn(&new_dn, &p_dn, &op->oq_modrdn.rs_newrdn, NULL); dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx ); ber_dupbv( &new_ndn, &bv ); entry->e_name = new_dn; entry->e_nname = new_ndn; /* perform the modifications */ res = apply_modify_to_entry(entry, mods, op, rs); if(res == LDAP_SUCCESS) { rs->sr_err = move_entry(entry, &op->o_req_ndn, &new_ndn, &op->o_bd->be_nsuffix[0], &ni->li_base_path); } else { rs->sr_err = res; } } } } else {no_such_object:; /* entry was null */ rs->sr_err = LDAP_NO_SUCH_OBJECT; } if ( entry != NULL ) { entry_free(entry); } rs->sr_text = ""; ldap_pvt_thread_rdwr_wunlock( &ni->li_rdwr ); send_ldap_result(op, rs); slap_graduate_commit_csn( op ); return 0;}/* return LDAP_SUCCESS IFF we can retrieve the specified entry. */int ldif_back_entry_get( Operation *op, struct berval *ndn, ObjectClass *oc, AttributeDescription *at, int rw, Entry **ent ){ struct ldif_info *ni = (struct ldif_info *) op->o_bd->be_private; struct berval op_dn = op->o_req_dn, op_ndn = op->o_req_ndn; assert( ndn != NULL ); assert( !BER_BVISNULL( ndn ) ); ldap_pvt_thread_rdwr_rlock( &ni->li_rdwr ); op->o_req_dn = *ndn; op->o_req_ndn = *ndn; *ent = (Entry *) get_entry( op, &ni->li_base_path ); op->o_req_dn = op_dn; op->o_req_ndn = op_ndn; ldap_pvt_thread_rdwr_runlock( &ni->li_rdwr ); if ( *ent && oc && !is_entry_objectclass_or_sub( *ent, oc ) ) { entry_free( *ent ); *ent = NULL; } return ( *ent == NULL ? 1 : 0 );}static int ldif_tool_entry_open(BackendDB * be, int mode) { struct ldif_info *ni = (struct ldif_info *) be->be_private; ni->li_tool_current = 0; return 0;} static int ldif_tool_entry_close(BackendDB * be) { struct ldif_info *ni = (struct ldif_info *) be->be_private; SLAP_FREE(ni->li_tool_cookie.entries); return 0;}static IDldif_tool_entry_first(BackendDB *be){ struct ldif_info *ni = (struct ldif_info *) be->be_private; ID id = 1; /* first entry in the array of entries shifted by one */ ni->li_tool_current = 1; if(ni->li_tool_cookie.entries == NULL) { Operation op = {0}; op.o_bd = be; op.o_req_dn = *be->be_suffix; op.o_req_ndn = *be->be_nsuffix; op.ors_scope = LDAP_SCOPE_SUBTREE; ni->li_tool_cookie.op = &op; (void)enum_tree( &ni->li_tool_cookie ); ni->li_tool_cookie.op = NULL; } return id;}static ID ldif_tool_entry_next(BackendDB *be){ struct ldif_info *ni = (struct ldif_info *) be->be_private; ni->li_tool_current += 1; if(ni->li_tool_current > ni->li_tool_cookie.eind) return NOID; else return ni->li_tool_current;}static Entry * ldif_tool_entry_get(BackendDB * be, ID id) { struct ldif_info *ni = (struct ldif_info *) be->be_private; Entry * e; if(id > ni->li_tool_cookie.eind || id < 1) return NULL; else { e = ni->li_tool_cookie.entries[id - 1]; ni->li_tool_cookie.entries[id - 1] = NULL; return e; }}static ID ldif_tool_entry_put(BackendDB * be, Entry * e, struct berval *text) { struct ldif_info *ni = (struct ldif_info *) be->be_private; struct berval dn = e->e_nname; struct berval leaf_path = BER_BVNULL; struct stat stats; int statres; int res = LDAP_SUCCESS; dn2path(&dn, &be->be_nsuffix[0], &ni->li_base_path, &leaf_path); if(leaf_path.bv_val != NULL) { struct berval base = BER_BVNULL; /* build path to container, and path to ldif of container */ get_parent_path(&leaf_path, &base); statres = stat(base.bv_val, &stats); /* check if container exists */ if(statres == -1 && errno == ENOENT) { /* container missing */ base.bv_val[base.bv_len] = '.'; statres = stat(base.bv_val, &stats); /* check for leaf node */ base.bv_val[base.bv_len] = '\0'; if(statres == -1 && errno == ENOENT) { res = LDAP_NO_SUCH_OBJECT; /* parent doesn't exist */ } else if(statres != -1) { /* create parent */ int mkdirres = mkdir(base.bv_val, 0750); if(mkdirres == -1) { res = LDAP_UNWILLING_TO_PERFORM; } } else res = LDAP_UNWILLING_TO_PERFORM; }/* container was possibly created, move on to add the entry */ if(res == LDAP_SUCCESS) { statres = stat(leaf_path.bv_val, &stats); if(statres == -1 && errno == ENOENT) { res = (int) spew_entry(e, &leaf_path); } else /* it already exists */ res = LDAP_ALREADY_EXISTS; } SLAP_FREE(base.bv_val); SLAP_FREE(leaf_path.bv_val); } if(res == LDAP_SUCCESS) { return 1; } else return NOID;}static intldif_back_db_init( BackendDB *be ){ struct ldif_info *ni; ni = ch_calloc( 1, sizeof(struct ldif_info) ); be->be_private = ni; be->be_cf_ocs = ldifocs; ldap_pvt_thread_rdwr_init(&ni->li_rdwr); return 0;}static intldif_back_db_destroy( Backend *be ){ struct ldif_info *ni = be->be_private; ch_free(ni->li_base_path.bv_val); ldap_pvt_thread_rdwr_destroy(&ni->li_rdwr); free( be->be_private ); return 0;}static intldif_back_db_open( Backend *be ){ struct ldif_info *ni = (struct ldif_info *) be->be_private; if( BER_BVISEMPTY(&ni->li_base_path)) {/* missing base path */ Debug( LDAP_DEBUG_ANY, "missing base path for back-ldif\n", 0, 0, 0); return 1; } return 0;}intldif_back_initialize( BackendInfo *bi ){ static char *controls[] = { LDAP_CONTROL_MANAGEDSAIT, NULL }; int rc; bi->bi_flags |= SLAP_BFLAG_INCREMENT | SLAP_BFLAG_REFERRALS; bi->bi_controls = controls; bi->bi_open = 0; bi->bi_close = 0; bi->bi_config = 0; bi->bi_destroy = 0; bi->bi_db_init = ldif_back_db_init; bi->bi_db_config = config_generic_wrapper; bi->bi_db_open = ldif_back_db_open; bi->bi_db_close = 0; bi->bi_db_destroy = ldif_back_db_destroy; bi->bi_op_bind = ldif_back_bind; bi->bi_op_unbind = 0; bi->bi_op_search = ldif_back_search; bi->bi_op_compare = 0; bi->bi_op_modify = ldif_back_modify; bi->bi_op_modrdn = ldif_back_modrdn; bi->bi_op_add = ldif_back_add; bi->bi_op_delete = ldif_back_delete; bi->bi_op_abandon = 0; bi->bi_extended = 0; bi->bi_chk_referrals = ldif_back_referrals; bi->bi_connection_init = 0; bi->bi_connection_destroy = 0; bi->bi_entry_get_rw = ldif_back_entry_get;#if 0 /* NOTE: uncomment to completely disable access control */#ifdef SLAP_OVERLAY_ACCESS bi->bi_access_allowed = slap_access_always_allowed;#endif /* SLAP_OVERLAY_ACCESS */#endif bi->bi_tool_entry_open = ldif_tool_entry_open; bi->bi_tool_entry_close = ldif_tool_entry_close; bi->bi_tool_entry_first = ldif_tool_entry_first; bi->bi_tool_entry_next = ldif_tool_entry_next; bi->bi_tool_entry_get = ldif_tool_entry_get; bi->bi_tool_entry_put = ldif_tool_entry_put; bi->bi_tool_entry_reindex = 0; bi->bi_tool_sync = 0; bi->bi_tool_dn2id_get = 0; bi->bi_tool_id2entry_get = 0; bi->bi_tool_entry_modify = 0; bi->bi_cf_ocs = ldifocs; rc = config_register_schema( ldifcfg, ldifocs ); if ( rc ) return rc; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -