a8272b2b6002cb77c5d186d8dad3d6bf83abf12f
[samba.git] / source4 / librpc / ndr / ndr_sec_helper.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    fast routines for getting the wire size of security objects
5
6    Copyright (C) Andrew Tridgell 2003
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 "librpc/gen_ndr/ndr_security.h"
26 #include "libcli/security/security.h"
27
28 /*
29   return the wire size of a dom_sid
30 */
31 size_t ndr_size_dom_sid(const struct dom_sid *sid, int flags)
32 {
33         if (!sid) return 0;
34         return 8 + 4*sid->num_auths;
35 }
36
37 size_t ndr_size_dom_sid28(const struct dom_sid *sid, int flags)
38 {
39         struct dom_sid zero_sid;
40
41         if (!sid) return 0;
42
43         ZERO_STRUCT(zero_sid);
44
45         if (memcmp(&zero_sid, sid, sizeof(zero_sid)) == 0) {
46                 return 0;
47         }
48
49         return 8 + 4*sid->num_auths;
50 }
51
52 /*
53   return the wire size of a security_ace
54 */
55 size_t ndr_size_security_ace(const struct security_ace *ace, int flags)
56 {
57         size_t ret;
58
59         if (!ace) return 0;
60
61         ret = 8 + ndr_size_dom_sid(&ace->trustee, flags);
62
63         switch (ace->type) {
64         case SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT:
65         case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
66         case SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT:
67         case SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT:
68                 ret += 4; /* uint32 bitmap ace->object.object.flags */
69                 if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
70                         ret += 16; /* GUID ace->object.object.type.type */
71                 }
72                 if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
73                         ret += 16; /* GUID ace->object.object.inherited_typeinherited_type */
74                 }
75                 break;
76         default:
77                 break;
78         }
79
80         return ret;
81 }
82
83 /*
84   return the wire size of a security_acl
85 */
86 size_t ndr_size_security_acl(const struct security_acl *acl, int flags)
87 {
88         size_t ret;
89         int i;
90         if (!acl) return 0;
91         ret = 8;
92         for (i=0;i<acl->num_aces;i++) {
93                 ret += ndr_size_security_ace(&acl->aces[i], flags);
94         }
95         return ret;
96 }
97
98 /*
99   return the wire size of a security descriptor
100 */
101 size_t ndr_size_security_descriptor(const struct security_descriptor *sd, int flags)
102 {
103         size_t ret;
104         if (!sd) return 0;
105         
106         ret = 20;
107         ret += ndr_size_dom_sid(sd->owner_sid, flags);
108         ret += ndr_size_dom_sid(sd->group_sid, flags);
109         ret += ndr_size_security_acl(sd->dacl, flags);
110         ret += ndr_size_security_acl(sd->sacl, flags);
111         return ret;
112 }
113
114 /*
115   print a dom_sid
116 */
117 void ndr_print_dom_sid(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
118 {
119         ndr->print(ndr, "%-25s: %s", name, dom_sid_string(ndr, sid));
120 }
121
122 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
123 {
124         ndr_print_dom_sid(ndr, name, sid);
125 }
126
127 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
128 {
129         ndr_print_dom_sid(ndr, name, sid);
130 }
131
132
133 /*
134   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
135 */
136 NTSTATUS ndr_pull_dom_sid2(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
137 {
138         uint32_t num_auths;
139         if (!(ndr_flags & NDR_SCALARS)) {
140                 return NT_STATUS_OK;
141         }
142         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &num_auths));
143         NDR_CHECK(ndr_pull_dom_sid(ndr, ndr_flags, sid));
144         if (sid->num_auths != num_auths) {
145                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
146                                       "Bad array size %u should exceed %u", 
147                                       num_auths, sid->num_auths);
148         }
149         return NT_STATUS_OK;
150 }
151
152 /*
153   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
154 */
155 NTSTATUS ndr_push_dom_sid2(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
156 {
157         if (!(ndr_flags & NDR_SCALARS)) {
158                 return NT_STATUS_OK;
159         }
160         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, sid->num_auths));
161         return ndr_push_dom_sid(ndr, ndr_flags, sid);
162 }
163
164 /*
165   parse a dom_sid28 - this is a dom_sid in a fixed 28 byte buffer, so we need to ensure there are only upto 5 sub_auth
166 */
167 NTSTATUS ndr_pull_dom_sid28(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
168 {
169         NTSTATUS status;
170         struct ndr_pull *subndr;
171
172         if (!(ndr_flags & NDR_SCALARS)) {
173                 return NT_STATUS_OK;
174         }
175
176         subndr = talloc_zero(ndr, struct ndr_pull);
177         NT_STATUS_HAVE_NO_MEMORY(subndr);
178         subndr->flags           = ndr->flags;
179         subndr->current_mem_ctx = ndr->current_mem_ctx;
180
181         subndr->data            = ndr->data + ndr->offset;
182         subndr->data_size       = 28;
183         subndr->offset          = 0;
184
185         NDR_CHECK(ndr_pull_advance(ndr, 28));
186
187         status = ndr_pull_dom_sid(subndr, ndr_flags, sid);
188         if (!NT_STATUS_IS_OK(status)) {
189                 /* handle a w2k bug which send random data in the buffer */
190                 ZERO_STRUCTP(sid);
191         }
192
193         return NT_STATUS_OK;
194 }
195
196 /*
197   push a dom_sid28 - this is a dom_sid in a 28 byte fixed buffer
198 */
199 NTSTATUS ndr_push_dom_sid28(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
200 {
201         uint32_t old_offset;
202         uint32_t padding;
203
204         if (!(ndr_flags & NDR_SCALARS)) {
205                 return NT_STATUS_OK;
206         }
207
208         if (sid->num_auths > 5) {
209                 return ndr_push_error(ndr, NDR_ERR_RANGE, 
210                                       "dom_sid28 allows only upto 5 sub auth [%u]", 
211                                       sid->num_auths);
212         }
213
214         old_offset = ndr->offset;
215         NDR_CHECK(ndr_push_dom_sid(ndr, ndr_flags, sid));
216
217         padding = 28 - (ndr->offset - old_offset);
218
219         if (padding > 0) {
220                 NDR_CHECK(ndr_push_zero(ndr, padding));
221         }
222
223         return NT_STATUS_OK;
224 }
225