9002c23d1bbc9a8aeb281d9f6dc3716d2e4e39d5
[ira/wip.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         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         if (!tdb)
70                 return False;
71         kbuf.dptr = key;
72         kbuf.dsize = strlen(key);
73         dbuf.dptr = data;
74         dbuf.dsize = size;
75         return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
76 }
77
78
79 /* delete a secets database entry
80  */
81 BOOL secrets_delete(char *key)
82 {
83         TDB_DATA kbuf;
84         if (!tdb)
85                 return False;
86         kbuf.dptr = key;
87         kbuf.dsize = strlen(key);
88         return tdb_delete(tdb, kbuf) == 0;
89 }
90
91 BOOL secrets_store_domain_sid(char *domain, DOM_SID *sid)
92 {
93         fstring key;
94
95         slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
96         return secrets_store(key, sid, sizeof(DOM_SID));
97 }
98
99 BOOL secrets_fetch_domain_sid(char *domain, DOM_SID *sid)
100 {
101         DOM_SID *dyn_sid;
102         fstring key;
103         size_t size;
104
105         slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
106         dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
107
108         if (dyn_sid == NULL)
109                 return False;
110
111         if (size != sizeof(DOM_SID))
112         { 
113                 SAFE_FREE(dyn_sid);
114                 return False;
115         }
116
117         *sid = *dyn_sid;
118         SAFE_FREE(dyn_sid);
119         return True;
120 }
121
122
123 /************************************************************************
124 form a key for fetching a domain trust password
125 ************************************************************************/
126 char *trust_keystr(char *domain)
127 {
128         static fstring keystr;
129
130         slprintf(keystr,sizeof(keystr)-1,"%s/%s", 
131                  SECRETS_MACHINE_ACCT_PASS, domain);
132
133         return keystr;
134 }
135
136 /************************************************************************
137  Routine to get the trust account password for a domain.
138  The user of this function must have locked the trust password file.
139 ************************************************************************/
140 BOOL secrets_fetch_trust_account_password(char *domain, uint8 ret_pwd[16],
141                                           time_t *pass_last_set_time)
142 {
143         struct machine_acct_pass *pass;
144         char *plaintext;
145         size_t size;
146
147         plaintext = secrets_fetch_machine_password();
148         if (plaintext) {
149                 /* we have an ADS password - use that */
150                 DEBUG(4,("Using ADS machine password\n"));
151                 E_md4hash((uchar *)plaintext, ret_pwd);
152                 SAFE_FREE(plaintext);
153                 return True;
154         }
155
156         if (!(pass = secrets_fetch(trust_keystr(domain), &size)) || 
157             size != sizeof(*pass))
158                 return False;
159
160         if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
161         memcpy(ret_pwd, pass->hash, 16);
162         SAFE_FREE(pass);
163         return True;
164 }
165
166
167 /************************************************************************
168  Routine to set the trust account password for a domain.
169 ************************************************************************/
170 BOOL secrets_store_trust_account_password(char *domain, uint8 new_pwd[16])
171 {
172         struct machine_acct_pass pass;
173
174         pass.mod_time = time(NULL);
175         memcpy(pass.hash, new_pwd, 16);
176
177         return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
178 }
179
180 /************************************************************************
181  Routine to set the plaintext machine account password for a realm
182 the password is assumed to be a null terminated ascii string
183 ************************************************************************/
184 BOOL secrets_store_machine_password(char *pass)
185 {
186         return secrets_store(SECRETS_MACHINE_PASSWORD, pass, strlen(pass)+1);
187 }
188
189
190 /************************************************************************
191  Routine to fetch the plaintext machine account password for a realm
192 the password is assumed to be a null terminated ascii string
193 ************************************************************************/
194 char *secrets_fetch_machine_password(void)
195 {
196         return (char *)secrets_fetch(SECRETS_MACHINE_PASSWORD, NULL);
197 }
198
199
200
201 /************************************************************************
202  Routine to delete the trust account password file for a domain.
203 ************************************************************************/
204
205 BOOL trust_password_delete(char *domain)
206 {
207         return secrets_delete(trust_keystr(domain));
208 }
209
210 /*******************************************************************
211  Reset the 'done' variables so after a client process is created
212  from a fork call these calls will be re-done. This should be
213  expanded if more variables need reseting.
214  ******************************************************************/
215
216 void reset_globals_after_fork(void)
217 {
218         unsigned char dummy;
219
220         /*
221          * Increment the global seed value to ensure every smbd starts
222          * with a new random seed.
223          */
224
225         if (tdb) {
226                 uint32 initial_val = sys_getpid();
227                 tdb_change_int_atomic(tdb, "INFO/random_seed", (int *)&initial_val, 1);
228                 set_rand_reseed_data((unsigned char *)&initial_val, sizeof(initial_val));
229         }
230
231         /*
232          * Re-seed the random crypto generator, so all smbd's
233          * started from the same parent won't generate the same
234          * sequence.
235          */
236         generate_random_buffer( &dummy, 1, True);
237 }