Merge branch 'master' of ctdb into 'master' of samba
[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 #include "auth/gensec/gensec_internal.h"
33
34 /**
35  * Callbacks for NTLMSSP - for both client and server operating modes
36  *
37  */
38
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[] = {
46         {
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,
62         }
63 };
64
65
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)
69 {
70         uint32_t ntlmssp_command;
71         uint32_t i;
72
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 */
77
78                 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
79                 return NT_STATUS_INVALID_PARAMETER;
80         }
81
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;
95                         }
96                         break;
97                 }
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;
106                 }
107         }
108
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;
113         }
114
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;
120                 }
121         }
122
123         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
124                   gensec_ntlmssp->ntlmssp_state->role, ntlmssp_command));
125
126         return NT_STATUS_INVALID_PARAMETER;
127 }
128
129 /**
130  * Next state function for the wrapped NTLMSSP state machine
131  *
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.
138  */
139
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)
144 {
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;
151
152         *out = data_blob(NULL, 0);
153
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;
158         }
159
160         status = gensec_ntlmssp_update_find(gensec_security, gensec_ntlmssp, input, &i);
161         NT_STATUS_NOT_OK_RETURN(status);
162
163         status = ntlmssp_callbacks[i].sync_fn(gensec_security, out_mem_ctx, input, out);
164         NT_STATUS_NOT_OK_RETURN(status);
165
166         return NT_STATUS_OK;
167 }
168
169 static const char *gensec_ntlmssp_oids[] = {
170         GENSEC_OID_NTLMSSP,
171         NULL
172 };
173
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
195 };
196
197
198 _PUBLIC_ NTSTATUS gensec_ntlmssp_init(void)
199 {
200         NTSTATUS ret;
201
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;
207         }
208
209         return ret;
210 }
211
212 uint32_t gensec_ntlmssp_neg_flags(struct gensec_security *gensec_security)
213 {
214         struct gensec_ntlmssp_context *gensec_ntlmssp;
215         if (gensec_security->ops != &gensec_ntlmssp_security_ops) {
216                 return 0;
217         }
218         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
219                                                struct gensec_ntlmssp_context);
220         return gensec_ntlmssp->ntlmssp_state->neg_flags;
221 }