Refactor the NTLMSSP code again - this time we use function pointers to
[kai/samba.git] / source3 / smbd / sesssetup.c
index 77f93812dd545f36260136de0fa28423303f610a..939cdf2a91310d28942de5cab05044965e39249b 100644 (file)
@@ -23,7 +23,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"
@@ -159,13 +160,18 @@ static int reply_spnego_kerberos(connection_struct *conn,
        }
        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);
+       pw = Get_Pwnam(user);
        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);
+               pw = Get_Pwnam(user2);
                if (pw) {
                        free(user);
                        user = user2;
@@ -177,9 +183,9 @@ static int reply_spnego_kerberos(connection_struct *conn,
                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);
+               return ERROR_NT(ret);
        }
        
        sess_vuid = register_vuid(server_info, user);
@@ -207,7 +213,7 @@ static int reply_spnego_kerberos(connection_struct *conn,
 send a security blob via a session setup reply
 ****************************************************************************/
 static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
-                                DATA_BLOB blob, uint32 errcode)
+                                DATA_BLOB blob, NTSTATUS errcode)
 {
        char *p;
 
@@ -216,7 +222,7 @@ static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
        /* we set NT_STATUS_MORE_PROCESSING_REQUIRED to tell the other end
           that we aren't finished yet */
 
-       SIVAL(outbuf, smb_rcls, errcode);
+       SIVAL(outbuf, smb_rcls, NT_STATUS_V(errcode));
        SSVAL(outbuf, smb_vwv0, 0xFF); /* no chaining possible */
        SSVAL(outbuf, smb_vwv3, blob.length);
        p = smb_buf(outbuf);
@@ -230,6 +236,50 @@ static BOOL reply_sesssetup_blob(connection_struct *conn, char *outbuf,
        return send_smb(smbd_server_fd(),outbuf);
 }
 
+/****************************************************************************
+send an NTLMSSP blob via a session setup reply, wrapped in SPNEGO
+****************************************************************************/
+static BOOL reply_spnego_ntlmssp_blob(connection_struct *conn, char *outbuf,
+                                        DATA_BLOB *ntlmssp_blob, NTSTATUS errcode) 
+{
+       DATA_BLOB response;
+        response = spnego_gen_auth_response(ntlmssp_blob);
+       reply_sesssetup_blob(conn, outbuf, response, errcode);
+       data_blob_free(&response);
+       return True;
+}
+
+/****************************************************************************
+ send an OK via a session setup reply, wrapped in SPNEGO.
+ get vuid and check first.
+****************************************************************************/
+static BOOL reply_spnego_ntlmssp_ok(connection_struct *conn, char *outbuf,
+                                   AUTH_NTLMSSP_STATE *auth_ntlmssp_state) 
+{
+       int sess_vuid;
+       pstring user;
+       DATA_BLOB null_blob = data_blob(NULL, 0);
+
+       sess_vuid = register_vuid(auth_ntlmssp_state->server_info, auth_ntlmssp_state->ntlmssp_state->user /* check this for weird */);
+
+       if (sess_vuid == -1) {
+               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+       }
+
+       set_message(outbuf,4,0,True);
+       SSVAL(outbuf, smb_vwv3, 0);
+
+       if (auth_ntlmssp_state->server_info->guest) {
+               SSVAL(outbuf,smb_vwv2,1);
+       }
+
+       add_signature(outbuf);
+       SSVAL(outbuf,smb_uid,sess_vuid);
+       reply_spnego_ntlmssp_blob(conn, outbuf, &null_blob, NT_STATUS_OK);
+       return True;
+}
+
 /****************************************************************************
 reply to a session setup spnego negotiate packet
 ****************************************************************************/
@@ -242,13 +292,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;
-       const uint8 *cryptkey;
        BOOL got_kerberos = False;
        NTSTATUS nt_status;
-       extern pstring global_myname;
-       char *cliname=NULL, *domname=NULL;
 
        /* parse out the OIDs and the first sec blob */
        if (!parse_negTokenTarg(blob1, OIDs, &secblob)) {
@@ -274,98 +320,57 @@ 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, "CddAA",
-                        "NTLMSSP",
-                        &ntlmssp_command,
-                        &neg_flags,
-                        &cliname,
-                        &domname)) {
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+       if (global_ntlmssp_state) {
+               auth_ntlmssp_end(&global_ntlmssp_state);
        }
-       
-       data_blob_free(&secblob);
 
-       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=0x%08x\n", neg_flags));
+       nt_status = auth_ntlmssp_update(global_ntlmssp_state, 
+                                       secblob, &chal);
 
-       debug_ntlmssp_flags(neg_flags);
+       data_blob_free(&secblob);
 
-       if (ntlmssp_auth_context) {
-               (ntlmssp_auth_context->free)(&ntlmssp_auth_context);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               nt_status = do_map_to_guest(nt_status, 
+                                           &global_ntlmssp_state->server_info, 
+                                           global_ntlmssp_state->ntlmssp_state->user, 
+                                           global_ntlmssp_state->ntlmssp_state->domain);
        }
+       
+       if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
+               if (!spnego_gen_challenge(&spnego_chal, &chal, NULL)) {
+                       DEBUG(3,("Failed to generate challenge\n"));
+                       data_blob_free(&chal);
+                       return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+               }
 
-       if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&ntlmssp_auth_context))) {
-               return ERROR_NT(nt_status);
-       }
+               /* now tell the client to send the auth packet */
+               reply_sesssetup_blob(conn, outbuf, spnego_chal, nt_status);
+               
+               data_blob_free(&chal);
+               data_blob_free(&spnego_chal);
 
