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 2 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, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_samr.h" /* for struct samrPassword */
28 #include "include/secrets.h"
29 #include "system/filesys.h"
32 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
34 * @param credentials Credentials structure on which to set the password
35 * @param fd open file descriptor to read the password from
36 * @param obtained This enum describes how 'specified' this password is
39 BOOL cli_credentials_parse_password_fd(struct cli_credentials *credentials,
40 int fd, enum credentials_obtained obtained)
45 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
46 p && p - pass < sizeof(pass);) {
47 switch (read(fd, p, 1)) {
49 if (*p != '\n' && *p != '\0') {
50 *++p = '\0'; /* advance p, and null-terminate pass */
55 *p = '\0'; /* null-terminate it, just in case... */
56 p = NULL; /* then force the loop condition to become false */
59 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
64 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
70 cli_credentials_set_password(credentials, pass, obtained);
75 * Read a named file, and parse it for a password
77 * @param credentials Credentials structure on which to set the password
78 * @param file a named file to read the password from
79 * @param obtained This enum describes how 'specified' this password is
82 BOOL cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
84 int fd = open(file, O_RDONLY, 0);
88 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
89 file, strerror(errno));
93 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
101 * Read a named file, and parse it for username, domain, realm and password
103 * @param credentials Credentials structure on which to set the password
104 * @param file a named file to read the details from
105 * @param obtained This enum describes how 'specified' this password is
108 BOOL cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
111 char *ptr, *val, *param;
115 lines = file_lines_load(file, &numlines, NULL);
119 /* fail if we can't open the credentials file */
120 d_printf("ERROR: Unable to open credentials file!\n");
124 for (i = 0; i < numlines; i++) {
125 len = strlen(lines[i]);
130 /* break up the line into parameter & value.
131 * will need to eat a little whitespace possibly */
133 if (!(ptr = strchr_m (lines[i], '=')))
139 /* eat leading white space */
140 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
143 if (strwicmp("password", param) == 0) {
144 cli_credentials_set_password(cred, val, obtained);
145 } else if (strwicmp("username", param) == 0) {
146 cli_credentials_set_username(cred, val, obtained);
147 } else if (strwicmp("domain", param) == 0) {
148 cli_credentials_set_domain(cred, val, obtained);
149 } else if (strwicmp("realm", param) == 0) {
150 cli_credentials_set_realm(cred, val, obtained);
152 memset(lines[i], 0, len);
162 * Fill in credentials for the machine trust account, from the secrets database.
164 * @param cred Credentials structure to fill in
165 * @retval NTSTATUS error detailing any failure
167 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred)
171 struct ldb_context *ldb;
173 struct ldb_message **msgs;
174 const char *attrs[] = {
181 "msDS-KeyVersionNumber",
185 const char *machine_account;
186 const char *password;
189 enum netr_SchannelType sct;
191 /* ok, we are going to get it now, don't recurse back here */
192 cred->machine_account_pending = False;
194 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
195 /* Local secrets are stored in secrets.ldb */
196 ldb = secrets_db_connect(mem_ctx);
198 DEBUG(1, ("Could not open secrets.ldb\n"));
199 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
202 /* search for the secret record */
203 ldb_ret = gendb_search(ldb,
204 mem_ctx, ldb_dn_explode(mem_ctx, SECRETS_PRIMARY_DOMAIN_DN),
206 SECRETS_PRIMARY_DOMAIN_FILTER,
207 cli_credentials_get_domain(cred));
209 DEBUG(1, ("Could not find join record to domain: %s\n",
210 cli_credentials_get_domain(cred)));
211 talloc_free(mem_ctx);
212 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
213 } else if (ldb_ret != 1) {
214 DEBUG(1, ("Found more than one (%d) join records to domain: %s\n",
215 ldb_ret, cli_credentials_get_domain(cred)));
216 talloc_free(mem_ctx);
217 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
220 password = ldb_msg_find_string(msgs[0], "secret", NULL);
222 machine_account = ldb_msg_find_string(msgs[0], "samAccountName", NULL);
224 if (!machine_account) {
225 DEBUG(1, ("Could not find 'samAccountName' in join record to domain: %s\n",
226 cli_credentials_get_domain(cred)));
227 talloc_free(mem_ctx);
228 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
231 sct = ldb_msg_find_int(msgs[0], "secureChannelType", 0);
233 DEBUG(1, ("Domain join for acocunt %s did not have a secureChannelType set!\n",
235 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
239 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msgs[0], "ntPwdHash");
240 struct samr_Password hash;
242 if (nt_password_hash) {
243 memcpy(hash.hash, nt_password_hash->data,
244 MIN(nt_password_hash->length, sizeof(hash.hash)));
246 cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
249 DEBUG(1, ("Could not find 'secret' in join record to domain: %s\n",
250 cli_credentials_get_domain(cred)));
251 talloc_free(mem_ctx);
252 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
256 cli_credentials_set_secure_channel_type(cred, sct);
258 domain = ldb_msg_find_string(msgs[0], "flatname", NULL);
260 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
263 realm = ldb_msg_find_string(msgs[0], "realm", NULL);
265 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
268 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
270 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
273 cli_credentials_set_kvno(cred, ldb_msg_find_int(msgs[0], "msDS-KeyVersionNumber", 0));
275 talloc_free(mem_ctx);
281 * Ask that when required, the credentials system will be filled with
282 * machine trust account, from the secrets database.
284 * @param cred Credentials structure to fill in
285 * @note This function is used to call the above function after, rather
286 * than during, popt processing.
289 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred)
291 cred->machine_account_pending = True;