2 Unix SMB/CIFS implementation.
3 Copyright (C) Andrew Tridgell 1992-2001
4 Copyright (C) Andrew Bartlett 2002
5 Copyright (C) Rafal Szczesniak 2002
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.
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.
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.
22 /* the Samba secrets database stores any generated, private information
23 such as the local SID and machine trust password */
28 #define DBGC_CLASS DBGC_PASSDB
30 static TDB_CONTEXT
*tdb
;
32 /* open up the secrets database */
33 BOOL
secrets_init(void)
40 pstrcpy(fname
, lp_private_dir());
41 pstrcat(fname
,"/secrets.tdb");
43 tdb
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
46 DEBUG(0,("Failed to open %s\n", fname
));
52 /* read a entry from the secrets database - the caller must free the result
53 if size is non-null then the size of the entry is put in there
55 void *secrets_fetch(const char *key
, size_t *size
)
62 kbuf
.dsize
= strlen(key
);
63 dbuf
= tdb_fetch(tdb
, kbuf
);
69 /* store a secrets entry
71 BOOL
secrets_store(const char *key
, void *data
, size_t size
)
78 kbuf
.dsize
= strlen(key
);
81 return tdb_store(tdb
, kbuf
, dbuf
, TDB_REPLACE
) == 0;
85 /* delete a secets database entry
87 BOOL
secrets_delete(const char *key
)
94 kbuf
.dsize
= strlen(key
);
95 return tdb_delete(tdb
, kbuf
) == 0;
98 BOOL
secrets_store_domain_sid(char *domain
, DOM_SID
*sid
)
102 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_SID
, domain
);
104 return secrets_store(key
, sid
, sizeof(DOM_SID
));
107 BOOL
secrets_fetch_domain_sid(char *domain
, DOM_SID
*sid
)
113 slprintf(key
, sizeof(key
)-1, "%s/%s", SECRETS_DOMAIN_SID
, domain
);
115 dyn_sid
= (DOM_SID
*)secrets_fetch(key
, &size
);
120 if (size
!= sizeof(DOM_SID
))
133 * Form a key for fetching the machine trust account password
135 * @param domain domain name
137 * @return stored password's key
139 const char *trust_keystr(const char *domain
)
141 static fstring keystr
;
143 slprintf(keystr
,sizeof(keystr
)-1,"%s/%s",
144 SECRETS_MACHINE_ACCT_PASS
, domain
);
151 * Form a key for fetching a trusted domain password
153 * @param domain trusted domain name
155 * @return stored password's key
157 char *trustdom_keystr(const char *domain
)
161 asprintf(&keystr
, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS
, domain
);
167 /************************************************************************
168 Routine to get the machine trust account password for a domain.
169 ************************************************************************/
170 BOOL
secrets_fetch_trust_account_password(char *domain
, uint8 ret_pwd
[16],
171 time_t *pass_last_set_time
)
173 struct machine_acct_pass
*pass
;
177 plaintext
= secrets_fetch_machine_password();
179 /* we have an ADS password - use that */
180 DEBUG(4,("Using ADS machine password\n"));
181 E_md4hash((uchar
*)plaintext
, ret_pwd
);
182 SAFE_FREE(plaintext
);
186 if (!(pass
= secrets_fetch(trust_keystr(domain
), &size
))) {
187 DEBUG(5, ("secrets_fetch failed!\n"));
191 if (size
!= sizeof(*pass
)) {
192 DEBUG(0, ("secrets were of incorrect size!\n"));
196 if (pass_last_set_time
) *pass_last_set_time
= pass
->mod_time
;
197 memcpy(ret_pwd
, pass
->hash
, 16);
202 /************************************************************************
203 Routine to get account password to trusted domain
204 ************************************************************************/
205 BOOL
secrets_fetch_trusted_domain_password(char *domain
, char** pwd
,
206 DOM_SID
*sid
, time_t *pass_last_set_time
)
208 struct trusted_dom_pass
*pass
;
211 /* fetching trusted domain password structure */
212 if (!(pass
= secrets_fetch(trustdom_keystr(domain
), &size
))) {
213 DEBUG(5, ("secrets_fetch failed!\n"));
217 if (size
!= sizeof(*pass
)) {
218 DEBUG(0, ("secrets were of incorrect size!\n"));
222 /* the trust's password */
224 *pwd
= strdup(pass
->pass
);
230 /* last change time */
231 if (pass_last_set_time
) *pass_last_set_time
= pass
->mod_time
;
234 memcpy(&sid
, &(pass
->domain_sid
), sizeof(sid
));
241 /************************************************************************
242 Routine to set the trust account password for a domain.
243 ************************************************************************/
244 BOOL
secrets_store_trust_account_password(char *domain
, uint8 new_pwd
[16])
246 struct machine_acct_pass pass
;
248 pass
.mod_time
= time(NULL
);
249 memcpy(pass
.hash
, new_pwd
, 16);
251 return secrets_store(trust_keystr(domain
), (void *)&pass
, sizeof(pass
));
255 * Routine to set the password for trusted domain
257 * @param domain remote domain name
258 * @param pwd plain text password of trust relationship
259 * @param sid remote domain sid
261 * @return true if succeeded
264 BOOL
secrets_store_trusted_domain_password(char* domain
, smb_ucs2_t
*uni_dom_name
,
265 size_t uni_name_len
, char* pwd
,
268 struct trusted_dom_pass pass
;
271 /* unicode domain name and its length */
275 strncpy_w(pass
.uni_name
, uni_dom_name
, sizeof(pass
.uni_name
) - 1);
276 pass
.uni_name_len
= uni_name_len
;
278 /* last change time */
279 pass
.mod_time
= time(NULL
);
281 /* password of the trust */
282 pass
.pass_len
= strlen(pwd
);
283 fstrcpy(pass
.pass
, pwd
);
286 memcpy(&(pass
.domain_sid
), &sid
, sizeof(sid
));
288 return secrets_store(trustdom_keystr(domain
), (void *)&pass
, sizeof(pass
));
291 /************************************************************************
292 Routine to set the plaintext machine account password for a realm
293 the password is assumed to be a null terminated ascii string
294 ************************************************************************/
295 BOOL
secrets_store_machine_password(char *pass
)
299 asprintf(&key
, "%s/%s", SECRETS_MACHINE_PASSWORD
, lp_workgroup());
301 ret
= secrets_store(key
, pass
, strlen(pass
)+1);
307 /************************************************************************
308 Routine to fetch the plaintext machine account password for a realm
309 the password is assumed to be a null terminated ascii string
310 ************************************************************************/
311 char *secrets_fetch_machine_password(void)
315 asprintf(&key
, "%s/%s", SECRETS_MACHINE_PASSWORD
, lp_workgroup());
317 ret
= (char *)secrets_fetch(key
, NULL
);
324 /************************************************************************
325 Routine to delete the machine trust account password file for a domain.
326 ************************************************************************/
328 BOOL
trust_password_delete(const char *domain
)
330 return secrets_delete(trust_keystr(domain
));
333 /************************************************************************
334 Routine to delete the password for trusted domain
335 ************************************************************************/
336 BOOL
trusted_domain_password_delete(const char *domain
)
338 return secrets_delete(trustdom_keystr(domain
));
342 /*******************************************************************
343 Reset the 'done' variables so after a client process is created
344 from a fork call these calls will be re-done. This should be
345 expanded if more variables need reseting.
346 ******************************************************************/
348 void reset_globals_after_fork(void)
355 * Increment the global seed value to ensure every smbd starts
356 * with a new random seed.
360 uint32 initial_val
= sys_getpid();
361 tdb_change_int32_atomic(tdb
, "INFO/random_seed", (int *)&initial_val
, 1);
362 set_rand_reseed_data((unsigned char *)&initial_val
, sizeof(initial_val
));
366 * Re-seed the random crypto generator, so all smbd's
367 * started from the same parent won't generate the same
370 generate_random_buffer( &dummy
, 1, True
);
373 BOOL
secrets_store_ldap_pw(const char* dn
, char* pw
)
378 if (asprintf(&key
, "%s/%s", SECRETS_LDAP_BIND_PW
, dn
) < 0) {
379 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
383 ret
= secrets_store(key
, pw
, strlen(pw
)+1);
391 * The linked list is allocated on the supplied talloc context, caller gets to destory
394 * @param ctx Allocation context
395 * @param enum_ctx Starting index, eg. we can start fetching at third
396 * or sixth trusted domain entry. Zero is the first index.
397 * Value it is set to is the enum context for the next enumeration.
398 * @param num_domains Number of domain entries to fetch at one call
399 * @param domains Pointer to array of trusted domain structs to be filled up
401 * @return nt status code of rpc response
404 NTSTATUS
secrets_get_trusted_domains(TALLOC_CTX
* ctx
, int* enum_ctx
, int max_num_domains
, int *num_domains
, TRUSTDOM
***domains
)
406 TDB_LIST_NODE
*keys
, *k
;
407 TRUSTDOM
*dom
= NULL
;
412 struct trusted_dom_pass
*pass
;
418 start_idx
= *enum_ctx
;
420 /* generate searching pattern */
421 if (!(pattern
= talloc_asprintf(ctx
, "%s/*", SECRETS_DOMTRUST_ACCT_PASS
))) {
422 DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n"));
423 return NT_STATUS_NO_MEMORY
;
426 DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n",
427 max_num_domains
, *enum_ctx
));
429 *domains
= talloc_zero(ctx
, sizeof(**domains
)*max_num_domains
);
431 /* fetching trusted domains' data and collecting them in a list */
432 keys
= tdb_search_keys(tdb
, pattern
);
435 * if there's no keys returned ie. no trusted domain,
436 * return "no more entries" code
438 status
= NT_STATUS_NO_MORE_ENTRIES
;
440 /* searching for keys in sectrets db -- way to go ... */
441 for (k
= keys
; k
; k
= k
->next
) {
444 /* important: ensure null-termination of the key string */
445 secrets_key
= strndup(k
->node_key
.dptr
, k
->node_key
.dsize
);
447 DEBUG(0, ("strndup failed!\n"));
448 return NT_STATUS_NO_MEMORY
;
451 pass
= secrets_fetch(secrets_key
, &size
);
453 if (size
!= sizeof(*pass
)) {
454 DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key
));
459 SAFE_FREE(secrets_key
);
461 if (idx
>= start_idx
&& idx
< start_idx
+ max_num_domains
) {
462 dom
= talloc_zero(ctx
, sizeof(*dom
));
464 /* free returned tdb record */
467 return NT_STATUS_NO_MEMORY
;
470 /* copy domain sid */
471 SMB_ASSERT(sizeof(dom
->sid
) == sizeof(pass
->domain_sid
));
472 memcpy(&(dom
->sid
), &(pass
->domain_sid
), sizeof(dom
->sid
));
474 /* copy unicode domain name */
475 dom
->name
= talloc_strdup_w(ctx
, pass
->uni_name
);
477 (*domains
)[idx
- start_idx
] = dom
;
482 /* set proper status code to return */
484 /* there are yet some entries to enumerate */
485 status
= STATUS_MORE_ENTRIES
;
487 /* this is the last entry in the whole enumeration */
488 status
= NT_STATUS_OK
;
494 /* free returned tdb record */
498 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains
));
500 /* free the results of searching the keys */
501 tdb_search_list_free(keys
);