r15281: A few updates for consistency's sake
[Samba/aatanasov.git] / source4 / librpc / ndr / ndr_sec_helper.c
blob8325aef93d1a074a80e6fd081bf919edee58cd9c
1 /*
2 Unix SMB/CIFS implementation.
4 fast routines for getting the wire size of security objects
6 Copyright (C) Andrew Tridgell 2003
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.
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.
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.
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_security.h"
28 return the wire size of a dom_sid
30 size_t ndr_size_dom_sid(const struct dom_sid *sid)
32 if (!sid) return 0;
33 return 8 + 4*sid->num_auths;
37 return the wire size of a dom_sid
39 size_t ndr_length_dom_sid(const struct dom_sid *sid)
41 if (!sid) return 0;
42 if (sid->sid_rev_num == 0) return 0;
43 return 8 + 4*sid->num_auths;
47 return the wire size of a security_ace
49 size_t ndr_size_security_ace(const struct security_ace *ace)
51 if (!ace) return 0;
52 return 8 + ndr_size_dom_sid(&ace->trustee);
57 return the wire size of a security_acl
59 size_t ndr_size_security_acl(const struct security_acl *acl)
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]);
68 return ret;
72 return the wire size of a security descriptor
74 size_t ndr_size_security_descriptor(const struct security_descriptor *sd)
76 size_t ret;
77 if (!sd) return 0;
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;
88 print a dom_sid
90 void ndr_print_dom_sid(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
92 ndr->print(ndr, "%-25s: %s", name, dom_sid_string(ndr, sid));
95 void ndr_print_dom_sid2(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
97 ndr_print_dom_sid(ndr, name, sid);
100 void ndr_print_dom_sid28(struct ndr_print *ndr, const char *name, const struct dom_sid *sid)
102 ndr_print_dom_sid(ndr, name, sid);
106 convert a dom_sid to a string
108 char *dom_sid_string(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
110 int i, ofs, maxlen;
111 uint32_t ia;
112 char *ret;
114 if (!sid) {
115 return talloc_strdup(mem_ctx, "(NULL SID)");
118 maxlen = sid->num_auths * 11 + 25;
119 ret = talloc_size(mem_ctx, maxlen);
120 if (!ret) return talloc_strdup(mem_ctx, "(SID ERR)");
122 ia = (sid->id_auth[5]) +
123 (sid->id_auth[4] << 8 ) +
124 (sid->id_auth[3] << 16) +
125 (sid->id_auth[2] << 24);
127 ofs = snprintf(ret, maxlen, "S-%u-%lu",
128 (uint_t)sid->sid_rev_num, (unsigned long)ia);
130 for (i = 0; i < sid->num_auths; i++) {
131 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu", (unsigned long)sid->sub_auths[i]);
134 return ret;