converted another bunch of stuff to NTSTATUS
[kamenim/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 /* 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 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         free(pass);
59         return True;
60 }
61
62 /* Check the machine account password is valid */
63
64 enum winbindd_result winbindd_check_machine_acct(
65         struct winbindd_cli_state *state)
66 {
67         int result = WINBINDD_ERROR;
68         uchar trust_passwd[16];
69         struct in_addr *ip_list = NULL;
70         int count;
71         uint16 validation_level;
72         fstring controller, trust_account;
73         int num_retries = 0;
74
75         DEBUG(3, ("[%5d]: check machine account\n", state->pid));
76
77         /* Get trust account password */
78
79  again:
80         if (!_get_trust_account_password(lp_workgroup(), trust_passwd, 
81                                          NULL)) {
82                 result = NT_STATUS_INTERNAL_ERROR;
83                 goto done;
84         }
85
86         /* Get domain controller */
87
88         if (!get_dc_list(True, lp_workgroup(), &ip_list, &count) ||
89             !lookup_pdc_name(global_myname, lp_workgroup(), &ip_list[0],
90                              controller)) {
91                 DEBUG(0, ("could not find domain controller for "
92                           "domain %s\n", lp_workgroup()));                
93                 result = NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND;
94                 goto done;
95         }
96
97         DEBUG(3, ("contacting controller %s to check secret\n", controller));
98
99         /* Contact domain controller to check secret */
100
101         slprintf(trust_account, sizeof(trust_account) - 1, "%s$",
102                  global_myname);
103
104 #if 0 /* XXX */
105         result = cli_nt_setup_creds(controller, lp_workgroup(), global_myname,
106                                     trust_account, trust_passwd, 
107                                     SEC_CHAN_WKSTA, &validation_level); 
108 #endif
109
110         /* There is a race condition between fetching the trust account
111            password and joining the domain so it's possible that the trust
112            account password has been changed on us.  We are returned
113            NT_STATUS_ACCESS_DENIED if this happens. */
114
115 #define MAX_RETRIES 8
116
117         if ((num_retries < MAX_RETRIES) && 
118             result == NT_STATUS_ACCESS_DENIED) {
119                 num_retries++;
120                 goto again;
121         }
122
123         /* Pass back result code - zero for success, other values for
124            specific failures. */
125
126         DEBUG(3, ("secret is %s\n", (result == NT_STATUS_OK) ?
127                   "good" : "bad"));
128
129  done:
130         state->response.data.num_entries = result;
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         for(domain = domain_list; domain; domain = domain->next) {
144
145                 /* Skip own domain */
146
147                 if (strequal(domain->name, lp_workgroup())) continue;
148
149                 /* Add domain to list */
150
151                 total_entries++;
152                 ted = Realloc(extra_data, sizeof(fstring) * 
153                                      total_entries);
154
155                 if (!ted) {
156                         DEBUG(0,("winbindd_list_trusted_domains: failed to enlarge buffer!\n"));
157                         if (extra_data) free(extra_data);
158                         return WINBINDD_ERROR;
159                 }
160                 else extra_data = ted;
161
162                 memcpy(&extra_data[extra_data_len], domain->name,
163                        strlen(domain->name));
164
165                 extra_data_len  += strlen(domain->name);
166                 extra_data[extra_data_len++] = ',';
167         }
168
169         if (extra_data) {
170                 if (extra_data_len > 1) extra_data[extra_data_len - 1] = '\0';
171                 state->response.extra_data = extra_data;
172                 state->response.length += extra_data_len;
173         }
174
175         return WINBINDD_OK;
176 }