s3-auth Hook checking passwords and generating session_info via the auth4_context
[samba.git] / auth / ntlmssp / ntlmssp.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 struct auth_session_info;
25
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
33 /**
34  * Callbacks for NTLMSSP - for both client and server operating modes
35  *
36  */
37
38 static const struct ntlmssp_callbacks {
39         enum ntlmssp_role role;
40         enum ntlmssp_message_type command;
41         NTSTATUS (*sync_fn)(struct gensec_security *gensec_security,
42                             TALLOC_CTX *out_mem_ctx,
43                             DATA_BLOB in, DATA_BLOB *out);
44 } ntlmssp_callbacks[] = {
45         {
46                 .role           = NTLMSSP_CLIENT,
47                 .command        = NTLMSSP_INITIAL,
48                 .sync_fn        = ntlmssp_client_initial,
49         },{
50                 .role           = NTLMSSP_SERVER,
51                 .command        = NTLMSSP_NEGOTIATE,
52                 .sync_fn        = gensec_ntlmssp_server_negotiate,
53         },{
54                 .role           = NTLMSSP_CLIENT,
55                 .command        = NTLMSSP_CHALLENGE,
56                 .sync_fn        = ntlmssp_client_challenge,
57         },{
58                 .role           = NTLMSSP_SERVER,
59                 .command        = NTLMSSP_AUTH,
60                 .sync_fn        = gensec_ntlmssp_server_auth,
61         }
62 };
63
64
65 static NTSTATUS gensec_ntlmssp_update_find(struct ntlmssp_state *ntlmssp_state,
66                                            const DATA_BLOB input, uint32_t *idx)
67 {
68         struct gensec_ntlmssp_context *gensec_ntlmssp =
69                 talloc_get_type_abort(ntlmssp_state->callback_private,
70                                       struct gensec_ntlmssp_context);
71         struct gensec_security *gensec_security = gensec_ntlmssp->gensec_security;
72         uint32_t ntlmssp_command;
73         uint32_t i;
74
75         if (ntlmssp_state->expected_state == NTLMSSP_DONE) {
76                 /* We are strict here because other modules, which we
77                  * don't fully control (such as GSSAPI) are also
78                  * strict, but are tested less often */
79
80                 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
81                 return NT_STATUS_INVALID_PARAMETER;
82         }
83
84         if (!input.length) {
85                 switch (ntlmssp_state->role) {
86                 case NTLMSSP_CLIENT:
87                         ntlmssp_command = NTLMSSP_INITIAL;
88                         break;
89                 case NTLMSSP_SERVER:
90                         if (gensec_security->want_features & GENSEC_FEATURE_DATAGRAM_MODE) {
91                                 /* 'datagram' mode - no neg packet */
92                                 ntlmssp_command = NTLMSSP_NEGOTIATE;
93                         } else {
94                                 /* This is normal in SPNEGO mech negotiation fallback */
95                                 DEBUG(2, ("Failed to parse NTLMSSP packet: zero length\n"));
96                                 return NT_STATUS_INVALID_PARAMETER;
97                         }
98                         break;
99                 }
100         } else {
101                 if (!msrpc_parse(ntlmssp_state,
102                                  &input, "Cd",
103                                  "NTLMSSP",
104                                  &ntlmssp_command)) {
105                         DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
106                         dump_data(2, input.data, input.length);
107                         return NT_STATUS_INVALID_PARAMETER;
108                 }
109         }
110
111         if (ntlmssp_command != ntlmssp_state->expected_state) {
112                 DEBUG(2, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command, ntlmssp_state->expected_state));
113                 return NT_STATUS_INVALID_PARAMETER;
114         }
115
116         for (i=0; i < ARRAY_SIZE(ntlmssp_callbacks); i++) {
117                 if (ntlmssp_callbacks[i].role == ntlmssp_state->role &&
118                     ntlmssp_callbacks[i].command == ntlmssp_command) {
119                         *idx = i;
120                         return NT_STATUS_OK;
121                 }
122         }
123
124         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
125                   ntlmssp_state->role, ntlmssp_command));
126
127         return NT_STATUS_INVALID_PARAMETER;
128 }
129
130 /**
131  * Next state function for the wrapped NTLMSSP state machine
132  *
133  * @param gensec_security GENSEC state, initialised to NTLMSSP
134  * @param out_mem_ctx The TALLOC_CTX for *out to be allocated on
135  * @param in The request, as a DATA_BLOB
136  * @param out The reply, as an talloc()ed DATA_BLOB, on *out_mem_ctx
137  * @return Error, MORE_PROCESSING_REQUIRED if a reply is sent,
138  *                or NT_STATUS_OK if the user is authenticated.
139  */
140
141 static NTSTATUS gensec_ntlmssp_update(struct gensec_security *gensec_security,
142                                       TALLOC_CTX *out_mem_ctx,
143                                       struct tevent_context *ev,
144                                       const DATA_BLOB input, DATA_BLOB *out)
145 {
146         struct gensec_ntlmssp_context *gensec_ntlmssp =
147                 talloc_get_type_abort(gensec_security->private_data,
148                                       struct gensec_ntlmssp_context);
149         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
150         NTSTATUS status;
151         uint32_t i;
152
153         *out = data_blob(NULL, 0);
154
155         if (!out_mem_ctx) {
156                 /* if the caller doesn't want to manage/own the memory,
157                    we can put it on our context */
158                 out_mem_ctx = ntlmssp_state;
159         }
160
161         status = gensec_ntlmssp_update_find(ntlmssp_state, input, &i);
162         NT_STATUS_NOT_OK_RETURN(status);
163
164         status = ntlmssp_callbacks[i].sync_fn(gensec_security, out_mem_ctx, input, out);
165         NT_STATUS_NOT_OK_RETURN(status);
166
167         return NT_STATUS_OK;
168 }
169
170 static const char *gensec_ntlmssp_oids[] = {
171         GENSEC_OID_NTLMSSP,
172         NULL
173 };
174
175 static const struct gensec_security_ops gensec_ntlmssp_security_ops = {
176         .name           = "ntlmssp",
177         .sasl_name      = GENSEC_SASL_NAME_NTLMSSP, /* "NTLM" */
178         .auth_type      = DCERPC_AUTH_TYPE_NTLMSSP,
179         .oid            = gensec_ntlmssp_oids,
180         .client_start   = gensec_ntlmssp_client_start,
181         .server_start   = gensec_ntlmssp_server_start,
182         .magic          = gensec_ntlmssp_magic,
183         .update         = gensec_ntlmssp_update,
184         .sig_size       = gensec_ntlmssp_sig_size,
185         .sign_packet    = gensec_ntlmssp_sign_packet,
186         .check_packet   = gensec_ntlmssp_check_packet,
187         .seal_packet    = gensec_ntlmssp_seal_packet,
188         .unseal_packet  = gensec_ntlmssp_unseal_packet,
189         .wrap           = gensec_ntlmssp_wrap,
190         .unwrap         = gensec_ntlmssp_unwrap,
191         .session_key    = gensec_ntlmssp_session_key,
192         .session_info   = gensec_ntlmssp_session_info,
193         .have_feature   = gensec_ntlmssp_have_feature,
194         .enabled        = true,
195         .priority       = GENSEC_NTLMSSP
196 };
197
198
199 _PUBLIC_ NTSTATUS gensec_ntlmssp_init(void)
200 {
201         NTSTATUS ret;
202
203         ret = gensec_register(&gensec_ntlmssp_security_ops);
204         if (!NT_STATUS_IS_OK(ret)) {
205                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
206                         gensec_ntlmssp_security_ops.name));
207                 return ret;
208         }
209
210         return ret;
211 }