2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
25 extern int DEBUGLEVEL;
27 extern pstring global_myname;
30 * This is set on startup - it defines the SID for this
31 * machine, and therefore the SAM database for which it is
35 DOM_SID global_sam_sid;
38 * This is the name associated with the SAM database for
39 * which this machine is responsible. In the case of a PDC
40 * or PDC, this name is the same as the workgroup. In the
41 * case of "security = domain" mode, this is the same as
42 * the name of the server (global_myname).
45 fstring global_sam_name;
48 * This is obtained on startup - it defines the SID for which
49 * this machine is a member. It is therefore only set, and
50 * used, in "security = domain" mode.
53 DOM_SID global_member_sid;
56 * note the lack of a "global_member_name" - this is because
57 * this is the same as "global_myworkgroup".
60 extern fstring global_myworkgroup;
61 /* fstring global_member_dom_name; */
67 DOM_SID global_sid_S_1_5_20; /* local well-known domain */
68 DOM_SID global_sid_S_1_1; /* everyone */
69 DOM_SID global_sid_S_1_3; /* Creator Owner */
70 DOM_SID global_sid_S_1_5; /* NT Authority */
72 static struct sid_name_map_info
80 { &global_sid_S_1_5_20, "BUILTIN" },
81 { &global_sid_S_1_1 , "Everyone" },
82 { &global_sid_S_1_3 , "Creator Owner" },
83 { &global_sid_S_1_5 , "NT Authority" },
84 { &global_sam_sid , global_sam_name },
85 { &global_member_sid , global_myworkgroup },
89 /****************************************************************************
90 Read the machine SID from a file.
91 ****************************************************************************/
93 static BOOL read_sid_from_file(int fd, char *sid_file)
98 memset(fline, '\0', sizeof(fline));
100 if (read(fd, fline, sizeof(fline) -1 ) < 0) {
101 DEBUG(0,("unable to read file %s. Error was %s\n",
102 sid_file, strerror(errno) ));
107 * Convert to the machine SID.
110 fline[sizeof(fline)-1] = '\0';
111 if (!string_to_sid( &global_sam_sid, fline)) {
112 DEBUG(0,("unable to generate machine SID.\n"));
116 sid_to_string(sid_str, &global_sam_sid);
117 DEBUG(5,("read_sid_from_file: sid %s\n", sid_str));
122 /****************************************************************************
123 sets up the name associated with the SAM database for which we are responsible
124 ****************************************************************************/
125 void get_sam_domain_name(void)
127 switch (lp_server_role())
129 case ROLE_DOMAIN_PDC:
130 case ROLE_DOMAIN_BDC:
132 /* we are PDC (or BDC) for a Domain */
133 fstrcpy(global_sam_name, lp_workgroup());
136 case ROLE_DOMAIN_MEMBER:
138 /* we are a "PDC", but FOR LOCAL SAM DATABASE ONLY */
139 fstrcpy(global_sam_name, global_myname);
144 /* no domain role, probably due to "security = share" */
145 memset(global_sam_name, 0, sizeof(global_sam_name));
151 /****************************************************************************
152 obtain the sid from the PDC.
153 ****************************************************************************/
154 BOOL get_member_domain_sid(void)
156 switch (lp_server_role())
158 case ROLE_DOMAIN_NONE:
160 ZERO_STRUCT(global_member_sid);
163 case ROLE_DOMAIN_PDC:
165 sid_copy(&global_member_sid, &global_sam_sid);
170 /* member or BDC, we're going for connection to PDC */
175 return get_domain_sids(NULL, &global_member_sid);
178 /****************************************************************************
179 obtain the sid from the PDC. do some verification along the way...
180 ****************************************************************************/
181 BOOL get_domain_sids(DOM_SID *sid3, DOM_SID *sid5)
185 struct cli_state cli;
190 if (sid3 == NULL && sid5 == NULL)
192 /* don't waste my time... */
196 if (!cli_connect_serverlist(&cli, lp_passwordserver()))
198 DEBUG(0,("get_member_domain_sid: unable to initialise client connection.\n"));
203 * Ok - we have an anonymous connection to the IPC$ share.
204 * Now start the NT Domain stuff :-).
218 fstrcpy(srv_name, "\\\\");
219 fstrcat(srv_name, global_myname);
222 /* open LSARPC session. */
223 res = res ? cli_nt_session_open(&cli, PIPE_LSARPC) : False;
225 /* lookup domain controller; receive a policy handle */
226 res = res ? do_lsa_open_policy(&cli, srv_name, &pol, False) : False;
230 /* send client info query, level 3. receive domain name and sid */
231 res = res ? do_lsa_query_info_pol(&cli, &pol, 3, dom3, sid3) : False;
236 /* send client info query, level 5. receive domain name and sid */
237 res = res ? do_lsa_query_info_pol(&cli, &pol, 5, dom5, sid5) : False;
240 /* close policy handle */
241 res = res ? do_lsa_close(&cli, &pol) : False;
243 /* close the session */
244 cli_nt_session_close(&cli);
251 DEBUG(2,("LSA Query Info Policy\n"));
254 sid_to_string(sid, sid3);
255 DEBUG(2,("Domain Member - Domain: %s SID: %s\n", dom3, sid));
259 sid_to_string(sid, sid5);
260 DEBUG(2,("Domain Controller - Domain: %s SID: %s\n", dom5, sid));
263 if (!strequal(dom3, global_myworkgroup) ||
264 !strequal(dom5, global_myworkgroup))
266 DEBUG(0,("get_member_domain_sid: %s is a DC for %s not %s\n",
267 cli.desthost, dom5, global_myworkgroup));
273 DEBUG(1,("lsa query info failed\n"));
279 /****************************************************************************
280 creates some useful well known sids
281 ****************************************************************************/
282 void generate_wellknown_sids(void)
284 string_to_sid(&global_sid_S_1_5_20, "S-1-5-32");
285 string_to_sid(&global_sid_S_1_1 , "S-1-1" );
286 string_to_sid(&global_sid_S_1_3 , "S-1-3" );
287 string_to_sid(&global_sid_S_1_5 , "S-1-5" );
290 /****************************************************************************
291 Generate the global machine sid. Look for the DOMAINNAME.SID file first, if
292 not found then look in smb.conf and use it to create the DOMAINNAME.SID file.
293 ****************************************************************************/
294 BOOL generate_sam_sid(char *domain_name)
300 pstring machine_sid_file;
304 uchar raw_sid_data[12];
306 pstrcpy(sid_file, lp_smb_passwd_file());
308 if (sid_file[0] == 0)
310 DEBUG(0,("cannot find smb passwd file\n"));
314 p = strrchr(sid_file, '/');
320 if (!directory_exist(sid_file, NULL)) {
321 if (dos_mkdir(sid_file, 0700) != 0) {
322 DEBUG(0,("can't create private directory %s : %s\n",
323 sid_file, strerror(errno)));
328 pstrcpy(machine_sid_file, sid_file);
329 pstrcat(machine_sid_file, "MACHINE.SID");
331 slprintf(file_name, sizeof(file_name)-1, "%s.SID", domain_name);
333 pstrcat(sid_file, file_name);
335 if (file_exist(machine_sid_file, NULL))
337 if (file_exist(machine_sid_file, NULL))
339 DEBUG(0,("both %s and %s exist when only one should, unable to continue\n",
340 machine_sid_file, sid_file));
343 if (file_rename(machine_sid_file, sid_file))
345 DEBUG(0,("could not rename %s to %s. Error was %s\n",
346 machine_sid_file, sid_file, strerror(errno)));
351 if ((fd = sys_open(sid_file, O_RDWR | O_CREAT, 0644)) == -1) {
352 DEBUG(0,("unable to open or create file %s. Error was %s\n",
353 sid_file, strerror(errno) ));
358 * Check if the file contains data.
361 if (sys_fstat( fd, &st) < 0) {
362 DEBUG(0,("unable to stat file %s. Error was %s\n",
363 sid_file, strerror(errno) ));
368 if (st.st_size > 0) {
370 * We have a valid SID - read it.
372 if (!read_sid_from_file( fd, sid_file)) {
373 DEBUG(0,("unable to read file %s. Error was %s\n",
374 sid_file, strerror(errno) ));
383 * Generate the new sid data & turn it into a string.
385 generate_random_buffer( raw_sid_data, 12, True);
387 fstrcpy( sid_string, "S-1-5-21");
388 for( i = 0; i < 3; i++) {
390 slprintf( tmp_string, sizeof(tmp_string) - 1, "-%u", IVAL(raw_sid_data, i*4));
391 fstrcat( sid_string, tmp_string);
394 fstrcat(sid_string, "\n");
397 * Ensure our new SID is valid.
400 if (!string_to_sid( &global_sam_sid, sid_string)) {
401 DEBUG(0,("unable to generate machine SID.\n"));
406 * Do an exclusive blocking lock on the file.
409 if (!do_file_lock( fd, 60, F_WRLCK)) {
410 DEBUG(0,("unable to lock file %s. Error was %s\n",
411 sid_file, strerror(errno) ));
417 * At this point we have a blocking lock on the SID
418 * file - check if in the meantime someone else wrote
419 * SID data into the file. If so - they were here first,
423 if (sys_fstat( fd, &st) < 0) {
424 DEBUG(0,("unable to stat file %s. Error was %s\n",
425 sid_file, strerror(errno) ));
430 if (st.st_size > 0) {
432 * Unlock as soon as possible to reduce
433 * contention on the exclusive lock.
435 do_file_lock( fd, 60, F_UNLCK);
438 * We have a valid SID - read it.
441 if (!read_sid_from_file( fd, sid_file)) {
442 DEBUG(0,("unable to read file %s. Error was %s\n",
443 sid_file, strerror(errno) ));
452 * The file is still empty and we have an exlusive lock on it.
453 * Write out out SID data into the file.
456 if (fchmod(fd, 0644) < 0) {
457 DEBUG(0,("unable to set correct permissions on file %s. \
458 Error was %s\n", sid_file, strerror(errno) ));
463 if (write( fd, sid_string, strlen(sid_string)) != strlen(sid_string)) {
464 DEBUG(0,("unable to write file %s. Error was %s\n",
465 sid_file, strerror(errno) ));
474 do_file_lock( fd, 60, F_UNLCK);
479 /**************************************************************************
480 turns a domain name into a SID.
482 *** side-effect: if the domain name is NULL, it is set to our domain ***
484 ***************************************************************************/
485 BOOL map_domain_name_to_sid(DOM_SID *sid, char **nt_domain)
489 if (nt_domain == NULL)
491 sid_copy(sid, &global_sam_sid);
495 if ((*nt_domain) == NULL)
497 DEBUG(5,("map_domain_name_to_sid: overriding NULL name to %s\n",
499 (*nt_domain) = strdup(global_sam_name);
500 sid_copy(sid, &global_sam_sid);
504 if ((*nt_domain)[0] == 0)
507 (*nt_domain) = strdup(global_sam_name);
508 DEBUG(5,("map_domain_name_to_sid: overriding blank name to %s\n",
510 sid_copy(sid, &global_sam_sid);
514 DEBUG(5,("map_domain_name_to_sid: %s\n", (*nt_domain)));
516 while (sid_name_map[i].name != NULL)
518 DEBUG(5,("compare: %s\n", sid_name_map[i].name));
519 if (strequal(sid_name_map[i].name, (*nt_domain)))
522 sid_copy(sid, sid_name_map[i].sid);
523 sid_to_string(sid_str, sid_name_map[i].sid);
524 DEBUG(5,("found %s\n", sid_str));
530 DEBUG(0,("map_domain_name_to_sid: mapping to %s NOT IMPLEMENTED\n",
535 /**************************************************************************
536 turns a domain SID into a name.
538 ***************************************************************************/
539 BOOL map_domain_sid_to_name(DOM_SID *sid, char *nt_domain)
543 sid_to_string(sid_str, sid);
545 DEBUG(5,("map_domain_sid_to_name: %s\n", sid_str));
547 if (nt_domain == NULL)
552 while (sid_name_map[i].sid != NULL)
554 sid_to_string(sid_str, sid_name_map[i].sid);
555 DEBUG(5,("compare: %s\n", sid_str));
556 if (sid_equal(sid_name_map[i].sid, sid))
558 fstrcpy(nt_domain, sid_name_map[i].name);
559 DEBUG(5,("found %s\n", nt_domain));
565 DEBUG(0,("map_domain_sid_to_name: mapping NOT IMPLEMENTED\n"));
570 /**************************************************************************
571 splits a name of format \DOMAIN\name or name into its two components.
572 sets the DOMAIN name to global_sam_name if it has not been specified.
573 ***************************************************************************/
574 BOOL split_domain_name(const char *fullname, char *domain, char *name)
579 if (fullname == NULL || domain == NULL || name == NULL)
584 if (fullname[0] == '\\')
588 fstrcpy(full_name, fullname);
589 p = strchr(full_name+1, '\\');
594 fstrcpy(domain, full_name);
599 fstrcpy(domain, global_sam_name);
600 fstrcpy(name, full_name);
603 DEBUG(10,("name '%s' split into domain:%s and nt name:%s'\n", fullname, domain, name));