2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Luke Kenneth Caseson Leighton 1998-1999
6 Copyright (C) Jeremy Allison 1999
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.
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.
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.
25 /* NOTE! the global_sam_sid is the SID of our local SAM. This is only
26 equal to the domain SID when we are a DC, otherwise its our
28 extern DOM_SID global_sam_sid;
29 extern pstring global_myname;
30 extern fstring global_myworkgroup;
36 DOM_SID global_sid_Builtin; /* Local well-known domain */
37 DOM_SID global_sid_World_Domain; /* Everyone domain */
38 DOM_SID global_sid_World; /* Everyone */
39 DOM_SID global_sid_Creator_Owner_Domain; /* Creator Owner domain */
40 DOM_SID global_sid_Creator_Owner; /* Creator Owner */
41 DOM_SID global_sid_Creator_Group; /* Creator Group */
42 DOM_SID global_sid_NT_Authority; /* NT Authority */
43 DOM_SID global_sid_NULL; /* NULL sid */
44 DOM_SID global_sid_Builtin_Guests; /* Builtin guest users */
45 DOM_SID global_sid_Authenticated_Users; /* All authenticated rids */
46 DOM_SID global_sid_Network; /* Network rids */
47 DOM_SID global_sid_Anonymous; /* Anonymous login */
49 const DOM_SID *global_sid_everyone = &global_sid_World;
52 * An NT compatible anonymous token.
55 static DOM_SID anon_sid_array[3];
57 NT_USER_TOKEN anonymous_token = {
62 /****************************************************************************
63 Creates some useful well known sids
64 ****************************************************************************/
66 void generate_wellknown_sids(void)
68 string_to_sid(&global_sid_Builtin, "S-1-5-32");
69 string_to_sid(&global_sid_Builtin_Guests, "S-1-5-32-546");
70 string_to_sid(&global_sid_World_Domain, "S-1-1");
71 string_to_sid(&global_sid_World, "S-1-1-0");
72 string_to_sid(&global_sid_Creator_Owner_Domain, "S-1-3");
73 string_to_sid(&global_sid_Creator_Owner, "S-1-3-0");
74 string_to_sid(&global_sid_Creator_Group, "S-1-3-1");
75 string_to_sid(&global_sid_NT_Authority, "S-1-5");
76 string_to_sid(&global_sid_NULL, "S-1-0-0");
77 string_to_sid(&global_sid_Authenticated_Users, "S-1-5-11");
78 string_to_sid(&global_sid_Network, "S-1-5-2");
79 string_to_sid(&global_sid_Anonymous, "S-1-5-7");
81 /* Create the anon token. */
82 sid_copy( &anonymous_token.user_sids[0], &global_sid_World);
83 sid_copy( &anonymous_token.user_sids[1], &global_sid_Network);
84 sid_copy( &anonymous_token.user_sids[2], &global_sid_Anonymous);
87 /**************************************************************************
88 Splits a name of format \DOMAIN\name or name into its two components.
89 Sets the DOMAIN name to global_myname if it has not been specified.
90 ***************************************************************************/
92 void split_domain_name(const char *fullname, char *domain, char *name)
97 sep = lp_winbind_separator();
99 *domain = *name = '\0';
101 if (fullname[0] == sep[0] || fullname[0] == '\\')
104 pstrcpy(full_name, fullname);
105 p = strchr_m(full_name+1, '\\');
106 if (!p) p = strchr_m(full_name+1, sep[0]);
110 fstrcpy(domain, full_name);
113 fstrcpy(domain, global_myname);
114 fstrcpy(name, full_name);
117 DEBUG(10,("split_domain_name:name '%s' split into domain :'%s' and user :'%s'\n",
118 fullname, domain, name));
121 /*****************************************************************
122 Convert a SID to an ascii string.
123 *****************************************************************/
125 char *sid_to_string(fstring sidstr_out, DOM_SID *sid)
132 fstrcpy(sidstr_out, "(NULL SID)");
136 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
137 ia = (sid->id_auth[5]) +
138 (sid->id_auth[4] << 8 ) +
139 (sid->id_auth[3] << 16) +
140 (sid->id_auth[2] << 24);
142 slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
144 for (i = 0; i < sid->num_auths; i++) {
145 slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
146 fstrcat(sidstr_out, subauth);
153 useful function for debug lines
155 const char *sid_string_static(DOM_SID *sid)
157 static fstring sid_str;
158 sid_to_string(sid_str, sid);
162 /*****************************************************************
163 Convert a string to a SID. Returns True on success, False on fail.
164 *****************************************************************/
166 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
170 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
173 if (StrnCaseCmp( sidstr, "S-", 2)) {
174 DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
178 memset((char *)sidout, '\0', sizeof(DOM_SID));
180 q = p = strdup(sidstr + 2);
182 DEBUG(0, ("string_to_sid: out of memory!\n"));
186 if (!next_token(&p, tok, "-", sizeof(tok))) {
187 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
192 /* Get the revision number. */
193 sidout->sid_rev_num = (uint8)strtoul(tok, NULL, 10);
195 if (!next_token(&p, tok, "-", sizeof(tok))) {
196 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
201 /* identauth in decimal should be < 2^32 */
202 ia = (uint32)strtoul(tok, NULL, 10);
204 /* NOTE - the ia value is in big-endian format. */
205 sidout->id_auth[0] = 0;
206 sidout->id_auth[1] = 0;
207 sidout->id_auth[2] = (ia & 0xff000000) >> 24;
208 sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
209 sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
210 sidout->id_auth[5] = (ia & 0x000000ff);
212 sidout->num_auths = 0;
214 while(next_token(&p, tok, "-", sizeof(tok)) &&
215 sidout->num_auths < MAXSUBAUTHS) {
217 * NOTE - the subauths are in native machine-endian format. They
218 * are converted to little-endian when linearized onto the wire.
220 sid_append_rid(sidout, (uint32)strtoul(tok, NULL, 10));
227 /*****************************************************************
228 Add a rid to the end of a sid
229 *****************************************************************/
231 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
233 if (sid->num_auths < MAXSUBAUTHS) {
234 sid->sub_auths[sid->num_auths++] = rid;
240 /*****************************************************************
241 Removes the last rid from the end of a sid
242 *****************************************************************/
244 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
246 if (sid->num_auths > 0) {
248 *rid = sid->sub_auths[sid->num_auths];
254 /*****************************************************************
255 Return the last rid from the end of a sid
256 *****************************************************************/
258 BOOL sid_peek_rid(DOM_SID *sid, uint32 *rid)
260 if (sid->num_auths > 0) {
261 *rid = sid->sub_auths[sid->num_auths - 1];
267 /*****************************************************************
269 *****************************************************************/
271 void sid_copy(DOM_SID *dst, const DOM_SID *src)
275 memset((char *)dst, '\0', sizeof(DOM_SID));
277 dst->sid_rev_num = src->sid_rev_num;
278 dst->num_auths = src->num_auths;
280 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
282 for (i = 0; i < src->num_auths; i++)
283 dst->sub_auths[i] = src->sub_auths[i];
286 /*****************************************************************
287 Duplicates a sid - mallocs the target.
288 *****************************************************************/
290 DOM_SID *sid_dup(DOM_SID *src)
297 if((dst = malloc(sizeof(DOM_SID))) != NULL) {
298 memset(dst, '\0', sizeof(DOM_SID));
305 /*****************************************************************
306 Write a sid out into on-the-wire format.
307 *****************************************************************/
308 BOOL sid_linearize(char *outbuf, size_t len, DOM_SID *sid)
312 if (len < sid_size(sid))
315 SCVAL(outbuf,0,sid->sid_rev_num);
316 SCVAL(outbuf,1,sid->num_auths);
317 memcpy(&outbuf[2], sid->id_auth, 6);
318 for(i = 0; i < sid->num_auths; i++)
319 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
324 /*****************************************************************
325 parse a on-the-wire SID to a DOM_SID
326 *****************************************************************/
327 BOOL sid_parse(char *inbuf, size_t len, DOM_SID *sid)
330 if (len < 8) return False;
331 sid->sid_rev_num = CVAL(inbuf, 0);
332 sid->num_auths = CVAL(inbuf, 1);
333 memcpy(sid->id_auth, inbuf+2, 6);
334 if (len < 8 + sid->num_auths*4) return False;
335 for (i=0;i<sid->num_auths;i++) {
336 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
342 /*****************************************************************
343 Compare the auth portion of two sids.
344 *****************************************************************/
345 int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
349 if (sid1 == sid2) return 0;
350 if (!sid1) return -1;
353 if (sid1->sid_rev_num != sid2->sid_rev_num)
354 return sid1->sid_rev_num - sid2->sid_rev_num;
356 for (i = 0; i < 6; i++)
357 if (sid1->id_auth[i] != sid2->id_auth[i])
358 return sid1->id_auth[i] - sid2->id_auth[i];
363 /*****************************************************************
365 *****************************************************************/
366 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
370 if (sid1 == sid2) return 0;
371 if (!sid1) return -1;
374 /* compare most likely different rids, first: i.e start at end */
375 if (sid1->num_auths != sid2->num_auths)
376 return sid1->num_auths - sid2->num_auths;
378 for (i = sid1->num_auths-1; i >= 0; --i)
379 if (sid1->sub_auths[i] != sid2->sub_auths[i])
380 return sid1->sub_auths[i] - sid2->sub_auths[i];
382 return sid_compare_auth(sid1, sid2);
385 /*****************************************************************
386 see if 2 SIDs are in the same domain
387 this just compares the leading sub-auths
388 *****************************************************************/
389 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
393 n = MIN(sid1->num_auths, sid2->num_auths);
395 for (i = n-1; i >= 0; --i)
396 if (sid1->sub_auths[i] != sid2->sub_auths[i])
397 return sid1->sub_auths[i] - sid2->sub_auths[i];
399 return sid_compare_auth(sid1, sid2);
402 /*****************************************************************
404 *****************************************************************/
405 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
407 return sid_compare(sid1, sid2) == 0;
412 /*****************************************************************
413 Check if the SID is the builtin SID (S-1-5-32).
414 *****************************************************************/
415 BOOL sid_check_is_builtin(const DOM_SID *sid)
417 return sid_equal(sid, &global_sid_Builtin);
421 /*****************************************************************
422 Check if the SID is our domain SID (S-1-5-21-x-y-z).
423 *****************************************************************/
424 BOOL sid_check_is_in_builtin(const DOM_SID *sid)
429 sid_copy(&dom_sid, sid);
430 sid_split_rid(&dom_sid, &rid);
432 return sid_equal(&dom_sid, &global_sid_Builtin);
436 /*****************************************************************
437 Calculates size of a sid.
438 *****************************************************************/
440 size_t sid_size(DOM_SID *sid)
445 return sid->num_auths * sizeof(uint32) + 8;
448 /*****************************************************************
449 Returns true if SID is internal (and non-mappable).
450 *****************************************************************/
452 BOOL non_mappable_sid(DOM_SID *sid)
458 sid_split_rid(&dom, &rid);
460 if (sid_equal(&dom, &global_sid_Builtin))
463 if (sid_equal(&dom, &global_sid_Creator_Owner_Domain))
466 if (sid_equal(&dom, &global_sid_NT_Authority))
473 return the binary string representation of a DOM_SID
476 char *sid_binstring(DOM_SID *sid)
479 int len = sid_size(sid);
481 if (!buf) return NULL;
482 sid_linearize(buf, len, sid);
483 s = binary_string(buf, len);