got rid of USE_TDB_MMAP_FLAG as its not needed any more
[kai/samba.git] / source3 / passdb / secrets.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0.
4    Samba registry functions
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* the Samba secrets database stores any generated, private information
23    such as the local SID and machine trust password */
24
25 #include "includes.h"
26
27 static TDB_CONTEXT *tdb;
28
29 /* open up the secrets database */
30 BOOL secrets_init(void)
31 {
32         pstring fname;
33
34         if (tdb) 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) return False;
55         kbuf.dptr = key;
56         kbuf.dsize = strlen(key);
57         dbuf = tdb_fetch(tdb, kbuf);
58         if (size) *size = dbuf.dsize;
59         return dbuf.dptr;
60 }
61
62 /* store a secrets entry 
63  */
64 BOOL secrets_store(char *key, void *data, size_t size)
65 {
66         TDB_DATA kbuf, dbuf;
67         if (!tdb) return False;
68         kbuf.dptr = key;
69         kbuf.dsize = strlen(key);
70         dbuf.dptr = data;
71         dbuf.dsize = size;
72         return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
73 }
74
75
76 /* delete a secets database entry
77  */
78 BOOL secrets_delete(char *key)
79 {
80         TDB_DATA kbuf;
81         if (!tdb) return False;
82         kbuf.dptr = key;
83         kbuf.dsize = strlen(key);
84         return tdb_delete(tdb, kbuf) == 0;
85 }
86
87 BOOL secrets_store_domain_sid(char *domain, DOM_SID *sid)
88 {
89         fstring key;
90
91         slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
92         return secrets_store(key, sid, sizeof(DOM_SID));
93 }
94
95 BOOL secrets_fetch_domain_sid(char *domain, DOM_SID *sid)
96 {
97         DOM_SID *dyn_sid;
98         fstring key;
99         size_t size;
100
101         slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain);
102         dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
103
104         if (dyn_sid == NULL)
105                 return False;
106
107         if (size != sizeof(DOM_SID))
108         { 
109                 free(dyn_sid);
110                 return False;
111         }
112
113         *sid = *dyn_sid;
114         free(dyn_sid);
115         return True;
116 }
117
118
119 /************************************************************************
120 form a key for fetching a domain trust password
121 ************************************************************************/
122 char *trust_keystr(char *domain)
123 {
124         static fstring keystr;
125
126         slprintf(keystr,sizeof(keystr)-1,"%s/%s", 
127                  SECRETS_MACHINE_ACCT_PASS, domain);
128
129         return keystr;
130 }
131
132 /************************************************************************
133  Routine to get the trust account password for a domain.
134  The user of this function must have locked the trust password file.
135 ************************************************************************/
136 BOOL secrets_fetch_trust_account_password(char *domain, uint8 ret_pwd[16],
137                                           time_t *pass_last_set_time)
138 {
139         struct machine_acct_pass *pass;
140         size_t size;
141
142         if (!(pass = secrets_fetch(trust_keystr(domain), &size)) || 
143             size != sizeof(*pass))
144                 return False;
145
146         if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
147         memcpy(ret_pwd, pass->hash, 16);
148         free(pass);
149         return True;
150 }
151
152
153 /************************************************************************
154  Routine to set the trust account password for a domain.
155 ************************************************************************/
156 BOOL secrets_store_trust_account_password(char *domain, uint8 new_pwd[16])
157 {
158         struct machine_acct_pass pass;
159
160         pass.mod_time = time(NULL);
161         memcpy(pass.hash, new_pwd, 16);
162
163         return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));
164 }
165
166 /************************************************************************
167  Routine to delete the trust account password file for a domain.
168 ************************************************************************/
169
170 BOOL trust_password_delete(char *domain)
171 {
172         return secrets_delete(trust_keystr(domain));
173 }
174
175 /*******************************************************************
176  Reset the 'done' variables so after a client process is created
177  from a fork call these calls will be re-done. This should be
178  expanded if more variables need reseting.
179  ******************************************************************/
180
181 void reset_globals_after_fork(void)
182 {
183         unsigned char dummy;
184
185         /*
186          * Increment the global seed value to ensure every smbd starts
187          * with a new random seed.
188          */
189
190         if (tdb) {
191                 uint32 initial_val = sys_getpid();
192                 tdb_change_int_atomic(tdb, "INFO/random_seed", (int *)&initial_val, 1);
193                 set_rand_reseed_data((unsigned char *)&initial_val, sizeof(initial_val));
194         }
195
196         /*
197          * Re-seed the random crypto generator, so all smbd's
198          * started from the same parent won't generate the same
199          * sequence.
200          */
201         generate_random_buffer( &dummy, 1, True);
202 }