fixed nttrans.c
[kai/samba.git] / source3 / lib / util_sid.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
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
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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25
26
27 extern int DEBUGLEVEL;
28 DOM_SID global_sam_sid;
29 extern pstring global_myname;
30 extern fstring global_myworkgroup;
31
32 /*
33  * Some useful sids
34  */
35
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 */
42
43 typedef struct _known_sid_users {
44         uint32 rid;
45         uint8 sid_name_use;
46         char *known_user_name;
47 } known_sid_users;
48
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"},
60                                                                                            {0, 0, NULL}};
61
62 static struct sid_name_map_info
63 {
64         DOM_SID *sid;
65         char *name;
66         known_sid_users *known_users;
67 }
68 sid_name_map[] =
69 {
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] },
76         { NULL, NULL, NULL}
77 };
78
79 /****************************************************************************
80  Creates some useful well known sids
81 ****************************************************************************/
82
83 void generate_wellknown_sids(void)
84 {
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");
91 }
92
93 /**************************************************************************
94  Turns a domain SID into a name, returned in the nt_domain argument.
95 ***************************************************************************/
96
97 BOOL map_domain_sid_to_name(DOM_SID *sid, char *nt_domain)
98 {
99         fstring sid_str;
100         int i = 0;
101         sid_to_string(sid_str, sid);
102
103         DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str));
104
105         if (nt_domain == NULL)
106                 return False;
107
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));
114                         return True;
115                 }
116                 i++;
117         }
118
119         DEBUG(5,("map_domain_sid_to_name: mapping for %s not found\n", sid_str));
120
121     return False;
122 }
123
124 /**************************************************************************
125  Looks up a known username from one of the known domains.
126 ***************************************************************************/
127
128 BOOL lookup_known_rid(DOM_SID *sid, uint32 rid, char *name, uint8 *psid_name_use)
129 {
130         int i = 0;
131         struct sid_name_map_info *psnm;
132
133         for(i = 0; sid_name_map[i].sid != NULL; i++) {
134                 psnm = &sid_name_map[i];
135                 if(sid_equal(psnm->sid, sid)) {
136                         int j;
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;
143                                         return True;
144                                 }
145                         }
146                 }
147         }
148
149         return False;
150 }
151
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 ***************************************************************************/
156
157 BOOL map_domain_name_to_sid(DOM_SID *sid, char *nt_domain)
158 {
159         int i = 0;
160
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);
164                 return True;
165         }
166
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);
171                 return True;
172     }
173
174         DEBUG(5,("map_domain_name_to_sid: %s\n", nt_domain));
175
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)) {
179                         fstring sid_str;
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));
183                         return True;
184                 }
185                 i++;
186         }
187
188         DEBUG(0,("map_domain_name_to_sid: mapping to %s not found.\n", nt_domain));
189         return False;
190 }
191
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 ***************************************************************************/
196
197 void split_domain_name(const char *fullname, char *domain, char *name)
198 {
199         pstring full_name;
200         char *p;
201
202         *domain = *name = '\0';
203
204         if (fullname[0] == '\\')
205                 fullname++;
206
207         pstrcpy(full_name, fullname);
208         p = strchr(full_name+1, '\\');
209
210         if (p != NULL) {
211                 *p = 0;
212                 fstrcpy(domain, full_name);
213                 fstrcpy(name, p+1);
214         } else {
215                 fstrcpy(domain, global_myname);
216                 fstrcpy(name, full_name);
217         }
218
219         DEBUG(10,("split_domain_name:name '%s' split into domain :'%s' and user :'%s'\n",
220                         fullname, domain, name));
221 }
222
223 /*****************************************************************
224  Convert a SID to an ascii string.
225 *****************************************************************/
226
227 char *sid_to_string(fstring sidstr_out, DOM_SID *sid)
228 {
229   char subauth[16];
230   int i;
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);
236
237   slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
238
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);
242   }
243
244   DEBUG(7,("sid_to_string returning %s\n", sidstr_out));
245   return sidstr_out;
246 }
247
248 /*****************************************************************
249  Convert a string to a SID. Returns True on success, False on fail.
250 *****************************************************************/  
251    
252 BOOL string_to_sid(DOM_SID *sidout, char *sidstr)
253 {
254   pstring tok;
255   char *p = sidstr;
256   /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
257   uint32 ia;
258
259   memset((char *)sidout, '\0', sizeof(DOM_SID));
260
261   if (StrnCaseCmp( sidstr, "S-", 2)) {
262     DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
263     return False;
264   }
265
266   p += 2;
267   if (!next_token(&p, tok, "-", sizeof(tok))) {
268     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
269     return False;
270   }
271
272   /* Get the revision number. */
273   sidout->sid_rev_num = (uint8)strtoul(tok, NULL, 10);
274
275   if (!next_token(&p, tok, "-", sizeof(tok))) {
276     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
277     return False;
278   }
279
280   /* identauth in decimal should be <  2^32 */
281   ia = (uint32)strtoul(tok, NULL, 10);
282
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);
290
291   sidout->num_auths = 0;
292
293   while(next_token(&p, tok, "-", sizeof(tok)) && 
294         sidout->num_auths < MAXSUBAUTHS) {
295     /* 
296      * NOTE - the subauths are in native machine-endian format. They
297      * are converted to little-endian when linearized onto the wire.
298      */
299         sid_append_rid(sidout, (uint32)strtoul(tok, NULL, 10));
300   }
301
302   DEBUG(7,("string_to_sid: converted SID %s ok\n", sidstr));
303
304   return True;
305 }
306
307 /*****************************************************************
308  Add a rid to the end of a sid
309 *****************************************************************/  
310
311 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
312 {
313         if (sid->num_auths < MAXSUBAUTHS) {
314                 sid->sub_auths[sid->num_auths++] = rid;
315                 return True;
316         }
317         return False;
318 }
319
320 /*****************************************************************
321  Removes the last rid from the end of a sid
322 *****************************************************************/  
323
324 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
325 {
326         if (sid->num_auths > 0) {
327                 sid->num_auths--;
328                 *rid = sid->sub_auths[sid->num_auths];
329                 return True;
330         }
331         return False;
332 }
333
334 /*****************************************************************
335  Copies a sid
336 *****************************************************************/  
337
338 void sid_copy(DOM_SID *dst, const DOM_SID *src)
339 {
340         int i;
341
342         dst->sid_rev_num = src->sid_rev_num;
343         dst->num_auths = src->num_auths;
344
345         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
346
347         for (i = 0; i < src->num_auths; i++)
348                 dst->sub_auths[i] = src->sub_auths[i];
349 }
350
351 /*****************************************************************
352  Duplicates a sid - mallocs the target.
353 *****************************************************************/
354
355 DOM_SID *sid_dup(DOM_SID *src)
356 {
357   DOM_SID *dst;
358
359   if(!src)
360     return NULL;
361
362   if((dst = malloc(sizeof(DOM_SID))) != NULL) {
363         memset(dst, '\0', sizeof(DOM_SID));
364         sid_copy( dst, src);
365   }
366
367   return dst;
368 }
369
370 /*****************************************************************
371  Write a sid out into on-the-wire format.
372 *****************************************************************/  
373
374 BOOL sid_linearize(char *outbuf, size_t len, DOM_SID *sid)
375 {
376         size_t i;
377
378         if(len < sid_size(sid))
379                 return False;
380
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]);
386
387         return True;
388 }
389
390 /*****************************************************************
391  Compare two sids.
392 *****************************************************************/  
393
394 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
395 {
396         int i;
397
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])
401                         return False;
402
403         if (sid1->num_auths != sid2->num_auths)
404                 return False;
405         if (sid1->sid_rev_num != sid2->sid_rev_num)
406                 return False;
407
408         for (i = 0; i < 6; i++)
409                 if (sid1->id_auth[i] != sid2->id_auth[i])
410                         return False;
411
412         return True;
413 }
414
415
416 /*****************************************************************
417  Calculates size of a sid.
418 *****************************************************************/  
419
420 size_t sid_size(DOM_SID *sid)
421 {
422         if (sid == NULL)
423                 return 0;
424
425         return sid->num_auths * sizeof(uint32) + 8;
426 }