Based orginally by work by Kai, this patch moves our NT_TOKEN generation into
[samba.git] / source3 / auth / auth_winbind.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind authentication mechnism
5
6    Copyright (C) Tim Potter 2000
7    Copyright (C) Andrew Bartlett 2001 - 2002
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 "includes.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_AUTH
28
29 /* Prototypes from common.h */
30
31 NSS_STATUS winbindd_request(int req_type, 
32                             struct winbindd_request *request,
33                             struct winbindd_response *response);
34
35 NTSTATUS get_info3_from_ndr(TALLOC_CTX *mem_ctx, struct winbindd_response *response, NET_USER_INFO_3 *info3)
36 {
37         uint8 *info3_ndr;
38         size_t len = response->length - sizeof(response);
39         prs_struct ps;
40         if (len > 0) {
41                 info3_ndr = response->extra_data;
42                 if (!prs_init(&ps, len, mem_ctx, UNMARSHALL)) {
43                         return NT_STATUS_NO_MEMORY;
44                 }
45                 prs_append_data(&ps, info3_ndr, len);
46                 ps.data_offset = 0;
47                 if (!net_io_user_info3("", info3, &ps, 1, 3)) {
48                         DEBUG(2, ("get_info3_from_ndr: could not parse info3 struct!\n"));
49                         return NT_STATUS_UNSUCCESSFUL;
50                 }
51                 prs_mem_free(&ps);
52
53                 return NT_STATUS_OK;
54         } else {
55                 DEBUG(2, ("get_info3_from_ndr: No info3 struct found!\n"));
56                 return NT_STATUS_UNSUCCESSFUL;
57         }
58 }
59
60 /* Authenticate a user with a challenge/response */
61
62 static NTSTATUS check_winbind_security(const struct auth_context *auth_context,
63                                        void *my_private_data, 
64                                        TALLOC_CTX *mem_ctx,
65                                        const auth_usersupplied_info *user_info, 
66                                        auth_serversupplied_info **server_info)
67 {
68         struct winbindd_request request;
69         struct winbindd_response response;
70         NSS_STATUS result;
71         NTSTATUS nt_status;
72         NET_USER_INFO_3 info3;
73
74         if (!user_info) {
75                 return NT_STATUS_INVALID_PARAMETER;
76         }
77
78         if (!auth_context) {
79                 DEBUG(3,("Password for user %s cannot be checked because we have no auth_info to get the challenge from.\n", 
80                          user_info->internal_username.str));            
81                 return NT_STATUS_UNSUCCESSFUL;
82         }               
83
84         /* Send off request */
85
86         ZERO_STRUCT(request);
87         ZERO_STRUCT(response);
88
89         request.data.auth_crap.flags = WINBIND_PAM_INFO3_NDR;
90
91         push_utf8_fstring(request.data.auth_crap.user, 
92                           user_info->smb_name.str);
93         push_utf8_fstring(request.data.auth_crap.domain, 
94                           user_info->domain.str);
95         push_utf8_fstring(request.data.auth_crap.workstation, 
96                           user_info->wksta_name.str);
97
98         memcpy(request.data.auth_crap.chal, auth_context->challenge.data, sizeof(request.data.auth_crap.chal));
99         
100         request.data.auth_crap.lm_resp_len = MIN(user_info->lm_resp.length, 
101                                                  sizeof(request.data.auth_crap.lm_resp));
102         request.data.auth_crap.nt_resp_len = MIN(user_info->nt_resp.length, 
103                                                  sizeof(request.data.auth_crap.nt_resp));
104         
105         memcpy(request.data.auth_crap.lm_resp, user_info->lm_resp.data, 
106                request.data.auth_crap.lm_resp_len);
107         memcpy(request.data.auth_crap.nt_resp, user_info->nt_resp.data, 
108                request.data.auth_crap.nt_resp_len);
109         
110         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
111
112         nt_status = NT_STATUS(response.data.auth.nt_status);
113
114         if (result == NSS_STATUS_SUCCESS && response.extra_data) {
115                 if (NT_STATUS_IS_OK(nt_status)) {
116                         if (NT_STATUS_IS_OK(nt_status = get_info3_from_ndr(mem_ctx, &response, &info3))) { 
117                                 nt_status = 
118                                         make_server_info_info3(mem_ctx, 
119                                                                user_info->internal_username.str, 
120                                                                user_info->smb_name.str, 
121                                                                user_info->domain.str, 
122                                                                server_info, 
123                                                                &info3); 
124                         }
125                 }
126         } else if (NT_STATUS_IS_OK(nt_status)) {
127                 nt_status = NT_STATUS_UNSUCCESSFUL;
128         }
129
130         return nt_status;
131 }
132
133 /* module initialisation */
134 NTSTATUS auth_init_winbind(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
135 {
136         if (!make_auth_methods(auth_context, auth_method)) {
137                 return NT_STATUS_NO_MEMORY;
138         }
139
140         (*auth_method)->name = "winbind";
141         (*auth_method)->auth = check_winbind_security;
142         return NT_STATUS_OK;
143 }