Ignore:
Timestamp:
Sep 30, 2007, 3:42:50 AM (18 years ago)
Author:
Paul Smedley
Message:

Update trunk to 3.2.0pre1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/samba/source/passdb/secrets.c

    r1 r77  
    1 /* 
     1/*
    22   Unix SMB/CIFS implementation.
    33   Copyright (C) Andrew Tridgell 1992-2001
     
    88   This program is free software; you can redistribute it and/or modify
    99   it under the terms of the GNU General Public License as published by
    10    the Free Software Foundation; either version 2 of the License, or
     10   the Free Software Foundation; either version 3 of the License, or
    1111   (at your option) any later version.
    12    
     12
    1313   This program is distributed in the hope that it will be useful,
    1414   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1515   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1616   GNU General Public License for more details.
    17    
     17
    1818   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/>.
    2120*/
    2221
     
    3837 *
    3938 * 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 *
    4241 * @note Not called by systems with a working /dev/urandom.
    4342 */
    44 static void get_rand_seed(int *new_seed) 
     43static void get_rand_seed(int *new_seed)
    4544{
    4645        *new_seed = sys_getpid();
     
    7069
    7170        /**
    72          * Set a reseed function for the crypto random generator 
    73          * 
     71         * Set a reseed function for the crypto random generator
     72         *
    7473         * This avoids a problem where systems without /dev/urandom
    7574         * could send the same challenge to multiple clients
     
    9897}
    9998
    100 /* store a secrets entry 
     99/* store a secrets entry
    101100 */
    102101BOOL secrets_store(const char *key, const void *data, size_t size)
     
    106105                return False;
    107106        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),
    109108                               TDB_REPLACE) == 0;
    110109}
     
    118117        if (!tdb)
    119118                return False;
    120         return tdb_delete(tdb, string_tdb_data(key)) == 0;
     119        return tdb_trans_delete(tdb, string_tdb_data(key)) == 0;
    121120}
    122121
     
    149148                return False;
    150149
    151         if (size != sizeof(DOM_SID)) { 
     150        if (size != sizeof(DOM_SID)) {
    152151                SAFE_FREE(dyn_sid);
    153152                return False;
     
    191190        }
    192191
    193         if (size != sizeof(struct GUID)) { 
     192        if (size != sizeof(struct GUID)) {
    194193                DEBUG(1,("UUID size %d is wrong!\n", (int)size));
    195194                SAFE_FREE(dyn_guid);
     
    213212        static fstring keystr;
    214213
    215         slprintf(keystr,sizeof(keystr)-1,"%s/%s", 
     214        slprintf(keystr,sizeof(keystr)-1,"%s/%s",
    216215                 SECRETS_MACHINE_ACCT_PASS, domain);
    217216        strupper_m(keystr);
     
    233232        pstr_sprintf(keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
    234233        strupper_m(keystr);
    235                
     234
    236235        return keystr;
    237236}
     
    257256************************************************************************/
    258257
    259 uint32 get_default_sec_channel(void) 
    260 {
    261         if (lp_server_role() == ROLE_DOMAIN_BDC || 
     258uint32 get_default_sec_channel(void)
     259{
     260        if (lp_server_role() == ROLE_DOMAIN_BDC ||
    262261            lp_server_role() == ROLE_DOMAIN_PDC) {
    263262                return SEC_CHAN_BDC;
     
    281280        size_t size = 0;
    282281
    283         plaintext = secrets_fetch_machine_password(domain, pass_last_set_time, 
     282        plaintext = secrets_fetch_machine_password(domain, pass_last_set_time,
    284283                                                   channel);
    285284        if (plaintext) {
     
    295294                return False;
    296295        }
    297        
     296
    298297        if (size != sizeof(*pass)) {
    299298                DEBUG(0, ("secrets were of incorrect size!\n"));
     
    331330 * @return length of the packed representation of the whole structure
    332331 **/
    333 static size_t tdb_sid_pack(char* pack_buf, int bufsize, DOM_SID* sid)
     332static size_t tdb_sid_pack(uint8 *pack_buf, int bufsize, DOM_SID* sid)
    334333{
    335334        int idx;
    336335        size_t len = 0;
    337        
     336
    338337        if (!sid || !pack_buf) return -1;
    339        
     338
    340339        len += tdb_pack(pack_buf + len, bufsize - len, "bb", sid->sid_rev_num,
    341340                        sid->num_auths);
    342        
     341
    343342        for (idx = 0; idx < 6; idx++) {
    344343                len += tdb_pack(pack_buf + len, bufsize - len, "b",
    345344                                sid->id_auth[idx]);
    346345        }
    347        
     346
    348347        for (idx = 0; idx < MAXSUBAUTHS; idx++) {
    349348                len += tdb_pack(pack_buf + len, bufsize - len, "d",
    350349                                sid->sub_auths[idx]);
    351350        }
    352        
     351
    353352        return len;
    354353}
     
    363362 * @return size of structure unpacked from buffer
    364363 **/
    365 static size_t tdb_sid_unpack(char* pack_buf, int bufsize, DOM_SID* sid)
     364static size_t tdb_sid_unpack(uint8 *pack_buf, int bufsize, DOM_SID* sid)
    366365{
    367366        int idx, len = 0;
    368        
     367
    369368        if (!sid || !pack_buf) return -1;
    370369
    371370        len += tdb_unpack(pack_buf + len, bufsize - len, "bb",
    372371                          &sid->sid_rev_num, &sid->num_auths);
    373                          
     372
    374373        for (idx = 0; idx < 6; idx++) {
    375374                len += tdb_unpack(pack_buf + len, bufsize - len, "b",
    376375                                  &sid->id_auth[idx]);
    377376        }
    378        
     377
    379378        for (idx = 0; idx < MAXSUBAUTHS; idx++) {
    380379                len += tdb_unpack(pack_buf + len, bufsize - len, "d",
    381380                                  &sid->sub_auths[idx]);
    382381        }
    383        
     382
    384383        return len;
    385384}
     
    394393 * @return length of the packed representation of the whole structure
    395394 **/
    396 static size_t tdb_trusted_dom_pass_pack(char* pack_buf, int bufsize,
     395static size_t tdb_trusted_dom_pass_pack(uint8 *pack_buf, int bufsize,
    397396                                        TRUSTED_DOM_PASS* pass)
    398397{
    399398        int idx, len = 0;
    400        
     399
    401400        if (!pack_buf || !pass) return -1;
    402        
     401
    403402        /* packing unicode domain name and password */
    404403        len += tdb_pack(pack_buf + len, bufsize - len, "d",
    405404                        pass->uni_name_len);
    406        
     405
    407406        for (idx = 0; idx < 32; idx++)
    408407                len +=  tdb_pack(pack_buf + len, bufsize - len, "w",
    409408                                 pass->uni_name[idx]);
    410        
     409
    411410        len += tdb_pack(pack_buf + len, bufsize - len, "dPd", pass->pass_len,
    412411                             pass->pass, pass->mod_time);
     
    428427 * @return size of structure unpacked from buffer
    429428 **/
    430 static size_t tdb_trusted_dom_pass_unpack(char* pack_buf, int bufsize,
     429static size_t tdb_trusted_dom_pass_unpack(uint8 *pack_buf, int bufsize,
    431430                                          TRUSTED_DOM_PASS* pass)
    432431{
    433432        int idx, len = 0;
    434        
     433
    435434        if (!pack_buf || !pass) return -1;
    436435
    437436        /* unpack unicode domain name and plaintext password */
    438437        len += tdb_unpack(pack_buf, bufsize - len, "d", &pass->uni_name_len);
    439        
     438
    440439        for (idx = 0; idx < 32; idx++)
    441440                len +=  tdb_unpack(pack_buf + len, bufsize - len, "w",
     
    444443        len += tdb_unpack(pack_buf + len, bufsize - len, "dPd",
    445444                          &pass->pass_len, &pass->pass, &pass->mod_time);
    446        
     445
    447446        /* unpack domain sid */
    448447        len += tdb_sid_unpack(pack_buf + len, bufsize - len,
    449448                              &pass->domain_sid);
    450        
    451         return len;     
     449
     450        return len;
    452451}
    453452
     
    461460        struct trusted_dom_pass pass;
    462461        size_t size = 0;
    463        
     462
    464463        /* unpacking structures */
    465         char* pass_buf;
     464        uint8 *pass_buf;
    466465        int pass_len = 0;
    467466
     
    469468
    470469        /* 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),
    472471                                               &size))) {
    473472                DEBUG(5, ("secrets_fetch failed!\n"));
     
    483482                return False;
    484483        }
    485                        
    486         /* the trust's password */     
     484
     485        /* the trust's password */
    487486        if (pwd) {
    488487                *pwd = SMB_STRDUP(pass.pass);
     
    497496        /* domain sid */
    498497        if (sid != NULL) sid_copy(sid, &pass.domain_sid);
    499                
     498
    500499        return True;
    501500}
     
    534533        int pass_len = 0;
    535534        int pass_buf_len = sizeof(pass_buf);
    536        
     535
    537536        struct trusted_dom_pass pass;
    538537        ZERO_STRUCT(pass);
     
    543542                return False;
    544543        }
    545        
     544
    546545        strncpy_w(pass.uni_name, uni_dom_name, sizeof(pass.uni_name) - 1);
    547546        pass.uni_name_len = strlen_w(uni_dom_name)+1;
     
    557556        /* domain sid */
    558557        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);
    561560
    562561        return secrets_store(trustdom_keystr(domain), (void *)&pass_buf, pass_len);
     
    576575
    577576        asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
    578         if (!key) 
     577        if (!key)
    579578                return False;
    580579        strupper_m(key);
     
    585584        if (!ret)
    586585                return ret;
    587        
     586
    588587        asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
    589         if (!key) 
     588        if (!key)
    590589                return False;
    591590        strupper_m(key);
     
    596595
    597596        asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
    598         if (!key) 
     597        if (!key)
    599598                return False;
    600599        strupper_m(key);
     
    612611************************************************************************/
    613612
    614 char *secrets_fetch_machine_password(const char *domain, 
     613char *secrets_fetch_machine_password(const char *domain,
    615614                                     time_t *pass_last_set_time,
    616615                                     uint32 *channel)
     
    622621        ret = (char *)secrets_fetch(key, NULL);
    623622        SAFE_FREE(key);
    624        
     623
    625624        if (pass_last_set_time) {
    626625                size_t size;
     
    637636                SAFE_FREE(key);
    638637        }
    639        
     638
    640639        if (channel) {
    641640                size_t size;
     
    652651                SAFE_FREE(key);
    653652        }
    654        
     653
    655654        return ret;
    656 }
    657 
    658 /*******************************************************************
    659  Wrapper around retrieving the trust account password
    660 *******************************************************************/
    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 account
    669                 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 this
    688          * 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;
    697655}
    698656
     
    719677        char *key = NULL;
    720678        BOOL ret;
    721        
     679
    722680        if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
    723681                DEBUG(0, ("secrets_store_ldap_pw: asprintf failed!\n"));
    724682                return False;
    725683        }
    726                
     684
    727685        ret = secrets_store(key, pw, strlen(pw)+1);
    728        
     686
    729687        SAFE_FREE(key);
    730688        return ret;
     
    739697        char *key = NULL;
    740698        size_t size = 0;
    741        
     699
    742700        *dn = smb_xstrdup(lp_ldap_admin_dn());
    743        
     701
    744702        if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, *dn) < 0) {
    745703                SAFE_FREE(*dn);
    746704                DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
    747705        }
    748        
     706
    749707        *pw=(char *)secrets_fetch(key, &size);
    750708        SAFE_FREE(key);
     
    756714                char *data;
    757715                fstring old_style_pw;
    758                
     716
    759717                if (!old_style_key) {
    760718                        DEBUG(0, ("fetch_ldap_pw: strdup failed!\n"));
     
    764722                for (p=old_style_key; *p; p++)
    765723                        if (*p == ',') *p = '/';
    766        
     724
    767725                data=(char *)secrets_fetch(old_style_key, &size);
    768726                if (!size && size < sizeof(old_style_pw)) {
     
    783741                        SAFE_FREE(old_style_key);
    784742                        SAFE_FREE(*dn);
    785                         return False;                   
     743                        return False;
    786744                }
    787745                if (!secrets_delete(old_style_key)) {
     
    791749                SAFE_FREE(old_style_key);
    792750
    793                 *pw = smb_xstrdup(old_style_pw);               
    794         }
    795        
     751                *pw = smb_xstrdup(old_style_pw);
     752        }
     753
    796754        return True;
    797755}
     
    799757/**
    800758 * Get trusted domains info from secrets.tdb.
    801  **/ 
     759 **/
    802760
    803761NTSTATUS secrets_trusted_domains(TALLOC_CTX *mem_ctx, uint32 *num_domains,
     
    813771
    814772        if (!secrets_init()) return NT_STATUS_ACCESS_DENIED;
    815        
     773
    816774        /* generate searching pattern */
    817775        pattern = talloc_asprintf(tmp_ctx, "%s/*", SECRETS_DOMTRUST_ACCT_PASS);
     
    840798        /* searching for keys in secrets db -- way to go ... */
    841799        for (k = keys; k; k = k->next) {
    842                 char *packed_pass;
     800                uint8 *packed_pass;
    843801                size_t size = 0, packed_size = 0;
    844802                struct trusted_dom_pass pass;
    845803                char *secrets_key;
    846804                struct trustdom_info *dom_info;
    847                
     805
    848806                /* important: ensure null-termination of the key string */
    849807                secrets_key = talloc_strndup(tmp_ctx,
    850                                              k->node_key.dptr,
     808                                             (const char *)k->node_key.dptr,
    851809                                             k->node_key.dsize);
    852810                if (!secrets_key) {
     
    857815                }
    858816
    859                 packed_pass = (char *)secrets_fetch(secrets_key, &size);
     817                packed_pass = (uint8 *)secrets_fetch(secrets_key, &size);
    860818                packed_size = tdb_trusted_dom_pass_unpack(packed_pass, size,
    861819                                                          &pass);
    862820                /* packed representation isn't needed anymore */
    863821                SAFE_FREE(packed_pass);
    864                
     822
    865823                if (size != packed_size) {
    866824                        DEBUG(2, ("Secrets record %s is invalid!\n",
     
    903861                }
    904862        }
    905        
     863
    906864        DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n",
    907865                  *num_domains));
     
    999957/******************************************************************************
    1000958  When kerberos is not available, choose between anonymous or
    1001   authenticated connections. 
     959  authenticated connections.
    1002960
    1003961  We need to use an authenticated connection if DCs have the
     
    1013971        *domain = (char *)secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
    1014972        *password = (char *)secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
    1015        
     973
    1016974        if (*username && **username) {
    1017975
    1018976                if (!*domain || !**domain)
    1019977                        *domain = smb_xstrdup(lp_workgroup());
    1020                
     978
    1021979                if (!*password || !**password)
    1022980                        *password = smb_xstrdup("");
    1023981
    1024                 DEBUG(3, ("IPC$ connections done by user %s\\%s\n", 
     982                DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
    1025983                          *domain, *username));
    1026984
     
    10601018                /* First opener, no version. */
    10611019                SIVAL(&ver,0,1);
    1062                 vers.dptr = (char *)&ver;
     1020                vers.dptr = (uint8 *)&ver;
    10631021                vers.dsize = 4;
    10641022                tdb_store_bystring(tdb_sc, "SCHANNEL_STORE_VERSION", vers, TDB_REPLACE);
     
    11171075                                pdc->domain);
    11181076
    1119         value.dptr = (char *)TALLOC(mem_ctx, value.dsize);
     1077        value.dptr = TALLOC_ARRAY(mem_ctx, uint8, value.dsize);
    11201078        if (!value.dptr) {
    11211079                TALLOC_FREE(keystr);
     
    12581216        char *tdbkey = NULL;
    12591217        BOOL ret;
    1260        
     1218
    12611219        if (asprintf(&tdbkey, "SECRETS/GENERIC/%s/%s", owner, key) < 0) {
    12621220                DEBUG(0, ("asprintf failed!\n"));
    12631221                return False;
    12641222        }
    1265                
     1223
    12661224        ret = secrets_store(tdbkey, secret, strlen(secret)+1);
    1267        
     1225
    12681226        SAFE_FREE(tdbkey);
    12691227        return ret;
     
    12881246                return NULL;
    12891247        }
    1290        
     1248
    12911249        secret = (char *)secrets_fetch(tdbkey, NULL);
    12921250        SAFE_FREE(tdbkey);
Note: See TracChangeset for help on using the changeset viewer.