jean-f. sent me some S-1-3-0,1,2,3 SIDs and names. S-1-3 doesn't exist.
[samba.git] / source3 / rpc_parse / parse_sec.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
8  *  Copyright (C) Paul Ashton                       1997.
9  *  
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #include "includes.h"
27
28 extern int DEBUGLEVEL;
29
30
31 /*******************************************************************
32 reads or writes a structure.
33 ********************************************************************/
34 void sec_io_info(char *desc, SEC_INFO *t, prs_struct *ps, int depth)
35 {
36         if (t == NULL) return;
37
38         prs_debug(ps, depth, desc, "sec_io_info");
39         depth++;
40
41         prs_align(ps);
42         
43         prs_uint32("perms", ps, depth, &(t->perms));
44 }
45
46 /*******************************************************************
47 reads or writes a structure.
48 ********************************************************************/
49 void sec_io_ace(char *desc, SEC_ACE *t, prs_struct *ps, int depth)
50 {
51         uint32 old_offset;
52         uint32 offset_ace_size;
53         if (t == NULL) return;
54
55         prs_debug(ps, depth, desc, "sec_io_ace");
56         depth++;
57
58         prs_align(ps);
59         
60         old_offset = ps->offset;
61
62         prs_uint8     ("type ", ps, depth, &(t->type));
63         prs_uint8     ("flags", ps, depth, &(t->flags));
64         prs_uint16_pre("size ", ps, depth, &(t->size ), &offset_ace_size);
65
66         sec_io_info   ("info ", &t->info, ps, depth);
67         prs_align(ps);
68         smb_io_dom_sid("sid  ", &t->sid , ps, depth);
69
70         prs_uint16_post("size ", ps, depth, &t->size, offset_ace_size, old_offset);
71 }
72
73 /*******************************************************************
74 reads or writes a structure.  this is one of those retrospective jobs,
75 which i hate.  why do we have to do this?  what's it all about?
76 ********************************************************************/
77 void sec_io_acl(char *desc, SEC_ACL *t, prs_struct *ps, int depth)
78 {
79         int i;
80         uint32 old_offset;
81         uint32 offset_acl_size;
82
83         if (t == NULL) return;
84
85         prs_debug(ps, depth, desc, "sec_io_acl");
86         depth++;
87
88         prs_align(ps);
89         
90         old_offset = ps->offset;
91
92         prs_uint16("revision", ps, depth, &(t->revision));
93         prs_uint16_pre("size     ", ps, depth, &(t->size     ), &offset_acl_size);
94         prs_uint32("num_aces ", ps, depth, &(t->num_aces ));
95
96         for (i = 0; i < MIN(t->num_aces, MAX_SEC_ACES); i++)
97         {
98                 fstring tmp;
99                 snprintf(tmp, sizeof(tmp), "ace[%02d]: ", i);
100                 sec_io_ace(tmp, &t->ace[i], ps, depth);
101         }
102
103         prs_align(ps);
104
105         prs_uint16_post("size     ", ps, depth, &t->size    , offset_acl_size, old_offset);
106 }
107
108
109 /*******************************************************************
110 reads or writes a structure.
111 ********************************************************************/
112 static void sec_io_desc(char *desc, SEC_DESC *t, prs_struct *ps, int depth)
113 {
114         uint32 off_owner_sid;
115         uint32 off_grp_sid  ;
116         uint32 off_sacl     ;
117         uint32 off_dacl      ;
118         uint32 old_offset;
119
120         if (t == NULL) return;
121
122         prs_debug(ps, depth, desc, "sec_io_desc");
123         depth++;
124
125         prs_align(ps);
126         
127         /* start of security descriptor stored for back-calc offset purposes */
128         old_offset = ps->offset;
129
130         prs_uint16("revision ", ps, depth, &(t->revision ));
131         prs_uint16("type     ", ps, depth, &(t->type     ));
132
133         prs_uint32_pre("off_owner_sid", ps, depth, &(t->off_owner_sid), &off_owner_sid);
134         prs_uint32_pre("off_grp_sid  ", ps, depth, &(t->off_grp_sid  ), &off_grp_sid  );
135         prs_uint32_pre("off_sacl     ", ps, depth, &(t->off_sacl     ), &off_sacl     );
136         prs_uint32_pre("off_dacl     ", ps, depth, &(t->off_dacl     ), &off_dacl     );
137
138         if (IS_BITS_SET_ALL(t->type, SEC_DESC_DACL_PRESENT))
139         {
140                 prs_uint32_post("off_dacl    ", ps, depth, &(t->off_dacl     ), off_dacl     , ps->offset - old_offset);
141                 ps->offset = old_offset + t->off_dacl;
142                 sec_io_acl     ("dacl"        , &t->dacl       , ps, depth);
143                 prs_align(ps);
144         }
145         else
146         {
147                 prs_uint32_post("off_dacl    ", ps, depth, &(t->off_dacl     ), off_dacl     , 0);
148         }
149
150         if (IS_BITS_SET_ALL(t->type, SEC_DESC_SACL_PRESENT))
151         {
152                 prs_uint32_post("off_sacl  ", ps, depth, &(t->off_sacl  ), off_sacl  , ps->offset - old_offset);
153                 ps->offset = old_offset + t->off_sacl;
154                 sec_io_acl     ("sacl"      , &t->sacl       , ps, depth);
155                 prs_align(ps);
156         }
157         else
158         {
159                 prs_uint32_post("off_sacl  ", ps, depth, &(t->off_sacl  ), off_sacl  , 0);
160         }
161
162         prs_uint32_post("off_owner_sid", ps, depth, &(t->off_owner_sid), off_owner_sid, ps->offset - old_offset);
163         if (t->off_owner_sid != 0)
164         {
165                 if (ps->io)
166                 {
167                         ps->offset = old_offset + t->off_owner_sid;
168                 }
169                 smb_io_dom_sid("owner_sid ", &t->owner_sid , ps, depth);
170                 prs_align(ps);
171         }
172
173         prs_uint32_post("off_grp_sid  ", ps, depth, &(t->off_grp_sid  ), off_grp_sid  , ps->offset - old_offset);
174         if (t->off_grp_sid != 0)
175         {
176                 if (ps->io)
177                 {
178                         ps->offset = old_offset + t->off_grp_sid;
179                 }
180                 smb_io_dom_sid("grp_sid", &t->grp_sid, ps, depth);
181                 prs_align(ps);
182         }
183 }
184
185 /*******************************************************************
186 creates a SEC_DESC_BUF structure.
187 ********************************************************************/
188 void make_sec_desc_buf(SEC_DESC_BUF *buf, int len, SEC_DESC *data)
189 {
190         ZERO_STRUCTP(buf);
191
192         /* max buffer size (allocated size) */
193         buf->max_len = len;
194         buf->undoc       = 0;
195         buf->len = data != NULL ? len : 0;
196         buf->sec = data;
197 }
198
199
200 /*******************************************************************
201 reads or writes a SEC_DESC_BUF structure.
202 ********************************************************************/
203 void sec_io_desc_buf(char *desc, SEC_DESC_BUF *sec, prs_struct *ps, int depth)
204 {
205         uint32 off_len;
206         uint32 off_max_len;
207         uint32 old_offset;
208         uint32 size;
209
210         if (sec == NULL) return;
211
212         prs_debug(ps, depth, desc, "sec_io_desc_buf");
213         depth++;
214
215         prs_align(ps);
216         
217         prs_uint32_pre("max_len", ps, depth, &(sec->max_len), &off_max_len);
218         prs_uint32    ("undoc  ", ps, depth, &(sec->undoc  ));
219         prs_uint32_pre("len    ", ps, depth, &(sec->len    ), &off_len);
220
221         old_offset = ps->offset;
222
223         /* reading, length is non-zero; writing, descriptor is non-NULL */
224         if ((sec->len != 0 || (!ps->io)) && sec->sec != NULL)
225         {
226                 sec_io_desc("sec   ", sec->sec, ps, depth);
227         }
228
229         size = ps->offset - old_offset;
230         prs_uint32_post("max_len", ps, depth, &(sec->max_len), off_max_len, size == 0 ? sec->max_len : size);
231         prs_uint32_post("len    ", ps, depth, &(sec->len    ), off_len    , size);
232 }
233