Fix the events API. Patch by metze with some minor modifications.
[sfrench/samba-autobuild/.git] / source / smbd / sesssetup.c
index c8bf2a4f94fd29e20e4b74f5baee98fbc7f76013..263196173927d5e9a88e945b63474b987a06690d 100644 (file)
@@ -1,9 +1,10 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 3.0
+   Unix SMB/CIFS implementation.
    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
 
 #include "includes.h"
 
-#if HAVE_KRB5
+uint32 global_client_caps = 0;
+
+static struct auth_ntlmssp_state *global_ntlmssp_state;
+
+/*
+  on a logon error possibly map the error to success if "map to guest"
+  is set approriately
+*/
+static NTSTATUS do_map_to_guest(NTSTATUS status, auth_serversupplied_info **server_info,
+                               const char *user, const char *domain)
+{
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
+               if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) || 
+                   (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_PASSWORD)) {
+                       DEBUG(3,("No such user %s [%s] - using guest account\n",
+                                user, domain));
+                       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));
+                       status = make_server_info_guest(server_info);
+               }
+       }
+
+       return status;
+}
+
+
+/****************************************************************************
+ Add the standard 'Samba' signature to the end of the session setup.
+****************************************************************************/
+static int add_signature(char *outbuf, char *p)
+{
+       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, 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;
+       auth_usersupplied_info *user_info = NULL;
+       
+       NTSTATUS nt_status;
+       unsigned char chal[8];
+
+       ZERO_STRUCT(chal);
+
+       DEBUG(3,("Got anonymous request\n"));
+
+       if (!NT_STATUS_IS_OK(nt_status = make_auth_context_fixed(&auth_context, chal))) {
+               return nt_status;
+       }
+
+       if (!make_user_info_guest(&user_info)) {
+               (auth_context->free)(&auth_context);
+               return NT_STATUS_NO_MEMORY;
+       }
+       
+       nt_status = auth_context->check_ntlm_password(auth_context, user_info, server_info);
+       (auth_context->free)(&auth_context);
+       free_user_info(&user_info);
+       return nt_status;
+}
+
+
+#ifdef HAVE_KRB5
 /****************************************************************************
 reply to a session setup spnego negotiate packet for kerberos
 ****************************************************************************/
@@ -32,137 +139,190 @@ static int reply_spnego_kerberos(connection_struct *conn,
                                 DATA_BLOB *secblob)
 {
        DATA_BLOB ticket;
-       krb5_context context;
-       krb5_principal server;
-       krb5_auth_context auth_context = NULL;
-       krb5_keytab keytab = NULL;
-       krb5_data packet;
-       krb5_ticket *tkt = NULL;
-       int ret;
-       char *realm, *client, *p;
-       fstring service;
-       extern pstring global_myname;
+       char *client, *p;
        const struct passwd *pw;
        char *user;
-       gid_t gid;
-       uid_t uid;
-       char *full_name;
        int sess_vuid;
-
-       realm = lp_realm();
-
-       if (!spnego_parse_krb5_wrap(*secblob, &ticket)) {
+       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, tok_id)) {
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
-       /* the service is the wins name lowercase with $ tacked on */
-       fstrcpy(service, global_myname);
-       strlower(service);
-       fstrcat(service, "$");
-
-       ret = krb5_init_context(&context);
-       if (ret) {
-               DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
+       ads = ads_init_simple();
 
-       ret = krb5_build_principal(context, &server, strlen(realm),
-                                  realm, service, NULL);
-       if (ret) {
-               DEBUG(1,("krb5_build_principal failed (%s)\n", error_message(ret)));
+       if (!ads) {
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
-       packet.length = ticket.length;
-       packet.data = (krb5_pointer)ticket.data;
+       ads->auth.realm = strdup(lp_realm());
 
-       if ((ret = krb5_rd_req(context, &auth_context, &packet, 
-                                      server, keytab, NULL, &tkt))) {
-               DEBUG(3,("krb5_rd_req failed (%s)\n", 
-                        error_message(ret)));
+       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);
        }
 
-       if ((ret = krb5_unparse_name(context, tkt->enc_part2->client,
-                                    &client))) {
-               DEBUG(3,("krb5_unparse_name failed (%s)\n", 
-                        error_message(ret)));
-               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 principle\n"));
+               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);
        }
 
        *p = 0;
-       if (strcasecmp(p+1, realm) != 0) {
-               DEBUG(3,("Ticket for incorrect realm %s\n", p+1));
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+       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);
+               }
+               foreign = True;
        }
