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"
30 #include "auth/credentials/credentials.h"
31 #include "auth/credentials/credentials_krb5.h"
34 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
36 * @param credentials Credentials structure on which to set the password
37 * @param fd open file descriptor to read the password from
38 * @param obtained This enum describes how 'specified' this password is
41 BOOL cli_credentials_parse_password_fd(struct cli_credentials *credentials,
42 int fd, enum credentials_obtained obtained)
47 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
48 p && p - pass < sizeof(pass);) {
49 switch (read(fd, p, 1)) {
51 if (*p != '\n' && *p != '\0') {
52 *++p = '\0'; /* advance p, and null-terminate pass */
58 *p = '\0'; /* null-terminate it, just in case... */
59 p = NULL; /* then force the loop condition to become false */
62 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
67 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
73 cli_credentials_set_password(credentials, pass, obtained);
78 * Read a named file, and parse it for a password
80 * @param credentials Credentials structure on which to set the password
81 * @param file a named file to read the password from
82 * @param obtained This enum describes how 'specified' this password is
85 BOOL cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
87 int fd = open(file, O_RDONLY, 0);
91 fprintf(stderr, "Error opening password file %s: %s\n",
92 file, strerror(errno));
96 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
104 * Read a named file, and parse it for username, domain, realm and password
106 * @param credentials Credentials structure on which to set the password
107 * @param file a named file to read the details from
108 * @param obtained This enum describes how 'specified' this password is
111 BOOL cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
114 char *ptr, *val, *param;
118 lines = file_lines_load(file, &numlines, NULL);
122 /* fail if we can't open the credentials file */
123 d_printf("ERROR: Unable to open credentials file!\n");
127 for (i = 0; i < numlines; i++) {
128 len = strlen(lines[i]);
133 /* break up the line into parameter & value.
134 * will need to eat a little whitespace possibly */
136 if (!(ptr = strchr_m (lines[i], '=')))
142 /* eat leading white space */
143 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
146 if (strwicmp("password", param) == 0) {
147 cli_credentials_set_password(cred, val, obtained);
148 } else if (strwicmp("username", param) == 0) {
149 cli_credentials_set_username(cred, val, obtained);
150 } else if (strwicmp("domain", param) == 0) {
151 cli_credentials_set_domain(cred, val, obtained);
152 } else if (strwicmp("realm", param) == 0) {
153 cli_credentials_set_realm(cred, val, obtained);
155 memset(lines[i], 0, len);
165 * Fill in credentials for the machine trust account, from the secrets database.
167 * @param cred Credentials structure to fill in
168 * @retval NTSTATUS error detailing any failure
170 NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred,
171 struct ldb_context *ldb,
178 struct ldb_message **msgs;
179 const char *attrs[] = {
187 "msDS-KeyVersionNumber",
191 "servicePrincipalName",
195 const char *machine_account;
196 const char *password;
197 const char *old_password;
200 enum netr_SchannelType sct;
201 const char *salt_principal;
204 /* ok, we are going to get it now, don't recurse back here */
205 cred->machine_account_pending = False;
207 /* some other parts of the system will key off this */
208 cred->machine_account = True;
210 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
213 /* Local secrets are stored in secrets.ldb */
214 ldb = secrets_db_connect(mem_ctx);
216 /* set anonymous as the fallback, if the machine account won't work */
217 cli_credentials_set_anonymous(cred);
218 DEBUG(1, ("Could not open secrets.ldb\n"));
219 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
223 /* search for the secret record */
224 ldb_ret = gendb_search(ldb,
225 mem_ctx, ldb_dn_new(mem_ctx, ldb, base),
229 DEBUG(1, ("Could not find entry to match filter: '%s' base: '%s'\n",
231 /* set anonymous as the fallback, if the machine account won't work */
232 cli_credentials_set_anonymous(cred);
233 talloc_free(mem_ctx);
234 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
235 } else if (ldb_ret != 1) {
236 DEBUG(1, ("Found more than one (%d) entry to match filter: '%s' base: '%s'\n",
237 ldb_ret, filter, base));
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;
244 password = ldb_msg_find_attr_as_string(msgs[0], "secret", NULL);
245 old_password = ldb_msg_find_attr_as_string(msgs[0], "priorSecret", NULL);
247 machine_account = ldb_msg_find_attr_as_string(msgs[0], "samAccountName", NULL);
249 if (!machine_account) {
250 machine_account = ldb_msg_find_attr_as_string(msgs[0], "servicePrincipalName", NULL);
252 if (!machine_account) {
253 DEBUG(1, ("Could not find 'samAccountName' in join record to domain: %s: filter: '%s' base: '%s'\n",
254 cli_credentials_get_domain(cred), filter, base));
255 /* set anonymous as the fallback, if the machine account won't work */
256 cli_credentials_set_anonymous(cred);
257 talloc_free(mem_ctx);
258 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
262 salt_principal = ldb_msg_find_attr_as_string(msgs[0], "saltPrincipal", NULL);
263 cli_credentials_set_salt_principal(cred, salt_principal);
265 sct = ldb_msg_find_attr_as_int(msgs[0], "secureChannelType", 0);
267 cli_credentials_set_secure_channel_type(cred, sct);
271 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msgs[0], "unicodePwd");
272 struct samr_Password hash;
274 if (nt_password_hash) {
275 memcpy(hash.hash, nt_password_hash->data,
276 MIN(nt_password_hash->length, sizeof(hash.hash)));
278 cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
280 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
283 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
287 domain = ldb_msg_find_attr_as_string(msgs[0], "flatname", NULL);
289 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
292 realm = ldb_msg_find_attr_as_string(msgs[0], "realm", NULL);
294 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
297 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
299 cli_credentials_set_kvno(cred, ldb_msg_find_attr_as_int(msgs[0], "msDS-KeyVersionNumber", 0));
301 /* If there was an external keytab specified by reference in
302 * the LDB, then use this. Otherwise we will make one up
303 * (chewing CPU time) from the password */
304 keytab = ldb_msg_find_attr_as_string(msgs[0], "krb5Keytab", NULL);
306 cli_credentials_set_keytab_name(cred, keytab, CRED_SPECIFIED);
308 keytab = ldb_msg_find_attr_as_string(msgs[0], "privateKeytab", NULL);
310 keytab = talloc_asprintf(mem_ctx, "FILE:%s", private_path(mem_ctx, keytab));
312 cli_credentials_set_keytab_name(cred, keytab, CRED_SPECIFIED);
316 talloc_free(mem_ctx);
322 * Fill in credentials for the machine trust account, from the secrets database.
324 * @param cred Credentials structure to fill in
325 * @retval NTSTATUS error detailing any failure
327 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred)
330 /* Bleh, nasty recursion issues: We are setting a machine
331 * account here, so we don't want the 'pending' flag around
333 cred->machine_account_pending = False;
334 filter = talloc_asprintf(cred, SECRETS_PRIMARY_DOMAIN_FILTER,
335 cli_credentials_get_domain(cred));
336 return cli_credentials_set_secrets(cred, NULL, SECRETS_PRIMARY_DOMAIN_DN,
341 * Fill in credentials for the machine trust account, from the secrets database.
343 * @param cred Credentials structure to fill in
344 * @retval NTSTATUS error detailing any failure
346 NTSTATUS cli_credentials_set_krbtgt(struct cli_credentials *cred)
349 /* Bleh, nasty recursion issues: We are setting a machine
350 * account here, so we don't want the 'pending' flag around
352 cred->machine_account_pending = False;
353 filter = talloc_asprintf(cred, SECRETS_KRBTGT_SEARCH,
354 cli_credentials_get_realm(cred),
355 cli_credentials_get_domain(cred));
356 return cli_credentials_set_secrets(cred, NULL, SECRETS_PRINCIPALS_DN,
361 * Fill in credentials for the machine trust account, from the secrets database.
363 * @param cred Credentials structure to fill in
364 * @retval NTSTATUS error detailing any failure
366 NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
367 const char *serviceprincipal)
370 /* Bleh, nasty recursion issues: We are setting a machine
371 * account here, so we don't want the 'pending' flag around
373 cred->machine_account_pending = False;
374 filter = talloc_asprintf(cred, SECRETS_PRINCIPAL_SEARCH,
375 cli_credentials_get_realm(cred),
376 cli_credentials_get_domain(cred),
378 return cli_credentials_set_secrets(cred, NULL, SECRETS_PRINCIPALS_DN,
383 * Ask that when required, the credentials system will be filled with
384 * machine trust account, from the secrets database.
386 * @param cred Credentials structure to fill in
387 * @note This function is used to call the above function after, rather
388 * than during, popt processing.
391 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred)
393 cred->machine_account_pending = True;