a small include file rearrangement that doesn't affect normal
[samba.git] / source / nsswitch / winbindd_misc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - miscellaneous other functions
5
6    Copyright (C) Tim Potter      2000
7    Copyright (C) Andrew Bartlett 2002
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "winbindd.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 /* Check the machine account password is valid */
31
32 enum winbindd_result winbindd_check_machine_acct(struct winbindd_cli_state *state)
33 {
34         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
35         uchar trust_passwd[16];
36         int num_retries = 0;
37         struct cli_state *cli;
38         uint32 sec_channel_type;
39         const char *contact_domain_name = NULL;
40         
41         DEBUG(3, ("[%5lu]: check machine account\n", (unsigned long)state->pid));
42
43         /* Get trust account password */
44
45  again:
46         if (!secrets_fetch_trust_account_password(
47                     lp_workgroup(), trust_passwd, NULL, &sec_channel_type)) {
48                 result = NT_STATUS_INTERNAL_ERROR;
49                 goto done;
50         }
51
52
53         /* use the realm name if appropriate and possible */
54         
55         if ( lp_security() == SEC_ADS )
56                 contact_domain_name = lp_realm();
57         
58         if ( !contact_domain_name || !*contact_domain_name )
59                 contact_domain_name = lp_workgroup();
60                 
61         /* This call does a cli_nt_setup_creds() which implicitly checks
62            the trust account password. */
63         /* Don't shut this down - it belongs to the connection cache code */
64         
65         result = cm_get_netlogon_cli(contact_domain_name,
66                 trust_passwd, sec_channel_type, True, &cli);
67
68         if (!NT_STATUS_IS_OK(result)) {
69                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
70                 goto done;
71         }
72
73         /* There is a race condition between fetching the trust account
74            password and the periodic machine password change.  So it's 
75            possible that the trust account password has been changed on us.  
76            We are returned 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.auth.nt_status = NT_STATUS_V(result);
94         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
95         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
96         state->response.data.auth.pam_error = nt_status_to_pam(result);
97
98         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Checking the trust account password returned %s\n", 
99                                                 state->response.data.auth.nt_status_string));
100
101         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
102 }
103
104 enum winbindd_result winbindd_list_trusted_domains(struct winbindd_cli_state
105                                                    *state)
106 {
107         struct winbindd_domain *domain;
108         int total_entries = 0, extra_data_len = 0;
109         char *ted, *extra_data = NULL;
110
111         DEBUG(3, ("[%5lu]: list trusted domains\n", (unsigned long)state->pid));
112
113         /* We need to refresh the trusted domain list as the domains may
114            have changed since we last looked.  There may be a sequence
115            number or something we should use but I haven't found it yet. */
116
117         if (!init_domain_list()) {
118                 DEBUG(1, ("winbindd_list_trusted_domains: could not "
119                           "refresh trusted domain list\n"));
120                 return WINBINDD_ERROR;
121         }
122
123         for(domain = domain_list(); domain; domain = domain->next) {
124
125                 /* Skip own domain */
126
127                 if (strequal(domain->name, lp_workgroup())) continue;
128
129                 /* Add domain to list */
130
131                 total_entries++;
132                 ted = Realloc(extra_data, sizeof(fstring) * 
133                               total_entries);
134
135                 if (!ted) {
136                         DEBUG(0,("winbindd_list_trusted_domains: failed to enlarge buffer!\n"));
137                         SAFE_FREE(extra_data);
138                         return WINBINDD_ERROR;
139                 } else 
140                         extra_data = ted;
141
142                 memcpy(&extra_data[extra_data_len], domain->name,
143                        strlen(domain->name));
144
145                 extra_data_len  += strlen(domain->name);
146                 extra_data[extra_data_len++] = ',';
147         }
148
149         if (extra_data) {
150                 if (extra_data_len > 1) 
151                         extra_data[extra_data_len - 1] = '\0';
152                 state->response.extra_data = extra_data;
153                 state->response.length += extra_data_len;
154         }
155
156         return WINBINDD_OK;
157 }
158
159
160 enum winbindd_result winbindd_show_sequence(struct winbindd_cli_state *state)
161 {
162         struct winbindd_domain *domain;
163         char *extra_data = NULL;
164         const char *which_domain;
165
166         DEBUG(3, ("[%5lu]: show sequence\n", (unsigned long)state->pid));
167
168         /* Ensure null termination */
169         state->request.domain_name[sizeof(state->request.domain_name)-1]='\0';  
170         which_domain = state->request.domain_name;
171
172         extra_data = strdup("");
173
174         /* this makes for a very simple data format, and is easily parsable as well
175            if that is ever needed */
176         for (domain = domain_list(); domain; domain = domain->next) {
177                 char *s;
178
179                 /* if we have a domain name restricting the request and this
180                    one in the list doesn't match, then just bypass the remainder
181                    of the loop */
182
183                 if ( *which_domain && !strequal(which_domain, domain->name) )
184                         continue;
185
186                 domain->methods->sequence_number(domain, &domain->sequence_number);
187                 
188                 if (DOM_SEQUENCE_NONE == (unsigned)domain->sequence_number) {
189                         asprintf(&s,"%s%s : DISCONNECTED\n", extra_data, 
190                                  domain->name);
191                 } else {
192                         asprintf(&s,"%s%s : %u\n", extra_data, 
193                                  domain->name, (unsigned)domain->sequence_number);
194                 }
195                 free(extra_data);
196                 extra_data = s;
197         }
198
199         state->response.extra_data = extra_data;
200         /* must add one to length to copy the 0 for string termination */
201         state->response.length += strlen(extra_data) + 1;
202
203         return WINBINDD_OK;
204 }
205
206 enum winbindd_result winbindd_ping(struct winbindd_cli_state
207                                                    *state)
208 {
209         DEBUG(3, ("[%5lu]: ping\n", (unsigned long)state->pid));
210
211         return WINBINDD_OK;
212 }
213
214 /* List various tidbits of information */
215
216 enum winbindd_result winbindd_info(struct winbindd_cli_state *state)
217 {
218
219         DEBUG(3, ("[%5lu]: request misc info\n", (unsigned long)state->pid));
220
221         state->response.data.info.winbind_separator = *lp_winbind_separator();
222         fstrcpy(state->response.data.info.samba_version, SAMBA_VERSION_STRING);
223
224         return WINBINDD_OK;
225 }
226
227 /* Tell the client the current interface version */
228
229 enum winbindd_result winbindd_interface_version(struct winbindd_cli_state *state)
230 {
231
232         DEBUG(3, ("[%5lu]: request interface version\n", (unsigned long)state->pid));
233         
234         state->response.data.interface_version = WINBIND_INTERFACE_VERSION;
235
236         return WINBINDD_OK;
237 }
238
239 /* What domain are we a member of? */
240
241 enum winbindd_result winbindd_domain_name(struct winbindd_cli_state *state)
242 {
243
244         DEBUG(3, ("[%5lu]: request domain name\n", (unsigned long)state->pid));
245         
246         fstrcpy(state->response.data.domain_name, lp_workgroup());
247
248         return WINBINDD_OK;
249 }
250
251 /* What's my name again? */
252
253 enum winbindd_result winbindd_netbios_name(struct winbindd_cli_state *state)
254 {
255
256         DEBUG(3, ("[%5lu]: request netbios name\n", (unsigned long)state->pid));
257         
258         fstrcpy(state->response.data.netbios_name, global_myname());
259
260         return WINBINDD_OK;
261 }
262
263 /* Where can I find the privilaged pipe? */
264
265 enum winbindd_result winbindd_priv_pipe_dir(struct winbindd_cli_state *state)
266 {
267
268         DEBUG(3, ("[%5lu]: request location of privileged pipe\n", (unsigned long)state->pid));
269         
270         state->response.extra_data = strdup(get_winbind_priv_pipe_dir());
271         if (!state->response.extra_data)
272                 return WINBINDD_ERROR;
273
274         /* must add one to length to copy the 0 for string termination */
275         state->response.length += strlen((char *)state->response.extra_data) + 1;
276
277         return WINBINDD_OK;
278 }