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