1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
/*
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
Copyright (C) Jelmer Vernooij 2005
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 */
#define SECRETS_DOMAIN_SID "SECRETS/SID"
#define SECRETS_DOMAIN_GUID "SECRETS/DOMGUID"
#define SECRETS_LDAP_BIND_PW "SECRETS/LDAP_BIND_PW"
#define SECRETS_MACHINE_ACCT_PASS "SECRETS/$MACHINE.ACC"
#define SECRETS_DOMTRUST_ACCT_PASS "SECRETS/$DOMTRUST.ACC"
#define SECRETS_MACHINE_PASSWORD "SECRETS/MACHINE_PASSWORD"
#define SECRETS_MACHINE_LAST_CHANGE_TIME "SECRETS/MACHINE_LAST_CHANGE_TIME"
#define SECRETS_MACHINE_SEC_CHANNEL_TYPE "SECRETS/MACHINE_SEC_CHANNEL_TYPE"
#define SECRETS_AFS_KEYFILE "SECRETS/AFS_KEYFILE"
#define SECRETS_AUTH_USER "SECRETS/AUTH_USER"
#define SECRETS_AUTH_DOMAIN "SECRETS/AUTH_DOMAIN"
#define SECRETS_AUTH_PASSWORD "SECRETS/AUTH_PASSWORD"
#include "includes.h"
#include "tdb.h"
#include "system/filesys.h"
#include "librpc/gen_ndr/ndr_security.h"
/* structure for storing machine account password
(ie. when samba server is member of a domain */
struct machine_acct_pass {
uint8_t hash[16];
time_t mod_time;
};
#define SECRETS_AFS_MAXKEYS 8
struct afs_key {
uint32_t kvno;
char key[8];
};
static TDB_CONTEXT *secrets_open(const char *fname)
{
TDB_CONTEXT *tdb = tdb_open(fname, 0, TDB_DEFAULT, O_RDONLY, 0600);
if (!tdb) {
DEBUG(0,("Failed to open %s\n", fname));
return NULL;
}
return tdb;
}
/* 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
*/
static void *secrets_fetch(TDB_CONTEXT *tdb, const char *key, size_t *size)
{
TDB_DATA kbuf, dbuf;
kbuf.dptr = strdup(key);
kbuf.dsize = strlen(key);
dbuf = tdb_fetch(tdb, kbuf);
if (size)
*size = dbuf.dsize;
free(kbuf.dptr);
return dbuf.dptr;
}
static BOOL secrets_fetch_domain_sid(TDB_CONTEXT *tdb, const char *domain, struct dom_sid *sid)
{
struct dom_sid *dyn_sid;
char *key;
size_t size;
asprintf(&key, "%s/%s", SECRETS_DOMAIN_SID, domain);
strupper_m(key);
dyn_sid = (struct dom_sid *)secrets_fetch(tdb, key, &size);
SAFE_FREE(key);
if (dyn_sid == NULL)
return False;
if (size != sizeof(struct dom_sid))
{
SAFE_FREE(dyn_sid);
return False;
}
*sid = *dyn_sid;
SAFE_FREE(dyn_sid);
return True;
}
static BOOL secrets_fetch_domain_guid(TDB_CONTEXT *tdb, const char *domain, struct GUID *guid)
{
struct GUID *dyn_guid;
char *key;
size_t size;
asprintf(&key, "%s/%s", SECRETS_DOMAIN_GUID, domain);
strupper_m(key);
dyn_guid = (struct GUID *)secrets_fetch(tdb, key, &size);
if (!dyn_guid) {
return False;
}
if (size != sizeof(struct GUID))
{
DEBUG(1,("GUID 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
**/
static char *trust_keystr(const char *domain)
{
char *keystr;
asprintf(&keystr, "%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)
{
char *keystr;
asprintf(&keystr, "%s/%s", SECRETS_DOMTRUST_ACCT_PASS, domain);
strupper_m(keystr);
return keystr;
}
/************************************************************************
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().
************************************************************************/
static BOOL secrets_fetch_trust_account_password(TDB_CONTEXT *tdb,
const char *domain, uint8_t ret_pwd[16],
time_t *pass_last_set_time,
uint32_t *channel)
{
struct machine_acct_pass *pass;
char *plaintext;
size_t size;
plaintext = secrets_fetch_machine_password(tdb, 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(tdb, 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);
SAFE_FREE(pass);
if (channel)
*channel = get_default_sec_channel();
return True;
}
/************************************************************************
Routine to get account password to trusted domain
************************************************************************/
static BOOL secrets_fetch_trusted_domain_password(TDB_CONTEXT *tdb, const char *domain, char** pwd, struct dom_sid **sid, time_t *pass_last_set_time)
{
struct trusted_dom_pass pass;
size_t size;
/* unpacking structures */
char* pass_buf;
int pass_len = 0;
ZERO_STRUCT(pass);
/* fetching trusted domain password structure */
if (!(pass_buf = secrets_fetch(tdb, 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 = strdup(pass.pass);
if (!*pwd) {
return False;
}
}
/* last change time */
if (pass_last_set_time) *pass_last_set_time = pass.mod_time;
/* domain sid */
*sid = dom_sid_dup(tdb, &pass.domain_sid);
return True;
}
/************************************************************************
Routine to fetch the plaintext machine account password for a realm
the password is assumed to be a null terminated ascii string
************************************************************************/
static char *secrets_fetch_machine_password(TDB_CONTEXT *tdb, const char *domain,
time_t *pass_last_set_time,
uint32_t *channel)
{
char *key = NULL;
char *ret;
asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
strupper_m(key);
ret = (char *)secrets_fetch(tdb, key, NULL);
SAFE_FREE(key);
if (pass_last_set_time) {
size_t size;
uint32_t *last_set_time;
asprintf(&key, "%s/%s", SECRETS_MACHINE_LAST_CHANGE_TIME, domain);
strupper_m(key);
last_set_time = secrets_fetch(tdb, 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_t *channel_type;
asprintf(&key, "%s/%s", SECRETS_MACHINE_SEC_CHANNEL_TYPE, domain);
strupper_m(key);
channel_type = secrets_fetch(tdb, 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;
}
/*******************************************************************
find the ldap password
******************************************************************/
static BOOL fetch_ldap_pw(TDB_CONTEXT *tdb, const char *dn, char** pw)
{
char *key = NULL;
size_t size;
if (asprintf(&key, "%s/%s", SECRETS_LDAP_BIND_PW, dn) < 0) {
DEBUG(0, ("fetch_ldap_pw: asprintf failed!\n"));
}
*pw=secrets_fetch(tdb, key, &size);
SAFE_FREE(key);
if (!size) {
return False;
}
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
**/
static NTSTATUS secrets_get_trusted_domains(TDB_CONTEXT *tdb, TALLOC_CTX* ctx, int* enum_ctx, unsigned int max_num_domains,
int *num_domains, struct samba3_trustdom ***domains)
{
TDB_LIST_NODE *keys, *k;
struct samba3_trustdom *dom = NULL;
char *pattern;
unsigned int start_idx;
uint32_t idx = 0;
size_t size, packed_size = 0;
fstring dom_name;
char *packed_pass;
struct trusted_dom_pass *pass = talloc(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, struct samba3_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 = 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(tdb, 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(ctx, struct samba3_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)++;
/* set proper status code to return */
if (k->next) {
/* there are yet some entries to enumerate */
status = STATUS_MORE_ENTRIES;
} else {
/* this is the last entry in the whole enumeration */
status = NT_STATUS_OK;
}
} else {
DEBUG(18, ("Secret is outside the required range.\n \
start_idx = %d, max_num_domains = %d. Not added to returned array\n",
start_idx, max_num_domains));
}
idx++;
}
DEBUG(5, ("secrets_get_trusted_domains: got %d domains\n", *num_domains));
/* free the results of searching the keys */
tdb_search_list_free(keys);
return status;
}
/*******************************************************************************
Fetch the current (highest) AFS key from secrets.tdb
*******************************************************************************/
static BOOL secrets_fetch_afs_key(TDB_CONTEXT *tdb, const char *cell, struct afs_key *result)
{
fstring key;
struct afs_keyfile *keyfile;
size_t size;
uint32_t i;
slprintf(key, sizeof(key)-1, "%s/%s", SECRETS_AFS_KEYFILE, cell);
keyfile = (struct afs_keyfile *)secrets_fetch(tdb, key, &size);
if (keyfile == NULL)
return False;
if (size != sizeof(struct afs_keyfile)) {
SAFE_FREE(keyfile);
return False;
}
i = ntohl(keyfile->nkeys);
if (i > SECRETS_AFS_MAXKEYS) {
SAFE_FREE(keyfile);
return False;
}
*result = keyfile->entry[i-1];
result->kvno = ntohl(result->kvno);
return True;
}
/******************************************************************************
When kerberos is not available, choose between anonymous or
authenticated connections.
We need to use an authenticated connection if DCs have the
RestrictAnonymous registry entry set > 0, or the "Additional
restrictions for anonymous connections" set in the win2k Local
Security Policy.
Caller to free() result in domain, username, password
*******************************************************************************/
static void secrets_fetch_ipc_userpass(TDB_CONTEXT *tdb, char **username, char **domain, char **password)
{
*username = secrets_fetch(tdb, SECRETS_AUTH_USER, NULL);
*domain = secrets_fetch(tdb, SECRETS_AUTH_DOMAIN, NULL);
*password = secrets_fetch(tdb, SECRETS_AUTH_PASSWORD, NULL);
if (*username && **username) {
if (!*domain || !**domain)
*domain = smb_xstrdup(lp_workgroup());
if (!*password || !**password)
*password = smb_xstrdup("");
DEBUG(3, ("IPC$ connections done by user %s\\%s\n",
*domain, *username));
} else {
DEBUG(3, ("IPC$ connections done anonymously\n"));
*username = smb_xstrdup("");
*domain = smb_xstrdup("");
*password = smb_xstrdup("");
}
}
|