r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[vlendec/samba-autobuild/.git] / source3 / libndr / sid.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libndr interface
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 #include "includes.h"
24
25 NTSTATUS ndr_push_dom_sid(struct ndr_push *ndr, int ndr_flags, const struct dom_sid *r)
26 {
27         uint32_t cntr_sub_auths_0;
28         if (ndr_flags & NDR_SCALARS) {
29                 NDR_CHECK(ndr_push_align(ndr, 4));
30                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, r->sid_rev_num));
31                 NDR_CHECK(ndr_push_int8(ndr, NDR_SCALARS, r->num_auths));
32                 NDR_CHECK(ndr_push_array_uint8(ndr, NDR_SCALARS, r->id_auth, 6));
33                 for (cntr_sub_auths_0 = 0; cntr_sub_auths_0 < r->num_auths; cntr_sub_auths_0++) {
34                         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, r->sub_auths[cntr_sub_auths_0]));
35                 }
36         }
37         if (ndr_flags & NDR_BUFFERS) {
38         }
39         return NT_STATUS_OK;
40 }
41
42 NTSTATUS ndr_pull_dom_sid(struct ndr_pull *ndr, int ndr_flags, struct dom_sid *r)
43 {
44         uint32_t cntr_sub_auths_0;
45         TALLOC_CTX *_mem_save_sub_auths_0;
46         if (ndr_flags & NDR_SCALARS) {
47                 NDR_CHECK(ndr_pull_align(ndr, 4));
48                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &r->sid_rev_num));
49                 NDR_CHECK(ndr_pull_int8(ndr, NDR_SCALARS, &r->num_auths));
50                 if (r->num_auths < 0 || r->num_auths > 15) {
51                         return ndr_pull_error(ndr, NDR_ERR_RANGE, "value out of range");
52                 }
53                 NDR_CHECK(ndr_pull_array_uint8(ndr, NDR_SCALARS, r->id_auth, 6));
54                 NDR_PULL_ALLOC_N(ndr, r->sub_auths, r->num_auths);
55                 _mem_save_sub_auths_0 = NDR_PULL_GET_MEM_CTX(ndr);
56                 NDR_PULL_SET_MEM_CTX(ndr, r->sub_auths, 0);
57                 for (cntr_sub_auths_0 = 0; cntr_sub_auths_0 < r->num_auths; cntr_sub_auths_0++) {
58                         NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->sub_auths[cntr_sub_auths_0]));
59                 }
60                 NDR_PULL_SET_MEM_CTX(ndr, _mem_save_sub_auths_0, 0);
61         }
62         if (ndr_flags & NDR_BUFFERS) {
63         }
64         return NT_STATUS_OK;
65 }
66
67 /*
68   convert a dom_sid to a string
69 */
70 char *dom_sid_string(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
71 {
72         int i, ofs, maxlen;
73         uint32_t ia;
74         char *ret;
75         
76         if (!sid) {
77                 return talloc_strdup(mem_ctx, "(NULL SID)");
78         }
79
80         maxlen = sid->num_auths * 11 + 25;
81         ret = talloc_size(mem_ctx, maxlen);
82         if (!ret) return talloc_strdup(mem_ctx, "(SID ERR)");
83
84         ia = (sid->id_auth[5]) +
85                 (sid->id_auth[4] << 8 ) +
86                 (sid->id_auth[3] << 16) +
87                 (sid->id_auth[2] << 24);
88
89         ofs = snprintf(ret, maxlen, "S-%u-%lu", 
90                        (unsigned int)sid->sid_rev_num, (unsigned long)ia);
91
92         for (i = 0; i < sid->num_auths; i++) {
93                 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu", (unsigned long)sid->sub_auths[i]);
94         }
95         
96         return ret;
97 }