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