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