Fix a segfault in auth/auth_domain.c error cases.
[ira/wip.git] / source3 / nsswitch / winbindd_pam.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4
5    Winbind daemon - pam auth funcions
6
7    Copyright (C) Andrew Tridgell 2000
8    Copyright (C) Tim Potter 2001
9    Copyright (C) Andrew Bartlett 2001
10    
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 2 of the License, or
14    (at your option) any later version.
15    
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20    
21    You should have received a copy of the GNU General Public License
22    along with this program; if not, write to the Free Software
23    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #include "winbindd.h"
27
28 /* Return a password structure from a username.  */
29
30 enum winbindd_result winbindd_pam_auth(struct winbindd_cli_state *state) 
31 {
32         NTSTATUS result;
33         fstring name_domain, name_user;
34         int passlen;
35         unsigned char trust_passwd[16];
36         time_t last_change_time;
37         uint32 smb_uid_low;
38         NET_USER_INFO_3 info3;
39         struct cli_state *cli;
40         uchar chal[8];
41         TALLOC_CTX *mem_ctx;
42         DATA_BLOB lm_resp;
43         DATA_BLOB nt_resp;
44
45         extern pstring global_myname;
46
47         DEBUG(3, ("[%5d]: pam auth %s\n", state->pid,
48                   state->request.data.auth.user));
49
50         if (!(mem_ctx = talloc_init_named("winbind pam auth for %s", state->request.data.auth.user))) {
51                 DEBUG(0, ("winbindd_pam_auth: could not talloc_init()!\n"));
52                 return WINBINDD_ERROR;
53         }
54
55         /* Parse domain and username */
56         
57         if (!parse_domain_user(state->request.data.auth.user, name_domain, 
58                                name_user)) {
59                 DEBUG(5,("no domain seperator (%s) in username (%s) - failing fauth\n", lp_winbind_separator(), state->request.data.auth.user));
60                 return WINBINDD_ERROR;
61         }
62
63         passlen = strlen(state->request.data.auth.pass);
64                 
65         if (!*state->request.data.auth.pass) {
66                 return WINBINDD_ERROR;
67         } else {
68                 unsigned char local_lm_response[24];
69                 unsigned char local_nt_response[24];
70                 
71                 generate_random_buffer(chal, 8, False);
72                 SMBencrypt( (const uchar *)state->request.data.auth.pass, chal, local_lm_response);
73                 
74                 SMBNTencrypt((const uchar *)state->request.data.auth.pass, chal, local_nt_response);
75
76                 lm_resp = data_blob_talloc(mem_ctx, local_lm_response, sizeof(local_lm_response));
77                 nt_resp = data_blob_talloc(mem_ctx, local_nt_response, sizeof(local_nt_response));
78         }
79         
80         /*
81          * Get the machine account password for our primary domain
82          */
83
84         if (!secrets_fetch_trust_account_password(
85                 lp_workgroup(), trust_passwd, &last_change_time)) {
86                 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
87                           "password for domain %s\n", lp_workgroup()));
88                 return WINBINDD_ERROR;
89         }
90
91         /* We really don't care what LUID we give the user. */
92
93         generate_random_buffer( (unsigned char *)&smb_uid_low, 4, False);
94
95         ZERO_STRUCT(info3);
96
97         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
98
99         if (!NT_STATUS_IS_OK(result)) {
100                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
101                 goto done;
102         }
103
104         result = cli_netlogon_sam_network_logon(cli, mem_ctx,
105                                                 name_user, name_domain, 
106                                                 global_myname, chal, 
107                                                 lm_resp, nt_resp, 
108                                                 &info3);
109         
110         uni_group_cache_store_netlogon(mem_ctx, &info3);
111 done:
112
113         cli_shutdown(cli);
114
115         talloc_destroy(mem_ctx);
116         
117         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
118 }
119         
120 /* Challenge Response Authentication Protocol */
121
122 enum winbindd_result winbindd_pam_auth_crap(struct winbindd_cli_state *state) 
123 {
124         NTSTATUS result;
125         unsigned char trust_passwd[16];
126         time_t last_change_time;
127         NET_USER_INFO_3 info3;
128         struct cli_state *cli;
129         TALLOC_CTX *mem_ctx;
130
131         DATA_BLOB lm_resp, nt_resp;
132
133         extern pstring global_myname;
134
135         DEBUG(3, ("[%5d]: pam auth crap domain: %s user: %s\n", state->pid,
136                   state->request.data.auth_crap.user, state->request.data.auth_crap.user));
137
138         if (!(mem_ctx = talloc_init_named("winbind pam auth crap for %s", state->request.data.auth.user))) {
139                 DEBUG(0, ("winbindd_pam_auth_crap: could not talloc_init()!\n"));
140                 return WINBINDD_ERROR;
141         }
142
143         lm_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.lm_resp, state->request.data.auth_crap.lm_resp_len);
144         nt_resp = data_blob_talloc(mem_ctx, state->request.data.auth_crap.nt_resp, state->request.data.auth_crap.nt_resp_len);
145         
146         /*
147          * Get the machine account password for our primary domain
148          */
149
150         if (!secrets_fetch_trust_account_password(
151                 lp_workgroup(), trust_passwd, &last_change_time)) {
152                 DEBUG(0, ("winbindd_pam_auth: could not fetch trust account "
153                           "password for domain %s\n", lp_workgroup()));
154                 return WINBINDD_ERROR;
155         }
156
157         ZERO_STRUCT(info3);
158
159         result = cm_get_netlogon_cli(lp_workgroup(), trust_passwd, &cli);
160
161         if (!NT_STATUS_IS_OK(result)) {
162                 DEBUG(3, ("could not open handle to NETLOGON pipe\n"));
163                 goto done;
164         }
165
166         result = cli_netlogon_sam_network_logon(cli, mem_ctx,
167                                                 state->request.data.auth_crap.user, state->request.data.auth_crap.domain, 
168                                                 global_myname, state->request.data.auth_crap.chal, 
169                                                 lm_resp, nt_resp, 
170                                                 &info3);
171         
172         uni_group_cache_store_netlogon(mem_ctx, &info3);
173 done:
174         talloc_destroy(mem_ctx);
175
176         cli_shutdown(cli);
177
178         return NT_STATUS_IS_OK(result) ? WINBINDD_OK : WINBINDD_ERROR;
179 }
180
181 /* Change a user password */
182
183 enum winbindd_result winbindd_pam_chauthtok(struct winbindd_cli_state *state)
184 {
185         char *oldpass, *newpass;
186         fstring domain, user;
187         uchar nt_oldhash[16];
188         uchar lm_oldhash[16];
189
190         DEBUG(3, ("[%5d]: pam chauthtok %s\n", state->pid,
191                 state->request.data.chauthtok.user));
192
193         /* Setup crap */
194
195         if (state == NULL)
196                 return WINBINDD_ERROR;
197
198         if (!parse_domain_user(state->request.data.chauthtok.user, domain, user))
199                 return WINBINDD_ERROR;
200
201         oldpass = state->request.data.chauthtok.oldpass;
202         newpass = state->request.data.chauthtok.newpass;
203
204         nt_lm_owf_gen(oldpass, nt_oldhash, lm_oldhash);
205
206         /* Change password */
207
208 #if 0
209
210         /* XXX */
211
212         if (!msrpc_sam_ntchange_pwd(server_state.controller, domain, user,
213                 lm_oldhash, nt_oldhash, newpass)) {
214                 DEBUG(0, ("password change failed for user %s/%s\n", domain, user));
215                 return WINBINDD_ERROR;
216         }
217 #endif
218     
219         return WINBINDD_OK;
220 }