Move NTLM authentication details into auth/ntlm
[sfrench/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/auth_proto.h"
25 #include "auth/credentials/credentials.h"
26 #include "libcli/security/security.h"
27 #include "librpc/gen_ndr/ndr_samr.h"
28 #include "libcli/smb_composite/smb_composite.h"
29 #include "param/param.h"
30 #include "libcli/resolve/resolve.h"
31
32 /* This version of 'security=server' rewirtten from scratch for Samba4
33  * libraries in 2008 */
34
35
36 static NTSTATUS server_want_check(struct auth_method_context *ctx,
37                                             TALLOC_CTX *mem_ctx,
38                                             const struct auth_usersupplied_info *user_info)
39 {
40         return NT_STATUS_OK;
41 }
42 /** 
43  * The challenge from the target server, when operating in security=server
44  **/
45 static NTSTATUS server_get_challenge(struct auth_method_context *ctx, TALLOC_CTX *mem_ctx, DATA_BLOB *_blob)
46 {
47         struct smb_composite_connect io;
48         struct smbcli_options smb_options;
49         const char **host_list;
50         NTSTATUS status;
51
52         /* Make a connection to the target server, found by 'password server' in smb.conf */
53         
54         lp_smbcli_options(ctx->auth_ctx->lp_ctx, &smb_options);
55
56         /* Make a negprot, WITHOUT SPNEGO, so we get a challenge nice an easy */
57         io.in.options.use_spnego = false;
58
59         /* Hope we don't get * (the default), as this won't work... */
60         host_list = lp_passwordserver(ctx->auth_ctx->lp_ctx); 
61         if (!host_list) {
62                 return NT_STATUS_INTERNAL_ERROR;
63         }
64         io.in.dest_host = host_list[0];
65         if (strequal(io.in.dest_host, "*")) {
66                 return NT_STATUS_INTERNAL_ERROR;
67         }
68         io.in.dest_ports = lp_smb_ports(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 = NULL;
74         io.in.service = NULL;
75
76         io.in.workgroup = ""; /* only used with SPNEGO, disabled above */
77
78         io.in.options = smb_options;
79
80         status = smb_composite_connect(&io, mem_ctx, lp_resolve_context(ctx->auth_ctx->lp_ctx),
81                                        ctx->auth_ctx->event_ctx);
82         if (!NT_STATUS_IS_OK(status)) {
83                 *_blob = io.out.tree->session->transport->negotiate.secblob;
84                 ctx->private_data = talloc_steal(ctx, io.out.tree->session);
85         }
86         return NT_STATUS_OK;
87 }
88
89 /** 
90  * Return an error based on username
91  *
92  * This function allows the testing of obsure errors, as well as the generation
93  * of NT_STATUS -> DOS error mapping tables.
94  *
95  * This module is of no value to end-users.
96  *
97  * The password is ignored.
98  *
99  * @return An NTSTATUS value based on the username
100  **/
101
102 static NTSTATUS server_check_password(struct auth_method_context *ctx,
103                                       TALLOC_CTX *mem_ctx,
104                                       const struct auth_usersupplied_info *user_info, 
105                                       struct auth_serversupplied_info **_server_info)
106 {
107         NTSTATUS nt_status;
108         struct auth_serversupplied_info *server_info;
109         struct cli_credentials *creds;
110         const char *user;
111         struct smb_composite_sesssetup session_setup;
112
113         struct smbcli_session *session = talloc_get_type(ctx->private_data, struct smbcli_session);
114
115         creds = cli_credentials_init(mem_ctx);
116
117         NT_STATUS_HAVE_NO_MEMORY(creds);
118         
119         cli_credentials_set_username(creds, user_info->client.account_name, CRED_SPECIFIED);
120         cli_credentials_set_domain(creds, user_info->client.domain_name, CRED_SPECIFIED);
121
122         switch (user_info->password_state) {
123         case AUTH_PASSWORD_PLAIN:
124                 cli_credentials_set_password(creds, user_info->password.plaintext, 
125                                              CRED_SPECIFIED);
126                 break;
127         case AUTH_PASSWORD_HASH:
128                 cli_credentials_set_nt_hash(creds, user_info->password.hash.nt,
129                                             CRED_SPECIFIED);
130                 break;
131                 
132         case AUTH_PASSWORD_RESPONSE:
133                 cli_credentials_set_ntlm_response(creds, &user_info->password.response.lanman, &user_info->password.response.nt, CRED_SPECIFIED);
134                 break;
135         }
136
137         session_setup.in.sesskey = session->transport->negotiate.sesskey;
138         session_setup.in.capabilities = session->transport->negotiate.capabilities;
139
140         session_setup.in.credentials = creds;
141         session_setup.in.workgroup = ""; /* Only used with SPNEGO, which we are not doing */
142
143         /* Check password with remove server - this should be async some day */
144         nt_status = smb_composite_sesssetup(session, &session_setup);
145
146         if (!NT_STATUS_IS_OK(nt_status)) {
147                 return nt_status;
148         }
149
150         server_info = talloc(mem_ctx, struct auth_serversupplied_info);
151         NT_STATUS_HAVE_NO_MEMORY(server_info);
152
153         server_info->account_sid = dom_sid_parse_talloc(server_info, SID_NT_ANONYMOUS);
154         NT_STATUS_HAVE_NO_MEMORY(server_info->account_sid);
155
156         /* is this correct? */
157         server_info->primary_group_sid = dom_sid_parse_talloc(server_info, SID_BUILTIN_GUESTS);
158         NT_STATUS_HAVE_NO_MEMORY(server_info->primary_group_sid);
159
160         server_info->n_domain_groups = 0;
161         server_info->domain_groups = NULL;
162
163         /* annoying, but the Anonymous really does have a session key, 
164            and it is all zeros! */
165         server_info->user_session_key = data_blob(NULL, 0);
166         server_info->lm_session_key = data_blob(NULL, 0);
167
168         server_info->account_name = talloc_strdup(server_info, user_info->client.account_name);
169         NT_STATUS_HAVE_NO_MEMORY(server_info->account_name);
170
171         server_info->domain_name = talloc_strdup(server_info, user_info->client.domain_name);
172         NT_STATUS_HAVE_NO_MEMORY(server_info->domain_name);
173
174         server_info->full_name = NULL;
175
176         server_info->logon_script = talloc_strdup(server_info, "");
177         NT_STATUS_HAVE_NO_MEMORY(server_info->logon_script);
178
179         server_info->profile_path = talloc_strdup(server_info, "");
180         NT_STATUS_HAVE_NO_MEMORY(server_info->profile_path);
181
182         server_info->home_directory = talloc_strdup(server_info, "");
183         NT_STATUS_HAVE_NO_MEMORY(server_info->home_directory);
184
185         server_info->home_drive = talloc_strdup(server_info, "");
186         NT_STATUS_HAVE_NO_MEMORY(server_info->home_drive);
187
188         server_info->last_logon = 0;
189         server_info->last_logoff = 0;
190         server_info->acct_expiry = 0;
191         server_info->last_password_change = 0;
192         server_info->allow_password_change = 0;
193         server_info->force_password_change = 0;
194
195         server_info->logon_count = 0;
196         server_info->bad_password_count = 0;
197
198         server_info->acct_flags = ACB_NORMAL;
199
200         server_info->authenticated = false;
201
202         *_server_info = server_info;
203
204         return nt_status;
205 }
206
207 static const struct auth_operations server_auth_ops = {
208         .name           = "server",
209         .get_challenge  = server_get_challenge,
210         .want_check     = server_want_check,
211         .check_password = server_check_password
212 };
213
214 _PUBLIC_ NTSTATUS auth_server_init(void)
215 {
216         NTSTATUS ret;
217
218         ret = auth_register(&server_auth_ops);
219         if (!NT_STATUS_IS_OK(ret)) {
220                 DEBUG(0,("Failed to register 'server' auth backend!\n"));
221                 return ret;
222         }
223
224         return ret;
225 }