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