autorid: initialize: link config to commonconfig as soon as it is allocated.
[Samba.git] / auth / ntlmssp / ntlmssp.c
blob916b376b1111e1e47c9c1175a168b32de281902d
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 struct auth_session_info;
26 #include "includes.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "auth/ntlmssp/ntlmssp_private.h"
29 #include "../libcli/auth/libcli_auth.h"
30 #include "librpc/gen_ndr/ndr_dcerpc.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/gensec/gensec_internal.h"
34 /**
35 * Callbacks for NTLMSSP - for both client and server operating modes
39 static const struct ntlmssp_callbacks {
40 enum ntlmssp_role role;
41 enum ntlmssp_message_type command;
42 NTSTATUS (*sync_fn)(struct gensec_security *gensec_security,
43 TALLOC_CTX *out_mem_ctx,
44 DATA_BLOB in, DATA_BLOB *out);
45 } ntlmssp_callbacks[] = {
47 .role = NTLMSSP_CLIENT,
48 .command = NTLMSSP_INITIAL,
49 .sync_fn = ntlmssp_client_initial,
50 },{
51 .role = NTLMSSP_SERVER,
52 .command = NTLMSSP_NEGOTIATE,
53 .sync_fn = gensec_ntlmssp_server_negotiate,
54 },{
55 .role = NTLMSSP_CLIENT,
56 .command = NTLMSSP_CHALLENGE,
57 .sync_fn = ntlmssp_client_challenge,
58 },{
59 .role = NTLMSSP_SERVER,
60 .command = NTLMSSP_AUTH,
61 .sync_fn = gensec_ntlmssp_server_auth,
66 static NTSTATUS gensec_ntlmssp_update_find(struct gensec_security *gensec_security,
67 struct gensec_ntlmssp_context *gensec_ntlmssp,
68 const DATA_BLOB input, uint32_t *idx)
70 uint32_t ntlmssp_command;
71 uint32_t i;
73 if (gensec_ntlmssp->ntlmssp_state->expected_state == NTLMSSP_DONE) {
74 /* We are strict here because other modules, which we
75 * don't fully control (such as GSSAPI) are also
76 * strict, but are tested less often */
78 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
79 return NT_STATUS_INVALID_PARAMETER;
82 if (!input.length) {
83 switch (gensec_ntlmssp->ntlmssp_state->role) {
84 case NTLMSSP_CLIENT:
85 ntlmssp_command = NTLMSSP_INITIAL;
86 break;
87 case NTLMSSP_SERVER:
88 if (gensec_security->want_features & GENSEC_FEATURE_DATAGRAM_MODE) {
89 /* 'datagram' mode - no neg packet */
90 ntlmssp_command = NTLMSSP_NEGOTIATE;
91 } else {
92 /* This is normal in SPNEGO mech negotiation fallback */
93 DEBUG(2, ("Failed to parse NTLMSSP packet: zero length\n"));
94 return NT_STATUS_INVALID_PARAMETER;
96 break;
98 } else {
99 if (!msrpc_parse(gensec_ntlmssp->ntlmssp_state,
100 &input, "Cd",
101 "NTLMSSP",
102 &ntlmssp_command)) {
103 DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
104 dump_data(2, input.data, input.length);
105 return NT_STATUS_INVALID_PARAMETER;
109 if (ntlmssp_command != gensec_ntlmssp->ntlmssp_state->expected_state) {
110 DEBUG(2, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command,
111 gensec_ntlmssp->ntlmssp_state->expected_state));
112 return NT_STATUS_INVALID_PARAMETER;
115 for (i=0; i < ARRAY_SIZE(ntlmssp_callbacks); i++) {
116 if (ntlmssp_callbacks[i].role == gensec_ntlmssp->ntlmssp_state->role &&
117 ntlmssp_callbacks[i].command == ntlmssp_command) {
118 *idx = i;
119 return NT_STATUS_OK;
123 DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
124 gensec_ntlmssp->ntlmssp_state->role, ntlmssp_command));
126 return NT_STATUS_INVALID_PARAMETER;
130 * Next state function for the wrapped NTLMSSP state machine
132 * @param gensec_security GENSEC state, initialised to NTLMSSP
133 * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
134 * @param in The request, as a DATA_BLOB
135 * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
136 * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
137 * or NT_STATUS_OK if the user is authenticated.
140 NTSTATUS gensec_ntlmssp_update(struct gensec_security *gensec_security,
141 TALLOC_CTX *out_mem_ctx,
142 struct tevent_context *ev,
143 const DATA_BLOB input, DATA_BLOB *out)
145 struct gensec_ntlmssp_context *gensec_ntlmssp =
146 talloc_get_type_abort(gensec_security->private_data,
147 struct gensec_ntlmssp_context);
148 struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
149 NTSTATUS status;
150 uint32_t i;
152 *out = data_blob(NULL, 0);
154 if (!out_mem_ctx) {
155 /* if the caller doesn't want to manage/own the memory,
156 we can put it on our context */
157 out_mem_ctx = ntlmssp_state;
160 status = gensec_ntlmssp_update_find(gensec_security, gensec_ntlmssp, input, &i);
161 NT_STATUS_NOT_OK_RETURN(status);
163 status = ntlmssp_callbacks[i].sync_fn(gensec_security, out_mem_ctx, input, out);
164 NT_STATUS_NOT_OK_RETURN(status);
166 return NT_STATUS_OK;
169 static const char *gensec_ntlmssp_oids[] = {
170 GENSEC_OID_NTLMSSP,
171 NULL
174 static const struct gensec_security_ops gensec_ntlmssp_security_ops = {
175 .name = "ntlmssp",
176 .sasl_name = GENSEC_SASL_NAME_NTLMSSP, /* "NTLM" */
177 .auth_type = DCERPC_AUTH_TYPE_NTLMSSP,
178 .oid = gensec_ntlmssp_oids,
179 .client_start = gensec_ntlmssp_client_start,
180 .server_start = gensec_ntlmssp_server_start,
181 .magic = gensec_ntlmssp_magic,
182 .update = gensec_ntlmssp_update,
183 .sig_size = gensec_ntlmssp_sig_size,
184 .sign_packet = gensec_ntlmssp_sign_packet,
185 .check_packet = gensec_ntlmssp_check_packet,
186 .seal_packet = gensec_ntlmssp_seal_packet,
187 .unseal_packet = gensec_ntlmssp_unseal_packet,
188 .wrap = gensec_ntlmssp_wrap,
189 .unwrap = gensec_ntlmssp_unwrap,
190 .session_key = gensec_ntlmssp_session_key,
191 .session_info = gensec_ntlmssp_session_info,
192 .have_feature = gensec_ntlmssp_have_feature,
193 .enabled = true,
194 .priority = GENSEC_NTLMSSP
198 _PUBLIC_ NTSTATUS gensec_ntlmssp_init(void)
200 NTSTATUS ret;
202 ret = gensec_register(&gensec_ntlmssp_security_ops);
203 if (!NT_STATUS_IS_OK(ret)) {
204 DEBUG(0,("Failed to register '%s' gensec backend!\n",
205 gensec_ntlmssp_security_ops.name));
206 return ret;
209 return ret;
212 uint32_t gensec_ntlmssp_neg_flags(struct gensec_security *gensec_security)
214 struct gensec_ntlmssp_context *gensec_ntlmssp;
215 if (gensec_security->ops != &gensec_ntlmssp_security_ops) {
216 return 0;
218 gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
219 struct gensec_ntlmssp_context);
220 return gensec_ntlmssp->ntlmssp_state->neg_flags;