2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
6 Copyright (C) Luke Kenneth Caseson Leighton 1998-1999
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 extern int DEBUGLEVEL;
28 DOM_SID global_sam_sid;
29 extern pstring global_myname;
30 extern fstring global_myworkgroup;
36 DOM_SID global_sid_S_1_5_0x20; /* local well-known domain */
37 DOM_SID global_sid_World_Domain; /* everyone */
38 DOM_SID global_sid_World; /* everyone */
39 DOM_SID global_sid_Creator_Owner_Domain; /* Creator Owner */
40 DOM_SID global_sid_Creator_Owner; /* Creator Owner */
41 DOM_SID global_sid_NT_Authority; /* NT Authority */
43 typedef struct _known_sid_users {
46 char *known_user_name;
49 /* static known_sid_users no_users[] = {{0, 0, NULL}}; */
50 static known_sid_users everyone_users[] = {{ 0, SID_NAME_WKN_GRP, "Everyone" }, {0, 0, NULL}};
51 static known_sid_users creator_owner_users[] = {{ 0, SID_NAME_ALIAS, "Creator Owner" }, {0, 0, NULL}};
52 static known_sid_users nt_authority_users[] = {{ 1, SID_NAME_ALIAS, "Dialup" },
53 { 2, SID_NAME_ALIAS, "Network"},
54 { 3, SID_NAME_ALIAS, "Batch"},
55 { 4, SID_NAME_ALIAS, "Interactive"},
56 { 6, SID_NAME_ALIAS, "Service"},
57 { 7, SID_NAME_ALIAS, "AnonymousLogon"},
58 { 8, SID_NAME_ALIAS, "Proxy"},
59 { 9, SID_NAME_ALIAS, "ServerLogon"},
62 static struct sid_name_map_info
66 known_sid_users *known_users;
70 { &global_sam_sid, global_myname, NULL},
71 { &global_sam_sid, global_myworkgroup, NULL},
72 { &global_sid_S_1_5_0x20, "BUILTIN", NULL},
73 { &global_sid_World_Domain, "", &everyone_users[0] },
74 { &global_sid_Creator_Owner_Domain, "", &creator_owner_users[0] },
75 { &global_sid_NT_Authority, "NT Authority", &nt_authority_users[0] },
79 /****************************************************************************
80 Creates some useful well known sids
81 ****************************************************************************/
83 void generate_wellknown_sids(void)
85 string_to_sid(&global_sid_S_1_5_0x20, "S-1-5-32");
86 string_to_sid(&global_sid_World_Domain, "S-1-1");
87 string_to_sid(&global_sid_World, "S-1-1-0");
88 string_to_sid(&global_sid_Creator_Owner_Domain, "S-1-3");
89 string_to_sid(&global_sid_Creator_Owner, "S-1-3-0");
90 string_to_sid(&global_sid_NT_Authority, "S-1-5");
93 /**************************************************************************
94 Turns a domain SID into a name, returned in the nt_domain argument.
95 ***************************************************************************/
97 BOOL map_domain_sid_to_name(DOM_SID *sid, char *nt_domain)
101 sid_to_string(sid_str, sid);
103 DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str));
105 if (nt_domain == NULL)
108 while (sid_name_map[i].sid != NULL) {
109 sid_to_string(sid_str, sid_name_map[i].sid);
110 DEBUG(5,("map_domain_sid_to_name: compare: %s\n", sid_str));
111 if (sid_equal(sid_name_map[i].sid, sid)) {
112 fstrcpy(nt_domain, sid_name_map[i].name);
113 DEBUG(5,("map_domain_sid_to_name: found '%s'\n", nt_domain));
119 DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n", sid_str));
124 /**************************************************************************
125 Looks up a known username from one of the known domains.
126 ***************************************************************************/
128 BOOL lookup_known_rid(DOM_SID *sid, uint32 rid, char *name, uint8 *psid_name_use)
131 struct sid_name_map_info *psnm;
133 for(i = 0; sid_name_map[i].sid != NULL; i++) {
134 psnm = &sid_name_map[i];
135 if(sid_equal(psnm->sid, sid)) {
137 for(j = 0; psnm->known_users && psnm->known_users[j].known_user_name != NULL; j++) {
138 if(rid == psnm->known_users[j].rid) {
139 DEBUG(5,("lookup_builtin_rid: rid = %u, domain = '%s', user = '%s'\n",
140 (unsigned int)rid, psnm->name, psnm->known_users[j].known_user_name ));
141 fstrcpy( name, psnm->known_users[j].known_user_name);
142 *psid_name_use = psnm->known_users[j].sid_name_use;
152 /**************************************************************************
153 Turns a domain name into a SID.
154 *** side-effect: if the domain name is NULL, it is set to our domain ***
155 ***************************************************************************/
157 BOOL map_domain_name_to_sid(DOM_SID *sid, char *nt_domain)
161 if (nt_domain == NULL) {
162 DEBUG(5,("map_domain_name_to_sid: mapping NULL domain to our SID.\n"));
163 sid_copy(sid, &global_sam_sid);
167 if (nt_domain[0] == 0) {
168 fstrcpy(nt_domain, global_myname);
169 DEBUG(5,("map_domain_name_to_sid: overriding blank name to %s\n", nt_domain));
170 sid_copy(sid, &global_sam_sid);
174 DEBUG(5,("map_domain_name_to_sid: %s\n", nt_domain));
176 while (sid_name_map[i].name != NULL) {
177 DEBUG(5,("map_domain_name_to_sid: compare: %s\n", sid_name_map[i].name));
178 if (strequal(sid_name_map[i].name, nt_domain)) {
180 sid_copy(sid, sid_name_map[i].sid);
181 sid_to_string(sid_str, sid_name_map[i].sid);
182 DEBUG(5,("map_domain_name_to_sid: found %s\n", sid_str));
188 DEBUG(0,("map_domain_name_to_sid: mapping to %s not found.\n", nt_domain));
192 /**************************************************************************
193 Splits a name of format \DOMAIN\name or name into its two components.
194 Sets the DOMAIN name to global_myname if it has not been specified.
195 ***************************************************************************/
197 void split_domain_name(const char *fullname, char *domain, char *name)
202 *domain = *name = '\0';
204 if (fullname[0] == '\\')
207 pstrcpy(full_name, fullname);
208 p = strchr(full_name+1, '\\');
212 fstrcpy(domain, full_name);
215 fstrcpy(domain, global_myname);
216 fstrcpy(name, full_name);
219 DEBUG(10,("split_domain_name:name '%s' split into domain :'%s' and user :'%s'\n",
220 fullname, domain, name));
223 /*****************************************************************
224 Convert a SID to an ascii string.
225 *****************************************************************/
227 char *sid_to_string(fstring sidstr_out, DOM_SID *sid)
231 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
232 uint32 ia = (sid->id_auth[5]) +
233 (sid->id_auth[4] << 8 ) +
234 (sid->id_auth[3] << 16) +
235 (sid->id_auth[2] << 24);
237 slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
239 for (i = 0; i < sid->num_auths; i++) {
240 slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
241 fstrcat(sidstr_out, subauth);
244 DEBUG(7,("sid_to_string returning %s\n", sidstr_out));
248 /*****************************************************************
249 Convert a string to a SID. Returns True on success, False on fail.
250 *****************************************************************/
252 BOOL string_to_sid(DOM_SID *sidout, char *sidstr)
256 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
259 memset((char *)sidout, '\0', sizeof(DOM_SID));
261 if (StrnCaseCmp( sidstr, "S-", 2)) {
262 DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
267 if (!next_token(&p, tok, "-", sizeof(tok))) {
268 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
272 /* Get the revision number. */
273 sidout->sid_rev_num = (uint8)strtoul(tok, NULL, 10);
275 if (!next_token(&p, tok, "-", sizeof(tok))) {
276 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
280 /* identauth in decimal should be < 2^32 */
281 ia = (uint32)strtoul(tok, NULL, 10);
283 /* NOTE - the ia value is in big-endian format. */
284 sidout->id_auth[0] = 0;
285 sidout->id_auth[1] = 0;
286 sidout->id_auth[2] = (ia & 0xff000000) >> 24;
287 sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
288 sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
289 sidout->id_auth[5] = (ia & 0x000000ff);
291 sidout->num_auths = 0;
293 while(next_token(&p, tok, "-", sizeof(tok)) &&
294 sidout->num_auths < MAXSUBAUTHS) {
296 * NOTE - the subauths are in native machine-endian format. They
297 * are converted to little-endian when linearized onto the wire.
299 sid_append_rid(sidout, (uint32)strtoul(tok, NULL, 10));
302 DEBUG(7,("string_to_sid: converted SID %s ok\n", sidstr));
307 /*****************************************************************
308 Add a rid to the end of a sid
309 *****************************************************************/
311 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
313 if (sid->num_auths < MAXSUBAUTHS) {
314 sid->sub_auths[sid->num_auths++] = rid;
320 /*****************************************************************
321 Removes the last rid from the end of a sid
322 *****************************************************************/
324 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
326 if (sid->num_auths > 0) {
328 *rid = sid->sub_auths[sid->num_auths];
334 /*****************************************************************
336 *****************************************************************/
338 void sid_copy(DOM_SID *dst, const DOM_SID *src)
342 dst->sid_rev_num = src->sid_rev_num;
343 dst->num_auths = src->num_auths;
345 memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
347 for (i = 0; i < src->num_auths; i++)
348 dst->sub_auths[i] = src->sub_auths[i];
351 /*****************************************************************
352 Duplicates a sid - mallocs the target.
353 *****************************************************************/
355 DOM_SID *sid_dup(DOM_SID *src)
362 if((dst = malloc(sizeof(DOM_SID))) != NULL) {
363 memset(dst, '\0', sizeof(DOM_SID));
370 /*****************************************************************
371 Write a sid out into on-the-wire format.
372 *****************************************************************/
374 BOOL sid_linearize(char *outbuf, size_t len, DOM_SID *sid)
378 if(len < sid_size(sid))
381 SCVAL(outbuf,0,sid->sid_rev_num);
382 SCVAL(outbuf,1,sid->num_auths);
383 memcpy(&outbuf[2], sid->id_auth, 6);
384 for(i = 0; i < sid->num_auths; i++)
385 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
390 /*****************************************************************
392 *****************************************************************/
394 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
398 /* compare most likely different rids, first: i.e start at end */
399 for (i = sid1->num_auths-1; i >= 0; --i)
400 if (sid1->sub_auths[i] != sid2->sub_auths[i])
403 if (sid1->num_auths != sid2->num_auths)
405 if (sid1->sid_rev_num != sid2->sid_rev_num)
408 for (i = 0; i < 6; i++)
409 if (sid1->id_auth[i] != sid2->id_auth[i])
416 /*****************************************************************
417 Calculates size of a sid.
418 *****************************************************************/
420 size_t sid_size(DOM_SID *sid)
425 return sid->num_auths * sizeof(uint32) + 8;