s4:rpc_server/netlogon: prepare dcesrv_netr_LogonSamLogon_base for async processing
[Samba.git] / auth / ntlmssp / gensec_ntlmssp_server.c
blob561c7cff5bd3b83177548734d47e687bbb055cb8
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 handle NLTMSSP, client server side parsing
6 Copyright (C) Andrew Tridgell 2001
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8 Copyright (C) Stefan Metzmacher 2005
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.
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.
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/>.
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"
40 /**
41 * Return the credentials of a logged on user, including session keys
42 * etc.
44 * Only valid after a successful authentication
46 * May only be called once per authentication.
50 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
51 TALLOC_CTX *mem_ctx,
52 struct auth_session_info **session_info)
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;
60 if (gensec_security->want_features & GENSEC_FEATURE_UNIX_TOKEN) {
61 session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
64 session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
65 session_info_flags |= AUTH_SESSION_INFO_NTLM;
67 if (gensec_security->auth_context && gensec_security->auth_context->generate_session_info) {
68 nt_status = gensec_security->auth_context->generate_session_info(gensec_security->auth_context, mem_ctx,
69 gensec_ntlmssp->server_returned_info,
70 gensec_ntlmssp->ntlmssp_state->user,
71 session_info_flags,
72 session_info);
73 } else {
74 DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
75 return NT_STATUS_INTERNAL_ERROR;
78 NT_STATUS_NOT_OK_RETURN(nt_status);
80 nt_status = gensec_ntlmssp_session_key(gensec_security, *session_info,
81 &(*session_info)->session_key);
82 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_USER_SESSION_KEY)) {
83 (*session_info)->session_key = data_blob_null;
84 nt_status = NT_STATUS_OK;
87 return nt_status;
90 /**
91 * Start NTLMSSP on the server side
94 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
96 NTSTATUS nt_status;
97 struct ntlmssp_state *ntlmssp_state;
98 struct gensec_ntlmssp_context *gensec_ntlmssp;
99 const char *netbios_name;
100 const char *netbios_domain;
101 const char *dns_name;
102 const char *dns_domain;
103 enum server_role role;
105 role = lpcfg_server_role(gensec_security->settings->lp_ctx);
107 nt_status = gensec_ntlmssp_start(gensec_security);
108 NT_STATUS_NOT_OK_RETURN(nt_status);
110 gensec_ntlmssp =
111 talloc_get_type_abort(gensec_security->private_data,
112 struct gensec_ntlmssp_context);
114 ntlmssp_state = talloc_zero(gensec_ntlmssp,
115 struct ntlmssp_state);
116 if (!ntlmssp_state) {
117 return NT_STATUS_NO_MEMORY;
119 gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
121 ntlmssp_state->role = NTLMSSP_SERVER;
123 ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
125 ntlmssp_state->allow_lm_response =
126 lpcfg_lanman_auth(gensec_security->settings->lp_ctx);
128 if (ntlmssp_state->allow_lm_response &&
129 gensec_setting_bool(gensec_security->settings,
130 "ntlmssp_server", "allow_lm_key", false))
132 ntlmssp_state->allow_lm_key = true;
135 ntlmssp_state->force_old_spnego = false;
137 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "force_old_spnego", false)) {
139 * For testing Windows 2000 mode
141 ntlmssp_state->force_old_spnego = true;
144 ntlmssp_state->neg_flags =
145 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
147 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
148 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
151 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
152 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
155 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
156 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
159 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
160 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
163 if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
164 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
167 if (ntlmssp_state->allow_lm_key) {
168 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_LM_KEY;
172 * We always allow NTLMSSP_NEGOTIATE_SIGN and NTLMSSP_NEGOTIATE_SEAL.
174 * These will be removed if the client doesn't want them.
176 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
177 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
179 if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
180 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
182 if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
183 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
185 if (gensec_security->want_features & GENSEC_FEATURE_LDAP_STYLE) {
187 * We need to handle NTLMSSP_NEGOTIATE_SIGN as
188 * NTLMSSP_NEGOTIATE_SEAL if GENSEC_FEATURE_LDAP_STYLE
189 * is requested.
191 ntlmssp_state->force_wrap_seal = true;
194 if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
195 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
196 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
199 if (role == ROLE_STANDALONE) {
200 ntlmssp_state->server.is_standalone = true;
201 } else {
202 ntlmssp_state->server.is_standalone = false;
205 if (gensec_security->settings->server_netbios_name) {
206 netbios_name = gensec_security->settings->server_netbios_name;
207 } else {
208 netbios_name = lpcfg_netbios_name(gensec_security->settings->lp_ctx);
211 if (gensec_security->settings->server_netbios_domain) {
212 netbios_domain = gensec_security->settings->server_netbios_domain;
213 } else {
214 netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
217 if (gensec_security->settings->server_dns_name) {
218 dns_name = gensec_security->settings->server_dns_name;
219 } else {
220 const char *dnsdomain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
221 char *lower_netbiosname;
223 lower_netbiosname = strlower_talloc(ntlmssp_state, netbios_name);
224 NT_STATUS_HAVE_NO_MEMORY(lower_netbiosname);
226 /* Find out the DNS host name */
227 if (dnsdomain && dnsdomain[0] != '\0') {
228 dns_name = talloc_asprintf(ntlmssp_state, "%s.%s",
229 lower_netbiosname,
230 dnsdomain);
231 talloc_free(lower_netbiosname);
232 NT_STATUS_HAVE_NO_MEMORY(dns_name);
233 } else {
234 dns_name = lower_netbiosname;
238 if (gensec_security->settings->server_dns_domain) {
239 dns_domain = gensec_security->settings->server_dns_domain;
240 } else {
241 dns_domain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
244 ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
245 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_name);
247 ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
248 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_domain);
250 ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
251 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
253 ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
254 NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
256 ntlmssp_state->neg_flags |= ntlmssp_state->required_flags;
257 ntlmssp_state->conf_flags = ntlmssp_state->neg_flags;
259 return NT_STATUS_OK;