Changeset 77 for trunk/samba/source/passdb/secrets.c
- Timestamp:
- Sep 30, 2007, 3:42:50 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/samba/source/passdb/secrets.c
r1 r77 1 /* 1 /* 2 2 Unix SMB/CIFS implementation. 3 3 Copyright (C) Andrew Tridgell 1992-2001 … … 8 8 This program is free software; you can redistribute it and/or modify 9 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2of the License, or10 the Free Software Foundation; either version 3 of the License, or 11 11 (at your option) any later version. 12 12 13 13 This program is distributed in the hope that it will be useful, 14 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 16 GNU General Public License for more details. 17 17 18 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 along with this program. If not, see <http://www.gnu.org/licenses/>. 21 20 */ 22 21 … … 38 37 * 39 38 * Initialised to the current pid, the very first time Samba starts, 40 * and incremented by one each time it is needed. 41 * 39 * and incremented by one each time it is needed. 40 * 42 41 * @note Not called by systems with a working /dev/urandom. 43 42 */ 44 static void get_rand_seed(int *new_seed) 43 static void get_rand_seed(int *new_seed) 45 44 { 46 45 *new_seed = sys_getpid(); … … 70 69 71 70 /** 72 * Set a reseed function for the crypto random generator 73 * 71 * Set a reseed function for the crypto random generator 72 * 74 73 * This avoids a problem where systems without /dev/urandom 75 74 * could send the same challenge to multiple clients … … 98 97 } 99 98 100 /* store a secrets entry 99 /* store a secrets entry 101 100 */ 102 101 BOOL secrets_store(const char *key, const void *data, size_t size) … … 106 105 return False; 107 106 return tdb_trans_store(tdb, string_tdb_data(key), 108 make_tdb_data((const char*)data, size),107 make_tdb_data((const uint8 *)data, size), 109 108 TDB_REPLACE) == 0; 110 109 } … … 118 117 if (!tdb) 119 118 return False; 120 return tdb_ delete(tdb, string_tdb_data(key)) == 0;119 return tdb_trans_delete(tdb, string_tdb_data(key)) == 0; 121 120 } 122 121 … … 149 148 return False; 150 149 151 if (size != sizeof(DOM_SID)) { 150 if (size != sizeof(DOM_SID)) { 152 151 SAFE_FREE(dyn_sid); 153 152 return False; … … 191 190 } 192 191 193 if (size != sizeof(struct GUID)) { 192 if (size != sizeof(struct GUID)) { 194 193 DEBUG(1,("UUID size %d is wrong!\n", (int)size)); 195 194 SAFE_FREE(dyn_guid); … … 213 212 static fstring keystr; 214 213 215 slprintf(keystr,sizeof(keystr)-1,"%s/%s", 214 slprintf(keystr,sizeof(keystr)-1,"%s/%s", 216 215 SECRETS_MACHINE_ACCT_PASS, domain); 217 216 strupper_m(keystr); … … 233 232 pstr_sprintf(keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain); 234 233 strupper_m(keystr); 235 234 236 235 return keystr; 237 236 } … … 257 256 ************************************************************************/ 258 257 259 uint32 get_default_sec_channel(void) 260 { 261 if (lp_server_role() == ROLE_DOMAIN_BDC || 258 uint32 get_default_sec_channel(void) 259 { 260 if (lp_server_role() == ROLE_DOMAIN_BDC || 262 261 lp_server_role() == ROLE_DOMAIN_PDC) { 263 262 return SEC_CHAN_BDC; … … 281 280 size_t size = 0; 282 281 283 plaintext = secrets_fetch_machine_password(domain, pass_last_set_time, 282 plaintext = secrets_fetch_machine_password(domain, pass_last_set_time, 284 283 channel); 285 284 if (plaintext) { … … 295 294 return False; 296 295 } 297 296 298 297 if (size != sizeof(*pass)) { 299 298 DEBUG(0, ("secrets were of incorrect size!\n")); … … 331 330 * @return length of the packed representation of the whole structure 332 331 **/ 333 static size_t tdb_sid_pack( char*pack_buf, int bufsize, DOM_SID* sid)332 static size_t tdb_sid_pack(uint8 *pack_buf, int bufsize, DOM_SID* sid) 334 333 { 335 334 int idx; 336 335 size_t len = 0; 337 336 338 337 if (!sid || !pack_buf) return -1; 339 338 340 339 len += tdb_pack(pack_buf + len, bufsize - len, "bb", sid->sid_rev_num, 341 340 sid->num_auths); 342 341 343 342 for (idx = 0; idx < 6; idx++) { 344 343 len += tdb_pack(pack_buf + len, bufsize - len, "b", 345 344 sid->id_auth[idx]); 346 345 } 347 346 348 347 for (idx = 0; idx < MAXSUBAUTHS; idx++) { 349 348 len += tdb_pack(pack_buf + len, bufsize - len, "d", 350 349 sid->sub_auths[idx]); 351 350 } 352 351 353 352 return len; 354 353 } … … 363 362 * @return size of structure unpacked from buffer 364 363 **/ 365 static size_t tdb_sid_unpack( char*pack_buf, int bufsize, DOM_SID* sid)364 static size_t tdb_sid_unpack(uint8 *pack_buf, int bufsize, DOM_SID* sid) 366 365 { 367 366 int idx, len = 0; 368 367 369 368 if (!sid || !pack_buf) return -1; 370 369 371 370 len += tdb_unpack(pack_buf + len, bufsize - len, "bb", 372 371 &sid->sid_rev_num, &sid->num_auths); 373 372 374 373 for (idx = 0; idx < 6; idx++) { 375 374 len += tdb_unpack(pack_buf + len, bufsize - len, "b", 376 375 &sid->id_auth[idx]); 377 376 } 378 377 379 378 for (idx = 0; idx < MAXSUBAUTHS; idx++) { 380 379 len += tdb_unpack(pack_buf + len, bufsize - len, "d", 381 380 &sid->sub_auths[idx]); 382 381 } 383 382 384 383 return len; 385 384 } … … 394 393 * @return length of the packed representation of the whole structure 395 394 **/ 396 static size_t tdb_trusted_dom_pass_pack( char*pack_buf, int bufsize,395 static size_t tdb_trusted_dom_pass_pack(uint8 *pack_buf, int bufsize, 397 396 TRUSTED_DOM_PASS* pass) 398 397 { 399 398 int idx, len = 0; 400 399 401 400 if (!pack_buf || !pass) return -1; 402 401 403 402 /* packing unicode domain name and password */ 404 403 len += tdb_pack(pack_buf + len, bufsize - len, "d", 405 404 pass->uni_name_len); 406 405 407 406 for (idx = 0; idx < 32; idx++) 408 407 len += tdb_pack(pack_buf + len, bufsize - len, "w", 409 408 pass->uni_name[idx]); 410 409 411 410 len += tdb_pack(pack_buf + len, bufsize - len, "dPd", pass->pass_len, 412 411 pass->pass, pass->mod_time); … … 428 427 * @return size of structure unpacked from buffer 429 428 **/ 430 static size_t tdb_trusted_dom_pass_unpack( char*pack_buf, int bufsize,429 static size_t tdb_trusted_dom_pass_unpack(uint8 *pack_buf, int bufsize, 431 430 TRUSTED_DOM_PASS* pass) 432 431 { 433 432 int idx, len = 0; 434 433 435 434 if (!pack_buf || !pass) return -1; 436 435 437 436 /* unpack unicode domain name and plaintext password */ 438 437 len += tdb_unpack(pack_buf, bufsize - len, "d", &pass->uni_name_len); 439 438 440 439 for (idx = 0; idx < 32; idx++) 441 440 len += tdb_unpack(pack_buf + len, bufsize - len, "w", … … 444 443 len += tdb_unpack(pack_buf + len, bufsize - len, "dPd", 445 444 &pass->pass_len, &pass->pass, &pass->mod_time); 446 445 447 446 /* unpack domain sid */ 448 447 len += tdb_sid_unpack(pack_buf + len, bufsize - len, 449 448 &pass->domain_sid); 450 451 return len; 449 450 return len; 452 451 } 453 452 … … 461 460 struct trusted_dom_pass pass; 462 461 size_t size = 0; 463 462 464 463 /* unpacking structures */ 465 char*pass_buf;464 uint8 *pass_buf; 466 465 int pass_len = 0; 467 466 … … 469 468 470 469 /* fetching trusted domain password structure */ 471 if (!(pass_buf = ( char*)secrets_fetch(trustdom_keystr(domain),470 if (!(pass_buf = (uint8 *)secrets_fetch(trustdom_keystr(domain), 472 471 &size))) { 473 472 DEBUG(5, ("secrets_fetch failed!\n")); … … 483 482 return False; 484 483 } 485 486 /* the trust's password */ 484 485 /* the trust's password */ 487 486 if (pwd) { 488 487 *pwd = SMB_STRDUP(pass.pass); … … 497 496 /* domain sid */ 498 497 if (sid != NULL) sid_copy(sid, &pass.domain_sid); 499 498 500 499 return True; 501 500 } … … 534 533 int pass_len = 0; 535 534 int pass_buf_len = sizeof(pass_buf); 536 535 537 536 struct trusted_dom_pass pass; 538 537 ZERO_STRUCT(pass); … … 543 542 return False; 544 543 } 545 544 546 545 strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1); 547 546 pass.uni_name_len = strlen_w(uni_dom_name)+1; … … 557 556 /* domain sid */ 558 557 sid_copy(&pass.domain_sid, sid); 559 560 pass_len = tdb_trusted_dom_pass_pack( pass_buf, pass_buf_len, &pass);558 559 pass_len = tdb_trusted_dom_pass_pack((uint8 *)pass_buf, pass_buf_len, &pass); 561 560 562 561 return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len); … … 576 575 577 576 asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain); 578 if (!key) 577 if (!key) 579 578 return False; 580 579 strupper_m(key); … … 585 584 if (!ret) 586 585 return ret; 587 586 588 587 asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain); 589 if (!key) 588 if (!key) 590 589 return False; 591 590 strupper_m(key); … … 596 595 597 596 asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain); 598 if (!key) 597 if (!key) 599 598 return False; 600 599 strupper_m(key); … … 612 611 ************************************************************************/ 613 612 614 char *secrets_fetch_machine_password(const char *domain, 613 char *secrets_fetch_machine_password(const char *domain, 615 614 time_t *pass_last_set_time, 616 615 uint32 *channel) … … 622 621 ret = (char *)secrets_fetch(key, NULL); 623 622 SAFE_FREE(key); 624 623 625 624 if (pass_last_set_time) { 626 625 size_t size; … … 637 636 SAFE_FREE(key); 638 637 } 639 638 640 639 if (channel) { 641 640 size_t size; … … 652 651 SAFE_FREE(key); 653 652 } 654 653 655 654 return ret; 656 }657 658 /*******************************************************************659 Wrapper around retrieving the trust account password660 *******************************************************************/661 662 BOOL get_trust_pw(const char *domain, uint8 ret_pwd[16], uint32 *channel)663 {664 DOM_SID sid;665 char *pwd;666 time_t last_set_time;667 668 /* if we are a DC and this is not our domain, then lookup an account669 for the domain trust */670 671 if ( IS_DC && !strequal(domain, lp_workgroup()) && lp_allow_trusted_domains() ) {672 if (!secrets_fetch_trusted_domain_password(domain, &pwd, &sid,673 &last_set_time)) {674 DEBUG(0, ("get_trust_pw: could not fetch trust "675 "account password for trusted domain %s\n",676 domain));677 return False;678 }679 680 *channel = SEC_CHAN_DOMAIN;681 E_md4hash(pwd, ret_pwd);682 SAFE_FREE(pwd);683 684 return True;685 }686 687 /* Just get the account for the requested domain. In the future this688 * might also cover to be member of more than one domain. */689 690 if (secrets_fetch_trust_account_password(domain, ret_pwd,691 &last_set_time, channel))692 return True;693 694 DEBUG(5, ("get_trust_pw: could not fetch trust account "695 "password for domain %s\n", domain));696 return False;697 655 } 698 656 … … 719 677 char *key = NULL; 720 678 BOOL ret; 721 679 722 680 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) { 723 681 DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n")); 724 682 return False; 725 683 } 726 684 727 685 ret = secrets_store(key, pw, strlen(pw)+1); 728 686 729 687 SAFE_FREE(key); 730 688 return ret; … … 739 697 char *key = NULL; 740 698 size_t size = 0; 741 699 742 700 *dn = smb_xstrdup(lp_ldap_admin_dn()); 743 701 744 702 if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) { 745 703 SAFE_FREE(*dn); 746 704 DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n")); 747 705 } 748 706 749 707 *pw=(char *)secrets_fetch(key, &size); 750 708 SAFE_FREE(key); … … 756 714 char *data; 757 715 fstring old_style_pw; 758 716 759 717 if (!old_style_key) { 760 718 DEBUG(0, ("fetch_ldap_pw: strdup failed!\n")); … … 764 722 for (p=old_style_key; *p; p++) 765 723 if (*p == ',') *p = '/'; 766 724 767 725 data=(char *)secrets_fetch(old_style_key, &size); 768 726 if (!size && size < sizeof(old_style_pw)) { … … 783 741 SAFE_FREE(old_style_key); 784 742 SAFE_FREE(*dn); 785 return False; 743 return False; 786 744 } 787 745 if (!secrets_delete(old_style_key)) { … … 791 749 SAFE_FREE(old_style_key); 792 750 793 *pw = smb_xstrdup(old_style_pw); 794 } 795 751 *pw = smb_xstrdup(old_style_pw); 752 } 753 796 754 return True; 797 755 } … … 799 757 /** 800 758 * Get trusted domains info from secrets.tdb. 801 **/ 759 **/ 802 760 803 761 NTSTATUS secrets_trusted_domains(TALLOC_CTX *mem_ctx, uint32 *num_domains, … … 813 771 814 772 if (!secrets_init()) return NT_STATUS_ACCESS_DENIED; 815 773 816 774 /* generate searching pattern */ 817 775 pattern = talloc_asprintf(tmp_ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS); … … 840 798 /* searching for keys in secrets db -- way to go ... */ 841 799 for (k = keys; k; k = k->next) { 842 char*packed_pass;800 uint8 *packed_pass; 843 801 size_t size = 0, packed_size = 0; 844 802 struct trusted_dom_pass pass; 845 803 char *secrets_key; 846 804 struct trustdom_info *dom_info; 847 805 848 806 /* important: ensure null-termination of the key string */ 849 807 secrets_key = talloc_strndup(tmp_ctx, 850 k->node_key.dptr,808 (const char *)k->node_key.dptr, 851 809 k->node_key.dsize); 852 810 if (!secrets_key) { … … 857 815 } 858 816 859 packed_pass = ( char*)secrets_fetch(secrets_key, &size);817 packed_pass = (uint8 *)secrets_fetch(secrets_key, &size); 860 818 packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size, 861 819 &pass); 862 820 /* packed representation isn't needed anymore */ 863 821 SAFE_FREE(packed_pass); 864 822 865 823 if (size != packed_size) { 866 824 DEBUG(2, ("Secrets record %s is invalid!\n", … … 903 861 } 904 862 } 905 863 906 864 DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", 907 865 *num_domains)); … … 999 957 /****************************************************************************** 1000 958 When kerberos is not available, choose between anonymous or 1001 authenticated connections. 959 authenticated connections. 1002 960 1003 961 We need to use an authenticated connection if DCs have the … … 1013 971 *domain = (char *)secrets_fetch(SECRETS_AUTH_DOMAIN, NULL); 1014 972 *password = (char *)secrets_fetch(SECRETS_AUTH_PASSWORD, NULL); 1015 973 1016 974 if (*username && **username) { 1017 975 1018 976 if (!*domain || !**domain) 1019 977 *domain = smb_xstrdup(lp_workgroup()); 1020 978 1021 979 if (!*password || !**password) 1022 980 *password = smb_xstrdup(""); 1023 981 1024 DEBUG(3, ("IPC$ connections done by user %s\\%s\n", 982 DEBUG(3, ("IPC$ connections done by user %s\\%s\n", 1025 983 *domain, *username)); 1026 984 … … 1060 1018 /* First opener, no version. */ 1061 1019 SIVAL(&ver,0,1); 1062 vers.dptr = ( char*)&ver;1020 vers.dptr = (uint8 *)&ver; 1063 1021 vers.dsize = 4; 1064 1022 tdb_store_bystring(tdb_sc, "SCHANNEL_STORE_VERSION", vers, TDB_REPLACE); … … 1117 1075 pdc->domain); 1118 1076 1119 value.dptr = (char *)TALLOC(mem_ctx, value.dsize);1077 value.dptr = TALLOC_ARRAY(mem_ctx, uint8, value.dsize); 1120 1078 if (!value.dptr) { 1121 1079 TALLOC_FREE(keystr); … … 1258 1216 char *tdbkey = NULL; 1259 1217 BOOL ret; 1260 1218 1261 1219 if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) { 1262 1220 DEBUG(0, ("asprintf failed!\n")); 1263 1221 return False; 1264 1222 } 1265 1223 1266 1224 ret = secrets_store(tdbkey, secret, strlen(secret)+1); 1267 1225 1268 1226 SAFE_FREE(tdbkey); 1269 1227 return ret; … … 1288 1246 return NULL; 1289 1247 } 1290 1248 1291 1249 secret = (char *)secrets_fetch(tdbkey, NULL); 1292 1250 SAFE_FREE(tdbkey);
Note:
See TracChangeset
for help on using the changeset viewer.