summaryrefslogtreecommitdiff
path: root/source4/libcli/ndr/ndr_sec.c
blob: 9ac5240b0f1cf8d217dafce99df33f11ac533bb4 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* 
   Unix SMB/CIFS implementation.

   routines for marshalling/unmarshalling security descriptors
   and related structures

   Copyright (C) Andrew Tridgell 2003
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include "includes.h"

/*
  parse a security_ace
*/
NTSTATUS ndr_pull_security_ace(struct ndr_pull *ndr, struct security_ace *ace)
{
	uint16 size;
	struct ndr_pull_save save;

	ndr_pull_save(ndr, &save);

	NDR_CHECK(ndr_pull_u8(ndr, &ace->type));
	NDR_CHECK(ndr_pull_u8(ndr, &ace->flags));
	NDR_CHECK(ndr_pull_u16(ndr, &size));
	NDR_CHECK(ndr_pull_limit_size(ndr, size, 4));

	NDR_CHECK(ndr_pull_u32(ndr, &ace->access_mask));

	if (sec_ace_object(ace->type)) {
		NDR_ALLOC(ndr, ace->obj);
		NDR_CHECK(ndr_pull_u32(ndr, &ace->obj->flags));
		if (ace->obj->flags & SEC_ACE_OBJECT_PRESENT) {
			NDR_CHECK(ndr_pull_guid(ndr, &ace->obj->object_guid));
		}
		if (ace->obj->flags & SEC_ACE_OBJECT_INHERITED_PRESENT) {
			NDR_CHECK(ndr_pull_guid(ndr, &ace->obj->inherit_guid));
		}
	}


	NDR_CHECK(ndr_pull_dom_sid(ndr, &ace->trustee));

	ndr_pull_restore(ndr, &save);
	NDR_CHECK(ndr_pull_advance(ndr, size));

	return NT_STATUS_OK;	
}

/*
  parse a security_acl
*/
NTSTATUS ndr_pull_security_acl(struct ndr_pull *ndr, struct security_acl *acl)
{
	int i;
	uint16 size;
	struct ndr_pull_save save;

	ndr_pull_save(ndr, &save);

	NDR_CHECK(ndr_pull_u16(ndr, &acl->revision));
	NDR_CHECK(ndr_pull_u16(ndr, &size));
	NDR_CHECK(ndr_pull_limit_size(ndr, size, 4));
	NDR_CHECK(ndr_pull_u32(ndr, &acl->num_aces));

	NDR_ALLOC_N(ndr, acl->aces, acl->num_aces);

	for (i=0;i<acl->num_aces;i++) {
		NDR_CHECK(ndr_pull_security_ace(ndr, &acl->aces[i]));
	}

	ndr_pull_restore(ndr, &save);
	NDR_CHECK(ndr_pull_advance(ndr, size));

	return NT_STATUS_OK;
}	

/*
  parse a security_acl offset and structure
*/
NTSTATUS ndr_pull_security_acl_ofs(struct ndr_pull *ndr, struct security_acl **acl)
{
	uint32 ofs;
	struct ndr_pull_save save;

	NDR_CHECK(ndr_pull_u32(ndr, &ofs));
	if (ofs == 0) {
		/* it is valid for an acl ptr to be NULL */
		*acl = NULL;
		return NT_STATUS_OK;
	}

	ndr_pull_save(ndr, &save);
	NDR_CHECK(ndr_pull_set_offset(ndr, ofs));
	NDR_ALLOC(ndr, *acl);
	NDR_CHECK(ndr_pull_security_acl(ndr, *acl));
	ndr_pull_restore(ndr, &save);

	return NT_STATUS_OK;
}


/*
  parse a dom_sid
*/
NTSTATUS ndr_pull_dom_sid(struct ndr_pull *ndr, struct dom_sid *sid)
{
	int i;

	NDR_CHECK(ndr_pull_u8(ndr, &sid->sid_rev_num));
	NDR_CHECK(ndr_pull_u8(ndr, &sid->num_auths));
	for (i=0;i<6;i++) {
		NDR_CHECK(ndr_pull_u8(ndr, &sid->id_auth[i]));
	}

	NDR_ALLOC_N(ndr, sid->sub_auths, sid->num_auths);

	for (i=0;i<sid->num_auths;i++) {
		NDR_CHECK(ndr_pull_u32(ndr, &sid->sub_auths[i]));
	}

	return NT_STATUS_OK;
}

/*
  parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
*/
NTSTATUS ndr_pull_dom_sid2(struct ndr_pull *ndr, struct dom_sid *sid)
{
	uint32 num_auths;
	NDR_CHECK(ndr_pull_u32(ndr, &num_auths));
	return ndr_pull_dom_sid(ndr, sid);
}

/*
  parse a dom_sid offset and structure
*/
NTSTATUS ndr_pull_dom_sid_ofs(struct ndr_pull *ndr, struct dom_sid **sid)
{
	uint32 ofs;
	struct ndr_pull_save save;

	NDR_CHECK(ndr_pull_u32(ndr, &ofs));
	if (ofs == 0) {
		/* it is valid for a dom_sid ptr to be NULL */
		*sid = NULL;
		return NT_STATUS_OK;
	}

	ndr_pull_save(ndr, &save);
	NDR_CHECK(ndr_pull_set_offset(ndr, ofs));
	NDR_ALLOC(ndr, *sid);
	NDR_CHECK(ndr_pull_dom_sid(ndr, *sid));
	ndr_pull_restore(ndr, &save);

	return NT_STATUS_OK;
}

