r5286: Some first steps in making the pidl code somewhat more generic for the
[samba.git] / source4 / librpc / ndr / ndr_sec.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    routines for marshalling/unmarshalling security descriptors
5    and related structures
6
7    Copyright (C) Andrew Tridgell 2003
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24
25 #include "includes.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27
28 /*
29   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
30 */
31 NTSTATUS ndr_pull_dom_sid2(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *sid)
32 {
33         uint32_t num_auths;
34         if (!(ndr_flags & NDR_SCALARS)) {
35                 return NT_STATUS_OK;
36         }
37         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &num_auths));
38         NDR_CHECK(ndr_pull_dom_sid(ndr, ndr_flags, sid));
39         if (sid->num_auths != num_auths) {
40                 return ndr_pull_error(ndr, NDR_ERR_CONFORMANT_SIZE, 
41                                       "Bad conformant size %u should be %u", 
42                                       num_auths, sid->num_auths);
43         }
44         return NT_STATUS_OK;
45 }
46
47 /*
48   parse a dom_sid2 - this is a dom_sid but with an extra copy of the num_auths field
49 */
50 NTSTATUS ndr_push_dom_sid2(struct ndr_push *ndr, int ndr_flags, struct dom_sid *sid)
51 {
52         if (!(ndr_flags & NDR_SCALARS)) {
53                 return NT_STATUS_OK;
54         }
55         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, sid->num_auths));
56         return ndr_push_dom_sid(ndr, ndr_flags, sid);
57 }
58
59
60 /*
61   print a dom_sid
62 */
63 void ndr_print_dom_sid(struct ndr_print *ndr, const char *name, struct dom_sid *sid)
64 {
65         ndr->print(ndr, "%-25s: %s", name, dom_sid_string(ndr, sid));
66 }
67
68 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, struct dom_sid2 *sid)
69 {
70         ndr_print_dom_sid(ndr, name, sid);
71 }
72
73 /*
74   return the wire size of a dom_sid
75 */
76 size_t ndr_size_dom_sid(struct dom_sid *sid)
77 {
78         if (!sid) return 0;
79         return 8 + 4*sid->num_auths;
80 }
81
82 /*
83   return the wire size of a security_ace
84 */
85 size_t ndr_size_security_ace(struct security_ace *ace)
86 {
87         if (!ace) return 0;
88         return 8 + ndr_size_dom_sid(&ace->trustee);
89 }
90
91
92 /*
93   return the wire size of a security_acl
94 */
95 size_t ndr_size_security_acl(struct security_acl *acl)
96 {
97         size_t ret;
98         int i;
99         if (!acl) return 0;
100         ret = 8;
101         for (i=0;i<acl->num_aces;i++) {
102                 ret += ndr_size_security_ace(&acl->aces[i]);
103         }
104         return ret;
105 }
106
107 /*
108   return the wire size of a security descriptor
109 */
110 size_t ndr_size_security_descriptor(struct security_descriptor *sd)
111 {
112         size_t ret;
113         if (!sd) return 0;
114         
115         ret = 20;
116         ret += ndr_size_dom_sid(sd->owner_sid);
117         ret += ndr_size_dom_sid(sd->group_sid);
118         ret += ndr_size_security_acl(sd->dacl);
119         ret += ndr_size_security_acl(sd->sacl);
120         return ret;
121 }