r3453: - split out the auth and popt includes
[abartlet/samba.git/.git] / source4 / 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 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "auth/auth.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_AUTH
30
31 static NTSTATUS get_info3_from_ndr(TALLOC_CTX *mem_ctx, struct winbindd_response *response, struct netr_SamInfo3 *info3)
32 {
33         size_t len = response->length - sizeof(struct winbindd_response);
34         if (len > 4) {
35                 NTSTATUS status;
36                 DATA_BLOB blob;
37                 blob.length = len - 4;
38                 blob.data = ((char *)response->extra_data) + 4;
39                 
40                 status = ndr_pull_struct_blob(&blob, mem_ctx, info3,
41                                               (ndr_pull_flags_fn_t)ndr_pull_netr_SamInfo3);
42
43                 return status;
44         } else {
45                 DEBUG(2, ("get_info3_from_ndr: No info3 struct found!\n"));
46                 return NT_STATUS_UNSUCCESSFUL;
47         }
48 }
49
50 /* Authenticate a user with a challenge/response */
51
52 static NTSTATUS check_winbind_security(const struct auth_context *auth_context,
53                                      void *my_private_data, 
54                                      TALLOC_CTX *mem_ctx,
55                                      const struct auth_usersupplied_info *user_info, 
56                                      struct auth_serversupplied_info **server_info)
57 {
58         struct winbindd_request request;
59         struct winbindd_response response;
60         NSS_STATUS result;
61         NTSTATUS nt_status;
62         struct netr_SamInfo3 info3;
63
64         if (!user_info) {
65                 return NT_STATUS_INVALID_PARAMETER;
66         }
67
68         if (!auth_context) {
69                 DEBUG(3,("Password for user %s cannot be checked because we have no auth_info to get the challenge from.\n", 
70                          user_info->internal_username.str));            
71                 return NT_STATUS_UNSUCCESSFUL;
72         }               
73
74         /* Send off request */
75
76         ZERO_STRUCT(request);
77         ZERO_STRUCT(response);
78         request.flags = WBFLAG_PAM_INFO3_NDR;
79         fstrcpy(request.data.auth_crap.user, 
80                 user_info->smb_name.str);
81         fstrcpy(request.data.auth_crap.domain, 
82                           user_info->domain.str);
83         fstrcpy(request.data.auth_crap.workstation, 
84                           user_info->wksta_name.str);
85
86         memcpy(request.data.auth_crap.chal, auth_context->challenge.data, sizeof(request.data.auth_crap.chal));
87         
88         request.data.auth_crap.lm_resp_len = MIN(user_info->lm_resp.length, 
89                                                  sizeof(request.data.auth_crap.lm_resp));
90         request.data.auth_crap.nt_resp_len = MIN(user_info->nt_resp.length, 
91                                                  sizeof(request.data.auth_crap.nt_resp));
92         
93         memcpy(request.data.auth_crap.lm_resp, user_info->lm_resp.data, 
94                request.data.auth_crap.lm_resp_len);
95         memcpy(request.data.auth_crap.nt_resp, user_info->nt_resp.data, 
96                request.data.auth_crap.nt_resp_len);
97         
98         result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
99
100         nt_status = NT_STATUS(response.data.auth.nt_status);
101
102         if (!NT_STATUS_IS_OK(nt_status)) {
103                 return nt_status;
104         }
105
106         if (result == NSS_STATUS_SUCCESS && response.extra_data) {
107                 if (NT_STATUS_IS_OK(nt_status)) {
108                         if (NT_STATUS_IS_OK(nt_status = get_info3_from_ndr(mem_ctx, &response, &info3))) { 
109                                 nt_status = 
110                                         make_server_info_info3(mem_ctx, 
111                                                                user_info->internal_username.str, 
112                                                                server_info, 
113                                                                &info3); 
114                         }
115                 }
116                 SAFE_FREE(response.extra_data);
117         } else if (result == NSS_STATUS_SUCCESS && !response.extra_data) {
118                 DEBUG(0, ("Winbindd authenticated the user [%s]\\[%s], "
119                           "but did not include the required info3 reply!\n", 
120                           user_info->smb_name.str, user_info->domain.str));
121                 nt_status = NT_STATUS_INSUFFICIENT_LOGON_INFO;
122         } else if (NT_STATUS_IS_OK(nt_status)) {
123                 DEBUG(1, ("Winbindd authentication for [%s]\\[%s] failed, "
124                           "but no error code is available!\n", 
125                           user_info->smb_name.str, user_info->domain.str));
126                 nt_status = NT_STATUS_NO_LOGON_SERVERS;
127         }
128
129         return nt_status;
130 }
131
132 /* module initialisation */
133 static NTSTATUS auth_init_winbind(struct auth_context *auth_context, 
134                                           const char *param, 
135                                           struct auth_methods **auth_method) 
136 {
137         if (!make_auth_methods(auth_context, auth_method))
138                 return NT_STATUS_NO_MEMORY;
139
140         (*auth_method)->name = "winbind";
141         (*auth_method)->auth = check_winbind_security;
142         return NT_STATUS_OK;
143 }
144
145 NTSTATUS auth_winbind_init(void)
146 {
147         NTSTATUS ret;
148         struct auth_operations ops;
149
150         ops.name = "winbind";
151         ops.init = auth_init_winbind;
152         ret = register_backend("auth", &ops);
153         if (!NT_STATUS_IS_OK(ret)) {
154                 DEBUG(0,("Failed to register '%s' auth backend!\n",
155                         ops.name));
156         }
157         return ret;
158 }