Merge branch 'v3-2-test-merge' into v3-2-stable
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_AUTH
27
28 static NTSTATUS get_info3_from_ndr(TALLOC_CTX *mem_ctx, struct winbindd_response *response, NET_USER_INFO_3 *info3)
29 {
30         uint8 *info3_ndr;
31         size_t len = response->length - sizeof(struct winbindd_response);
32         prs_struct ps;
33         if (len > 0) {
34                 info3_ndr = (uint8 *)response->extra_data.data;
35                 if (!prs_init(&ps, len, mem_ctx, UNMARSHALL)) {
36                         return NT_STATUS_NO_MEMORY;
37                 }
38                 prs_copy_data_in(&ps, (char *)info3_ndr, len);
39                 prs_set_offset(&ps,0);
40                 if (!net_io_user_info3("", info3, &ps, 1, 3, False)) {
41                         DEBUG(2, ("get_info3_from_ndr: could not parse info3 struct!\n"));
42                         return NT_STATUS_UNSUCCESSFUL;
43                 }
44                 prs_mem_free(&ps);
45
46                 return NT_STATUS_OK;
47         } else {
48                 DEBUG(2, ("get_info3_from_ndr: No info3 struct found!\n"));
49                 return NT_STATUS_UNSUCCESSFUL;
50         }
51 }
52
53 /* Authenticate a user with a challenge/response */
54
55 static NTSTATUS check_winbind_security(const struct auth_context *auth_context,
56                                        void *my_private_data, 
57                                        TALLOC_CTX *mem_ctx,
58                                        const auth_usersupplied_info *user_info, 
59                                        auth_serversupplied_info **server_info)
60 {
61         struct winbindd_request request;
62         struct winbindd_response response;
63         NSS_STATUS result;
64         NTSTATUS nt_status;
65         NET_USER_INFO_3 info3;
66
67         if (!user_info) {
68                 return NT_STATUS_INVALID_PARAMETER;
69         }
70
71         if (!auth_context) {
72                 DEBUG(3,("Password for user %s cannot be checked because we have no auth_info to get the challenge from.\n", 
73                          user_info->internal_username));
74                 return NT_STATUS_INVALID_PARAMETER;
75         }               
76
77         if (strequal(user_info->domain, get_global_sam_name())) {
78                 DEBUG(3,("check_winbind_security: Not using winbind, requested domain [%s] was for this SAM.\n",
79                         user_info->domain));
80                 return NT_STATUS_NOT_IMPLEMENTED;
81         }
82
83         /* Send off request */
84
85         ZERO_STRUCT(request);
86         ZERO_STRUCT(response);
87
88         request.flags = WBFLAG_PAM_INFO3_NDR;
89
90         request.data.auth_crap.logon_parameters = user_info->logon_parameters;
91
92         fstrcpy(request.data.auth_crap.user, user_info->smb_name);
93         fstrcpy(request.data.auth_crap.domain, user_info->domain);
94         fstrcpy(request.data.auth_crap.workstation, user_info->wksta_name);
95
96         memcpy(request.data.auth_crap.chal, auth_context->challenge.data, sizeof(request.data.auth_crap.chal));
97         
98         request.data.auth_crap.lm_resp_len = MIN(user_info->lm_resp.length, 
99                                                  sizeof(request.data.auth_crap.lm_resp));
100         request.data.auth_crap.nt_resp_len = MIN(user_info->nt_resp.length, 
101                                                  sizeof(request.data.auth_crap.nt_resp));
102         
103         memcpy(request.data.auth_crap.lm_resp, user_info->lm_resp.data, 
104                request.data.auth_crap.lm_resp_len);
105         memcpy(request.data.auth_crap.nt_resp, user_info->nt_resp.data, 
106                request.data.auth_crap.nt_resp_len);
107
108         /* we are contacting the privileged pipe */
109         become_root();
110         result = winbindd_priv_request_response(WINBINDD_PAM_AUTH_CRAP,
111                                                 &request, &response);
112         unbecome_root();
113
114         if ( result == NSS_STATUS_UNAVAIL )  {
115                 struct auth_methods *auth_method =
116                         (struct auth_methods *)my_private_data;
117
118                 if ( auth_method )
119                         return auth_method->auth(auth_context, auth_method->private_data, 
120                                 mem_ctx, user_info, server_info);
121                 else
122                         /* log an error since this should not happen */
123                         DEBUG(0,("check_winbind_security: ERROR!  my_private_data == NULL!\n"));
124         }
125
126         nt_status = NT_STATUS(response.data.auth.nt_status);
127
128         if (result == NSS_STATUS_SUCCESS && response.extra_data.data) {
129                 if (NT_STATUS_IS_OK(nt_status)) {
130                         if (NT_STATUS_IS_OK(nt_status = get_info3_from_ndr(mem_ctx, &response, &info3))) { 
131                                 nt_status = make_server_info_info3(mem_ctx, 
132                                         user_info->smb_name, user_info->domain, 
133                                         server_info, &info3); 
134                         }
135                         
136                         if (NT_STATUS_IS_OK(nt_status)) {
137                                 if (user_info->was_mapped) {
138                                         (*server_info)->was_mapped = user_info->was_mapped;
139                                 }
140                         }
141                 }
142         } else if (NT_STATUS_IS_OK(nt_status)) {
143                 nt_status = NT_STATUS_NO_LOGON_SERVERS;
144         }
145
146         SAFE_FREE(response.extra_data.data);
147         return nt_status;
148 }
149
150 /* module initialisation */
151 static NTSTATUS auth_init_winbind(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
152 {
153         if (!make_auth_methods(auth_context, auth_method)) {
154                 return NT_STATUS_NO_MEMORY;
155         }
156
157         (*auth_method)->name = "winbind";
158         (*auth_method)->auth = check_winbind_security;
159
160         if (param && *param) {
161                 /* we load the 'fallback' module - if winbind isn't here, call this
162                    module */
163                 auth_methods *priv;
164                 if (!load_auth_module(auth_context, param, &priv)) {
165                         return NT_STATUS_UNSUCCESSFUL;
166                 }
167                 (*auth_method)->private_data = (void *)priv;
168         }
169         return NT_STATUS_OK;
170 }
171
172 NTSTATUS auth_winbind_init(void)
173 {
174         return smb_register_auth(AUTH_INTERFACE_VERSION, "winbind", auth_init_winbind);
175 }