📄 secrets.c
字号:
/* Unix SMB/CIFS implementation. Copyright (C) Andrew Tridgell 1992-2001 Copyright (C) Andrew Bartlett 2002 Copyright (C) Rafal Szczesniak 2002 Copyright (C) Tim Potter 2001 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*//* the Samba secrets database stores any generated, private information such as the local SID and machine trust password */#include "includes.h"#undef DBGC_CLASS#define DBGC_CLASS DBGC_PASSDBstatic TDB_CONTEXT *tdb;/* Urrrg. global.... */BOOL global_machine_password_needs_changing;/** * Use a TDB to store an incrementing random seed. * * Initialised to the current pid, the very first time Samba starts, * and incremented by one each time it is needed. * * @note Not called by systems with a working /dev/urandom. */static void get_rand_seed(int *new_seed) { *new_seed = sys_getpid(); if (tdb) { tdb_change_int32_atomic(tdb, "INFO/random_seed", new_seed, 1); }}/* open up the secrets database */BOOL secrets_init(void){ pstring fname; unsigned char dummy; if (tdb) return True; pstrcpy(fname, lp_private_dir()); pstrcat(fname,"/secrets.tdb"); tdb = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600); if (!tdb) { DEBUG(0,("Failed to open %s\n", fname)); return False; } /** * Set a reseed function for the crypto random generator * * This avoids a problem where systems without /dev/urandom * could send the same challenge to multiple clients */ set_rand_reseed_callback(get_rand_seed); /* Ensure that the reseed is done now, while we are root, etc */ generate_random_buffer(&dummy, sizeof(dummy)); return True;}/* read a entry from the secrets database - the caller must free the result if size is non-null then the size of the entry is put in there */void *secrets_fetch(const char *key, size_t *size){ TDB_DATA dbuf; secrets_init(); if (!tdb) return NULL; dbuf = tdb_fetch(tdb, string_tdb_data(key)); if (size) *size = dbuf.dsize; return dbuf.dptr;}/* store a secrets entry */BOOL secrets_store(const char *key, const void *data, size_t size){ secrets_init(); if (!tdb) return False; return tdb_store(tdb, string_tdb_data(key), make_tdb_data(data, size), TDB_REPLACE) == 0;}/* delete a secets database entry */BOOL secrets_delete(const char *key){ secrets_init(); if (!tdb) return False; return tdb_delete(tdb, string_tdb_data(key)) == 0;}BOOL secrets_store_domain_sid(const char *domain, const DOM_SID *sid){ fstring key; BOOL ret; slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain); strupper_m(key); ret = secrets_store(key, sid, sizeof(DOM_SID)); /* Force a re-query, in case we modified our domain */ if (ret) reset_global_sam_sid(); return ret;}BOOL secrets_fetch_domain_sid(const char *domain, DOM_SID *sid){ DOM_SID *dyn_sid; fstring key; size_t size = 0; slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_SID, domain); strupper_m(key); dyn_sid = (DOM_SID *)secrets_fetch(key, &size); if (dyn_sid == NULL) return False; if (size != sizeof(DOM_SID)) { SAFE_FREE(dyn_sid); return False; } *sid = *dyn_sid; SAFE_FREE(dyn_sid); return True;}BOOL secrets_store_domain_guid(const char *domain, struct uuid *guid){ fstring key; slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain); strupper_m(key); return secrets_store(key, guid, sizeof(struct uuid));}BOOL secrets_fetch_domain_guid(const char *domain, struct uuid *guid){ struct uuid *dyn_guid; fstring key; size_t size = 0; struct uuid new_guid; slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_DOMAIN_GUID, domain); strupper_m(key); dyn_guid = (struct uuid *)secrets_fetch(key, &size); if ((!dyn_guid) && (lp_server_role() == ROLE_DOMAIN_PDC)) { smb_uuid_generate_random(&new_guid); if (!secrets_store_domain_guid(domain, &new_guid)) return False; dyn_guid = (struct uuid *)secrets_fetch(key, &size); if (dyn_guid == NULL) return False; } if (size != sizeof(struct uuid)) { DEBUG(1,("UUID size %d is wrong!\n", (int)size)); SAFE_FREE(dyn_guid); return False; } *guid = *dyn_guid; SAFE_FREE(dyn_guid); return True;}/** * Form a key for fetching the machine trust account password * * @param domain domain name * * @return stored password's key **/const char *trust_keystr(const char *domain){ static fstring keystr; slprintf(keystr,sizeof(keystr)-1,"%s/%s", SECRETS_MACHINE_ACCT_PASS, domain); strupper_m(keystr); return keystr;}/** * Form a key for fetching a trusted domain password * * @param domain trusted domain name * * @return stored password's key **/static char *trustdom_keystr(const char *domain){ static pstring keystr; pstr_sprintf(keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain); strupper_m(keystr); return keystr;}/************************************************************************ Lock the trust password entry.************************************************************************/BOOL secrets_lock_trust_account_password(const char *domain, BOOL dolock){ if (!tdb) return False; if (dolock) return (tdb_lock_bystring(tdb, trust_keystr(domain),0) == 0); else tdb_unlock_bystring(tdb, trust_keystr(domain)); return True;}/************************************************************************ Routine to get the default secure channel type for trust accounts************************************************************************/uint32 get_default_sec_channel(void) { if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) { return SEC_CHAN_BDC; } else { return SEC_CHAN_WKSTA; }}/************************************************************************ Routine to get the trust account password for a domain. The user of this function must have locked the trust password file using the above secrets_lock_trust_account_password().************************************************************************/BOOL secrets_fetch_trust_account_password(const char *domain, uint8 ret_pwd[16], time_t *pass_last_set_time, uint32 *channel){ struct machine_acct_pass *pass; char *plaintext; size_t size = 0; plaintext = secrets_fetch_machine_password(domain, pass_last_set_time, channel); if (plaintext) { DEBUG(4,("Using cleartext machine password\n")); E_md4hash(plaintext, ret_pwd); SAFE_FREE(plaintext); return True; } if (!(pass = secrets_fetch(trust_keystr(domain), &size))) { DEBUG(5, ("secrets_fetch failed!\n")); return False; } if (size != sizeof(*pass)) { DEBUG(0, ("secrets were of incorrect size!\n")); return False; } if (pass_last_set_time) { *pass_last_set_time = pass->mod_time; } memcpy(ret_pwd, pass->hash, 16); if (channel) { *channel = get_default_sec_channel(); } /* Test if machine password has expired and needs to be changed */ if (lp_machine_password_timeout()) { if (pass->mod_time > 0 && time(NULL) > (pass->mod_time + lp_machine_password_timeout())) { global_machine_password_needs_changing = True; } } SAFE_FREE(pass); return True;}/************************************************************************ Routine to get account password to trusted domain************************************************************************/BOOL secrets_fetch_trusted_domain_password(const char *domain, char** pwd, DOM_SID *sid, time_t *pass_last_set_time){ struct trusted_dom_pass pass; size_t size = 0; /* unpacking structures */ char* pass_buf; int pass_len = 0; ZERO_STRUCT(pass); /* fetching trusted domain password structure */ if (!(pass_buf = secrets_fetch(trustdom_keystr(domain), &size))) { DEBUG(5, ("secrets_fetch failed!\n")); return False; } /* unpack trusted domain password */ pass_len = tdb_trusted_dom_pass_unpack(pass_buf, size, &pass); SAFE_FREE(pass_buf); if (pass_len != size) { DEBUG(5, ("Invalid secrets size. Unpacked data doesn't match trusted_dom_pass structure.\n")); return False; } /* the trust's password */ if (pwd) { *pwd = SMB_STRDUP(pass.pass); if (!*pwd) { return False; } } /* last change time */ if (pass_last_set_time) *pass_last_set_time = pass.mod_time; /* domain sid */ if (sid != NULL) sid_copy(sid, &pass.domain_sid); return True;}/************************************************************************ Routine to set the trust account password for a domain.************************************************************************/BOOL secrets_store_trust_account_password(const char *domain, uint8 new_pwd[16]){ struct machine_acct_pass pass; pass.mod_time = time(NULL); memcpy(pass.hash, new_pwd, 16); return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));}/** * Routine to store the password for trusted domain * * @param domain remote domain name
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -