2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Stefan (metze) Metzmacher 2002-2004
6 Copyright (C) Andrew Tridgell 1992-2004
7 Copyright (C) Jeremy Allison 1999
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.
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.
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/>.
24 #include "lib/util/data_blob.h"
25 #include "system/locale.h"
26 #include "lib/util/debug.h"
27 #include "lib/util/util.h"
28 #include "librpc/gen_ndr/security.h"
30 #include "lib/util/smb_strtox.h"
31 #include "lib/util/tsort.h"
33 /*****************************************************************
34 Compare the auth portion of two sids.
35 *****************************************************************/
37 int dom_sid_compare_auth(const struct dom_sid
*sid1
,
38 const struct dom_sid
*sid2
)
54 if (sid1
->sid_rev_num
!= sid2
->sid_rev_num
) {
55 return NUMERIC_CMP(sid1
->sid_rev_num
, sid2
->sid_rev_num
);
58 for (i
= 0; i
< 6; i
++) {
59 if (sid1
->id_auth
[i
] != sid2
->id_auth
[i
]) {
60 return NUMERIC_CMP(sid1
->id_auth
[i
], sid2
->id_auth
[i
]);
67 /*****************************************************************
69 *****************************************************************/
71 int dom_sid_compare(const struct dom_sid
*sid1
, const struct dom_sid
*sid2
)
87 /* Compare most likely different rids, first: i.e start at end */
88 if (sid1
->num_auths
!= sid2
->num_auths
) {
89 return NUMERIC_CMP(sid1
->num_auths
, sid2
->num_auths
);
91 for (i
= sid1
->num_auths
-1; i
>= 0; --i
) {
92 if (sid1
->sub_auths
[i
] < sid2
->sub_auths
[i
]) {
95 if (sid1
->sub_auths
[i
] > sid2
->sub_auths
[i
]) {
100 return dom_sid_compare_auth(sid1
, sid2
);
103 /*****************************************************************
105 *****************************************************************/
107 bool dom_sid_equal(const struct dom_sid
*sid1
, const struct dom_sid
*sid2
)
109 return dom_sid_compare(sid1
, sid2
) == 0;
112 /*****************************************************************
113 Add a rid to the end of a sid
114 *****************************************************************/
116 bool sid_append_rid(struct dom_sid
*sid
, uint32_t rid
)
118 if (sid
->num_auths
< ARRAY_SIZE(sid
->sub_auths
)) {
119 sid
->sub_auths
[sid
->num_auths
++] = rid
;
126 See if 2 SIDs are in the same domain
127 this just compares the leading sub-auths
129 int dom_sid_compare_domain(const struct dom_sid
*sid1
,
130 const struct dom_sid
*sid2
)
134 n
= MIN(sid1
->num_auths
, sid2
->num_auths
);
136 for (i
= n
-1; i
>= 0; --i
) {
137 if (sid1
->sub_auths
[i
] < sid2
->sub_auths
[i
]) {
140 if (sid1
->sub_auths
[i
] > sid2
->sub_auths
[i
]) {
145 return dom_sid_compare_auth(sid1
, sid2
);
148 /*****************************************************************
149 Convert a string to a SID. Returns True on success, False on fail.
150 Return the first character not parsed in endp.
151 *****************************************************************/
152 #define AUTHORITY_MASK (~(0xffffffffffffULL))
154 bool dom_sid_parse_endp(const char *sidstr
,struct dom_sid
*sidout
,
163 *sidout
= (struct dom_sid
) {};
165 if ((sidstr
[0] != 'S' && sidstr
[0] != 's') || sidstr
[1] != '-') {
169 /* Get the revision number. */
172 if (!isdigit((unsigned char)*p
)) {
176 conv
= smb_strtoul(p
, &q
, 10, &error
, SMB_STR_STANDARD
);
177 if (error
!= 0 || (*q
!= '-') || conv
> UINT8_MAX
|| q
- p
> 4) {
180 sidout
->sid_rev_num
= (uint8_t) conv
;
183 if (!isdigit((unsigned char)*q
)) {
186 while (q
[0] == '0' && isdigit((unsigned char)q
[1])) {
188 * strtoull will think this is octal, which is not how SIDs
189 * work! So let's walk along until there are no leading zeros
190 * (or a single zero).
196 conv
= smb_strtoull(q
, &end
, 0, &error
, SMB_STR_STANDARD
);
197 if (conv
& AUTHORITY_MASK
|| error
!= 0) {
200 if (conv
>= (1ULL << 48) || end
- q
> 15) {
202 * This identauth looks like a big number, but resolves to a
203 * small number after rounding.
208 /* NOTE - the conv value is in big-endian format. */
209 sidout
->id_auth
[0] = (conv
& 0xff0000000000ULL
) >> 40;
210 sidout
->id_auth
[1] = (conv
& 0x00ff00000000ULL
) >> 32;
211 sidout
->id_auth
[2] = (conv
& 0x0000ff000000ULL
) >> 24;
212 sidout
->id_auth
[3] = (conv
& 0x000000ff0000ULL
) >> 16;
213 sidout
->id_auth
[4] = (conv
& 0x00000000ff00ULL
) >> 8;
214 sidout
->id_auth
[5] = (conv
& 0x0000000000ffULL
);
216 sidout
->num_auths
= 0;
219 /* Just id_auth, no subauths */
226 if (!isdigit((unsigned char)*q
)) {
229 while (q
[0] == '0' && isdigit((unsigned char)q
[1])) {
231 * strtoull will think this is octal, which is not how
232 * SIDs work! So let's walk along until there are no
233 * leading zeros (or a single zero).
237 conv
= smb_strtoull(q
, &end
, 0, &error
, SMB_STR_STANDARD
);
238 if (conv
> UINT32_MAX
|| error
!= 0 || end
- q
> 12) {
240 * This sub-auth is greater than 4294967295,
241 * and hence invalid. Windows will treat it as
242 * 4294967295, while we prefer to refuse (old
243 * versions of Samba will wrap, arriving at
244 * another number altogether).
246 DBG_NOTICE("bad sub-auth in %s\n", sidstr
);
250 if (!sid_append_rid(sidout
, conv
)) {
251 DEBUG(3, ("Too many sid auths in %s\n", sidstr
));
268 DEBUG(3, ("string_to_sid: SID %s is not in a valid format\n", sidstr
));
272 bool string_to_sid(struct dom_sid
*sidout
, const char *sidstr
)
274 return dom_sid_parse(sidstr
, sidout
);
277 bool dom_sid_parse(const char *sidstr
, struct dom_sid
*ret
)
279 return dom_sid_parse_endp(sidstr
, ret
, NULL
);
283 convert a string to a dom_sid, returning a talloc'd dom_sid
285 struct dom_sid
*dom_sid_parse_talloc(TALLOC_CTX
*mem_ctx
, const char *sidstr
)
288 ret
= talloc(mem_ctx
, struct dom_sid
);
292 if (!dom_sid_parse(sidstr
, ret
)) {
301 convert a string to a dom_sid, returning a talloc'd dom_sid
303 struct dom_sid
*dom_sid_parse_length(TALLOC_CTX
*mem_ctx
, const DATA_BLOB
*sid
)
305 char p
[sid
->length
+1];
306 memcpy(p
, sid
->data
, sid
->length
);
307 p
[sid
->length
] = '\0';
308 return dom_sid_parse_talloc(mem_ctx
, p
);
312 copy a dom_sid structure
314 struct dom_sid
*dom_sid_dup(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*dom_sid
)
322 ret
= talloc(mem_ctx
, struct dom_sid
);
326 sid_copy(ret
, dom_sid
);
332 add a rid to a domain dom_sid to make a full dom_sid. This function
333 returns a new sid in the supplied memory context
335 struct dom_sid
*dom_sid_add_rid(TALLOC_CTX
*mem_ctx
,
336 const struct dom_sid
*domain_sid
,
341 sid
= dom_sid_dup(mem_ctx
, domain_sid
);
342 if (!sid
) return NULL
;
344 if (!sid_append_rid(sid
, rid
)) {
353 Split up a SID into its domain and RID part
355 NTSTATUS
dom_sid_split_rid(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
,
356 struct dom_sid
**domain
, uint32_t *rid
)
358 if (sid
->num_auths
== 0) {
359 return NT_STATUS_INVALID_PARAMETER
;
363 if (!(*domain
= dom_sid_dup(mem_ctx
, sid
))) {
364 return NT_STATUS_NO_MEMORY
;
367 (*domain
)->num_auths
-= 1;
371 *rid
= sid
->sub_auths
[sid
->num_auths
- 1];
378 return true if the 2nd sid is in the domain given by the first sid
380 bool dom_sid_in_domain(const struct dom_sid
*domain_sid
,
381 const struct dom_sid
*sid
)
385 if (!domain_sid
|| !sid
) {
389 if (sid
->num_auths
< 2) {
393 if (domain_sid
->num_auths
!= (sid
->num_auths
- 1)) {
397 for (i
= domain_sid
->num_auths
-1; i
>= 0; --i
) {
398 if (domain_sid
->sub_auths
[i
] != sid
->sub_auths
[i
]) {
403 return dom_sid_compare_auth(domain_sid
, sid
) == 0;
406 bool dom_sid_has_account_domain(const struct dom_sid
*sid
)
412 if (sid
->sid_rev_num
!= 1) {
415 if (sid
->num_auths
!= 5) {
418 if (sid
->id_auth
[5] != 5) {
421 if (sid
->id_auth
[4] != 0) {
424 if (sid
->id_auth
[3] != 0) {
427 if (sid
->id_auth
[2] != 0) {
430 if (sid
->id_auth
[1] != 0) {
433 if (sid
->id_auth
[0] != 0) {
436 if (sid
->sub_auths
[0] != 21) {
443 bool dom_sid_is_valid_account_domain(const struct dom_sid
*sid
)
446 * We expect S-1-5-21-9-8-7, but we don't
447 * allow S-1-5-21-0-0-0 as this is used
448 * for claims and compound identities.
450 * With this structure:
453 * uint8_t sid_rev_num;
454 * int8_t num_auths; [range(0,15)]
455 * uint8_t id_auth[6];
456 * uint32_t sub_auths[15];
459 * S-1-5-21-9-8-7 looks like this:
460 * {1, 4, {0,0,0,0,0,5}, {21,9,8,7,0,0,0,0,0,0,0,0,0,0,0}};
466 if (sid
->sid_rev_num
!= 1) {
469 if (sid
->num_auths
!= 4) {
472 if (sid
->id_auth
[5] != 5) {
475 if (sid
->id_auth
[4] != 0) {
478 if (sid
->id_auth
[3] != 0) {
481 if (sid
->id_auth
[2] != 0) {
484 if (sid
->id_auth
[1] != 0) {
487 if (sid
->id_auth
[0] != 0) {
490 if (sid
->sub_auths
[0] != 21) {
493 if (sid
->sub_auths
[1] == 0) {
496 if (sid
->sub_auths
[2] == 0) {
499 if (sid
->sub_auths
[3] == 0) {
507 Convert a dom_sid to a string, printing into a buffer. Return the
508 string length. If it overflows, return the string length that would
509 result (buflen needs to be +1 for the terminating 0).
511 static int dom_sid_string_buf(const struct dom_sid
*sid
, char *buf
, int buflen
)
517 return strlcpy(buf
, "(NULL SID)", buflen
);
520 ia
= ((uint64_t)sid
->id_auth
[5]) +
521 ((uint64_t)sid
->id_auth
[4] << 8 ) +
522 ((uint64_t)sid
->id_auth
[3] << 16) +
523 ((uint64_t)sid
->id_auth
[2] << 24) +
524 ((uint64_t)sid
->id_auth
[1] << 32) +
525 ((uint64_t)sid
->id_auth
[0] << 40);
527 ret
= snprintf(buf
, buflen
, "S-%"PRIu8
"-", sid
->sid_rev_num
);
533 if (ia
>= UINT32_MAX
) {
534 ret
= snprintf(buf
+ofs
, MAX(buflen
-ofs
, 0), "0x%"PRIx64
, ia
);
536 ret
= snprintf(buf
+ofs
, MAX(buflen
-ofs
, 0), "%"PRIu64
, ia
);
543 for (i
= 0; i
< sid
->num_auths
; i
++) {
558 convert a dom_sid to a string
560 char *dom_sid_string(TALLOC_CTX
*mem_ctx
, const struct dom_sid
*sid
)
562 char buf
[DOM_SID_STR_BUFLEN
];
566 len
= dom_sid_string_buf(sid
, buf
, sizeof(buf
));
568 if ((len
< 0) || (len
+1 > sizeof(buf
))) {
569 return talloc_strdup(mem_ctx
, "(SID ERR)");
573 * Avoid calling strlen (via talloc_strdup), we already have
576 result
= (char *)talloc_memdup(mem_ctx
, buf
, len
+1);
577 if (result
== NULL
) {
582 * beautify the talloc_report output
584 talloc_set_name_const(result
, result
);
588 char *dom_sid_str_buf(const struct dom_sid
*sid
, struct dom_sid_buf
*dst
)
591 ret
= dom_sid_string_buf(sid
, dst
->buf
, sizeof(dst
->buf
));
592 if ((ret
< 0) || (ret
>= sizeof(dst
->buf
))) {
593 strlcpy(dst
->buf
, "(INVALID SID)", sizeof(dst
->buf
));