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