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