2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Jeremy Allison 1996-2002
5 Copyright (C) Andrew Tridgell 2002
6 Copyright (C) Gerald (Jerry) Carter 2000
7 Copyright (C) Stefan (metze) Metzmacher 2002
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.
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.
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.
26 /* NOTE! the global_sam_sid is the SID of our local SAM. This is only
27 equal to the domain SID when we are a DC, otherwise its our
29 static DOM_SID *global_sam_sid=NULL;
32 #define DBGC_CLASS DBGC_PASSDB
34 /****************************************************************************
35 Read a SID from a file. This is for compatibility with the old MACHINE.SID
37 ****************************************************************************/
38 static BOOL read_sid_from_file(const char *fname, DOM_SID *sid)
44 lines = file_lines_load(fname, &numlines);
46 if (!lines || numlines < 1) {
47 if (lines) file_lines_free(lines);
51 ret = string_to_sid(sid, lines[0]);
52 file_lines_free(lines);
57 generate a random sid - used to build our own sid if we don't have one
59 static void generate_random_sid(DOM_SID *sid)
62 uchar raw_sid_data[12];
64 memset((char *)sid, '\0', sizeof(*sid));
68 sid->sub_auths[sid->num_auths++] = 21;
70 generate_random_buffer(raw_sid_data, 12, True);
71 for (i = 0; i < 3; i++)
72 sid->sub_auths[sid->num_auths++] = IVAL(raw_sid_data, i*4);
75 /****************************************************************************
76 Generate the global machine sid.
77 ****************************************************************************/
79 static BOOL pdb_generate_sam_sid(void)
82 extern pstring global_myname;
83 extern fstring global_myworkgroup;
86 if(global_sam_sid==NULL)
87 if(!(global_sam_sid=(DOM_SID *)malloc(sizeof(DOM_SID))))
90 generate_wellknown_sids();
92 switch (lp_server_role()) {
102 if (secrets_fetch_domain_sid(global_myname, global_sam_sid)) {
105 /* We got our sid. If not a pdc/bdc, we're done. */
109 if (!secrets_fetch_domain_sid(global_myworkgroup, &domain_sid)) {
111 /* No domain sid and we're a pdc/bdc. Store it */
113 if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
114 DEBUG(0,("pdb_generate_sam_sid: Can't store domain SID as a pdc/bdc.\n"));
120 if (!sid_equal(&domain_sid, global_sam_sid)) {
122 /* Domain name sid doesn't match global sam sid. Re-store global sam sid as domain sid. */
124 DEBUG(0,("pdb_generate_sam_sid: Mismatched SIDs as a pdc/bdc.\n"));
125 if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
126 DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID as a pdc/bdc.\n"));
136 /* check for an old MACHINE.SID file for backwards compatibility */
137 asprintf(&fname, "%s/MACHINE.SID", lp_private_dir());
139 if (read_sid_from_file(fname, global_sam_sid)) {
140 /* remember it for future reference and unlink the old MACHINE.SID */
141 if (!secrets_store_domain_sid(global_myname, global_sam_sid)) {
142 DEBUG(0,("pdb_generate_sam_sid: Failed to store SID from file.\n"));
148 if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
149 DEBUG(0,("pdb_generate_sam_sid: Failed to store domain SID from file.\n"));
155 /* Stored the old sid from MACHINE.SID successfully.*/
162 /* we don't have the SID in secrets.tdb, we will need to
163 generate one and save it */
164 generate_random_sid(global_sam_sid);
166 if (!secrets_store_domain_sid(global_myname, global_sam_sid)) {
167 DEBUG(0,("pdb_generate_sam_sid: Failed to store generated machine SID.\n"));
171 if (!secrets_store_domain_sid(global_myworkgroup, global_sam_sid)) {
172 DEBUG(0,("pdb_generate_sam_sid: Failed to store generated domain SID.\n"));
180 /* return our global_sam_sid */
181 DOM_SID *get_global_sam_sid(void)
183 if (global_sam_sid != NULL)
184 return global_sam_sid;
186 /* memory for global_sam_sid is allocated in
187 pdb_generate_sam_sid() is needed*/
189 if (!pdb_generate_sam_sid())
192 return global_sam_sid;