This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[sfrench/samba-autobuild/.git] / source3 / 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 "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /* Check the machine account password is valid */
30
31 enum winbindd_result winbindd_check_machine_acct(struct winbindd_cli_state *state)
32 {
33         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
34         uchar trust_passwd[16];
35         int num_retries = 0;
36         struct cli_state *cli;
37         uint32 sec_channel_type;
38         DEBUG(3, ("[%5d]: check machine account\n", state->pid));
39
40         /* Get trust account password */
41
42  again:
43         if (!secrets_fetch_trust_account_password(
44                     lp_workgroup(), trust_passwd, NULL, &sec_channel_type)) {
45                 result = NT_STATUS_INTERNAL_ERROR;
46                 goto done;
47         }
48
49         /* This call does a cli_nt_setup_creds() which implicitly checks
50            the trust account password. */
51
52         /* Don't shut this down - it belongs to the connection cache code */
53         result = cm_get_netlogon_cli(lp_workgroup(),
54                                      trust_passwd, sec_channel_type,
55                                      True, &cli);
56
57         if (!NT_STATUS_IS_OK(result)) {
58                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
59                 goto done;
60         }
61
62         /* There is a race condition between fetching the trust account
63            password and the periodic machine password change.  So it's 
64            possible that the trust account password has been changed on us.  
65            We are returned NT_STATUS_ACCESS_DENIED if this happens. */
66
67 #define MAX_RETRIES 8
68
69         if ((num_retries < MAX_RETRIES) && 
70             NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
71                 num_retries++;
72                 goto again;
73         }
74
75         /* Pass back result code - zero for success, other values for
76            specific failures. */
77
78         DEBUG(3, ("secret is %s\n", NT_STATUS_IS_OK(result) ?  
79                   "good" : "bad"));
80
81  done:
82         state->response.data.auth.nt_status = NT_STATUS_V(result);
83         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
84         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
85         state->response.data.auth.pam_error = nt_status_to_pam(result);
86
87         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Checking the trust account password returned %s\n", 
88                                                 state->response.data.auth.nt_status_string));
89
90         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
91 }
92
93 enum winbindd_result winbindd_list_trusted_domains(struct winbindd_cli_state
94                                                    *state)
95 {
96         struct winbindd_domain *domain;
97         int total_entries = 0, extra_data_len = 0;
98         char *ted, *extra_data = NULL;
99
100         DEBUG(3, ("[%5d]: list trusted domains\n", state->pid));
101
102         /* We need to refresh the trusted domain list as the domains may
103            have changed since we last looked.  There may be a sequence
104            number or something we should use but I haven't found it yet. */
105
106         if (!init_domain_list()) {
107                 DEBUG(1, ("winbindd_list_trusted_domains: could not "
108                           "refresh trusted domain list\n"));
109                 return WINBINDD_ERROR;
110         }
111
112         for(domain = domain_list(); domain; domain = domain->next) {
113
114                 /* Skip own domain */
115
116                 if (strequal(domain->name, lp_workgroup())) continue;
117
118                 /* Add domain to list */
119
120                 total_entries++;
121                 ted = Realloc(extra_data, sizeof(fstring) * 
122                               total_entries);
123
124                 if (!ted) {
125                         DEBUG(0,("winbindd_list_trusted_domains: failed to enlarge buffer!\n"));
126                         SAFE_FREE(extra_data);
127                         return WINBINDD_ERROR;
128                 } else 
129                         extra_data = ted;
130
131                 memcpy(&extra_data[extra_data_len], domain->name,
132                        strlen(domain->name));
133
134                 extra_data_len  += strlen(domain->name);
135                 extra_data[extra_data_len++] = ',';
136         }
137
138         if (extra_data) {
139                 if (extra_data_len > 1) 
140                         extra_data[extra_data_len - 1] = '\0';
141                 state->response.extra_data = extra_data;
142                 state->response.length += extra_data_len;
143         }
144
145         return WINBINDD_OK;
146 }
147
148
149 enum winbindd_result winbindd_show_sequence(struct winbindd_cli_state *state)
150 {
151         struct winbindd_domain *domain;
152         char *extra_data = NULL;
153
154         DEBUG(3, ("[%5d]: show sequence\n", state->pid));
155
156         extra_data = strdup("");
157
158         /* this makes for a very simple data format, and is easily parsable as well
159            if that is ever needed */
160         for (domain = domain_list(); domain; domain = domain->next) {
161                 char *s;
162
163                 domain->methods->sequence_number(domain, &domain->sequence_number);
164                 
165                 if (DOM_SEQUENCE_NONE == (unsigned)domain->sequence_number) {
166                         asprintf(&s,"%s%s : DISCONNECTED\n", extra_data, 
167                                  domain->name);
168                 } else {
169                         asprintf(&s,"%s%s : %u\n", extra_data, 
170                                  domain->name, (unsigned)domain->sequence_number);
171                 }
172                 free(extra_data);
173                 extra_data = s;
174         }
175
176         state->response.extra_data = extra_data;
177         /* must add one to length to copy the 0 for string termination */
178         state->response.length += strlen(extra_data) + 1;
179
180         return WINBINDD_OK;
181 }
182
183 enum winbindd_result winbindd_ping(struct winbindd_cli_state
184                                                    *state)
185 {
186         DEBUG(3, ("[%5d]: ping\n", state->pid));
187
188         return WINBINDD_OK;
189 }
190
191 /* List various tidbits of information */
192
193 enum winbindd_result winbindd_info(struct winbindd_cli_state *state)
194 {
195
196         DEBUG(3, ("[%5d]: request misc info\n", state->pid));
197
198         state->response.data.info.winbind_separator = *lp_winbind_separator();
199         fstrcpy(state->response.data.info.samba_version, VERSION);
200
201         return WINBINDD_OK;
202 }
203
204 /* Tell the client the current interface version */
205
206 enum winbindd_result winbindd_interface_version(struct winbindd_cli_state *state)
207 {
208
209         DEBUG(3, ("[%5d]: request interface version\n", state->pid));
210         
211         state->response.data.interface_version = WINBIND_INTERFACE_VERSION;
212
213         return WINBINDD_OK;
214 }
215
216 /* What domain are we a member of? */
217
218 enum winbindd_result winbindd_domain_name(struct winbindd_cli_state *state)
219 {
220
221         DEBUG(3, ("[%5d]: request domain name\n", state->pid));
222         
223         fstrcpy(state->response.data.domain_name, lp_workgroup());
224
225         return WINBINDD_OK;
226 }
227
228 /* What's my name again? */
229
230 enum winbindd_result winbindd_netbios_name(struct winbindd_cli_state *state)
231 {
232
233         DEBUG(3, ("[%5d]: request netbios name\n", state->pid));
234         
235         fstrcpy(state->response.data.netbios_name, global_myname());
236
237         return WINBINDD_OK;
238 }
239
240 /* Where can I find the privilaged pipe? */
241
242 enum winbindd_result winbindd_priv_pipe_dir(struct winbindd_cli_state *state)
243 {
244
245         DEBUG(3, ("[%5d]: request location of privileged pipe\n", state->pid));
246         
247         state->response.extra_data = strdup(get_winbind_priv_pipe_dir());
248         if (!state->response.extra_data)
249                 return WINBINDD_ERROR;
250
251         /* must add one to length to copy the 0 for string termination */
252         state->response.length += strlen((char *)state->response.extra_data) + 1;
253
254         return WINBINDD_OK;
255 }