sync'ing up for 3.0alpha20 release
[tprouty/samba.git] / source / lib / util_sid.c
1 /* 
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
7    Copyright (C) Stefan (metze) Metzmacher      2002
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 extern pstring global_myname;
27 extern fstring global_myworkgroup;
28
29 /*
30  * Some useful sids
31  */
32
33 DOM_SID global_sid_World_Domain;                /* Everyone domain */
34 DOM_SID global_sid_World;                               /* Everyone */
35 DOM_SID global_sid_Creator_Owner_Domain;    /* Creator Owner domain */
36 DOM_SID global_sid_NT_Authority;                /* NT Authority */
37 DOM_SID global_sid_NULL;                        /* NULL sid */
38 DOM_SID global_sid_Authenticated_Users;         /* All authenticated rids */
39 DOM_SID global_sid_Network;                                     /* Network rids */
40
41 static DOM_SID global_sid_Creator_Owner;                /* Creator Owner */
42 static DOM_SID global_sid_Creator_Group;              /* Creator Group */
43 static DOM_SID global_sid_Anonymous;                            /* Anonymous login */
44
45 DOM_SID global_sid_Builtin;                             /* Local well-known domain */
46 DOM_SID global_sid_Builtin_Administrators;
47 DOM_SID global_sid_Builtin_Users;
48 DOM_SID global_sid_Builtin_Guests;                      /* Builtin guest users */
49
50 /*
51  * An NT compatible anonymous token.
52  */
53
54 static DOM_SID anon_sid_array[3];
55
56 NT_USER_TOKEN anonymous_token = {
57     3,
58     anon_sid_array
59 };
60
61 /****************************************************************************
62  Lookup string names for SID types.
63 ****************************************************************************/
64
65 const static struct {
66         enum SID_NAME_USE sid_type;
67         char *string;
68 } sid_name_type[] = {
69         {SID_NAME_USER, "user"},
70         {SID_NAME_DOM_GRP, "domain group"},
71         {SID_NAME_DOMAIN, "domain"},
72         {SID_NAME_ALIAS, "local group"},
73         {SID_NAME_WKN_GRP, "well-known group"},
74         {SID_NAME_DELETED, "deleted account"},
75         {SID_NAME_INVALID, "invalid account"},
76         {SID_NAME_UNKNOWN, "UNKNOWN"},
77
78         {SID_NAME_USE_NONE, NULL}
79 };
80
81 const char *sid_type_lookup(uint32 sid_type) 
82 {
83         int i = 0;
84
85         /* Look through list */
86         while(sid_name_type[i].sid_type != 0) {
87                 if (sid_name_type[i].sid_type == sid_type)
88                         return sid_name_type[i].string;
89                 i++;
90         }
91
92         /* Default return */
93         return "SID *TYPE* is INVALID";
94         
95 }
96
97
98 /****************************************************************************
99  Creates some useful well known sids
100 ****************************************************************************/
101
102 void generate_wellknown_sids(void)
103 {
104         string_to_sid(&global_sid_Builtin, "S-1-5-32");
105         string_to_sid(&global_sid_Builtin_Administrators, "S-1-5-32-544");
106         string_to_sid(&global_sid_Builtin_Users, "S-1-5-32-545");
107         string_to_sid(&global_sid_Builtin_Guests, "S-1-5-32-546");
108         string_to_sid(&global_sid_World_Domain, "S-1-1");
109         string_to_sid(&global_sid_World, "S-1-1-0");
110         string_to_sid(&global_sid_Creator_Owner_Domain, "S-1-3");
111         string_to_sid(&global_sid_Creator_Owner, "S-1-3-0");
112         string_to_sid(&global_sid_Creator_Group, "S-1-3-1");
113         string_to_sid(&global_sid_NT_Authority, "S-1-5");
114         string_to_sid(&global_sid_NULL, "S-1-0-0");
115         string_to_sid(&global_sid_Authenticated_Users, "S-1-5-11");
116         string_to_sid(&global_sid_Network, "S-1-5-2");
117         string_to_sid(&global_sid_Anonymous, "S-1-5-7");
118
119         /* Create the anon token. */
120         sid_copy( &anonymous_token.user_sids[0], &global_sid_World);
121         sid_copy( &anonymous_token.user_sids[1], &global_sid_Network);
122         sid_copy( &anonymous_token.user_sids[2], &global_sid_Anonymous);
123 }
124
125 /**************************************************************************
126  Splits a name of format \DOMAIN\name or name into its two components.
127  Sets the DOMAIN name to global_myname if it has not been specified.
128 ***************************************************************************/
129
130 void split_domain_name(const char *fullname, char *domain, char *name)
131 {
132         pstring full_name;
133         char *p, *sep;
134
135         sep = lp_winbind_separator();
136
137         *domain = *name = '\0';
138
139         if (fullname[0] == sep[0] || fullname[0] == '\\')
140                 fullname++;
141
142         pstrcpy(full_name, fullname);
143         p = strchr_m(full_name+1, '\\');
144         if (!p) p = strchr_m(full_name+1, sep[0]);
145
146         if (p != NULL) {
147                 *p = 0;
148                 fstrcpy(domain, full_name);
149                 fstrcpy(name, p+1);
150         } else {
151                 fstrcpy(domain, global_myname);
152                 fstrcpy(name, full_name);
153         }
154
155         DEBUG(10,("split_domain_name:name '%s' split into domain :'%s' and user :'%s'\n",
156                         fullname, domain, name));
157 }
158
159 /*****************************************************************
160  Convert a SID to an ascii string.
161 *****************************************************************/
162
163 char *sid_to_string(fstring sidstr_out, const DOM_SID *sid)
164 {
165   char subauth[16];
166   int i;
167   uint32 ia;
168   
169   if (!sid) {
170           fstrcpy(sidstr_out, "(NULL SID)");
171           return sidstr_out;
172   }
173
174   /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
175   ia = (sid->id_auth[5]) +
176           (sid->id_auth[4] << 8 ) +
177           (sid->id_auth[3] << 16) +
178           (sid->id_auth[2] << 24);
179
180   slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
181
182   for (i = 0; i < sid->num_auths; i++) {
183     slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
184     fstrcat(sidstr_out, subauth);
185   }
186
187   return sidstr_out;
188 }
189
190 /*
191   useful function for debug lines
192 */
193 const char *sid_string_static(const DOM_SID *sid)
194 {
195         static fstring sid_str;
196         sid_to_string(sid_str, sid);
197         return sid_str;
198 }
199
200 /*****************************************************************
201  Convert a string to a SID. Returns True on success, False on fail.
202 *****************************************************************/  
203    
204 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
205 {
206   pstring tok;
207   char *p, *q;
208   /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
209   uint32 ia;
210   
211   if (StrnCaseCmp( sidstr, "S-", 2)) {
212     DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
213     return False;
214   }
215
216   memset((char *)sidout, '\0', sizeof(DOM_SID));
217
218   q = p = strdup(sidstr + 2);
219   if (p == NULL) {
220     DEBUG(0, ("string_to_sid: out of memory!\n"));
221     return False;
222   }
223
224   if (!next_token(&p, tok, "-", sizeof(tok))) {
225     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
226     SAFE_FREE(q);
227     return False;
228   }
229
230   /* Get the revision number. */
231   sidout->sid_rev_num = (uint8)strtoul(tok, NULL, 10);
232
233   if (!next_token(&p, tok, "-", sizeof(tok))) {
234     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
235     SAFE_FREE(q);
236     return False;
237   }
238
239   /* identauth in decimal should be <  2^32 */
240   ia = (uint32)strtoul(tok, NULL, 10);
241
242   /* NOTE - the ia value is in big-endian format. */
243   sidout->id_auth[0] = 0;
244   sidout->id_auth[1] = 0;
245   sidout->id_auth[2] = (ia & 0xff000000) >> 24;
246   sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
247   sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
248   sidout->id_auth[5] = (ia & 0x000000ff);
249
250   sidout->num_auths = 0;
251
252   while(next_token(&p, tok, "-", sizeof(tok)) && 
253         sidout->num_auths < MAXSUBAUTHS) {
254     /* 
255      * NOTE - the subauths are in native machine-endian format. They
256      * are converted to little-endian when linearized onto the wire.
257      */
258         sid_append_rid(sidout, (uint32)strtoul(tok, NULL, 10));
259   }
260
261   SAFE_FREE(q);
262   return True;
263 }
264
265 /*****************************************************************
266  Add a rid to the end of a sid
267 *****************************************************************/  
268
269 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
270 {
271         if (sid->num_auths < MAXSUBAUTHS) {
272                 sid->sub_auths[sid->num_auths++] = rid;
273                 return True;
274         }
275         return False;
276 }
277
278 /*****************************************************************
279  Removes the last rid from the end of a sid
280 *****************************************************************/  
281
282 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
283 {
284         if (sid->num_auths > 0) {
285                 sid->num_auths--;
286                 *rid = sid->sub_auths[sid->num_auths];
287                 return True;
288         }
289         return False;
290 }
291
292 /*****************************************************************
293  Return the last rid from the end of a sid
294 *****************************************************************/  
295
296 BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
297 {
298         if (!sid || !rid)
299                 return False;           
300         
301         if (sid->num_auths > 0) {
302                 *rid = sid->sub_auths[sid->num_auths - 1];
303                 return True;
304         }
305         return False;
306 }
307
308 /*****************************************************************
309  Return the last rid from the end of a sid
310  and check the sid against the exp_dom_sid  
311 *****************************************************************/  
312
313 BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
314 {
315         if (!exp_dom_sid || !sid || !rid)
316                 return False;
317                         
318
319         if (sid_compare_domain(exp_dom_sid, sid)!=0){
320                 *rid=(-1);
321                 return False;
322         }
323         
324         return sid_peek_rid(sid, rid);
325 }
326
327 /*****************************************************************
328  Copies a sid
329 *****************************************************************/  
330
331 void sid_copy(DOM_SID *dst, const DOM_SID *src)
332 {
333         int i;
334
335         ZERO_STRUCTP(dst);
336
337         dst->sid_rev_num = src->sid_rev_num;
338         dst->num_auths = src->num_auths;
339
340         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
341
342         for (i = 0; i < src->num_auths; i++)
343                 dst->sub_auths[i] = src->sub_auths[i];
344 }
345
346
347 /*****************************************************************
348  Write a sid out into on-the-wire format.
349 *****************************************************************/  
350 BOOL sid_linearize(char *outbuf, size_t len, DOM_SID *sid)
351 {
352         size_t i;
353
354         if (len < sid_size(sid))
355                 return False;
356
357         SCVAL(outbuf,0,sid->sid_rev_num);
358         SCVAL(outbuf,1,sid->num_auths);
359         memcpy(&outbuf[2], sid->id_auth, 6);
360         for(i = 0; i < sid->num_auths; i++)
361                 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
362
363         return True;
364 }
365
366 /*****************************************************************
367  parse a on-the-wire SID to a DOM_SID
368 *****************************************************************/  
369 BOOL sid_parse(char *inbuf, size_t len, DOM_SID *sid)
370 {
371         int i;
372         if (len < 8) return False;
373
374         ZERO_STRUCTP(sid);
375
376         sid->sid_rev_num = CVAL(inbuf, 0);
377         sid->num_auths = CVAL(inbuf, 1);
378         memcpy(sid->id_auth, inbuf+2, 6);
379         if (len < 8 + sid->num_auths*4) return False;
380         for (i=0;i<sid->num_auths;i++) {
381                 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
382         }
383         return True;
384 }
385
386
387 /*****************************************************************
388  Compare the auth portion of two sids.
389 *****************************************************************/  
390 static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
391 {
392         int i;
393
394         if (sid1 == sid2) return 0;
395         if (!sid1) return -1;
396         if (!sid2) return 1;
397
398         if (sid1->sid_rev_num != sid2->sid_rev_num)
399                 return sid1->sid_rev_num - sid2->sid_rev_num;
400
401         for (i = 0; i < 6; i++)
402                 if (sid1->id_auth[i] != sid2->id_auth[i])
403                         return sid1->id_auth[i] - sid2->id_auth[i];
404
405         return 0;
406 }
407
408 /*****************************************************************
409  Compare two sids.
410 *****************************************************************/  
411 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
412 {
413         int i;
414
415         if (sid1 == sid2) return 0;
416         if (!sid1) return -1;
417         if (!sid2) return 1;
418
419         /* compare most likely different rids, first: i.e start at end */
420         if (sid1->num_auths != sid2->num_auths)
421                 return sid1->num_auths - sid2->num_auths;
422
423         for (i = sid1->num_auths-1; i >= 0; --i)
424                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
425                         return sid1->sub_auths[i] - sid2->sub_auths[i];
426
427         return sid_compare_auth(sid1, sid2);
428 }
429
430 /*****************************************************************
431 see if 2 SIDs are in the same domain
432 this just compares the leading sub-auths
433 *****************************************************************/  
434 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
435 {
436         int n, i;
437
438         n = MIN(sid1->num_auths, sid2->num_auths);
439
440         for (i = n-1; i >= 0; --i)
441                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
442                         return sid1->sub_auths[i] - sid2->sub_auths[i];
443
444         return sid_compare_auth(sid1, sid2);
445 }
446
447 /*****************************************************************
448  Compare two sids.
449 *****************************************************************/  
450 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
451 {
452         return sid_compare(sid1, sid2) == 0;
453 }
454
455
456
457 /*****************************************************************
458  Check if the SID is the builtin SID (S-1-5-32).
459 *****************************************************************/  
460 BOOL sid_check_is_builtin(const DOM_SID *sid)
461 {
462         return sid_equal(sid, &global_sid_Builtin);
463 }
464
465
466 /*****************************************************************
467  Check if the SID is our domain SID (S-1-5-21-x-y-z).
468 *****************************************************************/  
469 BOOL sid_check_is_in_builtin(const DOM_SID *sid)
470 {
471         DOM_SID dom_sid;
472         uint32 rid;
473
474         sid_copy(&dom_sid, sid);
475         sid_split_rid(&dom_sid, &rid);
476         
477         return sid_equal(&dom_sid, &global_sid_Builtin);
478 }
479
480
481 /*****************************************************************
482  Calculates size of a sid.
483 *****************************************************************/  
484
485 size_t sid_size(DOM_SID *sid)
486 {
487         if (sid == NULL)
488                 return 0;
489
490         return sid->num_auths * sizeof(uint32) + 8;
491 }
492
493 /*****************************************************************
494  Returns true if SID is internal (and non-mappable).
495 *****************************************************************/
496
497 BOOL non_mappable_sid(DOM_SID *sid)
498 {
499         DOM_SID dom;
500         uint32 rid;
501
502         sid_copy(&dom, sid);
503         sid_split_rid(&dom, &rid);
504
505         if (sid_equal(&dom, &global_sid_Builtin))
506                 return True;
507
508         if (sid_equal(&dom, &global_sid_Creator_Owner_Domain))
509                 return True;
510  
511         if (sid_equal(&dom, &global_sid_NT_Authority))
512                 return True;
513
514         return False;
515 }
516
517 /*
518   return the binary string representation of a DOM_SID
519   caller must free
520 */
521 char *sid_binstring(DOM_SID *sid)
522 {
523         char *buf, *s;
524         int len = sid_size(sid);
525         buf = malloc(len);
526         if (!buf) return NULL;
527         sid_linearize(buf, len, sid);
528         s = binary_string(buf, len);
529         free(buf);
530         return s;
531 }
532
533
534 /*
535   print a GUID structure for debugging
536 */
537 void print_guid(GUID *guid)
538 {
539         int i;
540
541         d_printf("%08x-%04x-%04x", 
542                  IVAL(guid->info, 0), SVAL(guid->info, 4), SVAL(guid->info, 6));
543         d_printf("-%02x%02x-", guid->info[8], guid->info[9]);
544         for (i=10;i<GUID_SIZE;i++)
545                 d_printf("%02x", guid->info[i]);
546         d_printf("\n");
547 }