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