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/ldb/include/ldb.h"
26 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
27 #include "param/secrets.h"
28 #include "system/filesys.h"
29 #include "util/util_ldb.h"
30 #include "auth/credentials/credentials.h"
31 #include "auth/credentials/credentials_krb5.h"
32 #include "param/param.h"
35 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
37 * @param credentials Credentials structure on which to set the password
38 * @param fd open file descriptor to read the password from
39 * @param obtained This enum describes how 'specified' this password is
42 bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
43 int fd, enum credentials_obtained obtained)
48 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
49 p && p - pass < sizeof(pass);) {
50 switch (read(fd, p, 1)) {
52 if (*p != '\n' && *p != '\0') {
53 *++p = '\0'; /* advance p, and null-terminate pass */
59 *p = '\0'; /* null-terminate it, just in case... */
60 p = NULL; /* then force the loop condition to become false */
63 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
68 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
74 cli_credentials_set_password(credentials, pass, obtained);
79 * Read a named file, and parse it for a password
81 * @param credentials Credentials structure on which to set the password
82 * @param file a named file to read the password from
83 * @param obtained This enum describes how 'specified' this password is
86 bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
88 int fd = open(file, O_RDONLY, 0);
92 fprintf(stderr, "Error opening password file %s: %s\n",
93 file, strerror(errno));
97 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
105 * Read a named file, and parse it for username, domain, realm and password
107 * @param credentials Credentials structure on which to set the password
108 * @param file a named file to read the details from
109 * @param obtained This enum describes how 'specified' this password is
112 bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
115 char *ptr, *val, *param;
119 lines = file_lines_load(file, &numlines, NULL);
123 /* fail if we can't open the credentials file */
124 d_printf("ERROR: Unable to open credentials file!\n");
128 for (i = 0; i < numlines; i++) {
129 len = strlen(lines[i]);
134 /* break up the line into parameter & value.
135 * will need to eat a little whitespace possibly */
137 if (!(ptr = strchr_m (lines[i], '=')))
143 /* eat leading white space */
144 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
147 if (strwicmp("password", param) == 0) {
148 cli_credentials_set_password(cred, val, obtained);
149 } else if (strwicmp("username", param) == 0) {
150 cli_credentials_set_username(cred, val, obtained);
151 } else if (strwicmp("domain", param) == 0) {
152 cli_credentials_set_domain(cred, val, obtained);
153 } else if (strwicmp("realm", param) == 0) {
154 cli_credentials_set_realm(cred, val, obtained);
156 memset(lines[i], 0, len);
166 * Fill in credentials for the machine trust account, from the secrets database.
168 * @param cred Credentials structure to fill in
169 * @retval NTSTATUS error detailing any failure
171 NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
172 struct loadparm_context *lp_ctx,
173 struct ldb_context *ldb,
180 struct ldb_message **msgs;
181 const char *attrs[] = {
189 "msDS-KeyVersionNumber",
193 "servicePrincipalName",
197 const char *machine_account;
198 const char *password;
199 const char *old_password;
202 enum netr_SchannelType sct;
203 const char *salt_principal;
206 /* ok, we are going to get it now, don't recurse back here */
207 cred->machine_account_pending = false;
209 /* some other parts of the system will key off this */
210 cred->machine_account = true;
212 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
215 /* Local secrets are stored in secrets.ldb */
216 ldb = secrets_db_connect(mem_ctx, lp_ctx);
218 /* set anonymous as the fallback, if the machine account won't work */
219 cli_credentials_set_anonymous(cred);
220 DEBUG(1, ("Could not open secrets.ldb\n"));
221 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
225 /* search for the secret record */
226 ldb_ret = gendb_search(ldb,
227 mem_ctx, ldb_dn_new(mem_ctx, ldb, base),
231 DEBUG(1, ("Could not find entry to match filter: '%s' base: '%s'\n",
233 /* set anonymous as the fallback, if the machine account won't work */
234 cli_credentials_set_anonymous(cred);
235 talloc_free(mem_ctx);
236 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
237 } else if (ldb_ret != 1) {
238 DEBUG(1, ("Found more than one (%d) entry to match filter: '%s' base: '%s'\n",
239 ldb_ret, filter, base));
240 /* set anonymous as the fallback, if the machine account won't work */
241 cli_credentials_set_anonymous(cred);
242 talloc_free(mem_ctx);
243 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
246 password = ldb_msg_find_attr_as_string(msgs[0], "secret", NULL);
247 old_password = ldb_msg_find_attr_as_string(msgs[0], "priorSecret", NULL);
249 machine_account = ldb_msg_find_attr_as_string(msgs[0], "samAccountName", NULL);
251 if (!machine_account) {
252 machine_account = ldb_msg_find_attr_as_string(msgs[0], "servicePrincipalName", NULL);
254 if (!machine_account) {
255 DEBUG(1, ("Could not find 'samAccountName' in join record to domain: %s: filter: '%s' base: '%s'\n",
256 cli_credentials_get_domain(cred), filter, base));
257 /* set anonymous as the fallback, if the machine account won't work */
258 cli_credentials_set_anonymous(cred);
259 talloc_free(mem_ctx);
260 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
264 salt_principal = ldb_msg_find_attr_as_string(msgs[0], "saltPrincipal", NULL);
265 cli_credentials_set_salt_principal(cred, salt_principal);
267 sct = ldb_msg_find_attr_as_int(msgs[0], "secureChannelType", 0);
269 cli_credentials_set_secure_channel_type(cred, sct);
273 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msgs[0], "unicodePwd");
274 struct samr_Password hash;
276 if (nt_password_hash) {
277 memcpy(hash.hash, nt_password_hash->data,
278 MIN(nt_password_hash->length, sizeof(hash.hash)));
280 cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
282 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
285 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
289 domain = ldb_msg_find_attr_as_string(msgs[0], "flatname", NULL);
291 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
294 realm = ldb_msg_find_attr_as_string(msgs[0], "realm", NULL);
296 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
299 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
301 cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msgs[0], "msDS-KeyVersionNumber", 0));
303 /* If there was an external keytab specified by reference in
304 * the LDB, then use this. Otherwise we will make one up
305 * (chewing CPU time) from the password */
306 keytab = ldb_msg_find_attr_as_string(msgs[0], "krb5Keytab", NULL);
308 cli_credentials_set_keytab_name(cred, keytab, CRED_SPECIFIED);
310 keytab = ldb_msg_find_attr_as_string(msgs[0], "privateKeytab", NULL);
312 keytab = talloc_asprintf(mem_ctx, "FILE:%s", private_path(mem_ctx, lp_ctx, keytab));
314 cli_credentials_set_keytab_name(cred, keytab, CRED_SPECIFIED);
318 talloc_free(mem_ctx);
324 * Fill in credentials for the machine trust account, from the secrets database.
326 * @param cred Credentials structure to fill in
327 * @retval NTSTATUS error detailing any failure
329 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred)
332 /* Bleh, nasty recursion issues: We are setting a machine
333 * account here, so we don't want the 'pending' flag around
335 cred->machine_account_pending = false;
336 filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER,
337 cli_credentials_get_domain(cred));
338 return cli_credentials_set_secrets(cred, global_loadparm, NULL,
339 SECRETS_PRIMARY_DOMAIN_DN,
344 * Fill in credentials for the machine trust account, from the secrets database.
346 * @param cred Credentials structure to fill in
347 * @retval NTSTATUS error detailing any failure
349 NTSTATUS cli_credentials_set_krbtgt(struct cli_credentials *cred)
352 /* Bleh, nasty recursion issues: We are setting a machine
353 * account here, so we don't want the 'pending' flag around
355 cred->machine_account_pending = false;
356 filter = talloc_asprintf(cred, SECRETS_KRBTGT_SEARCH,
357 cli_credentials_get_realm(cred),
358 cli_credentials_get_domain(cred));
359 return cli_credentials_set_secrets(cred, global_loadparm, NULL,
360 SECRETS_PRINCIPALS_DN,
365 * Fill in credentials for the machine trust account, from the secrets database.
367 * @param cred Credentials structure to fill in
368 * @retval NTSTATUS error detailing any failure
370 NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
371 const char *serviceprincipal)
374 /* Bleh, nasty recursion issues: We are setting a machine
375 * account here, so we don't want the 'pending' flag around
377 cred->machine_account_pending = false;
378 filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
379 cli_credentials_get_realm(cred),
380 cli_credentials_get_domain(cred),
382 return cli_credentials_set_secrets(cred, global_loadparm, NULL,
383 SECRETS_PRINCIPALS_DN, filter);
387 * Ask that when required, the credentials system will be filled with
388 * machine trust account, from the secrets database.
390 * @param cred Credentials structure to fill in
391 * @note This function is used to call the above function after, rather
392 * than during, popt processing.
395 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred)
397 cred->machine_account_pending = true;