s4:auth Change 'get_challenge' API to be more like Samba3
[bbaumbach/samba-autobuild/.git] / source4 / auth / ntlm / auth_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Authenticate by using a remote server
4    Copyright (C) Andrew Bartlett         2001-2002, 2008
5    Copyright (C) Jelmer Vernooij              2002
6    Copyright (C) Stefan Metzmacher            2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "auth/auth.h"
24 #include "auth/credentials/credentials.h"
25 #include "libcli/security/security.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "param/param.h"
28 #include "libcli/resolve/resolve.h"
29
30 /* This version of 'security=server' rewirtten from scratch for Samba4
31  * libraries in 2008 */
32
33
34 static NTSTATUS server_want_check(struct auth_method_context *ctx,
35                                             TALLOC_CTX *mem_ctx,
36                                             const struct auth_usersupplied_info *user_info)
37 {
38         return NT_STATUS_OK;
39 }
40 /** 
41  * The challenge from the target server, when operating in security=server
42  **/
43 static NTSTATUS server_get_challenge(struct auth_method_context *ctx, TALLOC_CTX *mem_ctx, uint8_t chal[8])
44 {
45         struct smb_composite_connect io;
46         struct smbcli_options smb_options;
47         const char **host_list;
48         NTSTATUS status;
49
50         /* Make a connection to the target server, found by 'password server' in smb.conf */
51         
52         lp_smbcli_options(ctx->auth_ctx->lp_ctx, &smb_options);
53
54         /* Make a negprot, WITHOUT SPNEGO, so we get a challenge nice an easy */
55         io.in.options.use_spnego = false;
56
57         /* Hope we don't get * (the default), as this won't work... */
58         host_list = lp_passwordserver(ctx->auth_ctx->lp_ctx); 
59         if (!host_list) {
60                 return NT_STATUS_INTERNAL_ERROR;
61         }
62         io.in.dest_host = host_list[0];
63         if (strequal(io.in.dest_host, "*")) {
64                 return NT_STATUS_INTERNAL_ERROR;
65         }
66         io.in.dest_ports = lp_smb_ports(ctx->auth_ctx->lp_ctx); 
67         io.in.socket_options = lp_socket_options(ctx->auth_ctx->lp_ctx);
68         io.in.gensec_settings = lp_gensec_settings(mem_ctx, ctx->auth_ctx->lp_ctx);
69
70         io.in.called_name = strupper_talloc(mem_ctx, io.in.dest_host);
71
72         /* We don't want to get as far as the session setup */
73         io.in.credentials = cli_credentials_init_anon(mem_ctx);
74         cli_credentials_set_workstation(io.in.credentials,
75                                         lp_netbios_name(ctx->auth_ctx->lp_ctx),
76                                         CRED_SPECIFIED);
77
78         io.in.service = NULL;
79
80         io.in.workgroup = ""; /* only used with SPNEGO, disabled above */
81
82         io.in.options = smb_options;
83         
84         io.in.iconv_convenience = lp_iconv_convenience(ctx->auth_ctx->lp_ctx);
85         lp_smbcli_session_options(ctx->auth_ctx->lp_ctx, &io.in.session_options);
86
87         status = smb_composite_connect(&io, mem_ctx, lp_resolve_context(ctx->auth_ctx->lp_ctx),
88                                        ctx->auth_ctx->event_ctx);
89         NT_STATUS_NOT_OK_RETURN(status);
90
91         if (io.out.tree->session->transport->negotiate.secblob.length != 8) {
92                 return NT_STATUS_INTERNAL_ERROR;
93         }
94         memcpy(chal, io.out.tree->session->transport->negotiate.secblob.data, 8);
95         ctx->private_data = talloc_steal(ctx, io.out.tree->session);
96         return NT_STATUS_OK;
97 }
98
99 /** 
100  * Return an error based on username
101  *
102  * This function allows the testing of obsure errors, as well as the generation
103  * of NT_STATUS -> DOS error mapping tables.
104  *
105  * This module is of no value to end-users.
106  *
107  * The password is ignored.
108  *
109  * @return An NTSTATUS value based on the username
110  **/
111
112 static NTSTATUS server_check_password(struct auth_method_context *ctx,
113                                       TALLOC_CTX *mem_ctx,
114                                       const struct auth_usersupplied_info *user_info, 
115                                       struct auth_serversupplied_info **_server_info)
116 {
117         NTSTATUS nt_status;
118         struct auth_serversupplied_info *server_info;
119         struct cli_credentials *creds;
120         struct smb_composite_sesssetup session_setup;
121
122         struct smbcli_session *session = talloc_get_type(ctx->private_data, struct smbcli_session);
123
124         creds = cli_credentials_init(mem_ctx);
125
126         NT_STATUS_HAVE_NO_MEMORY(creds);
127         
128         cli_credentials_set_username(creds, user_info->client.account_name, CRED_SPECIFIED);
129         cli_credentials_set_domain(creds, user_info->client.domain_name, CRED_SPECIFIED);
130
131         switch (user_info->password_state) {
132         case AUTH_PASSWORD_PLAIN:
133                 cli_credentials_set_password(creds, user_info->password.plaintext, 
134                                              CRED_SPECIFIED);
135                 break;
136         case AUTH_PASSWORD_HASH:
137                 cli_credentials_set_nt_hash(creds, user_info->password.hash.nt,
138                                             CRED_SPECIFIED);
139                 break;
140                 
141         case AUTH_PASSWORD_RESPONSE:
142                 cli_credentials_set_ntlm_response(creds, &user_info->password.response.lanman, &user_info->password.response.nt, CRED_SPECIFIED);
143                 break;
144         }
145
146         session_setup.in.sesskey = session->transport->negotiate.sesskey;
147         session_setup.in.capabilities = session->transport->negotiate.capabilities;
148
149         session_setup.in.credentials = creds;
150         session_setup.in.workgroup = ""; /* Only used with SPNEGO, which we are not doing */
151         session_setup.in.gensec_settings = lp_gensec_settings(session, ctx->auth_ctx->lp_ctx);
152
153         /* Check password with remove server - this should be async some day */
154         nt_status = smb_composite_sesssetup(session, &session_setup);
155
156         if (!NT_STATUS_IS_OK(nt_status)) {
157                 return nt_status;
158         }
159
160         server_info = talloc(mem_ctx, struct auth_serversupplied_info);
161         NT_STATUS_HAVE_NO_MEMORY(server_info);
162
163         server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
164         NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
165
166         /* is this correct? */
167         server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
168         NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
169
170         server_info->n_domain_groups = 0;
171         server_info->domain_groups = NULL;
172
173         /* annoying, but the Anonymous really does have a session key, 
174            and it is all zeros! */
175         server_info->user_session_key = data_blob(NULL, 0);
176         server_info->lm_session_key = data_blob(NULL, 0);
177
178         server_info->account_name = talloc_strdup(server_info, user_info->client.account_name);
179         NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
180
181         server_info->domain_name = talloc_strdup(server_info, user_info->client.domain_name);
182         NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
183
184         server_info->full_name = NULL;
185
186         server_info->logon_script = talloc_strdup(server_info, "");
187         NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
188
189         server_info->profile_path = talloc_strdup(server_info, "");
190         NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
191
192         server_info->home_directory = talloc_strdup(server_info, "");
193         NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
194
195         server_info->home_drive = talloc_strdup(server_info, "");
196         NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
197
198         server_info->last_logon = 0;
199         server_info->last_logoff = 0;
200         server_info->acct_expiry = 0;
201         server_info->last_password_change = 0;
202         server_info->allow_password_change = 0;
203         server_info->force_password_change = 0;
204
205         server_info->logon_count = 0;
206         server_info->bad_password_count = 0;
207
208         server_info->acct_flags = ACB_NORMAL;
209
210         server_info->authenticated = false;
211
212         *_server_info = server_info;
213
214         return nt_status;
215 }
216
217 static const struct auth_operations server_auth_ops = {
218         .name           = "server",
219         .get_challenge  = server_get_challenge,
220         .want_check     = server_want_check,
221         .check_password = server_check_password
222 };
223
224 _PUBLIC_ NTSTATUS auth_server_init(void)
225 {
226         NTSTATUS ret;
227
228         ret = auth_register(&server_auth_ops);
229         if (!NT_STATUS_IS_OK(ret)) {
230                 DEBUG(0,("Failed to register 'server' auth backend!\n"));
231                 return ret;
232         }
233
234         return ret;
235 }