Merge branch 'master' of ssh://jra@git.samba.org/data/git/samba
[sfrench/samba-autobuild/.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         } else {
74                 *_res = talloc_steal(mem_ctx, res);
75         }
76         talloc_free(tmp_ctx);
77         return ret;
78 }
79
80 /*
81   search for attrs in the modules below
82  */
83 int dsdb_module_search(struct ldb_module *module,
84                        TALLOC_CTX *mem_ctx,
85                        struct ldb_result **_res,
86                        struct ldb_dn *basedn, enum ldb_scope scope, 
87                        const char * const *attrs,
88                        const char *expression)
89 {
90         int ret;
91         struct ldb_request *req;
92         TALLOC_CTX *tmp_ctx;
93         struct ldb_result *res;
94
95         tmp_ctx = talloc_new(mem_ctx);
96
97         res = talloc_zero(tmp_ctx, struct ldb_result);
98         if (!res) {
99                 return LDB_ERR_OPERATIONS_ERROR;
100         }
101
102         ret = ldb_build_search_req(&req, ldb_module_get_ctx(module), tmp_ctx,
103                                    basedn,
104                                    scope,
105                                    expression,
106                                    attrs,
107                                    NULL,
108                                    res,
109                                    ldb_search_default_callback,
110                                    NULL);
111         if (ret != LDB_SUCCESS) {
112                 talloc_free(tmp_ctx);
113                 return ret;
114         }
115
116         ret = ldb_next_request(module, req);
117         if (ret == LDB_SUCCESS) {
118                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
119         }
120
121         talloc_free(req);
122         if (ret == LDB_SUCCESS) {
123                 *_res = talloc_steal(mem_ctx, res);
124         }
125         talloc_free(tmp_ctx);
126         return ret;
127 }
128