305e298e00631bc988e25e48ebb434a5a1975645
[kamenim/samba.git] / source4 / rpc_server / drsuapi / drsutil.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    useful utilities for the DRS server
5
6    Copyright (C) Andrew Tridgell 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 "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "libcli/security/dom_sid.h"
26 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
27
28 /*
29   format a drsuapi_DsReplicaObjectIdentifier naming context as a string
30  */
31 char *drs_ObjectIdentifier_to_string(TALLOC_CTX *mem_ctx,
32                                      struct drsuapi_DsReplicaObjectIdentifier *nc)
33 {
34         char *guid, *sid, *ret;
35         guid = GUID_string(mem_ctx, &nc->guid);
36         sid  = dom_sid_string(mem_ctx, &nc->sid);
37         ret = talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s",
38                               guid, sid, nc->dn);
39         talloc_free(guid);
40         talloc_free(sid);
41         return ret;
42 }
43
44 int drsuapi_search_with_extended_dn(struct ldb_context *ldb,
45                                 TALLOC_CTX *mem_ctx,
46                                 struct ldb_result **_res,
47                                 struct ldb_dn *basedn,
48                                 enum ldb_scope scope,
49                                 const char * const *attrs,
50                                 const char *format, ...)
51 {
52         va_list ap;
53         int ret;
54         struct ldb_request *req;
55         char *filter;
56         TALLOC_CTX *tmp_ctx;
57         struct ldb_result *res;
58
59         tmp_ctx = talloc_new(mem_ctx);
60
61         res = talloc_zero(tmp_ctx, struct ldb_result);
62         if (!res) {
63                 return LDB_ERR_OPERATIONS_ERROR;
64         }
65
66         va_start(ap, format);
67         filter = talloc_vasprintf(tmp_ctx, format, ap);
68         va_end(ap);
69
70         if (filter == NULL) {
71                 talloc_free(tmp_ctx);
72                 return LDB_ERR_OPERATIONS_ERROR;
73         }
74
75         ret = ldb_build_search_req(&req, ldb, tmp_ctx,
76                                    basedn,
77                                    scope,
78                                    filter,
79                                    attrs,
80                                    NULL,
81                                    res,
82                                    ldb_search_default_callback,
83                                    NULL);
84         if (ret != LDB_SUCCESS) {
85                 talloc_free(tmp_ctx);
86                 return ret;
87         }
88
89         ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, NULL);
90         if (ret != LDB_SUCCESS) {
91                 return ret;
92         }
93
94         ret = ldb_request(ldb, req);
95         if (ret == LDB_SUCCESS) {
96                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
97         }
98
99         talloc_free(req);
100         *_res = res;
101         return ret;
102 }
103