This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[metze/samba/wip.git] / source3 / 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    Copyright (C) Simo Sorce                     2002
9       
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 /*
28  * Some useful sids
29  */
30
31 DOM_SID global_sid_World_Domain;                /* Everyone domain */
32 DOM_SID global_sid_World;                               /* Everyone */
33 DOM_SID global_sid_Creator_Owner_Domain;    /* Creator Owner domain */
34 DOM_SID global_sid_NT_Authority;                /* NT Authority */
35 DOM_SID global_sid_System;              /* System */
36 DOM_SID global_sid_NULL;                        /* NULL sid */
37 DOM_SID global_sid_Authenticated_Users;         /* All authenticated rids */
38 DOM_SID global_sid_Network;                     /* Network rids */
39
40 DOM_SID global_sid_Creator_Owner;       /* Creator Owner */
41 DOM_SID global_sid_Creator_Group;       /* Creator Group */
42 DOM_SID global_sid_Anonymous;           /* Anonymous login */
43
44 DOM_SID global_sid_Builtin;                     /* Local well-known domain */
45 DOM_SID global_sid_Builtin_Administrators;      /* Builtin administrators */
46 DOM_SID global_sid_Builtin_Users;               /* Builtin users */
47 DOM_SID global_sid_Builtin_Guests;              /* Builtin guest users */
48 DOM_SID global_sid_Builtin_Power_Users;         /* Builtin power users */
49 DOM_SID global_sid_Builtin_Account_Operators;   /* Builtin account operators */
50 DOM_SID global_sid_Builtin_Server_Operators;    /* Builtin server operators */
51 DOM_SID global_sid_Builtin_Print_Operators;     /* Builtin print operators */
52 DOM_SID global_sid_Builtin_Backup_Operators;    /* Builtin backup operators */
53 DOM_SID global_sid_Builtin_Replicator;          /* Builtin replicator */
54
55 #define SECURITY_NULL_SID_AUTHORITY    0
56 #define SECURITY_WORLD_SID_AUTHORITY   1
57 #define SECURITY_LOCAL_SID_AUTHORITY   2
58 #define SECURITY_CREATOR_SID_AUTHORITY 3
59 #define SECURITY_NT_AUTHORITY          5
60
61 /*
62  * An NT compatible anonymous token.
63  */
64
65 static DOM_SID anon_sid_array[3];
66
67 NT_USER_TOKEN anonymous_token = {
68         3,
69         anon_sid_array
70 };
71
72 static DOM_SID system_sid_array[4];
73 NT_USER_TOKEN system_token = {
74         1,
75         system_sid_array
76 };
77
78 /****************************************************************************
79  Lookup string names for SID types.
80 ****************************************************************************/
81
82 const static struct {
83         enum SID_NAME_USE sid_type;
84         char *string;
85 } sid_name_type[] = {
86         {SID_NAME_USER, "User"},
87         {SID_NAME_DOM_GRP, "Domain Group"},
88         {SID_NAME_DOMAIN, "Domain"},
89         {SID_NAME_ALIAS, "Local Group"},
90         {SID_NAME_WKN_GRP, "Well-known Group"},
91         {SID_NAME_DELETED, "Deleted Account"},
92         {SID_NAME_INVALID, "Invalid Account"},
93         {SID_NAME_UNKNOWN, "UNKNOWN"},
94
95         {SID_NAME_USE_NONE, NULL}
96 };
97
98 const char *sid_type_lookup(uint32 sid_type) 
99 {
100         int i = 0;
101
102         /* Look through list */
103         while(sid_name_type[i].sid_type != 0) {
104                 if (sid_name_type[i].sid_type == sid_type)
105                         return sid_name_type[i].string;
106                 i++;
107         }
108
109         /* Default return */
110         return "SID *TYPE* is INVALID";
111 }
112
113 /****************************************************************************
114  Creates some useful well known sids
115 ****************************************************************************/
116
117 void generate_wellknown_sids(void)
118 {
119         static BOOL initialised = False;
120
121         if (initialised) 
122                 return;
123
124         /* SECURITY_NULL_SID_AUTHORITY */
125         string_to_sid(&global_sid_NULL, "S-1-0-0");
126
127         /* SECURITY_WORLD_SID_AUTHORITY */
128         string_to_sid(&global_sid_World_Domain, "S-1-1");
129         string_to_sid(&global_sid_World, "S-1-1-0");
130
131         /* SECURITY_CREATOR_SID_AUTHORITY */
132         string_to_sid(&global_sid_Creator_Owner_Domain, "S-1-3");
133         string_to_sid(&global_sid_Creator_Owner, "S-1-3-0");
134         string_to_sid(&global_sid_Creator_Group, "S-1-3-1");
135
136         /* SECURITY_NT_AUTHORITY */
137         string_to_sid(&global_sid_NT_Authority, "S-1-5");
138         string_to_sid(&global_sid_Network, "S-1-5-2");
139         string_to_sid(&global_sid_Anonymous, "S-1-5-7");
140         string_to_sid(&global_sid_Authenticated_Users, "S-1-5-11");
141         string_to_sid(&global_sid_System, "S-1-5-18");
142
143         /* SECURITY_BUILTIN_DOMAIN_RID */
144         string_to_sid(&global_sid_Builtin, "S-1-5-32");
145         string_to_sid(&global_sid_Builtin_Administrators, "S-1-5-32-544");
146         string_to_sid(&global_sid_Builtin_Users, "S-1-5-32-545");
147         string_to_sid(&global_sid_Builtin_Guests, "S-1-5-32-546");
148         string_to_sid(&global_sid_Builtin_Power_Users, "S-1-5-32-547");
149         string_to_sid(&global_sid_Builtin_Account_Operators, "S-1-5-32-548");
150         string_to_sid(&global_sid_Builtin_Server_Operators, "S-1-5-32-549");
151         string_to_sid(&global_sid_Builtin_Print_Operators, "S-1-5-32-550");
152         string_to_sid(&global_sid_Builtin_Backup_Operators, "S-1-5-32-551");
153         string_to_sid(&global_sid_Builtin_Replicator, "S-1-5-32-552");
154
155         /* Create the anon token. */
156         sid_copy( &anonymous_token.user_sids[0], &global_sid_World);
157         sid_copy( &anonymous_token.user_sids[1], &global_sid_Network);
158         sid_copy( &anonymous_token.user_sids[2], &global_sid_Anonymous);
159
160         /* Create the system token. */
161         sid_copy( &system_token.user_sids[0], &global_sid_System);
162         
163         initialised = True;
164 }
165
166 /**************************************************************************
167  Create the SYSTEM token.
168 ***************************************************************************/
169
170 NT_USER_TOKEN *get_system_token(void) 
171 {
172         generate_wellknown_sids(); /* The token is initialised here */
173         return &system_token;
174 }
175
176 /**************************************************************************
177  Splits a name of format \DOMAIN\name or name into its two components.
178  Sets the DOMAIN name to global_myname() if it has not been specified.
179 ***************************************************************************/
180
181 void split_domain_name(const char *fullname, char *domain, char *name)
182 {
183         pstring full_name;
184         char *p, *sep;
185
186         sep = lp_winbind_separator();
187
188         *domain = *name = '\0';
189
190         if (fullname[0] == sep[0] || fullname[0] == '\\')
191                 fullname++;
192
193         pstrcpy(full_name, fullname);
194         p = strchr_m(full_name+1, '\\');
195         if (!p) p = strchr_m(full_name+1, sep[0]);
196
197         if (p != NULL) {
198                 *p = 0;
199                 fstrcpy(domain, full_name);
200                 fstrcpy(name, p+1);
201         } else {
202                 fstrcpy(domain, global_myname());
203                 fstrcpy(name, full_name);
204         }
205
206         DEBUG(10,("split_domain_name:name '%s' split into domain :'%s' and user :'%s'\n",
207                         fullname, domain, name));
208 }
209
210 /****************************************************************************
211  Test if a SID is wellknown and resolvable.
212 ****************************************************************************/
213
214 BOOL resolvable_wellknown_sid(DOM_SID *sid)
215 {
216         uint32 ia = (sid->id_auth[5]) +
217                         (sid->id_auth[4] << 8 ) +
218                         (sid->id_auth[3] << 16) +
219                         (sid->id_auth[2] << 24);
220
221         if (sid->sid_rev_num != SEC_DESC_REVISION || sid->num_auths < 1)
222                 return False;
223
224         return (ia == SECURITY_WORLD_SID_AUTHORITY ||
225                 ia == SECURITY_CREATOR_SID_AUTHORITY);
226 }
227
228 /*****************************************************************
229  Convert a SID to an ascii string.
230 *****************************************************************/
231
232 char *sid_to_string(fstring sidstr_out, const DOM_SID *sid)
233 {
234         char subauth[16];
235         int i;
236         uint32 ia;
237   
238         if (!sid) {
239                 fstrcpy(sidstr_out, "(NULL SID)");
240                 return sidstr_out;
241         }
242
243         /*
244          * BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 
245          * in a range of 2^48.
246          */
247         ia = (sid->id_auth[5]) +
248                 (sid->id_auth[4] << 8 ) +
249                 (sid->id_auth[3] << 16) +
250                 (sid->id_auth[2] << 24);
251
252         slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
253
254         for (i = 0; i < sid->num_auths; i++) {
255                 slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
256                 fstrcat(sidstr_out, subauth);
257         }
258
259         return sidstr_out;
260 }
261
262 /*****************************************************************
263  Useful function for debug lines.
264 *****************************************************************/  
265
266 const char *sid_string_static(const DOM_SID *sid)
267 {
268         static fstring sid_str;
269         sid_to_string(sid_str, sid);
270         return sid_str;
271 }
272
273 /*****************************************************************
274  Convert a string to a SID. Returns True on success, False on fail.
275 *****************************************************************/  
276    
277 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
278 {
279         pstring tok;
280         char *q;
281         const char *p;
282         /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
283         uint32 ia;
284   
285         if (StrnCaseCmp( sidstr, "S-", 2)) {
286                 DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
287                 return False;
288         }
289
290         memset((char *)sidout, '\0', sizeof(DOM_SID));
291
292         p = q = strdup(sidstr + 2);
293         if (p == NULL) {
294                 DEBUG(0, ("string_to_sid: out of memory!\n"));
295                 return False;
296         }
297
298         if (!next_token(&p, tok, "-", sizeof(tok))) {
299                 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
300                 SAFE_FREE(q);
301                 return False;
302         }
303
304         /* Get the revision number. */
305         sidout->sid_rev_num = (uint8)strtoul(tok, NULL, 10);
306
307         if (!next_token(&p, tok, "-", sizeof(tok))) {
308                 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
309                 SAFE_FREE(q);
310                 return False;
311         }
312
313         /* identauth in decimal should be <  2^32 */
314         ia = (uint32)strtoul(tok, NULL, 10);
315
316         /* NOTE - the ia value is in big-endian format. */
317         sidout->id_auth[0] = 0;
318         sidout->id_auth[1] = 0;
319         sidout->id_auth[2] = (ia & 0xff000000) >> 24;
320         sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
321         sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
322         sidout->id_auth[5] = (ia & 0x000000ff);
323
324         sidout->num_auths = 0;
325
326         while(next_token(&p, tok, "-", sizeof(tok)) && 
327                 sidout->num_auths < MAXSUBAUTHS) {
328                 /* 
329                  * NOTE - the subauths are in native machine-endian format. They
330                  * are converted to little-endian when linearized onto the wire.
331                  */
332                 sid_append_rid(sidout, (uint32)strtoul(tok, NULL, 10));
333         }
334
335         SAFE_FREE(q);
336         return True;
337 }
338
339 /*****************************************************************
340  Add a rid to the end of a sid
341 *****************************************************************/  
342
343 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
344 {
345         if (sid->num_auths < MAXSUBAUTHS) {
346                 sid->sub_auths[sid->num_auths++] = rid;
347                 return True;
348         }
349         return False;
350 }
351
352 /*****************************************************************
353  Removes the last rid from the end of a sid
354 *****************************************************************/  
355
356 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
357 {
358         if (sid->num_auths > 0) {
359                 sid->num_auths--;
360                 *rid = sid->sub_auths[sid->num_auths];
361                 return True;
362         }
363         return False;
364 }
365
366 /*****************************************************************
367  Return the last rid from the end of a sid
368 *****************************************************************/  
369
370 BOOL sid_peek_rid(const DOM_SID *sid, uint32 *rid)
371 {
372         if (!sid || !rid)
373                 return False;           
374         
375         if (sid->num_auths > 0) {
376                 *rid = sid->sub_auths[sid->num_auths - 1];
377                 return True;
378         }
379         return False;
380 }
381
382 /*****************************************************************
383  Return the last rid from the end of a sid
384  and check the sid against the exp_dom_sid  
385 *****************************************************************/  
386
387 BOOL sid_peek_check_rid(const DOM_SID *exp_dom_sid, const DOM_SID *sid, uint32 *rid)
388 {
389         if (!exp_dom_sid || !sid || !rid)
390                 return False;
391                         
392
393         if (sid_compare_domain(exp_dom_sid, sid)!=0){
394                 *rid=(-1);
395                 return False;
396         }
397         
398         return sid_peek_rid(sid, rid);
399 }
400
401 /*****************************************************************
402  Copies a sid
403 *****************************************************************/  
404
405 void sid_copy(DOM_SID *dst, const DOM_SID *src)
406 {
407         int i;
408
409         ZERO_STRUCTP(dst);
410
411         dst->sid_rev_num = src->sid_rev_num;
412         dst->num_auths = src->num_auths;
413
414         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
415
416         for (i = 0; i < src->num_auths; i++)
417                 dst->sub_auths[i] = src->sub_auths[i];
418 }
419
420 /*****************************************************************
421  Write a sid out into on-the-wire format.
422 *****************************************************************/  
423
424 BOOL sid_linearize(char *outbuf, size_t len, const DOM_SID *sid)
425 {
426         size_t i;
427
428         if (len < sid_size(sid))
429                 return False;
430
431         SCVAL(outbuf,0,sid->sid_rev_num);
432         SCVAL(outbuf,1,sid->num_auths);
433         memcpy(&outbuf[2], sid->id_auth, 6);
434         for(i = 0; i < sid->num_auths; i++)
435                 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
436
437         return True;
438 }
439
440 /*****************************************************************
441  Parse a on-the-wire SID to a DOM_SID.
442 *****************************************************************/  
443
444 BOOL sid_parse(const char *inbuf, size_t len, DOM_SID *sid)
445 {
446         int i;
447         if (len < 8)
448                 return False;
449
450         ZERO_STRUCTP(sid);
451
452         sid->sid_rev_num = CVAL(inbuf, 0);
453         sid->num_auths = CVAL(inbuf, 1);
454         memcpy(sid->id_auth, inbuf+2, 6);
455         if (len < 8 + sid->num_auths*4)
456                 return False;
457         for (i=0;i<sid->num_auths;i++)
458                 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
459         return True;
460 }
461
462 /*****************************************************************
463  Compare the auth portion of two sids.
464 *****************************************************************/  
465
466 static int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
467 {
468         int i;
469
470         if (sid1 == sid2)
471                 return 0;
472         if (!sid1)
473                 return -1;
474         if (!sid2)
475                 return 1;
476
477         if (sid1->sid_rev_num != sid2->sid_rev_num)
478                 return sid1->sid_rev_num - sid2->sid_rev_num;
479
480         for (i = 0; i < 6; i++)
481                 if (sid1->id_auth[i] != sid2->id_auth[i])
482                         return sid1->id_auth[i] - sid2->id_auth[i];
483
484         return 0;
485 }
486
487 /*****************************************************************
488  Compare two sids.
489 *****************************************************************/  
490
491 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
492 {
493         int i;
494
495         if (sid1 == sid2)
496                 return 0;
497         if (!sid1)
498                 return -1;
499         if (!sid2)
500                 return 1;
501
502         /* Compare most likely different rids, first: i.e start at end */
503         if (sid1->num_auths != sid2->num_auths)
504                 return sid1->num_auths - sid2->num_auths;
505
506         for (i = sid1->num_auths-1; i >= 0; --i)
507                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
508                         return sid1->sub_auths[i] - sid2->sub_auths[i];
509
510         return sid_compare_auth(sid1, sid2);
511 }
512
513 /*****************************************************************
514  See if 2 SIDs are in the same domain
515  this just compares the leading sub-auths
516 *****************************************************************/  
517
518 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
519 {
520         int n, i;
521
522         n = MIN(sid1->num_auths, sid2->num_auths);
523
524         for (i = n-1; i >= 0; --i)
525                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
526                         return sid1->sub_auths[i] - sid2->sub_auths[i];
527
528         return sid_compare_auth(sid1, sid2);
529 }
530
531 /*****************************************************************
532  Compare two sids.
533 *****************************************************************/  
534
535 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
536 {
537         return sid_compare(sid1, sid2) == 0;
538 }
539
540 /*****************************************************************
541  Check if the SID is the builtin SID (S-1-5-32).
542 *****************************************************************/  
543
544 BOOL sid_check_is_builtin(const DOM_SID *sid)
545 {
546         return sid_equal(sid, &global_sid_Builtin);
547 }
548
549 /*****************************************************************
550  Check if the SID is one of the builtin SIDs (S-1-5-32-a).
551 *****************************************************************/  
552
553 BOOL sid_check_is_in_builtin(const DOM_SID *sid)
554 {
555         DOM_SID dom_sid;
556         uint32 rid;
557
558         sid_copy(&dom_sid, sid);
559         sid_split_rid(&dom_sid, &rid);
560         
561         return sid_equal(&dom_sid, &global_sid_Builtin);
562 }
563
564 /*****************************************************************
565  Calculates size of a sid.
566 *****************************************************************/  
567
568 size_t sid_size(const DOM_SID *sid)
569 {
570         if (sid == NULL)
571                 return 0;
572
573         return sid->num_auths * sizeof(uint32) + 8;
574 }
575
576 /*****************************************************************
577  Returns true if SID is internal (and non-mappable).
578 *****************************************************************/
579
580 BOOL non_mappable_sid(DOM_SID *sid)
581 {
582         DOM_SID dom;
583         uint32 rid;
584
585         sid_copy(&dom, sid);
586         sid_split_rid(&dom, &rid);
587
588         if (sid_equal(&dom, &global_sid_Builtin))
589                 return True;
590
591         if (sid_equal(&dom, &global_sid_NT_Authority))
592                 return True;
593
594         return False;
595 }
596
597 /*****************************************************************
598  Return the binary string representation of a DOM_SID.
599  Caller must free.
600 *****************************************************************/
601
602 char *sid_binstring(const DOM_SID *sid)
603 {
604         char *buf, *s;
605         int len = sid_size(sid);
606         buf = malloc(len);
607         if (!buf)
608                 return NULL;
609         sid_linearize(buf, len, sid);
610         s = binary_string(buf, len);
611         free(buf);
612         return s;
613 }
614
615
616 /*****************************************************************
617  Print a GUID structure for debugging.
618 *****************************************************************/
619
620 void print_guid(GUID *guid)
621 {
622         int i;
623
624         d_printf("%08x-%04x-%04x", 
625                  IVAL(guid->info, 0), SVAL(guid->info, 4), SVAL(guid->info, 6));
626         d_printf("-%02x%02x-", guid->info[8], guid->info[9]);
627         for (i=10;i<GUID_SIZE;i++)
628                 d_printf("%02x", guid->info[i]);
629         d_printf("\n");
630 }