r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[garming/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 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
27 /*
28   return the wire size of a dom_sid
29 */
30 size_t ndr_size_dom_sid(const struct dom_sid *sid)
31 {
32         if (!sid) return 0;
33         return 8 + 4*sid->num_auths;
34 }
35
36 /*
37   return the wire size of a dom_sid
38 */
39 size_t ndr_length_dom_sid(const struct dom_sid *sid)
40 {
41         if (!sid) return 0;
42         if (sid->sid_rev_num == 0) return 0;
43         return 8 + 4*sid->num_auths;
44 }
45
46 /*
47   return the wire size of a security_ace
48 */
49 size_t ndr_size_security_ace(const struct security_ace *ace)
50 {
51         if (!ace) return 0;
52         return 8 + ndr_size_dom_sid(&ace->trustee);
53 }
54
55
56 /*
57   return the wire size of a security_acl
58 */
59 size_t ndr_size_security_acl(const struct security_acl *acl)
60 {
61         size_t ret;
62         int i;
63         if (!acl) return 0;
64         ret = 8;
65         for (i=0;i<acl->num_aces;i++) {
66                 ret += ndr_size_security_ace(&acl->aces[i]);
67         }
68         return ret;
69 }
70
71 /*
72   return the wire size of a security descriptor
73 */
74 size_t ndr_size_security_descriptor(const struct security_descriptor *sd)
75 {
76         size_t ret;
77         if (!sd) return 0;
78         
79         ret = 20;
80         ret += ndr_size_dom_sid(sd->owner_sid);
81         ret += ndr_size_dom_sid(sd->group_sid);
82         ret += ndr_size_security_acl(sd->dacl);
83         ret += ndr_size_security_acl(sd->sacl);
84         return ret;
85 }
86
87 /*
88   print a dom_sid
89 */
90 void ndr_print_dom_sid(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
91 {
92         ndr->print(ndr, "%-25s: %s", name, dom_sid_string(ndr, sid));
93 }
94
95 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
96 {
97         ndr_print_dom_sid(ndr, name, sid);
98 }
99
100 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
101 {
102         ndr_print_dom_sid(ndr, name, sid);
103 }
104
105 /*
106   convert a dom_sid to a string
107 */
108 char *dom_sid_string(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
109 {
110         int i, ofs, maxlen;
111         uint32_t ia;
112         char *ret;
113         
114         if (!sid) {
115                 return talloc_strdup(mem_ctx, "(NULL SID)");
116         }
117
118         maxlen = sid->num_auths * 11 + 25;
119         ret = talloc_size(mem_ctx, maxlen);
120         if (!ret) return talloc_strdup(mem_ctx, "(SID ERR)");
121
122         ia = (sid->id_auth[5]) +
123                 (sid->id_auth[4] << 8 ) +
124                 (sid->id_auth[3] << 16) +
125                 (sid->id_auth[2] << 24);
126
127         ofs = snprintf(ret, maxlen, "S-%u-%lu", 
128                        (uint_t)sid->sid_rev_num, (unsigned long)ia);
129
130         for (i = 0; i < sid->num_auths; i++) {
131                 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu", (unsigned long)sid->sub_auths[i]);
132         }
133         
134         return ret;
135 }
136