Merge branch 'master' of ssh://git.samba.org/data/git/samba
[ira/wip.git] / source4 / librpc / ndr / ndr_dom_sid.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    Copyright (C) Stefan Metzmacher 2006-2008
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 size_t ndr_size_dom_sid0(const struct dom_sid *sid, int flags)
53 {
54         return ndr_size_dom_sid28(sid, flags);
55 }
56
57 /*
58   print a dom_sid
59 */
60 void ndr_print_dom_sid(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
61 {
62         ndr->print(ndr, "%-25s: %s", name, dom_sid_string(ndr, sid));
63 }
64
65 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
66 {
67         ndr_print_dom_sid(ndr, name, sid);
68 }
69
70 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
71 {
72         ndr_print_dom_sid(ndr, name, sid);
73 }
74
75 void ndr_print_dom_sid0(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
76 {
77         ndr_print_dom_sid(ndr, name, sid);
78 }
79
80
81 /*
82   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
83 */
84 enum ndr_err_code ndr_pull_dom_sid2(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
85 {
86         uint32_t num_auths;
87         if (!(ndr_flags & NDR_SCALARS)) {
88                 return NDR_ERR_SUCCESS;
89         }
90         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &num_auths));
91         NDR_CHECK(ndr_pull_dom_sid(ndr, ndr_flags, sid));
92         if (sid->num_auths != num_auths) {
93                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
94                                       "Bad array size %u should exceed %u", 
95                                       num_auths, sid->num_auths);
96         }
97         return NDR_ERR_SUCCESS;
98 }
99
100 /*
101   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
102 */
103 enum ndr_err_code ndr_push_dom_sid2(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
104 {
105         if (!(ndr_flags & NDR_SCALARS)) {
106                 return NDR_ERR_SUCCESS;
107         }
108         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, sid->num_auths));
109         return ndr_push_dom_sid(ndr, ndr_flags, sid);
110 }
111
112 /*
113   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
114 */
115 enum ndr_err_code ndr_pull_dom_sid28(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
116 {
117         enum ndr_err_code status;
118         struct ndr_pull *subndr;
119
120         if (!(ndr_flags & NDR_SCALARS)) {
121                 return NDR_ERR_SUCCESS;
122         }
123
124         subndr = talloc_zero(ndr, struct ndr_pull);
125         NDR_ERR_HAVE_NO_MEMORY(subndr);
126         subndr->flags           = ndr->flags;
127         subndr->current_mem_ctx = ndr->current_mem_ctx;
128
129         subndr->data            = ndr->data + ndr->offset;
130         subndr->data_size       = 28;
131         subndr->offset          = 0;
132
133         NDR_CHECK(ndr_pull_advance(ndr, 28));
134
135         status = ndr_pull_dom_sid(subndr, ndr_flags, sid);
136         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
137                 /* handle a w2k bug which send random data in the buffer */
138                 ZERO_STRUCTP(sid);
139         } else if (sid->num_auths == 0 && sid->sub_auths) {
140                 talloc_free(sid->sub_auths);
141                 sid->sub_auths = NULL;
142         }
143
144         return NDR_ERR_SUCCESS;
145 }
146
147 /*
148   push a dom_sid28 - this is a dom_sid in a 28 byte fixed buffer
149 */
150 enum ndr_err_code ndr_push_dom_sid28(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
151 {
152         uint32_t old_offset;
153         uint32_t padding;
154
155         if (!(ndr_flags & NDR_SCALARS)) {
156                 return NDR_ERR_SUCCESS;
157         }
158
159         if (sid->num_auths > 5) {
160                 return ndr_push_error(ndr, NDR_ERR_RANGE, 
161                                       "dom_sid28 allows only upto 5 sub auth [%u]", 
162                                       sid->num_auths);
163         }
164
165         old_offset = ndr->offset;
166         NDR_CHECK(ndr_push_dom_sid(ndr, ndr_flags, sid));
167
168         padding = 28 - (ndr->offset - old_offset);
169
170         if (padding > 0) {
171                 NDR_CHECK(ndr_push_zero(ndr, padding));
172         }
173
174         return NDR_ERR_SUCCESS;
175 }
176
177 /*
178   parse a dom_sid0 - this is a dom_sid in a variable byte buffer, which is maybe empty
179 */
180 enum ndr_err_code ndr_pull_dom_sid0(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
181 {
182         if (!(ndr_flags & NDR_SCALARS)) {
183                 return NDR_ERR_SUCCESS;
184         }
185
186         if (ndr->data_size == ndr->offset) {
187                 ZERO_STRUCTP(sid);
188                 return NDR_ERR_SUCCESS;
189         }
190
191         return ndr_pull_dom_sid(ndr, ndr_flags, sid);
192 }
193
194 /*
195   push a dom_sid0 - this is a dom_sid in a variable byte buffer, which is maybe empty
196 */
197 enum ndr_err_code ndr_push_dom_sid0(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *sid)
198 {
199         struct dom_sid zero_sid;
200
201         if (!(ndr_flags & NDR_SCALARS)) {
202                 return NDR_ERR_SUCCESS;
203         }
204
205         if (!sid) {
206                 return NDR_ERR_SUCCESS;
207         }
208
209         ZERO_STRUCT(zero_sid);
210
211         if (memcmp(&zero_sid, sid, sizeof(zero_sid)) == 0) {
212                 return NDR_ERR_SUCCESS;
213         }
214
215         return ndr_push_dom_sid(ndr, ndr_flags, sid);
216 }
217