libcli/auth Merge source4/libcli/security and util_sid.c into the common code
[ira/wip.git] / libcli / security / dom_sid.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4
5    Copyright (C) Stefan (metze) Metzmacher      2002-2004
6    Copyright (C) Andrew Tridgell                1992-2004
7    Copyright (C) Jeremy Allison                 1999
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "librpc/gen_ndr/security.h"
25 #include "dom_sid.h"
26
27 /*****************************************************************
28  Compare the auth portion of two sids.
29 *****************************************************************/
30
31 int dom_sid_compare_auth(const struct dom_sid *sid1,
32                          const struct dom_sid *sid2)
33 {
34         int i;
35
36         if (sid1 == sid2)
37                 return 0;
38         if (!sid1)
39                 return -1;
40         if (!sid2)
41                 return 1;
42
43         if (sid1->sid_rev_num != sid2->sid_rev_num)
44                 return sid1->sid_rev_num - sid2->sid_rev_num;
45
46         for (i = 0; i < 6; i++)
47                 if (sid1->id_auth[i] != sid2->id_auth[i])
48                         return sid1->id_auth[i] - sid2->id_auth[i];
49
50         return 0;
51 }
52
53 /*****************************************************************
54  Compare two sids.
55 *****************************************************************/
56
57 int dom_sid_compare(const struct dom_sid *sid1, const struct dom_sid *sid2)
58 {
59         int i;
60
61         if (sid1 == sid2)
62                 return 0;
63         if (!sid1)
64                 return -1;
65         if (!sid2)
66                 return 1;
67
68         /* Compare most likely different rids, first: i.e start at end */
69         if (sid1->num_auths != sid2->num_auths)
70                 return sid1->num_auths - sid2->num_auths;
71
72         for (i = sid1->num_auths-1; i >= 0; --i)
73                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
74                         return sid1->sub_auths[i] - sid2->sub_auths[i];
75
76         return dom_sid_compare_auth(sid1, sid2);
77 }
78
79 /*****************************************************************
80  Compare two sids.
81 *****************************************************************/
82
83 bool dom_sid_equal(const struct dom_sid *sid1, const struct dom_sid *sid2)
84 {
85         return dom_sid_compare(sid1, sid2) == 0;
86 }
87
88 /*****************************************************************
89  Add a rid to the end of a sid
90 *****************************************************************/
91
92 bool sid_append_rid(struct dom_sid *sid, uint32_t rid)
93 {
94         if (sid->num_auths < ARRAY_SIZE(sid->sub_auths)) {
95                 sid->sub_auths[sid->num_auths++] = rid;
96                 return true;
97         }
98         return false;
99 }
100
101 /*
102   See if 2 SIDs are in the same domain
103   this just compares the leading sub-auths
104 */
105 int dom_sid_compare_domain(const struct dom_sid *sid1,
106                            const struct dom_sid *sid2)
107 {
108         int n, i;
109
110         n = MIN(sid1->num_auths, sid2->num_auths);
111
112         for (i = n-1; i >= 0; --i)
113                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
114                         return sid1->sub_auths[i] - sid2->sub_auths[i];
115
116         return dom_sid_compare_auth(sid1, sid2);
117 }
118
119 /*****************************************************************
120  Convert a string to a SID. Returns True on success, False on fail.
121 *****************************************************************/
122
123 bool string_to_sid(struct dom_sid *sidout, const char *sidstr)
124 {
125         const char *p;
126         char *q;
127         /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
128         uint32_t conv;
129
130         ZERO_STRUCTP(sidout);
131
132         if ((sidstr[0] != 'S' && sidstr[0] != 's') || sidstr[1] != '-') {
133                 goto format_error;
134         }
135
136         /* Get the revision number. */
137         p = sidstr + 2;
138
139         if (!isdigit(*p)) {
140                 goto format_error;
141         }
142
143         conv = (uint32_t) strtoul(p, &q, 10);
144         if (!q || (*q != '-')) {
145                 goto format_error;
146         }
147         sidout->sid_rev_num = (uint8_t) conv;
148         q++;
149
150         if (!isdigit(*q)) {
151                 goto format_error;
152         }
153
154         /* get identauth */
155         conv = (uint32_t) strtoul(q, &q, 10);
156         if (!q) {
157                 goto format_error;
158         }
159
160         /* identauth in decimal should be <  2^32 */
161         /* NOTE - the conv value is in big-endian format. */
162         sidout->id_auth[0] = 0;
163         sidout->id_auth[1] = 0;
164         sidout->id_auth[2] = (conv & 0xff000000) >> 24;
165         sidout->id_auth[3] = (conv & 0x00ff0000) >> 16;
166         sidout->id_auth[4] = (conv & 0x0000ff00) >> 8;
167         sidout->id_auth[5] = (conv & 0x000000ff);
168
169         sidout->num_auths = 0;
170         if (*q != '-') {
171                 /* Just id_auth, no subauths */
172                 return true;
173         }
174
175         q++;
176
177         while (true) {
178                 char *end;
179
180                 if (!isdigit(*q)) {
181                         goto format_error;
182                 }
183
184                 conv = strtoul(q, &end, 10);
185                 if (end == q) {
186                         goto format_error;
187                 }
188
189                 if (!sid_append_rid(sidout, conv)) {
190                         DEBUG(3, ("Too many sid auths in %s\n", sidstr));
191                         return false;
192                 }
193
194                 q = end;
195                 if (*q != '-') {
196                         break;
197                 }
198                 q += 1;
199         }
200         return true;
201
202 format_error:
203         DEBUG(3, ("string_to_sid: SID %s is not in a valid format\n", sidstr));
204         return false;
205 }
206
207 bool dom_sid_parse(const char *sidstr, struct dom_sid *ret)
208 {
209         return string_to_sid(ret, sidstr);
210 }
211
212 /*
213   convert a string to a dom_sid, returning a talloc'd dom_sid
214 */
215 struct dom_sid *dom_sid_parse_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
216 {
217         struct dom_sid *ret;
218         ret = talloc(mem_ctx, struct dom_sid);
219         if (!ret) {
220                 return NULL;
221         }
222         if (!dom_sid_parse(sidstr, ret)) {
223                 talloc_free(ret);
224                 return NULL;
225         }
226
227         return ret;
228 }
229
230 /*
231   convert a string to a dom_sid, returning a talloc'd dom_sid
232 */
233 struct dom_sid *dom_sid_parse_length(TALLOC_CTX *mem_ctx, const DATA_BLOB *sid)
234 {
235         struct dom_sid *ret;
236         char *p = talloc_strndup(mem_ctx, (char *)sid->data, sid->length);
237         if (!p) {
238                 return NULL;
239         }
240         ret = dom_sid_parse_talloc(mem_ctx, p);
241         talloc_free(p);
242         return ret;
243 }
244
245 /*
246   copy a dom_sid structure
247 */
248 struct dom_sid *dom_sid_dup(TALLOC_CTX *mem_ctx, const struct dom_sid *dom_sid)
249 {
250         struct dom_sid *ret;
251         int i;
252
253         if (!dom_sid) {
254                 return NULL;
255         }
256
257         ret = talloc(mem_ctx, struct dom_sid);
258         if (!ret) {
259                 return NULL;
260         }
261
262         ret->sid_rev_num = dom_sid->sid_rev_num;
263         ret->id_auth[0] = dom_sid->id_auth[0];
264         ret->id_auth[1] = dom_sid->id_auth[1];
265         ret->id_auth[2] = dom_sid->id_auth[2];
266         ret->id_auth[3] = dom_sid->id_auth[3];
267         ret->id_auth[4] = dom_sid->id_auth[4];
268         ret->id_auth[5] = dom_sid->id_auth[5];
269         ret->num_auths = dom_sid->num_auths;
270
271         for (i=0;i<dom_sid->num_auths;i++) {
272                 ret->sub_auths[i] = dom_sid->sub_auths[i];
273         }
274
275         return ret;
276 }
277
278 /*
279   add a rid to a domain dom_sid to make a full dom_sid. This function
280   returns a new sid in the supplied memory context
281 */
282 struct dom_sid *dom_sid_add_rid(TALLOC_CTX *mem_ctx,
283                                 const struct dom_sid *domain_sid,
284                                 uint32_t rid)
285 {
286         struct dom_sid *sid;
287
288         sid = dom_sid_dup(mem_ctx, domain_sid);
289         if (!sid) return NULL;
290
291         if (!sid_append_rid(sid, rid)) {
292                 talloc_free(sid);
293                 return NULL;
294         }
295
296         return sid;
297 }
298
299 /*
300   Split up a SID into its domain and RID part
301 */
302 NTSTATUS dom_sid_split_rid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
303                            struct dom_sid **domain, uint32_t *rid)
304 {
305         if (sid->num_auths == 0) {
306                 return NT_STATUS_INVALID_PARAMETER;
307         }
308
309         if (domain) {
310                 if (!(*domain = dom_sid_dup(mem_ctx, sid))) {
311                         return NT_STATUS_NO_MEMORY;
312                 }
313
314                 (*domain)->num_auths -= 1;
315         }
316
317         if (rid) {
318                 *rid = sid->sub_auths[sid->num_auths - 1];
319         }
320
321         return NT_STATUS_OK;
322 }
323
324 /*
325   return true if the 2nd sid is in the domain given by the first sid
326 */
327 bool dom_sid_in_domain(const struct dom_sid *domain_sid,
328                        const struct dom_sid *sid)
329 {
330         int i;
331
332         if (!domain_sid || !sid) {
333                 return false;
334         }
335
336         if (domain_sid->num_auths > sid->num_auths) {
337                 return false;
338         }
339
340         for (i = domain_sid->num_auths-1; i >= 0; --i) {
341                 if (domain_sid->sub_auths[i] != sid->sub_auths[i]) {
342                         return false;
343                 }
344         }
345
346         return dom_sid_compare_auth(domain_sid, sid) == 0;
347 }
348
349 /*
350   convert a dom_sid to a string
351 */
352 char *dom_sid_string(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
353 {
354         int i, ofs, maxlen;
355         uint32_t ia;
356         char *ret;
357
358         if (!sid) {
359                 return talloc_strdup(mem_ctx, "(NULL SID)");
360         }
361
362         maxlen = sid->num_auths * 11 + 25;
363         ret = talloc_array(mem_ctx, char, maxlen);
364         if (!ret) return talloc_strdup(mem_ctx, "(SID ERR)");
365
366         ia = (sid->id_auth[5]) +
367                 (sid->id_auth[4] << 8 ) +
368                 (sid->id_auth[3] << 16) +
369                 (sid->id_auth[2] << 24);
370
371         ofs = snprintf(ret, maxlen, "S-%u-%lu",
372                        (unsigned int)sid->sid_rev_num, (unsigned long)ia);
373
374         for (i = 0; i < sid->num_auths; i++) {
375                 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu",
376                                 (unsigned long)sid->sub_auths[i]);
377         }
378
379         return ret;
380 }