2 Unix SMB/CIFS implementation.
4 User credentials handling
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 */
31 * Create a new credentials structure
32 * @param mem_ctx TALLOC_CTX parent for credentials structure
34 struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx)
36 struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
41 cred->netlogon_creds = NULL;
42 cred->machine_account_pending = False;
43 cred->workstation_obtained = CRED_UNINITIALISED;
44 cred->username_obtained = CRED_UNINITIALISED;
45 cred->password_obtained = CRED_UNINITIALISED;
46 cred->domain_obtained = CRED_UNINITIALISED;
47 cred->realm_obtained = CRED_UNINITIALISED;
48 cred->ccache_obtained = CRED_UNINITIALISED;
49 cred->keytab_obtained = CRED_UNINITIALISED;
50 cred->principal_obtained = CRED_UNINITIALISED;
52 cred->old_password = NULL;
53 cred->smb_krb5_context = NULL;
54 cred->salt_principal = NULL;
55 cred->machine_account = False;
61 * Obtain the username for this credentials context.
62 * @param cred credentials context
63 * @retval The username set on this context.
64 * @note Return value will never be NULL except by programmer error.
66 const char *cli_credentials_get_username(struct cli_credentials *cred)
68 if (cred->machine_account_pending) {
69 cli_credentials_set_machine_account(cred);
72 if (cred->username_obtained == CRED_CALLBACK) {
73 cred->username = cred->username_cb(cred);
74 cred->username_obtained = CRED_SPECIFIED;
77 return cred->username;
80 BOOL cli_credentials_set_username(struct cli_credentials *cred,
81 const char *val, enum credentials_obtained obtained)
83 if (obtained >= cred->username_obtained) {
84 cred->username = talloc_strdup(cred, val);
85 cred->username_obtained = obtained;
92 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
93 const char *(*username_cb) (struct cli_credentials *))
95 if (cred->username_obtained < CRED_CALLBACK) {
96 cred->username_cb = username_cb;
97 cred->username_obtained = CRED_CALLBACK;
107 * Obtain the client principal for this credentials context.
108 * @param cred credentials context
109 * @retval The username set on this context.
110 * @note Return value will never be NULL except by programmer error.
112 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
114 if (cred->machine_account_pending) {
115 cli_credentials_set_machine_account(cred);
118 if (cred->principal_obtained == CRED_CALLBACK) {
119 cred->principal = cred->principal_cb(cred);
120 cred->principal_obtained = CRED_SPECIFIED;
123 if (cred->principal_obtained < cred->username_obtained) {
124 if (cred->domain_obtained > cred->realm_obtained) {
125 return talloc_asprintf(mem_ctx, "%s@%s",
126 cli_credentials_get_username(cred),
127 cli_credentials_get_domain(cred));
129 return talloc_asprintf(mem_ctx, "%s@%s",
130 cli_credentials_get_username(cred),
131 cli_credentials_get_realm(cred));
134 return talloc_reference(mem_ctx, cred->principal);
137 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
139 enum credentials_obtained obtained)
141 if (obtained >= cred->principal_obtained) {
142 cred->principal = talloc_strdup(cred, val);
143 cred->principal_obtained = obtained;
150 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
151 const char *(*principal_cb) (struct cli_credentials *))
153 if (cred->principal_obtained < CRED_CALLBACK) {
154 cred->principal_cb = principal_cb;
155 cred->principal_obtained = CRED_CALLBACK;
162 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
164 if (cred->principal_obtained >= CRED_SPECIFIED) {
167 if (cred->username_obtained >= CRED_SPECIFIED) {
174 * Obtain the password for this credentials context.
175 * @param cred credentials context
176 * @retval If set, the cleartext password, otherwise NULL
178 const char *cli_credentials_get_password(struct cli_credentials *cred)
180 if (cred->machine_account_pending) {
181 cli_credentials_set_machine_account(cred);
184 if (cred->password_obtained == CRED_CALLBACK) {
185 cred->password = cred->password_cb(cred);
186 cred->password_obtained = CRED_SPECIFIED;
189 return cred->password;
192 BOOL cli_credentials_set_password(struct cli_credentials *cred,
194 enum credentials_obtained obtained)
196 if (obtained >= cred->password_obtained) {
197 cred->password = talloc_strdup(cred, val);
198 cred->password_obtained = obtained;
200 cred->nt_hash = NULL;
207 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
208 const char *(*password_cb) (struct cli_credentials *))
210 if (cred->password_obtained < CRED_CALLBACK) {
211 cred->password_cb = password_cb;
212 cred->password_obtained = CRED_CALLBACK;
220 * Obtain the 'old' password for this credentials context (used for join accounts).
221 * @param cred credentials context
222 * @retval If set, the cleartext password, otherwise NULL
224 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
226 if (cred->machine_account_pending) {
227 cli_credentials_set_machine_account(cred);
230 return cred->old_password;
233 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
235 enum credentials_obtained obtained)
237 cred->old_password = talloc_strdup(cred, val);
242 * Obtain the password for this credentials context.
243 * @param cred credentials context
244 * @retval If set, the cleartext password, otherwise NULL
246 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
249 const char *password = cli_credentials_get_password(cred);
252 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
257 E_md4hash(password, nt_hash->hash);
261 return cred->nt_hash;
265 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
266 const struct samr_Password *nt_hash,
267 enum credentials_obtained obtained)
269 if (obtained >= cred->password_obtained) {
270 cli_credentials_set_password(cred, NULL, obtained);
271 cred->nt_hash = talloc(cred, struct samr_Password);
272 *cred->nt_hash = *nt_hash;
280 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
281 * @param cred credentials context
282 * @retval The domain set on this context.
283 * @note Return value will never be NULL except by programmer error.
285 const char *cli_credentials_get_domain(struct cli_credentials *cred)
287 if (cred->machine_account_pending) {
288 cli_credentials_set_machine_account(cred);
291 if (cred->domain_obtained == CRED_CALLBACK) {
292 cred->domain = cred->domain_cb(cred);
293 cred->domain_obtained = CRED_SPECIFIED;
300 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
302 enum credentials_obtained obtained)
304 if (obtained >= cred->domain_obtained) {
305 /* it is important that the domain be in upper case,
306 * particularly for the sensitive NTLMv2
308 cred->domain = strupper_talloc(cred, val);
309 cred->domain_obtained = obtained;
316 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
317 const char *(*domain_cb) (struct cli_credentials *))
319 if (cred->domain_obtained < CRED_CALLBACK) {
320 cred->domain_cb = domain_cb;
321 cred->domain_obtained = CRED_CALLBACK;
329 * Obtain the Kerberos realm for this credentials context.
330 * @param cred credentials context
331 * @retval The realm set on this context.
332 * @note Return value will never be NULL except by programmer error.
334 const char *cli_credentials_get_realm(struct cli_credentials *cred)
336 if (cred->machine_account_pending) {
337 cli_credentials_set_machine_account(cred);
340 if (cred->realm_obtained == CRED_CALLBACK) {
341 cred->realm = cred->realm_cb(cred);
342 cred->realm_obtained = CRED_SPECIFIED;
349 * Set the realm for this credentials context, and force it to
350 * uppercase for the sainity of our local kerberos libraries
352 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
354 enum credentials_obtained obtained)
356 if (obtained >= cred->realm_obtained) {
357 cred->realm = strupper_talloc(cred, val);
358 cred->realm_obtained = obtained;
365 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
366 const char *(*realm_cb) (struct cli_credentials *))
368 if (cred->realm_obtained < CRED_CALLBACK) {
369 cred->realm_cb = realm_cb;
370 cred->realm_obtained = CRED_CALLBACK;
378 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
380 * @param cred credentials context
381 * @retval The workstation name set on this context.
382 * @note Return value will never be NULL except by programmer error.
384 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
386 if (cred->workstation_obtained == CRED_CALLBACK) {
387 cred->workstation = cred->workstation_cb(cred);
388 cred->workstation_obtained = CRED_SPECIFIED;
391 return cred->workstation;
394 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
396 enum credentials_obtained obtained)
398 if (obtained >= cred->workstation_obtained) {
399 cred->workstation = talloc_strdup(cred, val);
400 cred->workstation_obtained = obtained;
407 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
408 const char *(*workstation_cb) (struct cli_credentials *))
410 if (cred->workstation_obtained < CRED_CALLBACK) {
411 cred->workstation_cb = workstation_cb;
412 cred->workstation_obtained = CRED_CALLBACK;
420 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
422 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
424 * @param credentials Credentials structure on which to set the password
425 * @param data the string containing the username, password etc
426 * @param obtained This enum describes how 'specified' this password is
429 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
433 if (strcmp("%",data) == 0) {
434 cli_credentials_set_anonymous(credentials);
438 uname = talloc_strdup(credentials, data);
439 if ((p = strchr_m(uname,'%'))) {
441 cli_credentials_set_password(credentials, p+1, obtained);
444 if ((p = strchr_m(uname,'@'))) {
445 cli_credentials_set_principal(credentials, uname, obtained);
447 cli_credentials_set_realm(credentials, p+1, obtained);
449 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
451 cli_credentials_set_domain(credentials, uname, obtained);
454 cli_credentials_set_username(credentials, uname, obtained);
458 * Specifies default values for domain, workstation and realm
459 * from the smb.conf configuration file
461 * @param cred Credentials structure to fill in
463 void cli_credentials_set_conf(struct cli_credentials *cred)
465 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
466 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
467 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
468 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
472 * Guess defaults for credentials from environment variables,
473 * and from the configuration file
475 * @param cred Credentials structure to fill in
477 void cli_credentials_guess(struct cli_credentials *cred)
481 cli_credentials_set_conf(cred);
483 if (getenv("LOGNAME")) {
484 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
487 if (getenv("USER")) {
488 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
489 if ((p = strchr_m(getenv("USER"),'%'))) {
490 memset(p,0,strlen(cred->password));
494 if (getenv("DOMAIN")) {
495 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESS_ENV);
498 if (getenv("PASSWD")) {
499 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
502 if (getenv("PASSWD_FD")) {
503 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
506 if (getenv("PASSWD_FILE")) {
507 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESS_FILE);
510 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
514 * Attach NETLOGON credentials for use with SCHANNEL
517 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
518 struct creds_CredentialState *netlogon_creds)
520 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
524 * Return attached NETLOGON credentials
527 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
529 return cred->netlogon_creds;
533 * Set NETLOGON secure channel type
536 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
537 enum netr_SchannelType secure_channel_type)
539 cred->secure_channel_type = secure_channel_type;
543 * Return NETLOGON secure chanel type
546 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
548 return cred->secure_channel_type;
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 */