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
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/>.
29 * Description: utility to add records - modelled on ldapadd
31 * Author: Andrew Tridgell
36 #include "tools/cmdline.h"
38 #include "include/ldb_private.h"
40 static struct ldb_cmdline
*options
;
42 static void usage(struct ldb_context
*ldb
)
44 printf("Usage: ldbadd <options> <ldif...>\n");
45 printf("Adds records to a ldb, reading ldif the specified list of files\n\n");
46 ldb_cmdline_help(ldb
, "ldbadd", stdout
);
47 exit(LDB_ERR_OPERATIONS_ERROR
);
52 add records from an opened file
54 static int process_file(struct ldb_context
*ldb
, FILE *f
, unsigned int *count
)
56 struct ldb_ldif
*ldif
;
57 int fun_ret
= LDB_SUCCESS
, ret
;
58 struct ldb_control
**req_ctrls
= ldb_parse_control_strings(ldb
, ldb
, (const char **)options
->controls
);
59 struct ldif_read_file_state state
= {
63 if (options
->controls
!= NULL
&& req_ctrls
== NULL
) {
64 printf("parsing controls failed: %s\n", ldb_errstring(ldb
));
65 return LDB_ERR_OPERATIONS_ERROR
;
68 fun_ret
= ldb_transaction_start(ldb
);
69 if (fun_ret
!= LDB_SUCCESS
) {
70 fprintf(stderr
, "ERR: (%s) on transaction start\n",
75 while ((ldif
= ldb_ldif_read_file_state(ldb
, &state
))) {
76 if (ldif
->changetype
!= LDB_CHANGETYPE_ADD
&&
77 ldif
->changetype
!= LDB_CHANGETYPE_NONE
) {
78 fprintf(stderr
, "Only CHANGETYPE_ADD records allowed\n");
82 ret
= ldb_msg_normalize(ldb
, ldif
, ldif
->msg
, &ldif
->msg
);
83 if (ret
!= LDB_SUCCESS
) {
85 "ERR: Message canonicalize failed - %s\n",
88 ldb_ldif_read_free(ldb
, ldif
);
92 ret
= ldb_add_ctrl(ldb
, ldif
->msg
,req_ctrls
);
93 if (ret
!= LDB_SUCCESS
) {
94 fprintf(stderr
, "ERR: %s : \"%s\" on DN %s at block before line %llu\n",
95 ldb_strerror(ret
), ldb_errstring(ldb
),
96 ldb_dn_get_linearized(ldif
->msg
->dn
),
97 (unsigned long long)state
.line_no
);
101 if (options
->verbose
) {
102 printf("Added %s\n", ldb_dn_get_linearized(ldif
->msg
->dn
));
105 ldb_ldif_read_free(ldb
, ldif
);
111 if (fun_ret
== LDB_SUCCESS
&& !feof(f
)) {
112 fprintf(stderr
, "Failed to parse ldif\n");
113 fun_ret
= LDB_ERR_OPERATIONS_ERROR
;
116 if (fun_ret
== LDB_SUCCESS
) {
117 fun_ret
= ldb_transaction_commit(ldb
);
118 if (fun_ret
!= LDB_SUCCESS
) {
119 fprintf(stderr
, "ERR: (%s) on transaction commit\n",
123 ldb_transaction_cancel(ldb
);
131 int main(int argc
, const char **argv
)
133 struct ldb_context
*ldb
;
134 unsigned int i
, count
= 0;
135 int ret
= LDB_SUCCESS
;
136 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
138 ldb
= ldb_init(mem_ctx
, NULL
);
140 return LDB_ERR_OPERATIONS_ERROR
;
143 options
= ldb_cmdline_process(ldb
, argc
, argv
, usage
);
145 ret
= ldb_transaction_start(ldb
);
146 if (ret
!= LDB_SUCCESS
) {
147 printf("Failed to start transaction: %s\n", ldb_errstring(ldb
));
151 if (options
->argc
== 0) {
152 ret
= process_file(ldb
, stdin
, &count
);
154 for (i
=0;i
<options
->argc
;i
++) {
155 const char *fname
= options
->argv
[i
];
157 f
= fopen(fname
, "r");
160 return LDB_ERR_OPERATIONS_ERROR
;
162 ret
= process_file(ldb
, f
, &count
);
168 ret
= ldb_transaction_commit(ldb
);
169 if (ret
!= LDB_SUCCESS
) {
170 printf("Failed to commit transaction: %s\n", ldb_errstring(ldb
));
174 ldb_transaction_cancel(ldb
);
177 talloc_free(mem_ctx
);
180 printf("Add failed after processing %u records\n", count
);
182 printf("Added %u records successfully\n", count
);