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