auth: Remove plugable password-check functions from gensec_ntlmssp
[samba.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/common_auth.h"
35 #include "param/param.h"
36
37
38 /**
39  * Return the credentials of a logged on user, including session keys
40  * etc.
41  *
42  * Only valid after a successful authentication
43  *
44  * May only be called once per authentication.
45  *
46  */
47
48 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
49                                      TALLOC_CTX *mem_ctx,
50                                      struct auth_session_info **session_info)
51 {
52         NTSTATUS nt_status;
53         struct gensec_ntlmssp_context *gensec_ntlmssp =
54                 talloc_get_type_abort(gensec_security->private_data,
55                                       struct gensec_ntlmssp_context);
56         uint32_t session_info_flags = 0;
57
58         if (gensec_security->want_features & GENSEC_FEATURE_UNIX_TOKEN) {
59                 session_info_flags |= AUTH_SESSION_INFO_UNIX_TOKEN;
60         }
61
62         session_info_flags |= AUTH_SESSION_INFO_DEFAULT_GROUPS;
63
64         if (gensec_security->auth_context && gensec_security->auth_context->generate_session_info) {
65                 nt_status = gensec_security->auth_context->generate_session_info(gensec_security->auth_context, mem_ctx, 
66                                                                                  gensec_ntlmssp->server_returned_info,
67                                                                                  gensec_ntlmssp->ntlmssp_state->user,
68                                                                                  session_info_flags,
69                                                                                  session_info);
70         } else {
71                 DEBUG(0, ("Cannot generate a session_info without the auth_context\n"));
72                 return NT_STATUS_INTERNAL_ERROR;
73         }
74
75         NT_STATUS_NOT_OK_RETURN(nt_status);
76
77         nt_status = gensec_ntlmssp_session_key(gensec_security, *session_info,
78                                                &(*session_info)->session_key);
79         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_USER_SESSION_KEY)) {
80                 (*session_info)->session_key = data_blob_null;
81                 nt_status = NT_STATUS_OK;
82         }
83
84         return nt_status;
85 }
86
87 /**
88  * Start NTLMSSP on the server side
89  *
90  */
91 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
92 {
93         NTSTATUS nt_status;
94         struct ntlmssp_state *ntlmssp_state;
95         struct gensec_ntlmssp_context *gensec_ntlmssp;
96         const char *netbios_name;
97         const char *netbios_domain;
98         const char *dns_name;
99         const char *dns_domain;
100
101         nt_status = gensec_ntlmssp_start(gensec_security);
102         NT_STATUS_NOT_OK_RETURN(nt_status);
103
104         gensec_ntlmssp =
105                 talloc_get_type_abort(gensec_security->private_data,
106                                       struct gensec_ntlmssp_context);
107
108         ntlmssp_state = talloc_zero(gensec_ntlmssp,
109                                     struct ntlmssp_state);
110         if (!ntlmssp_state) {
111                 return NT_STATUS_NO_MEMORY;
112         }
113         gensec_ntlmssp->ntlmssp_state = ntlmssp_state;
114
115         ntlmssp_state->role = NTLMSSP_SERVER;
116
117         ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
118
119         if (lpcfg_lanman_auth(gensec_security->settings->lp_ctx) &&
120             gensec_setting_bool(gensec_security->settings,
121                                 "ntlmssp_server", "allow_lm_key", false))
122         {
123                 ntlmssp_state->allow_lm_key = true;
124         }
125
126         ntlmssp_state->neg_flags =
127                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_VERSION;
128
129         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "128bit", true)) {
130                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;
131         }
132
133         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "56bit", true)) {
134                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;
135         }
136
137         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "keyexchange", true)) {
138                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;
139         }
140
141         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "alwayssign", true)) {
142                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;
143         }
144
145         if (gensec_setting_bool(gensec_security->settings, "ntlmssp_server", "ntlm2", true)) {
146                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;
147         }
148
149         if (gensec_security->want_features & GENSEC_FEATURE_SESSION_KEY) {
150                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
151         }
152         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
153                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
154         }
155         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
156                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
157                 ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
158         }
159
160         if (lpcfg_server_role(gensec_security->settings->lp_ctx) == ROLE_STANDALONE) {
161                 ntlmssp_state->server.is_standalone = true;
162         } else {
163                 ntlmssp_state->server.is_standalone = false;
164         }
165
166         if (gensec_security->settings->server_netbios_name) {
167                 netbios_name = gensec_security->settings->server_netbios_name;
168         } else {
169                 netbios_name = lpcfg_netbios_name(gensec_security->settings->lp_ctx);
170         }
171
172         if (gensec_security->settings->server_netbios_domain) {
173                 netbios_domain = gensec_security->settings->server_netbios_domain;
174         } else {
175                 netbios_domain = lpcfg_workgroup(gensec_security->settings->lp_ctx);
176         }
177
178         if (gensec_security->settings->server_dns_name) {
179                 dns_name = gensec_security->settings->server_dns_name;
180         } else {
181                 const char *dnsdomain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
182                 char *lower_netbiosname;
183
184                 lower_netbiosname = strlower_talloc(ntlmssp_state, netbios_name);
185                 NT_STATUS_HAVE_NO_MEMORY(lower_netbiosname);
186
187                 /* Find out the DNS host name */
188                 if (dnsdomain && dnsdomain[0] != '\0') {
189                         dns_name = talloc_asprintf(ntlmssp_state, "%s.%s",
190                                                    lower_netbiosname,
191                                                    dnsdomain);
192                         talloc_free(lower_netbiosname);
193                         NT_STATUS_HAVE_NO_MEMORY(dns_name);
194                 } else {
195                         dns_name = lower_netbiosname;
196                 }
197         }
198
199         if (gensec_security->settings->server_dns_domain) {
200                 dns_domain = gensec_security->settings->server_dns_domain;
201         } else {
202                 dns_domain = lpcfg_dnsdomain(gensec_security->settings->lp_ctx);
203         }
204
205         ntlmssp_state->server.netbios_name = talloc_strdup(ntlmssp_state, netbios_name);
206         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_name);
207
208         ntlmssp_state->server.netbios_domain = talloc_strdup(ntlmssp_state, netbios_domain);
209         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.netbios_domain);
210
211         ntlmssp_state->server.dns_name = talloc_strdup(ntlmssp_state, dns_name);
212         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_name);
213
214         ntlmssp_state->server.dns_domain = talloc_strdup(ntlmssp_state, dns_domain);
215         NT_STATUS_HAVE_NO_MEMORY(ntlmssp_state->server.dns_domain);
216
217         return NT_STATUS_OK;
218 }
219