0cccdbe9719ae5185a5e54be2084b3acc1307ff2
[ira/wip.git] / source4 / libcli / ldap / ldap_ndr.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    wrap/unwrap NDR encoded elements for ldap calls
5    
6    Copyright (C) Andrew Tridgell  2005
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 */
23
24 #include "includes.h"
25 #include "libcli/ldap/ldap.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28
29 /*
30   encode a NDR uint32 as a ldap filter element
31 */
32 char *ldap_encode_ndr_uint32(TALLOC_CTX *mem_ctx, uint32_t value)
33 {
34         uint8_t buf[4];
35         struct ldb_val val;
36         SIVAL(buf, 0, value);
37         val.data = buf;
38         val.length = 4;
39         return ldb_binary_encode(mem_ctx, val);
40 }
41
42 /*
43   encode a NDR dom_sid as a ldap filter element
44 */
45 char *ldap_encode_ndr_dom_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
46 {
47         DATA_BLOB blob;
48         NTSTATUS status;
49         char *ret;
50         status = ndr_push_struct_blob(&blob, mem_ctx, sid, 
51                                       (ndr_push_flags_fn_t)ndr_push_dom_sid);
52         if (!NT_STATUS_IS_OK(status)) {
53                 return NULL;
54         }
55         ret = ldb_binary_encode(mem_ctx, blob);
56         data_blob_free(&blob);
57         return ret;
58 }
59
60
61 /*
62   encode a NDR GUID as a ldap filter element
63 */
64 char *ldap_encode_ndr_GUID(TALLOC_CTX *mem_ctx, struct GUID *guid)
65 {
66         DATA_BLOB blob;
67         NTSTATUS status;
68         char *ret;
69         status = ndr_push_struct_blob(&blob, mem_ctx, guid, 
70                                       (ndr_push_flags_fn_t)ndr_push_GUID);
71         if (!NT_STATUS_IS_OK(status)) {
72                 return NULL;
73         }
74         ret = ldb_binary_encode(mem_ctx, blob);
75         data_blob_free(&blob);
76         return ret;
77 }
78
79 /*
80   decode a NDR GUID from a ldap filter element
81 */
82 NTSTATUS ldap_decode_ndr_GUID(TALLOC_CTX *mem_ctx, struct ldb_val val, struct GUID *guid)
83 {
84         DATA_BLOB blob;
85         NTSTATUS status;
86
87         blob.data = val.data;
88         blob.length = val.length;
89         status = ndr_pull_struct_blob(&blob, mem_ctx, guid, 
90                                       (ndr_pull_flags_fn_t)ndr_pull_GUID);
91         talloc_free(val.data);
92         return status;
93 }