int main(int argc, const char **argv) { struct ldb_context *ldb; int ret; struct ldb_cmdline *options; const struct ldb_dn *dn1, *dn2; ldb_global_init(); ldb = ldb_init(NULL); options = ldb_cmdline_process(ldb, argc, argv, usage); if (options->argc < 2) { usage(); } dn1 = ldb_dn_explode(ldb, options->argv[0]); dn2 = ldb_dn_explode(ldb, options->argv[1]); ret = ldb_rename(ldb, dn1, dn2); if (ret == 0) { printf("Renamed 1 record\n"); } else { printf("rename of '%s' to '%s' failed - %s\n", options->argv[0], options->argv[1], ldb_errstring(ldb)); } talloc_free(ldb); return ret; }
static NTSTATUS unbecomeDC_ldap_move_computer(struct libnet_UnbecomeDC_state *s) { int ret; struct ldb_result *r; struct ldb_dn *basedn; struct ldb_dn *old_dn; struct ldb_dn *new_dn; static const char *_1_1_attrs[] = { "1.1", NULL }; basedn = ldb_dn_new_fmt(s, s->ldap.ldb, "<WKGUID=aa312825768811d1aded00c04fd8d5cd,%s>", s->domain.dn_str); NT_STATUS_HAVE_NO_MEMORY(basedn); ret = ldb_search(s->ldap.ldb, s, &r, basedn, LDB_SCOPE_BASE, _1_1_attrs, "(objectClass=*)"); talloc_free(basedn); if (ret != LDB_SUCCESS) { return NT_STATUS_LDAP(ret); } else if (r->count != 1) { talloc_free(r); return NT_STATUS_INVALID_NETWORK_RESPONSE; } old_dn = ldb_dn_new(r, s->ldap.ldb, s->dest_dsa.computer_dn_str); NT_STATUS_HAVE_NO_MEMORY(old_dn); new_dn = r->msgs[0]->dn; if (!ldb_dn_add_child_fmt(new_dn, "CN=%s", s->dest_dsa.netbios_name)) { talloc_free(r); return NT_STATUS_NO_MEMORY; } if (ldb_dn_compare(old_dn, new_dn) == 0) { /* we don't need to rename if the old and new dn match */ talloc_free(r); return NT_STATUS_OK; } ret = ldb_rename(s->ldap.ldb, old_dn, new_dn); if (ret != LDB_SUCCESS) { talloc_free(r); return NT_STATUS_LDAP(ret); } s->dest_dsa.computer_dn_str = ldb_dn_alloc_linearized(s, new_dn); NT_STATUS_HAVE_NO_MEMORY(s->dest_dsa.computer_dn_str); talloc_free(r); return NT_STATUS_OK; }
int main(int argc, const char **argv) { struct ldb_context *ldb; int ret; struct ldb_cmdline *options; struct ldb_dn *dn1, *dn2; TALLOC_CTX *mem_ctx = talloc_new(NULL); ldb = ldb_init(mem_ctx, NULL); if (ldb == NULL) { return LDB_ERR_OPERATIONS_ERROR; } options = ldb_cmdline_process(ldb, argc, argv, usage); if (options->argc < 2) { usage(ldb); } dn1 = ldb_dn_new(ldb, ldb, options->argv[0]); dn2 = ldb_dn_new(ldb, ldb, options->argv[1]); if ((dn1 == NULL) || (dn2 == NULL)) { return LDB_ERR_OPERATIONS_ERROR; } ret = ldb_rename(ldb, dn1, dn2); if (ret == LDB_SUCCESS) { printf("Renamed 1 record\n"); } else { printf("rename of '%s' to '%s' failed - %s\n", options->argv[0], options->argv[1], ldb_errstring(ldb)); } talloc_free(mem_ctx); return ret; }
/* process modifies for one file */ static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count) { struct ldb_ldif *ldif; int fun_ret = LDB_SUCCESS, ret; struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls); struct ldif_read_file_state state = { .f = f }; if (options->controls != NULL && req_ctrls== NULL) { printf("parsing controls failed: %s\n", ldb_errstring(ldb)); exit(LDB_ERR_OPERATIONS_ERROR); } fun_ret = ldb_transaction_start(ldb); if (fun_ret != LDB_SUCCESS) { fprintf(stderr, "ERR: (%s) on transaction start\n", ldb_errstring(ldb)); return fun_ret; } while ((ldif = ldb_ldif_read_file_state(ldb, &state))) { struct ldb_dn *olddn; bool deleteoldrdn = false; struct ldb_dn *newdn; const char *errstr = NULL; switch (ldif->changetype) { case LDB_CHANGETYPE_NONE: case LDB_CHANGETYPE_ADD: ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls); break; case LDB_CHANGETYPE_DELETE: ret = ldb_delete_ctrl(ldb, ldif->msg->dn,req_ctrls); break; case LDB_CHANGETYPE_MODIFY: ret = ldb_modify_ctrl(ldb, ldif->msg,req_ctrls); break; case LDB_CHANGETYPE_MODRDN: ret = ldb_ldif_parse_modrdn(ldb, ldif, ldif, &olddn, NULL, &deleteoldrdn, NULL, &newdn); if (ret == LDB_SUCCESS) { if (deleteoldrdn) { ret = ldb_rename(ldb, olddn, newdn); } else { errstr = "modrdn: deleteoldrdn=0 " "not supported."; ret = LDB_ERR_CONSTRAINT_VIOLATION; } } break; } if (ret != LDB_SUCCESS) { if (errstr == NULL) { errstr = ldb_errstring(ldb); } fprintf(stderr, "ERR: (%s) \"%s\" on DN %s at block before line %llu\n", ldb_strerror(ret), errstr, ldb_dn_get_linearized(ldif->msg->dn), (unsigned long long)state.line_no); fun_ret = ret; } else { (*count)++; if (options->verbose) { printf("Modified %s\n", ldb_dn_get_linearized(ldif->msg->dn)); } } ldb_ldif_read_free(ldb, ldif); if (ret) { break; } } if (fun_ret == LDB_SUCCESS && !feof(f)) { fprintf(stderr, "Failed to parse ldif\n"); fun_ret = LDB_ERR_OPERATIONS_ERROR; } if (fun_ret == LDB_SUCCESS) { fun_ret = ldb_transaction_commit(ldb); if (fun_ret != LDB_SUCCESS) { fprintf(stderr, "ERR: (%s) on transaction commit\n", ldb_errstring(ldb)); } } else { ldb_transaction_cancel(ldb); } return fun_ret; }
static NTSTATUS sldb_set(struct share_context *ctx, const char *name, struct share_info *info, int count) { struct ldb_context *ldb; struct ldb_message *msg; TALLOC_CTX *tmp_ctx; NTSTATUS ret; bool do_rename = false; char *newname; int err, i; if (!name) { return NT_STATUS_INVALID_PARAMETER; } tmp_ctx = talloc_new(NULL); if (!tmp_ctx) { DEBUG(0,("ERROR: Out of memory!\n")); return NT_STATUS_NO_MEMORY; } ldb = talloc_get_type(ctx->priv_data, struct ldb_context); msg = ldb_msg_new(tmp_ctx); if (!msg) { DEBUG(0,("ERROR: Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; goto done; } /* TODO: escape name */ msg->dn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", name); if (!msg->dn) { DEBUG(0,("ERROR: Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; goto done; } for (i = 0; i < count; i++) { if (strcasecmp(info[i].name, SHARE_NAME) == 0) { if (strcasecmp(name, (char *)info[i].value) != 0) { do_rename = true; newname = (char *)info[i].value; SHARE_MOD_STRING("cn", (char *)info[i].value); } } switch (info[i].type) { case SHARE_INFO_STRING: SHARE_MOD_STRING(info[i].name, (char *)info[i].value); break; case SHARE_INFO_INT: SHARE_MOD_INT(info[i].name, *((int *)info[i].value)); break; case SHARE_INFO_BLOB: SHARE_MOD_BLOB(info[i].name, (DATA_BLOB *)info[i].value); break; default: DEBUG(2,("ERROR: Invalid share info type for %s\n", info[i].name)); ret = NT_STATUS_INVALID_PARAMETER; goto done; } } if (do_rename) { struct ldb_dn *olddn, *newdn; olddn = msg->dn; /* TODO: escape newname */ newdn = ldb_dn_new_fmt(tmp_ctx, ldb, "CN=%s,CN=SHARES", newname); if (!newdn) { DEBUG(0,("ERROR: Out of memory!\n")); ret = NT_STATUS_NO_MEMORY; goto done; } err = ldb_rename(ldb, olddn, newdn); if (err != LDB_SUCCESS) { DEBUG(2,("ERROR: unable to rename share %s (to %s)\n" " err=%d [%s]\n", name, newname, err, ldb_errstring(ldb))); if (err == LDB_ERR_NO_SUCH_OBJECT) { ret = NT_STATUS_OBJECT_NAME_COLLISION; } else { ret = NT_STATUS_UNSUCCESSFUL; } goto done; } msg->dn = newdn; } err = ldb_modify(ldb, msg); if (err != LDB_SUCCESS) { DEBUG(2,("ERROR: unable to add share %s to share.ldb\n" " err=%d [%s]\n", name, err, ldb_errstring(ldb))); if (err == LDB_ERR_NO_SUCH_OBJECT) { ret = NT_STATUS_OBJECT_NAME_COLLISION; } else { ret = NT_STATUS_UNSUCCESSFUL; } goto done; } ret = NT_STATUS_OK; done: talloc_free(tmp_ctx); return ret; }