64eaea3153a234802c1f96dba6582078bb300a7f
[sharpe/samba-autobuild/.git] / source3 / nsswitch / winbindd_misc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind daemon - miscellaneous other functions
6
7    Copyright (C) Tim Potter      2000
8    Copyright (C) Andrew Bartlett 2002
9    
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.
14    
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.
19    
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.
23 */
24
25 #include "winbindd.h"
26
27 extern pstring global_myname;
28
29 /************************************************************************
30  Routine to get the trust account password for a domain
31 ************************************************************************/
32 static BOOL _get_trust_account_password(char *domain, unsigned char *ret_pwd, 
33                                         time_t *pass_last_set_time)
34 {
35         if (!secrets_fetch_trust_account_password(domain, ret_pwd, pass_last_set_time)) {
36                 return False;
37         }
38
39         return True;
40 }
41
42 /* Check the machine account password is valid */
43
44 enum winbindd_result winbindd_check_machine_acct(struct winbindd_cli_state *state)
45 {
46         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
47         uchar trust_passwd[16];
48         int num_retries = 0;
49         struct cli_state *cli;
50         DEBUG(3, ("[%5d]: check machine account\n", state->pid));
51
52         /* Get trust account password */
53
54  again:
55         if (!_get_trust_account_password(lp_workgroup(), trust_passwd, 
56                                          NULL)) {
57                 result = NT_STATUS_INTERNAL_ERROR;
58                 goto done;
59         }
60
61         /* This call does a cli_nt_setup_creds() which implicitly checks
62            the trust account password. */
63
64         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
65
66         if (!NT_STATUS_IS_OK(result)) {
67                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
68                 goto done;
69         }
70
71         cli_shutdown(cli);
72
73         /* There is a race condition between fetching the trust account
74            password and joining the domain so it's possible that the trust
75            account password has been changed on us.  We are returned
76            NT_STATUS_ACCESS_DENIED if this happens. */
77
78 #define MAX_RETRIES 8
79
80         if ((num_retries < MAX_RETRIES) && 
81             NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
82                 num_retries++;
83                 goto again;
84         }
85
86         /* Pass back result code - zero for success, other values for
87            specific failures. */
88
89         DEBUG(3, ("secret is %s\n", NT_STATUS_IS_OK(result) ?  
90                   "good" : "bad"));
91
92  done:
93         state->response.data.num_entries = NT_STATUS_V(result);
94
95         return WINBINDD_OK;
96 }
97
98 enum winbindd_result winbindd_list_trusted_domains(struct winbindd_cli_state
99                                                    *state)
100 {
101         struct winbindd_domain *domain;
102         int total_entries = 0, extra_data_len = 0;
103         char *ted, *extra_data = NULL;
104
105         DEBUG(3, ("[%5d]: list trusted domains\n", state->pid));
106
107         /* We need to refresh the trusted domain list as the domains may
108            have changed since we last looked.  There may be a sequence
109            number or something we should use but I haven't found it yet. */
110
111         init_domain_list();
112
113         for(domain = domain_list(); domain; domain = domain->next) {
114
115                 /* Skip own domain */
116
117                 if (strequal(domain->name, lp_workgroup())) continue;
118
119                 /* Add domain to list */
120
121                 total_entries++;
122                 ted = Realloc(extra_data, sizeof(fstring) * 
123                               total_entries);
124
125                 if (!ted) {
126                         DEBUG(0,("winbindd_list_trusted_domains: failed to enlarge buffer!\n"));
127                         SAFE_FREE(extra_data);
128                         return WINBINDD_ERROR;
129                 } else 
130                         extra_data = ted;
131
132                 memcpy(&extra_data[extra_data_len], domain->name,
133                        strlen(domain->name));
134
135                 extra_data_len  += strlen(domain->name);
136                 extra_data[extra_data_len++] = ',';
137         }
138
139         if (extra_data) {
140                 if (extra_data_len > 1) 
141                         extra_data[extra_data_len - 1] = '\0';
142                 state->response.extra_data = extra_data;
143                 state->response.length += extra_data_len;
144         }
145
146         return WINBINDD_OK;
147 }
148
149 enum winbindd_result winbindd_ping(struct winbindd_cli_state
150                                                    *state)
151 {
152         DEBUG(3, ("[%5d]: ping\n", state->pid));
153
154         return WINBINDD_OK;
155 }
156
157 /* List various tidbits of information */
158
159 enum winbindd_result winbindd_info(struct winbindd_cli_state *state)
160 {
161
162         DEBUG(3, ("[%5d]: request misc info\n", state->pid));
163
164         state->response.data.info.winbind_separator = *lp_winbind_separator();
165         fstrcpy(state->response.data.info.samba_version, VERSION);
166
167         return WINBINDD_OK;
168 }
169
170 /* Tell the client the current interface version */
171
172 enum winbindd_result winbindd_interface_version(struct winbindd_cli_state *state)
173 {
174
175         DEBUG(3, ("[%5d]: request interface version\n", state->pid));
176         
177         state->response.data.interface_version = WINBIND_INTERFACE_VERSION;
178
179         return WINBINDD_OK;
180 }
181
182 /* What domain are we a member of? */
183
184 enum winbindd_result winbindd_domain_name(struct winbindd_cli_state *state)
185 {
186
187         DEBUG(3, ("[%5d]: request domain name\n", state->pid));
188         
189         fstrcpy(state->response.data.domain_name, lp_workgroup());
190
191         return WINBINDD_OK;
192 }