/*
  parse a security descriptor 
*/
NTSTATUS ndr_pull_security_descriptor(struct ndr_pull *ndr, 
				      struct security_descriptor **sd)
{
	NDR_ALLOC(ndr, *sd);

	NDR_CHECK(ndr_pull_u8(ndr, &(*sd)->revision));
	NDR_CHECK(ndr_pull_u16(ndr, &(*sd)->type));
	NDR_CHECK(ndr_pull_dom_sid_ofs(ndr, &(*sd)->owner_sid));
	NDR_CHECK(ndr_pull_dom_sid_ofs(ndr, &(*sd)->group_sid));
	NDR_CHECK(ndr_pull_security_acl_ofs(ndr, &(*sd)->sacl));
	NDR_CHECK(ndr_pull_security_acl_ofs(ndr, &(*sd)->dacl));

	return NT_STATUS_OK;
}


/*
  parse a security_ace
*/
NTSTATUS ndr_push_security_ace(struct ndr_push *ndr, struct security_ace *ace)
{
	struct ndr_push_save save1, save2;

	NDR_CHECK(ndr_push_u8(ndr, ace->type));
	NDR_CHECK(ndr_push_u8(ndr, ace->flags));
	ndr_push_save(ndr, &save1);
	NDR_CHECK(ndr_push_u16(ndr, 0));
	NDR_CHECK(ndr_push_u32(ndr, ace->access_mask));

	if (sec_ace_object(ace->type)) {
		NDR_CHECK(ndr_push_u32(ndr, ace->obj->flags));
		if (ace->obj->flags & SEC_ACE_OBJECT_PRESENT) {
			NDR_CHECK(ndr_push_guid(ndr, &ace->obj->object_guid));
		}
		if (ace->obj->flags & SEC_ACE_OBJECT_INHERITED_PRESENT) {
			NDR_CHECK(ndr_push_guid(ndr, &ace->obj->inherit_guid));
		}
	}

	NDR_CHECK(ndr_push_dom_sid(ndr, &ace->trustee));

	ndr_push_save(ndr, &save2);
	ndr_push_restore(ndr, &save1);
	NDR_CHECK(ndr_push_u16(ndr, 2 + save2.offset - save1.offset));
	ndr_push_restore(ndr, &save2);

	return NT_STATUS_OK;	
}


/*
  push a security_acl
*/
NTSTATUS ndr_push_security_acl(struct ndr_push *ndr, struct security_acl *acl)
{
	int i;
	struct ndr_push_save save1, save2;

	NDR_CHECK(ndr_push_u16(ndr, acl->revision));
	ndr_push_save(ndr, &save1);
	NDR_CHECK(ndr_push_u16(ndr, 0));
	NDR_CHECK(ndr_push_u32(ndr, acl->num_aces));
	for (i=0;i<acl->num_aces;i++) {
		NDR_CHECK(ndr_push_security_ace(ndr, &acl->aces[i]));
	}
	ndr_push_save(ndr, &save2);
	ndr_push_restore(ndr, &save1);
	NDR_CHECK(ndr_push_u16(ndr, 2 + save2.offset - save1.offset));
	ndr_push_restore(ndr, &save2);

	return NT_STATUS_OK;
}	

/*
  push a dom_sid
*/
NTSTATUS ndr_push_dom_sid(struct ndr_push *ndr, struct dom_sid *sid)
{
	int i;

	NDR_CHECK(ndr_push_u8(ndr, sid->sid_rev_num));
	NDR_CHECK(ndr_push_u8(ndr, sid->num_auths));
	for (i=0;i<6;i++) {
		NDR_CHECK(ndr_push_u8(ndr, sid->id_auth[i]));
	}
	for (i=0;i<sid->num_auths;i++) {
		NDR_CHECK(ndr_push_u32(ndr, sid->sub_auths[i]));
	}

	return NT_STATUS_OK;
}


/*
  generate a ndr security descriptor 
*/
NTSTATUS ndr_push_security_descriptor(struct ndr_push *ndr, 
				      struct security_descriptor *sd)
{
	struct ndr_push_save save;
	struct ndr_push_save ofs1, ofs2, ofs3, ofs4;

	ndr_push_save(ndr, &save);

	NDR_CHECK(ndr_push_u8(ndr, sd->revision));
	NDR_CHECK(ndr_push_u16(ndr, sd->type));

	NDR_CHECK(ndr_push_offset(ndr, &ofs1));
	NDR_CHECK(ndr_push_offset(ndr, &ofs2));
	NDR_CHECK(ndr_push_offset(ndr, &ofs3));
	NDR_CHECK(ndr_push_offset(ndr, &ofs4));

	if (sd->owner_sid) {
		NDR_CHECK(ndr_push_offset_ptr(ndr, &ofs1, &save));
		NDR_CHECK(ndr_push_dom_sid(ndr, sd->owner_sid));
	}

	if (sd->group_sid) {
		NDR_CHECK(ndr_push_offset_ptr(ndr, &ofs2, &save));
		NDR_CHECK(ndr_push_dom_sid(ndr, sd->group_sid));
	}

	if (sd->sacl) {
		NDR_CHECK(ndr_push_offset_ptr(ndr, &ofs3, &save));
		NDR_CHECK(ndr_push_security_acl(ndr, sd->sacl));
	}

	if (sd->dacl) {
		NDR_CHECK(ndr_push_offset_ptr(ndr, &ofs4, &save));
		NDR_CHECK(ndr_push_security_acl(ndr, sd->dacl));
	}

	return NT_STATUS_OK;
}