+
+       /* 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);
        
-       user = 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);
 
-       /* the password is good - let them in */
-       pw = smb_getpwnam(user,False);
        if (!pw) {
                DEBUG(1,("Username %s is invalid on this system\n",user));
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+               data_blob_free(&ap_rep);
+               return ERROR_NT(NT_STATUS_NO_SUCH_USER);
        }
-       gid = pw->pw_gid;
-       uid = pw->pw_uid;
-       full_name = pw->pw_gecos;
-       
-       sess_vuid = register_vuid(uid,gid,user,user,realm,False, full_name);
+
+       if (!NT_STATUS_IS_OK(ret = make_server_info_pw(&server_info,pw))) {
+               DEBUG(1,("make_server_info_from_pw failed!\n"));
+               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);
 
        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);
-       p = smb_buf(outbuf);
-       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);
-       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;
 }
 
 /****************************************************************************
@@ -177,10 +337,9 @@ static int reply_spnego_negotiate(connection_struct *conn,
        char *OIDs[ASN1_MAX_OIDS];
        DATA_BLOB secblob;
        int i;
-       uint32 ntlmssp_command, neg_flags;
-       DATA_BLOB sess_key, chal, spnego_chal;
-       uint8 cryptkey[8];
+       DATA_BLOB chal;
        BOOL got_kerberos = False;
+       NTSTATUS nt_status;
 
        /* parse out the OIDs and the first sec blob */
        if (!parse_negTokenTarg(blob1, OIDs, &secblob)) {
@@ -189,15 +348,16 @@ static int reply_spnego_negotiate(connection_struct *conn,
        
        for (i=0;OIDs[i];i++) {
                DEBUG(3,("Got OID %s\n", OIDs[i]));
-               if (strcmp(OID_KERBEROS5_OLD, OIDs[i]) == 0) {
+               if (strcmp(OID_KERBEROS5, OIDs[i]) == 0 ||
+                   strcmp(OID_KERBEROS5_OLD, OIDs[i]) == 0) {
                        got_kerberos = True;
                }
                free(OIDs[i]);
        }
        DEBUG(3,("Got secblob of size %d\n", secblob.length));
 
-#if HAVE_KRB5
-       if (got_kerberos) {
+#ifdef HAVE_KRB5
+       if (got_kerberos && (SEC_ADS == lp_security())) {
                int ret = reply_spnego_kerberos(conn, inbuf, outbuf, 
                                                length, bufsize, &secblob);
                data_blob_free(&secblob);
@@ -205,61 +365,26 @@ 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,
-                        &sess_key)) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+       if (global_ntlmssp_state) {
+               auth_ntlmssp_end(&global_ntlmssp_state);
        }
 
-       data_blob_free(&secblob);
-       data_blob_free(&sess_key);
-
-       if (ntlmssp_command != NTLMSSP_NEGOTIATE) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+       nt_status = auth_ntlmssp_start(&global_ntlmssp_state);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               return ERROR_NT(nt_status);
        }
 
-       DEBUG(3,("Got neg_flags=%08x\n", neg_flags));
+       nt_status = auth_ntlmssp_update(global_ntlmssp_state, 
+                                       secblob, &chal);
 
-       if (!last_challenge(cryptkey)) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
-
-       /* Give them the challenge. For now, ignore neg_flags and just
-          return the flags we want. Obviously this is not correct */
-       
-       neg_flags = NTLMSSP_NEGOTIATE_UNICODE | 
-               NTLMSSP_NEGOTIATE_LM_KEY | 
-               NTLMSSP_NEGOTIATE_NTLM;
-
-       msrpc_gen(&chal, "Cddddbdddd",
-                 "NTLMSSP", 
-                 NTLMSSP_CHALLENGE,
-                 0,
-                 0x30, /* ?? */
-                 neg_flags,
-                 cryptkey, 8,
-                 0, 0, 0,
-                 0x3000); /* ?? */
-
-       if (!spnego_gen_challenge(&spnego_chal, &chal, &chal)) {
-               DEBUG(3,("Failed to generate challenge\n"));
-               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(&secblob);
 
+       reply_spnego_ntlmssp(conn, outbuf, &global_ntlmssp_state,
+                            &chal, nt_status);
+               
        data_blob_free(&chal);
-       data_blob_free(&spnego_chal);
 
-       /* and tell smbd that we have already replied to this packet */
+       /* already replied */
        return -1;
 }
 
@@ -271,124 +396,81 @@ static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
                             int length, int bufsize,
                             DATA_BLOB blob1)
 {
-       DATA_BLOB auth;
-       char *workgroup, *user, *machine;
-       DATA_BLOB lmhash, nthash, sess_key;
-       uint32 ntlmssp_command, neg_flags;
-       NTSTATUS nt_status;
-       int sess_vuid;
-       gid_t gid;
-       uid_t uid;
-       char *full_name;
-       char *p;
-       const struct passwd *pw;
+       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);
-       }
-
-       /* 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);
+               return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
-
-       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));
-
-#if 0
-       file_save("nthash1.dat", nthash.data, nthash.length);
-       file_save("lmhash1.dat", lmhash.data, lmhash.length);
-#endif
-
-       nt_status = pass_check_smb(user, user, 
-                                  workgroup, machine,
-                                  lmhash.data,
-                                  lmhash.length,
-                                  nthash.data,
-                                  nthash.length);
-
-       data_blob_free(&nthash);
-       data_blob_free(&lmhash);
-
-       if (!NT_STATUS_IS_OK(nt_status)) {
-               return ERROR_NT(nt_status);
-       }
-
-       /* the password is good - let them in */
-       pw = smb_getpwnam(user,False);
-       if (!pw) {
-               DEBUG(1,("Username %s is invalid on this system\n",user));
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+       if (!global_ntlmssp_state) {
+               /* auth before negotiatiate? */
+               return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
-       gid = pw->pw_gid;
-       uid = pw->pw_uid;
-       full_name = pw->pw_gecos;
+       
+       nt_status = auth_ntlmssp_update(global_ntlmssp_state, 
+                                               auth, &auth_reply);
 
-       sess_vuid = register_vuid(uid,gid,user,user,workgroup,False, full_name);
+       data_blob_free(&auth);
 
-       free(user);
-       free(workgroup);
-       free(machine);
-       
-       if (sess_vuid == -1) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-       }
+       reply_spnego_ntlmssp(conn, outbuf, &global_ntlmssp_state,
+                            &auth_reply, nt_status);
+               
+       data_blob_free(&auth_reply);
 
-       set_message(outbuf,4,0,True);
-       SSVAL(outbuf, smb_vwv3, 0);
-       p = smb_buf(outbuf);
-       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);
-       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 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;
-       extern uint32 global_client_caps;
        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"));
 
        if (global_client_caps == 0) {
                global_client_caps = IVAL(inbuf,smb_vwv10);
        }
                
