Add a couple of extra debugs for the secrets.tdb stuff
[kai/samba.git] / source3 / passdb / secrets.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0.
4    Copyright (C) Andrew Tridgell 1992-2001
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /* the Samba secrets database stores any generated, private information
22    such as the local SID and machine trust password */
23
24 #include "includes.h"
25
26 static TDB_CONTEXT *tdb;
27
28 /* open up the secrets database */
29 BOOL secrets_init(void)
30 {
31         pstring fname;
32
33         if (tdb)
34                 return True;
35
36         pstrcpy(fname, lp_private_dir());
37         pstrcat(fname,"/secrets.tdb");
38
39         tdb = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
40
41         if (!tdb) {
42                 DEBUG(0,("Failed to open %s\n", fname));
43                 return False;
44         }
45         return True;
46 }
47
48 /* read a entry from the secrets database - the caller must free the result
49    if size is non-null then the size of the entry is put in there
50  */
51 void *secrets_fetch(char *key, size_t *size)
52 {
53         TDB_DATA kbuf, dbuf;
54         secrets_init();
55         if (!tdb)
56                 return NULL;
57         kbuf.dptr = key;
58         kbuf.dsize = strlen(key);
59         dbuf = tdb_fetch(tdb, kbuf);
60         if (size)
61                 *size = dbuf.dsize;
62         return dbuf.dptr;
63 }
64
65 /* store a secrets entry 
66  */
67 BOOL secrets_store(char *key, void *data, size_t size)
68 {
69         TDB_DATA kbuf, dbuf;
70         secrets_init();
71         if (!tdb)
72                 return False;
73         kbuf.dptr = key;
74         kbuf.dsize = strlen(key);
75         dbuf.dptr = data;
76         dbuf.dsize = size;
77         return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
78 }
79
80
81 /* delete a secets database entry
82  */
83 BOOL secrets_delete(char *key)
84 {
85         TDB_DATA kbuf;
86         secrets_init();
87         if (!tdb)
88                 return False;
89         kbuf.dptr = key;
90         kbuf.dsize = strlen(key);
91         return tdb_delete(tdb, kbuf) == 0;
92 }
93
94 BOOL secrets_store_domain_sid(char *domain, DOM_SID *sid)
95 {
96         fstring key;
97
98         slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
99         return secrets_store(key, sid, sizeof(DOM_SID));
100 }
101
102 BOOL secrets_fetch_domain_sid(char *domain, DOM_SID *sid)
103 {
104         DOM_SID *dyn_sid;
105         fstring key;
106         size_t size;
107
108         slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
109         dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
110
111         if (dyn_sid == NULL)
112                 return False;
113
114         if (size != sizeof(DOM_SID))
115         { 
116                 SAFE_FREE(dyn_sid);
117                 return False;
118         }
119
120         *sid = *dyn_sid;
121         SAFE_FREE(dyn_sid);
122         return True;
123 }
124
125
126 /************************************************************************
127 form a key for fetching a domain trust password
128 ************************************************************************/
129 char *trust_keystr(char *domain)
130 {
131         static fstring keystr;
132
133         slprintf(keystr,sizeof(keystr)-1,"%s/%s", 
134                  SECRETS_MACHINE_ACCT_PASS, domain);
135
136         return keystr;
137 }
138
139 /************************************************************************
140  Routine to get the trust account password for a domain.
141 ************************************************************************/
142 BOOL secrets_fetch_trust_account_password(char *domain, uint8 ret_pwd[16],
143                                           time_t *pass_last_set_time)
144 {
145         struct machine_acct_pass *pass;
146         char *plaintext;
147         size_t size;
148
149         plaintext = secrets_fetch_machine_password();
150         if (plaintext) {
151                 /* we have an ADS password - use that */
152                 DEBUG(4,("Using ADS machine password\n"));
153                 E_md4hash((uchar *)plaintext, ret_pwd);
154                 SAFE_FREE(plaintext);
155                 return True;
156         }
157
158         if (!(pass = secrets_fetch(trust_keystr(domain), &size))) {
159                 DEBUG(5, ("secrets_fetch failed!\n"));
160                 return False;
161         }
162         
163         if (size != sizeof(*pass)) {
164                 DEBUG(0, ("secrets were of incorrect size!\n"));
165                 return False;
166         }
167
168         if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
169         memcpy(ret_pwd, pass->hash, 16);
170         SAFE_FREE(pass);
171         return True;
172 }
173
174
175 /************************************************************************
176  Routine to set the trust account password for a domain.
177 ************************************************************************/
178 BOOL secrets_store_trust_account_password(char *domain, uint8 new_pwd[16])
179 {
180         struct machine_acct_pass pass;
181
182         pass.mod_time = time(NULL);
183         memcpy(pass.hash, new_pwd, 16);
184
185         return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
186 }
187
188 /************************************************************************
189  Routine to set the plaintext machine account password for a realm
190 the password is assumed to be a null terminated ascii string
191 ************************************************************************/
192 BOOL secrets_store_machine_password(char *pass)
193 {
194         return secrets_store(SECRETS_MACHINE_PASSWORD, pass, strlen(pass)+1);
195 }
196
197
198 /************************************************************************
199  Routine to fetch the plaintext machine account password for a realm
200 the password is assumed to be a null terminated ascii string
201 ************************************************************************/
202 char *secrets_fetch_machine_password(void)
203 {
204         return (char *)secrets_fetch(SECRETS_MACHINE_PASSWORD, NULL);
205 }
206
207
208
209 /************************************************************************
210  Routine to delete the trust account password file for a domain.
211 ************************************************************************/
212
213 BOOL trust_password_delete(char *domain)
214 {
215         return secrets_delete(trust_keystr(domain));
216 }
217
218 /*******************************************************************
219  Reset the 'done' variables so after a client process is created
220  from a fork call these calls will be re-done. This should be
221  expanded if more variables need reseting.
222  ******************************************************************/
223
224 void reset_globals_after_fork(void)
225 {
226         unsigned char dummy;
227
228         secrets_init();
229
230         /*
231          * Increment the global seed value to ensure every smbd starts
232          * with a new random seed.
233          */
234
235         if (tdb) {
236                 uint32 initial_val = sys_getpid();
237                 tdb_change_int_atomic(tdb, "INFO/random_seed", (int *)&initial_val, 1);
238                 set_rand_reseed_data((unsigned char *)&initial_val, sizeof(initial_val));
239         }
240
241         /*
242          * Re-seed the random crypto generator, so all smbd's
243          * started from the same parent won't generate the same
244          * sequence.
245          */
246         generate_random_buffer( &dummy, 1, True);
247 }