trying to get HEAD building again. If you want the code
[gd/samba-autobuild/.git] / source3 / smbd / sesssetup.c
index 2e9e54b8d93f37285deea932f6f25943def0971a..7d77ed307105162d37240b79b0488d17b8bfb894 100644 (file)
@@ -3,6 +3,8 @@
    handle SMBsessionsetup
    Copyright (C) Andrew Tridgell 1998-2001
    Copyright (C) Andrew Bartlett      2001
+   Copyright (C) Jim McDonough        2002
+   Copyright (C) Luke Howard          2003
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -22,7 +24,8 @@
 #include "includes.h"
 
 uint32 global_client_caps = 0;
-static struct auth_context *ntlmssp_auth_context = NULL;
+
+static struct auth_ntlmssp_state *global_ntlmssp_state;
 
 /*
   on a logon error possibly map the error to success if "map to guest"
@@ -36,39 +39,70 @@ static NTSTATUS do_map_to_guest(NTSTATUS status, auth_serversupplied_info **serv
                    (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
                        DEBUG(3,("No such user %s [%s] - using guest account\n",
                                 user, domain));
-                       make_server_info_guest(server_info);
-                       status = NT_STATUS_OK;
+                       status = make_server_info_guest(server_info);
                }
        }
 
        if (NT_STATUS_EQUAL(status, NT_STATUS_WRONG_PASSWORD)) {
                if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD) {
                        DEBUG(3,("Registered username %s for guest access\n",user));
-                       make_server_info_guest(server_info);
-                       status = NT_STATUS_OK;
+                       status = make_server_info_guest(server_info);
                }
        }
 
        return status;
 }
 
-
 /****************************************************************************
  Add the standard 'Samba' signature to the end of the session setup.
 ****************************************************************************/
-static void add_signature(char *outbuf) 
+
+static int add_signature(char *outbuf, char *p)
 {
-       char *p;
-       p = smb_buf(outbuf);
+       char *start = p;
+       fstring lanman;
+
+       snprintf( lanman, sizeof(lanman), "Samba %s", VERSION );
+
        p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
-       p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
+       p += srvstr_push(outbuf, p, lanman, -1, STR_TERMINATE);
        p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
+
+       return PTR_DIFF(p, start);
+}
+
+/****************************************************************************
+ Send a security blob via a session setup reply.
+****************************************************************************/
+
+static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
+                                DATA_BLOB blob, NTSTATUS nt_status)
+{
+       char *p;
+
+       set_message(outbuf,4,0,True);
+
+       nt_status = nt_status_squash(nt_status);
+       SIVAL(outbuf, smb_rcls, NT_STATUS_V(nt_status));
+       SSVAL(outbuf, smb_vwv0, 0xFF); /* no chaining possible */
+       SSVAL(outbuf, smb_vwv3, blob.length);
+       p = smb_buf(outbuf);
+
+       /* should we cap this? */
+       memcpy(p, blob.data, blob.length);
+       p += blob.length;
+
+       p += add_signature( outbuf, p );
+
        set_message_end(outbuf,p);
+
+       return send_smb(smbd_server_fd(),outbuf);
 }
 
 /****************************************************************************
  Do a 'guest' logon, getting back the 
 ****************************************************************************/
