Move the code from lib/util_sid.c that deals with the global_sam_sid into
[kai/samba.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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /* NOTE! the global_sam_sid is the SID of our local SAM. This is only
26    equal to the domain SID when we are a DC, otherwise its our
27    workstation SID */
28 extern 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_Builtin;                             /* Local well-known domain */
37 DOM_SID global_sid_World_Domain;                /* Everyone domain */
38 DOM_SID global_sid_World;                               /* Everyone */
39 DOM_SID global_sid_Creator_Owner_Domain;    /* Creator Owner domain */
40 DOM_SID global_sid_Creator_Owner;               /* Creator Owner */
41 DOM_SID global_sid_Creator_Group;              /* Creator Group */
42 DOM_SID global_sid_NT_Authority;                /* NT Authority */
43 DOM_SID global_sid_NULL;                        /* NULL sid */
44 DOM_SID global_sid_Builtin_Guests;                      /* Builtin guest users */
45 DOM_SID global_sid_Authenticated_Users;         /* All authenticated rids */
46 DOM_SID global_sid_Network;                                     /* Network rids */
47 DOM_SID global_sid_Anonymous;                           /* Anonymous login */
48
49 const DOM_SID *global_sid_everyone = &global_sid_World;
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 /****************************************************************************
63  Creates some useful well known sids
64 ****************************************************************************/
65
66 void generate_wellknown_sids(void)
67 {
68         string_to_sid(&global_sid_Builtin, "S-1-5-32");
69         string_to_sid(&global_sid_Builtin_Guests, "S-1-5-32-546");
70         string_to_sid(&global_sid_World_Domain, "S-1-1");
71         string_to_sid(&global_sid_World, "S-1-1-0");
72         string_to_sid(&global_sid_Creator_Owner_Domain, "S-1-3");
73         string_to_sid(&global_sid_Creator_Owner, "S-1-3-0");
74         string_to_sid(&global_sid_Creator_Group, "S-1-3-1");
75         string_to_sid(&global_sid_NT_Authority, "S-1-5");
76         string_to_sid(&global_sid_NULL, "S-1-0-0");
77         string_to_sid(&global_sid_Authenticated_Users, "S-1-5-11");
78         string_to_sid(&global_sid_Network, "S-1-5-2");
79         string_to_sid(&global_sid_Anonymous, "S-1-5-7");
80
81         /* Create the anon token. */
82         sid_copy( &anonymous_token.user_sids[0], &global_sid_World);
83         sid_copy( &anonymous_token.user_sids[1], &global_sid_Network);
84         sid_copy( &anonymous_token.user_sids[2], &global_sid_Anonymous);
85 }
86
87 /**************************************************************************
88  Splits a name of format \DOMAIN\name or name into its two components.
89  Sets the DOMAIN name to global_myname if it has not been specified.
90 ***************************************************************************/
91
92 void split_domain_name(const char *fullname, char *domain, char *name)
93 {
94         pstring full_name;
95         char *p, *sep;
96
97         sep = lp_winbind_separator();
98
99         *domain = *name = '\0';
100
101         if (fullname[0] == sep[0] || fullname[0] == '\\')
102                 fullname++;
103
104         pstrcpy(full_name, fullname);
105         p = strchr_m(full_name+1, '\\');
106         if (!p) p = strchr_m(full_name+1, sep[0]);
107
108         if (p != NULL) {
109                 *p = 0;
110                 fstrcpy(domain, full_name);
111                 fstrcpy(name, p+1);
112         } else {
113                 fstrcpy(domain, global_myname);
114                 fstrcpy(name, full_name);
115         }
116
117         DEBUG(10,("split_domain_name:name '%s' split into domain :'%s' and user :'%s'\n",
118                         fullname, domain, name));
119 }
120
121 /*****************************************************************
122  Convert a SID to an ascii string.
123 *****************************************************************/
124
125 char *sid_to_string(fstring sidstr_out, DOM_SID *sid)
126 {
127   char subauth[16];
128   int i;
129   uint32 ia;
130   
131   if (!sid) {
132           fstrcpy(sidstr_out, "(NULL SID)");
133           return sidstr_out;
134   }
135
136   /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
137   ia = (sid->id_auth[5]) +
138           (sid->id_auth[4] << 8 ) +
139           (sid->id_auth[3] << 16) +
140           (sid->id_auth[2] << 24);
141
142   slprintf(sidstr_out, sizeof(fstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
143
144   for (i = 0; i < sid->num_auths; i++) {
145     slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
146     fstrcat(sidstr_out, subauth);
147   }
148
149   return sidstr_out;
150 }
151
152 /*
153   useful function for debug lines
154 */
155 const char *sid_string_static(DOM_SID *sid)
156 {
157         static fstring sid_str;
158         sid_to_string(sid_str, sid);
159         return sid_str;
160 }
161
162 /*****************************************************************
163  Convert a string to a SID. Returns True on success, False on fail.
164 *****************************************************************/  
165    
166 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
167 {
168   pstring tok;
169   char *p, *q;
170   /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
171   uint32 ia;
172   
173   if (StrnCaseCmp( sidstr, "S-", 2)) {
174     DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
175     return False;
176   }
177
178   memset((char *)sidout, '\0', sizeof(DOM_SID));
179
180   q = p = strdup(sidstr + 2);
181   if (p == NULL) {
182     DEBUG(0, ("string_to_sid: out of memory!\n"));
183     return False;
184   }
185
186   if (!next_token(&p, tok, "-", sizeof(tok))) {
187     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
188     SAFE_FREE(q);
189     return False;
190   }
191
192   /* Get the revision number. */
193   sidout->sid_rev_num = (uint8)strtoul(tok, NULL, 10);
194
195   if (!next_token(&p, tok, "-", sizeof(tok))) {
196     DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
197     SAFE_FREE(q);
198     return False;
199   }
200
201   /* identauth in decimal should be <  2^32 */
202   ia = (uint32)strtoul(tok, NULL, 10);
203
204   /* NOTE - the ia value is in big-endian format. */
205   sidout->id_auth[0] = 0;
206   sidout->id_auth[1] = 0;
207   sidout->id_auth[2] = (ia & 0xff000000) >> 24;
208   sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
209   sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
210   sidout->id_auth[5] = (ia & 0x000000ff);
211
212   sidout->num_auths = 0;
213
214   while(next_token(&p, tok, "-", sizeof(tok)) && 
215         sidout->num_auths < MAXSUBAUTHS) {
216     /* 
217      * NOTE - the subauths are in native machine-endian format. They
218      * are converted to little-endian when linearized onto the wire.
219      */
220         sid_append_rid(sidout, (uint32)strtoul(tok, NULL, 10));
221   }
222
223   SAFE_FREE(q);
224   return True;
225 }
226
227 /*****************************************************************
228  Add a rid to the end of a sid
229 *****************************************************************/  
230
231 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
232 {
233         if (sid->num_auths < MAXSUBAUTHS) {
234                 sid->sub_auths[sid->num_auths++] = rid;
235                 return True;
236         }
237         return False;
238 }
239
240 /*****************************************************************
241  Removes the last rid from the end of a sid
242 *****************************************************************/  
243
244 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
245 {
246         if (sid->num_auths > 0) {
247                 sid->num_auths--;
248                 *rid = sid->sub_auths[sid->num_auths];
249                 return True;
250         }
251         return False;
252 }
253
254 /*****************************************************************
255  Return the last rid from the end of a sid
256 *****************************************************************/  
257
258 BOOL sid_peek_rid(DOM_SID *sid, uint32 *rid)
259 {
260         if (sid->num_auths > 0) {
261                 *rid = sid->sub_auths[sid->num_auths - 1];
262                 return True;
263         }
264         return False;
265 }
266
267 /*****************************************************************
268  Copies a sid
269 *****************************************************************/  
270
271 void sid_copy(DOM_SID *dst, const DOM_SID *src)
272 {
273         int i;
274
275         memset((char *)dst, '\0', sizeof(DOM_SID));
276
277         dst->sid_rev_num = src->sid_rev_num;
278         dst->num_auths = src->num_auths;
279
280         memcpy(&dst->id_auth[0], &src->id_auth[0], sizeof(src->id_auth));
281
282         for (i = 0; i < src->num_auths; i++)
283                 dst->sub_auths[i] = src->sub_auths[i];
284 }
285
286 /*****************************************************************
287  Duplicates a sid - mallocs the target.
288 *****************************************************************/
289
290 DOM_SID *sid_dup(DOM_SID *src)
291 {
292   DOM_SID *dst;
293
294   if(!src)
295     return NULL;
296
297   if((dst = malloc(sizeof(DOM_SID))) != NULL) {
298         memset(dst, '\0', sizeof(DOM_SID));
299         sid_copy( dst, src);
300   }
301
302   return dst;
303 }
304
305 /*****************************************************************
306  Write a sid out into on-the-wire format.
307 *****************************************************************/  
308 BOOL sid_linearize(char *outbuf, size_t len, DOM_SID *sid)
309 {
310         size_t i;
311
312         if (len < sid_size(sid))
313                 return False;
314
315         SCVAL(outbuf,0,sid->sid_rev_num);
316         SCVAL(outbuf,1,sid->num_auths);
317         memcpy(&outbuf[2], sid->id_auth, 6);
318         for(i = 0; i < sid->num_auths; i++)
319                 SIVAL(outbuf, 8 + (i*4), sid->sub_auths[i]);
320
321         return True;
322 }
323
324 /*****************************************************************
325  parse a on-the-wire SID to a DOM_SID
326 *****************************************************************/  
327 BOOL sid_parse(char *inbuf, size_t len, DOM_SID *sid)
328 {
329         int i;
330         if (len < 8) return False;
331         sid->sid_rev_num = CVAL(inbuf, 0);
332         sid->num_auths = CVAL(inbuf, 1);
333         memcpy(sid->id_auth, inbuf+2, 6);
334         if (len < 8 + sid->num_auths*4) return False;
335         for (i=0;i<sid->num_auths;i++) {
336                 sid->sub_auths[i] = IVAL(inbuf, 8+i*4);
337         }
338         return True;
339 }
340
341
342 /*****************************************************************
343  Compare the auth portion of two sids.
344 *****************************************************************/  
345 int sid_compare_auth(const DOM_SID *sid1, const DOM_SID *sid2)
346 {
347         int i;
348
349         if (sid1 == sid2) return 0;
350         if (!sid1) return -1;
351         if (!sid2) return 1;
352
353         if (sid1->sid_rev_num != sid2->sid_rev_num)
354                 return sid1->sid_rev_num - sid2->sid_rev_num;
355
356         for (i = 0; i < 6; i++)
357                 if (sid1->id_auth[i] != sid2->id_auth[i])
358                         return sid1->id_auth[i] - sid2->id_auth[i];
359
360         return 0;
361 }
362
363 /*****************************************************************
364  Compare two sids.
365 *****************************************************************/  
366 int sid_compare(const DOM_SID *sid1, const DOM_SID *sid2)
367 {
368         int i;
369
370         if (sid1 == sid2) return 0;
371         if (!sid1) return -1;
372         if (!sid2) return 1;
373
374         /* compare most likely different rids, first: i.e start at end */
375         if (sid1->num_auths != sid2->num_auths)
376                 return sid1->num_auths - sid2->num_auths;
377
378         for (i = sid1->num_auths-1; i >= 0; --i)
379                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
380                         return sid1->sub_auths[i] - sid2->sub_auths[i];
381
382         return sid_compare_auth(sid1, sid2);
383 }
384
385 /*****************************************************************
386 see if 2 SIDs are in the same domain
387 this just compares the leading sub-auths
388 *****************************************************************/  
389 int sid_compare_domain(const DOM_SID *sid1, const DOM_SID *sid2)
390 {
391         int n, i;
392
393         n = MIN(sid1->num_auths, sid2->num_auths);
394
395         for (i = n-1; i >= 0; --i)
396                 if (sid1->sub_auths[i] != sid2->sub_auths[i])
397                         return sid1->sub_auths[i] - sid2->sub_auths[i];
398
399         return sid_compare_auth(sid1, sid2);
400 }
401
402 /*****************************************************************
403  Compare two sids.
404 *****************************************************************/  
405 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
406 {
407         return sid_compare(sid1, sid2) == 0;
408 }
409
410
411
412 /*****************************************************************
413  Check if the SID is the builtin SID (S-1-5-32).
414 *****************************************************************/  
415 BOOL sid_check_is_builtin(const DOM_SID *sid)
416 {
417         return sid_equal(sid, &global_sid_Builtin);
418 }
419
420
421 /*****************************************************************
422  Check if the SID is our domain SID (S-1-5-21-x-y-z).
423 *****************************************************************/  
424 BOOL sid_check_is_in_builtin(const DOM_SID *sid)
425 {
426         DOM_SID dom_sid;
427         uint32 rid;
428
429         sid_copy(&dom_sid, sid);
430         sid_split_rid(&dom_sid, &rid);
431         
432         return sid_equal(&dom_sid, &global_sid_Builtin);
433 }
434
435
436 /*****************************************************************
437  Calculates size of a sid.
438 *****************************************************************/  
439
440 size_t sid_size(DOM_SID *sid)
441 {
442         if (sid == NULL)
443                 return 0;
444
445         return sid->num_auths * sizeof(uint32) + 8;
446 }
447
448 /*****************************************************************
449  Returns true if SID is internal (and non-mappable).
450 *****************************************************************/
451
452 BOOL non_mappable_sid(DOM_SID *sid)
453 {
454         DOM_SID dom;
455         uint32 rid;
456
457         sid_copy(&dom, sid);
458         sid_split_rid(&dom, &rid);
459
460         if (sid_equal(&dom, &global_sid_Builtin))
461                 return True;
462
463         if (sid_equal(&dom, &global_sid_Creator_Owner_Domain))
464                 return True;
465  
466         if (sid_equal(&dom, &global_sid_NT_Authority))
467                 return True;
468
469         return False;
470 }
471
472 /*
473   return the binary string representation of a DOM_SID
474   caller must free
475 */
476 char *sid_binstring(DOM_SID *sid)
477 {
478         char *buf, *s;
479         int len = sid_size(sid);
480         buf = malloc(len);
481         if (!buf) return NULL;
482         sid_linearize(buf, len, sid);
483         s = binary_string(buf, len);
484         free(buf);
485         return s;
486 }
487