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