s4-mkrelease: Update for waf.
[sfrench/samba-autobuild/.git] / source3 / passdb / machine_sid.c
1 /* 
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
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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "secrets.h"
25 #include "dbwrap.h"
26
27 /* NOTE! the global_sam_sid is the SID of our local SAM. This is only
28    equal to the domain SID when we are a DC, otherwise its our
29    workstation SID */
30 static struct dom_sid *global_sam_sid=NULL;
31
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_PASSDB
34
35 /****************************************************************************
36  Read a SID from a file. This is for compatibility with the old MACHINE.SID
37  style of SID storage
38 ****************************************************************************/
39
40 static bool read_sid_from_file(const char *fname, struct dom_sid *sid)
41 {
42         char **lines;
43         int numlines;
44         bool ret;
45
46         lines = file_lines_load(fname, &numlines,0, NULL);
47
48         if (!lines || numlines < 1) {
49                 TALLOC_FREE(lines);
50                 return False;
51         }
52
53         ret = string_to_sid(sid, lines[0]);
54         TALLOC_FREE(lines);
55         return ret;
56 }
57
58 /*
59   generate a random sid - used to build our own sid if we don't have one
60 */
61 static void generate_random_sid(struct dom_sid *sid)
62 {
63         int i;
64         uchar raw_sid_data[12];
65
66         ZERO_STRUCTP(sid);
67
68         sid->sid_rev_num = 1;
69         sid->id_auth[5] = 5;
70         sid->num_auths = 0;
71         sid->sub_auths[sid->num_auths++] = 21;
72
73         generate_random_buffer(raw_sid_data, 12);
74         for (i = 0; i < 3; i++)
75                 sid->sub_auths[sid->num_auths++] = IVAL(raw_sid_data, i*4);
76 }
77
78 /****************************************************************************
79  Generate the global machine sid.
80 ****************************************************************************/
81
82 static struct dom_sid *pdb_generate_sam_sid(void)
83 {
84         struct dom_sid domain_sid;
85         char *fname = NULL;
86         struct dom_sid *sam_sid;
87
88         if(!(sam_sid=SMB_MALLOC_P(struct dom_sid)))
89                 return NULL;
90
91         if ( IS_DC ) {
92                 if (secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
93                         sid_copy(sam_sid, &domain_sid);
94                         return sam_sid;
95                 }
96         }
97
98         if (secrets_fetch_domain_sid(global_myname(), sam_sid)) {
99
100                 /* We got our sid. If not a pdc/bdc, we're done. */
101                 if ( !IS_DC )
102                         return sam_sid;
103
104                 if (!secrets_fetch_domain_sid(lp_workgroup(), &domain_sid)) {
105
106                         /* No domain sid and we're a pdc/bdc. Store it */
107
108                         if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
109                                 DEBUG(0,("pdb_generate_sam_sid: Can't store domain SID as a pdc/bdc.\n"));
110                                 SAFE_FREE(sam_sid);
111                                 return NULL;
112                         }
113                         return sam_sid;
114                 }
115
116                 if (!sid_equal(&domain_sid, sam_sid)) {
117
118                         /* Domain name sid doesn't match global sam sid. Re-store domain sid as 'local' sid. */
119
120                         DEBUG(0,("pdb_generate_sam_sid: Mismatched SIDs as a pdc/bdc.\n"));
121                         if (!secrets_store_domain_sid(global_myname(), &domain_sid)) {
122                                 DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID for local sid as PDC/BDC.\n"));
123                                 SAFE_FREE(sam_sid);
124                                 return NULL;
125                         }
126                         return sam_sid;
127                 }
128
129                 return sam_sid;
130         }
131
132         /* check for an old MACHINE.SID file for backwards compatibility */
133         if (asprintf(&fname, "%s/MACHINE.SID", lp_private_dir()) == -1) {
134                 SAFE_FREE(sam_sid);
135                 return NULL;
136         }
137
138         if (read_sid_from_file(fname, sam_sid)) {
139                 /* remember it for future reference and unlink the old MACHINE.SID */
140                 if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
141                         DEBUG(0,("pdb_generate_sam_sid: Failed to store SID from file.\n"));
142                         SAFE_FREE(fname);
143                         SAFE_FREE(sam_sid);
144                         return NULL;
145                 }
146                 unlink(fname);
147                 if ( !IS_DC ) {
148                         if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
149                                 DEBUG(0,("pdb_generate_sam_sid: Failed to store domain SID from file.\n"));
150                                 SAFE_FREE(fname);
151                                 SAFE_FREE(sam_sid);
152                                 return NULL;
153                         }
154                 }
155
156                 /* Stored the old sid from MACHINE.SID successfully.*/
157                 SAFE_FREE(fname);
158                 return sam_sid;
159         }
160
161         SAFE_FREE(fname);
162
163         /* we don't have the SID in secrets.tdb, we will need to
164            generate one and save it */
165         generate_random_sid(sam_sid);
166
167         if (!secrets_store_domain_sid(global_myname(), sam_sid)) {
168                 DEBUG(0,("pdb_generate_sam_sid: Failed to store generated machine SID.\n"));
169                 SAFE_FREE(sam_sid);
170                 return NULL;
171         }
172         if ( IS_DC ) {
173                 if (!secrets_store_domain_sid(lp_workgroup(), sam_sid)) {
174                         DEBUG(0,("pdb_generate_sam_sid: Failed to store generated domain SID.\n"));
175                         SAFE_FREE(sam_sid);
176                         return NULL;
177                 }
178         }
179
180         return sam_sid;
181 }   
182
183 /* return our global_sam_sid */
184 struct dom_sid *get_global_sam_sid(void)
185 {
186         struct db_context *db;
187
188         if (global_sam_sid != NULL)
189                 return global_sam_sid;
190
191         /*
192          * memory for global_sam_sid is allocated in
193          * pdb_generate_sam_sid() as needed
194          *
195          * Note: this is garded by a transaction
196          *       to prevent races on startup which
197          *       can happen with some dbwrap backends
198          */
199
200         db = secrets_db_ctx();
201         if (!db) {
202                 smb_panic("could not open secrets db");
203         }
204
205         if (db->transaction_start(db) != 0) {
206                 smb_panic("could not start transaction on secrets db");
207         }
208
209         if (!(global_sam_sid = pdb_generate_sam_sid())) {
210                 db->transaction_cancel(db);
211                 smb_panic("could not generate a machine SID");
212         }
213
214         if (db->transaction_commit(db) != 0) {
215                 smb_panic("could not start commit secrets db");
216         }
217
218         return global_sam_sid;
219 }
220
221 /** 
222  * Force get_global_sam_sid to requery the backends 
223  */
224 void reset_global_sam_sid(void) 
225 {
226         SAFE_FREE(global_sam_sid);
227 }
228
229 /*****************************************************************
230  Check if the SID is our domain SID (S-1-5-21-x-y-z).
231 *****************************************************************/  
232
233 bool sid_check_is_domain(const struct dom_sid *sid)
234 {
235         return sid_equal(sid, get_global_sam_sid());
236 }
237
238 /*****************************************************************
239  Check if the SID is our domain SID (S-1-5-21-x-y-z).
240 *****************************************************************/  
241
242 bool sid_check_is_in_our_domain(const struct dom_sid *sid)
243 {
244         struct dom_sid dom_sid;
245         uint32 rid;
246
247         sid_copy(&dom_sid, sid);
248         sid_split_rid(&dom_sid, &rid);
249         return sid_check_is_domain(&dom_sid);
250 }