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 3 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, see <http://www.gnu.org/licenses/>.
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 struct 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 ****************************************************************************/
39 static bool read_sid_from_file(const char *fname
, struct dom_sid
*sid
)
45 lines
= file_lines_load(fname
, &numlines
,0, NULL
);
47 if (!lines
|| numlines
< 1) {
52 ret
= string_to_sid(sid
, lines
[0]);
58 generate a random sid - used to build our own sid if we don't have one
60 static void generate_random_sid(struct dom_sid
*sid
)
63 uchar raw_sid_data
[12];
70 sid
->sub_auths
[sid
->num_auths
++] = 21;
72 generate_random_buffer(raw_sid_data
, 12);
73 for (i
= 0; i
< 3; i
++)
74 sid
->sub_auths
[sid
->num_auths
++] = IVAL(raw_sid_data
, i
*4);
77 /****************************************************************************
78 Generate the global machine sid.
79 ****************************************************************************/
81 static struct dom_sid
*pdb_generate_sam_sid(void)
83 struct dom_sid domain_sid
;
85 struct dom_sid
*sam_sid
;
87 if(!(sam_sid
=SMB_MALLOC_P(struct dom_sid
)))
91 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid
)) {
92 sid_copy(sam_sid
, &domain_sid
);
97 if (secrets_fetch_domain_sid(global_myname(), sam_sid
)) {
99 /* We got our sid. If not a pdc/bdc, we're done. */
103 if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid
)) {
105 /* No domain sid and we're a pdc/bdc. Store it */
107 if (!secrets_store_domain_sid(lp_workgroup(), sam_sid
)) {
108 DEBUG(0,("pdb_generate_sam_sid: Can't store domain SID as a pdc/bdc.\n"));
115 if (!sid_equal(&domain_sid
, sam_sid
)) {
117 /* Domain name sid doesn't match global sam sid. Re-store domain sid as 'local' sid. */
119 DEBUG(0,("pdb_generate_sam_sid: Mismatched SIDs as a pdc/bdc.\n"));
120 if (!secrets_store_domain_sid(global_myname(), &domain_sid
)) {
121 DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID for local sid as PDC/BDC.\n"));
131 /* check for an old MACHINE.SID file for backwards compatibility */
132 if (asprintf(&fname
, "%s/MACHINE.SID", lp_private_dir()) == -1) {
137 if (read_sid_from_file(fname
, sam_sid
)) {
138 /* remember it for future reference and unlink the old MACHINE.SID */
139 if (!secrets_store_domain_sid(global_myname(), sam_sid
)) {
140 DEBUG(0,("pdb_generate_sam_sid: Failed to store SID from file.\n"));
147 if (!secrets_store_domain_sid(lp_workgroup(), sam_sid
)) {
148 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(sam_sid
);
166 if (!secrets_store_domain_sid(global_myname(), sam_sid
)) {
167 DEBUG(0,("pdb_generate_sam_sid: Failed to store generated machine SID.\n"));
172 if (!secrets_store_domain_sid(lp_workgroup(), sam_sid
)) {
173 DEBUG(0,("pdb_generate_sam_sid: Failed to store generated domain SID.\n"));
182 /* return our global_sam_sid */
183 struct dom_sid
*get_global_sam_sid(void)
185 struct db_context
*db
;
187 if (global_sam_sid
!= NULL
)
188 return global_sam_sid
;
191 * memory for global_sam_sid is allocated in
192 * pdb_generate_sam_sid() as needed
194 * Note: this is garded by a transaction
195 * to prevent races on startup which
196 * can happen with some dbwrap backends
199 db
= secrets_db_ctx();
201 smb_panic("could not open secrets db");
204 if (db
->transaction_start(db
) != 0) {
205 smb_panic("could not start transaction on secrets db");
208 if (!(global_sam_sid
= pdb_generate_sam_sid())) {
209 db
->transaction_cancel(db
);
210 smb_panic("could not generate a machine SID");
213 if (db
->transaction_commit(db
) != 0) {
214 smb_panic("could not start commit secrets db");
217 return global_sam_sid
;
221 * Force get_global_sam_sid to requery the backends
223 void reset_global_sam_sid(void)
225 SAFE_FREE(global_sam_sid
);
228 /*****************************************************************
229 Check if the SID is our domain SID (S-1-5-21-x-y-z).
230 *****************************************************************/
232 bool sid_check_is_domain(const struct dom_sid
*sid
)
234 return sid_equal(sid
, get_global_sam_sid());
237 /*****************************************************************
238 Check if the SID is our domain SID (S-1-5-21-x-y-z).
239 *****************************************************************/
241 bool sid_check_is_in_our_domain(const struct dom_sid
*sid
)
243 struct dom_sid dom_sid
;
246 sid_copy(&dom_sid
, sid
);
247 sid_split_rid(&dom_sid
, &rid
);
248 return sid_check_is_domain(&dom_sid
);