s3:ntlm_auth: make logs more consistent with length check
[Samba.git] / lib / ldb / tools / ldbmodify.c
blob2eb8bcc300230e34af647e792802ff85db2e57e6
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 * Name: ldb
27 * Component: ldbmodify
29 * Description: utility to modify records - modelled on ldapmodify
31 * Author: Andrew Tridgell
34 #include "replace.h"
35 #include "ldb.h"
36 #include "tools/cmdline.h"
37 #include "ldbutil.h"
38 #include "include/ldb_private.h"
40 static struct ldb_cmdline *options;
42 static void usage(struct ldb_context *ldb)
44 printf("Usage: ldbmodify <options> <ldif...>\n");
45 printf("Modifies a ldb based upon ldif change records\n\n");
46 ldb_cmdline_help(ldb, "ldbmodify", stdout);
47 exit(LDB_ERR_OPERATIONS_ERROR);
51 process modifies for one file
53 static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count)
55 struct ldb_ldif *ldif;
56 int fun_ret = LDB_SUCCESS, ret;
57 struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
58 struct ldif_read_file_state state = {
59 .f = f
62 if (options->controls != NULL && req_ctrls== NULL) {
63 printf("parsing controls failed: %s\n", ldb_errstring(ldb));
64 exit(LDB_ERR_OPERATIONS_ERROR);
67 fun_ret = ldb_transaction_start(ldb);
68 if (fun_ret != LDB_SUCCESS) {
69 fprintf(stderr, "ERR: (%s) on transaction start\n",
70 ldb_errstring(ldb));
71 return fun_ret;
74 while ((ldif = ldb_ldif_read_file_state(ldb, &state))) {
75 struct ldb_dn *olddn;
76 bool deleteoldrdn = false;
77 struct ldb_dn *newdn;
78 const char *errstr = NULL;
80 switch (ldif->changetype) {
81 case LDB_CHANGETYPE_NONE:
82 case LDB_CHANGETYPE_ADD:
83 ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
84 break;
85 case LDB_CHANGETYPE_DELETE:
86 ret = ldb_delete_ctrl(ldb, ldif->msg->dn,req_ctrls);
87 break;
88 case LDB_CHANGETYPE_MODIFY:
89 ret = ldb_modify_ctrl(ldb, ldif->msg,req_ctrls);
90 break;
91 case LDB_CHANGETYPE_MODRDN:
92 ret = ldb_ldif_parse_modrdn(ldb, ldif, ldif, &olddn,
93 NULL, &deleteoldrdn,
94 NULL, &newdn);
95 if (ret == LDB_SUCCESS) {
96 if (deleteoldrdn) {
97 ret = ldb_rename(ldb, olddn, newdn);
98 } else {
99 errstr = "modrdn: deleteoldrdn=0 "
100 "not supported.";
101 ret = LDB_ERR_CONSTRAINT_VIOLATION;
104 break;
105 default:
106 ret = LDB_ERR_PROTOCOL_ERROR;
107 break;
109 if (ret != LDB_SUCCESS) {
110 if (errstr == NULL) {
111 errstr = ldb_errstring(ldb);
113 fprintf(stderr,
114 "ERR: (%s) \"%s\" on DN %s at block before "
115 "line %zu\n",
116 ldb_strerror(ret),
117 errstr,
118 ldb_dn_get_linearized(ldif->msg->dn),
119 state.line_no);
120 fun_ret = ret;
121 } else {
122 (*count)++;
123 if (options->verbose) {
124 printf("Modified %s\n", ldb_dn_get_linearized(ldif->msg->dn));
127 ldb_ldif_read_free(ldb, ldif);
128 if (ret) {
129 break;
133 if (fun_ret == LDB_SUCCESS && !feof(f)) {
134 fprintf(stderr, "Failed to parse ldif\n");
135 fun_ret = LDB_ERR_OPERATIONS_ERROR;
138 if (fun_ret == LDB_SUCCESS) {
139 fun_ret = ldb_transaction_commit(ldb);
140 if (fun_ret != LDB_SUCCESS) {
141 fprintf(stderr, "ERR: (%s) on transaction commit\n",
142 ldb_errstring(ldb));
144 } else {
145 ldb_transaction_cancel(ldb);
148 return fun_ret;
151 int main(int argc, const char **argv)
153 struct ldb_context *ldb;
154 unsigned int i, count = 0;
155 int ret = LDB_SUCCESS;
156 TALLOC_CTX *mem_ctx = talloc_new(NULL);
158 ldb = ldb_init(mem_ctx, NULL);
159 if (ldb == NULL) {
160 return LDB_ERR_OPERATIONS_ERROR;
163 options = ldb_cmdline_process(ldb, argc, argv, usage);
165 if (options->argc == 0) {
166 ret = process_file(ldb, stdin, &count);
167 } else {
168 for (i=0;i<options->argc;i++) {
169 const char *fname = options->argv[i];
170 FILE *f;
171 f = fopen(fname, "r");
172 if (!f) {
173 perror(fname);
174 return LDB_ERR_OPERATIONS_ERROR;
176 ret = process_file(ldb, f, &count);
177 fclose(f);
181 talloc_free(mem_ctx);
183 if (ret) {
184 printf("Modify failed after processing %u records\n", count);
185 } else {
186 printf("Modified %u records successfully\n", count);
189 return ret;