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(mem_ctx, struct cli_credentials);
40 cred->netlogon_creds = NULL;
41 cred->machine_account_pending = False;
42 cred->workstation_obtained = CRED_UNINITIALISED;
43 cred->username_obtained = CRED_UNINITIALISED;
44 cred->password_obtained = CRED_UNINITIALISED;
45 cred->domain_obtained = CRED_UNINITIALISED;
46 cred->realm_obtained = CRED_UNINITIALISED;
51 * Obtain the username for this credentials context.
52 * @param cred credentials context
53 * @retval The username set on this context.
54 * @note Return value will never be NULL except by programmer error.
56 const char *cli_credentials_get_username(struct cli_credentials *cred)
58 if (cred->machine_account_pending) {
59 cli_credentials_set_machine_account(cred);
62 if (cred->username_obtained == CRED_CALLBACK) {
63 cred->username = cred->username_cb(cred);
64 cred->username_obtained = CRED_SPECIFIED;
67 return cred->username;
70 BOOL cli_credentials_set_username(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
72 if (obtained >= cred->username_obtained) {
73 cred->username = talloc_strdup(cred, val);
74 cred->username_obtained = obtained;
82 * Obtain the password for this credentials context.
83 * @param cred credentials context
84 * @retval If set, the cleartext password, otherwise NULL
86 const char *cli_credentials_get_password(struct cli_credentials *cred)
88 if (cred->machine_account_pending) {
89 cli_credentials_set_machine_account(cred);
92 if (cred->password_obtained == CRED_CALLBACK) {
93 cred->password = cred->password_cb(cred);
94 cred->password_obtained = CRED_SPECIFIED;
97 return cred->password;
100 BOOL cli_credentials_set_password(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
102 if (obtained >= cred->password_obtained) {
103 cred->password = talloc_strdup(cred, val);
104 cred->password_obtained = obtained;
112 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
113 * @param cred credentials context
114 * @retval The domain set on this context.
115 * @note Return value will never be NULL except by programmer error.
117 const char *cli_credentials_get_domain(struct cli_credentials *cred)
119 if (cred->machine_account_pending) {
120 cli_credentials_set_machine_account(cred);
123 if (cred->domain_obtained == CRED_CALLBACK) {
124 cred->domain = cred->domain_cb(cred);
125 cred->domain_obtained = CRED_SPECIFIED;
132 BOOL cli_credentials_set_domain(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
134 if (obtained >= cred->domain_obtained) {
135 cred->domain = talloc_strdup(cred, val);
136 cred->domain_obtained = obtained;
144 * Obtain the Kerberos realm for this credentials context.
145 * @param cred credentials context
146 * @retval The realm set on this context.
147 * @note Return value will never be NULL except by programmer error.
149 const char *cli_credentials_get_realm(struct cli_credentials *cred)
151 if (cred->machine_account_pending) {
152 cli_credentials_set_machine_account(cred);
155 if (cred->realm_obtained == CRED_CALLBACK) {
156 cred->realm = cred->realm_cb(cred);
157 cred->realm_obtained = CRED_SPECIFIED;
164 * Obtain the user's Kerberos principal for this credentials context.
165 * @param cred credentials context
166 * @param mem_ctx A talloc context to return the prinipal name on.
167 * @retval The user's Kerberos principal
168 * @note Return value may be NULL due to out-of memeory or invalid mem_ctx
170 char *cli_credentials_get_principal(struct cli_credentials *cred,
173 return talloc_asprintf(mem_ctx, "%s@%s",
174 cli_credentials_get_username(cred),
175 cli_credentials_get_realm(cred));
178 BOOL cli_credentials_set_realm(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
180 if (obtained >= cred->realm_obtained) {
181 cred->realm = talloc_strdup(cred, val);
182 cred->realm_obtained = obtained;
190 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
192 * @param cred credentials context
193 * @retval The workstation name set on this context.
194 * @note Return value will never be NULL except by programmer error.
196 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
198 if (cred->workstation_obtained == CRED_CALLBACK) {
199 cred->workstation = cred->workstation_cb(cred);
200 cred->workstation_obtained = CRED_SPECIFIED;
203 return cred->workstation;
206 BOOL cli_credentials_set_workstation(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
208 if (obtained >= cred->workstation_obtained) {
209 cred->workstation = talloc_strdup(cred, val);
210 cred->workstation_obtained = obtained;
218 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
220 * @param credentials Credentials structure on which to set the password
221 * @param fd open file descriptor to read the password from
222 * @param obtained This enum describes how 'specified' this password is
225 BOOL cli_credentials_parse_password_fd(struct cli_credentials *credentials, int fd, enum credentials_obtained obtained)
230 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
231 p && p - pass < sizeof(pass);) {
232 switch (read(fd, p, 1)) {
234 if (*p != '\n' && *p != '\0') {
235 *++p = '\0'; /* advance p, and null-terminate pass */
240 *p = '\0'; /* null-terminate it, just in case... */
241 p = NULL; /* then force the loop condition to become false */
244 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
249 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
250 fd, strerror(errno));
255 cli_credentials_set_password(credentials, pass, obtained);
260 * Read a named file, and parse it for a password
262 * @param credentials Credentials structure on which to set the password
263 * @param file a named file to read the password from
264 * @param obtained This enum describes how 'specified' this password is
267 BOOL cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
269 int fd = open(file, O_RDONLY, 0);
273 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
274 file, strerror(errno));
278 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
286 * Read a named file, and parse it for username, domain, realm and password
288 * @param credentials Credentials structure on which to set the password
289 * @param file a named file to read the details from
290 * @param obtained This enum describes how 'specified' this password is
293 BOOL cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
298 char *ptr, *val, *param;
300 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
302 /* fail if we can't open the credentials file */
303 d_printf("ERROR: Unable to open credentials file!\n");
307 while (!x_feof(auth))
309 /* get a line from the file */
310 if (!x_fgets(buf, sizeof(buf), auth))
314 if ((len) && (buf[len-1]=='\n'))
322 /* break up the line into parameter & value.
323 * will need to eat a little whitespace possibly */
325 if (!(ptr = strchr_m (buf, '=')))
331 /* eat leading white space */
332 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
335 if (strwicmp("password", param) == 0) {
336 cli_credentials_set_password(cred, val, obtained);
337 } else if (strwicmp("username", param) == 0) {
338 cli_credentials_set_username(cred, val, obtained);
339 } else if (strwicmp("domain", param) == 0) {
340 cli_credentials_set_domain(cred, val, obtained);
341 } else if (strwicmp("realm", param) == 0) {
342 cli_credentials_set_realm(cred, val, obtained);
344 memset(buf, 0, sizeof(buf));
353 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
355 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
357 * @param credentials Credentials structure on which to set the password
358 * @param data the string containing the username, password etc
359 * @param obtained This enum describes how 'specified' this password is
362 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
366 if (strcmp("%",data) == 0) {
367 cli_credentials_set_anonymous(credentials);
371 uname = talloc_strdup(credentials, data);
372 if ((p = strchr_m(uname,'%'))) {
374 cli_credentials_set_password(credentials, p+1, obtained);
377 if ((p = strchr_m(uname,'@'))) {
379 cli_credentials_set_realm(credentials, p+1, obtained);
380 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
382 cli_credentials_set_domain(credentials, uname, obtained);
385 cli_credentials_set_username(credentials, uname, obtained);
389 * Specifies default values for domain, workstation and realm
390 * from the smb.conf configuration file
392 * @param cred Credentials structure to fill in
394 void cli_credentials_set_conf(struct cli_credentials *cred)
396 cli_credentials_set_domain(cred, lp_workgroup(), CRED_GUESSED);
397 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_GUESSED);
398 cli_credentials_set_realm(cred, lp_realm(), CRED_GUESSED);
402 * Guess defaults for credentials from environment variables,
403 * and from the configuration file
405 * @param cred Credentials structure to fill in
407 void cli_credentials_guess(struct cli_credentials *cred)
411 cli_credentials_set_username(cred, "", CRED_GUESSED);
412 cli_credentials_set_conf(cred);
414 if (getenv("LOGNAME")) {
415 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESSED);
418 if (getenv("USER")) {
419 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESSED);
420 if ((p = strchr_m(getenv("USER"),'%'))) {
421 memset(p,0,strlen(cred->password));
425 if (getenv("DOMAIN")) {
426 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESSED);
429 if (getenv("PASSWD")) {
430 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESSED);
433 if (getenv("PASSWD_FD")) {
434 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESSED);
437 if (getenv("PASSWD_FILE")) {
438 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESSED);
443 * Fill in credentials for the machine trust account, from the secrets database.
445 * @param cred Credentials structure to fill in
446 * @retval NTSTATUS error detailing any failure
448 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred)
452 struct ldb_context *ldb;
454 struct ldb_message **msgs;
455 const char *base_dn = SECRETS_PRIMARY_DOMAIN_DN;
456 const char *attrs[] = {
465 const char *machine_account;
466 const char *password;
469 enum netr_SchannelType sct;
471 /* ok, we are going to get it now, don't recurse back here */
472 cred->machine_account_pending = False;
474 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
475 /* Local secrets are stored in secrets.ldb */
476 ldb = secrets_db_connect(mem_ctx);
478 DEBUG(1, ("Could not open secrets.ldb\n"));
479 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
482 /* search for the secret record */
483 ldb_ret = gendb_search(ldb,
484 mem_ctx, base_dn, &msgs, attrs,
485 SECRETS_PRIMARY_DOMAIN_FILTER,
486 cli_credentials_get_domain(cred));
488 DEBUG(1, ("Could not find join record to domain: %s\n",
489 cli_credentials_get_domain(cred)));
490 talloc_free(mem_ctx);
491 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
492 } else if (ldb_ret != 1) {
493 DEBUG(1, ("Found more than one (%d) join records to domain: %s\n",
494 ldb_ret, cli_credentials_get_domain(cred)));
495 talloc_free(mem_ctx);
496 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
499 password = ldb_msg_find_string(msgs[0], "secret", NULL);
501 DEBUG(1, ("Could not find 'secret' in join record to domain: %s\n",
502 cli_credentials_get_domain(cred)));
503 talloc_free(mem_ctx);
504 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
507 machine_account = ldb_msg_find_string(msgs[0], "samAccountName", NULL);
508 if (!machine_account) {
509 DEBUG(1, ("Could not find 'samAccountName' in join record to domain: %s\n",
510 cli_credentials_get_domain(cred)));
511 talloc_free(mem_ctx);
512 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
515 sct = ldb_msg_find_int(msgs[0], "secureChannelType", 0);
517 cli_credentials_set_secure_channel_type(cred, sct);
519 DEBUG(1, ("Domain join for acocunt %s did not have a secureChannelType set!\n",
521 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
524 domain = ldb_msg_find_string(msgs[0], "flatname", NULL);
526 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
529 realm = ldb_msg_find_string(msgs[0], "realm", NULL);
531 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
534 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
535 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
536 talloc_free(mem_ctx);
542 * Ask that when required, the credentials system will be filled with
543 * machine trust account, from the secrets database.
545 * @param cred Credentials structure to fill in
546 * @note This function is used to call the above function after, rather
547 * than during, popt processing.
550 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred)
552 cred->machine_account_pending = True;
556 * Attach NETLOGON credentials for use with SCHANNEL
559 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
560 struct creds_CredentialState *netlogon_creds)
562 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
566 * Return attached NETLOGON credentials
569 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
571 return cred->netlogon_creds;
575 * Set NETLOGON secure channel type
578 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
579 enum netr_SchannelType secure_channel_type)
581 cred->secure_channel_type = secure_channel_type;
585 * Return NETLOGON secure chanel type
588 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
590 return cred->secure_channel_type;
594 * Fill in a credentials structure as the anonymous user
596 void cli_credentials_set_anonymous(struct cli_credentials *cred)
598 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
599 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
600 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
604 * Describe a credentials context as anonymous or authenticated
605 * @retval True if anonymous, False if a username is specified
608 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
610 const char *username = cli_credentials_get_username(cred);
612 /* Yes, it is deliberate that we die if we have a NULL pointer
613 * here - anonymous is "", not NULL, which is 'never specified,
614 * never guessed', ie programmer bug */