r8974: Support makefile fragments in .mk files
[Samba/gbeck.git] / source4 / lib / credentials.c
blob58a1b8c0e33f518e8c605d9d5729ac552a085090
1 /*
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.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "include/secrets.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_samr.h" /* for struct samrPassword */
29 /**
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);
36 if (!cred) {
37 return cred;
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;
47 return cred;
50 /**
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;
75 return True;
78 return False;
81 /**
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;
106 cred->nt_hash = NULL;
107 return True;
110 return False;
114 * Obtain the password for this credentials context.
115 * @param cred credentials context
116 * @retval If set, the cleartext password, otherwise NULL
118 const struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
119 TALLOC_CTX *mem_ctx)
121 const char *password = cli_credentials_get_password(cred);
123 if (password) {
124 struct samr_Password *nt_hash = talloc(mem_ctx, struct samr_Password);
125 if (!nt_hash) {
126 return NULL;
129 E_md4hash(password, nt_hash->hash);
131 return nt_hash;
132 } else {
133 return cred->nt_hash;
137 BOOL cli_credentials_set_nt_hash(struct cli_credentials *cred,
138 const struct samr_Password *nt_hash,
139 enum credentials_obtained obtained)
141 if (obtained >= cred->password_obtained) {
142 cli_credentials_set_password(cred, NULL, obtained);
143 cred->nt_hash = talloc(cred, struct samr_Password);
144 *cred->nt_hash = *nt_hash;
145 return True;
148 return False;
152 * Obtain the 'short' or 'NetBIOS' domain for this credentials context.
153 * @param cred credentials context
154 * @retval The domain set on this context.
155 * @note Return value will never be NULL except by programmer error.
157 const char *cli_credentials_get_domain(struct cli_credentials *cred)
159 if (cred->machine_account_pending) {
160 cli_credentials_set_machine_account(cred);
163 if (cred->domain_obtained == CRED_CALLBACK) {
164 cred->domain = cred->domain_cb(cred);
165 cred->domain_obtained = CRED_SPECIFIED;
168 return cred->domain;
172 BOOL cli_credentials_set_domain(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
174 if (obtained >= cred->domain_obtained) {
175 cred->domain = talloc_strdup(cred, val);
176 cred->domain_obtained = obtained;
177 return True;
180 return False;
184 * Obtain the Kerberos realm for this credentials context.
185 * @param cred credentials context
186 * @retval The realm set on this context.
187 * @note Return value will never be NULL except by programmer error.
189 const char *cli_credentials_get_realm(struct cli_credentials *cred)
191 if (cred->machine_account_pending) {
192 cli_credentials_set_machine_account(cred);
195 if (cred->realm_obtained == CRED_CALLBACK) {
196 cred->realm = cred->realm_cb(cred);
197 cred->realm_obtained = CRED_SPECIFIED;
200 return cred->realm;
204 * Obtain the user's Kerberos principal for this credentials context.
205 * @param cred credentials context
206 * @param mem_ctx A talloc context to return the prinipal name on.
207 * @retval The user's Kerberos principal
208 * @note Return value may be NULL due to out-of memeory or invalid mem_ctx
210 char *cli_credentials_get_principal(struct cli_credentials *cred,
211 TALLOC_CTX *mem_ctx)
213 return talloc_asprintf(mem_ctx, "%s@%s",
214 cli_credentials_get_username(cred),
215 cli_credentials_get_realm(cred));
219 * Set the realm for this credentials context, and force it to
220 * uppercase for the sainity of our local kerberos libraries
222 BOOL cli_credentials_set_realm(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
224 if (obtained >= cred->realm_obtained) {
225 cred->realm = strupper_talloc(cred, val);
226 cred->realm_obtained = obtained;
227 return True;
230 return False;
234 * Obtain the 'short' or 'NetBIOS' workstation name for this credentials context.
236 * @param cred credentials context
237 * @retval The workstation name set on this context.
238 * @note Return value will never be NULL except by programmer error.
240 const char *cli_credentials_get_workstation(struct cli_credentials *cred)
242 if (cred->workstation_obtained == CRED_CALLBACK) {
243 cred->workstation = cred->workstation_cb(cred);
244 cred->workstation_obtained = CRED_SPECIFIED;
247 return cred->workstation;
250 BOOL cli_credentials_set_workstation(struct cli_credentials *cred, const char *val, enum credentials_obtained obtained)
252 if (obtained >= cred->workstation_obtained) {
253 cred->workstation = talloc_strdup(cred, val);
254 cred->workstation_obtained = obtained;
255 return True;
258 return False;
262 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
264 * @param credentials Credentials structure on which to set the password
265 * @param fd open file descriptor to read the password from
266 * @param obtained This enum describes how 'specified' this password is
269 BOOL cli_credentials_parse_password_fd(struct cli_credentials *credentials, int fd, enum credentials_obtained obtained)
271 char *p;
272 char pass[128];
274 for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
275 p && p - pass < sizeof(pass);) {
276 switch (read(fd, p, 1)) {
277 case 1:
278 if (*p != '\n' && *p != '\0') {
279 *++p = '\0'; /* advance p, and null-terminate pass */
280 break;
282 case 0:
283 if (p - pass) {
284 *p = '\0'; /* null-terminate it, just in case... */
285 p = NULL; /* then force the loop condition to become false */
286 break;
287 } else {
288 fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
289 return False;
292 default:
293 fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
294 fd, strerror(errno));
295 return False;
299 cli_credentials_set_password(credentials, pass, obtained);
300 return True;
304 * Read a named file, and parse it for a password
306 * @param credentials Credentials structure on which to set the password
307 * @param file a named file to read the password from
308 * @param obtained This enum describes how 'specified' this password is
311 BOOL cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
313 int fd = open(file, O_RDONLY, 0);
314 BOOL ret;
316 if (fd < 0) {
317 fprintf(stderr, "Error opening PASSWD_FILE %s: %s\n",
318 file, strerror(errno));
319 return False;
322 ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
324 close(fd);
326 return ret;
330 * Read a named file, and parse it for username, domain, realm and password
332 * @param credentials Credentials structure on which to set the password
333 * @param file a named file to read the details from
334 * @param obtained This enum describes how 'specified' this password is
337 BOOL cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
339 XFILE *auth;
340 char buf[128];
341 uint16_t len = 0;
342 char *ptr, *val, *param;
344 if ((auth=x_fopen(file, O_RDONLY, 0)) == NULL)
346 /* fail if we can't open the credentials file */
347 d_printf("ERROR: Unable to open credentials file!\n");
348 return False;
351 while (!x_feof(auth))
353 /* get a line from the file */
354 if (!x_fgets(buf, sizeof(buf), auth))
355 continue;
356 len = strlen(buf);
358 if ((len) && (buf[len-1]=='\n'))
360 buf[len-1] = '\0';
361 len--;
363 if (len == 0)
364 continue;
366 /* break up the line into parameter & value.
367 * will need to eat a little whitespace possibly */
368 param = buf;
369 if (!(ptr = strchr_m (buf, '=')))
370 continue;
372 val = ptr+1;
373 *ptr = '\0';
375 /* eat leading white space */
376 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
377 val++;
379 if (strwicmp("password", param) == 0) {
380 cli_credentials_set_password(cred, val, obtained);
381 } else if (strwicmp("username", param) == 0) {
382 cli_credentials_set_username(cred, val, obtained);
383 } else if (strwicmp("domain", param) == 0) {
384 cli_credentials_set_domain(cred, val, obtained);
385 } else if (strwicmp("realm", param) == 0) {
386 cli_credentials_set_realm(cred, val, obtained);
388 memset(buf, 0, sizeof(buf));
391 x_fclose(auth);
392 return True;
397 * Given a string, typically obtained from a -U argument, parse it into domain, username, realm and password fields
399 * The format accepted is [domain\\]user[%password] or user[@realm][%password]
401 * @param credentials Credentials structure on which to set the password
402 * @param data the string containing the username, password etc
403 * @param obtained This enum describes how 'specified' this password is
406 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained)
408 char *uname, *p;
410 if (strcmp("%",data) == 0) {
411 cli_credentials_set_anonymous(credentials);
412 return;
415 uname = talloc_strdup(credentials, data);
416 if ((p = strchr_m(uname,'%'))) {
417 *p = 0;
418 cli_credentials_set_password(credentials, p+1, obtained);
421 if ((p = strchr_m(uname,'@'))) {
422 *p = 0;
423 cli_credentials_set_realm(credentials, p+1, obtained);
424 } else if ((p = strchr_m(uname,'\\')) || (p = strchr_m(uname, '/'))) {
425 *p = 0;
426 cli_credentials_set_domain(credentials, uname, obtained);
427 uname = p+1;
429 cli_credentials_set_username(credentials, uname, obtained);
433 * Specifies default values for domain, workstation and realm
434 * from the smb.conf configuration file
436 * @param cred Credentials structure to fill in
438 void cli_credentials_set_conf(struct cli_credentials *cred)
440 cli_credentials_set_domain(cred, lp_workgroup(), CRED_GUESSED);
441 cli_credentials_set_workstation(cred, lp_netbios_name(), CRED_GUESSED);
442 cli_credentials_set_realm(cred, lp_realm(), CRED_GUESSED);
446 * Guess defaults for credentials from environment variables,
447 * and from the configuration file
449 * @param cred Credentials structure to fill in
451 void cli_credentials_guess(struct cli_credentials *cred)
453 char *p;
455 cli_credentials_set_username(cred, "", CRED_GUESSED);
456 cli_credentials_set_conf(cred);
458 if (getenv("LOGNAME")) {
459 cli_credentials_set_username(cred, getenv("LOGNAME"), CRED_GUESSED);
462 if (getenv("USER")) {
463 cli_credentials_parse_string(cred, getenv("USER"), CRED_GUESSED);
464 if ((p = strchr_m(getenv("USER"),'%'))) {
465 memset(p,0,strlen(cred->password));
469 if (getenv("DOMAIN")) {
470 cli_credentials_set_domain(cred, getenv("DOMAIN"), CRED_GUESSED);
473 if (getenv("PASSWD")) {
474 cli_credentials_set_password(cred, getenv("PASSWD"), CRED_GUESSED);
477 if (getenv("PASSWD_FD")) {
478 cli_credentials_parse_password_fd(cred, atoi(getenv("PASSWD_FD")), CRED_GUESSED);
481 if (getenv("PASSWD_FILE")) {
482 cli_credentials_parse_password_file(cred, getenv("PASSWD_FILE"), CRED_GUESSED);
487 * Fill in credentials for the machine trust account, from the secrets database.
489 * @param cred Credentials structure to fill in
490 * @retval NTSTATUS error detailing any failure
492 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred)
494 TALLOC_CTX *mem_ctx;
496 struct ldb_context *ldb;
497 int ldb_ret;
498 struct ldb_message **msgs;
499 const char *base_dn = SECRETS_PRIMARY_DOMAIN_DN;
500 const char *attrs[] = {
501 "secret",
502 "samAccountName",
503 "flatname",
504 "realm",
505 "secureChannelType",
506 "ntPwdHash",
507 "msDS-KeyVersionNumber",
508 NULL
511 const char *machine_account;
512 const char *password;
513 const char *domain;
514 const char *realm;
515 enum netr_SchannelType sct;
517 /* ok, we are going to get it now, don't recurse back here */
518 cred->machine_account_pending = False;
520 mem_ctx = talloc_named(cred, 0, "cli_credentials fetch machine password");
521 /* Local secrets are stored in secrets.ldb */
522 ldb = secrets_db_connect(mem_ctx);
523 if (!ldb) {
524 DEBUG(1, ("Could not open secrets.ldb\n"));
525 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
528 /* search for the secret record */
529 ldb_ret = gendb_search(ldb,
530 mem_ctx, base_dn, &msgs, attrs,
531 SECRETS_PRIMARY_DOMAIN_FILTER,
532 cli_credentials_get_domain(cred));
533 if (ldb_ret == 0) {
534 DEBUG(1, ("Could not find join record to domain: %s\n",
535 cli_credentials_get_domain(cred)));
536 talloc_free(mem_ctx);
537 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
538 } else if (ldb_ret != 1) {
539 DEBUG(1, ("Found more than one (%d) join records to domain: %s\n",
540 ldb_ret, cli_credentials_get_domain(cred)));
541 talloc_free(mem_ctx);
542 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
545 password = ldb_msg_find_string(msgs[0], "secret", NULL);
547 machine_account = ldb_msg_find_string(msgs[0], "samAccountName", NULL);
549 if (!machine_account) {
550 DEBUG(1, ("Could not find 'samAccountName' in join record to domain: %s\n",
551 cli_credentials_get_domain(cred)));
552 talloc_free(mem_ctx);
553 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
556 sct = ldb_msg_find_int(msgs[0], "secureChannelType", 0);
557 if (!sct) {
558 DEBUG(1, ("Domain join for acocunt %s did not have a secureChannelType set!\n",
559 machine_account));
560 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
563 if (!password) {
564 const struct ldb_val *nt_password_hash = ldb_msg_find_ldb_val(msgs[0], "ntPwdHash");
565 struct samr_Password hash;
566 ZERO_STRUCT(hash);
567 if (nt_password_hash) {
568 memcpy(hash.hash, nt_password_hash->data,
569 MIN(nt_password_hash->length, sizeof(hash.hash)));
571 cli_credentials_set_nt_hash(cred, &hash, CRED_SPECIFIED);
572 } else {
574 DEBUG(1, ("Could not find 'secret' in join record to domain: %s\n",
575 cli_credentials_get_domain(cred)));
576 talloc_free(mem_ctx);
577 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
581 cli_credentials_set_secure_channel_type(cred, sct);
583 domain = ldb_msg_find_string(msgs[0], "flatname", NULL);
584 if (domain) {
585 cli_credentials_set_domain(cred, domain, CRED_SPECIFIED);
588 realm = ldb_msg_find_string(msgs[0], "realm", NULL);
589 if (realm) {
590 cli_credentials_set_realm(cred, realm, CRED_SPECIFIED);
593 cli_credentials_set_username(cred, machine_account, CRED_SPECIFIED);
594 if (password) {
595 cli_credentials_set_password(cred, password, CRED_SPECIFIED);
598 cli_credentials_set_kvno(cred, ldb_msg_find_int(msgs[0], "msDS-KeyVersionNumber", 0));
600 talloc_free(mem_ctx);
602 return NT_STATUS_OK;
606 * Ask that when required, the credentials system will be filled with
607 * machine trust account, from the secrets database.
609 * @param cred Credentials structure to fill in
610 * @note This function is used to call the above function after, rather
611 * than during, popt processing.
614 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred)
616 cred->machine_account_pending = True;
620 * Attach NETLOGON credentials for use with SCHANNEL
623 void cli_credentials_set_netlogon_creds(struct cli_credentials *cred,
624 struct creds_CredentialState *netlogon_creds)
626 cred->netlogon_creds = talloc_reference(cred, netlogon_creds);
630 * Return attached NETLOGON credentials
633 struct creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred)
635 return cred->netlogon_creds;
638 /**
639 * Set NETLOGON secure channel type
642 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
643 enum netr_SchannelType secure_channel_type)
645 cred->secure_channel_type = secure_channel_type;
649 * Return NETLOGON secure chanel type
652 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred)
654 return cred->secure_channel_type;
657 /**
658 * Set Kerberos KVNO
661 void cli_credentials_set_kvno(struct cli_credentials *cred,
662 int kvno)
664 cred->kvno = kvno;
668 * Return Kerberos KVNO
671 int cli_credentials_get_kvno(struct cli_credentials *cred)
673 return cred->kvno;
677 * Fill in a credentials structure as the anonymous user
679 void cli_credentials_set_anonymous(struct cli_credentials *cred)
681 cli_credentials_set_username(cred, "", CRED_SPECIFIED);
682 cli_credentials_set_domain(cred, "", CRED_SPECIFIED);
683 cli_credentials_set_password(cred, NULL, CRED_SPECIFIED);
687 * Describe a credentials context as anonymous or authenticated
688 * @retval True if anonymous, False if a username is specified
691 BOOL cli_credentials_is_anonymous(struct cli_credentials *cred)
693 const char *username = cli_credentials_get_username(cred);
695 /* Yes, it is deliberate that we die if we have a NULL pointer
696 * here - anonymous is "", not NULL, which is 'never specified,
697 * never guessed', ie programmer bug */
698 if (!username[0])
699 return True;
701 return False;