s3: Make sure we call wbcAuthenticateUserEx correctly
[obnox/samba-ctdb.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 /* Authenticate a user with a challenge/response */
29
30 static NTSTATUS check_winbind_security(const struct auth_context *auth_context,
31                                        void *my_private_data, 
32                                        TALLOC_CTX *mem_ctx,
33                                        const auth_usersupplied_info *user_info, 
34                                        auth_serversupplied_info **server_info)
35 {
36         NTSTATUS nt_status;
37         wbcErr wbc_status;
38         struct wbcAuthUserParams params;
39         struct wbcAuthUserInfo *info = NULL;
40         struct wbcAuthErrorInfo *err = NULL;
41
42         ZERO_STRUCT(params);
43
44         if (!user_info) {
45                 return NT_STATUS_INVALID_PARAMETER;
46         }
47
48         if (!auth_context) {
49                 DEBUG(3,("Password for user %s cannot be checked because we have no auth_info to get the challenge from.\n", 
50                          user_info->internal_username));
51                 return NT_STATUS_INVALID_PARAMETER;
52         }               
53
54         if (strequal(user_info->domain, get_global_sam_name())) {
55                 DEBUG(3,("check_winbind_security: Not using winbind, requested domain [%s] was for this SAM.\n",
56                         user_info->domain));
57                 return NT_STATUS_NOT_IMPLEMENTED;
58         }
59
60         /* Send off request */
61
62         params.account_name     = user_info->smb_name;
63         params.domain_name      = user_info->domain;
64         params.workstation_name = user_info->wksta_name;
65
66         params.flags            = 0;
67         params.parameter_control= user_info->logon_parameters;
68
69         params.level            = WBC_AUTH_USER_LEVEL_RESPONSE;
70
71         memcpy(params.password.response.challenge,
72                auth_context->challenge.data,
73                sizeof(params.password.response.challenge));
74
75         if (user_info->nt_resp.length != 0) {
76                 params.password.response.nt_length = user_info->nt_resp.length;
77                 params.password.response.nt_data = user_info->nt_resp.data;
78         }
79         if (user_info->lm_resp.length != 0) {
80                 params.password.response.lm_length = user_info->lm_resp.length;
81                 params.password.response.lm_data = user_info->lm_resp.data;
82         }
83
84         /* we are contacting the privileged pipe */
85         become_root();
86         wbc_status = wbcAuthenticateUserEx(&params, &info, &err);
87         unbecome_root();
88
89         if (!WBC_ERROR_IS_OK(wbc_status)) {
90                 DEBUG(10,("check_winbind_security: wbcAuthenticateUserEx failed: %s\n",
91                         wbcErrorString(wbc_status)));
92         }
93
94         if (wbc_status == WBC_ERR_NO_MEMORY) {
95                 return NT_STATUS_NO_MEMORY;
96         }
97
98         if (wbc_status == WBC_ERR_WINBIND_NOT_AVAILABLE) {
99                 struct auth_methods *auth_method =
100                         (struct auth_methods *)my_private_data;
101
102                 if ( auth_method )
103                         return auth_method->auth(auth_context, auth_method->private_data, 
104                                 mem_ctx, user_info, server_info);
105                 else
106                         /* log an error since this should not happen */
107                         DEBUG(0,("check_winbind_security: ERROR!  my_private_data == NULL!\n"));
108         }
109
110         if (wbc_status == WBC_ERR_AUTH_ERROR) {
111                 nt_status = NT_STATUS(err->nt_status);
112                 wbcFreeMemory(err);
113                 return nt_status;
114         }
115
116         if (!WBC_ERROR_IS_OK(wbc_status)) {
117                 return NT_STATUS_LOGON_FAILURE;
118         }
119
120         nt_status = make_server_info_wbcAuthUserInfo(mem_ctx,
121                                                      user_info->smb_name,
122                                                      user_info->domain,
123                                                      info, server_info);
124         wbcFreeMemory(info);
125         if (!NT_STATUS_IS_OK(nt_status)) {
126                 return nt_status;
127         }
128
129         (*server_info)->nss_token |= user_info->was_mapped;
130
131         return nt_status;
132 }
133
134 /* module initialisation */
135 static NTSTATUS auth_init_winbind(struct auth_context *auth_context, const char *param, auth_methods **auth_method) 
136 {
137         if (!make_auth_methods(auth_context, auth_method)) {
138                 return NT_STATUS_NO_MEMORY;
139         }
140
141         (*auth_method)->name = "winbind";
142         (*auth_method)->auth = check_winbind_security;
143
144         if (param && *param) {
145                 /* we load the 'fallback' module - if winbind isn't here, call this
146                    module */
147                 auth_methods *priv;
148                 if (!load_auth_module(auth_context, param, &priv)) {
149                         return NT_STATUS_UNSUCCESSFUL;
150                 }
151                 (*auth_method)->private_data = (void *)priv;
152         }
153         return NT_STATUS_OK;
154 }
155
156 NTSTATUS auth_winbind_init(void)
157 {
158         return smb_register_auth(AUTH_INTERFACE_VERSION, "winbind", auth_init_winbind);
159 }