default SID map now reads in "trusted domains" from smb.conf.
[ira/wip.git] / source3 / lib / util_sid.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 extern int DEBUGLEVEL;
26
27
28 /*****************************************************************
29  Convert a SID to an ascii string.
30 *****************************************************************/
31
32 char *sid_to_string(pstring sidstr_out, const DOM_SID *sid)
33 {
34   char subauth[16];
35   int i;
36   /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
37   uint32 ia = (sid->id_auth[5]) +
38               (sid->id_auth[4] << 8 ) +
39               (sid->id_auth[3] << 16) +
40               (sid->id_auth[2] << 24);
41
42   slprintf(sidstr_out, sizeof(pstring) - 1, "S-%u-%lu", (unsigned int)sid->sid_rev_num, (unsigned long)ia);
43
44   for (i = 0; i < sid->num_auths; i++)
45   {
46     slprintf(subauth, sizeof(subauth)-1, "-%lu", (unsigned long)sid->sub_auths[i]);
47     pstrcat(sidstr_out, subauth);
48   }
49
50   DEBUG(7,("sid_to_string returning %s\n", sidstr_out));
51   return sidstr_out;
52 }
53
54 /*****************************************************************
55  Convert a string to a SID. Returns True on success, False on fail.
56 *****************************************************************/  
57    
58 BOOL string_to_sid(DOM_SID *sidout, const char *sidstr)
59 {
60         const char *p = sidstr;
61         /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
62         uint32 ia;
63
64         memset((char *)sidout, '\0', sizeof(DOM_SID));
65
66         if (StrnCaseCmp( sidstr, "S-", 2))
67         {
68                 DEBUG(0,("string_to_sid: Sid %s does not start with 'S-'.\n", sidstr));
69                 return False;
70         }
71
72         if ((p = strchr(p, '-')) == NULL)
73         {
74                 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
75                 return False;
76         }
77
78         p++;
79
80         /* Get the revision number. */
81         sidout->sid_rev_num = (uint8)strtoul(p,NULL,10);
82
83         if ((p = strchr(p, '-')) == NULL)
84         {
85                 DEBUG(0,("string_to_sid: Sid %s is not in a valid format.\n", sidstr));
86                 return False;
87         }
88
89         p++;
90
91         /* identauth in decimal should be <  2^32 */
92         ia = (uint32)strtoul(p,NULL,10);
93
94         /* NOTE - the ia value is in big-endian format. */
95         sidout->id_auth[0] = 0;
96         sidout->id_auth[1] = 0;
97         sidout->id_auth[2] = (ia & 0xff000000) >> 24;
98         sidout->id_auth[3] = (ia & 0x00ff0000) >> 16;
99         sidout->id_auth[4] = (ia & 0x0000ff00) >> 8;
100         sidout->id_auth[5] = (ia & 0x000000ff);
101
102         sidout->num_auths = 0;
103
104         while (((p = strchr(p, '-')) != NULL) && sidout->num_auths < MAXSUBAUTHS)
105         {
106                 p++;
107                 /*
108                  * NOTE - the subauths are in native machine-endian format. They
109                  * are converted to little-endian when linearized onto the wire.
110                  */
111                 sid_append_rid(sidout, (uint32)strtoul(p, NULL, 10));
112         }
113
114         return True;
115 }
116
117 /*****************************************************************
118  add a rid to the end of a sid
119 *****************************************************************/  
120 BOOL sid_append_rid(DOM_SID *sid, uint32 rid)
121 {
122         if (sid->num_auths < MAXSUBAUTHS)
123         {
124                 sid->sub_auths[sid->num_auths++] = rid;
125                 return True;
126         }
127         return False;
128 }
129
130 /*****************************************************************
131  removes the last rid from the end of a sid
132 *****************************************************************/  
133 BOOL sid_split_rid(DOM_SID *sid, uint32 *rid)
134 {
135         if (sid->num_auths > 0)
136         {
137                 sid->num_auths--;
138                 if (rid != NULL)
139                 {
140                         (*rid) = sid->sub_auths[sid->num_auths];
141                 }
142                 return True;
143         }
144         return False;
145 }
146
147 /*****************************************************************
148  copies a sid
149 *****************************************************************/  
150 void sid_copy(DOM_SID *sid1, const DOM_SID *sid2)
151 {
152         int i;
153
154         for (i = 0; i < 6; i++)
155         {
156                 sid1->id_auth[i] = sid2->id_auth[i];
157         }
158
159         for (i = 0; i < sid2->num_auths; i++)
160         {
161                 sid1->sub_auths[i] = sid2->sub_auths[i];
162         }
163
164         sid1->num_auths   = sid2->num_auths;
165         sid1->sid_rev_num = sid2->sid_rev_num;
166 }
167
168 /*****************************************************************
169  compare two sids up to the auths of the first sid
170 *****************************************************************/  
171 BOOL sid_front_equal(const DOM_SID *sid1, const DOM_SID *sid2)
172 {
173         int i;
174
175         /* compare most likely different rids, first: i.e start at end */
176         for (i = sid1->num_auths-1; i >= 0; --i)
177         {
178                 if (sid1->sub_auths[i] != sid2->sub_auths[i]) return False;
179         }
180
181         if (sid1->num_auths   >  sid2->num_auths  ) return False;
182         if (sid1->sid_rev_num != sid2->sid_rev_num) return False;
183
184         for (i = 0; i < 6; i++)
185         {
186                 if (sid1->id_auth[i] != sid2->id_auth[i]) return False;
187         }
188
189         return True;
190 }
191
192 /*****************************************************************
193  compare two sids
194 *****************************************************************/  
195 BOOL sid_equal(const DOM_SID *sid1, const DOM_SID *sid2)
196 {
197         int i;
198
199         /* compare most likely different rids, first: i.e start at end */
200         for (i = sid1->num_auths-1; i >= 0; --i)
201         {
202                 if (sid1->sub_auths[i] != sid2->sub_auths[i]) return False;
203         }
204
205         if (sid1->num_auths   != sid2->num_auths  ) return False;
206         if (sid1->sid_rev_num != sid2->sid_rev_num) return False;
207
208         for (i = 0; i < 6; i++)
209         {
210                 if (sid1->id_auth[i] != sid2->id_auth[i]) return False;
211         }
212
213         return True;
214 }
215
216
217 /*****************************************************************
218  calculates size of a sid
219 *****************************************************************/  
220 int sid_size(const DOM_SID *sid)
221 {
222         if (sid == NULL)
223         {
224                 return 0;
225         }
226         return sid->num_auths * sizeof(uint32) + 8;
227 }
228
229
230 /*****************************************************************
231  Duplicates a sid - mallocs the target.
232 *****************************************************************/
233
234 DOM_SID *sid_dup(const DOM_SID *src)
235 {
236   DOM_SID *dst;
237
238   if(!src)
239     return NULL;
240
241   if((dst = (DOM_SID*)malloc(sizeof(DOM_SID))) != NULL) {
242        memset(dst, '\0', sizeof(DOM_SID));
243        sid_copy( dst, src);
244   }
245
246   return dst;
247 }
248
249
250 /****************************************************************************
251  Read a SID from a file.
252 ****************************************************************************/
253
254 static BOOL read_sid_from_file(int fd, char *sid_file, DOM_SID *sid)
255 {   
256   fstring fline;
257         fstring sid_str;
258     
259   memset(fline, '\0', sizeof(fline));
260
261   if (read(fd, fline, sizeof(fline) -1 ) < 0) {
262     DEBUG(0,("unable to read file %s. Error was %s\n",
263            sid_file, strerror(errno) ));
264     return False;
265   }
266
267   /*
268    * Convert to the machine SID.
269    */
270
271   fline[sizeof(fline)-1] = '\0';
272   if (!string_to_sid(sid, fline)) {
273     DEBUG(0,("unable to read sid.\n"));
274     return False;
275   }
276
277         sid_to_string(sid_str, sid);
278         DEBUG(5,("read_sid_from_file: sid %s\n", sid_str));
279
280   return True;
281 }
282
283 /****************************************************************************
284  Generate the global machine sid. Look for the DOMAINNAME.SID file first, if
285  not found then look in smb.conf and use it to create the DOMAINNAME.SID file.
286 ****************************************************************************/
287 BOOL read_sid(char *domain_name, DOM_SID *sid)
288 {
289         int fd;
290         char *p;
291         pstring sid_file;
292         fstring file_name;
293         SMB_STRUCT_STAT st;
294
295         pstrcpy(sid_file, lp_smb_passwd_file());
296
297         DEBUG(10,("read_sid: Domain: %s\n", domain_name));
298
299         if (sid_file[0] == 0)
300         {
301                 DEBUG(0,("cannot find smb passwd file\n"));
302                 return False;
303         }
304
305         p = strrchr(sid_file, '/');
306         if (p != NULL)
307         {
308                 *++p = '\0';
309         }
310
311         if (!directory_exist(sid_file, NULL))
312         {
313                 if (mkdir(sid_file, 0700) != 0)
314                 {
315                         DEBUG(0,("can't create private directory %s : %s\n",
316                                  sid_file, strerror(errno)));
317                         return False;
318                 }
319         }
320
321         slprintf(file_name, sizeof(file_name)-1, "%s.SID", domain_name);
322         strupper(file_name);
323         pstrcat(sid_file, file_name);
324     
325         if ((fd = sys_open(sid_file, O_RDWR | O_CREAT, 0644)) == -1) {
326                 DEBUG(0,("unable to open or create file %s. Error was %s\n",
327                          sid_file, strerror(errno) ));
328                 return False;
329         } 
330   
331         /*
332          * Check if the file contains data.
333          */
334         
335         if (sys_fstat(fd, &st) < 0) {
336                 DEBUG(0,("unable to stat file %s. Error was %s\n",
337                          sid_file, strerror(errno) ));
338                 close(fd);
339                 return False;
340         } 
341   
342         if (st.st_size == 0)
343         {
344                 close(fd);
345                 return False;
346         }
347
348         /*
349          * We have a valid SID - read it.
350          */
351
352         if (!read_sid_from_file(fd, sid_file, sid))
353         {
354                 DEBUG(0,("unable to read file %s. Error was %s\n",
355                          sid_file, strerror(errno) ));
356                 close(fd);
357                 return False;
358         }
359         close(fd);
360         return True;
361 }   
362
363
364 /****************************************************************************
365  Generate the global machine sid. Look for the DOMAINNAME.SID file first, if
366  not found then look in smb.conf and use it to create the DOMAINNAME.SID file.
367 ****************************************************************************/
368 BOOL write_sid(char *domain_name, DOM_SID *sid)
369 {
370         int fd;
371         char *p;
372         pstring sid_file;
373         fstring sid_string;
374         fstring file_name;
375         SMB_STRUCT_STAT st;
376
377         pstrcpy(sid_file, lp_smb_passwd_file());
378         sid_to_string(sid_string, sid);
379
380         DEBUG(10,("write_sid: Domain: %s SID: %s\n", domain_name, sid_string));
381         fstrcat(sid_string, "\n");
382
383         if (sid_file[0] == 0)
384         {
385                 DEBUG(0,("cannot find smb passwd file\n"));
386                 return False;
387         }
388
389         p = strrchr(sid_file, '/');
390         if (p != NULL)
391         {
392                 *++p = '\0';
393         }
394
395         if (!directory_exist(sid_file, NULL)) {
396                 if (mkdir(sid_file, 0700) != 0) {
397                         DEBUG(0,("can't create private directory %s : %s\n",
398                                  sid_file, strerror(errno)));
399                         return False;
400                 }
401         }
402
403         slprintf(file_name, sizeof(file_name)-1, "%s.SID", domain_name);
404         strupper(file_name);
405         pstrcat(sid_file, file_name);
406     
407         if ((fd = sys_open(sid_file, O_RDWR | O_CREAT, 0644)) == -1) {
408                 DEBUG(0,("unable to open or create file %s. Error was %s\n",
409                          sid_file, strerror(errno) ));
410                 return False;
411         } 
412   
413         /*
414          * Check if the file contains data.
415          */
416         
417         if (sys_fstat(fd, &st) < 0) {
418                 DEBUG(0,("unable to stat file %s. Error was %s\n",
419                          sid_file, strerror(errno) ));
420                 close(fd);
421                 return False;
422         } 
423   
424         if (st.st_size > 0)
425         {
426                 /*
427                  * We have a valid SID already.
428                  */
429                 close(fd);
430                 DEBUG(0,("SID file %s already exists\n", sid_file));
431                 return False;
432         } 
433   
434         if (!do_file_lock(fd, 60, F_WRLCK))
435         {
436                 DEBUG(0,("unable to lock file %s. Error was %s\n",
437                          sid_file, strerror(errno) ));
438                 close(fd);
439                 return False;
440         } 
441   
442         /*
443          * At this point we have a blocking lock on the SID
444          * file - check if in the meantime someone else wrote
445          * SID data into the file. If so - they were here first,
446          * use their data.
447          */
448         
449         if (sys_fstat(fd, &st) < 0)
450         {
451                 DEBUG(0,("unable to stat file %s. Error was %s\n",
452                          sid_file, strerror(errno) ));
453                 close(fd);
454                 return False;
455         } 
456   
457         if (st.st_size > 0)
458         {
459                 /*
460                  * Unlock as soon as possible to reduce
461                  * contention on the exclusive lock.
462                  */ 
463                 do_file_lock(fd, 60, F_UNLCK);
464                 
465                 /*
466                  * We have a valid SID already.
467                  */
468                 
469                 DEBUG(0,("SID file %s already exists\n", sid_file));
470                 close(fd);
471                 return False;
472         } 
473         
474         /*
475          * The file is still empty and we have an exlusive lock on it.
476          * Write out out SID data into the file.
477          */
478         
479         if (fchmod(fd, 0644) < 0)
480         {
481                 DEBUG(0,("unable to set correct permissions on file %s. \
482 Error was %s\n", sid_file, strerror(errno) ));
483                 close(fd);
484                 return False;
485         } 
486         
487         if (write(fd, sid_string, strlen(sid_string)) != strlen(sid_string))
488         {
489                 DEBUG(0,("unable to write file %s. Error was %s\n",
490                          sid_file, strerror(errno) ));
491                 close(fd);
492                 return False;
493         } 
494         
495         /*
496          * Unlock & exit.
497          */
498         
499         do_file_lock(fd, 60, F_UNLCK);
500         close(fd);
501         return True;
502 }   
503
504 /****************************************************************************
505 create a random SID.
506 ****************************************************************************/
507 BOOL create_new_sid(DOM_SID *sid)
508 {
509         uchar raw_sid_data[12];
510         fstring sid_string;
511         int i;
512
513         /*
514          * Generate the new sid data & turn it into a string.
515          */
516         generate_random_buffer(raw_sid_data, 12, True);
517                 
518         fstrcpy(sid_string, "S-1-5-21");
519         for(i = 0; i < 3; i++)
520         {
521                 fstring tmp_string;
522                 slprintf(tmp_string, sizeof(tmp_string) - 1, "-%u", IVAL(raw_sid_data, i*4));
523                 fstrcat(sid_string, tmp_string);
524         }
525         
526         fstrcat(sid_string, "\n");
527         
528         /*
529          * Ensure our new SID is valid.
530          */
531         
532         if (!string_to_sid(sid, sid_string))
533         {
534                 DEBUG(0,("unable to generate machine SID.\n"));
535                 return False;
536         } 
537
538         return True;
539 }
540