libcli/security Merge source3/ string_to_sid() to common code
[gd/samba-autobuild/.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 static 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  Convert a string to a SID. Returns True on success, False on fail.
103 *****************************************************************/
104
105 bool string_to_sid(struct dom_sid *sidout, const char *sidstr)
106 {
107         const char *p;
108         char *q;
109         /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
110         uint32_t conv;
111
112         if ((sidstr[0] != 'S' && sidstr[0] != 's') || sidstr[1] != '-') {
113                 goto format_error;
114         }
115
116         ZERO_STRUCTP(sidout);
117
118         /* Get the revision number. */
119         p = sidstr + 2;
120
121         if (!isdigit(*p)) {
122                 goto format_error;
123         }
124
125         conv = (uint32_t) strtoul(p, &q, 10);
126         if (!q || (*q != '-')) {
127                 goto format_error;
128         }
129         sidout->sid_rev_num = (uint8_t) conv;
130         q++;
131
132         if (!isdigit(*q)) {
133                 goto format_error;
134         }
135
136         /* get identauth */
137         conv = (uint32_t) strtoul(q, &q, 10);
138         if (!q) {
139                 goto format_error;
140         } else if (*q == '\0') {
141                 /* Just id_auth, no subauths */
142         } else if (*q != '-') {
143                 goto format_error;
144         }
145         /* identauth in decimal should be <  2^32 */
146         /* NOTE - the conv value is in big-endian format. */
147         sidout->id_auth[0] = 0;
148         sidout->id_auth[1] = 0;
149         sidout->id_auth[2] = (conv & 0xff000000) >> 24;
150         sidout->id_auth[3] = (conv & 0x00ff0000) >> 16;
151         sidout->id_auth[4] = (conv & 0x0000ff00) >> 8;
152         sidout->id_auth[5] = (conv & 0x000000ff);
153
154         sidout->num_auths = 0;
155         if (*q == '\0') {
156                 return true;
157         }
158
159         q++;
160
161         while (true) {
162                 char *end;
163
164                 if (!isdigit(*q)) {
165                         goto format_error;
166                 }
167
168                 conv = strtoul(q, &end, 10);
169                 if (end == q) {
170                         goto format_error;
171                 }
172
173                 if (!sid_append_rid(sidout, conv)) {
174                         DEBUG(3, ("Too many sid auths in %s\n", sidstr));
175                         return false;
176                 }
177
178                 q = end;
179                 if (*q == '\0') {
180                         break;
181                 }
182                 if (*q != '-') {
183                         goto format_error;
184                 }
185                 q += 1;
186         }
187         return true;
188
189 format_error:
190         DEBUG(3, ("string_to_sid: SID %s is not in a valid format\n", sidstr));
191         return false;
192 }
193
194 bool dom_sid_parse(const char *sidstr, struct dom_sid *ret)
195 {
196         return string_to_sid(ret, sidstr);
197 }
198
199 /*
200   convert a string to a dom_sid, returning a talloc'd dom_sid
201 */
202 struct dom_sid *dom_sid_parse_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
203 {
204         struct dom_sid *ret;
205         ret = talloc(mem_ctx, struct dom_sid);
206         if (!ret) {
207                 return NULL;
208         }
209         if (!dom_sid_parse(sidstr, ret)) {
210                 talloc_free(ret);
211                 return NULL;
212         }
213
214         return ret;
215 }
216
217 /*
218   convert a string to a dom_sid, returning a talloc'd dom_sid
219 */
220 struct dom_sid *dom_sid_parse_length(TALLOC_CTX *mem_ctx, const DATA_BLOB *sid)
221 {
222         struct dom_sid *ret;
223         char *p = talloc_strndup(mem_ctx, (char *)sid->data, sid->length);
224         if (!p) {
225                 return NULL;
226         }
227         ret = dom_sid_parse_talloc(mem_ctx, p);
228         talloc_free(p);
229         return ret;
230 }
231
232 /*
233   copy a dom_sid structure
234 */
235 struct dom_sid *dom_sid_dup(TALLOC_CTX *mem_ctx, const struct dom_sid *dom_sid)
236 {
237         struct dom_sid *ret;
238         int i;
239
240         if (!dom_sid) {
241                 return NULL;
242         }
243
244         ret = talloc(mem_ctx, struct dom_sid);
245         if (!ret) {
246                 return NULL;
247         }
248
249         ret->sid_rev_num = dom_sid->sid_rev_num;
250         ret->id_auth[0] = dom_sid->id_auth[0];
251         ret->id_auth[1] = dom_sid->id_auth[1];
252         ret->id_auth[2] = dom_sid->id_auth[2];
253         ret->id_auth[3] = dom_sid->id_auth[3];
254         ret->id_auth[4] = dom_sid->id_auth[4];
255         ret->id_auth[5] = dom_sid->id_auth[5];
256         ret->num_auths = dom_sid->num_auths;
257
258         for (i=0;i<dom_sid->num_auths;i++) {
259                 ret->sub_auths[i] = dom_sid->sub_auths[i];
260         }
261
262         return ret;
263 }
264
265 /*
266   add a rid to a domain dom_sid to make a full dom_sid. This function
267   returns a new sid in the supplied memory context
268 */
269 struct dom_sid *dom_sid_add_rid(TALLOC_CTX *mem_ctx,
270                                 const struct dom_sid *domain_sid,
271                                 uint32_t rid)
272 {
273         struct dom_sid *sid;
274
275         sid = talloc(mem_ctx, struct dom_sid);
276         if (!sid) return NULL;
277
278         *sid = *domain_sid;
279
280         sid->sub_auths[sid->num_auths] = rid;
281         sid->num_auths++;
282
283         return sid;
284 }
285
286 /*
287   Split up a SID into its domain and RID part
288 */
289 NTSTATUS dom_sid_split_rid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
290                            struct dom_sid **domain, uint32_t *rid)
291 {
292         if (sid->num_auths == 0) {
293                 return NT_STATUS_INVALID_PARAMETER;
294         }
295
296         if (domain) {
297                 if (!(*domain = dom_sid_dup(mem_ctx, sid))) {
298                         return NT_STATUS_NO_MEMORY;
299                 }
300
301                 (*domain)->num_auths -= 1;
302         }
303
304         if (rid) {
305                 *rid = sid->sub_auths[sid->num_auths - 1];
306         }
307
308         return NT_STATUS_OK;
309 }
310
311 /*
312   return true if the 2nd sid is in the domain given by the first sid
313 */
314 bool dom_sid_in_domain(const struct dom_sid *domain_sid,
315                        const struct dom_sid *sid)
316 {
317         int i;
318
319         if (!domain_sid || !sid) {
320                 return false;
321         }
322
323         if (domain_sid->num_auths > sid->num_auths) {
324                 return false;
325         }
326
327         for (i = domain_sid->num_auths-1; i >= 0; --i) {
328                 if (domain_sid->sub_auths[i] != sid->sub_auths[i]) {
329                         return false;
330                 }
331         }
332
333         return dom_sid_compare_auth(domain_sid, sid) == 0;
334 }
335
336 /*
337   convert a dom_sid to a string
338 */
339 char *dom_sid_string(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
340 {
341         int i, ofs, maxlen;
342         uint32_t ia;
343         char *ret;
344
345         if (!sid) {
346                 return talloc_strdup(mem_ctx, "(NULL SID)");
347         }
348
349         maxlen = sid->num_auths * 11 + 25;
350         ret = talloc_array(mem_ctx, char, maxlen);
351         if (!ret) return talloc_strdup(mem_ctx, "(SID ERR)");
352
353         ia = (sid->id_auth[5]) +
354                 (sid->id_auth[4] << 8 ) +
355                 (sid->id_auth[3] << 16) +
356                 (sid->id_auth[2] << 24);
357
358         ofs = snprintf(ret, maxlen, "S-%u-%lu",
359                        (unsigned int)sid->sid_rev_num, (unsigned long)ia);
360
361         for (i = 0; i < sid->num_auths; i++) {
362                 ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu",
363                                 (unsigned long)sid->sub_auths[i]);
364         }
365
366         return ret;
367 }