s4: Fix the build
[abartlet/samba.git/.git] / source4 / lib / ldb / tools / ldbutil.c
1 /*
2    ldb database library utility
3
4    Copyright (C) Matthieu Patou 2009
5
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
9
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.
14
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.
19
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/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Description: Common function used by ldb_add/ldb_modify/ldb_delete
28  *
29  *  Author: Matthieu Patou
30  */
31
32 #include "ldb.h"
33 #include "ldb_module.h"
34
35 /* autostarts a transacion if none active */
36 static int ldb_do_autotransaction(struct ldb_context *ldb,
37                                        struct ldb_request *req)
38 {
39         int ret;
40
41         ret = ldb_transaction_start(ldb);
42         if (ret != LDB_SUCCESS) {
43                 return ret;
44         }
45
46         ret = ldb_request(ldb, req);
47         if (ret == LDB_SUCCESS) {
48                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
49         }
50
51         if (ret == LDB_SUCCESS) {
52                 return ldb_transaction_commit(ldb);
53         }
54         ldb_transaction_cancel(ldb);
55
56         if (ldb_errstring(ldb) == NULL) {
57                 /* no error string was setup by the backend */
58                 ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
59         }
60
61         return ret;
62 }
63 /*
64   Same as ldb_add but accept control
65 */
66 int ldb_add_ctrl(struct ldb_context *ldb,
67                 const struct ldb_message *message,
68                 struct ldb_control **controls)
69 {
70         struct ldb_request *req;
71         int ret;
72
73         ret = ldb_msg_sanity_check(ldb, message);
74         if (ret != LDB_SUCCESS) {
75                 return ret;
76         }
77
78         ret = ldb_build_add_req(&req, ldb, ldb,
79                                         message,
80                                         controls,
81                                         NULL,
82                                         ldb_op_default_callback,
83                                         NULL);
84
85         if (ret != LDB_SUCCESS) return ret;
86
87         /* do request and autostart a transaction */
88         ret = ldb_do_autotransaction(ldb, req);
89
90         talloc_free(req);
91         return ret;
92 }
93
94 /*
95   same as ldb_delete but accept control
96 */
97 int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
98                 struct ldb_control **controls)
99 {
100         struct ldb_request *req;
101         int ret;
102
103         ret = ldb_build_del_req(&req, ldb, ldb,
104                                         dn,
105                                         controls,
106                                         NULL,
107                                         ldb_op_default_callback,
108                                         NULL);
109
110         if (ret != LDB_SUCCESS) return ret;
111
112         /* do request and autostart a transaction */
113         ret = ldb_do_autotransaction(ldb, req);
114
115         talloc_free(req);
116         return ret;
117 }
118
119
120 /*
121   same as ldb_modify, but accepts controls
122 */
123 int ldb_modify_ctrl(struct ldb_context *ldb,
124                     const struct ldb_message *message,
125                     struct ldb_control **controls)
126 {
127         struct ldb_request *req;
128         int ret;
129
130         ret = ldb_msg_sanity_check(ldb, message);
131         if (ret != LDB_SUCCESS) {
132                 return ret;
133         }
134
135         ret = ldb_build_mod_req(&req, ldb, ldb,
136                                         message,
137                                         controls,
138                                         NULL,
139                                         ldb_op_default_callback,
140                                         NULL);
141
142         if (ret != LDB_SUCCESS) return ret;
143
144         /* do request and autostart a transaction */
145         ret = ldb_do_autotransaction(ldb, req);
146
147         talloc_free(req);
148         return ret;
149 }