-       p = smb_buf(inbuf);
+       p = (uint8 *)smb_buf(inbuf);
 
+       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
-       chdir("/home/tridge");
        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);
@@ -411,6 +493,20 @@ 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
@@ -419,57 +515,77 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                          int length,int bufsize)
 {
        int sess_vuid;
-       gid_t gid;
-       uid_t uid;
-       char* full_name;
        int   smb_bufsize;    
-       int   smb_apasslen = 0;   
-       pstring smb_apasswd;
-       int   smb_ntpasslen = 0;   
-       pstring smb_ntpasswd;
-       pstring user;
-       pstring orig_user;
+       DATA_BLOB lm_resp;
+       DATA_BLOB nt_resp;
+       DATA_BLOB plaintext_password;
+       fstring user;
+       fstring sub_user; /* Sainitised username for substituion */
        fstring domain;
        fstring native_os;
        fstring native_lanman;
-       BOOL guest=False;
        static BOOL done_sesssetup = False;
        extern BOOL global_encrypted_passwords_negotiated;
-       extern uint32 global_client_caps;
+       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;
+       extern struct auth_context *negprot_global_auth_context;
+       auth_serversupplied_info *server_info = NULL;
+
+       NTSTATUS nt_status;
+
        BOOL doencrypt = global_encrypted_passwords_negotiated;
-       START_PROFILE(SMBsesssetupX);
        
-       if (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY) {
-               /* it's a SPNEGO session setup */
+       START_PROFILE(SMBsesssetupX);
+
+       ZERO_STRUCT(lm_resp);
+       ZERO_STRUCT(nt_resp);
+       ZERO_STRUCT(plaintext_password);
+
+       DEBUG(3,("wct=%d flg2=0x%x\n", CVAL(inbuf, smb_wct), SVAL(inbuf, smb_flg2)));
+
+       /* a SPNEGO session setup has 12 command words, whereas a normal
+          NT1 session setup has 13. See the cifs spec. */
+       if (CVAL(inbuf, smb_wct) == 12 &&
+           (SVAL(inbuf, smb_flg2) & FLAGS2_EXTENDED_SECURITY)) {
+               if (!global_spnego_negotiated) {
+                       DEBUG(0,("reply_sesssetup_and_X:  Rejecting attempt at SPNEGO session setup when it was not negoitiated.\n"));
+                       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);
        }
 
-       *smb_apasswd = *smb_ntpasswd = 0;
-       
        smb_bufsize = SVAL(inbuf,smb_vwv2);
-       
+
        if (Protocol < PROTOCOL_NT1) {
-               smb_apasslen = SVAL(inbuf,smb_vwv7);
-               if (smb_apasslen > MAX_PASS_LEN) {
-                       return ERROR_DOS(ERRDOS,ERRbuftoosmall);
+               uint16 passlen1 = SVAL(inbuf,smb_vwv7);
+               if ((passlen1 > MAX_PASS_LEN) || (passlen1 > smb_bufrem(inbuf, smb_buf(inbuf)))) {
+                       return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
 
-               memcpy(smb_apasswd,smb_buf(inbuf),smb_apasslen);
-               srvstr_pull(inbuf, user, smb_buf(inbuf)+smb_apasslen, sizeof(user), -1, STR_TERMINATE);
-               
-               if (!doencrypt && (lp_security() != SEC_SERVER)) {
-                       smb_apasslen = strlen(smb_apasswd);
+               if (doencrypt) {
+                       lm_resp = data_blob(smb_buf(inbuf), passlen1);
+               } else {
+                       plaintext_password = data_blob(smb_buf(inbuf), passlen1+1);
+                       /* Ensure null termination */
+                       plaintext_password.data[passlen1] = 0;
                }
+
+               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);
                enum remote_arch_types ra_type = get_remote_arch();
                char *p = smb_buf(inbuf);    
-               
+
                if(global_client_caps == 0)
                        global_client_caps = IVAL(inbuf,smb_vwv11);
                
@@ -483,17 +599,7 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                                set_remote_arch( RA_WIN95);
                        }
                }
-               
-               if (passlen1 != 24 && passlen2 < 24)
-                       doencrypt = False;
-               
-               if (passlen1 > MAX_PASS_LEN) {
-                       return ERROR_DOS(ERRDOS,ERRbuftoosmall);
-               }
-               
-               passlen1 = MIN(passlen1, MAX_PASS_LEN);
-               passlen2 = MIN(passlen2, MAX_PASS_LEN);
-               
+
                if (!doencrypt) {
                        /* both Win95 and WinNT stuff up the password lengths for
                           non-encrypting systems. Uggh. 
@@ -511,232 +617,161 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                                passlen2 = 0;
                }
                
-               if (lp_restrict_anonymous()) {
-                       /* there seems to be no reason behind the
-                        * differences in MS clients formatting
-                        * various info like the domain, NativeOS, and
-                        * NativeLanMan fields. Win95 in particular
-                        * seems to have an extra null byte between
-                        * the username and the domain, or the
-                        * password length calculation is wrong, which
-                        * throws off the string extraction routines
-                        * below.  This makes the value of domain be
-                        * the empty string, which fails the restrict
-                        * anonymous check further down.  This
-                        * compensates for that, and allows browsing
-                        * to work in mixed NT and win95 environments
-                        * even when restrict anonymous is true. AAB
-                        * */
-                       dump_data(100, p, 0x70);
-                       DEBUG(9, ("passlen1=%d, passlen2=%d\n", passlen1, passlen2));
-                       if (ra_type == RA_WIN95 && !passlen1 && !passlen2 && p[0] == 0 && p[1] == 0) {
-                               DEBUG(0, ("restrict anonymous parameter used in a win95 environment!\n"));
-                               DEBUG(0, ("client is win95 and broken passlen1 offset -- attempting fix\n"));
-                               DEBUG(0, ("if win95 cilents are having difficulty browsing, you will be unable to use restrict anonymous\n"));
-                               passlen1 = 1;
-                       }
+               /* check for nasty tricks */
+               if (passlen1 > MAX_PASS_LEN || passlen1 > smb_bufrem(inbuf, p)) {
+                       return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
 
-               /* Save the lanman2 password and the NT md4 password. */
-               smb_apasslen = passlen1;
-               memcpy(smb_apasswd,p,smb_apasslen);
+               if (passlen2 > MAX_PASS_LEN || passlen2 > smb_bufrem(inbuf, p+passlen1)) {
+                       return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
+               }
 
-               smb_ntpasslen = passlen2;
-               memcpy(smb_ntpasswd,p+passlen1,smb_ntpasslen);
+               /* Save the lanman2 password and the NT md4 password. */
+               
+               if ((doencrypt) && (passlen1 != 0) && (passlen1 != 24)) {
+                       doencrypt = False;
+               }
 
-               if (smb_apasslen != 24 || !doencrypt) {
-                       /* trim the password */
-                       smb_apasslen = strlen(smb_apasswd);
-                       
-                       /* wfwg sometimes uses a space instead of a null */
-                       if (strequal(smb_apasswd," ")) {
-                               smb_apasslen = 0;
-                               *smb_apasswd = 0;
-                       }
+               if (doencrypt) {
+                       lm_resp = data_blob(p, passlen1);
+                       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),  unic ? passlen2 : passlen1, 
+                                   STR_TERMINATE);
+                       plaintext_password = data_blob(pass, strlen(pass)+1);
                }
                
                p += passlen1 + passlen2;
-               p += srvstr_pull(inbuf, user, p, sizeof(user), -1,
-                                STR_TERMINATE);
-               p += srvstr_pull(inbuf, domain, p, sizeof(domain), 
-                                -1, STR_TERMINATE);
-               p += srvstr_pull(inbuf, native_os, p, sizeof(native_os), 
-                                -1, STR_TERMINATE);
-               p += srvstr_pull(inbuf, native_lanman, p, sizeof(native_lanman),
-                                -1, STR_TERMINATE);
+               p += srvstr_pull_buf(inbuf, user, p, sizeof(user), STR_TERMINATE);
+               p += srvstr_pull_buf(inbuf, domain, p, sizeof(domain), STR_TERMINATE);
+               p += srvstr_pull_buf(inbuf, native_os, p, sizeof(native_os), STR_TERMINATE);
+               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 (lp_security() == SEC_SHARE) {
-               /* in share level we should ignore any passwords */
-               smb_ntpasslen = 0;
-               smb_apasslen = 0;
-               guest = True;
+       if (SVAL(inbuf,smb_vwv4) == 0) {
+               setup_new_vc_session();
        }
 
+       DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n", domain, user, get_remote_machine_name()));
 
-       DEBUG(3,("sesssetupX:name=[%s]\\[%s]@[%s]\n",user, domain, remote_machine));
-       
-       if (done_sesssetup && lp_restrict_anonymous()) {
-               /* tests show that even if browsing is done over
-                * already validated connections without a username
-                * and password the domain is still provided, which it
-                * wouldn't be if it was a purely anonymous
-                * connection.  So, in order to restrict anonymous, we
-                * only deny connections that have no session
-                * information.  If a domain has been provided, then
-                * it's not a purely anonymous connection. AAB */
-               if (!*user && !*smb_apasswd && !*domain) {
-                       DEBUG(0, ("restrict anonymous is True and anonymous connection attempted. Denying access.\n"));
-                       END_PROFILE(SMBsesssetupX);
-                       return ERROR_DOS(ERRDOS,ERRnoaccess);
+       if (*user) {
+               if (global_spnego_negotiated) {
+                       
+                       /* 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);
                }
-       }
+               fstrcpy(sub_user, user);
 
-       /* If no username is sent use the guest account */
-       if (!*user) {
-               pstrcpy(user,lp_guestaccount(-1));
-               guest = True;
+               /* setup the string used by %U */
+               sub_set_smb_name(user);
+       } else {
+               fstrcpy(sub_user, lp_guestaccount());
        }
-       
-       pstrcpy(current_user_info.smb_name,user);
-       
+
+       sub_set_smb_name(sub_user);
+
        reload_services(True);
        
-       /*
-        * Save the username before mapping. We will use
-        * the original username sent to us for security=server
-        * and security=domain checking.
-        */
-       
-       pstrcpy( orig_user, user);
-       
-       /*
-        * Always try the "DOMAIN\user" lookup first, as this is the most
-        * specific case. If this fails then try the simple "user" lookup.
-        * But don't do this for guests, as this is always a local user.
-        */
+       if (lp_security() == SEC_SHARE) {
+               /* in share level we should ignore any passwords */
+
+               data_blob_free(&lm_resp);
+               data_blob_free(&nt_resp);
+               data_blob_clear_free(&plaintext_password);
+
+               map_username(sub_user);
+               add_session_user(sub_user);
+               /* Then force it to null for the benfit of the code below */
+               *user = 0;
+       }
        
-       if (!guest) {
-               pstring dom_user;
-               
-               /* Work out who's who */
-               
-               slprintf(dom_user, sizeof(dom_user) - 1,"%s%s%s",
-                        domain, lp_winbind_separator(), user);
-               
-               if (sys_getpwnam(dom_user) != NULL) {
-                       pstrcpy(user, dom_user);
-                       DEBUG(3,("Using unix username %s\n", dom_user));
+       if (!*user) {
+
+               nt_status = check_guest_password(&server_info);
+
+       } else if (doencrypt) {
+               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);
+               }
+       } else {
+               struct auth_context *plaintext_auth_context = NULL;
+               const uint8 *chal;
+               if (NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&plaintext_auth_context))) {
+                       chal = plaintext_auth_context->get_ntlm_challenge(plaintext_auth_context);
+                       
+                       if (!make_user_info_for_reply(&user_info, 
+                                                     user, domain, chal,
+                                                     plaintext_password)) {
+                               nt_status = NT_STATUS_NO_MEMORY;
+                       }
                
-               /*
-                * Pass the user through the NT -> unix user mapping
-                * function.
-                */
-               
-               (void)map_username(user);
-               
-               /*
-                * Do any UNIX username case mangling.
-                */
-               smb_getpwnam(user, True);
+                       if (NT_STATUS_IS_OK(nt_status)) {
+                               nt_status = plaintext_auth_context->check_ntlm_password(plaintext_auth_context, 
+                                                                                       user_info, 
+                                                                                       &server_info); 
+                               
+                               (plaintext_auth_context->free)(&plaintext_auth_context);
+                       }
+               }
        }
+
+       free_user_info(&user_info);
        
-       add_session_user(user);
+       data_blob_free(&lm_resp);
+       data_blob_free(&nt_resp);
+       data_blob_clear_free(&plaintext_password);
        
-       if (!guest) {
-               NTSTATUS nt_status;
-               nt_status = pass_check_smb(orig_user, user, 
-                                          domain, remote_machine,
-                                          (unsigned char *)smb_apasswd, 
-                                          smb_apasslen, 
-                                          (unsigned char *)smb_ntpasswd,
-                                          smb_ntpasslen);
-         
-               if NT_STATUS_IS_OK(nt_status) {
-
-               } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER) {
-                       if ((lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_USER) || 
-                           (lp_map_to_guest() ==  MAP_TO_GUEST_ON_BAD_PASSWORD)) {
-                               DEBUG(3,("No such user %s [%s] - using guest account\n",user, domain));
-                               pstrcpy(user,lp_guestaccount(-1));
-                               guest = True;
-                       } else {
-                               /* Match WinXP and don't give the game away */
-                               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-                       }
-               } else if NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD) {
-                       if (lp_map_to_guest() ==  MAP_TO_GUEST_ON_BAD_PASSWORD) {
-                               pstrcpy(user,lp_guestaccount(-1));
-                               DEBUG(3,("Registered username %s for guest access\n",user));
-                               guest = True;
-                       } else {
-                               /* Match WinXP and don't give the game away */
-                               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-                       }
-               } else {
-                       return ERROR_NT(nt_status);
-               }
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               nt_status = do_map_to_guest(nt_status, &server_info, user, domain);
        }
        
-       if (!strequal(user,lp_guestaccount(-1)) &&
-           lp_servicenumber(user) < 0) {
-               add_home_service(user,get_user_home_dir(user));
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               return ERROR_NT(nt_status_squash(nt_status));
        }
-
-
+       
        /* it's ok - setup a reply */
-       if (Protocol < PROTOCOL_NT1) {
-               set_message(outbuf,3,0,True);
-       } else {
-               char *p;
-               set_message(outbuf,3,0,True);
-               p = smb_buf(outbuf);
-               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);
+       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?? */
        }
        
-       /* Set the correct uid in the outgoing and incoming packets
-          We will use this on future requests to determine which
-          user we should become.
-       */
-       {
-               const struct passwd *pw = smb_getpwnam(user,False);
-               if (!pw) {
-                       DEBUG(1,("Username %s is invalid on this system\n",user));
-                       END_PROFILE(SMBsesssetupX);
-                       return ERROR_NT(NT_STATUS_LOGON_FAILURE);
-               }
-               gid = pw->pw_gid;
-               uid = pw->pw_uid;
-               full_name = pw->pw_gecos;
-       }
-       
-       if (guest)
+       if (server_info->guest) {
                SSVAL(outbuf,smb_vwv2,1);
-       
+       }
+
        /* register the name and uid as being validated, so further connections
           to a uid can get through without a password, on the same VC */
-       
-       sess_vuid = register_vuid(uid,gid,user,orig_user,domain,guest, full_name);
-       
+
+       /* register_vuid keeps the server info */
+       sess_vuid = register_vuid(server_info, sub_user);
+  
        if (sess_vuid == -1) {
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
        SSVAL(outbuf,smb_uid,sess_vuid);
        SSVAL(inbuf,smb_uid,sess_vuid);