c29d008f4a127dd284b360878e4a2851384faef7
[ira/wip.git] / source3 / smbd / auth_winbind.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 2.0
4
5    Winbind authentication mechnism
6
7    Copyright (C) Tim Potter 2000
8    Copyright (C) Andrew Bartlett 2001
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26
27 /* Prototypes from common.h */
28
29 NSS_STATUS winbindd_request(int req_type, 
30                             struct winbindd_request *request,
31                             struct winbindd_response *response);
32
33
34 /* Authenticate a user with a challenge/response */
35
36 static NTSTATUS check_winbind_security(void *my_private_data,
37                                 const auth_usersupplied_info *user_info, 
38                                 const auth_authsupplied_info *auth_info,
39                                 auth_serversupplied_info **server_info)
40 {
41         struct winbindd_request request;
42         struct winbindd_response response;
43         NSS_STATUS result;
44         struct passwd *pw;
45         NTSTATUS nt_status;
46
47         if (!user_info) {
48                 return NT_STATUS_LOGON_FAILURE;
49         }
50
51         if (!auth_info) {
52                 DEBUG(3,("Password for user %s cannot be checked becouse we have no auth_info to get the challange from.\n", 
53                          user_info->internal_username.str));            
54                 return NT_STATUS_LOGON_FAILURE;
55         }               
56
57         /* Send off request */
58
59         ZERO_STRUCT(request);
60         ZERO_STRUCT(response);
61
62         snprintf(request.data.auth_crap.user, sizeof(request.data.auth_crap.user),
63                  "%s\\%s", user_info->domain.str, user_info->smb_name.str);
64
65         memcpy(request.data.auth_crap.chal, auth_info->challange.data, sizeof(request.data.auth_crap.chal));
66         
67         request.data.auth_crap.lm_resp_len = MIN(user_info->lm_resp.length, 
68                                                  sizeof(request.data.auth_crap.lm_resp));
69         request.data.auth_crap.nt_resp_len = MIN(user_info->nt_resp.length, 
70                                                  sizeof(request.data.auth_crap.nt_resp));
71         
72         memcpy(request.data.auth_crap.lm_resp, user_info->lm_resp.data, 
73                sizeof(request.data.auth_crap.lm_resp_len));
74         memcpy(request.data.auth_crap.nt_resp, user_info->nt_resp.data, 
75                request.data.auth_crap.lm_resp_len);
76         
77         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
78
79         if (result == NSS_STATUS_SUCCESS) {
80                 
81                 pw = Get_Pwnam(user_info->internal_username.str);
82                 
83                 if (pw) {                       
84                         if (make_server_info_pw(server_info, pw)) {
85                                 nt_status = NT_STATUS_OK;
86                         } else {
87                                 nt_status = NT_STATUS_NO_MEMORY;
88                         }
89                 } else {
90                         nt_status = NT_STATUS_NO_SUCH_USER;
91                 }
92         } else {
93                 nt_status = NT_STATUS_LOGON_FAILURE;
94         }
95
96         return nt_status;
97 }
98
99 BOOL auth_init_winbind(auth_methods **auth_method) 
100 {
101         if (!make_auth_methods(auth_method)) {
102                 return False;
103         }
104
105         (*auth_method)->auth = check_winbind_security;
106         return True;
107 }
108
109
110
111