+
 static NTSTATUS check_guest_password(auth_serversupplied_info **server_info) 
 {
        struct auth_context *auth_context;
@@ -113,10 +147,20 @@ static int reply_spnego_kerberos(connection_struct *conn,
        int sess_vuid;
        NTSTATUS ret;
        DATA_BLOB auth_data;
+       DATA_BLOB ap_rep, ap_rep_wrapped, response;
        auth_serversupplied_info *server_info = NULL;
        ADS_STRUCT *ads;
+       uint8 session_key[16];
+       uint8 tok_id[2];
+       BOOL foreign = False;
+
+       ZERO_STRUCT(ticket);
+       ZERO_STRUCT(auth_data);
+       ZERO_STRUCT(ap_rep);
+       ZERO_STRUCT(ap_rep_wrapped);
+       ZERO_STRUCT(response);
 
-       if (!spnego_parse_krb5_wrap(*secblob, &ticket)) {
+       if (!spnego_parse_krb5_wrap(*secblob, &ticket, tok_id)) {
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
@@ -128,19 +172,22 @@ static int reply_spnego_kerberos(connection_struct *conn,
 
        ads->auth.realm = strdup(lp_realm());
 
-       ret = ads_verify_ticket(ads, &ticket, &client, &auth_data);
+       ret = ads_verify_ticket(ads, &ticket, &client, &auth_data, &ap_rep, session_key);
        if (!NT_STATUS_IS_OK(ret)) {
                DEBUG(1,("Failed to verify incoming ticket!\n"));       
                ads_destroy(&ads);
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
+       data_blob_free(&auth_data);
+
        DEBUG(3,("Ticket name is [%s]\n", client));
 
        p = strchr_m(client, '@');
        if (!p) {
                DEBUG(3,("Doesn't look like a valid principal\n"));
                ads_destroy(&ads);
+               data_blob_free(&ap_rep);
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
@@ -148,90 +195,142 @@ static int reply_spnego_kerberos(connection_struct *conn,
        if (strcasecmp(p+1, ads->auth.realm) != 0) {
                DEBUG(3,("Ticket for foreign realm %s@%s\n", client, p+1));
                if (!lp_allow_trusted_domains()) {
+                       data_blob_free(&ap_rep);
                        return ERROR_NT(NT_STATUS_LOGON_FAILURE);
                }
-               /* this gives a fully qualified user name (ie. with full realm).
-                  that leads to very long usernames, but what else can we do? */
-               asprintf(&user, "%s%s%s", p+1, lp_winbind_separator(), client);
-       } else {
-               user = strdup(client);
+               foreign = True;
        }
-       ads_destroy(&ads);
 
-       /* the password is good - let them in */
-       pw = smb_getpwnam(user,False);
-       if (!pw && !strstr(user, lp_winbind_separator())) {
-               char *user2;
-               /* try it with a winbind domain prefix */
-               asprintf(&user2, "%s%s%s", lp_workgroup(), lp_winbind_separator(), user);
-               pw = smb_getpwnam(user2,False);
-               if (pw) {
-                       free(user);
-                       user = user2;
-               }
+       /* this gives a fully qualified user name (ie. with full realm).
+          that leads to very long usernames, but what else can we do? */
+       asprintf(&user, "%s%s%s", p+1, lp_winbind_separator(), client);
+       
+       pw = Get_Pwnam(user);
+       if (!pw && !foreign) {
+               pw = Get_Pwnam(client);
+               SAFE_FREE(user);
+               user = smb_xstrdup(client);
        }
 
+       ads_destroy(&ads);
+
+       /* setup the string used by %U */
+       sub_set_smb_name(user);
+
+       reload_services(True);
+
        if (!pw) {
                DEBUG(1,("Username %s is invalid on this system\n",user));
+               data_blob_free(&ap_rep);
                return ERROR_NT(NT_STATUS_NO_SUCH_USER);
        }
 
-       if (!make_server_info_pw(&server_info,pw)) {
+       if (!NT_STATUS_IS_OK(ret = make_server_info_pw(&server_info,pw))) {
                DEBUG(1,("make_server_info_from_pw failed!\n"));
-               return ERROR_NT(NT_STATUS_NO_MEMORY);
+               data_blob_free(&ap_rep);
+               return ERROR_NT(ret);
        }
-       
+
+       /* Copy out the session key from the AP_REQ. */
+       memcpy(server_info->session_key, session_key, sizeof(session_key));
+
+       /* register_vuid keeps the server info */
        sess_vuid = register_vuid(server_info, user);
 
        free(user);
-       free_server_info(&server_info);
 
        if (sess_vuid == -1) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+               ret = NT_STATUS_LOGON_FAILURE;
+       } else {
+               set_message(outbuf,4,0,True);
+               SSVAL(outbuf, smb_vwv3, 0);
+                       
+               if (server_info->guest) {
+                       SSVAL(outbuf,smb_vwv2,1);
+               }
+               
+               SSVAL(outbuf, smb_uid, sess_vuid);
        }
 
-       set_message(outbuf,4,0,True);
-       SSVAL(outbuf, smb_vwv3, 0);
-       add_signature(outbuf);
-       SSVAL(outbuf,smb_uid,sess_vuid);
-       SSVAL(inbuf,smb_uid,sess_vuid);
-       
-       return chain_reply(inbuf,outbuf,length,bufsize);
+        /* wrap that up in a nice GSS-API wrapping */
+       if (NT_STATUS_IS_OK(ret)) {
+               ap_rep_wrapped = spnego_gen_krb5_wrap(ap_rep, TOK_ID_KRB_AP_REP);
+       } else {
+               ap_rep_wrapped = data_blob(NULL, 0);
+       }
+       response = spnego_gen_auth_response(&ap_rep_wrapped, ret, OID_KERBEROS5_OLD);
+       reply_sesssetup_blob(conn, outbuf, response, ret);
+
+       data_blob_free(&ap_rep);
+       data_blob_free(&ap_rep_wrapped);
+       data_blob_free(&response);
+
+       return -1; /* already replied */
 }
 #endif
 
-
 /****************************************************************************
-send a security blob via a session setup reply
-****************************************************************************/
-static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
-                                DATA_BLOB blob)
+ Send a session setup reply, wrapped in SPNEGO.
+ Get vuid and check first.
+ End the NTLMSSP exchange context if we are OK/complete fail
+***************************************************************************/
+
+static BOOL reply_spnego_ntlmssp(connection_struct *conn, char *outbuf,
+                                AUTH_NTLMSSP_STATE **auth_ntlmssp_state,
+                                DATA_BLOB *ntlmssp_blob, NTSTATUS nt_status) 
 {
-       char *p;
+       BOOL ret;
+       DATA_BLOB response;
+       struct auth_serversupplied_info *server_info = NULL;
 
-       set_message(outbuf,4,0,True);
+       if (NT_STATUS_IS_OK(nt_status)) {
+               server_info = (*auth_ntlmssp_state)->server_info;
+       } else {
+               nt_status = do_map_to_guest(nt_status, 
+                                           &server_info, 
+                                           (*auth_ntlmssp_state)->ntlmssp_state->user, 
+                                           (*auth_ntlmssp_state)->ntlmssp_state->domain);
+       }
 
-       /* we set NT_STATUS_MORE_PROCESSING_REQUIRED to tell the other end
-          that we aren't finished yet */
+       if (NT_STATUS_IS_OK(nt_status)) {
+               int sess_vuid;
+               /* register_vuid keeps the server info */
+               sess_vuid = register_vuid(server_info, (*auth_ntlmssp_state)->ntlmssp_state->user);
+               (*auth_ntlmssp_state)->server_info = NULL;
 
-       SIVAL(outbuf, smb_rcls, NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED));
-       SSVAL(outbuf, smb_vwv0, 0xFF); /* no chaining possible */
-       SSVAL(outbuf, smb_vwv3, blob.length);
-       p = smb_buf(outbuf);
-       memcpy(p, blob.data, blob.length);
-       p += blob.length;
-       p += srvstr_push(outbuf, p, "Unix", -1, STR_TERMINATE);
-       p += srvstr_push(outbuf, p, "Samba", -1, STR_TERMINATE);
-       p += srvstr_push(outbuf, p, lp_workgroup(), -1, STR_TERMINATE);
-       set_message_end(outbuf,p);
-       
-       return send_smb(smbd_server_fd(),outbuf);
+               if (sess_vuid == -1) {
+                       nt_status = NT_STATUS_LOGON_FAILURE;
+               } else {
+                       
+                       set_message(outbuf,4,0,True);
+                       SSVAL(outbuf, smb_vwv3, 0);
+                       
+                       if (server_info->guest) {
+                               SSVAL(outbuf,smb_vwv2,1);
+                       }
+                       
+                       SSVAL(outbuf,smb_uid,sess_vuid);
+               }
+       }
+
+        response = spnego_gen_auth_response(ntlmssp_blob, nt_status, OID_NTLMSSP);
+       ret = reply_sesssetup_blob(conn, outbuf, response, nt_status);
+       data_blob_free(&response);
+
+       /* NT_STATUS_MORE_PROCESSING_REQUIRED from our NTLMSSP code tells us,
+          and the other end, that we are not finished yet. */
+
+       if (!ret || !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               auth_ntlmssp_end(auth_ntlmssp_state);
+       }
+
+       return ret;
 }
 
 /****************************************************************************
-reply to a session setup spnego negotiate packet
+ Reply to a session setup spnego negotiate packet.
 ****************************************************************************/
+
 static int reply_spnego_negotiate(connection_struct *conn, 
                                  char *inbuf,
                                  char *outbuf,
@@ -241,12 +340,9 @@ static int reply_spnego_negotiate(connection_struct *conn,
        char *OIDs[ASN1_MAX_OIDS];
        DATA_BLOB secblob;
        int i;
-       uint32 ntlmssp_command, neg_flags, chal_flags;
-       DATA_BLOB chal, spnego_chal, extra_data;
-       const uint8 *cryptkey;
+       DATA_BLOB chal;
        BOOL got_kerberos = False;
        NTSTATUS nt_status;
-       extern pstring global_myname;
 
        /* parse out the OIDs and the first sec blob */
        if (!parse_negTokenTarg(blob1, OIDs, &secblob)) {
@@ -264,7 +360,7 @@ static int reply_spnego_negotiate(connection_struct *conn,
        DEBUG(3,("Got secblob of size %d\n", secblob.length));
 
 #ifdef HAVE_KRB5
-       if (got_kerberos) {
+       if (got_kerberos && (SEC_ADS == lp_security())) {
                int ret = reply_spnego_kerberos(conn, inbuf, outbuf, 
                                                length, bufsize, &secblob);
                data_blob_free(&secblob);
@@ -272,276 +368,82 @@ static int reply_spnego_negotiate(connection_struct *conn,
        }
 #endif
 
-       /* parse the NTLMSSP packet */
-#if 0
-       file_save("secblob.dat", secblob.data, secblob.length);
-#endif
-
-       if (!msrpc_parse(&secblob, "CddB",
-                        "NTLMSSP",
-                        &ntlmssp_command,
-                        &neg_flags,
-                        &extra_data)) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
-       
-       DEBUG(5, ("Extra data: \n"));
-       dump_data(5, extra_data.data, extra_data.length);
-
-       data_blob_free(&secblob);
-       data_blob_free(&extra_data);
-
-       if (ntlmssp_command != NTLMSSP_NEGOTIATE) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
-
-       DEBUG(3,("Got neg_flags=0x%08x\n", neg_flags));
-
-       debug_ntlmssp_flags(neg_flags);
-
-       if (ntlmssp_auth_context) {
-               (ntlmssp_auth_context->free)(&ntlmssp_auth_context);
+       if (global_ntlmssp_state) {
+               auth_ntlmssp_end(&global_ntlmssp_state);
        }
 
-       if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&ntlmssp_auth_context))) {
+       nt_status = auth_ntlmssp_start(&global_ntlmssp_state);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                return ERROR_NT(nt_status);
        }
 
-       cryptkey = ntlmssp_auth_context->get_ntlm_challenge(ntlmssp_auth_context);
+       nt_status = auth_ntlmssp_update(global_ntlmssp_state, 
+                                       secblob, &chal);
 
-       /* Give them the challenge. For now, ignore neg_flags and just
-          return the flags we want. Obviously this is not correct */
-       
-       chal_flags = NTLMSSP_NEGOTIATE_UNICODE | 
-               NTLMSSP_NEGOTIATE_LM_KEY | 
-               NTLMSSP_NEGOTIATE_NTLM |
-               NTLMSSP_CHAL_TARGET_INFO;
-       
-       {
-               DATA_BLOB domain_blob, netbios_blob, realm_blob;
-               
-               msrpc_gen(&domain_blob, 
-                         "U",
-                         lp_workgroup());
+       data_blob_free(&secblob);
 
-               msrpc_gen(&netbios_blob, 
-                         "U",
-                         global_myname);
-               
-               msrpc_gen(&realm_blob, 
-                         "U",
-                         lp_realm());
+       reply_spnego_ntlmssp(conn, outbuf, &global_ntlmssp_state,
+                            &chal, nt_status);
                
-
-               msrpc_gen(&chal, "CddddbBBBB",
-                         "NTLMSSP", 
-                         NTLMSSP_CHALLENGE,
-                         0,
-                         0x30, /* ?? */
-                         chal_flags,
-                         cryptkey, 8,
-                         domain_blob.data, domain_blob.length,
-                         domain_blob.data, domain_blob.length,
-                         netbios_blob.data, netbios_blob.length,
-                         realm_blob.data, realm_blob.length);
-
-               data_blob_free(&domain_blob);
-               data_blob_free(&netbios_blob);
-               data_blob_free(&realm_blob);
-       }
-
-       if (!spnego_gen_challenge(&spnego_chal, &chal, &chal)) {
-               DEBUG(3,("Failed to generate challenge\n"));
-               data_blob_free(&chal);
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
-
-       /* now tell the client to send the auth packet */
-       reply_sesssetup_blob(conn, outbuf, spnego_chal);
-
        data_blob_free(&chal);
-       data_blob_free(&spnego_chal);
 
-       /* and tell smbd that we have already replied to this packet */
+       /* already replied */
        return -1;
 }
-
        
 /****************************************************************************
-reply to a session setup spnego auth packet
+ Reply to a session setup spnego auth packet.
 ****************************************************************************/
+
 static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
                             int length, int bufsize,
                             DATA_BLOB blob1)
 {
-       DATA_BLOB auth;
-       char *workgroup = NULL, *user = NULL, *machine = NULL;
-       DATA_BLOB lmhash, nthash, sess_key;
-       DATA_BLOB plaintext_password = data_blob(NULL, 0);
-       uint32 ntlmssp_command, neg_flags;
-       NTSTATUS nt_status;
-       int sess_vuid;
-       BOOL as_guest;
-       uint32 auth_flags = AUTH_FLAG_NONE;
-       auth_usersupplied_info *user_info = NULL;
-       auth_serversupplied_info *server_info = NULL;
-       extern fstring remote_machine;
-
-       /* we must have setup the auth context by now */
-       if (!ntlmssp_auth_context) {
-               DEBUG(2,("ntlmssp_auth_context is NULL in reply_spnego_auth\n"));
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
+       DATA_BLOB auth, auth_reply;
+       NTSTATUS nt_status = NT_STATUS_INVALID_PARAMETER;
 
        if (!spnego_parse_auth(blob1, &auth)) {
 #if 0
                file_save("auth.dat", blob1.data, blob1.length);
 #endif
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+               return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
-
-       /* now the NTLMSSP encoded auth hashes */
-       if (!msrpc_parse(&auth, "CdBBUUUBd", 
-                        "NTLMSSP", 
-                        &ntlmssp_command, 
-                        &lmhash,
-                        &nthash,
-                        &workgroup, 
-                        &user, 
-                        &machine,
-                        &sess_key,
-                        &neg_flags)) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
-
-       data_blob_free(&auth);
-       data_blob_free(&sess_key);
        
-       DEBUG(3,("Got user=[%s] workgroup=[%s] machine=[%s] len1=%d len2=%d\n",
-                user, workgroup, machine, lmhash.length, nthash.length));
-
-       /* the client has given us its machine name (which we otherwise would not get on port 445).
-          we need to possibly reload smb.conf if smb.conf includes depend on the machine name */
-       fstrcpy(remote_machine, machine);
-       reload_services(True);
-
-#if 0
-       file_save("nthash1.dat", nthash.data, nthash.length);
-       file_save("lmhash1.dat", lmhash.data, lmhash.length);
-#endif
-
-       if (lmhash.length) {
-               auth_flags |= AUTH_FLAG_LM_RESP;
+       if (!global_ntlmssp_state) {
+               /* auth before negotiatiate? */
+               return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
-
-       if (nthash.length == 24) {
-               auth_flags |= AUTH_FLAG_NTLM_RESP;
-       } else if (nthash.length > 24) {
-               auth_flags |= AUTH_FLAG_NTLMv2_RESP;
-       }
-
-       if (!make_user_info_map(&user_info, 
-                               user, workgroup, 
-                               machine, 
-                               lmhash, nthash,
-                               plaintext_password, 
-                               auth_flags, True)) {
-               return ERROR_NT(NT_STATUS_NO_MEMORY);
-       }
-
-       nt_status = ntlmssp_auth_context->check_ntlm_password(ntlmssp_auth_context, user_info, &server_info); 
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               nt_status = do_map_to_guest(nt_status, &server_info, user, workgroup);
-       }
-
-       SAFE_FREE(workgroup);
-       SAFE_FREE(machine);
-                       
-       (ntlmssp_auth_context->free)(&ntlmssp_auth_context);
-
-       free_user_info(&user_info);
-       
-       data_blob_free(&lmhash);
        
-       data_blob_free(&nthash);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               SAFE_FREE(user);
-               return ERROR_NT(nt_status_squash(nt_status));
-       }
-
-       as_guest = server_info->guest;
-
-       sess_vuid = register_vuid(server_info, user);
-       free_server_info(&server_info);
+       nt_status = auth_ntlmssp_update(global_ntlmssp_state, 
+                                               auth, &auth_reply);
 
-       SAFE_FREE(user);
-  
-       if (sess_vuid == -1) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
-
-       set_message(outbuf,4,0,True);
-       SSVAL(outbuf, smb_vwv3, 0);
+       data_blob_free(&auth);
 
-       if (as_guest) {
-               SSVAL(outbuf,smb_vwv2,1);
-       }
+       reply_spnego_ntlmssp(conn, outbuf, &global_ntlmssp_state,
+                            &auth_reply, nt_status);
+               
+       data_blob_free(&auth_reply);
 
-       add_signature(outbuf);
-       SSVAL(outbuf,smb_uid,sess_vuid);
-       SSVAL(inbuf,smb_uid,sess_vuid);
-       
-       return chain_reply(inbuf,outbuf,length,bufsize);
+       /* and tell smbd that we have already replied to this packet */
+       return -1;
 }
 
-
 /****************************************************************************
-reply to a session setup spnego anonymous packet
+ Reply to a session setup command.
 ****************************************************************************/
-static int reply_spnego_anonymous(connection_struct *conn, char *inbuf, char *outbuf,
-                                 int length, int bufsize)
-{
-       int sess_vuid;
-       auth_serversupplied_info *server_info = NULL;
-       NTSTATUS nt_status;
-
-       nt_status = check_guest_password(&server_info);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return ERROR_NT(nt_status_squash(nt_status));
-       }
-
-       sess_vuid = register_vuid(server_info, lp_guestaccount());
-
-       free_server_info(&server_info);
-  
-       if (sess_vuid == -1) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
-
-       set_message(outbuf,4,0,True);
-       SSVAL(outbuf, smb_vwv3, 0);
-       add_signature(outbuf);
-       SSVAL(outbuf,smb_uid,sess_vuid);
-       SSVAL(inbuf,smb_uid,sess_vuid);
-       
-       return chain_reply(inbuf,outbuf,length,bufsize);
-}
 
-
-/****************************************************************************
-reply to a session setup command
-****************************************************************************/
-static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,char *outbuf,
+static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,
+                                       char *outbuf,
                                        int length,int bufsize)
 {
        uint8 *p;
        DATA_BLOB blob1;
        int ret;
+       size_t bufrem;
+       fstring native_os, native_lanman;
+       char *p2;
+       uint16 data_blob_len = SVAL(inbuf, smb_vwv7);
+       enum remote_arch_types ra_type = get_remote_arch();
 
        DEBUG(3,("Doing spnego session setup\n"));
 
@@ -551,18 +453,27 @@ static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,cha
                
        p = (uint8 *)smb_buf(inbuf);
 
-       if (SVAL(inbuf, smb_vwv7) == 0) {
-               /* an anonymous request */
-               return reply_spnego_anonymous(conn, inbuf, outbuf, length, bufsize);
+       if (data_blob_len == 0) {
+               /* an invalid request */
+               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
+       bufrem = smb_bufrem(inbuf, p);
        /* pull the spnego blob */
-       blob1 = data_blob(p, SVAL(inbuf, smb_vwv7));
+       blob1 = data_blob(p, MIN(bufrem, data_blob_len));
 
 #if 0
        file_save("negotiate.dat", blob1.data, blob1.length);
 #endif
 
+       p2 = inbuf + smb_vwv13 + data_blob_len;
+       p2 += srvstr_pull_buf(inbuf, native_os, p2, sizeof(native_os), STR_TERMINATE);
+       p2 += srvstr_pull_buf(inbuf, native_lanman, p2, sizeof(native_lanman), STR_TERMINATE);
+       DEBUG(3,("NativeOS=[%s] NativeLanMan=[%s]\n", native_os, native_lanman));
+
+       if ( ra_type == RA_WIN2K )
+               ra_lanman_string( native_lanman );
+
        if (blob1.data[0] == ASN1_APPLICATION(0)) {
                /* its a negTokenTarg packet */
                ret = reply_spnego_negotiate(conn, inbuf, outbuf, length, bufsize, blob1);
@@ -585,10 +496,25 @@ static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,cha
        return ERROR_NT(NT_STATUS_LOGON_FAILURE);
 }
 
+/****************************************************************************
+ On new VC == 0, shutdown *all* old connections and users.
+ It seems that only NT4.x does this. At W2K and above (XP etc.).
+ a new session setup with VC==0 is ignored.
+****************************************************************************/
+
+static void setup_new_vc_session(void)
+{
+       DEBUG(2,("setup_new_vc_session: New VC == 0, if NT4.x compatible we would close all old resources.\n"));
+#if 0
+       conn_close_all();
+       invalidate_all_vuids();
+#endif
+}
 
 /****************************************************************************
-reply to a session setup command
+ Reply to a session setup command.
 ****************************************************************************/
+
 int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                          int length,int bufsize)
 {
@@ -597,8 +523,8 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        DATA_BLOB lm_resp;
        DATA_BLOB nt_resp;
        DATA_BLOB plaintext_password;
-       pstring user;
-       pstring sub_user; /* Sainitised username for substituion */
+       fstring user;
+       fstring sub_user; /* Sainitised username for substituion */
        fstring domain;
        fstring native_os;
        fstring native_lanman;
@@ -606,8 +532,6 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        extern BOOL global_encrypted_passwords_negotiated;
        extern BOOL global_spnego_negotiated;
        extern int Protocol;
-       extern fstring remote_machine;
-       extern userdom_struct current_user_info;
        extern int max_send;
 
        auth_usersupplied_info *user_info = NULL;
@@ -617,7 +541,7 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        NTSTATUS nt_status;
 
        BOOL doencrypt = global_encrypted_passwords_negotiated;
-
+       
        START_PROFILE(SMBsesssetupX);
 
        ZERO_STRUCT(lm_resp);
@@ -635,6 +559,9 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                        return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
                }
 
+               if (SVAL(inbuf,smb_vwv4) == 0) {
+                       setup_new_vc_session();
+               }
                return reply_sesssetup_and_X_spnego(conn, inbuf, outbuf, length, bufsize);
        }
 
@@ -656,7 +583,7 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
 
                srvstr_pull_buf(inbuf, user, smb_buf(inbuf)+passlen1, sizeof(user), STR_TERMINATE);
                *domain = 0;
-  
+
        } else {
                uint16 passlen1 = SVAL(inbuf,smb_vwv7);
                uint16 passlen2 = SVAL(inbuf,smb_vwv8);
@@ -676,7 +603,7 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                                set_remote_arch( RA_WIN95);
                        }
                }
-               
+
                if (!doencrypt) {
                        /* both Win95 and WinNT stuff up the password lengths for
                           non-encrypting systems. Uggh. 
@@ -714,8 +641,11 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                        nt_resp = data_blob(p+passlen1, passlen2);
                } else {
                        pstring pass;
+                       BOOL unic;
+                       unic=SVAL(inbuf, smb_flg2) & FLAGS2_UNICODE_STRINGS;
                        srvstr_pull(inbuf, pass, smb_buf(inbuf), 
-                                   sizeof(pass),  passlen1, STR_TERMINATE);
+                                   sizeof(pass),  unic ? passlen2 : passlen1, 
+                                   STR_TERMINATE);
                        plaintext_password = data_blob(pass, strlen(pass)+1);
                }
                
@@ -726,31 +656,35 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                p += srvstr_pull_buf(inbuf, native_lanman, p, sizeof(native_lanman), STR_TERMINATE);
                DEBUG(3,("Domain=[%s]  NativeOS=[%s] NativeLanMan=[%s]\n",
                         domain,native_os,native_lanman));
+
+               if ( ra_type == RA_WIN2K )
+                       ra_lanman_string( native_lanman );
+
        }
        
-       /* don't allow for weird usernames or domains */
-       alpha_strcpy(user, user, ". _-$", sizeof(user));
-       alpha_strcpy(domain, domain, ". _-", sizeof(domain));
-       if (strstr(user, "..") || strstr(domain,"..")) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+       if (SVAL(inbuf,smb_vwv4) == 0) {
+               setup_new_vc_session();
        }
 
-       DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n", domain, user, remote_machine));
+       DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n", domain, user, get_remote_machine_name()));
 
        if (*user) {
                if (global_spnego_negotiated) {
                        
-                       /* This has to be here, becouse this is a perfectly valid behaviour for guest logons :-( */
+                       /* This has to be here, because this is a perfectly valid behaviour for guest logons :-( */
                        
                        DEBUG(0,("reply_sesssetup_and_X:  Rejecting attempt at 'normal' session setup after negotiating spnego.\n"));
                        return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
                }
-               pstrcpy(sub_user, user);
+               fstrcpy(sub_user, user);
+
+               /* setup the string used by %U */
+               sub_set_smb_name(user);
        } else {
-               pstrcpy(sub_user, lp_guestaccount());
+               fstrcpy(sub_user, lp_guestaccount());
        }
 
-       pstrcpy(current_user_info.smb_name,sub_user);
+       sub_set_smb_name(sub_user);
 
        reload_services(True);
        
@@ -772,11 +706,13 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                nt_status = check_guest_password(&server_info);
 
        } else if (doencrypt) {
-               if (!make_user_info_for_reply_enc(&user_info, 
-                                                 user, domain, 
-                                                 lm_resp, nt_resp)) {
-                       nt_status = NT_STATUS_NO_MEMORY;
-               } else {
+               if (!negprot_global_auth_context) {
+                       DEBUG(0, ("reply_sesssetup_and_X:  Attempted encrypted session setup without negprot denied!\n"));
+                       return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+               }
+               nt_status = make_user_info_for_reply_enc(&user_info, user, domain,
+                                                        lm_resp, nt_resp);
+               if (NT_STATUS_IS_OK(nt_status)) {
                        nt_status = negprot_global_auth_context->check_ntlm_password(negprot_global_auth_context, 
                                                                                     user_info, 
                                                                                     &server_info);
@@ -818,11 +754,11 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        }
        
        /* it's ok - setup a reply */
-       if (Protocol < PROTOCOL_NT1) {
-               set_message(outbuf,3,0,True);
-       } else {
-               set_message(outbuf,3,0,True);
-               add_signature(outbuf);
+       set_message(outbuf,3,0,True);
+       if (Protocol >= PROTOCOL_NT1) {
+               char *p = smb_buf( outbuf );
+               p += add_signature( outbuf, p );
+               set_message_end( outbuf, p );
                /* perhaps grab OS version here?? */
        }
        
@@ -833,15 +769,13 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        /* register the name and uid as being validated, so further connections
           to a uid can get through without a password, on the same VC */
 
+       /* register_vuid keeps the server info */
        sess_vuid = register_vuid(server_info, sub_user);
-
-       free_server_info(&server_info);
   
        if (sess_vuid == -1) {
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
        SSVAL(outbuf,smb_uid,sess_vuid);
        SSVAL(inbuf,smb_uid,sess_vuid);