smbd: fix session setup with security = share.
[samba.git] / source / smbd / sesssetup.c
index 35efbc009f94f5e4b16c64ec6a135a1b5000b88d..cf90c7183a2f740b105f6f30b12563f7f4a03033 100644 (file)
@@ -118,8 +118,7 @@ static void sessionsetup_start_signing_engine(
  Send a security blob via a session setup reply.
 ****************************************************************************/
 
-static void reply_sesssetup_blob(connection_struct *conn,
-                                struct smb_request *req,
+static void reply_sesssetup_blob(struct smb_request *req,
                                 DATA_BLOB blob,
                                 NTSTATUS nt_status)
 {
@@ -139,7 +138,7 @@ static void reply_sesssetup_blob(connection_struct *conn,
        }
 
        show_msg((char *)req->outbuf);
-       send_smb(smbd_server_fd(),(char *)req->outbuf);
+       srv_send_smb(smbd_server_fd(),(char *)req->outbuf,req->encrypted);
        TALLOC_FREE(req->outbuf);
 }
 
@@ -247,8 +246,7 @@ static bool make_krb5_skew_error(DATA_BLOB *pblob_out)
  Reply to a session setup spnego negotiate packet for kerberos.
 ****************************************************************************/
 
-static void reply_spnego_kerberos(connection_struct *conn,
-                                 struct smb_request *req,
+static void reply_spnego_kerberos(struct smb_request *req,
                                  DATA_BLOB *secblob,
                                  uint16 vuid,
                                  bool *p_invalidate_vuid)
@@ -261,7 +259,7 @@ static void reply_spnego_kerberos(connection_struct *conn,
        fstring user;
        int sess_vuid = req->vuid;
        NTSTATUS ret = NT_STATUS_OK;
-       PAC_DATA *pac_data;
+       struct PAC_DATA *pac_data = NULL;
        DATA_BLOB ap_rep, ap_rep_wrapped, response;
        auth_serversupplied_info *server_info = NULL;
        DATA_BLOB session_key = data_blob_null;
@@ -270,10 +268,9 @@ static void reply_spnego_kerberos(connection_struct *conn,
        fstring real_username;
        bool map_domainuser_to_guest = False;
        bool username_was_mapped;
-       PAC_LOGON_INFO *logon_info = NULL;
+       struct PAC_LOGON_INFO *logon_info = NULL;
 
        ZERO_STRUCT(ticket);
-       ZERO_STRUCT(pac_data);
        ZERO_STRUCT(ap_rep);
        ZERO_STRUCT(ap_rep_wrapped);
        ZERO_STRUCT(response);
@@ -397,10 +394,9 @@ static void reply_spnego_kerberos(connection_struct *conn,
 
        domain = p+1;
 
-       if (logon_info && logon_info->info3.hdr_logon_dom.uni_str_len) {
-               unistr2_to_ascii(netbios_domain_name,
-                               &logon_info->info3.uni_logon_dom,
-                               sizeof(netbios_domain_name));
+       if (logon_info && logon_info->info3.base.domain.string) {
+               fstrcpy(netbios_domain_name,
+                       logon_info->info3.base.domain.string);
                domain = netbios_domain_name;
                DEBUG(10, ("Mapped to [%s] (using PAC)\n", domain));
 
@@ -413,30 +409,24 @@ static void reply_spnego_kerberos(connection_struct *conn,
                   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);
+               wbcErr wbc_status;
+               struct wbcDomainInfo *info = NULL;
 
                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);
+               wbc_status = wbcDomainInfo(domain, &info);
 
-               if (wb_result == NSS_STATUS_SUCCESS) {
+               if (WBC_ERROR_IS_OK(wbc_status)) {
 
                        fstrcpy(netbios_domain_name,
-                               wb_response.data.domain_info.name);
-                       domain = netbios_domain_name;
+                               info->short_name);
 
+                       wbcFreeMemory(info);
+                       domain = netbios_domain_name;
                        DEBUG(10, ("Mapped to [%s] (using Winbind)\n", domain));
                } else {
-                       DEBUG(3, ("Could not find short name -- winbind "
-                                 "not running?\n"));
+                       DEBUG(3, ("Could not find short name: %s\n",
+                               wbcErrorString(wbc_status)));
                }
        }
 
@@ -539,7 +529,9 @@ static void reply_spnego_kerberos(connection_struct *conn,
                }
        }
 
-       server_info->was_mapped |= username_was_mapped;
+       if (username_was_mapped) {
+               server_info->was_mapped = username_was_mapped;
+       }
 
        /* we need to build the token for the user. make_server_info_guest()
           already does this */
@@ -547,6 +539,8 @@ static void reply_spnego_kerberos(connection_struct *conn,
        if ( !server_info->ptok ) {
                ret = create_local_token( server_info );
                if ( !NT_STATUS_IS_OK(ret) ) {
+                       DEBUG(10,("failed to create local token: %s\n",
+                               nt_errstr(ret)));
                        SAFE_FREE(client);
                        data_blob_free(&ap_rep);
                        data_blob_free(&session_key);
@@ -605,7 +599,7 @@ static void reply_spnego_kerberos(connection_struct *conn,
        }
        response = spnego_gen_auth_response(&ap_rep_wrapped, ret,
                        OID_KERBEROS5_OLD);
-       reply_sesssetup_blob(conn, req, response, ret);
+       reply_sesssetup_blob(req, response, ret);
 
        data_blob_free(&ap_rep);
        data_blob_free(&ap_rep_wrapped);
@@ -623,11 +617,11 @@ static void reply_spnego_kerberos(connection_struct *conn,
  leg of the NTLM auth steps.
 ***************************************************************************/
 
-static void reply_spnego_ntlmssp(connection_struct *conn,
-                                struct smb_request *req,
+static void reply_spnego_ntlmssp(struct smb_request *req,
                                 uint16 vuid,
                                 AUTH_NTLMSSP_STATE **auth_ntlmssp_state,
                                 DATA_BLOB *ntlmssp_blob, NTSTATUS nt_status,
+                                const char *OID,
                                 bool wrap)
 {
        DATA_BLOB response;
@@ -688,12 +682,12 @@ static void reply_spnego_ntlmssp(connection_struct *conn,
 
        if (wrap) {
                response = spnego_gen_auth_response(ntlmssp_blob,
-                               nt_status, OID_NTLMSSP);
+                               nt_status, OID);
        } else {
                response = *ntlmssp_blob;
        }
 
-       reply_sesssetup_blob(conn, req, response, nt_status);
+       reply_sesssetup_blob(req, response, nt_status);
        if (wrap) {
                data_blob_free(&response);
        }
@@ -752,12 +746,33 @@ NTSTATUS parse_spnego_mechanisms(DATA_BLOB blob_in, DATA_BLOB *pblob_out,
        return NT_STATUS_OK;
 }
 
+/****************************************************************************
+ Fall back from krb5 to NTLMSSP.
+****************************************************************************/
+
+static void reply_spnego_downgrade_to_ntlmssp(struct smb_request *req,
+                                               uint16 vuid)
+{
+       DATA_BLOB response;
+
+       reply_outbuf(req, 4, 0);
+        SSVAL(req->outbuf,smb_uid,vuid);
+
+       DEBUG(3,("reply_spnego_downgrade_to_ntlmssp: Got krb5 ticket in SPNEGO "
+               "but set to downgrade to NTLMSSP\n"));
+
+       response = spnego_gen_auth_response(NULL,
+                       NT_STATUS_MORE_PROCESSING_REQUIRED,
+                       OID_NTLMSSP);
+       reply_sesssetup_blob(req, response, NT_STATUS_MORE_PROCESSING_REQUIRED);
+       data_blob_free(&response);
+}
+
 /****************************************************************************
  Reply to a session setup spnego negotiate packet.
 ****************************************************************************/
 
-static void reply_spnego_negotiate(connection_struct *conn,
-                                  struct smb_request *req,
+static void reply_spnego_negotiate(struct smb_request *req,
                                   uint16 vuid,
                                   DATA_BLOB blob1,
                                   AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
@@ -783,7 +798,7 @@ static void reply_spnego_negotiate(connection_struct *conn,
        if ( got_kerberos_mechanism && ((lp_security()==SEC_ADS) ||
                                lp_use_kerberos_keytab()) ) {
                bool destroy_vuid = True;
-               reply_spnego_kerberos(conn, req, &secblob, vuid,
+               reply_spnego_kerberos(req, &secblob, vuid,
                                      &destroy_vuid);
                data_blob_free(&secblob);
                if (destroy_vuid) {
@@ -798,6 +813,15 @@ static void reply_spnego_negotiate(connection_struct *conn,
                auth_ntlmssp_end(auth_ntlmssp_state);
        }
 
+       if (got_kerberos_mechanism) {
+               data_blob_free(&secblob);
+               /* The mechtoken is a krb5 ticket, but
+                * we need to fall back to NTLM. */
+               reply_spnego_downgrade_to_ntlmssp(req,
+                                       vuid);
+               return;
+       }
+
        status = auth_ntlmssp_start(auth_ntlmssp_state);
        if (!NT_STATUS_IS_OK(status)) {
                /* Kill the intermediate vuid */
@@ -811,8 +835,8 @@ static void reply_spnego_negotiate(connection_struct *conn,
 
        data_blob_free(&secblob);
 
-       reply_spnego_ntlmssp(conn, req, vuid, auth_ntlmssp_state,
-                            &chal, status, True);
+       reply_spnego_ntlmssp(req, vuid, auth_ntlmssp_state,
+                            &chal, status, OID_NTLMSSP, true);
 
        data_blob_free(&chal);
 
@@ -824,8 +848,7 @@ static void reply_spnego_negotiate(connection_struct *conn,
  Reply to a session setup spnego auth packet.
 ****************************************************************************/
 
-static void reply_spnego_auth(connection_struct *conn,
-                             struct smb_request *req,
+static void reply_spnego_auth(struct smb_request *req,
                              uint16 vuid,
                              DATA_BLOB blob1,
                              AUTH_NTLMSSP_STATE **auth_ntlmssp_state)
@@ -833,7 +856,7 @@ static void reply_spnego_auth(connection_struct *conn,
        DATA_BLOB auth = data_blob_null;
        DATA_BLOB auth_reply = data_blob_null;
        DATA_BLOB secblob = data_blob_null;
-       NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
+       NTSTATUS status = NT_STATUS_LOGON_FAILURE;
 
        if (!spnego_parse_auth(blob1, &auth)) {
 #if 0
@@ -843,7 +866,7 @@ static void reply_spnego_auth(connection_struct *conn,
                invalidate_vuid(vuid);
 
                reply_nterror(req, nt_status_squash(
-                                     NT_STATUS_INVALID_PARAMETER));
+                                     NT_STATUS_LOGON_FAILURE));
                return;
        }
 
@@ -853,24 +876,43 @@ static void reply_spnego_auth(connection_struct *conn,
                bool got_krb5_mechanism = False;
                status = parse_spnego_mechanisms(auth, &secblob,
                                &got_krb5_mechanism);
-               if (NT_STATUS_IS_OK(status)) {
-                       DEBUG(3,("reply_spnego_auth: Got secblob of size %lu\n",
-                                       (unsigned long)secblob.length));
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       /* Kill the intermediate vuid */
+                       invalidate_vuid(vuid);
+                       reply_nterror(req, nt_status_squash(status));
+                       return;
+               }
+
+               DEBUG(3,("reply_spnego_auth: Got secblob of size %lu\n",
+                               (unsigned long)secblob.length));
 #ifdef HAVE_KRB5
-                       if ( got_krb5_mechanism && ((lp_security()==SEC_ADS) ||
-                                               lp_use_kerberos_keytab()) ) {
-                               bool destroy_vuid = True;
-                               reply_spnego_kerberos(conn, req, &secblob,
-                                                     vuid, &destroy_vuid);
-                               data_blob_free(&secblob);
-                               data_blob_free(&auth);
-                               if (destroy_vuid) {
-                                       /* Kill the intermediate vuid */
-                                       invalidate_vuid(vuid);
-                               }
-                               return;
+               if ( got_krb5_mechanism && ((lp_security()==SEC_ADS) ||
+                                       lp_use_kerberos_keytab()) ) {
+                       bool destroy_vuid = True;
+                       reply_spnego_kerberos(req, &secblob,
+                                             vuid, &destroy_vuid);
+                       data_blob_free(&secblob);
+                       data_blob_free(&auth);
+                       if (destroy_vuid) {
+                               /* Kill the intermediate vuid */
+                               invalidate_vuid(vuid);
                        }
+                       return;
+               }
 #endif
+               /* Can't blunder into NTLMSSP auth if we have
+                * a krb5 ticket. */
+
+               if (got_krb5_mechanism) {
+                       /* Kill the intermediate vuid */
+                       invalidate_vuid(vuid);
+                       DEBUG(3,("reply_spnego_auth: network "
+                               "misconfiguration, client sent us a "
+                               "krb5 ticket and kerberos security "
+                               "not enabled"));
+                       reply_nterror(req, nt_status_squash(
+                                       NT_STATUS_LOGON_FAILURE));
                }
        }
 
@@ -878,13 +920,13 @@ static void reply_spnego_auth(connection_struct *conn,
        data_blob_free(&secblob);
 
        if (!*auth_ntlmssp_state) {
-               /* Kill the intermediate vuid */
-               invalidate_vuid(vuid);
-
-               /* auth before negotiatiate? */
-               reply_nterror(req, nt_status_squash(
-                                     NT_STATUS_INVALID_PARAMETER));
-               return;
+               status = auth_ntlmssp_start(auth_ntlmssp_state);
+               if (!NT_STATUS_IS_OK(status)) {
+                       /* Kill the intermediate vuid */
+                       invalidate_vuid(vuid);
+                       reply_nterror(req, nt_status_squash(status));
+                       return;
+               }
        }
 
        status = auth_ntlmssp_update(*auth_ntlmssp_state,
@@ -892,9 +934,11 @@ static void reply_spnego_auth(connection_struct *conn,
 
        data_blob_free(&auth);
 
-       reply_spnego_ntlmssp(conn, req, vuid,
+       /* Don't send the mechid as we've already sent this (RFC4178). */
+
+       reply_spnego_ntlmssp(req, vuid,
                             auth_ntlmssp_state,
-                            &auth_reply, status, True);
+                            &auth_reply, status, NULL, true);
 
        data_blob_free(&auth_reply);
 
@@ -1104,8 +1148,7 @@ static NTSTATUS check_spnego_blob_complete(uint16 smbpid, uint16 vuid,
  conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
-static void reply_sesssetup_and_X_spnego(connection_struct *conn,
-                                        struct smb_request *req)
+static void reply_sesssetup_and_X_spnego(struct smb_request *req)
 {
        uint8 *p;
        DATA_BLOB blob1;
@@ -1225,7 +1268,7 @@ static void reply_sesssetup_and_X_spnego(connection_struct *conn,
 
                /* its a negTokenTarg packet */
 
-               reply_spnego_negotiate(conn, req, vuid, blob1,
+               reply_spnego_negotiate(req, vuid, blob1,
                                       &vuser->auth_ntlmssp_state);
                data_blob_free(&blob1);
                return;
@@ -1235,7 +1278,7 @@ static void reply_sesssetup_and_X_spnego(connection_struct *conn,
 
                /* its a auth packet */
 
-               reply_spnego_auth(conn, req, vuid, blob1,
+               reply_spnego_auth(req, vuid, blob1,
                                  &vuser->auth_ntlmssp_state);
                data_blob_free(&blob1);
                return;
@@ -1260,9 +1303,9 @@ static void reply_sesssetup_and_X_spnego(connection_struct *conn,
 
                data_blob_free(&blob1);
 
-               reply_spnego_ntlmssp(conn, req, vuid,
+               reply_spnego_ntlmssp(req, vuid,
                                     &vuser->auth_ntlmssp_state,
-                                    &chal, status, False);
+                                    &chal, status, OID_NTLMSSP, false);
                data_blob_free(&chal);
                return;
        }
@@ -1317,7 +1360,8 @@ static void setup_new_vc_session(void)
 #endif
        if (lp_reset_on_zero_vc()) {
                connections_forall(shutdown_other_smbds,
-                               CONST_DISCARD(void *,client_addr(addr)));
+                       CONST_DISCARD(void *,
+                       client_addr(get_client_fd(),addr,sizeof(addr))));
        }
 }
 
@@ -1325,7 +1369,7 @@ static void setup_new_vc_session(void)
  Reply to a session setup command.
 ****************************************************************************/
 
-void reply_sesssetup_and_X(connection_struct *conn, struct smb_request *req)
+void reply_sesssetup_and_X(struct smb_request *req)
 {
        int sess_vuid;
        int smb_bufsize;
@@ -1376,7 +1420,7 @@ void reply_sesssetup_and_X(connection_struct *conn, struct smb_request *req)
                        setup_new_vc_session();
                }
 
-               reply_sesssetup_and_X_spnego(conn, req);
+               reply_sesssetup_and_X_spnego(req);
                END_PROFILE(SMBsesssetupX);
                return;
        }
@@ -1489,31 +1533,33 @@ void reply_sesssetup_and_X(connection_struct *conn, struct smb_request *req)
                if (doencrypt) {
                        lm_resp = data_blob(p, passlen1);
                        nt_resp = data_blob(p+passlen1, passlen2);
-               } else {
-                       pstring pass;
+               } else if (lp_security() != SEC_SHARE) {
+                       char *pass = NULL;
                        bool unic= smb_flag2 & 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(req->inbuf, req->flags2, pass,
-                                           smb_buf(req->inbuf), sizeof(pass),
-                                           passlen1, STR_TERMINATE|STR_ASCII);
+                               (void)srvstr_pull_talloc(talloc_tos(),
+                                                       req->inbuf,
+                                                       req->flags2,
+                                                       &pass,
+                                                       smb_buf(req->inbuf),
+                                                       passlen1,
+                                                       STR_TERMINATE|STR_ASCII);
                        } else {
-                               srvstr_pull(req->inbuf, req->flags2, pass,
-                                           smb_buf(req->inbuf), sizeof(pass),
-                                           unic ? passlen2 : passlen1,
-                                           STR_TERMINATE);
+                               (void)srvstr_pull_talloc(talloc_tos(),
+                                                       req->inbuf,
+                                                       req->flags2,
+                                                       &pass,
+                                                       smb_buf(req->inbuf),
+                                                       unic ? passlen2 : passlen1,
+                                                       STR_TERMINATE);
+                       }
+                       if (!pass) {
+                               reply_nterror(req, nt_status_squash(
+                                             NT_STATUS_INVALID_PARAMETER));
+                               END_PROFILE(SMBsesssetupX);
+                               return;
                        }
                        plaintext_password = data_blob(pass, strlen(pass)+1);
                }