Caching user, group and domain sam handles was a stupid idea.
[ira/wip.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 /* Some routines to fetch the trust account password from a HEAD
29    version of Samba.  Yuck.  )-: */
30
31 /************************************************************************
32 form a key for fetching a domain trust password from
33 ************************************************************************/
34 static char *trust_keystr(char *domain)
35 {
36         static fstring keystr;
37
38         snprintf(keystr,sizeof(keystr),"%s/%s", SECRETS_MACHINE_ACCT_PASS, 
39                  domain);
40
41         return keystr;
42 }
43
44 /************************************************************************
45  Routine to get the trust account password for a domain
46 ************************************************************************/
47 static BOOL _get_trust_account_password(char *domain, unsigned char *ret_pwd, 
48                                         time_t *pass_last_set_time)
49 {
50         struct machine_acct_pass *pass;
51         size_t size;
52
53         if (!(pass = secrets_fetch(trust_keystr(domain), &size)) ||
54             size != sizeof(*pass)) return False;
55
56         if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
57         memcpy(ret_pwd, pass->hash, 16);
58         SAFE_FREE(pass);
59         return True;
60 }
61
62 /* Check the machine account password is valid */
63
64 enum winbindd_result winbindd_check_machine_acct(struct winbindd_cli_state *state)
65 {
66         NTSTATUS status;
67         uchar trust_passwd[16];
68         struct in_addr *ip_list = NULL;
69         int count;
70         fstring controller, trust_account;
71         int num_retries = 0;
72
73         DEBUG(3, ("[%5d]: check machine account\n", state->pid));
74
75         /* Get trust account password */
76
77  again:
78         if (!_get_trust_account_password(lp_workgroup(), trust_passwd, 
79                                          NULL)) {
80                 status = NT_STATUS_INTERNAL_ERROR;
81                 goto done;
82         }
83
84         /* Get domain controller */
85
86         if (!get_dc_list(True, lp_workgroup(), &ip_list, &count) ||
87             !lookup_pdc_name(global_myname, lp_workgroup(), &ip_list[0],
88                              controller)) {
89                 DEBUG(0, ("could not find domain controller for "
90                           "domain %s\n", lp_workgroup()));                
91                 status = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
92                 goto done;
93         }
94
95         DEBUG(3, ("contacting controller %s to check secret\n", controller));
96
97         /* Contact domain controller to check secret */
98
99         slprintf(trust_account, sizeof(trust_account) - 1, "%s$",
100                  global_myname);
101
102 #if 0 /* XXX */
103         {
104                 uint16 validation_level;
105         status = cli_nt_setup_creds(controller, lp_workgroup(), global_myname,
106                                     trust_account, trust_passwd, 
107                                     SEC_CHAN_WKSTA, &validation_level); 
108         }
109 #endif
110
111         /* There is a race condition between fetching the trust account
112            password and joining the domain so it's possible that the trust
113            account password has been changed on us.  We are returned
114            NT_STATUS_ACCESS_DENIED if this happens. */
115
116 #define MAX_RETRIES 8
117
118         if ((num_retries < MAX_RETRIES) && 
119             NT_STATUS_V(status) == NT_STATUS_V(NT_STATUS_ACCESS_DENIED)) {
120                 num_retries++;
121                 goto again;
122         }
123
124         /* Pass back result code - zero for success, other values for
125            specific failures. */
126
127         DEBUG(3, ("secret is %s\n", NT_STATUS_IS_OK(status) ?  "good" : "bad"));
128
129  done:
130         state->response.data.num_entries = NT_STATUS_V(status);
131         return WINBINDD_OK;
132 }
133
134 enum winbindd_result winbindd_list_trusted_domains(struct winbindd_cli_state
135                                                    *state)
136 {
137         struct winbindd_domain *domain;
138         int total_entries = 0, extra_data_len = 0;
139         char *ted, *extra_data = NULL;
140
141         DEBUG(3, ("[%5d]: list trusted domains\n", state->pid));
142
143         if (domain_list == NULL)
144                 get_domain_info();
145
146         for(domain = domain_list; domain; domain = domain->next) {
147
148                 /* Skip own domain */
149
150                 if (strequal(domain->name, lp_workgroup())) continue;
151
152                 /* Add domain to list */
153
154                 total_entries++;
155                 ted = Realloc(extra_data, sizeof(fstring) * 
156                                      total_entries);
157
158                 if (!ted) {
159                         DEBUG(0,("winbindd_list_trusted_domains: failed to enlarge buffer!\n"));
160                         SAFE_FREE(extra_data);
161                         return WINBINDD_ERROR;
162                 }
163                 else extra_data = ted;
164
165                 memcpy(&extra_data[extra_data_len], domain->name,
166                        strlen(domain->name));
167
168                 extra_data_len  += strlen(domain->name);
169                 extra_data[extra_data_len++] = ',';
170         }
171
172         if (extra_data) {
173                 if (extra_data_len > 1) extra_data[extra_data_len - 1] = '\0';
174                 state->response.extra_data = extra_data;
175                 state->response.length += extra_data_len;
176         }
177
178         return WINBINDD_OK;
179 }