2 Unix SMB/Netbios implementation.
5 Winbind daemon - miscellaneous other functions
7 Copyright (C) Tim Potter 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern pstring global_myname;
28 /* Some routines to fetch the trust account password from a HEAD
29 version of Samba. Yuck. )-: */
31 /************************************************************************
32 form a key for fetching a domain trust password from
33 ************************************************************************/
34 static char *trust_keystr(char *domain)
36 static fstring keystr;
38 snprintf(keystr,sizeof(keystr),"%s/%s", SECRETS_MACHINE_ACCT_PASS,
44 /************************************************************************
45 Routine to get the trust account password for a domain
46 ************************************************************************/
47 static BOOL _get_trust_account_password(char *domain, unsigned char *ret_pwd,
48 time_t *pass_last_set_time)
50 struct machine_acct_pass *pass;
53 if (!(pass = secrets_fetch(trust_keystr(domain), &size)) ||
54 size != sizeof(*pass)) return False;
56 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
57 memcpy(ret_pwd, pass->hash, 16);
62 /* Check the machine account password is valid */
64 enum winbindd_result winbindd_check_machine_acct(struct winbindd_cli_state *state)
67 uchar trust_passwd[16];
68 struct in_addr *ip_list = NULL;
70 fstring controller, trust_account;
73 DEBUG(3, ("[%5d]: check machine account\n", state->pid));
75 /* Get trust account password */
78 if (!_get_trust_account_password(lp_workgroup(), trust_passwd,
80 status = NT_STATUS_INTERNAL_ERROR;
84 /* Get domain controller */
86 if (!get_dc_list(True, lp_workgroup(), &ip_list, &count) ||
87 !lookup_pdc_name(global_myname, lp_workgroup(), &ip_list[0],
89 DEBUG(0, ("could not find domain controller for "
90 "domain %s\n", lp_workgroup()));
91 status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
95 DEBUG(3, ("contacting controller %s to check secret\n", controller));
97 /* Contact domain controller to check secret */
99 slprintf(trust_account, sizeof(trust_account) - 1, "%s$",
104 uint16 validation_level;
105 status = cli_nt_setup_creds(controller, lp_workgroup(), global_myname,
106 trust_account, trust_passwd,
107 SEC_CHAN_WKSTA, &validation_level);
111 /* There is a race condition between fetching the trust account
112 password and joining the domain so it's possible that the trust
113 account password has been changed on us. We are returned
114 NT_STATUS_ACCESS_DENIED if this happens. */
116 #define MAX_RETRIES 8
118 if ((num_retries < MAX_RETRIES) &&
119 NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
124 /* Pass back result code - zero for success, other values for
125 specific failures. */
127 DEBUG(3, ("secret is %s\n", NT_STATUS_IS_OK(status) ? "good" : "bad"));
130 state->response.data.num_entries = NT_STATUS_V(status);
134 enum winbindd_result winbindd_list_trusted_domains(struct winbindd_cli_state
137 struct winbindd_domain *domain;
138 int total_entries = 0, extra_data_len = 0;
139 char *ted, *extra_data = NULL;
141 DEBUG(3, ("[%5d]: list trusted domains\n", state->pid));
143 if (domain_list == NULL)
146 for(domain = domain_list; domain; domain = domain->next) {
148 /* Skip own domain */
150 if (strequal(domain->name, lp_workgroup())) continue;
152 /* Add domain to list */
155 ted = Realloc(extra_data, sizeof(fstring) *
159 DEBUG(0,("winbindd_list_trusted_domains: failed to enlarge buffer!\n"));
160 SAFE_FREE(extra_data);
161 return WINBINDD_ERROR;
163 else extra_data = ted;
165 memcpy(&extra_data[extra_data_len], domain->name,
166 strlen(domain->name));
168 extra_data_len += strlen(domain->name);
169 extra_data[extra_data_len++] = ',';
173 if (extra_data_len > 1) extra_data[extra_data_len - 1] = '\0';
174 state->response.extra_data = extra_data;
175 state->response.length += extra_data_len;