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