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 "auth/credentials/credentials_proto.h"
34 #include "param/param.h"
35 #include "lib/events/events.h"
36 #include "dsdb/samdb/samdb.h"
40 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
42 * @param credentials Credentials structure on which to set the password
43 * @param fd open file descriptor to read the password from
44 * @param obtained This enum describes how 'specified' this password is
47 _PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
48 int fd, enum credentials_obtained obtained)
53 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
54 p && p - pass < sizeof(pass);) {
55 switch (read(fd, p, 1)) {
57 if (*p != '\n' && *p != '\0') {
58 *++p = '\0'; /* advance p, and null-terminate pass */
64 *p = '\0'; /* null-terminate it, just in case... */
65 p = NULL; /* then force the loop condition to become false */
68 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
73 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
79 cli_credentials_set_password(credentials, pass, obtained);
84 * Read a named file, and parse it for a password
86 * @param credentials Credentials structure on which to set the password
87 * @param file a named file to read the password from
88 * @param obtained This enum describes how 'specified' this password is
91 _PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
93 int fd = open(file, O_RDONLY, 0);
97 fprintf(stderr, "Error opening password file %s: %s\n",
98 file, strerror(errno));
102 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
110 * Read a named file, and parse it for username, domain, realm and password
112 * @param credentials Credentials structure on which to set the password
113 * @param file a named file to read the details from
114 * @param obtained This enum describes how 'specified' this password is
117 _PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
120 char *ptr, *val, *param;
124 lines = file_lines_load(file, &numlines, 0, NULL);
128 /* fail if we can't open the credentials file */
129 d_printf("ERROR: Unable to open credentials file!\n");
133 for (i = 0; i < numlines; i++) {
134 len = strlen(lines[i]);
139 /* break up the line into parameter & value.
140 * will need to eat a little whitespace possibly */
142 if (!(ptr = strchr_m (lines[i], '=')))
148 /* eat leading white space */
149 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
152 if (strwicmp("password", param) == 0) {
153 cli_credentials_set_password(cred, val, obtained);
154 } else if (strwicmp("username", param) == 0) {
155 cli_credentials_set_username(cred, val, obtained);
156 } else if (strwicmp("domain", param) == 0) {
157 cli_credentials_set_domain(cred, val, obtained);
158 } else if (strwicmp("realm", param) == 0) {
159 cli_credentials_set_realm(cred, val, obtained);
161 memset(lines[i], 0, len);
171 * Fill in credentials for the machine trust account, from the secrets database.
173 * @param cred Credentials structure to fill in
174 * @retval NTSTATUS error detailing any failure
176 _PUBLIC_ NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
177 struct tevent_context *event_ctx,
178 struct loadparm_context *lp_ctx,
179 struct ldb_context *ldb,
187 struct ldb_message *msg;
188 const char *attrs[] = {
196 "msDS-KeyVersionNumber",
200 "servicePrincipalName",
205 const char *machine_account;
206 const char *password;
207 const char *old_password;
210 enum netr_SchannelType sct;
211 const char *salt_principal;
213 const struct ldb_val *whenChanged;
215 /* ok, we are going to get it now, don't recurse back here */
216 cred->machine_account_pending = false;
218 /* some other parts of the system will key off this */
219 cred->machine_account = true;
221 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
224 /* Local secrets are stored in secrets.ldb */
225 ldb = secrets_db_connect(mem_ctx, event_ctx, lp_ctx);
227 /* set anonymous as the fallback, if the machine account won't work */
228 cli_credentials_set_anonymous(cred);
229 *error_string = talloc_strdup(cred, "Could not open secrets.ldb");
230 talloc_free(mem_ctx);
231 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
235 ldb_ret = dsdb_search_one(ldb, ldb, &msg,
236 ldb_dn_new(mem_ctx, ldb, base),
238 attrs, 0, "%s", filter);
240 if (ldb_ret != LDB_SUCCESS) {
241 *error_string = talloc_asprintf(cred, "Could not find entry to match filter: '%s' base: '%s': %s: %s\n",
242 filter, base ? base : "",
243 ldb_strerror(ldb_ret), ldb_errstring(ldb));
244 /* set anonymous as the fallback, if the machine account won't work */
245 cli_credentials_set_anonymous(cred);
246 talloc_free(mem_ctx);
247 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
250 password = ldb_msg_find_attr_as_string(msg, "secret", NULL);
251 old_password = ldb_msg_find_attr_as_string(msg, "priorSecret", NULL);
253 machine_account = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
255 if (!machine_account) {
256 machine_account = ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL);
258 if (!machine_account) {
259 const char *ldap_bind_dn = ldb_msg_find_attr_as_string(msg, "ldapBindDn", NULL);
261 *error_string = talloc_asprintf(cred,
262 "Could not find 'samAccountName', "
263 "'servicePrincipalName' or "
264 "'ldapBindDn' in secrets record: %s",
265 ldb_dn_get_linearized(msg->dn));
266 /* set anonymous as the fallback, if the machine account won't work */
267 cli_credentials_set_anonymous(cred);
268 talloc_free(mem_ctx);
269 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
271 /* store bind dn in credentials */
272 cli_credentials_set_bind_dn(cred, ldap_bind_dn);
277 salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
278 cli_credentials_set_salt_principal(cred, salt_principal);
280 sct = ldb_msg_find_attr_as_int(msg, "secureChannelType", 0);
282 cli_credentials_set_secure_channel_type(cred, sct);
286 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msg, "unicodePwd");
287 struct samr_Password hash;
289 if (nt_password_hash) {
290 memcpy(hash.hash, nt_password_hash->data,
291 MIN(nt_password_hash->length, sizeof(hash.hash)));
293 cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
295 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
298 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
302 domain = ldb_msg_find_attr_as_string(msg, "flatname", NULL);
304 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
307 realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
309 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
312 if (machine_account) {
313 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
316 cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0));
318 whenChanged = ldb_msg_find_ldb_val(msg, "whenChanged");
321 if (ldb_val_to_time(whenChanged, &lct) == LDB_SUCCESS) {
322 cli_credentials_set_password_last_changed_time(cred, lct);
326 /* If there was an external keytab specified by reference in
327 * the LDB, then use this. Otherwise we will make one up
328 * (chewing CPU time) from the password */
329 keytab = ldb_msg_find_attr_as_string(msg, "krb5Keytab", NULL);
331 cli_credentials_set_keytab_name(cred, event_ctx, lp_ctx, keytab, CRED_SPECIFIED);
333 keytab = ldb_msg_find_attr_as_string(msg, "privateKeytab", NULL);
335 keytab = talloc_asprintf(mem_ctx, "FILE:%s", samdb_relative_path(ldb, mem_ctx, keytab));
337 cli_credentials_set_keytab_name(cred, event_ctx, lp_ctx, keytab, CRED_SPECIFIED);
341 talloc_free(mem_ctx);
347 * Fill in credentials for the machine trust account, from the secrets database.
349 * @param cred Credentials structure to fill in
350 * @retval NTSTATUS error detailing any failure
352 _PUBLIC_ NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
353 struct loadparm_context *lp_ctx)
358 /* Bleh, nasty recursion issues: We are setting a machine
359 * account here, so we don't want the 'pending' flag around
361 cred->machine_account_pending = false;
362 filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER,
363 cli_credentials_get_domain(cred));
364 status = cli_credentials_set_secrets(cred, event_context_find(cred), lp_ctx, NULL,
365 SECRETS_PRIMARY_DOMAIN_DN,
366 filter, &error_string);
367 if (!NT_STATUS_IS_OK(status)) {
368 DEBUG(1, ("Could not find machine account in secrets database: %s: %s", nt_errstr(status), error_string));
369 talloc_free(error_string);
375 * Fill in credentials for the machine trust account, from the secrets database.
377 * @param cred Credentials structure to fill in
378 * @retval NTSTATUS error detailing any failure
380 NTSTATUS cli_credentials_set_krbtgt(struct cli_credentials *cred,
381 struct tevent_context *event_ctx,
382 struct loadparm_context *lp_ctx)
387 /* Bleh, nasty recursion issues: We are setting a machine
388 * account here, so we don't want the 'pending' flag around
390 cred->machine_account_pending = false;
391 filter = talloc_asprintf(cred, SECRETS_KRBTGT_SEARCH,
392 cli_credentials_get_realm(cred),
393 cli_credentials_get_domain(cred));
394 status = cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL,
395 SECRETS_PRINCIPALS_DN,
396 filter, &error_string);
397 if (!NT_STATUS_IS_OK(status)) {
398 DEBUG(1, ("Could not find krbtgt (master Kerberos) account in secrets database: %s: %s", nt_errstr(status), error_string));
399 talloc_free(error_string);
405 * Fill in credentials for a particular prinicpal, from the secrets database.
407 * @param cred Credentials structure to fill in
408 * @retval NTSTATUS error detailing any failure
410 _PUBLIC_ NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
411 struct tevent_context *event_ctx,
412 struct loadparm_context *lp_ctx,
413 const char *serviceprincipal)
418 /* Bleh, nasty recursion issues: We are setting a machine
419 * account here, so we don't want the 'pending' flag around
421 cred->machine_account_pending = false;
422 filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
423 cli_credentials_get_realm(cred),
424 cli_credentials_get_domain(cred),
426 status = cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL,
427 SECRETS_PRINCIPALS_DN, filter,
429 if (!NT_STATUS_IS_OK(status)) {
430 DEBUG(1, ("Could not find %s principal in secrets database: %s: %s", serviceprincipal, nt_errstr(status), error_string));
436 * Ask that when required, the credentials system will be filled with
437 * machine trust account, from the secrets database.
439 * @param cred Credentials structure to fill in
440 * @note This function is used to call the above function after, rather
441 * than during, popt processing.
444 _PUBLIC_ void cli_credentials_set_machine_account_pending(struct cli_credentials *cred,
445 struct loadparm_context *lp_ctx)
447 cred->machine_account_pending = true;
448 cred->machine_account_pending_lp_ctx = lp_ctx;