r7882: Looks like a large patch - but what it actually does is make Samba
[ira/wip.git] / source3 / smbd / sesssetup.c
index 314ffbb4a9b9a58819dfebb67e38afb8d270a78b..95fe571cff1a775a2ba6d87bab503de3f9eba0d6 100644 (file)
@@ -141,18 +141,20 @@ static int reply_spnego_kerberos(connection_struct *conn,
                                 DATA_BLOB *secblob)
 {
        DATA_BLOB ticket;
-       char *client, *p;
-       const struct passwd *pw;
-       char *user;
+       char *client, *p, *domain;
+       fstring netbios_domain_name;
+       struct passwd *pw;
+       fstring user;
        int sess_vuid;
        NTSTATUS ret;
        DATA_BLOB auth_data;
        DATA_BLOB ap_rep, ap_rep_wrapped, response;
        auth_serversupplied_info *server_info = NULL;
-       uint8 session_key[16];
+       DATA_BLOB session_key = data_blob(NULL, 0);
        uint8 tok_id[2];
-       BOOL foreign = False;
        DATA_BLOB nullblob = data_blob(NULL, 0);
+       fstring real_username;
+       BOOL map_domainuser_to_guest = False;
 
        ZERO_STRUCT(ticket);
        ZERO_STRUCT(auth_data);
@@ -164,7 +166,7 @@ static int reply_spnego_kerberos(connection_struct *conn,
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
-       ret = ads_verify_ticket(lp_realm(), &ticket, &client, &auth_data, &ap_rep, session_key);
+       ret = ads_verify_ticket(lp_realm(), &ticket, &client, &auth_data, &ap_rep, &session_key);
 
        data_blob_free(&ticket);
 
@@ -181,6 +183,7 @@ static int reply_spnego_kerberos(connection_struct *conn,
        if (!p) {
                DEBUG(3,("Doesn't look like a valid principal\n"));
                data_blob_free(&ap_rep);
+               data_blob_free(&session_key);
                SAFE_FREE(client);
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
@@ -190,50 +193,121 @@ static int reply_spnego_kerberos(connection_struct *conn,
                DEBUG(3,("Ticket for foreign realm %s@%s\n", client, p+1));
                if (!lp_allow_trusted_domains()) {
                        data_blob_free(&ap_rep);
+                       data_blob_free(&session_key);
                        SAFE_FREE(client);
                        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%c%s", p+1, *lp_winbind_separator(), client);
-       
-       pw = smb_getpwnam( user );
+
+       domain = p+1;
+
+       {
+               /* If we have winbind running, we can (and must) shorten the
+                  username by using the short netbios name. Otherwise we will
+                  have inconsistent user names. With Kerberos, we get the
+                  fully qualified realm, with ntlmssp we get the short
+                  name. And even w2k3 does use ntlmssp if you for example
+                  connect to an ip address. */
+
+               struct winbindd_request wb_request;
+               struct winbindd_response wb_response;
+               NSS_STATUS wb_result;
+
+               ZERO_STRUCT(wb_request);
+               ZERO_STRUCT(wb_response);
+
+               DEBUG(10, ("Mapping [%s] to short name\n", domain));
+
+               fstrcpy(wb_request.domain_name, domain);
+
+               wb_result = winbindd_request_response(WINBINDD_DOMAIN_INFO,
+                                            &wb_request, &wb_response);
+
+               if (wb_result == NSS_STATUS_SUCCESS) {
+
+                       fstrcpy(netbios_domain_name,
+                               wb_response.data.domain_info.name);
+                       domain = netbios_domain_name;
+
+                       DEBUG(10, ("Mapped to [%s]\n", domain));
+               } else {
+                       DEBUG(3, ("Could not find short name -- winbind "
+                                 "not running?\n"));
+               }
+       }
+
+       fstr_sprintf(user, "%s%c%s", domain, *lp_winbind_separator(), client);
        
-       SAFE_FREE(user);
-       SAFE_FREE(client);
+       /* lookup the passwd struct, create a new user if necessary */
+
+       map_username( user );
 
+       pw = smb_getpwnam( user, real_username, True );
        if (!pw) {
-               DEBUG(1,("Username %s is invalid on this system\n",user));
-               data_blob_free(&ap_rep);
-               return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+
+               /* this was originally the behavior of Samba 2.2, if a user
+                  did not have a local uid but has been authenticated, then 
+                  map them to a guest account */
+
+               if (lp_map_to_guest() == MAP_TO_GUEST_ON_BAD_UID){ 
+                       map_domainuser_to_guest = True;
+                       fstrcpy(user,lp_guestaccount());
+                       pw = smb_getpwnam( user, real_username, True );
+               } 
+
+               /* extra sanity check that the guest account is valid */
+
+               if ( !pw ) {
+                       DEBUG(1,("Username %s is invalid on this system\n", user));
+                       SAFE_FREE(client);
+                       data_blob_free(&ap_rep);
+                       data_blob_free(&session_key);
+                       return ERROR_NT(NT_STATUS_LOGON_FAILURE);
+               }
        }
 
        /* setup the string used by %U */
        
-       sub_set_smb_name(pw->pw_name);
+       sub_set_smb_name( real_username );
        reload_services(True);
-       
-       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);
+       if ( map_domainuser_to_guest ) {
+               make_server_info_guest(&server_info);
+       } else {
+               ret = make_server_info_pw(&server_info, real_username, pw);
+               if ( !NT_STATUS_IS_OK(ret) ) {
+                       DEBUG(1,("make_server_info_from_pw failed!\n"));
+                       SAFE_FREE(client);
+                       data_blob_free(&ap_rep);
+                       data_blob_free(&session_key);
+                       passwd_free(&pw);
+                       return ERROR_NT(ret);
+               }
        }
+       passwd_free(&pw);
+
+        /* make_server_info_pw does not set the domain. Without this we end up
+        * with the local netbios name in substitutions for %D. */
 
-       /* Copy out the session key from the AP_REQ. */
-       memcpy(server_info->session_key, session_key, sizeof(session_key));
+        if (server_info->sam_account != NULL) {
+                pdb_set_domain(server_info->sam_account, domain, PDB_SET);
+        }
 
        /* register_vuid keeps the server info */
-       sess_vuid = register_vuid(server_info, nullblob, user);
+       /* register_vuid takes ownership of session_key, no need to free after this.
+          A better interface would copy it.... */
+       sess_vuid = register_vuid(server_info, session_key, nullblob, client);
 
-       free(user);
+       SAFE_FREE(client);
 
        if (sess_vuid == -1) {
                ret = NT_STATUS_LOGON_FAILURE;
        } else {
+               /* current_user_info is changed on new vuid */
+               reload_services( True );
+
                set_message(outbuf,4,0,True);
                SSVAL(outbuf, smb_vwv3, 0);
                        
@@ -243,14 +317,14 @@ static int reply_spnego_kerberos(connection_struct *conn,
                
                SSVAL(outbuf, smb_uid, sess_vuid);
 
-               if (!server_info->guest) {
+               if (!server_info->guest && !srv_signing_started()) {
                        /* We need to start the signing engine
                         * here but a W2K client sends the old
                         * "BSRSPYL " signature instead of the
                         * correct one. Subsequent packets will
                         * be correct.
                         */
-                       srv_check_sign_mac(inbuf);
+                       srv_check_sign_mac(inbuf, False);
                }
        }
 
@@ -297,15 +371,19 @@ static BOOL reply_spnego_ntlmssp(connection_struct *conn, char *inbuf, char *out
        if (NT_STATUS_IS_OK(nt_status)) {
                int sess_vuid;
                DATA_BLOB nullblob = data_blob(NULL, 0);
+               DATA_BLOB session_key = data_blob((*auth_ntlmssp_state)->ntlmssp_state->session_key.data, (*auth_ntlmssp_state)->ntlmssp_state->session_key.length);
 
                /* register_vuid keeps the server info */
-               sess_vuid = register_vuid(server_info, nullblob, (*auth_ntlmssp_state)->ntlmssp_state->user);
+               sess_vuid = register_vuid(server_info, session_key, nullblob, (*auth_ntlmssp_state)->ntlmssp_state->user);
                (*auth_ntlmssp_state)->server_info = NULL;
 
                if (sess_vuid == -1) {
                        nt_status = NT_STATUS_LOGON_FAILURE;
                } else {
                        
+                       /* current_user_info is changed on new vuid */
+                       reload_services( True );
+
                        set_message(outbuf,4,0,True);
                        SSVAL(outbuf, smb_vwv3, 0);
                        
@@ -315,14 +393,15 @@ static BOOL reply_spnego_ntlmssp(connection_struct *conn, char *inbuf, char *out
                        
                        SSVAL(outbuf,smb_uid,sess_vuid);
 
-                       if (!server_info->guest) {
+                       if (!server_info->guest && !srv_signing_started()) {
                                /* We need to start the signing engine
                                 * here but a W2K client sends the old
                                 * "BSRSPYL " signature instead of the
                                 * correct one. Subsequent packets will
                                 * be correct.
                                 */
-                               srv_check_sign_mac(inbuf);
+
+                               srv_check_sign_mac(inbuf, False);
                        }
                }
        }
@@ -355,7 +434,9 @@ static int reply_spnego_negotiate(connection_struct *conn,
        DATA_BLOB secblob;
        int i;
        DATA_BLOB chal;
-       BOOL got_kerberos = False;
+#ifdef HAVE_KRB5
+       BOOL got_kerberos_mechanism = False;
+#endif
        NTSTATUS nt_status;
 
        /* parse out the OIDs and the first sec blob */
@@ -372,11 +453,13 @@ static int reply_spnego_negotiate(connection_struct *conn,
           server sent back krb5/mskrb5/ntlmssp as mechtypes, but the 
           client (2ksp3) replied with ntlmssp/mskrb5/krb5 and an 
           NTLMSSP mechtoken.                 --jerry              */
-       
+
+#ifdef HAVE_KRB5       
        if (strcmp(OID_KERBEROS5, OIDs[0]) == 0 ||
            strcmp(OID_KERBEROS5_OLD, OIDs[0]) == 0) {
-               got_kerberos = True;
+               got_kerberos_mechanism = True;
        }
+#endif
                
        for (i=0;OIDs[i];i++) {
                DEBUG(3,("Got OID %s\n", OIDs[i]));
@@ -385,7 +468,7 @@ static int reply_spnego_negotiate(connection_struct *conn,
        DEBUG(3,("Got secblob of size %lu\n", (unsigned long)secblob.length));
 
 #ifdef HAVE_KRB5
-       if (got_kerberos && (SEC_ADS == lp_security())) {
+       if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) || lp_use_kerberos_keytab()) ) {
                int ret = reply_spnego_kerberos(conn, inbuf, outbuf, 
                                                length, bufsize, &secblob);
                data_blob_free(&secblob);
@@ -465,7 +548,7 @@ static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,
        DATA_BLOB blob1;
        int ret;
        size_t bufrem;
-       fstring native_os, native_lanman;
+       fstring native_os, native_lanman, primary_domain;
        char *p2;
        uint16 data_blob_len = SVAL(inbuf, smb_vwv7);
        enum remote_arch_types ra_type = get_remote_arch();
@@ -474,6 +557,11 @@ static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,
 
        if (global_client_caps == 0) {
                global_client_caps = IVAL(inbuf,smb_vwv10);
+
+               if (!(global_client_caps & CAP_STATUS32)) {
+                       remove_from_common_flags2(FLAGS2_32_BIT_ERROR_CODES);
+               }
+
        }
                
        p = (uint8 *)smb_buf(inbuf);
@@ -494,11 +582,20 @@ static int reply_sesssetup_and_X_spnego(connection_struct *conn, char *inbuf,
        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 );
+       p2 += srvstr_pull_buf(inbuf, primary_domain, p2, sizeof(primary_domain), STR_TERMINATE);
+       DEBUG(3,("NativeOS=[%s] NativeLanMan=[%s] PrimaryDomain=[%s]\n", 
+               native_os, native_lanman, primary_domain));
 
+       if ( ra_type == RA_WIN2K ) {
+               /* Windows 2003 doesn't set the native lanman string, 
+                  but does set primary domain which is a bug I think */
+                          
+               if ( !strlen(native_lanman) )
+                       ra_lanman_string( primary_domain );
+               else
+                       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);
@@ -553,10 +650,11 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        fstring domain;
        fstring native_os;
        fstring native_lanman;
+       fstring primary_domain;
        static BOOL done_sesssetup = False;
        extern BOOL global_encrypted_passwords_negotiated;
        extern BOOL global_spnego_negotiated;
-       extern int Protocol;
+       extern enum protocol_types Protocol;
        extern int max_send;
 
        auth_usersupplied_info *user_info = NULL;
@@ -566,6 +664,8 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
        NTSTATUS nt_status;
 
        BOOL doencrypt = global_encrypted_passwords_negotiated;
+
+       DATA_BLOB session_key;
        
        START_PROFILE(SMBsesssetupX);
 
@@ -594,6 +694,10 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
 
        if (Protocol < PROTOCOL_NT1) {
                uint16 passlen1 = SVAL(inbuf,smb_vwv7);
+
+               /* Never do NT status codes with protocols before NT1 as we don't get client caps. */
+               remove_from_common_flags2(FLAGS2_32_BIT_ERROR_CODES);
+
                if ((passlen1 > MAX_PASS_LEN) || (passlen1 > smb_bufrem(inbuf, smb_buf(inbuf)))) {
                        return ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
@@ -614,18 +718,26 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                uint16 passlen2 = SVAL(inbuf,smb_vwv8);
                enum remote_arch_types ra_type = get_remote_arch();
                char *p = smb_buf(inbuf);    
+               char *save_p = smb_buf(inbuf);
+               uint16 byte_count;
+                       
 
-               if(global_client_caps == 0)
+               if(global_client_caps == 0) {
                        global_client_caps = IVAL(inbuf,smb_vwv11);
                
-               /* client_caps is used as final determination if client is NT or Win95. 
-                  This is needed to return the correct error codes in some
-                  circumstances.
-               */
+                       if (!(global_client_caps & CAP_STATUS32)) {
+                               remove_from_common_flags2(FLAGS2_32_BIT_ERROR_CODES);
+                       }
+
+                       /* client_caps is used as final determination if client is NT or Win95. 
+                          This is needed to return the correct error codes in some
+                          circumstances.
+                       */
                
-               if(ra_type == RA_WINNT || ra_type == RA_WIN2K || ra_type == RA_WIN95) {
-                       if(!(global_client_caps & (CAP_NT_SMBS | CAP_STATUS32))) {
-                               set_remote_arch( RA_WIN95);
+                       if(ra_type == RA_WINNT || ra_type == RA_WIN2K || ra_type == RA_WIN95) {
+                               if(!(global_client_caps & (CAP_NT_SMBS | CAP_STATUS32))) {
+                                       set_remote_arch( RA_WIN95);
+                               }
                        }
                }
 
@@ -666,11 +778,25 @@ 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),  unic ? passlen2 : passlen1, 
-                                   STR_TERMINATE);
+                       BOOL unic=SVAL(inbuf, smb_flg2) & FLAGS2_UNICODE_STRINGS;
+
+#if 0
+                       /* This was the previous fix. Not sure if it's still valid. JRA. */
+                       if ((ra_type == RA_WINNT) && (passlen2 == 0) && unic && passlen1) {
+                               /* NT4.0 stuffs up plaintext unicode password lengths... */
+                               srvstr_pull(inbuf, pass, smb_buf(inbuf) + 1,
+                                       sizeof(pass), passlen1, STR_TERMINATE);
+#endif
+
+                       if (unic && (passlen2 == 0) && passlen1) {
+                               /* Only a ascii plaintext password was sent. */
+                               srvstr_pull(inbuf, pass, smb_buf(inbuf), sizeof(pass),
+                                       passlen1, STR_TERMINATE|STR_ASCII);
+                       } else {
+                               srvstr_pull(inbuf, pass, smb_buf(inbuf), 
+                                       sizeof(pass),  unic ? passlen2 : passlen1, 
+                                       STR_TERMINATE);
+                       }
                        plaintext_password = data_blob(pass, strlen(pass)+1);
                }
                
@@ -679,14 +805,31 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
                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 );
+               /* not documented or decoded by Ethereal but there is one more string 
+                  in the extra bytes which is the same as the PrimaryDomain when using 
+                  extended security.  Windows NT 4 and 2003 use this string to store 
+                  the native lanman string. Windows 9x does not include a string here 
+                  at all so we have to check if we have any extra bytes left */
+               
+               byte_count = SVAL(inbuf, smb_vwv13);
+               if ( PTR_DIFF(p, save_p) < byte_count)
+                       p += srvstr_pull_buf(inbuf, primary_domain, p, sizeof(primary_domain), STR_TERMINATE);
+               else 
+                       fstrcpy( primary_domain, "null" );
+
+               DEBUG(3,("Domain=[%s]  NativeOS=[%s] NativeLanMan=[%s] PrimaryDomain=[%s]\n",
+                        domain, native_os, native_lanman, primary_domain));
+
+               if ( ra_type == RA_WIN2K ) {
+                       if ( strlen(native_lanman) == 0 )
+                               ra_lanman_string( primary_domain );
+                       else
+                               ra_lanman_string( native_lanman );
+               }
 
        }
-       
+
        if (SVAL(inbuf,smb_vwv4) == 0) {
                setup_new_vc_session();
        }
@@ -766,18 +909,25 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
 
        free_user_info(&user_info);
        
-       data_blob_free(&lm_resp);
-       data_blob_clear_free(&plaintext_password);
-       
        if (!NT_STATUS_IS_OK(nt_status)) {
                nt_status = do_map_to_guest(nt_status, &server_info, user, domain);
        }
        
        if (!NT_STATUS_IS_OK(nt_status)) {
                data_blob_free(&nt_resp);
+               data_blob_free(&lm_resp);
+               data_blob_clear_free(&plaintext_password);
                return ERROR_NT(nt_status_squash(nt_status));
        }
 
+       if (server_info->user_session_key.data) {
+               session_key = data_blob(server_info->user_session_key.data, server_info->user_session_key.length);
+       } else {
+               session_key = data_blob(NULL, 0);
+       }
+
+       data_blob_clear_free(&plaintext_password);
+       
        /* it's ok - setup a reply */
        set_message(outbuf,3,0,True);
        if (Protocol >= PROTOCOL_NT1) {
@@ -795,14 +945,18 @@ int reply_sesssetup_and_X(connection_struct *conn, char *inbuf,char *outbuf,
           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, nt_resp, sub_user);
+       sess_vuid = register_vuid(server_info, session_key, nt_resp.data ? nt_resp : lm_resp, sub_user);
        data_blob_free(&nt_resp);
+       data_blob_free(&lm_resp);
 
        if (sess_vuid == -1) {
                return ERROR_NT(NT_STATUS_LOGON_FAILURE);
        }
 
-       if (!server_info->guest && !srv_check_sign_mac(inbuf)) {
+       /* current_user_info is changed on new vuid */
+       reload_services( True );
+
+       if (!server_info->guest && !srv_signing_started() && !srv_check_sign_mac(inbuf, True)) {
                exit_server("reply_sesssetup_and_X: bad smb signature");
        }