s3:auth Change auth_ntlmssp_server_info API to return NTSTATUS
[metze/samba/wip.git] / source3 / smbd / sesssetup.c
1 /*
2    Unix SMB/CIFS implementation.
3    handle SMBsessionsetup
4    Copyright (C) Andrew Tridgell 1998-2001
5    Copyright (C) Andrew Bartlett      2001
6    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2002
7    Copyright (C) Luke Howard          2003
8    Copyright (C) Volker Lendecke      2007
9    Copyright (C) Jeremy Allison       2007
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "smbd/globals.h"
27 #include "../libcli/auth/spnego.h"
28 #include "../libcli/auth/ntlmssp.h"
29 #include "librpc/gen_ndr/messaging.h"
30
31 /* For split krb5 SPNEGO blobs. */
32 struct pending_auth_data {
33         struct pending_auth_data *prev, *next;
34         uint16 vuid; /* Tag for this entry. */
35         uint16 smbpid; /* Alternate tag for this entry. */
36         size_t needed_len;
37         DATA_BLOB partial_data;
38 };
39
40 /*
41   on a logon error possibly map the error to success if "map to guest"
42   is set approriately
43 */
44 static NTSTATUS do_map_to_guest(NTSTATUS status,
45                                 struct auth_serversupplied_info **server_info,
46                                 const char *user, const char *domain)
47 {
48         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
49                 if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) ||
50                     (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
51                         DEBUG(3,("No such user %s [%s] - using guest account\n",
52                                  user, domain));
53                         status = make_server_info_guest(NULL, server_info);
54                 }
55         }
56
57         if (NT_STATUS_EQUAL(status, NT_STATUS_WRONG_PASSWORD)) {
58                 if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD) {
59                         DEBUG(3,("Registered username %s for guest access\n",
60                                 user));
61                         status = make_server_info_guest(NULL, server_info);
62                 }
63         }
64
65         return status;
66 }
67
68 /****************************************************************************
69  Add the standard 'Samba' signature to the end of the session setup.
70 ****************************************************************************/
71
72 static int push_signature(uint8 **outbuf)
73 {
74         char *lanman;
75         int result, tmp;
76
77         result = 0;
78
79         tmp = message_push_string(outbuf, "Unix", STR_TERMINATE);
80
81         if (tmp == -1) return -1;
82         result += tmp;
83
84         if (asprintf(&lanman, "Samba %s", samba_version_string()) != -1) {
85                 tmp = message_push_string(outbuf, lanman, STR_TERMINATE);
86                 SAFE_FREE(lanman);
87         }
88         else {
89                 tmp = message_push_string(outbuf, "Samba", STR_TERMINATE);
90         }
91
92         if (tmp == -1) return -1;
93         result += tmp;
94
95         tmp = message_push_string(outbuf, lp_workgroup(), STR_TERMINATE);
96
97         if (tmp == -1) return -1;
98         result += tmp;
99
100         return result;
101 }
102
103 /****************************************************************************
104  Send a security blob via a session setup reply.
105 ****************************************************************************/
106
107 static void reply_sesssetup_blob(struct smb_request *req,
108                                  DATA_BLOB blob,
109                                  NTSTATUS nt_status)
110 {
111         if (!NT_STATUS_IS_OK(nt_status) &&
112             !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
113                 reply_nterror(req, nt_status_squash(nt_status));
114                 return;
115         }
116
117         nt_status = nt_status_squash(nt_status);
118         SIVAL(req->outbuf, smb_rcls, NT_STATUS_V(nt_status));
119         SSVAL(req->outbuf, smb_vwv0, 0xFF); /* no chaining possible */
120         SSVAL(req->outbuf, smb_vwv3, blob.length);
121
122         if ((message_push_blob(&req->outbuf, blob) == -1)
123             || (push_signature(&req->outbuf) == -1)) {
124                 reply_nterror(req, NT_STATUS_NO_MEMORY);
125         }
126 }
127
128 /****************************************************************************
129  Do a 'guest' logon, getting back the
130 ****************************************************************************/
131
132 static NTSTATUS check_guest_password(struct auth_serversupplied_info **server_info)
133 {
134         struct auth_context *auth_context;
135         struct auth_usersupplied_info *user_info = NULL;
136
137         NTSTATUS nt_status;
138         unsigned char chal[8];
139
140         ZERO_STRUCT(chal);
141
142         DEBUG(3,("Got anonymous request\n"));
143
144         if (!NT_STATUS_IS_OK(nt_status = make_auth_context_fixed(&auth_context,
145                                         chal))) {
146                 return nt_status;
147         }
148
149         if (!make_user_info_guest(&user_info)) {
150                 (auth_context->free)(&auth_context);
151                 return NT_STATUS_NO_MEMORY;
152         }
153
154         nt_status = auth_context->check_ntlm_password(auth_context,
155                                                 user_info,
156                                                 server_info);
157         (auth_context->free)(&auth_context);
158         free_user_info(&user_info);
159         return nt_status;
160 }
161
162
163 #ifdef HAVE_KRB5
164
165 #if 0
166 /* Experiment that failed. See "only happens with a KDC" comment below. */
167 /****************************************************************************
168  Cerate a clock skew error blob for a Windows client.
169 ****************************************************************************/
170
171 static bool make_krb5_skew_error(DATA_BLOB *pblob_out)
172 {
173         krb5_context context = NULL;
174         krb5_error_code kerr = 0;
175         krb5_data reply;
176         krb5_principal host_princ = NULL;
177         char *host_princ_s = NULL;
178         bool ret = False;
179
180         *pblob_out = data_blob_null;
181
182         initialize_krb5_error_table();
183         kerr = krb5_init_context(&context);
184         if (kerr) {
185                 return False;
186         }
187         /* Create server principal. */
188         asprintf(&host_princ_s, "%s$@%s", global_myname(), lp_realm());
189         if (!host_princ_s) {
190                 goto out;
191         }
192         strlower_m(host_princ_s);
193
194         kerr = smb_krb5_parse_name(context, host_princ_s, &host_princ);
195         if (kerr) {
196                 DEBUG(10,("make_krb5_skew_error: smb_krb5_parse_name failed "
197                         "for name %s: Error %s\n",
198                         host_princ_s, error_message(kerr) ));
199                 goto out;
200         }
201
202         kerr = smb_krb5_mk_error(context, KRB5KRB_AP_ERR_SKEW,
203                         host_princ, &reply);
204         if (kerr) {
205                 DEBUG(10,("make_krb5_skew_error: smb_krb5_mk_error "
206                         "failed: Error %s\n",
207                         error_message(kerr) ));
208                 goto out;
209         }
210
211         *pblob_out = data_blob(reply.data, reply.length);
212         kerberos_free_data_contents(context,&reply);
213         ret = True;
214
215   out:
216
217         if (host_princ_s) {
218                 SAFE_FREE(host_princ_s);
219         }
220         if (host_princ) {
221                 krb5_free_principal(context, host_princ);
222         }
223         krb5_free_context(context);
224         return ret;
225 }
226 #endif
227
228 /****************************************************************************
229  Reply to a session setup spnego negotiate packet for kerberos.
230 ****************************************************************************/
231
232 static void reply_spnego_kerberos(struct smb_request *req,
233                                   DATA_BLOB *secblob,
234                                   const char *mechOID,
235                                   uint16 vuid,
236                                   bool *p_invalidate_vuid)
237 {
238         TALLOC_CTX *mem_ctx;
239         DATA_BLOB ticket;
240         char *client, *p, *domain;
241         fstring netbios_domain_name;
242         struct passwd *pw;
243         fstring user;
244         int sess_vuid = req->vuid;
245         NTSTATUS ret = NT_STATUS_OK;
246         DATA_BLOB ap_rep, ap_rep_wrapped, response;
247         struct auth_serversupplied_info *server_info = NULL;
248         DATA_BLOB session_key = data_blob_null;
249         uint8 tok_id[2];
250         DATA_BLOB nullblob = data_blob_null;
251         fstring real_username;
252         bool map_domainuser_to_guest = False;
253         bool username_was_mapped;
254         struct PAC_LOGON_INFO *logon_info = NULL;
255         struct smbd_server_connection *sconn = smbd_server_conn;
256
257         ZERO_STRUCT(ticket);
258         ZERO_STRUCT(ap_rep);
259         ZERO_STRUCT(ap_rep_wrapped);
260         ZERO_STRUCT(response);
261
262         /* Normally we will always invalidate the intermediate vuid. */
263         *p_invalidate_vuid = True;
264
265         mem_ctx = talloc_init("reply_spnego_kerberos");
266         if (mem_ctx == NULL) {
267                 reply_nterror(req, nt_status_squash(NT_STATUS_NO_MEMORY));
268                 return;
269         }
270
271         if (!spnego_parse_krb5_wrap(*secblob, &ticket, tok_id)) {
272                 talloc_destroy(mem_ctx);
273                 reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
274                 return;
275         }
276
277         ret = ads_verify_ticket(mem_ctx, lp_realm(), 0, &ticket,
278                                 &client, &logon_info, &ap_rep,
279                                 &session_key, True);
280
281         data_blob_free(&ticket);
282
283         if (!NT_STATUS_IS_OK(ret)) {
284 #if 0
285                 /* Experiment that failed.
286                  * See "only happens with a KDC" comment below. */
287
288                 if (NT_STATUS_EQUAL(ret, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
289
290                         /*
291                          * Windows in this case returns
292                          * NT_STATUS_MORE_PROCESSING_REQUIRED
293                          * with a negTokenTarg blob containing an krb5_error
294                          * struct ASN1 encoded containing KRB5KRB_AP_ERR_SKEW.
295                          * The client then fixes its clock and continues rather
296                          * than giving an error. JRA.
297                          * -- Looks like this only happens with a KDC. JRA.
298                          */
299
300                         bool ok = make_krb5_skew_error(&ap_rep);
301                         if (!ok) {
302                                 talloc_destroy(mem_ctx);
303                                 return ERROR_NT(nt_status_squash(
304                                                 NT_STATUS_LOGON_FAILURE));
305                         }
306                         ap_rep_wrapped = spnego_gen_krb5_wrap(ap_rep,
307                                         TOK_ID_KRB_ERROR);
308                         response = spnego_gen_auth_response(&ap_rep_wrapped,
309                                         ret, OID_KERBEROS5_OLD);
310                         reply_sesssetup_blob(conn, inbuf, outbuf, response,
311                                         NT_STATUS_MORE_PROCESSING_REQUIRED);
312
313                         /*
314                          * In this one case we don't invalidate the
315                          * intermediate vuid as we're expecting the client
316                          * to re-use it for the next sessionsetupX packet. JRA.
317                          */
318
319                         *p_invalidate_vuid = False;
320
321                         data_blob_free(&ap_rep);
322                         data_blob_free(&ap_rep_wrapped);
323                         data_blob_free(&response);
324                         talloc_destroy(mem_ctx);
325                         return -1; /* already replied */
326                 }
327 #else
328                 if (!NT_STATUS_EQUAL(ret, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
329                         ret = NT_STATUS_LOGON_FAILURE;
330                 }
331 #endif
332                 DEBUG(1,("Failed to verify incoming ticket with error %s!\n",
333                                 nt_errstr(ret)));
334                 talloc_destroy(mem_ctx);
335                 reply_nterror(req, nt_status_squash(ret));
336                 return;
337         }
338
339         DEBUG(3,("Ticket name is [%s]\n", client));
340
341         p = strchr_m(client, '@');
342         if (!p) {
343                 DEBUG(3,("Doesn't look like a valid principal\n"));
344                 data_blob_free(&ap_rep);
345                 data_blob_free(&session_key);
346                 talloc_destroy(mem_ctx);
347                 reply_nterror(req,nt_status_squash(NT_STATUS_LOGON_FAILURE));
348                 return;
349         }
350
351         *p = 0;
352
353         /* save the PAC data if we have it */
354
355         if (logon_info) {
356                 netsamlogon_cache_store( client, &logon_info->info3 );
357         }
358
359         if (!strequal(p+1, lp_realm())) {
360                 DEBUG(3,("Ticket for foreign realm %s@%s\n", client, p+1));
361                 if (!lp_allow_trusted_domains()) {
362                         data_blob_free(&ap_rep);
363                         data_blob_free(&session_key);
364                         talloc_destroy(mem_ctx);
365                         reply_nterror(req, nt_status_squash(
366                                               NT_STATUS_LOGON_FAILURE));
367                         return;
368                 }
369         }
370
371         /* this gives a fully qualified user name (ie. with full realm).
372            that leads to very long usernames, but what else can we do? */
373
374         domain = p+1;
375
376         if (logon_info && logon_info->info3.base.domain.string) {
377                 fstrcpy(netbios_domain_name,
378                         logon_info->info3.base.domain.string);
379                 domain = netbios_domain_name;
380                 DEBUG(10, ("Mapped to [%s] (using PAC)\n", domain));
381
382         } else {
383
384                 /* If we have winbind running, we can (and must) shorten the
385                    username by using the short netbios name. Otherwise we will
386                    have inconsistent user names. With Kerberos, we get the
387                    fully qualified realm, with ntlmssp we get the short
388                    name. And even w2k3 does use ntlmssp if you for example
389                    connect to an ip address. */
390
391                 wbcErr wbc_status;
392                 struct wbcDomainInfo *info = NULL;
393
394                 DEBUG(10, ("Mapping [%s] to short name\n", domain));
395
396                 wbc_status = wbcDomainInfo(domain, &info);
397
398                 if (WBC_ERROR_IS_OK(wbc_status)) {
399
400                         fstrcpy(netbios_domain_name,
401                                 info->short_name);
402
403                         wbcFreeMemory(info);
404                         domain = netbios_domain_name;
405                         DEBUG(10, ("Mapped to [%s] (using Winbind)\n", domain));
406                 } else {
407                         DEBUG(3, ("Could not find short name: %s\n",
408                                 wbcErrorString(wbc_status)));
409                 }
410         }
411
412         fstr_sprintf(user, "%s%c%s", domain, *lp_winbind_separator(), client);
413
414         /* lookup the passwd struct, create a new user if necessary */
415
416         username_was_mapped = map_username(user);
417
418         pw = smb_getpwnam( mem_ctx, user, real_username, True );
419
420         if (pw) {
421                 /* if a real user check pam account restrictions */
422                 /* only really perfomed if "obey pam restriction" is true */
423                 /* do this before an eventual mapping to guest occurs */
424                 ret = smb_pam_accountcheck(pw->pw_name);
425                 if (  !NT_STATUS_IS_OK(ret)) {
426                         DEBUG(1,("PAM account restriction "
427                                 "prevents user login\n"));
428                         data_blob_free(&ap_rep);
429                         data_blob_free(&session_key);
430                         TALLOC_FREE(mem_ctx);
431                         reply_nterror(req, nt_status_squash(ret));
432                         return;
433                 }
434         }
435
436         if (!pw) {
437
438                 /* this was originally the behavior of Samba 2.2, if a user
439                    did not have a local uid but has been authenticated, then
440                    map them to a guest account */
441
442                 if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID){
443                         map_domainuser_to_guest = True;
444                         fstrcpy(user,lp_guestaccount());
445                         pw = smb_getpwnam( mem_ctx, user, real_username, True );
446                 }
447
448                 /* extra sanity check that the guest account is valid */
449
450                 if ( !pw ) {
451                         DEBUG(1,("Username %s is invalid on this system\n",
452                                 user));
453                         data_blob_free(&ap_rep);
454                         data_blob_free(&session_key);
455                         TALLOC_FREE(mem_ctx);
456                         reply_nterror(req, nt_status_squash(
457                                               NT_STATUS_LOGON_FAILURE));
458                         return;
459                 }
460         }
461
462         /* setup the string used by %U */
463
464         sub_set_smb_name( real_username );
465         reload_services(True);
466
467         if ( map_domainuser_to_guest ) {
468                 make_server_info_guest(NULL, &server_info);
469         } else if (logon_info) {
470                 /* pass the unmapped username here since map_username()
471                    will be called again from inside make_server_info_info3() */
472
473                 ret = make_server_info_info3(mem_ctx, client, domain,
474                                              &server_info, &logon_info->info3);
475                 if ( !NT_STATUS_IS_OK(ret) ) {
476                         DEBUG(1,("make_server_info_info3 failed: %s!\n",
477                                  nt_errstr(ret)));
478                         data_blob_free(&ap_rep);
479                         data_blob_free(&session_key);
480                         TALLOC_FREE(mem_ctx);
481                         reply_nterror(req, nt_status_squash(ret));
482                         return;
483                 }
484
485         } else {
486                 /*
487                  * We didn't get a PAC, we have to make up the user
488                  * ourselves. Try to ask the pdb backend to provide
489                  * SID consistency with ntlmssp session setup
490                  */
491                 struct samu *sampass;
492
493                 sampass = samu_new(talloc_tos());
494                 if (sampass == NULL) {
495                         ret = NT_STATUS_NO_MEMORY;
496                         data_blob_free(&ap_rep);
497                         data_blob_free(&session_key);
498                         TALLOC_FREE(mem_ctx);
499                         reply_nterror(req, nt_status_squash(ret));
500                         return;
501                 }
502
503                 if (pdb_getsampwnam(sampass, real_username)) {
504                         DEBUG(10, ("found user %s in passdb, calling "
505                                    "make_server_info_sam\n", real_username));
506                         ret = make_server_info_sam(&server_info, sampass);
507                         TALLOC_FREE(sampass);
508                 } else {
509                         /*
510                          * User not in passdb, make it up artificially
511                          */
512                         TALLOC_FREE(sampass);
513                         DEBUG(10, ("didn't find user %s in passdb, calling "
514                                    "make_server_info_pw\n", real_username));
515                         ret = make_server_info_pw(&server_info, real_username,
516                                                   pw);
517                 }
518
519                 if ( !NT_STATUS_IS_OK(ret) ) {
520                         DEBUG(1,("make_server_info_[sam|pw] failed: %s!\n",
521                                  nt_errstr(ret)));
522                         data_blob_free(&ap_rep);
523                         data_blob_free(&session_key);
524                         TALLOC_FREE(mem_ctx);
525                         reply_nterror(req, nt_status_squash(ret));
526                         return;
527                 }
528
529                 /* make_server_info_pw does not set the domain. Without this
530                  * we end up with the local netbios name in substitutions for
531                  * %D. */
532
533                 if (server_info->info3 != NULL) {
534                         server_info->info3->base.domain.string =
535                                 talloc_strdup(server_info->info3, domain);
536                 }
537         }
538
539         server_info->nss_token |= username_was_mapped;
540
541         /* we need to build the token for the user. make_server_info_guest()
542            already does this */
543
544         if ( !server_info->ptok ) {
545                 ret = create_local_token( server_info );
546                 if ( !NT_STATUS_IS_OK(ret) ) {
547                         DEBUG(10,("failed to create local token: %s\n",
548                                 nt_errstr(ret)));
549                         data_blob_free(&ap_rep);
550                         data_blob_free(&session_key);
551                         TALLOC_FREE( mem_ctx );
552                         TALLOC_FREE( server_info );
553                         reply_nterror(req, nt_status_squash(ret));
554                         return;
555                 }
556         }
557
558         if (!is_partial_auth_vuid(sconn, sess_vuid)) {
559                 sess_vuid = register_initial_vuid(sconn);
560         }
561
562         data_blob_free(&server_info->user_session_key);
563         server_info->user_session_key = session_key;
564         talloc_steal(server_info, session_key.data);
565
566         session_key = data_blob_null;
567
568         /* register_existing_vuid keeps the server info */
569         /* register_existing_vuid takes ownership of session_key on success,
570          * no need to free after this on success. A better interface would copy
571          * it.... */
572
573         sess_vuid = register_existing_vuid(sconn,
574                                         sess_vuid,
575                                         server_info,
576                                         nullblob,
577                                         client);
578
579         reply_outbuf(req, 4, 0);
580         SSVAL(req->outbuf,smb_uid,sess_vuid);
581
582         if (sess_vuid == UID_FIELD_INVALID ) {
583                 ret = NT_STATUS_LOGON_FAILURE;
584         } else {
585                 /* current_user_info is changed on new vuid */
586                 reload_services( True );
587
588                 SSVAL(req->outbuf, smb_vwv3, 0);
589
590                 if (server_info->guest) {
591                         SSVAL(req->outbuf,smb_vwv2,1);
592                 }
593
594                 SSVAL(req->outbuf, smb_uid, sess_vuid);
595
596                 /* Successful logon. Keep this vuid. */
597                 *p_invalidate_vuid = False;
598         }
599
600         /* wrap that up in a nice GSS-API wrapping */
601         if (NT_STATUS_IS_OK(ret)) {
602                 ap_rep_wrapped = spnego_gen_krb5_wrap(ap_rep,
603                                 TOK_ID_KRB_AP_REP);
604         } else {
605                 ap_rep_wrapped = data_blob_null;
606         }
607         response = spnego_gen_auth_response(&ap_rep_wrapped, ret,
608                         mechOID);
609         reply_sesssetup_blob(req, response, ret);
610
611         data_blob_free(&ap_rep);
612         data_blob_free(&ap_rep_wrapped);
613         data_blob_free(&response);
614         TALLOC_FREE(mem_ctx);
615 }
616
617 #endif
618
619 /****************************************************************************
620  Send a session setup reply, wrapped in SPNEGO.
621  Get vuid and check first.
622  End the NTLMSSP exchange context if we are OK/complete fail
623  This should be split into two functions, one to handle each
624  leg of the NTLM auth steps.
625 ***************************************************************************/
626
627 static void reply_spnego_ntlmssp(struct smb_request *req,
628                                  uint16 vuid,
629                                  struct auth_ntlmssp_state **auth_ntlmssp_state,
630                                  DATA_BLOB *ntlmssp_blob, NTSTATUS nt_status,
631                                  const char *OID,
632                                  bool wrap)
633 {
634         bool do_invalidate = true;
635         DATA_BLOB response;
636         struct auth_serversupplied_info *server_info = NULL;
637         struct smbd_server_connection *sconn = smbd_server_conn;
638
639         if (NT_STATUS_IS_OK(nt_status)) {
640                 nt_status = auth_ntlmssp_server_info(talloc_tos(), (*auth_ntlmssp_state), &server_info);
641         } else {
642                 /* Note that this server_info won't have a session
643                  * key.  But for map to guest, that's exactly the right
644                  * thing - we can't reasonably guess the key the
645                  * client wants, as the password was wrong */
646                 nt_status = do_map_to_guest(nt_status,
647                                             &server_info,
648                                             auth_ntlmssp_get_username(*auth_ntlmssp_state),
649                                             auth_ntlmssp_get_domain(*auth_ntlmssp_state));
650         }
651
652         reply_outbuf(req, 4, 0);
653
654         SSVAL(req->outbuf, smb_uid, vuid);
655
656         if (NT_STATUS_IS_OK(nt_status)) {
657                 DATA_BLOB nullblob = data_blob_null;
658
659                 if (!is_partial_auth_vuid(sconn, vuid)) {
660                         nt_status = NT_STATUS_LOGON_FAILURE;
661                         goto out;
662                 }
663
664                 /* register_existing_vuid keeps the server info */
665                 if (register_existing_vuid(sconn, vuid,
666                                            server_info, nullblob,
667                                            auth_ntlmssp_get_username(*auth_ntlmssp_state)) !=
668                                            vuid) {
669                         /* The problem is, *auth_ntlmssp_state points
670                          * into the vuser this will have
671                          * talloc_free()'ed in
672                          * register_existing_vuid() */
673                         do_invalidate = false;
674                         nt_status = NT_STATUS_LOGON_FAILURE;
675                         goto out;
676                 }
677
678                 /* current_user_info is changed on new vuid */
679                 reload_services( True );
680
681                 SSVAL(req->outbuf, smb_vwv3, 0);
682
683                 if (server_info->guest) {
684                         SSVAL(req->outbuf,smb_vwv2,1);
685                 }
686         }
687
688   out:
689
690         if (wrap) {
691                 response = spnego_gen_auth_response(ntlmssp_blob,
692                                 nt_status, OID);
693         } else {
694                 response = *ntlmssp_blob;
695         }
696
697         reply_sesssetup_blob(req, response, nt_status);
698         if (wrap) {
699                 data_blob_free(&response);
700         }
701
702         /* NT_STATUS_MORE_PROCESSING_REQUIRED from our NTLMSSP code tells us,
703            and the other end, that we are not finished yet. */
704
705         if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
706                 /* NB. This is *NOT* an error case. JRA */
707                 if (do_invalidate) {
708                         auth_ntlmssp_end(auth_ntlmssp_state);
709                         if (!NT_STATUS_IS_OK(nt_status)) {
710                                 /* Kill the intermediate vuid */
711                                 invalidate_vuid(sconn, vuid);
712                         }
713                 }
714         }
715 }
716
717 /****************************************************************************
718  Is this a krb5 mechanism ?
719 ****************************************************************************/
720
721 NTSTATUS parse_spnego_mechanisms(DATA_BLOB blob_in,
722                 DATA_BLOB *pblob_out,
723                 char **kerb_mechOID)
724 {
725         char *OIDs[ASN1_MAX_OIDS];
726         int i;
727         NTSTATUS ret = NT_STATUS_OK;
728
729         *kerb_mechOID = NULL;
730
731         /* parse out the OIDs and the first sec blob */
732         if (!parse_negTokenTarg(blob_in, OIDs, pblob_out)) {
733                 return NT_STATUS_LOGON_FAILURE;
734         }
735
736         /* only look at the first OID for determining the mechToken --
737            according to RFC2478, we should choose the one we want
738            and renegotiate, but i smell a client bug here..
739
740            Problem observed when connecting to a member (samba box)
741            of an AD domain as a user in a Samba domain.  Samba member
742            server sent back krb5/mskrb5/ntlmssp as mechtypes, but the
743            client (2ksp3) replied with ntlmssp/mskrb5/krb5 and an
744            NTLMSSP mechtoken.                 --jerry              */
745
746 #ifdef HAVE_KRB5
747         if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 ||
748             strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
749                 *kerb_mechOID = SMB_STRDUP(OIDs[0]);
750                 if (*kerb_mechOID == NULL) {
751                         ret = NT_STATUS_NO_MEMORY;
752                 }
753         }
754 #endif
755
756         for (i=0;OIDs[i];i++) {
757                 DEBUG(5,("parse_spnego_mechanisms: Got OID %s\n", OIDs[i]));
758                 talloc_free(OIDs[i]);
759         }
760         return ret;
761 }
762
763 /****************************************************************************
764  Fall back from krb5 to NTLMSSP.
765 ****************************************************************************/
766
767 static void reply_spnego_downgrade_to_ntlmssp(struct smb_request *req,
768                                                 uint16 vuid)
769 {
770         DATA_BLOB response;
771
772         reply_outbuf(req, 4, 0);
773         SSVAL(req->outbuf,smb_uid,vuid);
774
775         DEBUG(3,("reply_spnego_downgrade_to_ntlmssp: Got krb5 ticket in SPNEGO "
776                 "but set to downgrade to NTLMSSP\n"));
777
778         response = spnego_gen_auth_response(NULL,
779                         NT_STATUS_MORE_PROCESSING_REQUIRED,
780                         OID_NTLMSSP);
781         reply_sesssetup_blob(req, response, NT_STATUS_MORE_PROCESSING_REQUIRED);
782         data_blob_free(&response);
783 }
784
785 /****************************************************************************
786  Reply to a session setup spnego negotiate packet.
787 ****************************************************************************/
788
789 static void reply_spnego_negotiate(struct smb_request *req,
790                                    uint16 vuid,
791                                    DATA_BLOB blob1,
792                                    struct auth_ntlmssp_state **auth_ntlmssp_state)
793 {
794         DATA_BLOB secblob;
795         DATA_BLOB chal;
796         char *kerb_mech = NULL;
797         NTSTATUS status;
798         struct smbd_server_connection *sconn = smbd_server_conn;
799
800         status = parse_spnego_mechanisms(blob1, &secblob, &kerb_mech);
801         if (!NT_STATUS_IS_OK(status)) {
802                 /* Kill the intermediate vuid */
803                 invalidate_vuid(sconn, vuid);
804                 reply_nterror(req, nt_status_squash(status));
805                 return;
806         }
807
808         DEBUG(3,("reply_spnego_negotiate: Got secblob of size %lu\n",
809                                 (unsigned long)secblob.length));
810
811 #ifdef HAVE_KRB5
812         if (kerb_mech && ((lp_security()==SEC_ADS) ||
813                                 USE_KERBEROS_KEYTAB) ) {
814                 bool destroy_vuid = True;
815                 reply_spnego_kerberos(req, &secblob, kerb_mech,
816                                       vuid, &destroy_vuid);
817                 data_blob_free(&secblob);
818                 if (destroy_vuid) {
819                         /* Kill the intermediate vuid */
820                         invalidate_vuid(sconn, vuid);
821                 }
822                 SAFE_FREE(kerb_mech);
823                 return;
824         }
825 #endif
826
827         if (*auth_ntlmssp_state) {
828                 auth_ntlmssp_end(auth_ntlmssp_state);
829         }
830
831         if (kerb_mech) {
832                 data_blob_free(&secblob);
833                 /* The mechtoken is a krb5 ticket, but
834                  * we need to fall back to NTLM. */
835                 reply_spnego_downgrade_to_ntlmssp(req, vuid);
836                 SAFE_FREE(kerb_mech);
837                 return;
838         }
839
840         status = auth_ntlmssp_start(auth_ntlmssp_state);
841         if (!NT_STATUS_IS_OK(status)) {
842                 /* Kill the intermediate vuid */
843                 invalidate_vuid(sconn, vuid);
844                 reply_nterror(req, nt_status_squash(status));
845                 return;
846         }
847
848         status = auth_ntlmssp_update(*auth_ntlmssp_state,
849                                         secblob, &chal);
850
851         data_blob_free(&secblob);
852
853         reply_spnego_ntlmssp(req, vuid, auth_ntlmssp_state,
854                              &chal, status, OID_NTLMSSP, true);
855
856         data_blob_free(&chal);
857
858         /* already replied */
859         return;
860 }
861
862 /****************************************************************************
863  Reply to a session setup spnego auth packet.
864 ****************************************************************************/
865
866 static void reply_spnego_auth(struct smb_request *req,
867                               uint16 vuid,
868                               DATA_BLOB blob1,
869                               struct auth_ntlmssp_state **auth_ntlmssp_state)
870 {
871         DATA_BLOB auth = data_blob_null;
872         DATA_BLOB auth_reply = data_blob_null;
873         DATA_BLOB secblob = data_blob_null;
874         NTSTATUS status = NT_STATUS_LOGON_FAILURE;
875         struct smbd_server_connection *sconn = smbd_server_conn;
876
877         if (!spnego_parse_auth(blob1, &auth)) {
878 #if 0
879                 file_save("auth.dat", blob1.data, blob1.length);
880 #endif
881                 /* Kill the intermediate vuid */
882                 invalidate_vuid(sconn, vuid);
883
884                 reply_nterror(req, nt_status_squash(
885                                       NT_STATUS_LOGON_FAILURE));
886                 return;
887         }
888
889         if (auth.data[0] == ASN1_APPLICATION(0)) {
890                 /* Might be a second negTokenTarg packet */
891                 char *kerb_mech = NULL;
892
893                 status = parse_spnego_mechanisms(auth, &secblob, &kerb_mech);
894
895                 if (!NT_STATUS_IS_OK(status)) {
896                         /* Kill the intermediate vuid */
897                         invalidate_vuid(sconn, vuid);
898                         reply_nterror(req, nt_status_squash(status));
899                         return;
900                 }
901
902                 DEBUG(3,("reply_spnego_auth: Got secblob of size %lu\n",
903                                 (unsigned long)secblob.length));
904 #ifdef HAVE_KRB5
905                 if (kerb_mech && ((lp_security()==SEC_ADS) ||
906                                         USE_KERBEROS_KEYTAB)) {
907                         bool destroy_vuid = True;
908                         reply_spnego_kerberos(req, &secblob, kerb_mech,
909                                               vuid, &destroy_vuid);
910                         data_blob_free(&secblob);
911                         data_blob_free(&auth);
912                         if (destroy_vuid) {
913                                 /* Kill the intermediate vuid */
914                                 invalidate_vuid(sconn, vuid);
915                         }
916                         SAFE_FREE(kerb_mech);
917                         return;
918                 }
919 #endif
920                 /* Can't blunder into NTLMSSP auth if we have
921                  * a krb5 ticket. */
922
923                 if (kerb_mech) {
924                         /* Kill the intermediate vuid */
925                         invalidate_vuid(sconn, vuid);
926                         DEBUG(3,("reply_spnego_auth: network "
927                                 "misconfiguration, client sent us a "
928                                 "krb5 ticket and kerberos security "
929                                 "not enabled\n"));
930                         reply_nterror(req, nt_status_squash(
931                                         NT_STATUS_LOGON_FAILURE));
932                         SAFE_FREE(kerb_mech);
933                 }
934         }
935
936         /* If we get here it wasn't a negTokenTarg auth packet. */
937         data_blob_free(&secblob);
938
939         if (!*auth_ntlmssp_state) {
940                 status = auth_ntlmssp_start(auth_ntlmssp_state);
941                 if (!NT_STATUS_IS_OK(status)) {
942                         /* Kill the intermediate vuid */
943                         invalidate_vuid(sconn, vuid);
944                         reply_nterror(req, nt_status_squash(status));
945                         return;
946                 }
947         }
948
949         status = auth_ntlmssp_update(*auth_ntlmssp_state,
950                                         auth, &auth_reply);
951
952         data_blob_free(&auth);
953
954         /* Don't send the mechid as we've already sent this (RFC4178). */
955
956         reply_spnego_ntlmssp(req, vuid,
957                              auth_ntlmssp_state,
958                              &auth_reply, status, NULL, true);
959
960         data_blob_free(&auth_reply);
961
962         /* and tell smbd that we have already replied to this packet */
963         return;
964 }
965
966 /****************************************************************************
967  Delete an entry on the list.
968 ****************************************************************************/
969
970 static void delete_partial_auth(struct smbd_server_connection *sconn,
971                                 struct pending_auth_data *pad)
972 {
973         if (!pad) {
974                 return;
975         }
976         DLIST_REMOVE(sconn->smb1.pd_list, pad);
977         data_blob_free(&pad->partial_data);
978         SAFE_FREE(pad);
979 }
980
981 /****************************************************************************
982  Search for a partial SPNEGO auth fragment matching an smbpid.
983 ****************************************************************************/
984
985 static struct pending_auth_data *get_pending_auth_data(
986                 struct smbd_server_connection *sconn,
987                 uint16_t smbpid)
988 {
989         struct pending_auth_data *pad;
990 /*
991  * NOTE: using the smbpid here is completely wrong...
992  *       see [MS-SMB]
993  *       3.3.5.3 Receiving an SMB_COM_SESSION_SETUP_ANDX Request
994  */
995         for (pad = sconn->smb1.pd_list; pad; pad = pad->next) {
996                 if (pad->smbpid == smbpid) {
997                         break;
998                 }
999         }
1000         return pad;
1001 }
1002
1003 /****************************************************************************
1004  Check the size of an SPNEGO blob. If we need more return
1005  NT_STATUS_MORE_PROCESSING_REQUIRED, else return NT_STATUS_OK. Don't allow
1006  the blob to be more than 64k.
1007 ****************************************************************************/
1008
1009 static NTSTATUS check_spnego_blob_complete(struct smbd_server_connection *sconn,
1010                                            uint16 smbpid, uint16 vuid,
1011                                            DATA_BLOB *pblob)
1012 {
1013         struct pending_auth_data *pad = NULL;
1014         ASN1_DATA *data;
1015         size_t needed_len = 0;
1016
1017         pad = get_pending_auth_data(sconn, smbpid);
1018
1019         /* Ensure we have some data. */
1020         if (pblob->length == 0) {
1021                 /* Caller can cope. */
1022                 DEBUG(2,("check_spnego_blob_complete: zero blob length !\n"));
1023                 delete_partial_auth(sconn, pad);
1024                 return NT_STATUS_OK;
1025         }
1026
1027         /* Were we waiting for more data ? */
1028         if (pad) {
1029                 DATA_BLOB tmp_blob;
1030                 size_t copy_len = MIN(65536, pblob->length);
1031
1032                 /* Integer wrap paranoia.... */
1033
1034                 if (pad->partial_data.length + copy_len <
1035                                 pad->partial_data.length ||
1036                     pad->partial_data.length + copy_len < copy_len) {
1037
1038                         DEBUG(2,("check_spnego_blob_complete: integer wrap "
1039                                 "pad->partial_data.length = %u, "
1040                                 "copy_len = %u\n",
1041                                 (unsigned int)pad->partial_data.length,
1042                                 (unsigned int)copy_len ));
1043
1044                         delete_partial_auth(sconn, pad);
1045                         return NT_STATUS_INVALID_PARAMETER;
1046                 }
1047
1048                 DEBUG(10,("check_spnego_blob_complete: "
1049                         "pad->partial_data.length = %u, "
1050                         "pad->needed_len = %u, "
1051                         "copy_len = %u, "
1052                         "pblob->length = %u,\n",
1053                         (unsigned int)pad->partial_data.length,
1054                         (unsigned int)pad->needed_len,
1055                         (unsigned int)copy_len,
1056                         (unsigned int)pblob->length ));
1057
1058                 tmp_blob = data_blob(NULL,
1059                                 pad->partial_data.length + copy_len);
1060
1061                 /* Concatenate the two (up to copy_len) bytes. */
1062                 memcpy(tmp_blob.data,
1063                         pad->partial_data.data,
1064                         pad->partial_data.length);
1065                 memcpy(tmp_blob.data + pad->partial_data.length,
1066                         pblob->data,
1067                         copy_len);
1068
1069                 /* Replace the partial data. */
1070                 data_blob_free(&pad->partial_data);
1071                 pad->partial_data = tmp_blob;
1072                 ZERO_STRUCT(tmp_blob);
1073
1074                 /* Are we done ? */
1075                 if (pblob->length >= pad->needed_len) {
1076                         /* Yes, replace pblob. */
1077                         data_blob_free(pblob);
1078                         *pblob = pad->partial_data;
1079                         ZERO_STRUCT(pad->partial_data);
1080                         delete_partial_auth(sconn, pad);
1081                         return NT_STATUS_OK;
1082                 }
1083
1084                 /* Still need more data. */
1085                 pad->needed_len -= copy_len;
1086                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
1087         }
1088
1089         if ((pblob->data[0] != ASN1_APPLICATION(0)) &&
1090             (pblob->data[0] != ASN1_CONTEXT(1))) {
1091                 /* Not something we can determine the
1092                  * length of.
1093                  */
1094                 return NT_STATUS_OK;
1095         }
1096
1097         /* This is a new SPNEGO sessionsetup - see if
1098          * the data given in this blob is enough.
1099          */
1100
1101         data = asn1_init(NULL);
1102         if (data == NULL) {
1103                 return NT_STATUS_NO_MEMORY;
1104         }
1105
1106         asn1_load(data, *pblob);
1107         asn1_start_tag(data, pblob->data[0]);
1108         if (data->has_error || data->nesting == NULL) {
1109                 asn1_free(data);
1110                 /* Let caller catch. */
1111                 return NT_STATUS_OK;
1112         }
1113
1114         /* Integer wrap paranoia.... */
1115
1116         if (data->nesting->taglen + data->nesting->start < data->nesting->taglen ||
1117             data->nesting->taglen + data->nesting->start < data->nesting->start) {
1118
1119                 DEBUG(2,("check_spnego_blob_complete: integer wrap "
1120                         "data.nesting->taglen = %u, "
1121                         "data.nesting->start = %u\n",
1122                         (unsigned int)data->nesting->taglen,
1123                         (unsigned int)data->nesting->start ));
1124
1125                 asn1_free(data);
1126                 return NT_STATUS_INVALID_PARAMETER;
1127         }
1128
1129         /* Total length of the needed asn1 is the tag length
1130          * plus the current offset. */
1131
1132         needed_len = data->nesting->taglen + data->nesting->start;
1133         asn1_free(data);
1134
1135         DEBUG(10,("check_spnego_blob_complete: needed_len = %u, "
1136                 "pblob->length = %u\n",
1137                 (unsigned int)needed_len,
1138                 (unsigned int)pblob->length ));
1139
1140         if (needed_len <= pblob->length) {
1141                 /* Nothing to do - blob is complete. */
1142                 return NT_STATUS_OK;
1143         }
1144
1145         /* Refuse the blob if it's bigger than 64k. */
1146         if (needed_len > 65536) {
1147                 DEBUG(2,("check_spnego_blob_complete: needed_len "
1148                         "too large (%u)\n",
1149                         (unsigned int)needed_len ));
1150                 return NT_STATUS_INVALID_PARAMETER;
1151         }
1152
1153         /* We must store this blob until complete. */
1154         if (!(pad = SMB_MALLOC_P(struct pending_auth_data))) {
1155                 return NT_STATUS_NO_MEMORY;
1156         }
1157         pad->needed_len = needed_len - pblob->length;
1158         pad->partial_data = data_blob(pblob->data, pblob->length);
1159         if (pad->partial_data.data == NULL) {
1160                 SAFE_FREE(pad);
1161                 return NT_STATUS_NO_MEMORY;
1162         }
1163         pad->smbpid = smbpid;
1164         pad->vuid = vuid;
1165         DLIST_ADD(sconn->smb1.pd_list, pad);
1166
1167         return NT_STATUS_MORE_PROCESSING_REQUIRED;
1168 }
1169
1170 /****************************************************************************
1171  Reply to a session setup command.
1172  conn POINTER CAN BE NULL HERE !
1173 ****************************************************************************/
1174
1175 static void reply_sesssetup_and_X_spnego(struct smb_request *req)
1176 {
1177         const uint8 *p;
1178         DATA_BLOB blob1;
1179         size_t bufrem;
1180         char *tmp;
1181         const char *native_os;
1182         const char *native_lanman;
1183         const char *primary_domain;
1184         const char *p2;
1185         uint16 data_blob_len = SVAL(req->vwv+7, 0);
1186         enum remote_arch_types ra_type = get_remote_arch();
1187         int vuid = req->vuid;
1188         user_struct *vuser = NULL;
1189         NTSTATUS status = NT_STATUS_OK;
1190         uint16 smbpid = req->smbpid;
1191         struct smbd_server_connection *sconn = smbd_server_conn;
1192
1193         DEBUG(3,("Doing spnego session setup\n"));
1194
1195         if (global_client_caps == 0) {
1196                 global_client_caps = IVAL(req->vwv+10, 0);
1197
1198                 if (!(global_client_caps & CAP_STATUS32)) {
1199                         remove_from_common_flags2(FLAGS2_32_BIT_ERROR_CODES);
1200                 }
1201
1202         }
1203
1204         p = req->buf;
1205
1206         if (data_blob_len == 0) {
1207                 /* an invalid request */
1208                 reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
1209                 return;
1210         }
1211
1212         bufrem = smbreq_bufrem(req, p);
1213         /* pull the spnego blob */
1214         blob1 = data_blob(p, MIN(bufrem, data_blob_len));
1215
1216 #if 0
1217         file_save("negotiate.dat", blob1.data, blob1.length);
1218 #endif
1219
1220         p2 = (char *)req->buf + blob1.length;
1221
1222         p2 += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p2,
1223                                      STR_TERMINATE);
1224         native_os = tmp ? tmp : "";
1225
1226         p2 += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p2,
1227                                      STR_TERMINATE);
1228         native_lanman = tmp ? tmp : "";
1229
1230         p2 += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p2,
1231                                      STR_TERMINATE);
1232         primary_domain = tmp ? tmp : "";
1233
1234         DEBUG(3,("NativeOS=[%s] NativeLanMan=[%s] PrimaryDomain=[%s]\n",
1235                 native_os, native_lanman, primary_domain));
1236
1237         if ( ra_type == RA_WIN2K ) {
1238                 /* Vista sets neither the OS or lanman strings */
1239
1240                 if ( !strlen(native_os) && !strlen(native_lanman) )
1241                         set_remote_arch(RA_VISTA);
1242
1243                 /* Windows 2003 doesn't set the native lanman string,
1244                    but does set primary domain which is a bug I think */
1245
1246                 if ( !strlen(native_lanman) ) {
1247                         ra_lanman_string( primary_domain );
1248                 } else {
1249                         ra_lanman_string( native_lanman );
1250                 }
1251         }
1252
1253         /* Did we get a valid vuid ? */
1254         if (!is_partial_auth_vuid(sconn, vuid)) {
1255                 /* No, then try and see if this is an intermediate sessionsetup
1256                  * for a large SPNEGO packet. */
1257                 struct pending_auth_data *pad;
1258                 pad = get_pending_auth_data(sconn, smbpid);
1259                 if (pad) {
1260                         DEBUG(10,("reply_sesssetup_and_X_spnego: found "
1261                                 "pending vuid %u\n",
1262                                 (unsigned int)pad->vuid ));
1263                         vuid = pad->vuid;
1264                 }
1265         }
1266
1267         /* Do we have a valid vuid now ? */
1268         if (!is_partial_auth_vuid(sconn, vuid)) {
1269                 /* No, start a new authentication setup. */
1270                 vuid = register_initial_vuid(sconn);
1271                 if (vuid == UID_FIELD_INVALID) {
1272                         data_blob_free(&blob1);
1273                         reply_nterror(req, nt_status_squash(
1274                                               NT_STATUS_INVALID_PARAMETER));
1275                         return;
1276                 }
1277         }
1278
1279         vuser = get_partial_auth_user_struct(sconn, vuid);
1280         /* This MUST be valid. */
1281         if (!vuser) {
1282                 smb_panic("reply_sesssetup_and_X_spnego: invalid vuid.");
1283         }
1284
1285         /* Large (greater than 4k) SPNEGO blobs are split into multiple
1286          * sessionsetup requests as the Windows limit on the security blob
1287          * field is 4k. Bug #4400. JRA.
1288          */
1289
1290         status = check_spnego_blob_complete(sconn, smbpid, vuid, &blob1);
1291         if (!NT_STATUS_IS_OK(status)) {
1292                 if (!NT_STATUS_EQUAL(status,
1293                                 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
1294                         /* Real error - kill the intermediate vuid */
1295                         invalidate_vuid(sconn, vuid);
1296                 }
1297                 data_blob_free(&blob1);
1298                 reply_nterror(req, nt_status_squash(status));
1299                 return;
1300         }
1301
1302         if (blob1.data[0] == ASN1_APPLICATION(0)) {
1303
1304                 /* its a negTokenTarg packet */
1305
1306                 reply_spnego_negotiate(req, vuid, blob1,
1307                                        &vuser->auth_ntlmssp_state);
1308                 data_blob_free(&blob1);
1309                 return;
1310         }
1311
1312         if (blob1.data[0] == ASN1_CONTEXT(1)) {
1313
1314                 /* its a auth packet */
1315
1316                 reply_spnego_auth(req, vuid, blob1,
1317                                   &vuser->auth_ntlmssp_state);
1318                 data_blob_free(&blob1);
1319                 return;
1320         }
1321
1322         if (strncmp((char *)(blob1.data), "NTLMSSP", 7) == 0) {
1323                 DATA_BLOB chal;
1324
1325                 if (!vuser->auth_ntlmssp_state) {
1326                         status = auth_ntlmssp_start(&vuser->auth_ntlmssp_state);
1327                         if (!NT_STATUS_IS_OK(status)) {
1328                                 /* Kill the intermediate vuid */
1329                                 invalidate_vuid(sconn, vuid);
1330                                 data_blob_free(&blob1);
1331                                 reply_nterror(req, nt_status_squash(status));
1332                                 return;
1333                         }
1334                 }
1335
1336                 status = auth_ntlmssp_update(vuser->auth_ntlmssp_state,
1337                                                 blob1, &chal);
1338
1339                 data_blob_free(&blob1);
1340
1341                 reply_spnego_ntlmssp(req, vuid,
1342                                      &vuser->auth_ntlmssp_state,
1343                                      &chal, status, OID_NTLMSSP, false);
1344                 data_blob_free(&chal);
1345                 return;
1346         }
1347
1348         /* what sort of packet is this? */
1349         DEBUG(1,("Unknown packet in reply_sesssetup_and_X_spnego\n"));
1350
1351         data_blob_free(&blob1);
1352
1353         reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
1354 }
1355
1356 /****************************************************************************
1357  On new VC == 0, shutdown *all* old connections and users.
1358  It seems that only NT4.x does this. At W2K and above (XP etc.).
1359  a new session setup with VC==0 is ignored.
1360 ****************************************************************************/
1361
1362 static int shutdown_other_smbds(const struct connections_key *key,
1363                                 const struct connections_data *crec,
1364                                 void *private_data)
1365 {
1366         const char *ip = (const char *)private_data;
1367
1368         if (!process_exists(crec->pid)) {
1369                 return 0;
1370         }
1371
1372         if (procid_is_me(&crec->pid)) {
1373                 return 0;
1374         }
1375
1376         if (strcmp(ip, crec->addr) != 0) {
1377                 return 0;
1378         }
1379
1380         DEBUG(0,("shutdown_other_smbds: shutting down pid %u "
1381                  "(IP %s)\n", (unsigned int)procid_to_pid(&crec->pid), ip));
1382
1383         messaging_send(smbd_messaging_context(), crec->pid, MSG_SHUTDOWN,
1384                        &data_blob_null);
1385         return 0;
1386 }
1387
1388 static void setup_new_vc_session(void)
1389 {
1390         char addr[INET6_ADDRSTRLEN];
1391
1392         DEBUG(2,("setup_new_vc_session: New VC == 0, if NT4.x "
1393                 "compatible we would close all old resources.\n"));
1394 #if 0
1395         conn_close_all();
1396         invalidate_all_vuids();
1397 #endif
1398         if (lp_reset_on_zero_vc()) {
1399                 connections_forall_read(shutdown_other_smbds,
1400                         CONST_DISCARD(void *,
1401                         client_addr(get_client_fd(),addr,sizeof(addr))));
1402         }
1403 }
1404
1405 /****************************************************************************
1406  Reply to a session setup command.
1407 ****************************************************************************/
1408
1409 void reply_sesssetup_and_X(struct smb_request *req)
1410 {
1411         int sess_vuid;
1412         int smb_bufsize;
1413         DATA_BLOB lm_resp;
1414         DATA_BLOB nt_resp;
1415         DATA_BLOB plaintext_password;
1416         char *tmp;
1417         const char *user;
1418         fstring sub_user; /* Sainitised username for substituion */
1419         const char *domain;
1420         const char *native_os;
1421         const char *native_lanman;
1422         const char *primary_domain;
1423         struct auth_usersupplied_info *user_info = NULL;
1424         struct auth_serversupplied_info *server_info = NULL;
1425         uint16 smb_flag2 = req->flags2;
1426
1427         NTSTATUS nt_status;
1428         struct smbd_server_connection *sconn = smbd_server_conn;
1429
1430         bool doencrypt = sconn->smb1.negprot.encrypted_passwords;
1431
1432         START_PROFILE(SMBsesssetupX);
1433
1434         ZERO_STRUCT(lm_resp);
1435         ZERO_STRUCT(nt_resp);
1436         ZERO_STRUCT(plaintext_password);
1437
1438         DEBUG(3,("wct=%d flg2=0x%x\n", req->wct, req->flags2));
1439
1440         /* a SPNEGO session setup has 12 command words, whereas a normal
1441            NT1 session setup has 13. See the cifs spec. */
1442         if (req->wct == 12 &&
1443             (req->flags2 & FLAGS2_EXTENDED_SECURITY)) {
1444
1445                 if (!sconn->smb1.negprot.spnego) {
1446                         DEBUG(0,("reply_sesssetup_and_X:  Rejecting attempt "
1447                                  "at SPNEGO session setup when it was not "
1448                                  "negotiated.\n"));
1449                         reply_nterror(req, nt_status_squash(
1450                                               NT_STATUS_LOGON_FAILURE));
1451                         END_PROFILE(SMBsesssetupX);
1452                         return;
1453                 }
1454
1455                 if (SVAL(req->vwv+4, 0) == 0) {
1456                         setup_new_vc_session();
1457                 }
1458
1459                 reply_sesssetup_and_X_spnego(req);
1460                 END_PROFILE(SMBsesssetupX);
1461                 return;
1462         }
1463
1464         smb_bufsize = SVAL(req->vwv+2, 0);
1465
1466         if (get_Protocol() < PROTOCOL_NT1) {
1467                 uint16 passlen1 = SVAL(req->vwv+7, 0);
1468
1469                 /* Never do NT status codes with protocols before NT1 as we
1470                  * don't get client caps. */
1471                 remove_from_common_flags2(FLAGS2_32_BIT_ERROR_CODES);
1472
1473                 if ((passlen1 > MAX_PASS_LEN) || (passlen1 > req->buflen)) {
1474                         reply_nterror(req, nt_status_squash(
1475                                               NT_STATUS_INVALID_PARAMETER));
1476                         END_PROFILE(SMBsesssetupX);
1477                         return;
1478                 }
1479
1480                 if (doencrypt) {
1481                         lm_resp = data_blob(req->buf, passlen1);
1482                 } else {
1483                         plaintext_password = data_blob(req->buf, passlen1+1);
1484                         /* Ensure null termination */
1485                         plaintext_password.data[passlen1] = 0;
1486                 }
1487
1488                 srvstr_pull_req_talloc(talloc_tos(), req, &tmp,
1489                                        req->buf + passlen1, STR_TERMINATE);
1490                 user = tmp ? tmp : "";
1491
1492                 domain = "";
1493
1494         } else {
1495                 uint16 passlen1 = SVAL(req->vwv+7, 0);
1496                 uint16 passlen2 = SVAL(req->vwv+8, 0);
1497                 enum remote_arch_types ra_type = get_remote_arch();
1498                 const uint8_t *p = req->buf;
1499                 const uint8_t *save_p = req->buf;
1500                 uint16 byte_count;
1501
1502
1503                 if(global_client_caps == 0) {
1504                         global_client_caps = IVAL(req->vwv+11, 0);
1505
1506                         if (!(global_client_caps & CAP_STATUS32)) {
1507                                 remove_from_common_flags2(
1508                                                 FLAGS2_32_BIT_ERROR_CODES);
1509                         }
1510
1511                         /* client_caps is used as final determination if
1512                          * client is NT or Win95. This is needed to return
1513                          * the correct error codes in some circumstances.
1514                         */
1515
1516                         if(ra_type == RA_WINNT || ra_type == RA_WIN2K ||
1517                                         ra_type == RA_WIN95) {
1518                                 if(!(global_client_caps & (CAP_NT_SMBS|
1519                                                         CAP_STATUS32))) {
1520                                         set_remote_arch( RA_WIN95);
1521                                 }
1522                         }
1523                 }
1524
1525                 if (!doencrypt) {
1526                         /* both Win95 and WinNT stuff up the password
1527                          * lengths for non-encrypting systems. Uggh.
1528
1529                            if passlen1==24 its a win95 system, and its setting
1530                            the password length incorrectly. Luckily it still
1531                            works with the default code because Win95 will null
1532                            terminate the password anyway
1533
1534                            if passlen1>0 and passlen2>0 then maybe its a NT box
1535                            and its setting passlen2 to some random value which
1536                            really stuffs things up. we need to fix that one.  */
1537
1538                         if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 &&
1539                                         passlen2 != 1) {
1540                                 passlen2 = 0;
1541                         }
1542                 }
1543
1544                 /* check for nasty tricks */
1545                 if (passlen1 > MAX_PASS_LEN
1546                     || passlen1 > smbreq_bufrem(req, p)) {
1547                         reply_nterror(req, nt_status_squash(
1548                                               NT_STATUS_INVALID_PARAMETER));
1549                         END_PROFILE(SMBsesssetupX);
1550                         return;
1551                 }
1552
1553                 if (passlen2 > MAX_PASS_LEN
1554                     || passlen2 > smbreq_bufrem(req, p+passlen1)) {
1555                         reply_nterror(req, nt_status_squash(
1556                                               NT_STATUS_INVALID_PARAMETER));
1557                         END_PROFILE(SMBsesssetupX);
1558                         return;
1559                 }
1560
1561                 /* Save the lanman2 password and the NT md4 password. */
1562
1563                 if ((doencrypt) && (passlen1 != 0) && (passlen1 != 24)) {
1564                         doencrypt = False;
1565                 }
1566
1567                 if (doencrypt) {
1568                         lm_resp = data_blob(p, passlen1);
1569                         nt_resp = data_blob(p+passlen1, passlen2);
1570                 } else if (lp_security() != SEC_SHARE) {
1571                         /*
1572                          * In share level we should ignore any passwords, so
1573                          * only read them if we're not.
1574                          */
1575                         char *pass = NULL;
1576                         bool unic= smb_flag2 & FLAGS2_UNICODE_STRINGS;
1577
1578                         if (unic && (passlen2 == 0) && passlen1) {
1579                                 /* Only a ascii plaintext password was sent. */
1580                                 (void)srvstr_pull_talloc(talloc_tos(),
1581                                                         req->inbuf,
1582                                                         req->flags2,
1583                                                         &pass,
1584                                                         req->buf,
1585                                                         passlen1,
1586                                                         STR_TERMINATE|STR_ASCII);
1587                         } else {
1588                                 (void)srvstr_pull_talloc(talloc_tos(),
1589                                                         req->inbuf,
1590                                                         req->flags2,
1591                                                         &pass,
1592                                                         req->buf,
1593                                                         unic ? passlen2 : passlen1,
1594                                                         STR_TERMINATE);
1595                         }
1596                         if (!pass) {
1597                                 reply_nterror(req, nt_status_squash(
1598                                               NT_STATUS_INVALID_PARAMETER));
1599                                 END_PROFILE(SMBsesssetupX);
1600                                 return;
1601                         }
1602                         plaintext_password = data_blob(pass, strlen(pass)+1);
1603                 }
1604
1605                 p += passlen1 + passlen2;
1606
1607                 p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
1608                                             STR_TERMINATE);
1609                 user = tmp ? tmp : "";
1610
1611                 p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
1612                                             STR_TERMINATE);
1613                 domain = tmp ? tmp : "";
1614
1615                 p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
1616                                             STR_TERMINATE);
1617                 native_os = tmp ? tmp : "";
1618
1619                 p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
1620                                             STR_TERMINATE);
1621                 native_lanman = tmp ? tmp : "";
1622
1623                 /* not documented or decoded by Ethereal but there is one more
1624                  * string in the extra bytes which is the same as the
1625                  * PrimaryDomain when using extended security.  Windows NT 4
1626                  * and 2003 use this string to store the native lanman string.
1627                  * Windows 9x does not include a string here at all so we have
1628                  * to check if we have any extra bytes left */
1629
1630                 byte_count = SVAL(req->vwv+13, 0);
1631                 if ( PTR_DIFF(p, save_p) < byte_count) {
1632                         p += srvstr_pull_req_talloc(talloc_tos(), req, &tmp, p,
1633                                                     STR_TERMINATE);
1634                         primary_domain = tmp ? tmp : "";
1635                 } else {
1636                         primary_domain = talloc_strdup(talloc_tos(), "null");
1637                 }
1638
1639                 DEBUG(3,("Domain=[%s]  NativeOS=[%s] NativeLanMan=[%s] "
1640                         "PrimaryDomain=[%s]\n",
1641                         domain, native_os, native_lanman, primary_domain));
1642
1643                 if ( ra_type == RA_WIN2K ) {
1644                         if ( strlen(native_lanman) == 0 )
1645                                 ra_lanman_string( primary_domain );
1646                         else
1647                                 ra_lanman_string( native_lanman );
1648                 }
1649
1650         }
1651
1652         if (SVAL(req->vwv+4, 0) == 0) {
1653                 setup_new_vc_session();
1654         }
1655
1656         DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n",
1657                                 domain, user, get_remote_machine_name()));
1658
1659         if (*user) {
1660                 if (sconn->smb1.negprot.spnego) {
1661
1662                         /* This has to be here, because this is a perfectly
1663                          * valid behaviour for guest logons :-( */
1664
1665                         DEBUG(0,("reply_sesssetup_and_X:  Rejecting attempt "
1666                                 "at 'normal' session setup after "
1667                                 "negotiating spnego.\n"));
1668                         reply_nterror(req, nt_status_squash(
1669                                               NT_STATUS_LOGON_FAILURE));
1670                         END_PROFILE(SMBsesssetupX);
1671                         return;
1672                 }
1673                 fstrcpy(sub_user, user);
1674         } else {
1675                 fstrcpy(sub_user, lp_guestaccount());
1676         }
1677
1678         sub_set_smb_name(sub_user);
1679
1680         reload_services(True);
1681
1682         if (lp_security() == SEC_SHARE) {
1683                 /* In share level we should ignore any passwords */
1684
1685                 data_blob_free(&lm_resp);
1686                 data_blob_free(&nt_resp);
1687                 data_blob_clear_free(&plaintext_password);
1688
1689                 map_username(sub_user);
1690                 add_session_user(sconn, sub_user);
1691                 add_session_workgroup(sconn, domain);
1692                 /* Then force it to null for the benfit of the code below */
1693                 user = "";
1694         }
1695
1696         if (!*user) {
1697
1698                 nt_status = check_guest_password(&server_info);
1699
1700         } else if (doencrypt) {
1701                 struct auth_context *negprot_auth_context = NULL;
1702                 negprot_auth_context = sconn->smb1.negprot.auth_context;
1703                 if (!negprot_auth_context) {
1704                         DEBUG(0, ("reply_sesssetup_and_X:  Attempted encrypted "
1705                                 "session setup without negprot denied!\n"));
1706                         reply_nterror(req, nt_status_squash(
1707                                               NT_STATUS_LOGON_FAILURE));
1708                         END_PROFILE(SMBsesssetupX);
1709                         return;
1710                 }
1711                 nt_status = make_user_info_for_reply_enc(&user_info, user,
1712                                                 domain,
1713                                                 lm_resp, nt_resp);
1714                 if (NT_STATUS_IS_OK(nt_status)) {
1715                         nt_status = negprot_auth_context->check_ntlm_password(
1716                                         negprot_auth_context,
1717                                         user_info,
1718                                         &server_info);
1719                 }
1720         } else {
1721                 struct auth_context *plaintext_auth_context = NULL;
1722
1723                 nt_status = make_auth_context_subsystem(
1724                                 &plaintext_auth_context);
1725
1726                 if (NT_STATUS_IS_OK(nt_status)) {
1727                         uint8_t chal[8];
1728
1729                         plaintext_auth_context->get_ntlm_challenge(
1730                                         plaintext_auth_context, chal);
1731
1732                         if (!make_user_info_for_reply(&user_info,
1733                                                       user, domain, chal,
1734                                                       plaintext_password)) {
1735                                 nt_status = NT_STATUS_NO_MEMORY;
1736                         }
1737
1738                         if (NT_STATUS_IS_OK(nt_status)) {
1739                                 nt_status = plaintext_auth_context->check_ntlm_password(
1740                                                 plaintext_auth_context,
1741                                                 user_info,
1742                                                 &server_info);
1743
1744                                 (plaintext_auth_context->free)(
1745                                                 &plaintext_auth_context);
1746                         }
1747                 }
1748         }
1749
1750         free_user_info(&user_info);
1751
1752         if (!NT_STATUS_IS_OK(nt_status)) {
1753                 nt_status = do_map_to_guest(nt_status, &server_info,
1754                                 user, domain);
1755         }
1756
1757         if (!NT_STATUS_IS_OK(nt_status)) {
1758                 data_blob_free(&nt_resp);
1759                 data_blob_free(&lm_resp);
1760                 data_blob_clear_free(&plaintext_password);
1761                 reply_nterror(req, nt_status_squash(nt_status));
1762                 END_PROFILE(SMBsesssetupX);
1763                 return;
1764         }
1765
1766         /* Ensure we can't possible take a code path leading to a
1767          * null defref. */
1768         if (!server_info) {
1769                 reply_nterror(req, nt_status_squash(NT_STATUS_LOGON_FAILURE));
1770                 END_PROFILE(SMBsesssetupX);
1771                 return;
1772         }
1773
1774         if (!server_info->ptok) {
1775                 nt_status = create_local_token(server_info);
1776
1777                 if (!NT_STATUS_IS_OK(nt_status)) {
1778                         DEBUG(10, ("create_local_token failed: %s\n",
1779                                    nt_errstr(nt_status)));
1780                         data_blob_free(&nt_resp);
1781                         data_blob_free(&lm_resp);
1782                         data_blob_clear_free(&plaintext_password);
1783                         reply_nterror(req, nt_status_squash(nt_status));
1784                         END_PROFILE(SMBsesssetupX);
1785                         return;
1786                 }
1787         }
1788
1789         data_blob_clear_free(&plaintext_password);
1790
1791         /* it's ok - setup a reply */
1792         reply_outbuf(req, 3, 0);
1793         if (get_Protocol() >= PROTOCOL_NT1) {
1794                 push_signature(&req->outbuf);
1795                 /* perhaps grab OS version here?? */
1796         }
1797
1798         if (server_info->guest) {
1799                 SSVAL(req->outbuf,smb_vwv2,1);
1800         }
1801
1802         /* register the name and uid as being validated, so further connections
1803            to a uid can get through without a password, on the same VC */
1804
1805         if (lp_security() == SEC_SHARE) {
1806                 sess_vuid = UID_FIELD_INVALID;
1807                 TALLOC_FREE(server_info);
1808         } else {
1809                 /* Ignore the initial vuid. */
1810                 sess_vuid = register_initial_vuid(sconn);
1811                 if (sess_vuid == UID_FIELD_INVALID) {
1812                         data_blob_free(&nt_resp);
1813                         data_blob_free(&lm_resp);
1814                         reply_nterror(req, nt_status_squash(
1815                                               NT_STATUS_LOGON_FAILURE));
1816                         END_PROFILE(SMBsesssetupX);
1817                         return;
1818                 }
1819                 /* register_existing_vuid keeps the server info */
1820                 sess_vuid = register_existing_vuid(sconn, sess_vuid,
1821                                         server_info,
1822                                         nt_resp.data ? nt_resp : lm_resp,
1823                                         sub_user);
1824                 if (sess_vuid == UID_FIELD_INVALID) {
1825                         data_blob_free(&nt_resp);
1826                         data_blob_free(&lm_resp);
1827                         reply_nterror(req, nt_status_squash(
1828                                               NT_STATUS_LOGON_FAILURE));
1829                         END_PROFILE(SMBsesssetupX);
1830                         return;
1831                 }
1832
1833                 /* current_user_info is changed on new vuid */
1834                 reload_services( True );
1835         }
1836
1837         data_blob_free(&nt_resp);
1838         data_blob_free(&lm_resp);
1839
1840         SSVAL(req->outbuf,smb_uid,sess_vuid);
1841         SSVAL(req->inbuf,smb_uid,sess_vuid);
1842         req->vuid = sess_vuid;
1843
1844         if (!sconn->smb1.sessions.done_sesssetup) {
1845                 sconn->smb1.sessions.max_send =
1846                         MIN(sconn->smb1.sessions.max_send,smb_bufsize);
1847         }
1848         sconn->smb1.sessions.done_sesssetup = true;
1849
1850         END_PROFILE(SMBsesssetupX);
1851         chain_reply(req);
1852         return;
1853 }