summaryrefslogtreecommitdiff
path: root/libcli/ldap/ldap_ndr.c
blob: 9cc502eb36b8cd1a110223d8522ac8a571c379e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* 
   Unix SMB/CIFS mplementation.

   wrap/unwrap NDR encoded elements for ldap calls
   
   Copyright (C) Andrew Tridgell  2005
    
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   
*/

#include "includes.h"
#include <ldb.h>
#include "librpc/gen_ndr/ndr_security.h"
#include "librpc/gen_ndr/ndr_misc.h"
#include "libcli/ldap/ldap_ndr.h"

/*
  encode a NDR uint32 as a ldap filter element
*/
char *ldap_encode_ndr_uint32(TALLOC_CTX *mem_ctx, uint32_t value)
{
	uint8_t buf[4];
	struct ldb_val val;
	SIVAL(buf, 0, value);
	val.data = buf;
	val.length = 4;
	return ldb_binary_encode(mem_ctx, val);
}

/*
  encode a NDR dom_sid as a ldap filter element
*/
char *ldap_encode_ndr_dom_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
{
	DATA_BLOB blob;
	enum ndr_err_code ndr_err;
	char *ret;
	ndr_err = ndr_push_struct_blob(&blob, mem_ctx, sid,
				       (ndr_push_flags_fn_t)ndr_push_dom_sid);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		return NULL;
	}
	ret = ldb_binary_encode(mem_ctx, blob);
	data_blob_free(&blob);
	return ret;
}


/*
  encode a NDR GUID as a ldap filter element
*/
char *ldap_encode_ndr_GUID(TALLOC_CTX *mem_ctx, const struct GUID *guid)
{
	DATA_BLOB blob;
	NTSTATUS status;
	char *ret;
	status = GUID_to_ndr_blob(guid, mem_ctx, &blob);
	if (!NT_STATUS_IS_OK(status)) {
		return NULL;
	}
	ret = ldb_binary_encode(mem_ctx, blob);
	data_blob_free(&blob);
	return ret;
}

/*
  decode a NDR GUID from a ldap filter element
*/
NTSTATUS ldap_decode_ndr_GUID(TALLOC_CTX *mem_ctx, struct ldb_val val, struct GUID *guid)
{
	DATA_BLOB blob;
	enum ndr_err_code ndr_err;

	blob.data = val.data;
	blob.length = val.length;
	ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, guid,
				       (ndr_pull_flags_fn_t)ndr_pull_GUID);
	talloc_free(val.data);
	if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
		return ndr_map_error2ntstatus(ndr_err);
	}
	return NT_STATUS_OK;
}