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;
60 * Obtain the username for this credentials context.
61 * @param cred credentials context
62 * @retval The username set on this context.
63 * @note Return value will never be NULL except by programmer error.
65 const char *cli_credentials_get_username(struct cli_credentials *cred)
67 if (cred->machine_account_pending) {
68 cli_credentials_set_machine_account(cred);
71 if (cred->username_obtained == CRED_CALLBACK) {
72 cred->username = cred->username_cb(cred);
73 cred->username_obtained = CRED_SPECIFIED;
76 return cred->username;
79 BOOL cli_credentials_set_username(struct cli_credentials *cred,
80 const char *val, enum credentials_obtained obtained)
82 if (obtained >= cred->username_obtained) {
83 cred->username = talloc_strdup(cred, val);
84 cred->username_obtained = obtained;
91 BOOL cli_credentials_set_username_callback(struct cli_credentials *cred,
92 const char *(*username_cb) (struct cli_credentials *))
94 if (cred->username_obtained < CRED_CALLBACK) {
95 cred->username_cb = username_cb;
96 cred->username_obtained = CRED_CALLBACK;
106 * Obtain the client principal for this credentials context.
107 * @param cred credentials context
108 * @retval The username set on this context.
109 * @note Return value will never be NULL except by programmer error.
111 const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
113 if (cred->machine_account_pending) {
114 cli_credentials_set_machine_account(cred);
117 if (cred->principal_obtained == CRED_CALLBACK) {
118 cred->principal = cred->principal_cb(cred);
119 cred->principal_obtained = CRED_SPECIFIED;
122 if (cred->principal_obtained < cred->username_obtained) {
123 if (cred->domain_obtained > cred->realm_obtained) {
124 return talloc_asprintf(mem_ctx, "%s@%s",
125 cli_credentials_get_username(cred),
126 cli_credentials_get_domain(cred));
128 return talloc_asprintf(mem_ctx, "%s@%s",
129 cli_credentials_get_username(cred),
130 cli_credentials_get_realm(cred));
133 return talloc_reference(mem_ctx, cred->principal);
136 BOOL cli_credentials_set_principal(struct cli_credentials *cred,
138 enum credentials_obtained obtained)
140 if (obtained >= cred->principal_obtained) {
141 cred->principal = talloc_strdup(cred, val);
142 cred->principal_obtained = obtained;
149 BOOL cli_credentials_set_principal_callback(struct cli_credentials *cred,
150 const char *(*principal_cb) (struct cli_credentials *))
152 if (cred->principal_obtained < CRED_CALLBACK) {
153 cred->principal_cb = principal_cb;
154 cred->principal_obtained = CRED_CALLBACK;
161 BOOL cli_credentials_authentication_requested(struct cli_credentials *cred)
163 if (cred->principal_obtained >= CRED_SPECIFIED) {
166 if (cred->username_obtained >= CRED_SPECIFIED) {
173 * Obtain the password for this credentials context.
174 * @param cred credentials context
175 * @retval If set, the cleartext password, otherwise NULL
177 const char *cli_credentials_get_password(struct cli_credentials *cred)
179 if (cred->machine_account_pending) {
180 cli_credentials_set_machine_account(cred);
183 if (cred->password_obtained == CRED_CALLBACK) {
184 cred->password = cred->password_cb(cred);
185 cred->password_obtained = CRED_SPECIFIED;
188 return cred->password;
191 BOOL cli_credentials_set_password(struct cli_credentials *cred,
193 enum credentials_obtained obtained)
195 if (obtained >= cred->password_obtained) {
196 cred->password = talloc_strdup(cred, val);
197 cred->password_obtained = obtained;
199 cred->nt_hash = NULL;
206 BOOL cli_credentials_set_password_callback(struct cli_credentials *cred,
207 const char *(*password_cb) (struct cli_credentials *))
209 if (cred->password_obtained < CRED_CALLBACK) {
210 cred->password_cb = password_cb;
211 cred->password_obtained = CRED_CALLBACK;
219 * Obtain the 'old' password for this credentials context (used for join accounts).
220 * @param cred credentials context
221 * @retval If set, the cleartext password, otherwise NULL
223 const char *cli_credentials_get_old_password(struct cli_credentials *cred)
225 if (cred->machine_account_pending) {
226 cli_credentials_set_machine_account(cred);
229 return cred->old_password;
232 BOOL cli_credentials_set_old_password(struct cli_credentials *cred,
234 enum credentials_obtained obtained)
236 cred->old_password = talloc_strdup(cred, val);
241 * Obtain the password for this credentials context.
242 * @param cred credentials context
243 * @retval If set, the cleartext password, otherwise NULL
245 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
248 const char *password = cli_credentials_get_password(cred);
251 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
256 E_md4hash(password, nt_hash->hash);
260 return cred->nt_hash;
264 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
265 const struct samr_Password *nt_hash,
266 enum credentials_obtained obtained)
268 if (obtained >= cred->password_obtained) {
269 cli_credentials_set_password(cred, NULL, obtained);
270 cred->nt_hash = talloc(cred, struct samr_Password);
271 *cred->nt_hash = *nt_hash;
279 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
280 * @param cred credentials context
281 * @retval The domain set on this context.
282 * @note Return value will never be NULL except by programmer error.
284 const char *cli_credentials_get_domain(struct cli_credentials *cred)
286 if (cred->machine_account_pending) {
287 cli_credentials_set_machine_account(cred);
290 if (cred->domain_obtained == CRED_CALLBACK) {
291 cred->domain = cred->domain_cb(cred);
292 cred->domain_obtained = CRED_SPECIFIED;
299 BOOL cli_credentials_set_domain(struct cli_credentials *cred,
301 enum credentials_obtained obtained)
303 if (obtained >= cred->domain_obtained) {
304 /* it is important that the domain be in upper case,
305 * particularly for the sensitive NTLMv2
307 cred->domain = strupper_talloc(cred, val);
308 cred->domain_obtained = obtained;
315 BOOL cli_credentials_set_domain_callback(struct cli_credentials *cred,
316 const char *(*domain_cb) (struct cli_credentials *))
318 if (cred->domain_obtained < CRED_CALLBACK) {
319 cred->domain_cb = domain_cb;
320 cred->domain_obtained = CRED_CALLBACK;
328 * Obtain the Kerberos realm for this credentials context.
329 * @param cred credentials context
330 * @retval The realm set on this context.
331 * @note Return value will never be NULL except by programmer error.
333 const char *cli_credentials_get_realm(struct cli_credentials *cred)
335 if (cred->machine_account_pending) {
336 cli_credentials_set_machine_account(cred);
339 if (cred->realm_obtained == CRED_CALLBACK) {
340 cred->realm = cred->realm_cb(cred);
341 cred->realm_obtained = CRED_SPECIFIED;
348 * Set the realm for this credentials context, and force it to
349 * uppercase for the sainity of our local kerberos libraries
351 BOOL cli_credentials_set_realm(struct cli_credentials *cred,
353 enum credentials_obtained obtained)
355 if (obtained >= cred->realm_obtained) {
356 cred->realm = strupper_talloc(cred, val);
357 cred->realm_obtained = obtained;
364 BOOL cli_credentials_set_realm_callback(struct cli_credentials *cred,
365 const char *(*realm_cb) (struct cli_credentials *))
367 if (cred->realm_obtained < CRED_CALLBACK) {
368 cred->realm_cb = realm_cb;
369 cred->realm_obtained = CRED_CALLBACK;
377 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
379 * @param cred credentials context
380 * @retval The workstation name set on this context.
381 * @note Return value will never be NULL except by programmer error.
383 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
385 if (cred->workstation_obtained == CRED_CALLBACK) {
386 cred->workstation = cred->workstation_cb(cred);
387 cred->workstation_obtained = CRED_SPECIFIED;
390 return cred->workstation;
393 BOOL cli_credentials_set_workstation(struct cli_credentials *cred,
395 enum credentials_obtained obtained)
397 if (obtained >= cred->workstation_obtained) {
398 cred->workstation = talloc_strdup(cred, val);
399 cred->workstation_obtained = obtained;
406 BOOL cli_credentials_set_workstation_callback(struct cli_credentials *cred,
407 const char *(*workstation_cb) (struct cli_credentials *))
409 if (cred->workstation_obtained < CRED_CALLBACK) {
410 cred->workstation_cb = workstation_cb;
411 cred->workstation_obtained = CRED_CALLBACK;
419 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
421 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
423 * @param credentials Credentials structure on which to set the password
424 * @param data the string containing the username, password etc
425 * @param obtained This enum describes how 'specified' this password is
428 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
432 if (strcmp("%",data) == 0) {
433 cli_credentials_set_anonymous(credentials);
437 uname = talloc_strdup(credentials, data);
438 if ((p = strchr_m(uname,'%'))) {
440 cli_credentials_set_password(credentials, p+1, obtained);
443 if ((p = strchr_m(uname,'@'))) {
444 cli_credentials_set_principal(credentials, uname, obtained);
446 cli_credentials_set_realm(credentials, p+1, obtained);
448 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
450 cli_credentials_set_domain(credentials, uname, obtained);
453 cli_credentials_set_username(credentials, uname, obtained);
457 * Specifies default values for domain, workstation and realm
458 * from the smb.conf configuration file
460 * @param cred Credentials structure to fill in
462 void cli_credentials_set_conf(struct cli_credentials *cred)
464 cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
465 cli_credentials_set_domain(cred, lp_workgroup(), CRED_UNINITIALISED);
466 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_UNINITIALISED);
467 cli_credentials_set_realm(cred, lp_realm(), CRED_UNINITIALISED);
471 * Guess defaults for credentials from environment variables,
472 * and from the configuration file
474 * @param cred Credentials structure to fill in
476 void cli_credentials_guess(struct cli_credentials *cred)
480 cli_credentials_set_conf(cred);
482 if (getenv("LOGNAME")) {
483 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESS_ENV);
486 if (getenv("USER")) {
487 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESS_ENV);
488 if ((p = strchr_m(getenv("USER"),'%'))) {
489 memset(p,0,strlen(cred->password));
493 if (getenv("DOMAIN")) {
494 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESS_ENV);
497 if (getenv("PASSWD")) {
498 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESS_ENV);
501 if (getenv("PASSWD_FD")) {
502 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESS_FILE);
505 if (getenv("PASSWD_FILE")) {
506 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESS_FILE);
509 cli_credentials_set_ccache(cred, NULL, CRED_GUESS_FILE);
513 * Attach NETLOGON credentials for use with SCHANNEL
516 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
517 struct creds_CredentialState *netlogon_creds)
519 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
523 * Return attached NETLOGON credentials
526 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
528 return cred->netlogon_creds;
532 * Set NETLOGON secure channel type
535 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
536 enum netr_SchannelType secure_channel_type)
538 cred->secure_channel_type = secure_channel_type;
542 * Return NETLOGON secure chanel type
545 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
547 return cred->secure_channel_type;
551 * Fill in a credentials structure as the anonymous user
553 void cli_credentials_set_anonymous(struct cli_credentials *cred)
555 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
556 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
557 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
561 * Describe a credentials context as anonymous or authenticated
562 * @retval True if anonymous, False if a username is specified
565 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
567 const char *username = cli_credentials_get_username(cred);
569 /* Yes, it is deliberate that we die if we have a NULL pointer
570 * here - anonymous is "", not NULL, which is 'never specified,
571 * never guessed', ie programmer bug */