Make sure prototypes are always included, make some functions static and
[tprouty/samba.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 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
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "libcli/ldap/ldap.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "librpc/gen_ndr/ndr_misc.h"
28 #include "libcli/ldap/ldap_ndr.h"
29
30 /*
31   encode a NDR uint32 as a ldap filter element
32 */
33 char *ldap_encode_ndr_uint32(TALLOC_CTX *mem_ctx, uint32_t value)
34 {
35         uint8_t buf[4];
36         struct ldb_val val;
37         SIVAL(buf, 0, value);
38         val.data = buf;
39         val.length = 4;
40         return ldb_binary_encode(mem_ctx, val);
41 }
42
43 /*
44   encode a NDR dom_sid as a ldap filter element
45 */
46 char *ldap_encode_ndr_dom_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
47 {
48         DATA_BLOB blob;
49         enum ndr_err_code ndr_err;
50         char *ret;
51         ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, sid,
52                                        (ndr_push_flags_fn_t)ndr_push_dom_sid);
53         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
54                 return NULL;
55         }
56         ret = ldb_binary_encode(mem_ctx, blob);
57         data_blob_free(&blob);
58         return ret;
59 }
60
61
62 /*
63   encode a NDR GUID as a ldap filter element
64 */
65 char *ldap_encode_ndr_GUID(TALLOC_CTX *mem_ctx, struct GUID *guid)
66 {
67         DATA_BLOB blob;
68         enum ndr_err_code ndr_err;
69         char *ret;
70         ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, guid,
71                                        (ndr_push_flags_fn_t)ndr_push_GUID);
72         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
73                 return NULL;
74         }
75         ret = ldb_binary_encode(mem_ctx, blob);
76         data_blob_free(&blob);
77         return ret;
78 }
79
80 /*
81   decode a NDR GUID from a ldap filter element
82 */
83 NTSTATUS ldap_decode_ndr_GUID(TALLOC_CTX *mem_ctx, struct ldb_val val, struct GUID *guid)
84 {
85         DATA_BLOB blob;
86         enum ndr_err_code ndr_err;
87
88         blob.data = val.data;
89         blob.length = val.length;
90         ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, NULL, guid,
91                                        (ndr_pull_flags_fn_t)ndr_pull_GUID);
92         talloc_free(val.data);
93         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
94                 return ndr_map_error2ntstatus(ndr_err);
95         }
96         return NT_STATUS_OK;
97 }