This changes the winbind protcol a bit:
[amitay/samba.git] / source3 / nsswitch / winbindd_misc.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind daemon - miscellaneous other functions
6
7    Copyright (C) Tim Potter 2000
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         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
64
65         if (!NT_STATUS_IS_OK(result)) {
66                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
67                 goto done;
68         }
69
70         cli_shutdown(cli);
71
72         /* There is a race condition between fetching the trust account
73            password and joining the domain so it's possible that the trust
74            account password has been changed on us.  We are returned
75            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.num_entries = NT_STATUS_V(result);
93
94         return WINBINDD_OK;
95 }
96
97 enum winbindd_result winbindd_list_trusted_domains(struct winbindd_cli_state
98                                                    *state)
99 {
100         struct winbindd_domain *domain;
101         int total_entries = 0, extra_data_len = 0;
102         char *ted, *extra_data = NULL;
103
104         DEBUG(3, ("[%5d]: list trusted domains\n", state->pid));
105
106         if (domain_list == NULL)
107                 get_domain_info();
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 enum winbindd_result winbindd_ping(struct winbindd_cli_state
146                                                    *state)
147 {
148         DEBUG(3, ("[%5d]: ping\n", state->pid));
149
150         return WINBINDD_OK;
151 }