This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[kai/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 "winbindd.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_WINBIND
28
29 /************************************************************************
30  Routine to get the trust account password for a domain
31 ************************************************************************/
32 static BOOL _get_trust_account_password(const 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         /* Don't shut this down - it belongs to the connection cache code */
65         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
66
67         if (!NT_STATUS_IS_OK(result)) {
68                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
69                 goto done;
70         }
71
72         /* There is a race condition between fetching the trust account
73            password and the periodic machine password change.  So it's 
74            possible that the trust account password has been changed on us.  
75            We are returned NT_STATUS_ACCESS_DENIED if this happens. */
76
77 #define MAX_RETRIES 8
78
79         if ((num_retries < MAX_RETRIES) && 
80             NT_STATUS_V(result) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
81                 num_retries++;
82                 goto again;
83         }
84
85         /* Pass back result code - zero for success, other values for
86            specific failures. */
87
88         DEBUG(3, ("secret is %s\n", NT_STATUS_IS_OK(result) ?  
89                   "good" : "bad"));
90
91  done:
92         state->response.data.auth.nt_status = NT_STATUS_V(result);
93         fstrcpy(state->response.data.auth.nt_status_string, nt_errstr(result));
94         fstrcpy(state->response.data.auth.error_string, nt_errstr(result));
95         state->response.data.auth.pam_error = nt_status_to_pam(result);
96
97         DEBUG(NT_STATUS_IS_OK(result) ? 5 : 2, ("Checking the trust account password returned %s\n", 
98                                                 state->response.data.auth.nt_status_string));
99
100         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
101 }
102
103 enum winbindd_result winbindd_list_trusted_domains(struct winbindd_cli_state
104                                                    *state)
105 {
106         struct winbindd_domain *domain;
107         int total_entries = 0, extra_data_len = 0;
108         char *ted, *extra_data = NULL;
109
110         DEBUG(3, ("[%5d]: list trusted domains\n", state->pid));
111
112         /* We need to refresh the trusted domain list as the domains may
113            have changed since we last looked.  There may be a sequence
114            number or something we should use but I haven't found it yet. */
115
116         init_domain_list();
117
118         for(domain = domain_list(); domain; domain = domain->next) {
119
120                 /* Skip own domain */
121
122                 if (strequal(domain->name, lp_workgroup())) continue;
123
124                 /* Add domain to list */
125
126                 total_entries++;
127                 ted = Realloc(extra_data, sizeof(fstring) * 
128                               total_entries);
129
130                 if (!ted) {
131                         DEBUG(0,("winbindd_list_trusted_domains: failed to enlarge buffer!\n"));
132                         SAFE_FREE(extra_data);
133                         return WINBINDD_ERROR;
134                 } else 
135                         extra_data = ted;
136
137                 memcpy(&extra_data[extra_data_len], domain->name,
138                        strlen(domain->name));
139
140                 extra_data_len  += strlen(domain->name);
141                 extra_data[extra_data_len++] = ',';
142         }
143
144         if (extra_data) {
145                 if (extra_data_len > 1) 
146                         extra_data[extra_data_len - 1] = '\0';
147                 state->response.extra_data = extra_data;
148                 state->response.length += extra_data_len;
149         }
150
151         return WINBINDD_OK;
152 }
153
154
155 enum winbindd_result winbindd_show_sequence(struct winbindd_cli_state *state)
156 {
157         struct winbindd_domain *domain;
158         char *extra_data = NULL;
159
160         DEBUG(3, ("[%5d]: show sequence\n", state->pid));
161
162         extra_data = strdup("");
163
164         /* this makes for a very simple data format, and is easily parsable as well
165            if that is ever needed */
166         for (domain = domain_list(); domain; domain = domain->next) {
167                 char *s;
168
169                 domain->methods->sequence_number(domain, &domain->sequence_number);
170                 
171                 if (DOM_SEQUENCE_NONE == (unsigned)domain->sequence_number) {
172                         asprintf(&s,"%s%s : DISCONNECTED\n", extra_data, 
173                                  domain->name);
174                 } else {
175                         asprintf(&s,"%s%s : %u\n", extra_data, 
176                                  domain->name, (unsigned)domain->sequence_number);
177                 }
178                 free(extra_data);
179                 extra_data = s;
180         }
181
182         state->response.extra_data = extra_data;
183         state->response.length += strlen(extra_data);
184
185         return WINBINDD_OK;
186 }
187
188 enum winbindd_result winbindd_ping(struct winbindd_cli_state
189                                                    *state)
190 {
191         DEBUG(3, ("[%5d]: ping\n", state->pid));
192
193         return WINBINDD_OK;
194 }
195
196 /* List various tidbits of information */
197
198 enum winbindd_result winbindd_info(struct winbindd_cli_state *state)
199 {
200
201         DEBUG(3, ("[%5d]: request misc info\n", state->pid));
202
203         state->response.data.info.winbind_separator = *lp_winbind_separator();
204         fstrcpy(state->response.data.info.samba_version, VERSION);
205
206         return WINBINDD_OK;
207 }
208
209 /* Tell the client the current interface version */
210
211 enum winbindd_result winbindd_interface_version(struct winbindd_cli_state *state)
212 {
213
214         DEBUG(3, ("[%5d]: request interface version\n", state->pid));
215         
216         state->response.data.interface_version = WINBIND_INTERFACE_VERSION;
217
218         return WINBINDD_OK;
219 }
220
221 /* What domain are we a member of? */
222
223 enum winbindd_result winbindd_domain_name(struct winbindd_cli_state *state)
224 {
225
226         DEBUG(3, ("[%5d]: request domain name\n", state->pid));
227         
228         fstrcpy(state->response.data.domain_name, lp_workgroup());
229
230         return WINBINDD_OK;
231 }