📄 secrets.c
字号:
* @param pwd plain text password of trust relationship * @param sid remote domain sid * * @return true if succeeded **/BOOL secrets_store_trusted_domain_password(const char* domain, smb_ucs2_t *uni_dom_name, size_t uni_name_len, const char* pwd, DOM_SID sid){ /* packing structures */ pstring pass_buf; int pass_len = 0; int pass_buf_len = sizeof(pass_buf); struct trusted_dom_pass pass; ZERO_STRUCT(pass); /* unicode domain name and its length */ if (!uni_dom_name) return False; strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1); pass.uni_name_len = uni_name_len; /* last change time */ pass.mod_time = time(NULL); /* password of the trust */ pass.pass_len = strlen(pwd); fstrcpy(pass.pass, pwd); /* domain sid */ sid_copy(&pass.domain_sid, &sid); pass_len = tdb_trusted_dom_pass_pack(pass_buf, pass_buf_len, &pass); return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len);}/************************************************************************ Routine to set the plaintext machine account password for a realmthe password is assumed to be a null terminated ascii string************************************************************************/BOOL secrets_store_machine_password(const char *pass, const char *domain, uint32 sec_channel){ char *key = NULL; BOOL ret; uint32 last_change_time; uint32 sec_channel_type; asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain); if (!key) return False; strupper_m(key); ret = secrets_store(key, pass, strlen(pass)+1); SAFE_FREE(key); if (!ret) return ret; asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain); if (!key) return False; strupper_m(key); SIVAL(&last_change_time, 0, time(NULL)); ret = secrets_store(key, &last_change_time, sizeof(last_change_time)); SAFE_FREE(key); asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain); if (!key) return False; strupper_m(key); SIVAL(&sec_channel_type, 0, sec_channel); ret = secrets_store(key, &sec_channel_type, sizeof(sec_channel_type)); SAFE_FREE(key); return ret;}/************************************************************************ Routine to fetch the plaintext machine account password for a realm the password is assumed to be a null terminated ascii string.************************************************************************/char *secrets_fetch_machine_password(const char *domain, time_t *pass_last_set_time, uint32 *channel){ char *key = NULL; char *ret; asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain); strupper_m(key); ret = (char *)secrets_fetch(key, NULL); SAFE_FREE(key); if (pass_last_set_time) { size_t size; uint32 *last_set_time; asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain); strupper_m(key); last_set_time = secrets_fetch(key, &size); if (last_set_time) { *pass_last_set_time = IVAL(last_set_time,0); SAFE_FREE(last_set_time); } else { *pass_last_set_time = 0; } SAFE_FREE(key); } if (channel) { size_t size; uint32 *channel_type; asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain); strupper_m(key); channel_type = secrets_fetch(key, &size); if (channel_type) { *channel = IVAL(channel_type,0); SAFE_FREE(channel_type); } else { *channel = get_default_sec_channel(); } SAFE_FREE(key); } return ret;}/******************************************************************* Wrapper around retrieving the trust account password*******************************************************************/ BOOL get_trust_pw(const char *domain, uint8 ret_pwd[16], uint32 *channel){ DOM_SID sid; char *pwd; time_t last_set_time; /* if we are a DC and this is not our domain, then lookup an account for the domain trust */ if ( IS_DC && !strequal(domain, lp_workgroup()) && lp_allow_trusted_domains() ) { if (!secrets_fetch_trusted_domain_password(domain, &pwd, &sid, &last_set_time)) { DEBUG(0, ("get_trust_pw: could not fetch trust " "account password for trusted domain %s\n", domain)); return False; } *channel = SEC_CHAN_DOMAIN; E_md4hash(pwd, ret_pwd); SAFE_FREE(pwd); return True; } /* Just get the account for the requested domain. In the future this * might also cover to be member of more than one domain. */ if (secrets_fetch_trust_account_password(domain, ret_pwd, &last_set_time, channel)) return True; DEBUG(5, ("get_trust_pw: could not fetch trust account " "password for domain %s\n", domain)); return False;}/************************************************************************ Routine to delete the machine trust account password file for a domain.************************************************************************/BOOL trust_password_delete(const char *domain){ return secrets_delete(trust_keystr(domain));}/************************************************************************ Routine to delete the password for trusted domain************************************************************************/BOOL trusted_domain_password_delete(const char *domain){ return secrets_delete(trustdom_keystr(domain));}BOOL secrets_store_ldap_pw(const char* dn, char* pw){ char *key = NULL; BOOL ret; if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) { DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n")); return False; } ret = secrets_store(key, pw, strlen(pw)+1); SAFE_FREE(key); return ret;}/******************************************************************* Find the ldap password.******************************************************************/BOOL fetch_ldap_pw(char **dn, char** pw){ char *key = NULL; size_t size = 0; *dn = smb_xstrdup(lp_ldap_admin_dn()); if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) { SAFE_FREE(*dn); DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n")); } *pw=secrets_fetch(key, &size); SAFE_FREE(key); if (!size) { /* Upgrade 2.2 style entry */ char *p; char* old_style_key = SMB_STRDUP(*dn); char *data; fstring old_style_pw; if (!old_style_key) { DEBUG(0, ("fetch_ldap_pw: strdup failed!\n")); return False; } for (p=old_style_key; *p; p++) if (*p == ',') *p = '/'; data=secrets_fetch(old_style_key, &size); if (!size && size < sizeof(old_style_pw)) { DEBUG(0,("fetch_ldap_pw: neither ldap secret retrieved!\n")); SAFE_FREE(old_style_key); SAFE_FREE(*dn); return False; } size = MIN(size, sizeof(fstring)-1); strncpy(old_style_pw, data, size); old_style_pw[size] = 0; SAFE_FREE(data); if (!secrets_store_ldap_pw(*dn, old_style_pw)) { DEBUG(0,("fetch_ldap_pw: ldap secret could not be upgraded!\n")); SAFE_FREE(old_style_key); SAFE_FREE(*dn); return False; } if (!secrets_delete(old_style_key)) { DEBUG(0,("fetch_ldap_pw: old ldap secret could not be deleted!\n")); } SAFE_FREE(old_style_key); *pw = smb_xstrdup(old_style_pw); } return True;}/** * Get trusted domains info from secrets.tdb. * * The linked list is allocated on the supplied talloc context, caller gets to destroy * when done. * * @param ctx Allocation context * @param enum_ctx Starting index, eg. we can start fetching at third * or sixth trusted domain entry. Zero is the first index. * Value it is set to is the enum context for the next enumeration. * @param num_domains Number of domain entries to fetch at one call * @param domains Pointer to array of trusted domain structs to be filled up * * @return nt status code of rpc response **/ NTSTATUS secrets_get_trusted_domains(TALLOC_CTX* ctx, int* enum_ctx, unsigned int max_num_domains, int *num_domains, TRUSTDOM ***domains){ TDB_LIST_NODE *keys, *k; TRUSTDOM *dom = NULL; char *pattern; unsigned int start_idx; uint32 idx = 0; size_t size = 0, packed_size = 0; fstring dom_name; char *packed_pass; struct trusted_dom_pass *pass = TALLOC_ZERO_P(ctx, struct trusted_dom_pass); NTSTATUS status; if (!secrets_init()) return NT_STATUS_ACCESS_DENIED; if (!pass) { DEBUG(0, ("talloc_zero failed!\n")); return NT_STATUS_NO_MEMORY; } *num_domains = 0; start_idx = *enum_ctx; /* generate searching pattern */ if (!(pattern = talloc_asprintf(ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS))) { DEBUG(0, ("secrets_get_trusted_domains: talloc_asprintf() failed!\n")); return NT_STATUS_NO_MEMORY; } DEBUG(5, ("secrets_get_trusted_domains: looking for %d domains, starting at index %d\n", max_num_domains, *enum_ctx)); *domains = TALLOC_ZERO_ARRAY(ctx, TRUSTDOM *, max_num_domains); /* fetching trusted domains' data and collecting them in a list */ keys = tdb_search_keys(tdb, pattern); /* * if there's no keys returned ie. no trusted domain, * return "no more entries" code */ status = NT_STATUS_NO_MORE_ENTRIES; /* searching for keys in secrets db -- way to go ... */ for (k = keys; k; k = k->next) { char *secrets_key; /* important: ensure null-termination of the key string */ secrets_key = SMB_STRNDUP(k->node_key.dptr, k->node_key.dsize); if (!secrets_key) { DEBUG(0, ("strndup failed!\n")); return NT_STATUS_NO_MEMORY; } packed_pass = secrets_fetch(secrets_key, &size); packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size, pass); /* packed representation isn't needed anymore */ SAFE_FREE(packed_pass); if (size != packed_size) { DEBUG(2, ("Secrets record %s is invalid!\n", secrets_key)); continue; } pull_ucs2_fstring(dom_name, pass->uni_name); DEBUG(18, ("Fetched secret record num %d.\nDomain name: %s, SID: %s\n", idx, dom_name, sid_string_static(&pass->domain_sid))); SAFE_FREE(secrets_key); if (idx >= start_idx && idx < start_idx + max_num_domains) { dom = TALLOC_ZERO_P(ctx, TRUSTDOM); if (!dom) { /* free returned tdb record */ return NT_STATUS_NO_MEMORY; } /* copy domain sid */ SMB_ASSERT(sizeof(dom->sid) == sizeof(pass->domain_sid)); memcpy(&(dom->sid), &(pass->domain_sid), sizeof(dom->sid)); /* copy unicode domain name */ dom->name = TALLOC_MEMDUP(ctx, pass->uni_name, (strlen_w(pass->uni_name) + 1) * sizeof(smb_ucs2_t)); (*domains)[idx - start_idx] = dom; DEBUG(18, ("Secret record is in required range.\n \ start_idx = %d, max_num_domains = %d. Added to returned array.\n", start_idx, max_num_domains)); *enum_ctx = idx + 1; (*num_domains)++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -