This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[ira/wip.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       
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25
26 /****************************************************************************
27  Read a SID from a file. This is for compatibility with the old MACHINE.SID
28  style of SID storage
29 ****************************************************************************/
30 static BOOL read_sid_from_file(const char *fname, DOM_SID *sid)
31 {
32         char **lines;
33         int numlines;
34         BOOL ret;
35
36         lines = file_lines_load(fname, &numlines);
37         
38         if (!lines || numlines < 1) {
39                 if (lines) file_lines_free(lines);
40                 return False;
41         }
42         
43         ret = string_to_sid(sid, lines[0]);
44         file_lines_free(lines);
45         return ret;
46 }
47
48 /*
49   generate a random sid - used to build our own sid if we don't have one
50 */
51 static void generate_random_sid(DOM_SID *sid)
52 {
53         int i;
54         uchar raw_sid_data[12];
55
56         memset((char *)sid, '\0', sizeof(*sid));
57         sid->sid_rev_num = 1;
58         sid->id_auth[5] = 5;
59         sid->num_auths = 0;
60         sid->sub_auths[sid->num_auths++] = 21;
61
62         generate_random_buffer(raw_sid_data, 12, True);
63         for (i = 0; i < 3; i++)
64                 sid->sub_auths[sid->num_auths++] = IVAL(raw_sid_data, i*4);
65 }
66
67 /****************************************************************************
68  Generate the global machine sid.
69 ****************************************************************************/
70
71 BOOL pdb_generate_sam_sid(void)
72 {
73         char *fname = NULL;
74         extern pstring global_myname;
75         extern fstring global_myworkgroup;
76         BOOL is_dc = False;
77
78         generate_wellknown_sids();
79
80         switch (lp_server_role()) {
81         case ROLE_DOMAIN_PDC:
82         case ROLE_DOMAIN_BDC:
83                 is_dc = True;
84                 break;
85         default:
86                 is_dc = False;
87                 break;
88         }
89
90         if (secrets_fetch_domain_sid(global_myname, &global_sam_sid)) {
91                 DOM_SID domain_sid;
92
93                 /* We got our sid. If not a pdc/bdc, we're done. */
94                 if (!is_dc)
95                         return True;
96
97                 if (!secrets_fetch_domain_sid(global_myworkgroup, &domain_sid)) {
98
99                         /* No domain sid and we're a pdc/bdc. Store it */
100
101                         if (!secrets_store_domain_sid(global_myworkgroup, &global_sam_sid)) {
102                                 DEBUG(0,("pdb_generate_sam_sid: Can't store domain SID as a pdc/bdc.\n"));
103                                 return False;
104                         }
105                         return True;
106                 }
107
108                 if (!sid_equal(&domain_sid, &global_sam_sid)) {
109
110                         /* Domain name sid doesn't match global sam sid. Re-store global sam sid as domain sid. */
111
112                         DEBUG(0,("pdb_generate_sam_sid: Mismatched SIDs as a pdc/bdc.\n"));
113                         if (!secrets_store_domain_sid(global_myworkgroup, &global_sam_sid)) {
114                                 DEBUG(0,("pdb_generate_sam_sid: Can't re-store domain SID as a pdc/bdc.\n"));
115                                 return False;
116                         }
117                         return True;
118                 }
119
120                 return True;
121                 
122         }
123
124         /* check for an old MACHINE.SID file for backwards compatibility */
125         asprintf(&fname, "%s/MACHINE.SID", lp_private_dir());
126
127         if (read_sid_from_file(fname, &global_sam_sid)) {
128                 /* remember it for future reference and unlink the old MACHINE.SID */
129                 if (!secrets_store_domain_sid(global_myname, &global_sam_sid)) {
130                         DEBUG(0,("pdb_generate_sam_sid: Failed to store SID from file.\n"));
131                         SAFE_FREE(fname);
132                         return False;
133                 }
134                 unlink(fname);
135                 if (is_dc) {
136                         if (!secrets_store_domain_sid(global_myworkgroup, &global_sam_sid)) {
137                                 DEBUG(0,("pdb_generate_sam_sid: Failed to store domain SID from file.\n"));
138                                 SAFE_FREE(fname);
139                                 return False;
140                         }
141                 }
142
143                 /* Stored the old sid from MACHINE.SID successfully.
144                         Patch from Stefan "metze" Metzmacher <metze@metzemix.de>*/
145                 SAFE_FREE(fname);
146                 return True;
147         }
148
149         SAFE_FREE(fname);
150
151         /* we don't have the SID in secrets.tdb, we will need to
152            generate one and save it */
153         generate_random_sid(&global_sam_sid);
154
155         if (!secrets_store_domain_sid(global_myname, &global_sam_sid)) {
156                 DEBUG(0,("pdb_generate_sam_sid: Failed to store generated machine SID.\n"));
157                 return False;
158         }
159         if (is_dc) {
160                 if (!secrets_store_domain_sid(global_myworkgroup, &global_sam_sid)) {
161                         DEBUG(0,("pdb_generate_sam_sid: Failed to store generated domain SID.\n"));
162                         return False;
163                 }
164         }
165
166         return True;
167 }