-       cryptkey = ntlmssp_auth_context->get_ntlm_challenge(ntlmssp_auth_context);
+               /* and tell smbd that we have already replied to this packet */
+               return -1;
 
-       /* 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_128 | 
-               NTLMSSP_NEGOTIATE_NTLM |
-               NTLMSSP_CHAL_TARGET_INFO;
-       
-       {
-               DATA_BLOB domain_blob, struct_blob;
-               fstring dnsname, dnsdomname;
-               
-               msrpc_gen(&domain_blob, 
-                         "U",
-                         lp_workgroup());
-
-               fstrcpy(dnsdomname, lp_realm());
-               strlower(dnsdomname);
-
-               fstrcpy(dnsname, global_myname);
-               fstrcat(dnsname, ".");
-               fstrcat(dnsname, lp_realm());
-               strlower(dnsname);
-
-               msrpc_gen(&struct_blob, "aaaaa",
-                         2, lp_workgroup(),
-                         1, global_myname,
-                         4, dnsdomname,
-                         3, dnsname,
-                         0, "");
-
-               msrpc_gen(&chal, "CdUdbddB",
-                         "NTLMSSP", 
-                         NTLMSSP_CHALLENGE,
-                         lp_workgroup(),
-                         chal_flags,
-                         cryptkey, 8,
-                         0, 0,
-                         struct_blob.data, struct_blob.length);
-
-               data_blob_free(&domain_blob);
-               data_blob_free(&struct_blob);
-       }
-
-       if (!spnego_gen_challenge(&spnego_chal, &chal, &chal)) {
-               DEBUG(3,("Failed to generate challenge\n"));
+       } else if (NT_STATUS_IS_OK(nt_status)) {
+               reply_spnego_ntlmssp_ok(conn, outbuf, 
+                                                global_ntlmssp_state);
+               auth_ntlmssp_end(&global_ntlmssp_state);
                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, NT_STATUS_V(NT_STATUS_MORE_PROCESSING_REQUIRED));
+               /* and tell smbd that we have already replied to this packet */
+               return -1;
+       } 
 
+       auth_ntlmssp_end(&global_ntlmssp_state);
        data_blob_free(&chal);
-       data_blob_free(&spnego_chal);
 
-       /* and tell smbd that we have already replied to this packet */
-       return -1;
+       return ERROR_NT(nt_status_squash(nt_status));
 }
 
        
@@ -376,23 +381,8 @@ static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
                             int length, int bufsize,
                             DATA_BLOB blob1)
 {
-       DATA_BLOB auth, response;
-       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;
+       DATA_BLOB auth, auth_reply;
        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;
-
-       /* 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);
-       }
 
        if (!spnego_parse_auth(blob1, &auth)) {
 #if 0
@@ -401,105 +391,22 @@ static int reply_spnego_auth(connection_struct *conn, char *inbuf, char *outbuf,
                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);
-       }
+       nt_status = auth_ntlmssp_update(global_ntlmssp_state, 
+                                         auth, &auth_reply);
 
        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 */
+       data_blob_free(&auth_reply);
 
-       set_remote_machine_name(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 (nthash.length == 24) {
-               auth_flags |= AUTH_FLAG_NTLM_RESP;
-       } else if (nthash.length > 24) {
-               auth_flags |= AUTH_FLAG_NTLMv2_RESP;
-       }
+       if (NT_STATUS_IS_OK(nt_status)) {
+               reply_spnego_ntlmssp_ok(conn, outbuf, 
+                                       global_ntlmssp_state);
+               auth_ntlmssp_end(&global_ntlmssp_state);
 
-       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);
+       } else { /* !NT_STATUS_IS_OK(nt_status) */
+               auth_ntlmssp_end(&global_ntlmssp_state);
                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);
-
-       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);
-
-       if (as_guest) {
-               SSVAL(outbuf,smb_vwv2,1);
-       }
-
-       add_signature(outbuf);
-       SSVAL(outbuf,smb_uid,sess_vuid);
-       SSVAL(inbuf,smb_uid,sess_vuid);
-
-        response = spnego_gen_auth_response();
-       reply_sesssetup_blob(conn, outbuf, response, 0);
-
        /* and tell smbd that we have already replied to this packet */
        return -1;
 }
@@ -604,8 +511,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;
@@ -623,7 +530,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);
@@ -736,7 +643,7 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        
        /* don't allow for weird usernames or domains */
        alpha_strcpy(user, user, ". _-$", sizeof(user));
-       alpha_strcpy(domain, domain, ". _-", sizeof(domain));
+       alpha_strcpy(domain, domain, ". _-@", sizeof(domain));
        if (strstr(user, "..") || strstr(domain,"..")) {
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
@@ -751,12 +658,15 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                        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);
+       fstrcpy(current_user_info.smb_name,sub_user);
 
        reload_services(True);
        
@@ -778,11 +688,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);
@@ -824,10 +736,8 @@ 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);
+       set_message(outbuf,3,0,True);
+       if (Protocol == PROTOCOL_NT1) {
                add_signature(outbuf);
                /* perhaps grab OS version here?? */
        }