s4-ldb: fixed request handling for schemaUpdateNow op
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4
5    Copyright (C) Andrew Tridgell 2009
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "ldb.h"
23 #include "ldb_module.h"
24
25 /*
26   search for attrs on one DN, in the modules below
27  */
28 int dsdb_module_search_dn(struct ldb_module *module,
29                           TALLOC_CTX *mem_ctx,
30                           struct ldb_result **_res,
31                           struct ldb_dn *basedn,
32                           const char * const *attrs)
33 {
34         int ret;
35         struct ldb_request *req;
36         TALLOC_CTX *tmp_ctx;
37         struct ldb_result *res;
38
39         tmp_ctx = talloc_new(mem_ctx);
40
41         res = talloc_zero(tmp_ctx, struct ldb_result);
42         if (!res) {
43                 return LDB_ERR_OPERATIONS_ERROR;
44         }
45
46         ret = ldb_build_search_req(&req, ldb_module_get_ctx(module), tmp_ctx,
47                                    basedn,
48                                    LDB_SCOPE_BASE,
49                                    NULL,
50                                    attrs,
51                                    NULL,
52                                    res,
53                                    ldb_search_default_callback,
54                                    NULL);
55         if (ret != LDB_SUCCESS) {
56                 talloc_free(tmp_ctx);
57                 return ret;
58         }
59
60         ret = ldb_next_request(module, req);
61         if (ret == LDB_SUCCESS) {
62                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
63         }
64
65         if (ret != LDB_SUCCESS) {
66                 talloc_free(tmp_ctx);
67                 return ret;
68         }
69
70         if (res->count != 1) {
71                 /* we may be reading a DB that does not have the 'check base on search' option... */
72                 ret = LDB_ERR_NO_SUCH_OBJECT;
73                 ldb_asprintf_errstring(ldb_module_get_ctx(module), 
74                                        "dsdb_module_search_dn: did not find base dn %s (%d results)", 
75                                        ldb_dn_get_linearized(basedn), res->count);
76         } else {
77                 *_res = talloc_steal(mem_ctx, res);
78         }
79         talloc_free(tmp_ctx);
80         return ret;
81 }
82
83 /*
84   search for attrs in the modules below
85  */
86 int dsdb_module_search(struct ldb_module *module,
87                        TALLOC_CTX *mem_ctx,
88                        struct ldb_result **_res,
89                        struct ldb_dn *basedn, enum ldb_scope scope, 
90                        const char * const *attrs,
91                        const char *expression)
92 {
93         int ret;
94         struct ldb_request *req;
95         TALLOC_CTX *tmp_ctx;
96         struct ldb_result *res;
97
98         tmp_ctx = talloc_new(mem_ctx);
99
100         res = talloc_zero(tmp_ctx, struct ldb_result);
101         if (!res) {
102                 return LDB_ERR_OPERATIONS_ERROR;
103         }
104
105         ret = ldb_build_search_req(&req, ldb_module_get_ctx(module), tmp_ctx,
106                                    basedn,
107                                    scope,
108                                    expression,
109                                    attrs,
110                                    NULL,
111                                    res,
112                                    ldb_search_default_callback,
113                                    NULL);
114         if (ret != LDB_SUCCESS) {
115                 talloc_free(tmp_ctx);
116                 return ret;
117         }
118
119         ret = ldb_next_request(module, req);
120         if (ret == LDB_SUCCESS) {
121                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
122         }
123
124         talloc_free(req);
125         if (ret == LDB_SUCCESS) {
126                 *_res = talloc_steal(mem_ctx, res);
127         }
128         talloc_free(tmp_ctx);
129         return ret;
130 }
131