s4:drs split addentry and getncchanges into separate files
[ira/wip.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 "librpc/gen_ndr/ndr_drsuapi.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "rpc_server/common/common.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "param/param.h"
29 #include "librpc/gen_ndr/ndr_drsblobs.h"
30 #include "libcli/security/dom_sid.h"
31 #include "rpc_server/drsuapi/dcesrv_drsuapi.h"
32
33 /*
34   format a drsuapi_DsReplicaObjectIdentifier naming context as a string
35  */
36 char *drs_ObjectIdentifier_to_string(TALLOC_CTX *mem_ctx,
37                                      struct drsuapi_DsReplicaObjectIdentifier *nc)
38 {
39         char *guid, *sid, *ret;
40         guid = GUID_string(mem_ctx, &nc->guid);
41         sid  = dom_sid_string(mem_ctx, &nc->sid);
42         ret = talloc_asprintf(mem_ctx, "<GUID=%s>;<SID=%s>;%s",
43                               guid, sid, nc->dn);
44         talloc_free(guid);
45         talloc_free(sid);
46         return ret;
47 }
48
49 int drsuapi_search_with_extended_dn(struct ldb_context *ldb,
50                                 TALLOC_CTX *mem_ctx,
51                                 struct ldb_result **_res,
52                                 struct ldb_dn *basedn,
53                                 enum ldb_scope scope,
54                                 const char * const *attrs,
55                                 const char *format, ...)
56 {
57         va_list ap;
58         int ret;
59         struct ldb_request *req;
60         char *filter;
61         TALLOC_CTX *tmp_ctx;
62         struct ldb_result *res;
63
64         tmp_ctx = talloc_new(mem_ctx);
65
66         res = talloc_zero(tmp_ctx, struct ldb_result);
67         if (!res) {
68                 return LDB_ERR_OPERATIONS_ERROR;
69         }
70
71         va_start(ap, format);
72         filter = talloc_vasprintf(tmp_ctx, format, ap);
73         va_end(ap);
74
75         if (filter == NULL) {
76                 talloc_free(tmp_ctx);
77                 return LDB_ERR_OPERATIONS_ERROR;
78         }
79
80         ret = ldb_build_search_req(&req, ldb, tmp_ctx,
81                                    basedn,
82                                    scope,
83                                    filter,
84                                    attrs,
85                                    NULL,
86                                    res,
87                                    ldb_search_default_callback,
88                                    NULL);
89         if (ret != LDB_SUCCESS) {
90                 talloc_free(tmp_ctx);
91                 return ret;
92         }
93
94         ret = ldb_request_add_control(req, LDB_CONTROL_EXTENDED_DN_OID, true, NULL);
95         if (ret != LDB_SUCCESS) {
96                 return ret;
97         }
98
99         ret = ldb_request(ldb, req);
100         if (ret == LDB_SUCCESS) {
101                 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
102         }
103
104         talloc_free(req);
105         *_res = res;
106         return ret;
107 }
108