2 Unix SMB/CIFS implementation.
4 Copyright (C) Jelmer Vernooij 2005
5 Copyright (C) Tim Potter 2001
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/filesys.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "include/secrets.h"
27 #include "lib/ldb/include/ldb.h"
30 * Create a new credentials structure
31 * @param mem_ctx TALLOC_CTX parent for credentials structure
33 struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
35 struct cli_credentials *cred = talloc_zero(mem_ctx, struct cli_credentials);
44 * Obtain the username for this credentials context.
45 * @param cred credentials context
46 * @retval The username set on this context.
47 * @note Return value will never be NULL except by programmer error.
49 const char *cli_credentials_get_username(struct cli_credentials *cred)
51 if (cred->machine_account_pending) {
52 cli_credentials_set_machine_account(cred);
55 if (cred->username_obtained == CRED_CALLBACK) {
56 cred->username = cred->username_cb(cred);
57 cred->username_obtained = CRED_SPECIFIED;
60 return cred->username;
63 BOOL cli_credentials_set_username(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
65 if (obtained >= cred->username_obtained) {
66 cred->username = talloc_strdup(cred, val);
67 cred->username_obtained = obtained;
75 * Obtain the password for this credentials context.
76 * @param cred credentials context
77 * @retval If set, the cleartext password, otherwise NULL
79 const char *cli_credentials_get_password(struct cli_credentials *cred)
81 if (cred->machine_account_pending) {
82 cli_credentials_set_machine_account(cred);
85 if (cred->password_obtained == CRED_CALLBACK) {
86 cred->password = cred->password_cb(cred);
87 cred->password_obtained = CRED_SPECIFIED;
90 return cred->password;
93 BOOL cli_credentials_set_password(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
95 if (obtained >= cred->password_obtained) {
96 cred->password = talloc_strdup(cred, val);
97 cred->password_obtained = obtained;
105 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
106 * @param cred credentials context
107 * @retval The domain set on this context.
108 * @note Return value will never be NULL except by programmer error.
110 const char *cli_credentials_get_domain(struct cli_credentials *cred)
112 if (cred->machine_account_pending) {
113 cli_credentials_set_machine_account(cred);
116 if (cred->domain_obtained == CRED_CALLBACK) {
117 cred->domain = cred->domain_cb(cred);
118 cred->domain_obtained = CRED_SPECIFIED;
125 BOOL cli_credentials_set_domain(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
127 if (obtained >= cred->domain_obtained) {
128 cred->domain = talloc_strdup(cred, val);
129 cred->domain_obtained = obtained;
137 * Obtain the Kerberos realm for this credentials context.
138 * @param cred credentials context
139 * @retval The realm set on this context.
140 * @note Return value will never be NULL except by programmer error.
142 const char *cli_credentials_get_realm(struct cli_credentials *cred)
144 if (cred->machine_account_pending) {
145 cli_credentials_set_machine_account(cred);
148 if (cred->realm_obtained == CRED_CALLBACK) {
149 cred->realm = cred->realm_cb(cred);
150 cred->realm_obtained = CRED_SPECIFIED;
157 * Obtain the user's Kerberos principal for this credentials context.
158 * @param cred credentials context
159 * @param mem_ctx A talloc context to return the prinipal name on.
160 * @retval The user's Kerberos principal
161 * @note Return value may be NULL due to out-of memeory or invalid mem_ctx
163 char *cli_credentials_get_principal(struct cli_credentials *cred,
166 return talloc_asprintf(mem_ctx, "%s@%s",
167 cli_credentials_get_username(cred),
168 cli_credentials_get_realm(cred));
171 BOOL cli_credentials_set_realm(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
173 if (obtained >= cred->realm_obtained) {
174 cred->realm = talloc_strdup(cred, val);
175 cred->realm_obtained = obtained;
183 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
185 * @param cred credentials context
186 * @retval The workstation name set on this context.
187 * @note Return value will never be NULL except by programmer error.
189 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
191 if (cred->workstation_obtained == CRED_CALLBACK) {
192 cred->workstation = cred->workstation_cb(cred);
193 cred->workstation_obtained = CRED_SPECIFIED;
196 return cred->workstation;
199 BOOL cli_credentials_set_workstation(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
201 if (obtained >= cred->workstation_obtained) {
202 cred->workstation = talloc_strdup(cred, val);
203 cred->workstation_obtained = obtained;
211 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
213 * @param credentials Credentials structure on which to set the password
214 * @param fd open file descriptor to read the password from
215 * @param obtained This enum describes how 'specified' this password is
218 BOOL cli_credentials_parse_password_fd(struct cli_credentials *credentials, int fd, enum credentials_obtained obtained)
223 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
224 p && p - pass < sizeof(pass);) {
225 switch (read(fd, p, 1)) {
227 if (*p != '\n' && *p != '\0') {
228 *++p = '\0'; /* advance p, and null-terminate pass */
233 *p = '\0'; /* null-terminate it, just in case... */
234 p = NULL; /* then force the loop condition to become false */
237 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
242 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
243 fd, strerror(errno));
248 cli_credentials_set_password(credentials, pass, obtained);
253 * Read a named file, and parse it for a password
255 * @param credentials Credentials structure on which to set the password
256 * @param file a named file to read the password from
257 * @param obtained This enum describes how 'specified' this password is
260 BOOL cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
262 int fd = open(file, O_RDONLY, 0);
266 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
267 file, strerror(errno));
271 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
279 * Read a named file, and parse it for username, domain, realm and password
281 * @param credentials Credentials structure on which to set the password
282 * @param file a named file to read the details from
283 * @param obtained This enum describes how 'specified' this password is
286 BOOL cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
291 char *ptr, *val, *param;
293 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
295 /* fail if we can't open the credentials file */
296 d_printf("ERROR: Unable to open credentials file!\n");
300 while (!x_feof(auth))
302 /* get a line from the file */
303 if (!x_fgets(buf, sizeof(buf), auth))
307 if ((len) && (buf[len-1]=='\n'))
315 /* break up the line into parameter & value.
316 * will need to eat a little whitespace possibly */
318 if (!(ptr = strchr_m (buf, '=')))
324 /* eat leading white space */
325 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
328 if (strwicmp("password", param) == 0) {
329 cli_credentials_set_password(cred, val, obtained);
330 } else if (strwicmp("username", param) == 0) {
331 cli_credentials_set_username(cred, val, obtained);
332 } else if (strwicmp("domain", param) == 0) {
333 cli_credentials_set_domain(cred, val, obtained);
334 } else if (strwicmp("realm", param) == 0) {
335 cli_credentials_set_realm(cred, val, obtained);
337 memset(buf, 0, sizeof(buf));
346 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
348 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
350 * @param credentials Credentials structure on which to set the password
351 * @param data the string containing the username, password etc
352 * @param obtained This enum describes how 'specified' this password is
355 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
359 uname = talloc_strdup(credentials, data);
360 if ((p = strchr_m(uname,'%'))) {
362 cli_credentials_set_password(credentials, p+1, obtained);
365 if ((p = strchr_m(uname,'@'))) {
367 cli_credentials_set_realm(credentials, p+1, obtained);
368 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
370 cli_credentials_set_domain(credentials, uname, obtained);
373 cli_credentials_set_username(credentials, uname, obtained);
377 * Specifies default values for domain, workstation and realm
378 * from the smb.conf configuration file
380 * @param cred Credentials structure to fill in
382 void cli_credentials_set_conf(struct cli_credentials *cred)
384 cli_credentials_set_domain(cred, lp_workgroup(), CRED_GUESSED);
385 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_GUESSED);
386 cli_credentials_set_realm(cred, lp_realm(), CRED_GUESSED);
390 * Guess defaults for credentials from environment variables,
391 * and from the configuration file
393 * @param cred Credentials structure to fill in
395 void cli_credentials_guess(struct cli_credentials *cred)
399 cli_credentials_set_username(cred, "", CRED_GUESSED);
400 cli_credentials_set_conf(cred);
402 if (getenv("LOGNAME")) {
403 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESSED);
406 if (getenv("USER")) {
407 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESSED);
408 if ((p = strchr_m(getenv("USER"),'%'))) {
409 memset(p,0,strlen(cred->password));
413 if (getenv("DOMAIN")) {
414 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESSED);
417 if (getenv("PASSWD")) {
418 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESSED);
421 if (getenv("PASSWD_FD")) {
422 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESSED);
425 if (getenv("PASSWD_FILE")) {
426 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESSED);
431 * Fill in credentials for the machine trust account, from the secrets database.
433 * @param cred Credentials structure to fill in
434 * @retval NTSTATUS error detailing any failure
436 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred)
440 struct ldb_context *ldb;
442 struct ldb_message **msgs;
443 const char *base_dn = SECRETS_PRIMARY_DOMAIN_DN;
444 const char *attrs[] = {
452 const char *machine_account;
453 const char *password;
457 /* ok, we are going to get it now, don't recurse back here */
458 cred->machine_account_pending = False;
460 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
461 /* Local secrets are stored in secrets.ldb */
462 ldb = secrets_db_connect(mem_ctx);
464 DEBUG(1, ("Could not open secrets.ldb\n"));
465 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
468 /* search for the secret record */
469 ldb_ret = gendb_search(ldb,
470 mem_ctx, base_dn, &msgs, attrs,
471 SECRETS_PRIMARY_DOMAIN_FILTER,
472 cli_credentials_get_domain(cred));
474 DEBUG(1, ("Could not find join record to domain: %s\n",
475 cli_credentials_get_domain(cred)));
476 talloc_free(mem_ctx);
477 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
478 } else if (ldb_ret != 1) {
479 DEBUG(1, ("Found more than one (%d) join records to domain: %s\n",
480 ldb_ret, cli_credentials_get_domain(cred)));
481 talloc_free(mem_ctx);
482 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
485 password = ldb_msg_find_string(msgs[0], "secret", NULL);
487 DEBUG(1, ("Could not find 'secret' in join record to domain: %s\n",
488 cli_credentials_get_domain(cred)));
489 talloc_free(mem_ctx);
490 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
493 machine_account = ldb_msg_find_string(msgs[0], "samAccountName", NULL);
494 if (!machine_account) {
495 DEBUG(1, ("Could not find 'samAccountName' in join record to domain: %s\n",
496 cli_credentials_get_domain(cred)));
497 talloc_free(mem_ctx);
498 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
501 domain = ldb_msg_find_string(msgs[0], "flatname", NULL);
503 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
506 realm = ldb_msg_find_string(msgs[0], "realm", NULL);
508 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
511 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
512 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
513 talloc_free(mem_ctx);
519 * Ask that when required, the credentials system will be filled with
520 * machine trust account, from the secrets database.
522 * @param cred Credentials structure to fill in
523 * @note This function is used to call the above function after, rather
524 * than during, popt processing.
527 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred)
529 cred->machine_account_pending = True;
533 * Attach NETLOGON credentials for use with SCHANNEL
536 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
537 struct creds_CredentialState *netlogon_creds)
539 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
543 * Return attached NETLOGON credentials
546 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
548 return cred->netlogon_creds;
552 * Fill in a credentials structure as the anonymous user
554 void cli_credentials_set_anonymous(struct cli_credentials *cred)
556 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
557 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
558 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
562 * Describe a credentials context as anonymous or authenticated
563 * @retval True if anonymous, False if a username is specified
566 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
568 const char *username = cli_credentials_get_username(cred);
570 /* Yes, it is deliberate that we die if we have a NULL pointer
571 * here - anonymous is "", not NULL, which is 'never specified,
572 * never guessed', ie programmer bug */