2 Unix SMB/CIFS implementation.
4 User credentials handling (as regards on-disk files)
6 Copyright (C) Jelmer Vernooij 2005
7 Copyright (C) Tim Potter 2001
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/events/events.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
28 #include "param/secrets.h"
29 #include "system/filesys.h"
30 #include "../lib/util/util_ldb.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/credentials/credentials_krb5.h"
33 #include "param/param.h"
34 #include "lib/events/events.h"
37 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
39 * @param credentials Credentials structure on which to set the password
40 * @param fd open file descriptor to read the password from
41 * @param obtained This enum describes how 'specified' this password is
44 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
45 int fd, enum credentials_obtained obtained)
50 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
51 p && p - pass < sizeof(pass);) {
52 switch (read(fd, p, 1)) {
54 if (*p != '\n' && *p != '\0') {
55 *++p = '\0'; /* advance p, and null-terminate pass */
61 *p = '\0'; /* null-terminate it, just in case... */
62 p = NULL; /* then force the loop condition to become false */
65 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
70 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
76 cli_credentials_set_password(credentials, pass, obtained);
81 * Read a named file, and parse it for a password
83 * @param credentials Credentials structure on which to set the password
84 * @param file a named file to read the password from
85 * @param obtained This enum describes how 'specified' this password is
88 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
90 int fd = open(file, O_RDONLY, 0);
94 fprintf(stderr, "Error opening password file %s: %s\n",
95 file, strerror(errno));
99 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
107 * Read a named file, and parse it for username, domain, realm and password
109 * @param credentials Credentials structure on which to set the password
110 * @param file a named file to read the details from
111 * @param obtained This enum describes how 'specified' this password is
114 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
117 char *ptr, *val, *param;
121 lines = file_lines_load(file, &numlines, 0, NULL);
125 /* fail if we can't open the credentials file */
126 d_printf("ERROR: Unable to open credentials file!\n");
130 for (i = 0; i < numlines; i++) {
131 len = strlen(lines[i]);
136 /* break up the line into parameter & value.
137 * will need to eat a little whitespace possibly */
139 if (!(ptr = strchr_m (lines[i], '=')))
145 /* eat leading white space */
146 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
149 if (strwicmp("password", param) == 0) {
150 cli_credentials_set_password(cred, val, obtained);
151 } else if (strwicmp("username", param) == 0) {
152 cli_credentials_set_username(cred, val, obtained);
153 } else if (strwicmp("domain", param) == 0) {
154 cli_credentials_set_domain(cred, val, obtained);
155 } else if (strwicmp("realm", param) == 0) {
156 cli_credentials_set_realm(cred, val, obtained);
158 memset(lines[i], 0, len);
168 * Fill in credentials for the machine trust account, from the secrets database.
170 * @param cred Credentials structure to fill in
171 * @retval NTSTATUS error detailing any failure
173 _PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
174 struct event_context *event_ctx,
175 struct loadparm_context *lp_ctx,
176 struct ldb_context *ldb,
183 struct ldb_message **msgs;
184 const char *attrs[] = {
192 "msDS-KeyVersionNumber",
196 "servicePrincipalName",
201 const char *machine_account;
202 const char *password;
203 const char *old_password;
206 enum netr_SchannelType sct;
207 const char *salt_principal;
210 /* ok, we are going to get it now, don't recurse back here */
211 cred->machine_account_pending = false;
213 /* some other parts of the system will key off this */
214 cred->machine_account = true;
216 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
219 /* Local secrets are stored in secrets.ldb */
220 ldb = secrets_db_connect(mem_ctx, event_ctx, lp_ctx);
222 /* set anonymous as the fallback, if the machine account won't work */
223 cli_credentials_set_anonymous(cred);
224 DEBUG(1, ("Could not open secrets.ldb\n"));
225 talloc_free(mem_ctx);
226 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
230 /* search for the secret record */
231 ldb_ret = gendb_search(ldb,
232 mem_ctx, ldb_dn_new(mem_ctx, ldb, base),
236 DEBUG(5, ("(normal if no LDAP backend required) Could not find entry to match filter: '%s' base: '%s'\n",
238 /* set anonymous as the fallback, if the machine account won't work */
239 cli_credentials_set_anonymous(cred);
240 talloc_free(mem_ctx);
241 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
242 } else if (ldb_ret != 1) {
243 DEBUG(5, ("Found more than one (%d) entry to match filter: '%s' base: '%s'\n",
244 ldb_ret, filter, base));
245 /* set anonymous as the fallback, if the machine account won't work */
246 cli_credentials_set_anonymous(cred);
247 talloc_free(mem_ctx);
248 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
251 password = ldb_msg_find_attr_as_string(msgs[0], "secret", NULL);
252 old_password = ldb_msg_find_attr_as_string(msgs[0], "priorSecret", NULL);
254 machine_account = ldb_msg_find_attr_as_string(msgs[0], "samAccountName", NULL);
256 if (!machine_account) {
257 machine_account = ldb_msg_find_attr_as_string(msgs[0], "servicePrincipalName", NULL);
259 if (!machine_account) {
260 const char *ldap_bind_dn = ldb_msg_find_attr_as_string(msgs[0], "ldapBindDn", NULL);
262 DEBUG(1, ("Could not find 'samAccountName', 'servicePrincipalName' or 'ldapBindDn' in secrets record: filter: '%s' base: '%s'\n",
264 /* set anonymous as the fallback, if the machine account won't work */
265 cli_credentials_set_anonymous(cred);
266 talloc_free(mem_ctx);
267 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
272 salt_principal = ldb_msg_find_attr_as_string(msgs[0], "saltPrincipal", NULL);
273 cli_credentials_set_salt_principal(cred, salt_principal);
275 sct = ldb_msg_find_attr_as_int(msgs[0], "secureChannelType", 0);
277 cli_credentials_set_secure_channel_type(cred, sct);
281 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msgs[0], "unicodePwd");
282 struct samr_Password hash;
284 if (nt_password_hash) {
285 memcpy(hash.hash, nt_password_hash->data,
286 MIN(nt_password_hash->length, sizeof(hash.hash)));
288 cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
290 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
293 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
297 domain = ldb_msg_find_attr_as_string(msgs[0], "flatname", NULL);
299 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
302 realm = ldb_msg_find_attr_as_string(msgs[0], "realm", NULL);
304 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
307 if (machine_account) {
308 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
311 cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msgs[0], "msDS-KeyVersionNumber", 0));
313 /* If there was an external keytab specified by reference in
314 * the LDB, then use this. Otherwise we will make one up
315 * (chewing CPU time) from the password */
316 keytab = ldb_msg_find_attr_as_string(msgs[0], "krb5Keytab", NULL);
318 cli_credentials_set_keytab_name(cred, event_ctx, lp_ctx, keytab, CRED_SPECIFIED);
320 keytab = ldb_msg_find_attr_as_string(msgs[0], "privateKeytab", NULL);
322 keytab = talloc_asprintf(mem_ctx, "FILE:%s", private_path(mem_ctx, lp_ctx, keytab));
324 cli_credentials_set_keytab_name(cred, event_ctx, lp_ctx, keytab, CRED_SPECIFIED);
328 talloc_free(mem_ctx);
334 * Fill in credentials for the machine trust account, from the secrets database.
336 * @param cred Credentials structure to fill in
337 * @retval NTSTATUS error detailing any failure
339 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
340 struct loadparm_context *lp_ctx)
344 /* Bleh, nasty recursion issues: We are setting a machine
345 * account here, so we don't want the 'pending' flag around
347 cred->machine_account_pending = false;
348 filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER,
349 cli_credentials_get_domain(cred));
350 status = cli_credentials_set_secrets(cred, event_context_find(cred), lp_ctx, NULL,
351 SECRETS_PRIMARY_DOMAIN_DN,
353 if (!NT_STATUS_IS_OK(status)) {
354 DEBUG(1, ("Could not find machine account in secrets database: %s", nt_errstr(status)));
360 * Fill in credentials for the machine trust account, from the secrets database.
362 * @param cred Credentials structure to fill in
363 * @retval NTSTATUS error detailing any failure
365 NTSTATUS cli_credentials_set_krbtgt(struct cli_credentials *cred,
366 struct event_context *event_ctx,
367 struct loadparm_context *lp_ctx)
371 /* Bleh, nasty recursion issues: We are setting a machine
372 * account here, so we don't want the 'pending' flag around
374 cred->machine_account_pending = false;
375 filter = talloc_asprintf(cred, SECRETS_KRBTGT_SEARCH,
376 cli_credentials_get_realm(cred),
377 cli_credentials_get_domain(cred));
378 status = cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL,
379 SECRETS_PRINCIPALS_DN,
381 if (!NT_STATUS_IS_OK(status)) {
382 DEBUG(1, ("Could not find krbtgt (master Kerberos) account in secrets database: %s", nt_errstr(status)));
388 * Fill in credentials for a particular prinicpal, from the secrets database.
390 * @param cred Credentials structure to fill in
391 * @retval NTSTATUS error detailing any failure
393 _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
394 struct event_context *event_ctx,
395 struct loadparm_context *lp_ctx,
396 const char *serviceprincipal)
400 /* Bleh, nasty recursion issues: We are setting a machine
401 * account here, so we don't want the 'pending' flag around
403 cred->machine_account_pending = false;
404 filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
405 cli_credentials_get_realm(cred),
406 cli_credentials_get_domain(cred),
408 status = cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL,
409 SECRETS_PRINCIPALS_DN, filter);
410 if (!NT_STATUS_IS_OK(status)) {
411 DEBUG(1, ("Could not find %s principal in secrets database: %s", serviceprincipal, nt_errstr(status)));
417 * Ask that when required, the credentials system will be filled with
418 * machine trust account, from the secrets database.
420 * @param cred Credentials structure to fill in
421 * @note This function is used to call the above function after, rather
422 * than during, popt processing.
425 _PUBLIC_ void cli_credentials_set_machine_account_pending(struct cli_credentials *cred,
426 struct loadparm_context *lp_ctx)
428 cred->machine_account_pending = true;
429 cred->machine_account_pending_lp_ctx = lp_ctx;