Changeset 988 for vendor/current/source3/passdb/secrets.c
- Timestamp:
- Nov 24, 2016, 1:14:11 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/source3/passdb/secrets.c
r740 r988 25 25 #include "includes.h" 26 26 #include "system/filesys.h" 27 #include "passdb.h"28 27 #include "../libcli/auth/libcli_auth.h" 29 28 #include "librpc/gen_ndr/ndr_secrets.h" 30 29 #include "secrets.h" 31 #include "dbwrap.h" 30 #include "dbwrap/dbwrap.h" 31 #include "dbwrap/dbwrap_open.h" 32 32 #include "../libcli/security/security.h" 33 33 #include "util_tdb.h" … … 38 38 static struct db_context *db_ctx; 39 39 40 /** 41 * Use a TDB to store an incrementing random seed. 42 * 43 * Initialised to the current pid, the very first time Samba starts, 44 * and incremented by one each time it is needed. 45 * 46 * @note Not called by systems with a working /dev/urandom. 47 */ 48 static void get_rand_seed(void *userdata, int *new_seed) 49 { 50 *new_seed = sys_getpid(); 51 if (db_ctx) { 52 dbwrap_trans_change_int32_atomic(db_ctx, "INFO/random_seed", 53 new_seed, 1); 54 } 40 /* open up the secrets database with specified private_dir path */ 41 bool secrets_init_path(const char *private_dir) 42 { 43 char *fname = NULL; 44 TALLOC_CTX *frame; 45 46 if (db_ctx != NULL) { 47 return True; 48 } 49 50 if (private_dir == NULL) { 51 return False; 52 } 53 54 frame = talloc_stackframe(); 55 fname = talloc_asprintf(frame, "%s/secrets.tdb", private_dir); 56 if (fname == NULL) { 57 TALLOC_FREE(frame); 58 return False; 59 } 60 61 db_ctx = db_open(NULL, fname, 0, 62 TDB_DEFAULT, O_RDWR|O_CREAT, 0600, 63 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE); 64 65 if (db_ctx == NULL) { 66 DEBUG(0,("Failed to open %s\n", fname)); 67 TALLOC_FREE(frame); 68 return False; 69 } 70 71 TALLOC_FREE(frame); 72 return True; 55 73 } 56 74 … … 58 76 bool secrets_init(void) 59 77 { 60 char *fname = NULL; 61 unsigned char dummy; 62 63 if (db_ctx != NULL) 64 return True; 65 66 fname = talloc_asprintf(talloc_tos(), "%s/secrets.tdb", 67 lp_private_dir()); 68 if (fname == NULL) { 69 return false; 70 } 71 72 db_ctx = db_open(NULL, fname, 0, 73 TDB_DEFAULT, O_RDWR|O_CREAT, 0600); 74 75 if (db_ctx == NULL) { 76 DEBUG(0,("Failed to open %s\n", fname)); 77 TALLOC_FREE(fname); 78 return False; 79 } 80 81 TALLOC_FREE(fname); 82 83 /** 84 * Set a reseed function for the crypto random generator 85 * 86 * This avoids a problem where systems without /dev/urandom 87 * could send the same challenge to multiple clients 88 */ 89 set_rand_reseed_callback(get_rand_seed, NULL); 90 91 /* Ensure that the reseed is done now, while we are root, etc */ 92 generate_random_buffer(&dummy, sizeof(dummy)); 93 94 return True; 78 return secrets_init_path(lp_private_dir()); 95 79 } 96 80 … … 119 103 TDB_DATA dbuf; 120 104 void *result; 105 NTSTATUS status; 121 106 122 107 if (!secrets_init()) { … … 124 109 } 125 110 126 if (db_ctx->fetch(db_ctx, talloc_tos(), string_tdb_data(key), 127 &dbuf) != 0) { 128 return NULL; 129 } 130 131 result = memdup(dbuf.dptr, dbuf.dsize); 111 status = dbwrap_fetch(db_ctx, talloc_tos(), string_tdb_data(key), 112 &dbuf); 113 if (!NT_STATUS_IS_OK(status)) { 114 return NULL; 115 } 116 117 result = smb_memdup(dbuf.dptr, dbuf.dsize); 132 118 if (result == NULL) { 133 119 return NULL; … … 153 139 154 140 status = dbwrap_trans_store(db_ctx, string_tdb_data(key), 155 make_tdb_data((const uint8 *)data, size),141 make_tdb_data((const uint8_t *)data, size), 156 142 TDB_REPLACE); 157 143 return NT_STATUS_IS_OK(status); … … 320 306 size_t size = 0; 321 307 322 *dn = smb_xstrdup(lp_ldap_admin_dn( ));308 *dn = smb_xstrdup(lp_ldap_admin_dn(talloc_tos())); 323 309 324 310 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) { … … 379 365 } 380 366 381 /**382 * Get trusted domains info from secrets.tdb.383 **/384 385 struct list_trusted_domains_state {386 uint32 num_domains;387 struct trustdom_info **domains;388 };389 390 static int list_trusted_domain(struct db_record *rec, void *private_data)391 {392 const size_t prefix_len = strlen(SECRETS_DOMTRUST_ACCT_PASS);393 struct TRUSTED_DOM_PASS pass;394 enum ndr_err_code ndr_err;395 DATA_BLOB blob;396 struct trustdom_info *dom_info;397 398 struct list_trusted_domains_state *state =399 (struct list_trusted_domains_state *)private_data;400 401 if ((rec->key.dsize < prefix_len)402 || (strncmp((char *)rec->key.dptr, SECRETS_DOMTRUST_ACCT_PASS,403 prefix_len) != 0)) {404 return 0;405 }406 407 blob = data_blob_const(rec->value.dptr, rec->value.dsize);408 409 ndr_err = ndr_pull_struct_blob(&blob, talloc_tos(), &pass,410 (ndr_pull_flags_fn_t)ndr_pull_TRUSTED_DOM_PASS);411 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {412 return false;413 }414 415 if (pass.domain_sid.num_auths != 4) {416 DEBUG(0, ("SID %s is not a domain sid, has %d "417 "auths instead of 4\n",418 sid_string_dbg(&pass.domain_sid),419 pass.domain_sid.num_auths));420 return 0;421 }422 423 if (!(dom_info = TALLOC_P(state->domains, struct trustdom_info))) {424 DEBUG(0, ("talloc failed\n"));425 return 0;426 }427 428 dom_info->name = talloc_strdup(dom_info, pass.uni_name);429 if (!dom_info->name) {430 TALLOC_FREE(dom_info);431 return 0;432 }433 434 sid_copy(&dom_info->sid, &pass.domain_sid);435 436 ADD_TO_ARRAY(state->domains, struct trustdom_info *, dom_info,437 &state->domains, &state->num_domains);438 439 if (state->domains == NULL) {440 state->num_domains = 0;441 return -1;442 }443 return 0;444 }445 446 NTSTATUS secrets_trusted_domains(TALLOC_CTX *mem_ctx, uint32 *num_domains,447 struct trustdom_info ***domains)448 {449 struct list_trusted_domains_state state;450 451 if (!secrets_init()) {452 return NT_STATUS_ACCESS_DENIED;453 }454 455 state.num_domains = 0;456 457 /*458 * Make sure that a talloc context for the trustdom_info structs459 * exists460 */461 462 if (!(state.domains = TALLOC_ARRAY(463 mem_ctx, struct trustdom_info *, 1))) {464 return NT_STATUS_NO_MEMORY;465 }466 467 db_ctx->traverse_read(db_ctx, list_trusted_domain, (void *)&state);468 469 *num_domains = state.num_domains;470 *domains = state.domains;471 return NT_STATUS_OK;472 }473 474 367 /******************************************************************************* 475 368 Store a complete AFS keyfile into secrets.tdb. … … 498 391 struct afs_keyfile *keyfile; 499 392 size_t size = 0; 500 uint32 i;393 uint32_t i; 501 394 502 395 slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell); … … 580 473 } 581 474 582 bool secrets_delete_generic(const char *owner, const char *key)583 {584 char *tdbkey = NULL;585 bool ret;586 587 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {588 DEBUG(0, ("asprintf failed!\n"));589 return False;590 }591 592 ret = secrets_delete(tdbkey);593 594 SAFE_FREE(tdbkey);595 return ret;596 }597 598 475 /******************************************************************* 599 476 Find the ldap password.
Note:
See TracChangeset
for help on using the changeset viewer.