auth/ntlmssp: always allow NTLMSSP_NEGOTIATE_{SIGN,SEAL} in gensec_ntlmssp_server_start()
[nivanova/samba-autobuild/.git] / auth / ntlmssp / gensec_ntlmssp_server.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "system/network.h"
26 #include "lib/tsocket/tsocket.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "../librpc/gen_ndr/ndr_ntlmssp.h"
29 #include "auth/ntlmssp/ntlmssp_ndr.h"
30 #include "auth/ntlmssp/ntlmssp_private.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "../lib/crypto/crypto.h"
33 #include "auth/gensec/gensec.h"
34 #include "auth/gensec/gensec_internal.h"
35 #include "auth/common_auth.h"
36 #include "param/param.h"
37 #include "param/loadparm.h"
38 #include "libds/common/roles.h"
39
40 /**
41  * Return the credentials of a logged on user, including session keys
42  * etc.
43  *
44  * Only valid after a successful authentication
45  *
46  * May only be called once per authentication.
47  *
48  */
49
50 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
51                                      TALLOC_CTX *mem_ctx,
52                                      struct auth_session_info **session_info)
53 {
54         NTSTATUS nt_status;
55         struct gensec_ntlmssp_context *gensec_ntlmssp =
56                 talloc_get_type_abort(gensec_security->private_data,
57                                       struct gensec_ntlmssp_context);
58         uint32_t session_info_flags = 0;
59
60         if (gensec_security->want_features & GENSEC_FEATURE_UNIX_TOKEN) {
61                 session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
62         }
63
64         session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
65
66         if (gensec_security->auth_context && gensec_security->auth_context->generate_session_info) {
67                 nt_status = gensec_security->auth_context->generate_session_info(gensec_security->auth_context, mem_ctx, 
68                                                                                  gensec_ntlmssp->server_returned_info,
69                                                                                  gensec_ntlmssp->ntlmssp_state->user,
70                                                                                  session_info_flags,
71                                                                                  session_info);
72         } else {
73                 DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
74                 return NT_STATUS_INTERNAL_ERROR;
75         }
76
77         NT_STATUS_NOT_OK_RETURN(nt_status);
78
79         nt_status = gensec_ntlmssp_session_key(gensec_security, *session_info,
80                                                &(*session_info)->session_key);
81         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_USER_SESSION_KEY)) {
82                 (*session_info)->session_key = data_blob_null;
83                 nt_status = NT_STATUS_OK;
84         }
85
86         return nt_status;
87 }
88
89 /**
90  * Start NTLMSSP on the server side
91  *
92  */
93 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
94 {
95         NTSTATUS nt_status;
96         struct ntlmssp_state *ntlmssp_state;
97         struct gensec_ntlmssp_context *gensec_ntlmssp;
98         const char *netbios_name;
99         const char *netbios_domain;
100         const char *dns_name;
101         const char *dns_domain;
102         enum server_role role;
103
104         role = lpcfg_server_role(gensec_security->settings->lp_ctx);
105
106         nt_status = gensec_ntlmssp_start(gensec_security);
107         NT_STATUS_NOT_OK_RETURN(nt_status);
108
109         gensec_ntlmssp =
110                 talloc_get_type_abort(gensec_security->private_data,
111                                       struct gensec_ntlmssp_context);
112
113         ntlmssp_state = talloc_zero(gensec_ntlmssp,
114                                     struct ntlmssp_state);
115         if (!ntlmssp_state) {
116                 return NT_STATUS_NO_MEMORY;
117         }
118         gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
119
120         ntlmssp_state->role = NTLMSSP_SERVER;
121
122         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
123
124         ntlmssp_state->allow_lm_response =
125                 lpcfg_lanman_auth(gensec_security->settings->lp_ctx);
126
127         if (ntlmssp_state->allow_lm_response &&
128             gensec_setting_bool(gensec_security->settings,
129                                 "ntlmssp_server", "allow_lm_key", false))
130         {
131                 ntlmssp_state->allow_lm_key = true;
132         }
133
134         ntlmssp_state->force_old_spnego = false;
135
136         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "force_old_spnego", false)) {
137                 /*
138                  * For testing Windows 2000 mode
139                  */
140                 ntlmssp_state->force_old_spnego = true;
141         }
142
143         ntlmssp_state->neg_flags =
144                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
145
146         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
147                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
148         }
149
150         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
151                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
152         }
153
154         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
155                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
156         }
157
158         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
159                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
160         }
161
162         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
163                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
164         }
165
166         if (ntlmssp_state->allow_lm_key) {
167                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
168         }
169
170         /*
171          * We always allow NTLMSSP_NEGOTIATE_SIGN and NTLMSSP_NEGOTIATE_SEAL.
172          *
173          * These will be removed if the client doesn't want them.
174          */
175         ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
176         ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
177
178         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
179                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
180         }
181         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
182                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
183
184                 if (gensec_security->want_features & GENSEC_FEATURE_LDAP_STYLE) {
185                         /*
186                          * We need to handle NTLMSSP_NEGOTIATE_SIGN as
187                          * NTLMSSP_NEGOTIATE_SEAL if GENSEC_FEATURE_LDAP_STYLE
188                          * is requested.
189                          */
190                         ntlmssp_state->force_wrap_seal = true;
191                 }
192         }
193         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
194                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
195                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
196         }
197
198         if (role == ROLE_STANDALONE) {
199                 ntlmssp_state->server.is_standalone = true;
200         } else {
201                 ntlmssp_state->server.is_standalone = false;
202         }
203
204         if (gensec_security->settings->server_netbios_name) {
205                 netbios_name = gensec_security->settings->server_netbios_name;
206         } else {
207                 netbios_name = lpcfg_netbios_name(gensec_security->settings->lp_ctx);
208         }
209
210         if (gensec_security->settings->server_netbios_domain) {
211                 netbios_domain = gensec_security->settings->server_netbios_domain;
212         } else {
213                 netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
214         }
215
216         if (gensec_security->settings->server_dns_name) {
217                 dns_name = gensec_security->settings->server_dns_name;
218         } else {
219                 const char *dnsdomain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
220                 char *lower_netbiosname;
221
222                 lower_netbiosname = strlower_talloc(ntlmssp_state, netbios_name);
223                 NT_STATUS_HAVE_NO_MEMORY(lower_netbiosname);
224
225                 /* Find out the DNS host name */
226                 if (dnsdomain && dnsdomain[0] != '\0') {
227                         dns_name = talloc_asprintf(ntlmssp_state, "%s.%s",
228                                                    lower_netbiosname,
229                                                    dnsdomain);
230                         talloc_free(lower_netbiosname);
231                         NT_STATUS_HAVE_NO_MEMORY(dns_name);
232                 } else {
233                         dns_name = lower_netbiosname;
234                 }
235         }
236
237         if (gensec_security->settings->server_dns_domain) {
238                 dns_domain = gensec_security->settings->server_dns_domain;
239         } else {
240                 dns_domain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
241         }
242
243         ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
244         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_name);
245
246         ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
247         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_domain);
248
249         ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
250         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
251
252         ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
253         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
254
255         ntlmssp_state->neg_flags |= ntlmssp_state->required_flags;
256         ntlmssp_state->conf_flags = ntlmssp_state->neg_flags;
257
258         return NT_STATUS_OK;
259 }
260