Ignore:
Timestamp:
Nov 24, 2016, 1:14:11 PM (9 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: update vendor to version 4.4.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/current/source3/smbd/reply.c

    r860 r988  
    3333#include "rpc_client/rpc_client.h"
    3434#include "../librpc/gen_ndr/ndr_spoolss_c.h"
     35#include "../librpc/gen_ndr/open_files.h"
    3536#include "rpc_client/cli_spoolss.h"
    3637#include "rpc_client/init_spoolss.h"
     
    4041#include "auth.h"
    4142#include "smbprofile.h"
     43#include "../lib/tsocket/tsocket.h"
     44#include "lib/tevent_wait.h"
     45#include "libcli/smb/smb_signing.h"
     46#include "lib/util/sys_rw_data.h"
    4247
    4348/****************************************************************************
     
    249254/****************************************************************************
    250255 Pull a string and check the path allowing a wilcard - provide for error return.
     256 Passes in posix flag.
    251257****************************************************************************/
    252258
    253 size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
     259static size_t srvstr_get_path_wcard_internal(TALLOC_CTX *ctx,
    254260                        const char *base_ptr,
    255                         uint16 smb_flags2,
     261                        uint16_t smb_flags2,
    256262                        char **pp_dest,
    257263                        const char *src,
    258264                        size_t src_len,
    259265                        int flags,
     266                        bool posix_pathnames,
    260267                        NTSTATUS *err,
    261268                        bool *contains_wcard)
     
    284291        }
    285292
    286         if (lp_posix_pathnames()) {
     293        if (posix_pathnames) {
    287294                *err = check_path_syntax_posix(*pp_dest);
    288295        } else {
     
    294301
    295302/****************************************************************************
     303 Pull a string and check the path allowing a wilcard - provide for error return.
     304****************************************************************************/
     305
     306size_t srvstr_get_path_wcard(TALLOC_CTX *ctx,
     307                        const char *base_ptr,
     308                        uint16_t smb_flags2,
     309                        char **pp_dest,
     310                        const char *src,
     311                        size_t src_len,
     312                        int flags,
     313                        NTSTATUS *err,
     314                        bool *contains_wcard)
     315{
     316        return srvstr_get_path_wcard_internal(ctx,
     317                        base_ptr,
     318                        smb_flags2,
     319                        pp_dest,
     320                        src,
     321                        src_len,
     322                        flags,
     323                        false,
     324                        err,
     325                        contains_wcard);
     326}
     327
     328/****************************************************************************
     329 Pull a string and check the path allowing a wilcard - provide for error return.
     330 posix_pathnames version.
     331****************************************************************************/
     332
     333size_t srvstr_get_path_wcard_posix(TALLOC_CTX *ctx,
     334                        const char *base_ptr,
     335                        uint16_t smb_flags2,
     336                        char **pp_dest,
     337                        const char *src,
     338                        size_t src_len,
     339                        int flags,
     340                        NTSTATUS *err,
     341                        bool *contains_wcard)
     342{
     343        return srvstr_get_path_wcard_internal(ctx,
     344                        base_ptr,
     345                        smb_flags2,
     346                        pp_dest,
     347                        src,
     348                        src_len,
     349                        flags,
     350                        true,
     351                        err,
     352                        contains_wcard);
     353}
     354
     355/****************************************************************************
    296356 Pull a string and check the path - provide for error return.
    297357****************************************************************************/
     
    299359size_t srvstr_get_path(TALLOC_CTX *ctx,
    300360                        const char *base_ptr,
    301                         uint16 smb_flags2,
     361                        uint16_t smb_flags2,
    302362                        char **pp_dest,
    303363                        const char *src,
     
    307367{
    308368        bool ignore;
    309         return srvstr_get_path_wcard(ctx, base_ptr, smb_flags2, pp_dest, src,
    310                                      src_len, flags, err, &ignore);
    311 }
     369        return srvstr_get_path_wcard_internal(ctx,
     370                        base_ptr,
     371                        smb_flags2,
     372                        pp_dest,
     373                        src,
     374                        src_len,
     375                        flags,
     376                        false,
     377                        err,
     378                        &ignore);
     379}
     380
     381/****************************************************************************
     382 Pull a string and check the path - provide for error return.
     383 posix_pathnames version.
     384****************************************************************************/
     385
     386size_t srvstr_get_path_posix(TALLOC_CTX *ctx,
     387                        const char *base_ptr,
     388                        uint16_t smb_flags2,
     389                        char **pp_dest,
     390                        const char *src,
     391                        size_t src_len,
     392                        int flags,
     393                        NTSTATUS *err)
     394{
     395        bool ignore;
     396        return srvstr_get_path_wcard_internal(ctx,
     397                        base_ptr,
     398                        smb_flags2,
     399                        pp_dest,
     400                        src,
     401                        src_len,
     402                        flags,
     403                        true,
     404                        err,
     405                        &ignore);
     406}
     407
    312408
    313409size_t srvstr_get_path_req_wcard(TALLOC_CTX *mem_ctx, struct smb_request *req,
     
    315411                                 NTSTATUS *err, bool *contains_wcard)
    316412{
    317         return srvstr_get_path_wcard(mem_ctx, (char *)req->inbuf, req->flags2,
    318                                      pp_dest, src, smbreq_bufrem(req, src),
    319                                      flags, err, contains_wcard);
     413        ssize_t bufrem = smbreq_bufrem(req, src);
     414
     415        if (bufrem < 0) {
     416                *err = NT_STATUS_INVALID_PARAMETER;
     417                return 0;
     418        }
     419
     420        if (req->posix_pathnames) {
     421                return srvstr_get_path_wcard_internal(mem_ctx,
     422                                (const char *)req->inbuf,
     423                                req->flags2,
     424                                pp_dest,
     425                                src,
     426                                bufrem,
     427                                flags,
     428                                true,
     429                                err,
     430                                contains_wcard);
     431        } else {
     432                return srvstr_get_path_wcard_internal(mem_ctx,
     433                                (const char *)req->inbuf,
     434                                req->flags2,
     435                                pp_dest,
     436                                src,
     437                                bufrem,
     438                                flags,
     439                                false,
     440                                err,
     441                                contains_wcard);
     442        }
    320443}
    321444
     
    327450        return srvstr_get_path_req_wcard(mem_ctx, req, pp_dest, src,
    328451                                         flags, err, &ignore);
     452}
     453
     454/**
     455 * pull a string from the smb_buf part of a packet. In this case the
     456 * string can either be null terminated or it can be terminated by the
     457 * end of the smbbuf area
     458 */
     459size_t srvstr_pull_req_talloc(TALLOC_CTX *ctx, struct smb_request *req,
     460                              char **dest, const uint8_t *src, int flags)
     461{
     462        ssize_t bufrem = smbreq_bufrem(req, src);
     463
     464        if (bufrem < 0) {
     465                return 0;
     466        }
     467
     468        return pull_string_talloc(ctx, req->inbuf, req->flags2, dest, src,
     469                                  bufrem, flags);
    329470}
    330471
     
    400541}
    401542
    402 static bool netbios_session_retarget(struct smbd_server_connection *sconn,
     543static bool netbios_session_retarget(struct smbXsrv_connection *xconn,
    403544                                     const char *name, int name_type)
    404545{
     
    409550        char *p;
    410551        int retarget_type = 0x20;
    411         int retarget_port = 139;
     552        int retarget_port = NBT_SMB_PORT;
    412553        struct sockaddr_storage retarget_addr;
    413554        struct sockaddr_in *in_addr;
     
    415556        uint8_t outbuf[10];
    416557
    417         if (get_socket_port(sconn->sock) != 139) {
     558        if (get_socket_port(xconn->transport.sock) != NBT_SMB_PORT) {
    418559                return false;
    419560        }
     
    480621        *(uint16_t *)(outbuf+8) = htons(retarget_port);
    481622
    482         if (!srv_send_smb(sconn, (char *)outbuf, false, 0, false,
     623        if (!srv_send_smb(xconn, (char *)outbuf, false, 0, false,
    483624                          NULL)) {
    484                 exit_server_cleanly("netbios_session_regarget: srv_send_smb "
     625                exit_server_cleanly("netbios_session_retarget: srv_send_smb "
    485626                                    "failed.");
    486627        }
     
    492633}
    493634
     635static void reply_called_name_not_present(char *outbuf)
     636{
     637        smb_setlen(outbuf, 1);
     638        SCVAL(outbuf, 0, 0x83);
     639        SCVAL(outbuf, 4, 0x82);
     640}
     641
    494642/****************************************************************************
    495643 Reply to a (netbios-level) special message.
    496644****************************************************************************/
    497645
    498 void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inbuf_size)
    499 {
     646void reply_special(struct smbXsrv_connection *xconn, char *inbuf, size_t inbuf_size)
     647{
     648        struct smbd_server_connection *sconn = xconn->client->sconn;
    500649        int msg_type = CVAL(inbuf,0);
    501650        int msg_flags = CVAL(inbuf,1);
     
    512661
    513662        switch (msg_type) {
    514         case 0x81: /* session request */
     663        case NBSSrequest: /* session request */
    515664        {
    516665                /* inbuf_size is guarenteed to be at least 4. */
     
    521670                *name1 = *name2 = 0;
    522671
    523                 if (sconn->nbt.got_session) {
     672                if (xconn->transport.nbt.got_session) {
    524673                        exit_server_cleanly("multiple session request not permitted");
    525674                }
    526675
    527                 SCVAL(outbuf,0,0x82);
     676                SCVAL(outbuf,0,NBSSpositive);
    528677                SCVAL(outbuf,3,0);
    529678
     
    532681                if (name_len1 <= 0 || name_len1 > inbuf_size - 4) {
    533682                        DEBUG(0,("Invalid name length in session request\n"));
     683                        reply_called_name_not_present(outbuf);
    534684                        break;
    535685                }
     
    537687                if (name_len2 <= 0 || name_len2 > inbuf_size - 4 - name_len1) {
    538688                        DEBUG(0,("Invalid name length in session request\n"));
     689                        reply_called_name_not_present(outbuf);
    539690                        break;
    540691                }
     
    547698                if (name_type1 == -1 || name_type2 == -1) {
    548699                        DEBUG(0,("Invalid name type in session request\n"));
     700                        reply_called_name_not_present(outbuf);
    549701                        break;
    550702                }
     
    553705                         name1, name_type1, name2, name_type2));
    554706
    555                 if (netbios_session_retarget(sconn, name1, name_type1)) {
     707                if (netbios_session_retarget(xconn, name1, name_type1)) {
    556708                        exit_server_cleanly("retargeted client");
    557709                }
     
    563715                if (strequal(name1, "*SMBSERVER     ")
    564716                    || strequal(name1, "*SMBSERV       "))  {
    565                         fstrcpy(name1, sconn->client_id.addr);
     717                        char *raddr;
     718
     719                        raddr = tsocket_address_inet_addr_string(sconn->remote_address,
     720                                                                 talloc_tos());
     721                        if (raddr == NULL) {
     722                                exit_server_cleanly("could not allocate raddr");
     723                        }
     724
     725                        fstrcpy(name1, raddr);
    566726                }
    567727
    568728                set_local_machine_name(name1, True);
    569729                set_remote_machine_name(name2, True);
     730
     731                if (is_ipaddress(sconn->remote_hostname)) {
     732                        char *p = discard_const_p(char, sconn->remote_hostname);
     733
     734                        talloc_free(p);
     735
     736                        sconn->remote_hostname = talloc_strdup(sconn,
     737                                                get_remote_machine_name());
     738                        if (sconn->remote_hostname == NULL) {
     739                                exit_server_cleanly("could not copy remote name");
     740                        }
     741                        xconn->remote_hostname = sconn->remote_hostname;
     742                }
    570743
    571744                DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
     
    576749                        /* We are being asked for a pathworks session ---
    577750                           no thanks! */
    578                         SCVAL(outbuf, 0,0x83);
     751                        reply_called_name_not_present(outbuf);
    579752                        break;
    580753                }
    581754
    582                 /* only add the client's machine name to the list
    583                    of possibly valid usernames if we are operating
    584                    in share mode security */
    585                 if (lp_security() == SEC_SHARE) {
    586                         add_session_user(sconn, get_remote_machine_name());
    587                 }
    588 
    589                 reload_services(sconn->msg_ctx, sconn->sock, True);
     755                reload_services(sconn, conn_snum_used, true);
    590756                reopen_logs();
    591757
    592                 sconn->nbt.got_session = true;
     758                xconn->transport.nbt.got_session = true;
    593759                break;
    594760        }
     
    596762        case 0x89: /* session keepalive request
    597763                      (some old clients produce this?) */
    598                 SCVAL(outbuf,0,SMBkeepalive);
     764                SCVAL(outbuf,0,NBSSkeepalive);
    599765                SCVAL(outbuf,3,0);
    600766                break;
    601767
    602         case 0x82: /* positive session response */
    603         case 0x83: /* negative session response */
    604         case 0x84: /* retarget session response */
     768        case NBSSpositive: /* positive session response */
     769        case NBSSnegative: /* negative session response */
     770        case NBSSretarget: /* retarget session response */
    605771                DEBUG(0,("Unexpected session response\n"));
    606772                break;
    607773
    608         case SMBkeepalive: /* session keepalive */
     774        case NBSSkeepalive: /* session keepalive */
    609775        default:
    610776                return;
     
    614780                    msg_type, msg_flags));
    615781
    616         srv_send_smb(sconn, outbuf, false, 0, false, NULL);
     782        srv_send_smb(xconn, outbuf, false, 0, false, NULL);
     783
     784        if (CVAL(outbuf, 0) != 0x82) {
     785                exit_server_cleanly("invalid netbios session");
     786        }
    617787        return;
    618788}
     
    632802        int pwlen=0;
    633803        NTSTATUS nt_status;
    634         const char *p;
    635         DATA_BLOB password_blob;
     804        const uint8_t *p;
     805        const char *p2;
    636806        TALLOC_CTX *ctx = talloc_tos();
    637         struct smbd_server_connection *sconn = req->sconn;
     807        struct smbXsrv_connection *xconn = req->xconn;
     808        NTTIME now = timeval_to_nttime(&req->request_time);
    638809
    639810        START_PROFILE(SMBtcon);
     
    645816        }
    646817
    647         p = (const char *)req->buf + 1;
     818        p = req->buf + 1;
    648819        p += srvstr_pull_req_talloc(ctx, req, &service_buf, p, STR_TERMINATE);
    649820        p += 1;
     
    658829                return;
    659830        }
    660         p = strrchr_m(service_buf,'\\');
    661         if (p) {
    662                 service = p+1;
     831        p2 = strrchr_m(service_buf,'\\');
     832        if (p2) {
     833                service = p2+1;
    663834        } else {
    664835                service = service_buf;
    665836        }
    666837
    667         password_blob = data_blob(password, pwlen+1);
    668 
    669         conn = make_connection(sconn,service,password_blob,dev,
     838        conn = make_connection(req, now, service, dev,
    670839                               req->vuid,&nt_status);
    671840        req->conn = conn;
    672 
    673         data_blob_clear_free(&password_blob);
    674841
    675842        if (!conn) {
     
    680847
    681848        reply_outbuf(req, 2, 0);
    682         SSVAL(req->outbuf,smb_vwv0,sconn->smb1.negprot.max_recv);
     849        SSVAL(req->outbuf,smb_vwv0,xconn->smb1.negprot.max_recv);
    683850        SSVAL(req->outbuf,smb_vwv1,conn->cnum);
    684851        SSVAL(req->outbuf,smb_tid,conn->cnum);
     
    700867        connection_struct *conn = req->conn;
    701868        const char *service = NULL;
    702         DATA_BLOB password;
    703869        TALLOC_CTX *ctx = talloc_tos();
    704870        /* what the cleint thinks the device is */
     
    709875        int passlen;
    710876        char *path = NULL;
    711         const char *p, *q;
    712         uint16 tcon_flags;
    713         struct smbd_server_connection *sconn = req->sconn;
     877        const uint8_t *p;
     878        const char *q;
     879        uint16_t tcon_flags;
     880        struct smbXsrv_session *session = NULL;
     881        NTTIME now = timeval_to_nttime(&req->request_time);
     882        bool session_key_updated = false;
     883        uint16_t optional_support = 0;
     884        struct smbXsrv_connection *xconn = req->xconn;
    714885
    715886        START_PROFILE(SMBtconX);
     
    725896
    726897        /* we might have to close an old one */
    727         if ((tcon_flags & 0x1) && conn) {
    728                 close_cnum(conn,req->vuid);
     898        if ((tcon_flags & TCONX_FLAG_DISCONNECT_TID) && conn) {
     899                struct smbXsrv_tcon *tcon;
     900                NTSTATUS status;
     901
     902                tcon = conn->tcon;
    729903                req->conn = NULL;
    730904                conn = NULL;
     905
     906                /*
     907                 * TODO: cancel all outstanding requests on the tcon
     908                 */
     909                status = smbXsrv_tcon_disconnect(tcon, req->vuid);
     910                if (!NT_STATUS_IS_OK(status)) {
     911                        DEBUG(0, ("reply_tcon_and_X: "
     912                                  "smbXsrv_tcon_disconnect() failed: %s\n",
     913                                  nt_errstr(status)));
     914                        /*
     915                         * If we hit this case, there is something completely
     916                         * wrong, so we better disconnect the transport connection.
     917                         */
     918                        END_PROFILE(SMBtconX);
     919                        exit_server(__location__ ": smbXsrv_tcon_disconnect failed");
     920                        return;
     921                }
     922
     923                TALLOC_FREE(tcon);
    731924        }
    732925
     
    737930        }
    738931
    739         if (sconn->smb1.negprot.encrypted_passwords) {
    740                 password = data_blob_talloc(talloc_tos(), req->buf, passlen);
    741                 if (lp_security() == SEC_SHARE) {
    742                         /*
    743                          * Security = share always has a pad byte
    744                          * after the password.
    745                          */
    746                         p = (const char *)req->buf + passlen + 1;
    747                 } else {
    748                         p = (const char *)req->buf + passlen;
    749                 }
     932        if (xconn->smb1.negprot.encrypted_passwords) {
     933                p = req->buf + passlen;
    750934        } else {
    751                 password = data_blob_talloc(talloc_tos(), req->buf, passlen+1);
    752                 /* Ensure correct termination */
    753                 password.data[passlen]=0;
    754                 p = (const char *)req->buf + passlen + 1;
     935                p = req->buf + passlen + 1;
    755936        }
    756937
     
    758939
    759940        if (path == NULL) {
    760                 data_blob_clear_free(&password);
    761941                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
    762942                END_PROFILE(SMBtconX);
     
    771951                q = strchr_m(path+2,'\\');
    772952                if (!q) {
    773                         data_blob_clear_free(&password);
    774953                        reply_nterror(req, NT_STATUS_BAD_NETWORK_NAME);
    775954                        END_PROFILE(SMBtconX);
     
    786965
    787966        if (client_devicetype == NULL) {
    788                 data_blob_clear_free(&password);
    789967                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
    790968                END_PROFILE(SMBtconX);
     
    794972        DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
    795973
    796         conn = make_connection(sconn, service, password, client_devicetype,
     974        nt_status = smb1srv_session_lookup(xconn,
     975                                           req->vuid, now, &session);
     976        if (NT_STATUS_EQUAL(nt_status, NT_STATUS_USER_SESSION_DELETED)) {
     977                reply_force_doserror(req, ERRSRV, ERRbaduid);
     978                END_PROFILE(SMBtconX);
     979                return;
     980        }
     981        if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
     982                reply_nterror(req, nt_status);
     983                END_PROFILE(SMBtconX);
     984                return;
     985        }
     986        if (!NT_STATUS_IS_OK(nt_status)) {
     987                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
     988                END_PROFILE(SMBtconX);
     989                return;
     990        }
     991
     992        if (session->global->auth_session_info == NULL) {
     993                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
     994                END_PROFILE(SMBtconX);
     995                return;
     996        }
     997
     998        /*
     999         * If there is no application key defined yet
     1000         * we create one.
     1001         *
     1002         * This means we setup the application key on the
     1003         * first tcon that happens via the given session.
     1004         *
     1005         * Once the application key is defined, it does not
     1006         * change any more.
     1007         */
     1008        if (session->global->application_key.length == 0 &&
     1009            session->global->signing_key.length > 0)
     1010        {
     1011                struct smbXsrv_session *x = session;
     1012                struct auth_session_info *session_info =
     1013                        session->global->auth_session_info;
     1014                uint8_t session_key[16];
     1015
     1016                ZERO_STRUCT(session_key);
     1017                memcpy(session_key, x->global->signing_key.data,
     1018                       MIN(x->global->signing_key.length, sizeof(session_key)));
     1019
     1020                /*
     1021                 * The application key is truncated/padded to 16 bytes
     1022                 */
     1023                x->global->application_key = data_blob_talloc(x->global,
     1024                                                             session_key,
     1025                                                             sizeof(session_key));
     1026                ZERO_STRUCT(session_key);
     1027                if (x->global->application_key.data == NULL) {
     1028                        reply_nterror(req, NT_STATUS_NO_MEMORY);
     1029                        END_PROFILE(SMBtconX);
     1030                        return;
     1031                }
     1032
     1033                if (tcon_flags & TCONX_FLAG_EXTENDED_SIGNATURES) {
     1034                        smb_key_derivation(x->global->application_key.data,
     1035                                           x->global->application_key.length,
     1036                                           x->global->application_key.data);
     1037                        optional_support |= SMB_EXTENDED_SIGNATURES;
     1038                }
     1039
     1040                /*
     1041                 * Place the application key into the session_info
     1042                 */
     1043                data_blob_clear_free(&session_info->session_key);
     1044                session_info->session_key = data_blob_dup_talloc(session_info,
     1045                                                x->global->application_key);
     1046                if (session_info->session_key.data == NULL) {
     1047                        data_blob_clear_free(&x->global->application_key);
     1048                        reply_nterror(req, NT_STATUS_NO_MEMORY);
     1049                        END_PROFILE(SMBtconX);
     1050                        return;
     1051                }
     1052                session_key_updated = true;
     1053        }
     1054
     1055        conn = make_connection(req, now, service, client_devicetype,
    7971056                               req->vuid, &nt_status);
    7981057        req->conn =conn;
    7991058
    800         data_blob_clear_free(&password);
    801 
    8021059        if (!conn) {
     1060                if (session_key_updated) {
     1061                        struct smbXsrv_session *x = session;
     1062                        struct auth_session_info *session_info =
     1063                                session->global->auth_session_info;
     1064                        data_blob_clear_free(&x->global->application_key);
     1065                        data_blob_clear_free(&session_info->session_key);
     1066                }
    8031067                reply_nterror(req, nt_status);
    8041068                END_PROFILE(SMBtconX);
     
    8271091                if (tcon_flags & TCONX_FLAG_EXTENDED_RESPONSE) {
    8281092                        /* Return permissions. */
    829                         uint32 perm1 = 0;
    830                         uint32 perm2 = 0;
     1093                        uint32_t perm1 = 0;
     1094                        uint32_t perm2 = 0;
    8311095
    8321096                        reply_outbuf(req, 7, 0);
     
    8561120                /* what does setting this bit do? It is set by NT4 and
    8571121                   may affect the ability to autorun mounted cdroms */
    858                 SSVAL(req->outbuf, smb_vwv2, SMB_SUPPORT_SEARCH_BITS|
    859                       (lp_csc_policy(SNUM(conn)) << 2));
     1122                optional_support |= SMB_SUPPORT_SEARCH_BITS;
     1123                optional_support |=
     1124                        (lp_csc_policy(SNUM(conn)) << SMB_CSC_POLICY_SHIFT);
    8601125
    8611126                if (lp_msdfs_root(SNUM(conn)) && lp_host_msdfs()) {
    8621127                        DEBUG(2,("Serving %s as a Dfs root\n",
    863                                  lp_servicename(SNUM(conn)) ));
    864                         SSVAL(req->outbuf, smb_vwv2,
    865                               SMB_SHARE_IN_DFS | SVAL(req->outbuf, smb_vwv2));
    866                 }
    867         }
    868 
     1128                                 lp_servicename(ctx, SNUM(conn)) ));
     1129                        optional_support |= SMB_SHARE_IN_DFS;
     1130                }
     1131
     1132                SSVAL(req->outbuf, smb_vwv2, optional_support);
     1133        }
     1134
     1135        SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
     1136        SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
    8691137
    8701138        DEBUG(3,("tconX service=%s \n",
     
    8721140
    8731141        /* set the incoming and outgoing tid to the just created one */
    874         SSVAL(req->inbuf,smb_tid,conn->cnum);
     1142        SSVAL(discard_const_p(uint8_t, req->inbuf),smb_tid,conn->cnum);
    8751143        SSVAL(req->outbuf,smb_tid,conn->cnum);
    8761144
     
    8781146
    8791147        req->tid = conn->cnum;
    880         chain_reply(req);
    881         return;
    8821148}
    8831149
     
    8861152****************************************************************************/
    8871153
    888 void reply_unknown_new(struct smb_request *req, uint8 type)
     1154void reply_unknown_new(struct smb_request *req, uint8_t type)
    8891155{
    8901156        DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
     
    9021168{
    9031169        connection_struct *conn = req->conn;
    904         uint16 device;
    905         uint16 function;
    906         uint32 ioctl_code;
     1170        uint16_t device;
     1171        uint16_t function;
     1172        uint32_t ioctl_code;
    9071173        int replysize;
    9081174        char *p;
     
    9431209                case IOCTL_QUERY_JOB_INFO:                 
    9441210                {
     1211                        NTSTATUS status;
     1212                        size_t len = 0;
    9451213                        files_struct *fsp = file_fsp(
    9461214                                req, SVAL(req->vwv+0, 0));
     
    9511219                        }
    9521220                        /* Job number */
    953                         if (fsp->print_file) {
    954                                 SSVAL(p, 0, fsp->print_file->rap_jobid);
    955                         } else {
    956                                 SSVAL(p, 0, 0);
     1221                        SSVAL(p, 0, print_spool_rap_jobid(fsp->print_file));
     1222
     1223                        status = srvstr_push((char *)req->outbuf, req->flags2, p+2,
     1224                                    lp_netbios_name(), 15,
     1225                                    STR_TERMINATE|STR_ASCII, &len);
     1226                        if (!NT_STATUS_IS_OK(status)) {
     1227                                reply_nterror(req, status);
     1228                                END_PROFILE(SMBioctl);
     1229                                return;
    9571230                        }
    958                         srvstr_push((char *)req->outbuf, req->flags2, p+2,
    959                                     global_myname(), 15,
    960                                     STR_TERMINATE|STR_ASCII);
    9611231                        if (conn) {
    962                                 srvstr_push((char *)req->outbuf, req->flags2,
    963                                             p+18, lp_servicename(SNUM(conn)),
    964                                             13, STR_TERMINATE|STR_ASCII);
     1232                                status = srvstr_push((char *)req->outbuf, req->flags2,
     1233                                            p+18,
     1234                                            lp_servicename(talloc_tos(),
     1235                                                           SNUM(conn)),
     1236                                            13, STR_TERMINATE|STR_ASCII, &len);
     1237                                if (!NT_STATUS_IS_OK(status)) {
     1238                                        reply_nterror(req, status);
     1239                                        END_PROFILE(SMBioctl);
     1240                                        return;
     1241                                }
    9651242                        } else {
    9661243                                memset(p+18, 0, 13);
     
    10001277        char *name = NULL;
    10011278        NTSTATUS status;
     1279        uint32_t ucf_flags = (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    10021280        TALLOC_CTX *ctx = talloc_tos();
    10031281
     
    10201298                                req->flags2 & FLAGS2_DFS_PATHNAMES,
    10211299                                name,
    1022                                 0,
     1300                                ucf_flags,
    10231301                                NULL,
    10241302                                &smb_fname);
     
    10881366        char *fname = NULL;
    10891367        int mode=0;
    1090         SMB_OFF_T size=0;
     1368        off_t size=0;
    10911369        time_t mtime=0;
    10921370        const char *p;
     
    11141392                mtime = 0;
    11151393        } else {
     1394                uint32_t ucf_flags = (req->posix_pathnames ?
     1395                                UCF_POSIX_PATHNAMES : 0);
    11161396                status = filename_convert(ctx,
    11171397                                conn,
    11181398                                req->flags2 & FLAGS2_DFS_PATHNAMES,
    11191399                                fname,
    1120                                 0,
     1400                                ucf_flags,
    11211401                                NULL,
    11221402                                &smb_fname);
     
    11681448                srv_put_dos_date3((char *)req->outbuf,smb_vwv1,mtime);
    11691449        }
    1170         SIVAL(req->outbuf,smb_vwv3,(uint32)size);
     1450        SIVAL(req->outbuf,smb_vwv3,(uint32_t)size);
    11711451
    11721452        if (get_Protocol() >= PROTOCOL_NT1) {
     
    11991479        const char *p;
    12001480        NTSTATUS status;
     1481        uint32_t ucf_flags = (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    12011482        TALLOC_CTX *ctx = talloc_tos();
    12021483
     
    12211502                                req->flags2 & FLAGS2_DFS_PATHNAMES,
    12221503                                fname,
    1223                                 0,
     1504                                ucf_flags,
    12241505                                NULL,
    12251506                                &smb_fname);
     
    12471528        mtime = srv_make_unix_date3(req->vwv+1);
    12481529
    1249         ft.mtime = convert_time_t_to_timespec(mtime);
    1250         status = smb_set_file_time(conn, NULL, smb_fname, &ft, true);
    1251         if (!NT_STATUS_IS_OK(status)) {
    1252                 reply_nterror(req, status);
    1253                 goto out;
    1254         }
    1255 
    12561530        if (mode != FILE_ATTRIBUTE_NORMAL) {
    12571531                if (VALID_STAT_OF_DIR(smb_fname->st))
     
    12601534                        mode &= ~FILE_ATTRIBUTE_DIRECTORY;
    12611535
     1536                status = check_access(conn, NULL, smb_fname,
     1537                                        FILE_WRITE_ATTRIBUTES);
     1538                if (!NT_STATUS_IS_OK(status)) {
     1539                        reply_nterror(req, status);
     1540                        goto out;
     1541                }
     1542
    12621543                if (file_set_dosmode(conn, smb_fname, mode, NULL,
    12631544                                     false) != 0) {
     
    12651546                        goto out;
    12661547                }
     1548        }
     1549
     1550        ft.mtime = convert_time_t_to_timespec(mtime);
     1551        status = smb_set_file_time(conn, NULL, smb_fname, &ft, true);
     1552        if (!NT_STATUS_IS_OK(status)) {
     1553                reply_nterror(req, status);
     1554                goto out;
    12671555        }
    12681556
     
    12841572{
    12851573        connection_struct *conn = req->conn;
     1574        uint64_t ret;
    12861575        uint64_t dfree,dsize,bsize;
    12871576        START_PROFILE(SMBdskattr);
    12881577
    1289         if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (uint64_t)-1) {
     1578        ret = get_dfree_info(conn, ".", &bsize, &dfree, &dsize);
     1579        if (ret == (uint64_t)-1) {
    12901580                reply_nterror(req, map_nt_error_from_unix(errno));
    12911581                END_PROFILE(SMBdskattr);
    12921582                return;
     1583        }
     1584
     1585        /*
     1586         * Force max to fit in 16 bit fields.
     1587         */
     1588        while (dfree > WORDMAX || dsize > WORDMAX || bsize < 512) {
     1589                dfree /= 2;
     1590                dsize /= 2;
     1591                bsize *= 2;
     1592                if (bsize > (WORDMAX*512)) {
     1593                        bsize = (WORDMAX*512);
     1594                        if (dsize > WORDMAX)
     1595                                dsize = WORDMAX;
     1596                        if (dfree >  WORDMAX)
     1597                                dfree = WORDMAX;
     1598                        break;
     1599                }
    12931600        }
    12941601
     
    13621669
    13631670/****************************************************************************
     1671 Make a dir struct.
     1672****************************************************************************/
     1673
     1674static bool make_dir_struct(TALLOC_CTX *ctx,
     1675                            char *buf,
     1676                            const char *mask,
     1677                            const char *fname,
     1678                            off_t size,
     1679                            uint32_t mode,
     1680                            time_t date,
     1681                            bool uc)
     1682{
     1683        char *p;
     1684        char *mask2 = talloc_strdup(ctx, mask);
     1685
     1686        if (!mask2) {
     1687                return False;
     1688        }
     1689
     1690        if ((mode & FILE_ATTRIBUTE_DIRECTORY) != 0) {
     1691                size = 0;
     1692        }
     1693
     1694        memset(buf+1,' ',11);
     1695        if ((p = strchr_m(mask2,'.')) != NULL) {
     1696                *p = 0;
     1697                push_ascii(buf+1,mask2,8, 0);
     1698                push_ascii(buf+9,p+1,3, 0);
     1699                *p = '.';
     1700        } else {
     1701                push_ascii(buf+1,mask2,11, 0);
     1702        }
     1703
     1704        memset(buf+21,'\0',DIR_STRUCT_SIZE-21);
     1705        SCVAL(buf,21,mode);
     1706        srv_put_dos_date(buf,22,date);
     1707        SSVAL(buf,26,size & 0xFFFF);
     1708        SSVAL(buf,28,(size >> 16)&0xFFFF);
     1709        /* We only uppercase if FLAGS2_LONG_PATH_COMPONENTS is zero in the input buf.
     1710           Strange, but verified on W2K3. Needed for OS/2. JRA. */
     1711        push_ascii(buf+30,fname,12, uc ? STR_UPPER : 0);
     1712        DEBUG(8,("put name [%s] from [%s] into dir struct\n",buf+30, fname));
     1713        return True;
     1714}
     1715
     1716/****************************************************************************
    13641717 Reply to a search.
    13651718 Can be called from SMBsearch, SMBffirst or SMBfunique.
     
    13701723        connection_struct *conn = req->conn;
    13711724        char *path = NULL;
    1372         const char *mask = NULL;
     1725        char *mask = NULL;
    13731726        char *directory = NULL;
    13741727        struct smb_filename *smb_fname = NULL;
    13751728        char *fname = NULL;
    1376         SMB_OFF_T size;
    1377         uint32 mode;
     1729        off_t size;
     1730        uint32_t mode;
    13781731        struct timespec date;
    1379         uint32 dirtype;
     1732        uint32_t dirtype;
    13801733        unsigned int numentries = 0;
    13811734        unsigned int maxentries = 0;
     
    13931746        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
    13941747        struct dptr_struct *dirptr = NULL;
     1748        struct smbXsrv_connection *xconn = req->xconn;
    13951749        struct smbd_server_connection *sconn = req->sconn;
    13961750
     
    14021756        }
    14031757
    1404         if (lp_posix_pathnames()) {
     1758        if (req->posix_pathnames) {
    14051759                reply_unknown_new(req, req->cmd);
    14061760                goto out;
     
    14301784
    14311785        if (status_len == 0) {
     1786                uint32_t ucf_flags = UCF_ALWAYS_ALLOW_WCARD_LCOMP |
     1787                        (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    14321788                nt_status = filename_convert(ctx, conn,
    14331789                                             req->flags2 & FLAGS2_DFS_PATHNAMES,
    14341790                                             path,
    1435                                              UCF_ALWAYS_ALLOW_WCARD_LCOMP,
     1791                                             ucf_flags,
    14361792                                             &mask_contains_wcard,
    14371793                                             &smb_fname);
     
    14501806                p = strrchr_m(directory,'/');
    14511807                if ((p != NULL) && (*directory != '/')) {
    1452                         mask = p + 1;
     1808                        mask = talloc_strdup(ctx, p + 1);
    14531809                        directory = talloc_strndup(ctx, directory,
    14541810                                                   PTR_DIFF(p, directory));
    14551811                } else {
    1456                         mask = directory;
     1812                        mask = talloc_strdup(ctx, directory);
    14571813                        directory = talloc_strdup(ctx,".");
    14581814                }
     
    14671823
    14681824                nt_status = dptr_create(conn,
     1825                                        NULL, /* req */
    14691826                                        NULL, /* fsp */
    14701827                                        directory,
     
    15021859                }
    15031860
    1504                 mask = dptr_wcard(sconn, dptr_num);
     1861                mask = talloc_strdup(ctx, dptr_wcard(sconn, dptr_num));
    15051862                if (!mask) {
    15061863                        goto SearchEmpty;
     
    15101867                 * check from the initial saved string.
    15111868                 */
    1512                 mask_contains_wcard = ms_has_wild(mask);
     1869                if (!req->posix_pathnames) {
     1870                        mask_contains_wcard = ms_has_wild(mask);
     1871                }
    15131872                dirtype = dptr_attr(sconn, dptr_num);
    15141873        }
     
    15221881                char buf[DIR_STRUCT_SIZE];
    15231882                memcpy(buf,status,21);
    1524                 if (!make_dir_struct(ctx,buf,"???????????",volume_label(SNUM(conn)),
     1883                if (!make_dir_struct(ctx,buf,"???????????",volume_label(ctx, SNUM(conn)),
    15251884                                0,FILE_ATTRIBUTE_VOLUME,0,!allow_long_path_components)) {
    15261885                        reply_nterror(req, NT_STATUS_NO_MEMORY);
     
    15411900        } else {
    15421901                unsigned int i;
    1543                 maxentries = MIN(
    1544                         maxentries,
    1545                         ((BUFFER_SIZE -
    1546                           ((uint8 *)smb_buf(req->outbuf) + 3 - req->outbuf))
    1547                          /DIR_STRUCT_SIZE));
     1902                size_t hdr_size = ((uint8_t *)smb_buf(req->outbuf) + 3 - req->outbuf);
     1903                size_t available_space = xconn->smb1.sessions.max_send - hdr_size;
     1904
     1905                maxentries = MIN(maxentries, available_space/DIR_STRUCT_SIZE);
    15481906
    15491907                DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
    1550                         directory,lp_dontdescend(SNUM(conn))));
    1551                 if (in_list(directory, lp_dontdescend(SNUM(conn)),True)) {
     1908                         directory,lp_dont_descend(ctx, SNUM(conn))));
     1909                if (in_list(directory, lp_dont_descend(ctx, SNUM(conn)),True)) {
    15521910                        check_descend = True;
    15531911                }
     
    16422000 out:
    16432001        TALLOC_FREE(directory);
     2002        TALLOC_FREE(mask);
    16442003        TALLOC_FREE(smb_fname);
    16452004        END_PROFILE(SMBsearch);
     
    16652024        START_PROFILE(SMBfclose);
    16662025
    1667         if (lp_posix_pathnames()) {
     2026        if (req->posix_pathnames) {
    16682027                reply_unknown_new(req, req->cmd);
    16692028                END_PROFILE(SMBfclose);
     
    17142073        struct smb_filename *smb_fname = NULL;
    17152074        char *fname = NULL;
    1716         uint32 fattr=0;
    1717         SMB_OFF_T size = 0;
     2075        uint32_t fattr=0;
     2076        off_t size = 0;
    17182077        time_t mtime=0;
    17192078        int info;
     
    17212080        int oplock_request;
    17222081        int deny_mode;
    1723         uint32 dos_attr;
    1724         uint32 access_mask;
    1725         uint32 share_mode;
    1726         uint32 create_disposition;
    1727         uint32 create_options = 0;
     2082        uint32_t dos_attr;
     2083        uint32_t access_mask;
     2084        uint32_t share_mode;
     2085        uint32_t create_disposition;
     2086        uint32_t create_options = 0;
    17282087        uint32_t private_flags = 0;
    17292088        NTSTATUS status;
    1730         bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
     2089        uint32_t ucf_flags = UCF_PREP_CREATEFILE |
     2090                        (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    17312091        TALLOC_CTX *ctx = talloc_tos();
    17322092
     
    17502110
    17512111        if (!map_open_params_to_ntcreate(fname, deny_mode,
    1752                                         OPENX_FILE_EXISTS_OPEN, &access_mask,
    1753                                         &share_mode, &create_disposition,
    1754                                         &create_options, &private_flags)) {
     2112                                         OPENX_FILE_EXISTS_OPEN, &access_mask,
     2113                                         &share_mode, &create_disposition,
     2114                                         &create_options, &private_flags)) {
    17552115                reply_force_doserror(req, ERRDOS, ERRbadaccess);
    17562116                goto out;
     
    17612121                                req->flags2 & FLAGS2_DFS_PATHNAMES,
    17622122                                fname,
    1763                                 (create_disposition == FILE_CREATE)
    1764                                         ? UCF_CREATING_FILE : 0,
     2123                                ucf_flags,
    17652124                                NULL,
    17662125                                &smb_fname);
     
    17872146                dos_attr,                               /* file_attributes */
    17882147                oplock_request,                         /* oplock_request */
     2148                NULL,                                   /* lease */
    17892149                0,                                      /* allocation_size */
    17902150                private_flags,
     
    17922152                NULL,                                   /* ea_list */
    17932153                &fsp,                                   /* result */
    1794                 &info);                                 /* pinfo */
     2154                &info,                                  /* pinfo */
     2155                NULL, NULL);                            /* create context */
    17952156
    17962157        if (!NT_STATUS_IS_OK(status)) {
    1797                 if (open_was_deferred(req->mid)) {
     2158                if (open_was_deferred(req->xconn, req->mid)) {
    17982159                        /* We have re-scheduled this call. */
    17992160                        goto out;
     
    18032164        }
    18042165
     2166        /* Ensure we're pointing at the correct stat struct. */
     2167        TALLOC_FREE(smb_fname);
     2168        smb_fname = fsp->fsp_name;
     2169
    18052170        size = smb_fname->st.st_ex_size;
    18062171        fattr = dos_mode(conn, smb_fname);
    1807 
    1808         /* Deal with other possible opens having a modified
    1809            write time. JRA. */
    1810         if (ask_sharemode) {
    1811                 struct timespec write_time_ts;
    1812 
    1813                 ZERO_STRUCT(write_time_ts);
    1814                 get_file_infos(fsp->file_id, 0, NULL, &write_time_ts);
    1815                 if (!null_timespec(write_time_ts)) {
    1816                         update_stat_ex_mtime(&smb_fname->st, write_time_ts);
    1817                 }
    1818         }
    18192172
    18202173        mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
     
    18372190                srv_put_dos_date3((char *)req->outbuf,smb_vwv2,mtime);
    18382191        }
    1839         SIVAL(req->outbuf,smb_vwv4,(uint32)size);
     2192        SIVAL(req->outbuf,smb_vwv4,(uint32_t)size);
    18402193        SSVAL(req->outbuf,smb_vwv6,deny_mode);
    18412194
     
    18502203        }
    18512204 out:
    1852         TALLOC_FREE(smb_fname);
    18532205        END_PROFILE(SMBopen);
    18542206        return;
     
    18642216        struct smb_filename *smb_fname = NULL;
    18652217        char *fname = NULL;
    1866         uint16 open_flags;
     2218        uint16_t open_flags;
    18672219        int deny_mode;
    1868         uint32 smb_attr;
     2220        uint32_t smb_attr;
    18692221        /* Breakout the oplock request bits so we can set the
    18702222                reply bits separately. */
     
    18742226#if 0
    18752227        int smb_sattr = SVAL(req->vwv+4, 0);
    1876         uint32 smb_time = make_unix_date3(req->vwv+6);
     2228        uint32_t smb_time = make_unix_date3(req->vwv+6);
    18772229#endif
    18782230        int smb_ofun;
    1879         uint32 fattr=0;
     2231        uint32_t fattr=0;
    18802232        int mtime=0;
    18812233        int smb_action = 0;
     
    18842236        uint64_t allocation_size;
    18852237        ssize_t retval = -1;
    1886         uint32 access_mask;
    1887         uint32 share_mode;
    1888         uint32 create_disposition;
    1889         uint32 create_options = 0;
     2238        uint32_t access_mask;
     2239        uint32_t share_mode;
     2240        uint32_t create_disposition;
     2241        uint32_t create_options = 0;
    18902242        uint32_t private_flags = 0;
     2243        uint32_t ucf_flags = UCF_PREP_CREATEFILE |
     2244                        (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    18912245        TALLOC_CTX *ctx = talloc_tos();
    18922246
     
    19262280
    19272281        if (!map_open_params_to_ntcreate(fname, deny_mode,
    1928                                         smb_ofun,
    1929                                         &access_mask, &share_mode,
    1930                                         &create_disposition,
    1931                                         &create_options,
    1932                                         &private_flags)) {
     2282                                         smb_ofun,
     2283                                         &access_mask, &share_mode,
     2284                                         &create_disposition,
     2285                                         &create_options,
     2286                                         &private_flags)) {
    19332287                reply_force_doserror(req, ERRDOS, ERRbadaccess);
    19342288                goto out;
     
    19392293                                req->flags2 & FLAGS2_DFS_PATHNAMES,
    19402294                                fname,
    1941                                 (create_disposition == FILE_CREATE)
    1942                                         ? UCF_CREATING_FILE : 0,
     2295                                ucf_flags,
    19432296                                NULL,
    19442297                                &smb_fname);
     
    19652318                smb_attr,                               /* file_attributes */
    19662319                oplock_request,                         /* oplock_request */
     2320                NULL,                                   /* lease */
    19672321                0,                                      /* allocation_size */
    19682322                private_flags,
     
    19702324                NULL,                                   /* ea_list */
    19712325                &fsp,                                   /* result */
    1972                 &smb_action);                           /* pinfo */
     2326                &smb_action,                            /* pinfo */
     2327                NULL, NULL);                            /* create context */
    19732328
    19742329        if (!NT_STATUS_IS_OK(status)) {
    1975                 if (open_was_deferred(req->mid)) {
     2330                if (open_was_deferred(req->xconn, req->mid)) {
    19762331                        /* We have re-scheduled this call. */
    19772332                        goto out;
     
    19902345                        goto out;
    19912346                }
    1992                 retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
     2347                retval = vfs_set_filelen(fsp, (off_t)allocation_size);
    19932348                if (retval < 0) {
    19942349                        close_file(req, fsp, ERROR_CLOSE);
     
    20352390                reply_outbuf(req, 15, 0);
    20362391        }
     2392
     2393        SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
     2394        SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
    20372395
    20382396        if (core_oplock_request && lp_fake_oplocks(SNUM(conn))) {
     
    20532411                srv_put_dos_date3((char *)req->outbuf,smb_vwv4,mtime);
    20542412        }
    2055         SIVAL(req->outbuf,smb_vwv6,(uint32)fsp->fsp_name->st.st_ex_size);
     2413        SIVAL(req->outbuf,smb_vwv6,(uint32_t)fsp->fsp_name->st.st_ex_size);
    20562414        SSVAL(req->outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
    20572415        SSVAL(req->outbuf,smb_vwv11,smb_action);
     
    20612419        }
    20622420
    2063         chain_reply(req);
    20642421 out:
    20652422        TALLOC_FREE(smb_fname);
     
    20752432{
    20762433        struct smbd_server_connection *sconn = req->sconn;
    2077         user_struct *vuser;
     2434        struct user_struct *vuser;
     2435        struct smbXsrv_session *session = NULL;
     2436        NTSTATUS status;
    20782437
    20792438        START_PROFILE(SMBulogoffX);
     
    20822441
    20832442        if(vuser == NULL) {
    2084                 DEBUG(3,("ulogoff, vuser id %d does not map to user.\n",
    2085                          req->vuid));
    2086         }
    2087 
    2088         /* in user level security we are supposed to close any files
    2089                 open by this user */
    2090         if ((vuser != NULL) && (lp_security() != SEC_SHARE)) {
    2091                 file_close_user(sconn, req->vuid);
    2092         }
    2093 
    2094         invalidate_vuid(sconn, req->vuid);
     2443                DEBUG(3,("ulogoff, vuser id %llu does not map to user.\n",
     2444                         (unsigned long long)req->vuid));
     2445
     2446                req->vuid = UID_FIELD_INVALID;
     2447                reply_force_doserror(req, ERRSRV, ERRbaduid);
     2448                END_PROFILE(SMBulogoffX);
     2449                return;
     2450        }
     2451
     2452        session = vuser->session;
     2453        vuser = NULL;
     2454
     2455        /*
     2456         * TODO: cancel all outstanding requests on the session
     2457         */
     2458        status = smbXsrv_session_logoff(session);
     2459        if (!NT_STATUS_IS_OK(status)) {
     2460                DEBUG(0, ("reply_ulogoff: "
     2461                          "smbXsrv_session_logoff() failed: %s\n",
     2462                          nt_errstr(status)));
     2463                /*
     2464                 * If we hit this case, there is something completely
     2465                 * wrong, so we better disconnect the transport connection.
     2466                 */
     2467                END_PROFILE(SMBulogoffX);
     2468                exit_server(__location__ ": smbXsrv_session_logoff failed");
     2469                return;
     2470        }
     2471
     2472        TALLOC_FREE(session);
    20952473
    20962474        reply_outbuf(req, 2, 0);
    2097 
    2098         DEBUG( 3, ( "ulogoffX vuid=%d\n", req->vuid ) );
     2475        SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
     2476        SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
     2477
     2478        DEBUG(3, ("ulogoffX vuid=%llu\n",
     2479                  (unsigned long long)req->vuid));
    20992480
    21002481        END_PROFILE(SMBulogoffX);
    21012482        req->vuid = UID_FIELD_INVALID;
    2102         chain_reply(req);
    21032483}
    21042484
     
    21122492        struct smb_filename *smb_fname = NULL;
    21132493        char *fname = NULL;
    2114         uint32 fattr = 0;
     2494        uint32_t fattr = 0;
    21152495        struct smb_file_time ft;
    21162496        files_struct *fsp;
    21172497        int oplock_request = 0;
    21182498        NTSTATUS status;
    2119         uint32 access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
    2120         uint32 share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
    2121         uint32 create_disposition;
    2122         uint32 create_options = 0;
     2499        uint32_t access_mask = FILE_GENERIC_READ | FILE_GENERIC_WRITE;
     2500        uint32_t share_mode = FILE_SHARE_READ|FILE_SHARE_WRITE;
     2501        uint32_t create_disposition;
     2502        uint32_t create_options = 0;
     2503        uint32_t ucf_flags = UCF_PREP_CREATEFILE |
     2504                        (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    21232505        TALLOC_CTX *ctx = talloc_tos();
    21242506
     
    21482530                                req->flags2 & FLAGS2_DFS_PATHNAMES,
    21492531                                fname,
    2150                                 UCF_CREATING_FILE,
     2532                                ucf_flags,
    21512533                                NULL,
    21522534                                &smb_fname);
     
    21872569                fattr,                                  /* file_attributes */
    21882570                oplock_request,                         /* oplock_request */
     2571                NULL,                                   /* lease */
    21892572                0,                                      /* allocation_size */
    21902573                0,                                      /* private_flags */
     
    21922575                NULL,                                   /* ea_list */
    21932576                &fsp,                                   /* result */
    2194                 NULL);                                  /* pinfo */
     2577                NULL,                                   /* pinfo */
     2578                NULL, NULL);                            /* create context */
    21952579
    21962580        if (!NT_STATUS_IS_OK(status)) {
    2197                 if (open_was_deferred(req->mid)) {
     2581                if (open_was_deferred(req->xconn, req->mid)) {
    21982582                        /* We have re-scheduled this call. */
    21992583                        goto out;
     
    22442628        char *wire_name = NULL;
    22452629        char *fname = NULL;
    2246         uint32 fattr;
     2630        uint32_t fattr;
    22472631        files_struct *fsp;
    22482632        int oplock_request;
     
    22502634        NTSTATUS status;
    22512635        int i;
     2636        uint32_t ucf_flags = UCF_PREP_CREATEFILE |
     2637                        (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    22522638        TALLOC_CTX *ctx = talloc_tos();
    22532639
     
    22892675                                req->flags2 & FLAGS2_DFS_PATHNAMES,
    22902676                                fname,
    2291                                 UCF_CREATING_FILE,
     2677                                ucf_flags,
    22922678                                NULL,
    22932679                                &smb_fname);
     
    23042690                /* Create the file. */
    23052691                status = SMB_VFS_CREATE_FILE(
    2306                         conn,                                   /* conn */
    2307                         req,                                    /* req */
    2308                         0,                                      /* root_dir_fid */
    2309                         smb_fname,                              /* fname */
     2692                        conn,                                   /* conn */
     2693                        req,                                    /* req */
     2694                        0,                                      /* root_dir_fid */
     2695                        smb_fname,                              /* fname */
    23102696                        FILE_GENERIC_READ | FILE_GENERIC_WRITE, /* access_mask */
    2311                         FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
    2312                         FILE_CREATE,                            /* create_disposition*/
    2313                         0,                                      /* create_options */
    2314                         fattr,                                  /* file_attributes */
    2315                         oplock_request,                         /* oplock_request */
    2316                         0,                                      /* allocation_size */
    2317                         0,                                      /* private_flags */
    2318                         NULL,                                   /* sd */
    2319                         NULL,                                   /* ea_list */
    2320                         &fsp,                                   /* result */
    2321                         NULL);                                  /* pinfo */
     2697                        FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
     2698                        FILE_CREATE,                            /* create_disposition*/
     2699                        0,                                      /* create_options */
     2700                        fattr,                                  /* file_attributes */
     2701                        oplock_request,                         /* oplock_request */
     2702                        NULL,                                   /* lease */
     2703                        0,                                      /* allocation_size */
     2704                        0,                                      /* private_flags */
     2705                        NULL,                                   /* sd */
     2706                        NULL,                                   /* ea_list */
     2707                        &fsp,                                   /* result */
     2708                        NULL,                                   /* pinfo */
     2709                        NULL, NULL);                            /* create context */
    23222710
    23232711                if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
     
    23282716
    23292717                if (!NT_STATUS_IS_OK(status)) {
    2330                         if (open_was_deferred(req->mid)) {
     2718                        if (open_was_deferred(req->xconn, req->mid)) {
    23312719                                /* We have re-scheduled this call. */
    23322720                                goto out;
     
    23822770 out:
    23832771        TALLOC_FREE(smb_fname);
    2384         TALLOC_FREE(fname);
    23852772        TALLOC_FREE(wire_name);
    23862773        END_PROFILE(SMBctemp);
     
    23932780
    23942781static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
    2395                         uint16 dirtype)
     2782                        uint16_t dirtype)
    23962783{
    23972784        if (!CAN_WRITE(conn)) {
     
    24022789                        (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
    24032790                /* Only bother to read the DOS attribute if we might deny the
    2404                    rename on the grounds of attribute missmatch. */
     2791                   rename on the grounds of attribute mismatch. */
    24052792                uint32_t fmode = dos_mode(conn, fsp->fsp_name);
    24062793                if ((fmode & ~dirtype) & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)) {
     
    24102797
    24112798        if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
    2412                 if (fsp->posix_open) {
     2799                if (fsp->posix_flags & FSP_POSIX_FLAGS_RENAME) {
    24132800                        return NT_STATUS_OK;
    24142801                }
     
    24172804                   directory, allow the rename. */
    24182805
    2419                 if (file_find_subpath(fsp)) {
     2806                if (lp_strict_rename(SNUM(conn))) {
     2807                        /*
     2808                         * Strict rename, check open file db.
     2809                         */
     2810                        if (have_file_open_below(fsp->conn, fsp->fsp_name)) {
     2811                                return NT_STATUS_ACCESS_DENIED;
     2812                        }
     2813                } else if (file_find_subpath(fsp)) {
     2814                        /*
     2815                         * No strict rename, just look in local process.
     2816                         */
    24202817                        return NT_STATUS_ACCESS_DENIED;
    24212818                }
     
    24372834                        struct smb_request *req,
    24382835                        struct smb_filename *smb_fname,
    2439                         uint32 dirtype)
    2440 {
    2441         uint32 fattr;
     2836                        uint32_t dirtype)
     2837{
     2838        uint32_t fattr;
    24422839        files_struct *fsp;
    2443         uint32 dirtype_orig = dirtype;
     2840        uint32_t dirtype_orig = dirtype;
    24442841        NTSTATUS status;
    24452842        int ret;
    2446         bool posix_paths = lp_posix_pathnames();
     2843        bool posix_paths = (req != NULL && req->posix_pathnames);
    24472844
    24482845        DEBUG(10,("do_unlink: %s, dirtype = %d\n",
     
    24742871        }
    24752872
    2476         if (!dir_check_ftype(conn, fattr, dirtype)) {
     2873        if (!dir_check_ftype(fattr, dirtype)) {
    24772874                if (fattr & FILE_ATTRIBUTE_DIRECTORY) {
    24782875                        return NT_STATUS_FILE_IS_A_DIRECTORY;
     
    25322929                                FILE_ATTRIBUTE_NORMAL,
    25332930                 0,                     /* oplock_request */
     2931                 NULL,                  /* lease */
    25342932                 0,                     /* allocation_size */
    25352933                 0,                     /* private_flags */
     
    25372935                 NULL,                  /* ea_list */
    25382936                 &fsp,                  /* result */
    2539                  NULL);                 /* pinfo */
     2937                 NULL,                  /* pinfo */
     2938                 NULL, NULL);           /* create context */
    25402939
    25412940        if (!NT_STATUS_IS_OK(status)) {
     
    25562955
    25572956        /* The set is across all open files on this dev/inode pair. */
    2558         if (!set_delete_on_close(fsp, true,
     2957        if (!set_delete_on_close(fsp, True,
    25592958                                conn->session_info->security_token,
    2560                                 &conn->session_info->utok)) {
     2959                                conn->session_info->unix_token)) {
    25612960                close_file(req, fsp, NORMAL_CLOSE);
    25622961                return NT_STATUS_ACCESS_DENIED;
     
    25722971
    25732972NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
    2574                           uint32 dirtype, struct smb_filename *smb_fname,
     2973                          uint32_t dirtype, struct smb_filename *smb_fname,
    25752974                          bool has_wild)
    25762975{
     
    26553054                        goto out;
    26563055                }
     3056                if (dirtype == 0) {
     3057                        dirtype = FILE_ATTRIBUTE_NORMAL;
     3058                }
    26573059
    26583060                if (strequal(fname_mask,"????????.???")) {
     
    27393141                        status = do_unlink(conn, req, smb_fname, dirtype);
    27403142                        if (!NT_STATUS_IS_OK(status)) {
     3143                                TALLOC_FREE(dir_hnd);
    27413144                                TALLOC_FREE(frame);
    27423145                                TALLOC_FREE(talloced);
    2743                                 continue;
     3146                                goto out;
    27443147                        }
    27453148
     
    27733176        char *name = NULL;
    27743177        struct smb_filename *smb_fname = NULL;
    2775         uint32 dirtype;
     3178        uint32_t dirtype;
    27763179        NTSTATUS status;
    27773180        bool path_contains_wcard = False;
     3181        uint32_t ucf_flags = UCF_COND_ALLOW_WCARD_LCOMP |
     3182                        (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    27783183        TALLOC_CTX *ctx = talloc_tos();
    27793184
     
    27983203                                  req->flags2 & FLAGS2_DFS_PATHNAMES,
    27993204                                  name,
    2800                                   UCF_COND_ALLOW_WCARD_LCOMP,
     3205                                  ucf_flags,
    28013206                                  &path_contains_wcard,
    28023207                                  &smb_fname);
     
    28163221                                  path_contains_wcard);
    28173222        if (!NT_STATUS_IS_OK(status)) {
    2818                 if (open_was_deferred(req->mid)) {
     3223                if (open_was_deferred(req->xconn, req->mid)) {
    28193224                        /* We have re-scheduled this call. */
    28203225                        goto out;
     
    28503255****************************************************************************/
    28513256
    2852 ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos, size_t nread)
     3257ssize_t fake_sendfile(struct smbXsrv_connection *xconn, files_struct *fsp,
     3258                      off_t startpos, size_t nread)
    28533259{
    28543260        size_t bufsize;
     
    28703276                size_t cur_read;
    28713277
    2872                 if (tosend > bufsize) {
    2873                         cur_read = bufsize;
    2874                 } else {
    2875                         cur_read = tosend;
    2876                 }
     3278                cur_read = MIN(tosend, bufsize);
    28773279                ret = read_file(fsp,buf,startpos,cur_read);
    28783280                if (ret == -1) {
     
    28863288                }
    28873289
    2888                 if (write_data(fsp->conn->sconn->sock, buf, cur_read)
    2889                     != cur_read) {
    2890                         char addr[INET6_ADDRSTRLEN];
     3290                ret = write_data(xconn->transport.sock, buf, cur_read);
     3291                if (ret != cur_read) {
     3292                        int saved_errno = errno;
    28913293                        /*
    28923294                         * Try and give an error message saying what
     
    28953297                        DEBUG(0, ("write_data failed for client %s. "
    28963298                                  "Error %s\n",
    2897                                   get_peer_addr(fsp->conn->sconn->sock, addr,
    2898                                                 sizeof(addr)),
    2899                                   strerror(errno)));
     3299                                  smbXsrv_connection_dbg(xconn),
     3300                                  strerror(saved_errno)));
    29003301                        SAFE_FREE(buf);
     3302                        errno = saved_errno;
    29013303                        return -1;
    29023304                }
     
    29113313/****************************************************************************
    29123314 Deal with the case of sendfile reading less bytes from the file than
    2913  requested. Fill with zeros (all we can do).
     3315 requested. Fill with zeros (all we can do). Returns 0 on success
    29143316****************************************************************************/
    29153317
    2916 void sendfile_short_send(files_struct *fsp,
    2917                                 ssize_t nread,
    2918                                 size_t headersize,
    2919                                 size_t smb_maxcnt)
     3318ssize_t sendfile_short_send(struct smbXsrv_connection *xconn,
     3319                            files_struct *fsp,
     3320                            ssize_t nread,
     3321                            size_t headersize,
     3322                            size_t smb_maxcnt)
    29203323{
    29213324#define SHORT_SEND_BUFSIZE 1024
     
    29243327                        "header for file %s (%s). Terminating\n",
    29253328                        fsp_str_dbg(fsp), strerror(errno)));
    2926                 exit_server_cleanly("sendfile_short_send failed");
     3329                return -1;
    29273330        }
    29283331
     
    29323335                char *buf = SMB_CALLOC_ARRAY(char, SHORT_SEND_BUFSIZE);
    29333336                if (!buf) {
    2934                         exit_server_cleanly("sendfile_short_send: "
    2935                                 "malloc failed");
     3337                        DEBUG(0,("sendfile_short_send: malloc failed "
     3338                                "for file %s (%s). Terminating\n",
     3339                                fsp_str_dbg(fsp), strerror(errno)));
     3340                        return -1;
    29363341                }
    29373342
     
    29553360                         */
    29563361                        size_t to_write;
     3362                        ssize_t ret;
    29573363
    29583364                        to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread);
    2959                         if (write_data(fsp->conn->sconn->sock, buf, to_write)
    2960                             != to_write) {
    2961                                 char addr[INET6_ADDRSTRLEN];
     3365                        ret = write_data(xconn->transport.sock, buf, to_write);
     3366                        if (ret != to_write) {
     3367                                int saved_errno = errno;
    29623368                                /*
    29633369                                 * Try and give an error message saying what
     
    29663372                                DEBUG(0, ("write_data failed for client %s. "
    29673373                                          "Error %s\n",
    2968                                           get_peer_addr(
    2969                                                   fsp->conn->sconn->sock, addr,
    2970                                                   sizeof(addr)),
    2971                                           strerror(errno)));
    2972                                 exit_server_cleanly("sendfile_short_send: "
    2973                                                     "write_data failed");
     3374                                          smbXsrv_connection_dbg(xconn),
     3375                                          strerror(saved_errno)));
     3376                                errno = saved_errno;
     3377                                return -1;
    29743378                        }
    29753379                        nread += to_write;
     
    29773381                SAFE_FREE(buf);
    29783382        }
     3383
     3384        return 0;
    29793385}
    29803386
     
    29833389****************************************************************************/
    29843390
    2985 static void reply_readbraw_error(struct smbd_server_connection *sconn)
     3391static void reply_readbraw_error(struct smbXsrv_connection *xconn)
    29863392{
    29873393        char header[4];
     
    29893395        SIVAL(header,0,0);
    29903396
    2991         smbd_lock_socket(sconn);
    2992         if (write_data(sconn->sock,header,4) != 4) {
    2993                 char addr[INET6_ADDRSTRLEN];
     3397        smbd_lock_socket(xconn);
     3398        if (write_data(xconn->transport.sock,header,4) != 4) {
     3399                int saved_errno = errno;
    29943400                /*
    29953401                 * Try and give an error message saying what
     
    29983404                DEBUG(0, ("write_data failed for client %s. "
    29993405                          "Error %s\n",
    3000                           get_peer_addr(sconn->sock, addr, sizeof(addr)),
    3001                           strerror(errno)));
     3406                          smbXsrv_connection_dbg(xconn),
     3407                          strerror(saved_errno)));
     3408                errno = saved_errno;
    30023409
    30033410                fail_readraw();
    30043411        }
    3005         smbd_unlock_socket(sconn);
     3412        smbd_unlock_socket(xconn);
    30063413}
    30073414
     
    30133420                               struct smb_request *req,
    30143421                               files_struct *fsp,
    3015                                SMB_OFF_T startpos,
     3422                               off_t startpos,
    30163423                               size_t nread,
    30173424                               ssize_t mincount)
    30183425{
    3019         struct smbd_server_connection *sconn = req->sconn;
     3426        struct smbXsrv_connection *xconn = req->xconn;
    30203427        char *outbuf = NULL;
    30213428        ssize_t ret=0;
     
    30303437        if ( !req_is_in_chain(req) && (nread > 0) && (fsp->base_fsp == NULL) &&
    30313438            (fsp->wcp == NULL) &&
    3032             lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
     3439            lp_use_sendfile(SNUM(conn), xconn->smb1.signing_state) ) {
    30333440                ssize_t sendfile_read = -1;
    30343441                char header[4];
     
    30383445                header_blob = data_blob_const(header, 4);
    30393446
    3040                 sendfile_read = SMB_VFS_SENDFILE(sconn->sock, fsp,
     3447                sendfile_read = SMB_VFS_SENDFILE(xconn->transport.sock, fsp,
    30413448                                                 &header_blob, startpos,
    30423449                                                 nread);
     
    30583465                                DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
    30593466
    3060                                 if (fake_sendfile(fsp, startpos, nread) == -1) {
     3467                                if (fake_sendfile(xconn, fsp, startpos, nread) == -1) {
    30613468                                        DEBUG(0,("send_file_readbraw: "
    30623469                                                 "fake_sendfile failed for "
     
    30893496                /* Deal with possible short send. */
    30903497                if (sendfile_read != 4+nread) {
    3091                         sendfile_short_send(fsp, sendfile_read, 4, nread);
     3498                        ret = sendfile_short_send(xconn, fsp,
     3499                                                  sendfile_read, 4, nread);
     3500                        if (ret == -1) {
     3501                                fail_readraw();
     3502                        }
    30923503                }
    30933504                return;
     
    30963507normal_readbraw:
    30973508
    3098         outbuf = TALLOC_ARRAY(NULL, char, nread+4);
     3509        outbuf = talloc_array(NULL, char, nread+4);
    30993510        if (!outbuf) {
    3100                 DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
     3511                DEBUG(0,("send_file_readbraw: talloc_array failed for size %u.\n",
    31013512                        (unsigned)(nread+4)));
    3102                 reply_readbraw_error(sconn);
     3513                reply_readbraw_error(xconn);
    31033514                return;
    31043515        }
     
    31163527
    31173528        _smb_setlen(outbuf,ret);
    3118         if (write_data(sconn->sock, outbuf, 4+ret) != 4+ret) {
    3119                 char addr[INET6_ADDRSTRLEN];
     3529        if (write_data(xconn->transport.sock, outbuf, 4+ret) != 4+ret) {
     3530                int saved_errno = errno;
    31203531                /*
    31213532                 * Try and give an error message saying what
    31223533                 * client failed.
    31233534                 */
    3124                 DEBUG(0, ("write_data failed for client %s. "
    3125                           "Error %s\n",
    3126                           get_peer_addr(fsp->conn->sconn->sock, addr,
    3127                                         sizeof(addr)),
    3128                           strerror(errno)));
     3535                DEBUG(0, ("write_data failed for client %s. Error %s\n",
     3536                          smbXsrv_connection_dbg(xconn),
     3537                          strerror(saved_errno)));
     3538                errno = saved_errno;
    31293539
    31303540                fail_readraw();
     
    31413551{
    31423552        connection_struct *conn = req->conn;
    3143         struct smbd_server_connection *sconn = req->sconn;
     3553        struct smbXsrv_connection *xconn = req->xconn;
    31443554        ssize_t maxcount,mincount;
    31453555        size_t nread = 0;
    3146         SMB_OFF_T startpos;
     3556        off_t startpos;
    31473557        files_struct *fsp;
    31483558        struct lock_struct lock;
    3149         SMB_OFF_T size = 0;
     3559        off_t size = 0;
    31503560
    31513561        START_PROFILE(SMBreadbraw);
    31523562
    3153         if (srv_is_signing_active(sconn) || req->encrypted) {
     3563        if (srv_is_signing_active(xconn) || req->encrypted) {
    31543564                exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
    31553565                        "raw reads/writes are disallowed.");
     
    31573567
    31583568        if (req->wct < 8) {
    3159                 reply_readbraw_error(sconn);
     3569                reply_readbraw_error(xconn);
    31603570                END_PROFILE(SMBreadbraw);
    31613571                return;
    31623572        }
    31633573
    3164         if (sconn->smb1.echo_handler.trusted_fde) {
     3574        if (xconn->smb1.echo_handler.trusted_fde) {
    31653575                DEBUG(2,("SMBreadbraw rejected with NOT_SUPPORTED because of "
    31663576                         "'async smb echo handler = yes'\n"));
    3167                 reply_readbraw_error(sconn);
     3577                reply_readbraw_error(xconn);
    31683578                END_PROFILE(SMBreadbraw);
    31693579                return;
     
    31933603                        "- cache prime?\n",
    31943604                        (int)SVAL(req->vwv+0, 0)));
    3195                 reply_readbraw_error(sconn);
     3605                reply_readbraw_error(xconn);
    31963606                END_PROFILE(SMBreadbraw);
    31973607                return;
     
    32043614                DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
    32053615                                (int)SVAL(req->vwv+0, 0)));
    3206                 reply_readbraw_error(sconn);
     3616                reply_readbraw_error(xconn);
    32073617                END_PROFILE(SMBreadbraw);
    32083618                return;
    32093619        }
    32103620
    3211         flush_write_cache(fsp, READRAW_FLUSH);
     3621        flush_write_cache(fsp, SAMBA_READRAW_FLUSH);
    32123622
    32133623        startpos = IVAL_TO_SMB_OFF_T(req->vwv+1, 0);
     
    32163626                 * This is a large offset (64 bit) read.
    32173627                 */
    3218 #ifdef LARGE_SMB_OFF_T
    3219 
    3220                 startpos |= (((SMB_OFF_T)IVAL(req->vwv+8, 0)) << 32);
    3221 
    3222 #else /* !LARGE_SMB_OFF_T */
    3223 
    3224                 /*
    3225                  * Ensure we haven't been sent a >32 bit offset.
    3226                  */
    3227 
    3228                 if(IVAL(req->vwv+8, 0) != 0) {
    3229                         DEBUG(0,("reply_readbraw: large offset "
    3230                                 "(%x << 32) used and we don't support "
    3231                                 "64 bit offsets.\n",
    3232                         (unsigned int)IVAL(req->vwv+8, 0) ));
    3233                         reply_readbraw_error(sconn);
    3234                         END_PROFILE(SMBreadbraw);
    3235                         return;
    3236                 }
    3237 
    3238 #endif /* LARGE_SMB_OFF_T */
     3628
     3629                startpos |= (((off_t)IVAL(req->vwv+8, 0)) << 32);
    32393630
    32403631                if(startpos < 0) {
     
    32423633                                "readraw offset (%.0f) !\n",
    32433634                                (double)startpos ));
    3244                         reply_readbraw_error(sconn);
     3635                        reply_readbraw_error(xconn);
    32453636                        END_PROFILE(SMBreadbraw);
    32463637                        return;
     
    32593650
    32603651        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
    3261                 reply_readbraw_error(sconn);
     3652                reply_readbraw_error(xconn);
    32623653                END_PROFILE(SMBreadbraw);
    32633654                return;
     
    32793670#endif
    32803671
    3281         DEBUG( 3, ( "reply_readbraw: fnum=%d start=%.0f max=%lu "
     3672        DEBUG( 3, ( "reply_readbraw: %s start=%.0f max=%lu "
    32823673                "min=%lu nread=%lu\n",
    3283                 fsp->fnum, (double)startpos,
     3674                fsp_fnum_dbg(fsp), (double)startpos,
    32843675                (unsigned long)maxcount,
    32853676                (unsigned long)mincount,
     
    33083699        ssize_t nread = -1;
    33093700        char *data;
    3310         SMB_OFF_T startpos;
     3701        off_t startpos;
    33113702        size_t numtoread;
     3703        size_t maxtoread;
    33123704        NTSTATUS status;
    33133705        files_struct *fsp;
    33143706        struct byte_range_lock *br_lck = NULL;
    33153707        char *p = NULL;
    3316         struct smbd_server_connection *sconn = req->sconn;
     3708        struct smbXsrv_connection *xconn = req->xconn;
    33173709
    33183710        START_PROFILE(SMBlockread);
     
    33393731        numtoread = SVAL(req->vwv+1, 0);
    33403732        startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
    3341 
    3342         numtoread = MIN(BUFFER_SIZE - (smb_size + 3*2 + 3), numtoread);
    3343 
    3344         reply_outbuf(req, 5, numtoread + 3);
    3345 
    3346         data = smb_buf(req->outbuf) + 3;
    33473733
    33483734        /*
     
    33513737         * Thus instead of asking for a read lock here we need to ask
    33523738         * for a write lock. JRA.
    3353          * Note that the requested lock size is unaffected by max_recv.
     3739         * Note that the requested lock size is unaffected by max_send.
    33543740         */
    33553741
     
    33633749                        False, /* Non-blocking lock. */
    33643750                        &status,
    3365                         NULL,
    33663751                        NULL);
    33673752        TALLOC_FREE(br_lck);
     
    33743759
    33753760        /*
    3376          * However the requested READ size IS affected by max_recv. Insanity.... JRA.
     3761         * However the requested READ size IS affected by max_send. Insanity.... JRA.
    33773762         */
    3378 
    3379         if (numtoread > sconn->smb1.negprot.max_recv) {
    3380                 DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u). \
     3763        maxtoread = xconn->smb1.sessions.max_send - (smb_size + 5*2 + 3);
     3764
     3765        if (numtoread > maxtoread) {
     3766                DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u/%u). \
    33813767Returning short read of maximum allowed for compatibility with Windows 2000.\n",
    3382                         (unsigned int)numtoread,
    3383                         (unsigned int)sconn->smb1.negprot.max_recv));
    3384                 numtoread = MIN(numtoread, sconn->smb1.negprot.max_recv);
    3385         }
     3768                        (unsigned int)numtoread, (unsigned int)maxtoread,
     3769                        (unsigned int)xconn->smb1.sessions.max_send));
     3770                numtoread = maxtoread;
     3771        }
     3772
     3773        reply_outbuf(req, 5, numtoread + 3);
     3774
     3775        data = smb_buf(req->outbuf) + 3;
     3776
    33863777        nread = read_file(fsp,data,startpos,numtoread);
    33873778
     
    34003791        SSVAL(p,1,nread);
    34013792
    3402         DEBUG(3,("lockread fnum=%d num=%d nread=%d\n",
    3403                  fsp->fnum, (int)numtoread, (int)nread));
     3793        DEBUG(3,("lockread %s num=%d nread=%d\n",
     3794                 fsp_fnum_dbg(fsp), (int)numtoread, (int)nread));
    34043795
    34053796        END_PROFILE(SMBlockread);
     
    34183809        connection_struct *conn = req->conn;
    34193810        size_t numtoread;
     3811        size_t maxtoread;
    34203812        ssize_t nread = 0;
    34213813        char *data;
    3422         SMB_OFF_T startpos;
    3423         int outsize = 0;
     3814        off_t startpos;
    34243815        files_struct *fsp;
    34253816        struct lock_struct lock;
    3426         struct smbd_server_connection *sconn = req->sconn;
     3817        struct smbXsrv_connection *xconn = req->xconn;
    34273818
    34283819        START_PROFILE(SMBread);
     
    34503841        startpos = IVAL_TO_SMB_OFF_T(req->vwv+2, 0);
    34513842
    3452         numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
    3453 
    34543843        /*
    3455          * The requested read size cannot be greater than max_recv. JRA.
     3844         * The requested read size cannot be greater than max_send. JRA.
    34563845         */
    3457         if (numtoread > sconn->smb1.negprot.max_recv) {
    3458                 DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u). \
     3846        maxtoread = xconn->smb1.sessions.max_send - (smb_size + 5*2 + 3);
     3847
     3848        if (numtoread > maxtoread) {
     3849                DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u/%u). \
    34593850Returning short read of maximum allowed for compatibility with Windows 2000.\n",
    3460                         (unsigned int)numtoread,
    3461                         (unsigned int)sconn->smb1.negprot.max_recv));
    3462                 numtoread = MIN(numtoread, sconn->smb1.negprot.max_recv);
     3851                        (unsigned int)numtoread, (unsigned int)maxtoread,
     3852                        (unsigned int)xconn->smb1.sessions.max_send));
     3853                numtoread = maxtoread;
    34633854        }
    34643855
     
    34923883        SSVAL(smb_buf(req->outbuf),1,nread);
    34933884
    3494         DEBUG( 3, ( "read fnum=%d num=%d nread=%d\n",
    3495                 fsp->fnum, (int)numtoread, (int)nread ) );
     3885        DEBUG(3, ("read %s num=%d nread=%d\n",
     3886                  fsp_fnum_dbg(fsp), (int)numtoread, (int)nread));
    34963887
    34973888strict_unlock:
     
    35103901{
    35113902        int outsize;
    3512         char *data;
    3513 
    3514         outsize = srv_set_message(outbuf,12,smb_maxcnt,False);
    3515         data = smb_buf(outbuf);
     3903
     3904        outsize = srv_set_message(outbuf,12,smb_maxcnt + 1 /* padding byte */,
     3905                                  False);
    35163906
    35173907        memset(outbuf+smb_vwv0,'\0',24); /* valgrind init. */
     
    35213911        SSVAL(outbuf,smb_vwv5,smb_maxcnt);
    35223912        SSVAL(outbuf,smb_vwv6,
    3523               req_wct_ofs(req)
     3913              (smb_wct - 4)     /* offset from smb header to wct */
    35243914              + 1               /* the wct field */
    35253915              + 12 * sizeof(uint16_t) /* vwv */
    3526               + 2);             /* the buflen field */
     3916              + 2               /* the buflen field */
     3917              + 1);             /* padding byte */
    35273918        SSVAL(outbuf,smb_vwv7,(smb_maxcnt >> 16));
    35283919        SSVAL(outbuf,smb_vwv11,smb_maxcnt);
     3920        SCVAL(smb_buf(outbuf), 0, 0); /* padding byte */
    35293921        /* Reset the outgoing length, set_message truncates at 0x1FFFF. */
    3530         _smb_setlen_large(outbuf,(smb_size + 12*2 + smb_maxcnt - 4));
     3922        _smb_setlen_large(outbuf,
     3923                          smb_size + 12*2 + smb_maxcnt - 4 + 1 /* pad */);
    35313924        return outsize;
    35323925}
     
    35373930
    35383931static void send_file_readX(connection_struct *conn, struct smb_request *req,
    3539                             files_struct *fsp, SMB_OFF_T startpos,
     3932                            files_struct *fsp, off_t startpos,
    35403933                            size_t smb_maxcnt)
    35413934{
     3935        struct smbXsrv_connection *xconn = req->xconn;
    35423936        ssize_t nread = -1;
    35433937        struct lock_struct lock;
     
    35603954
    35613955        if (!req_is_in_chain(req) &&
    3562             !req->encrypted && (fsp->base_fsp == NULL) &&
     3956            !req->encrypted &&
     3957            (fsp->base_fsp == NULL) &&
    35633958            (fsp->wcp == NULL) &&
    3564             lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
    3565                 uint8 headerbuf[smb_size + 12 * 2];
     3959            lp_use_sendfile(SNUM(conn), xconn->smb1.signing_state) ) {
     3960                uint8_t headerbuf[smb_size + 12 * 2 + 1 /* padding byte */];
    35663961                DATA_BLOB header;
    35673962
     
    35923987                setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
    35933988
    3594                 nread = SMB_VFS_SENDFILE(req->sconn->sock, fsp, &header,
     3989                nread = SMB_VFS_SENDFILE(xconn->transport.sock, fsp, &header,
    35953990                                         startpos, smb_maxcnt);
    35963991                if (nread == -1) {
     3992                        saved_errno = errno;
     3993
    35973994                        /* Returning ENOSYS means no data at all was sent.
    35983995                           Do this as a normal read. */
     
    36114008                                set_use_sendfile(SNUM(conn), False);
    36124009                                DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
    3613                                 nread = fake_sendfile(fsp, startpos,
     4010                                nread = fake_sendfile(xconn, fsp, startpos,
    36144011                                                      smb_maxcnt);
    36154012                                if (nread == -1) {
     4013                                        saved_errno = errno;
    36164014                                        DEBUG(0,("send_file_readX: "
    36174015                                                 "fake_sendfile failed for "
    3618                                                  "file %s (%s).\n",
     4016                                                 "file %s (%s) for client %s. "
     4017                                                 "Terminating\n",
    36194018                                                 fsp_str_dbg(fsp),
    3620                                                  strerror(errno)));
     4019                                                 smbXsrv_connection_dbg(xconn),
     4020                                                 strerror(saved_errno)));
     4021                                        errno = saved_errno;
    36214022                                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
    36224023                                }
    3623                                 DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
    3624                                         fsp->fnum, (int)smb_maxcnt, (int)nread ) );
     4024                                DEBUG(3, ("send_file_readX: fake_sendfile %s max=%d nread=%d\n",
     4025                                          fsp_fnum_dbg(fsp), (int)smb_maxcnt, (int)nread));
    36254026                                /* No outbuf here means successful sendfile. */
    36264027                                goto strict_unlock;
     
    36454046                }
    36464047
    3647                 DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
    3648                         fsp->fnum, (int)smb_maxcnt, (int)nread ) );
     4048                DEBUG(3, ("send_file_readX: sendfile %s max=%d nread=%d\n",
     4049                          fsp_fnum_dbg(fsp), (int)smb_maxcnt, (int)nread));
    36494050
    36504051                /* Deal with possible short send. */
    36514052                if (nread != smb_maxcnt + sizeof(headerbuf)) {
    3652                         sendfile_short_send(fsp, nread, sizeof(headerbuf), smb_maxcnt);
     4053                        ssize_t ret;
     4054
     4055                        ret = sendfile_short_send(xconn, fsp, nread,
     4056                                                  sizeof(headerbuf), smb_maxcnt);
     4057                        if (ret == -1) {
     4058                                const char *r;
     4059                                r = "send_file_readX: sendfile_short_send failed";
     4060                                DEBUG(0,("%s for file %s (%s).\n",
     4061                                         r, fsp_str_dbg(fsp), strerror(errno)));
     4062                                exit_server_cleanly(r);
     4063                        }
    36534064                }
    36544065                /* No outbuf here means successful sendfile. */
     
    36614072
    36624073        if ((smb_maxcnt & 0xFF0000) > 0x10000) {
    3663                 uint8 headerbuf[smb_size + 2*12];
     4074                uint8_t headerbuf[smb_size + 2*12 + 1 /* padding byte */];
     4075                ssize_t ret;
     4076
     4077                if (!S_ISREG(fsp->fsp_name->st.st_ex_mode) ||
     4078                    (startpos > fsp->fsp_name->st.st_ex_size) ||
     4079                    (smb_maxcnt > (fsp->fsp_name->st.st_ex_size - startpos))) {
     4080                        /*
     4081                         * We already know that we would do a short
     4082                         * read, so don't try the sendfile() path.
     4083                         */
     4084                        goto nosendfile_read;
     4085                }
    36644086
    36654087                construct_reply_common_req(req, (char *)headerbuf);
     
    36674089
    36684090                /* Send out the header. */
    3669                 if (write_data(req->sconn->sock, (char *)headerbuf,
    3670                                sizeof(headerbuf)) != sizeof(headerbuf)) {
    3671 
    3672                         char addr[INET6_ADDRSTRLEN];
     4091                ret = write_data(xconn->transport.sock, (char *)headerbuf,
     4092                                 sizeof(headerbuf));
     4093                if (ret != sizeof(headerbuf)) {
     4094                        saved_errno = errno;
    36734095                        /*
    36744096                         * Try and give an error message saying what
    36754097                         * client failed.
    36764098                         */
    3677                         DEBUG(0, ("write_data failed for client %s. "
    3678                                   "Error %s\n",
    3679                                   get_peer_addr(req->sconn->sock, addr,
    3680                                                 sizeof(addr)),
    3681                                   strerror(errno)));
    3682 
    36834099                        DEBUG(0,("send_file_readX: write_data failed for file "
    3684                                  "%s (%s). Terminating\n", fsp_str_dbg(fsp),
    3685                                  strerror(errno)));
     4100                                 "%s (%s) for client %s. Terminating\n",
     4101                                 fsp_str_dbg(fsp),
     4102                                 smbXsrv_connection_dbg(xconn),
     4103                                 strerror(saved_errno)));
     4104                        errno = saved_errno;
    36864105                        exit_server_cleanly("send_file_readX sendfile failed");
    36874106                }
    3688                 nread = fake_sendfile(fsp, startpos, smb_maxcnt);
     4107                nread = fake_sendfile(xconn, fsp, startpos, smb_maxcnt);
    36894108                if (nread == -1) {
    3690                         DEBUG(0,("send_file_readX: fake_sendfile failed for "
    3691                                  "file %s (%s).\n", fsp_str_dbg(fsp),
    3692                                  strerror(errno)));
     4109                        saved_errno = errno;
     4110                        DEBUG(0,("send_file_readX: fake_sendfile failed for file "
     4111                                 "%s (%s) for client %s. Terminating\n",
     4112                                 fsp_str_dbg(fsp),
     4113                                 smbXsrv_connection_dbg(xconn),
     4114                                 strerror(saved_errno)));
     4115                        errno = saved_errno;
    36934116                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
    36944117                }
     
    36984121nosendfile_read:
    36994122
    3700         reply_outbuf(req, 12, smb_maxcnt);
    3701 
    3702         nread = read_file(fsp, smb_buf(req->outbuf), startpos, smb_maxcnt);
     4123        reply_outbuf(req, 12, smb_maxcnt + 1 /* padding byte */);
     4124        SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
     4125        SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
     4126
     4127        nread = read_file(fsp, smb_buf(req->outbuf) + 1 /* padding byte */,
     4128                          startpos, smb_maxcnt);
    37034129        saved_errno = errno;
    37044130
     
    37124138        setup_readX_header(req, (char *)req->outbuf, nread);
    37134139
    3714         DEBUG( 3, ( "send_file_readX fnum=%d max=%d nread=%d\n",
    3715                     fsp->fnum, (int)smb_maxcnt, (int)nread ) );
    3716 
    3717         chain_reply(req);
     4140        DEBUG(3, ("send_file_readX %s max=%d nread=%d\n",
     4141                  fsp_fnum_dbg(fsp), (int)smb_maxcnt, (int)nread));
    37184142        return;
    37194143
     
    37254149
    37264150/****************************************************************************
     4151 Work out how much space we have for a read return.
     4152****************************************************************************/
     4153
     4154static size_t calc_max_read_pdu(const struct smb_request *req)
     4155{
     4156        struct smbXsrv_connection *xconn = req->xconn;
     4157
     4158        if (xconn->protocol < PROTOCOL_NT1) {
     4159                return xconn->smb1.sessions.max_send;
     4160        }
     4161
     4162        if (!lp_large_readwrite()) {
     4163                return xconn->smb1.sessions.max_send;
     4164        }
     4165
     4166        if (req_is_in_chain(req)) {
     4167                return xconn->smb1.sessions.max_send;
     4168        }
     4169
     4170        if (req->encrypted) {
     4171                /*
     4172                 * Don't take encrypted traffic up to the
     4173                 * limit. There are padding considerations
     4174                 * that make that tricky.
     4175                 */
     4176                return xconn->smb1.sessions.max_send;
     4177        }
     4178
     4179        if (srv_is_signing_active(xconn)) {
     4180                return 0x1FFFF;
     4181        }
     4182
     4183        if (!lp_unix_extensions()) {
     4184                return 0x1FFFF;
     4185        }
     4186
     4187        /*
     4188         * We can do ultra-large POSIX reads.
     4189         */
     4190        return 0xFFFFFF;
     4191}
     4192
     4193/****************************************************************************
     4194 Calculate how big a read can be. Copes with all clients. It's always
     4195 safe to return a short read - Windows does this.
     4196****************************************************************************/
     4197
     4198static size_t calc_read_size(const struct smb_request *req,
     4199                             size_t upper_size,
     4200                             size_t lower_size)
     4201{
     4202        struct smbXsrv_connection *xconn = req->xconn;
     4203        size_t max_pdu = calc_max_read_pdu(req);
     4204        size_t total_size = 0;
     4205        size_t hdr_len = MIN_SMB_SIZE + VWV(12);
     4206        size_t max_len = max_pdu - hdr_len - 1 /* padding byte */;
     4207
     4208        /*
     4209         * Windows explicitly ignores upper size of 0xFFFF.
     4210         * See [MS-SMB].pdf <26> Section 2.2.4.2.1:
     4211         * We must do the same as these will never fit even in
     4212         * an extended size NetBIOS packet.
     4213         */
     4214        if (upper_size == 0xFFFF) {
     4215                upper_size = 0;
     4216        }
     4217
     4218        if (xconn->protocol < PROTOCOL_NT1) {
     4219                upper_size = 0;
     4220        }
     4221
     4222        total_size = ((upper_size<<16) | lower_size);
     4223
     4224        /*
     4225         * LARGE_READX test shows it's always safe to return
     4226         * a short read. Windows does so.
     4227         */
     4228        return MIN(total_size, max_len);
     4229}
     4230
     4231/****************************************************************************
    37274232 Reply to a read and X.
    37284233****************************************************************************/
     
    37324237        connection_struct *conn = req->conn;
    37334238        files_struct *fsp;
    3734         SMB_OFF_T startpos;
     4239        off_t startpos;
    37354240        size_t smb_maxcnt;
     4241        size_t upper_size;
    37364242        bool big_readX = False;
    37374243#if 0
     
    37684274        }
    37694275
    3770         if (global_client_caps & CAP_LARGE_READX) {
    3771                 size_t upper_size = SVAL(req->vwv+7, 0);
    3772                 smb_maxcnt |= (upper_size<<16);
    3773                 if (upper_size > 1) {
    3774                         /* Can't do this on a chained packet. */
    3775                         if ((CVAL(req->vwv+0, 0) != 0xFF)) {
    3776                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
    3777                                 END_PROFILE(SMBreadX);
    3778                                 return;
    3779                         }
    3780                         /* We currently don't do this on signed or sealed data. */
    3781                         if (srv_is_signing_active(req->sconn) || req->encrypted) {
    3782                                 reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
    3783                                 END_PROFILE(SMBreadX);
    3784                                 return;
    3785                         }
    3786                         /* Is there room in the reply for this data ? */
    3787                         if (smb_maxcnt > (0xFFFFFF - (smb_size -4 + 12*2)))  {
    3788                                 reply_nterror(req,
    3789                                               NT_STATUS_INVALID_PARAMETER);
    3790                                 END_PROFILE(SMBreadX);
    3791                                 return;
    3792                         }
    3793                         big_readX = True;
    3794                 }
     4276        upper_size = SVAL(req->vwv+7, 0);
     4277        smb_maxcnt = calc_read_size(req, upper_size, smb_maxcnt);
     4278        if (smb_maxcnt > (0x1FFFF - (MIN_SMB_SIZE + VWV(12)))) {
     4279                /*
     4280                 * This is a heuristic to avoid keeping large
     4281                 * outgoing buffers around over long-lived aio
     4282                 * requests.
     4283                 */
     4284                big_readX = True;
    37954285        }
    37964286
    37974287        if (req->wct == 12) {
    3798 #ifdef LARGE_SMB_OFF_T
    37994288                /*
    38004289                 * This is a large offset (64 bit) read.
    38014290                 */
    3802                 startpos |= (((SMB_OFF_T)IVAL(req->vwv+10, 0)) << 32);
    3803 
    3804 #else /* !LARGE_SMB_OFF_T */
    3805 
    3806                 /*
    3807                  * Ensure we haven't been sent a >32 bit offset.
    3808                  */
    3809 
    3810                 if(IVAL(req->vwv+10, 0) != 0) {
    3811                         DEBUG(0,("reply_read_and_X - large offset (%x << 32) "
    3812                                  "used and we don't support 64 bit offsets.\n",
    3813                                  (unsigned int)IVAL(req->vwv+10, 0) ));
    3814                         END_PROFILE(SMBreadX);
    3815                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
    3816                         return;
    3817                 }
    3818 
    3819 #endif /* LARGE_SMB_OFF_T */
     4291                startpos |= (((off_t)IVAL(req->vwv+10, 0)) << 32);
    38204292
    38214293        }
     
    38404312        }
    38414313
    3842         smbd_lock_socket(req->sconn);
     4314        smbd_lock_socket(req->xconn);
    38434315        send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
    3844         smbd_unlock_socket(req->sconn);
     4316        smbd_unlock_socket(req->xconn);
    38454317
    38464318 out:
     
    38554327void error_to_writebrawerr(struct smb_request *req)
    38564328{
    3857         uint8 *old_outbuf = req->outbuf;
     4329        uint8_t *old_outbuf = req->outbuf;
    38584330
    38594331        reply_outbuf(req, 1, 0);
     
    38734345                                size_t *len)
    38744346{
    3875         uint8_t msgtype = SMBkeepalive;
    3876 
    3877         while (msgtype == SMBkeepalive) {
     4347        uint8_t msgtype = NBSSkeepalive;
     4348
     4349        while (msgtype == NBSSkeepalive) {
    38784350                NTSTATUS status;
    38794351
     
    39074379{
    39084380        connection_struct *conn = req->conn;
     4381        struct smbXsrv_connection *xconn = req->xconn;
    39094382        char *buf = NULL;
    39104383        ssize_t nwritten=0;
     
    39124385        size_t numtowrite=0;
    39134386        size_t tcount;
    3914         SMB_OFF_T startpos;
    3915         char *data=NULL;
     4387        off_t startpos;
     4388        const char *data=NULL;
    39164389        bool write_through;
    39174390        files_struct *fsp;
     
    39264399         * we're finished.
    39274400         */
    3928         SCVAL(req->inbuf,smb_com,SMBwritec);
    3929 
    3930         if (srv_is_signing_active(req->sconn)) {
     4401        SCVAL(discard_const_p(uint8_t, req->inbuf),smb_com,SMBwritec);
     4402
     4403        if (srv_is_signing_active(xconn)) {
    39314404                END_PROFILE(SMBwritebraw);
    39324405                exit_server_cleanly("reply_writebraw: SMB signing is active - "
     
    39414414        }
    39424415
    3943         if (req->sconn->smb1.echo_handler.trusted_fde) {
     4416        if (xconn->smb1.echo_handler.trusted_fde) {
    39444417                DEBUG(2,("SMBwritebraw rejected with NOT_SUPPORTED because of "
    39454418                         "'async smb echo handler = yes'\n"));
     
    39724445
    39734446        if(get_Protocol() <= PROTOCOL_COREPLUS) {
    3974                 numtowrite = SVAL(smb_buf(req->inbuf),-2);
    3975                 data = smb_buf(req->inbuf);
     4447                numtowrite = SVAL(smb_buf_const(req->inbuf),-2);
     4448                data = smb_buf_const(req->inbuf);
    39764449        } else {
    39774450                numtowrite = SVAL(req->vwv+10, 0);
     
    40044477        }
    40054478
    4006         DEBUG(3,("reply_writebraw: initial write fnum=%d start=%.0f num=%d "
     4479        DEBUG(3, ("reply_writebraw: initial write %s start=%.0f num=%d "
    40074480                        "wrote=%d sync=%d\n",
    4008                 fsp->fnum, (double)startpos, (int)numtowrite,
     4481                fsp_fnum_dbg(fsp), (double)startpos, (int)numtowrite,
    40094482                (int)nwritten, (int)write_through));
    40104483
     
    40184491
    40194492        /* Allocate a buffer of 64k + length. */
    4020         buf = TALLOC_ARRAY(NULL, char, 65540);
     4493        buf = talloc_array(NULL, char, 65540);
    40214494        if (!buf) {
    40224495                reply_nterror(req, NT_STATUS_NO_MEMORY);
     
    40334506        SSVALS(buf,smb_vwv0,0xFFFF);
    40344507        show_msg(buf);
    4035         if (!srv_send_smb(req->sconn,
     4508        if (!srv_send_smb(req->xconn,
    40364509                          buf,
    40374510                          false, 0, /* no signing */
     
    40434516
    40444517        /* Now read the raw data into the buffer and write it */
    4045         status = read_smb_length(req->sconn->sock, buf, SMB_SECONDARY_WAIT,
     4518        status = read_smb_length(xconn->transport.sock, buf, SMB_SECONDARY_WAIT,
    40464519                                 &numtowrite);
    40474520        if (!NT_STATUS_IS_OK(status)) {
     
    40674540                }
    40684541
    4069                 status = read_data(req->sconn->sock, buf+4, numtowrite);
     4542                status = read_data_ntstatus(xconn->transport.sock, buf+4,
     4543                                            numtowrite);
    40704544
    40714545                if (!NT_STATUS_IS_OK(status)) {
    4072                         char addr[INET6_ADDRSTRLEN];
    40734546                        /* Try and give an error message
    40744547                         * saying what client failed. */
     
    40764549                                  "raw read failed (%s) for client %s. "
    40774550                                  "Terminating\n", nt_errstr(status),
    4078                                   get_peer_addr(req->sconn->sock, addr,
    4079                                                 sizeof(addr))));
     4551                                  smbXsrv_connection_dbg(xconn)));
    40804552                        exit_server_cleanly("secondary writebraw failed");
    40814553                }
     
    41114583        }
    41124584
    4113         DEBUG(3,("reply_writebraw: secondart write fnum=%d start=%.0f num=%d "
     4585        DEBUG(3,("reply_writebraw: secondart write %s start=%.0f num=%d "
    41144586                "wrote=%d\n",
    4115                 fsp->fnum, (double)startpos, (int)numtowrite,
     4587                fsp_fnum_dbg(fsp), (double)startpos, (int)numtowrite,
    41164588                (int)total_written));
    41174589
     
    41294601                /*
    41304602                 * Fix for "rabbit pellet" mode, trigger an early TCP ack by
    4131                  * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
     4603                 * sending a NBSSkeepalive. Thanks to DaveCB at Sun for this.
    41324604                 * JRA.
    41334605                 */
    4134                 if (!send_keepalive(req->sconn->sock)) {
     4606                if (!send_keepalive(xconn->transport.sock)) {
    41354607                        exit_server_cleanly("reply_writebraw: send of "
    41364608                                "keepalive failed");
     
    41624634        ssize_t nwritten = -1;
    41634635        size_t numtowrite;
    4164         SMB_OFF_T startpos;
     4636        off_t startpos;
    41654637        const char *data;
    41664638        NTSTATUS status = NT_STATUS_OK;
     
    42524724        SSVAL(req->outbuf,smb_vwv0,nwritten);
    42534725
    4254         DEBUG(3,("writeunlock fnum=%d num=%d wrote=%d\n",
    4255                  fsp->fnum, (int)numtowrite, (int)nwritten));
     4726        DEBUG(3, ("writeunlock %s num=%d wrote=%d\n",
     4727                  fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
    42564728
    42574729strict_unlock:
     
    42764748        size_t numtowrite;
    42774749        ssize_t nwritten = -1;
    4278         SMB_OFF_T startpos;
     4750        off_t startpos;
    42794751        const char *data;
    42804752        files_struct *fsp;
     
    43374809                 * This is actually an allocate call, and set EOF. JRA.
    43384810                 */
    4339                 nwritten = vfs_allocate_file_space(fsp, (SMB_OFF_T)startpos);
     4811                nwritten = vfs_allocate_file_space(fsp, (off_t)startpos);
    43404812                if (nwritten < 0) {
    43414813                        reply_nterror(req, NT_STATUS_DISK_FULL);
    43424814                        goto strict_unlock;
    43434815                }
    4344                 nwritten = vfs_set_filelen(fsp, (SMB_OFF_T)startpos);
     4816                nwritten = vfs_set_filelen(fsp, (off_t)startpos);
    43454817                if (nwritten < 0) {
    43464818                        reply_nterror(req, NT_STATUS_DISK_FULL);
     
    43794851        }
    43804852
    4381         DEBUG(3,("write fnum=%d num=%d wrote=%d\n", fsp->fnum, (int)numtowrite, (int)nwritten));
     4853        DEBUG(3, ("write %s num=%d wrote=%d\n", fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
    43824854
    43834855strict_unlock:
     
    43984870                                                1 /* pad byte */)
    43994871
    4400 bool is_valid_writeX_buffer(struct smbd_server_connection *sconn,
     4872bool is_valid_writeX_buffer(struct smbXsrv_connection *xconn,
    44014873                            const uint8_t *inbuf)
    44024874{
    44034875        size_t numtowrite;
    4404         connection_struct *conn = NULL;
    44054876        unsigned int doff = 0;
    44064877        size_t len = smb_len_large(inbuf);
     4878        uint16_t fnum;
     4879        struct smbXsrv_open *op = NULL;
     4880        struct files_struct *fsp = NULL;
     4881        NTSTATUS status;
    44074882
    44084883        if (is_encrypted_packet(inbuf)) {
     
    44234898        }
    44244899
    4425         conn = conn_find(sconn, SVAL(inbuf, smb_tid));
    4426         if (conn == NULL) {
    4427                 DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
     4900        fnum = SVAL(inbuf, smb_vwv2);
     4901        status = smb1srv_open_lookup(xconn,
     4902                                     fnum,
     4903                                     0, /* now */
     4904                                     &op);
     4905        if (!NT_STATUS_IS_OK(status)) {
     4906                DEBUG(10,("is_valid_writeX_buffer: bad fnum\n"));
    44284907                return false;
    44294908        }
    4430         if (IS_IPC(conn)) {
     4909        fsp = op->compat;
     4910        if (fsp == NULL) {
     4911                DEBUG(10,("is_valid_writeX_buffer: bad fsp\n"));
     4912                return false;
     4913        }
     4914        if (fsp->conn == NULL) {
     4915                DEBUG(10,("is_valid_writeX_buffer: bad fsp->conn\n"));
     4916                return false;
     4917        }
     4918
     4919        if (IS_IPC(fsp->conn)) {
    44314920                DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
    44324921                return false;
    44334922        }
    4434         if (IS_PRINT(conn)) {
     4923        if (IS_PRINT(fsp->conn)) {
    44354924                DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
    44364925                return false;
     
    44834972{
    44844973        connection_struct *conn = req->conn;
     4974        struct smbXsrv_connection *xconn = req->xconn;
    44854975        files_struct *fsp;
    44864976        struct lock_struct lock;
    4487         SMB_OFF_T startpos;
     4977        off_t startpos;
    44884978        size_t numtowrite;
    44894979        bool write_through;
     
    44914981        unsigned int smb_doff;
    44924982        unsigned int smblen;
    4493         char *data;
     4983        const char *data;
    44944984        NTSTATUS status;
    44954985        int saved_errno = 0;
     
    44994989        if ((req->wct != 12) && (req->wct != 14)) {
    45004990                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
    4501                 END_PROFILE(SMBwriteX);
    4502                 return;
     4991                goto out;
    45034992        }
    45044993
     
    45175006                if (IS_IPC(conn)) {
    45185007                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
    4519                         END_PROFILE(SMBwriteX);
    4520                         return;
     5008                        goto out;
    45215009                }
    45225010                if (numtowrite != req->unread_bytes) {
    45235011                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
    4524                         END_PROFILE(SMBwriteX);
    4525                         return;
     5012                        goto out;
    45265013                }
    45275014        } else {
     
    45295016                                smb_doff + numtowrite > smblen) {
    45305017                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
    4531                         END_PROFILE(SMBwriteX);
    4532                         return;
     5018                        goto out;
    45335019                }
    45345020        }
     
    45385024                if (req->unread_bytes) {
    45395025                        reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
    4540                         END_PROFILE(SMBwriteX);
    4541                         return;
     5026                        goto out;
    45425027                }
    45435028                reply_pipe_write_and_X(req);
    4544                 END_PROFILE(SMBwriteX);
    4545                 return;
     5029                goto out;
    45465030        }
    45475031
     
    45515035
    45525036        if (!check_fsp(conn, req, fsp)) {
    4553                 END_PROFILE(SMBwriteX);
    4554                 return;
     5037                goto out;
    45555038        }
    45565039
    45575040        if (!CHECK_WRITE(fsp)) {
    45585041                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
    4559                 END_PROFILE(SMBwriteX);
    4560                 return;
     5042                goto out;
    45615043        }
    45625044
     
    45645046
    45655047        if(req->wct == 14) {
    4566 #ifdef LARGE_SMB_OFF_T
    45675048                /*
    45685049                 * This is a large offset (64 bit) write.
    45695050                 */
    4570                 startpos |= (((SMB_OFF_T)IVAL(req->vwv+12, 0)) << 32);
    4571 
    4572 #else /* !LARGE_SMB_OFF_T */
    4573 
    4574                 /*
    4575                  * Ensure we haven't been sent a >32 bit offset.
    4576                  */
    4577 
    4578                 if(IVAL(req->vwv+12, 0) != 0) {
    4579                         DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
    4580                                  "used and we don't support 64 bit offsets.\n",
    4581                                  (unsigned int)IVAL(req->vwv+12, 0) ));
    4582                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
    4583                         END_PROFILE(SMBwriteX);
    4584                         return;
    4585                 }
    4586 
    4587 #endif /* LARGE_SMB_OFF_T */
     5051                startpos |= (((off_t)IVAL(req->vwv+12, 0)) << 32);
     5052
    45885053        }
    45895054
     
    46425107
    46435108        reply_outbuf(req, 6, 0);
     5109        SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
     5110        SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
    46445111        SSVAL(req->outbuf,smb_vwv2,nwritten);
    46455112        SSVAL(req->outbuf,smb_vwv4,nwritten>>16);
    46465113
    4647         DEBUG(3,("writeX fnum=%d num=%d wrote=%d\n",
    4648                 fsp->fnum, (int)numtowrite, (int)nwritten));
     5114        DEBUG(3,("writeX %s num=%d wrote=%d\n",
     5115                fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten));
    46495116
    46505117        status = sync_file(conn, fsp, write_through);
     
    46575124
    46585125        END_PROFILE(SMBwriteX);
    4659         chain_reply(req);
    46605126        return;
    46615127
    46625128out:
     5129        if (req->unread_bytes) {
     5130                /* writeX failed. drain socket. */
     5131                if (drain_socket(xconn->transport.sock, req->unread_bytes) !=
     5132                                req->unread_bytes) {
     5133                        smb_panic("failed to drain pending bytes");
     5134                }
     5135                req->unread_bytes = 0;
     5136        }
     5137
    46635138        END_PROFILE(SMBwriteX);
    46645139        return;
     
    46725147{
    46735148        connection_struct *conn = req->conn;
    4674         SMB_OFF_T startpos;
    4675         SMB_OFF_T res= -1;
     5149        off_t startpos;
     5150        off_t res= -1;
    46765151        int mode,umode;
    46775152        files_struct *fsp;
     
    46915166        }
    46925167
    4693         flush_write_cache(fsp, SEEK_FLUSH);
     5168        flush_write_cache(fsp, SAMBA_SEEK_FLUSH);
    46945169
    46955170        mode = SVAL(req->vwv+1, 0) & 3;
    46965171        /* NB. This doesn't use IVAL_TO_SMB_OFF_T as startpos can be signed in this case. */
    4697         startpos = (SMB_OFF_T)IVALS(req->vwv+2, 0);
     5172        startpos = (off_t)IVALS(req->vwv+2, 0);
    46985173
    46995174        switch (mode) {
     
    47185193                if((res = SMB_VFS_LSEEK(fsp,startpos,umode)) == -1) {
    47195194                        if(errno == EINVAL) {
    4720                                 SMB_OFF_T current_pos = startpos;
     5195                                off_t current_pos = startpos;
    47215196
    47225197                                if(fsp_stat(fsp) == -1) {
     
    47455220        SIVAL(req->outbuf,smb_vwv0,res);
    47465221
    4747         DEBUG(3,("lseek fnum=%d ofs=%.0f newpos = %.0f mode=%d\n",
    4748                 fsp->fnum, (double)startpos, (double)res, mode));
     5222        DEBUG(3,("lseek %s ofs=%.0f newpos = %.0f mode=%d\n",
     5223                fsp_fnum_dbg(fsp), (double)startpos, (double)res, mode));
    47495224
    47505225        END_PROFILE(SMBlseek);
     
    47595234{
    47605235        connection_struct *conn = req->conn;
    4761         uint16 fnum;
     5236        uint16_t fnum;
    47625237        files_struct *fsp;
    47635238
     
    48155290}
    48165291
    4817 /****************************************************************************
    4818  Reply to a close - has to deal with closing a directory opened by NT SMB's.
    4819 ****************************************************************************/
     5292struct reply_close_state {
     5293        files_struct *fsp;
     5294        struct smb_request *smbreq;
     5295};
     5296
     5297static void do_smb1_close(struct tevent_req *req);
    48205298
    48215299void reply_close(struct smb_request *req)
     
    48445322        }
    48455323
    4846         if(fsp->is_directory) {
    4847                 /*
    4848                  * Special case - close NT SMB directory handle.
    4849                  */
    4850                 DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
    4851                 status = close_file(req, fsp, NORMAL_CLOSE);
    4852         } else {
     5324        DEBUG(3, ("Close %s fd=%d %s (numopen=%d)\n",
     5325                  fsp->is_directory ? "directory" : "file",
     5326                  fsp->fh->fd, fsp_fnum_dbg(fsp),
     5327                  conn->num_files_open));
     5328
     5329        if (!fsp->is_directory) {
    48535330                time_t t;
    4854                 /*
    4855                  * Close ordinary file.
    4856                  */
    4857 
    4858                 DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
    4859                          fsp->fh->fd, fsp->fnum,
    4860                          conn->num_files_open));
    48615331
    48625332                /*
     
    48665336                t = srv_make_unix_date3(req->vwv+1);
    48675337                set_close_write_time(fsp, convert_time_t_to_timespec(t));
     5338        }
     5339
     5340        if (fsp->num_aio_requests != 0) {
     5341
     5342                struct reply_close_state *state;
     5343
     5344                DEBUG(10, ("closing with aio %u requests pending\n",
     5345                           fsp->num_aio_requests));
    48685346
    48695347                /*
    4870                  * close_file() returns the unix errno if an error
    4871                  * was detected on close - normally this is due to
    4872                  * a disk full error. If not then it was probably an I/O error.
     5348                 * We depend on the aio_extra destructor to take care of this
     5349                 * close request once fsp->num_aio_request drops to 0.
    48735350                 */
    48745351
    4875                 status = close_file(req, fsp, NORMAL_CLOSE);
    4876         } 
    4877 
     5352                fsp->deferred_close = tevent_wait_send(
     5353                        fsp, fsp->conn->sconn->ev_ctx);
     5354                if (fsp->deferred_close == NULL) {
     5355                        status = NT_STATUS_NO_MEMORY;
     5356                        goto done;
     5357                }
     5358
     5359                state = talloc(fsp, struct reply_close_state);
     5360                if (state == NULL) {
     5361                        TALLOC_FREE(fsp->deferred_close);
     5362                        status = NT_STATUS_NO_MEMORY;
     5363                        goto done;
     5364                }
     5365                state->fsp = fsp;
     5366                state->smbreq = talloc_move(fsp, &req);
     5367                tevent_req_set_callback(fsp->deferred_close, do_smb1_close,
     5368                                        state);
     5369                END_PROFILE(SMBclose);
     5370                return;
     5371        }
     5372
     5373        /*
     5374         * close_file() returns the unix errno if an error was detected on
     5375         * close - normally this is due to a disk full error. If not then it
     5376         * was probably an I/O error.
     5377         */
     5378
     5379        status = close_file(req, fsp, NORMAL_CLOSE);
     5380done:
    48785381        if (!NT_STATUS_IS_OK(status)) {
    48795382                reply_nterror(req, status);
     
    48855388        END_PROFILE(SMBclose);
    48865389        return;
     5390}
     5391
     5392static void do_smb1_close(struct tevent_req *req)
     5393{
     5394        struct reply_close_state *state = tevent_req_callback_data(
     5395                req, struct reply_close_state);
     5396        struct smb_request *smbreq;
     5397        NTSTATUS status;
     5398        int ret;
     5399
     5400        ret = tevent_wait_recv(req);
     5401        TALLOC_FREE(req);
     5402        if (ret != 0) {
     5403                DEBUG(10, ("tevent_wait_recv returned %s\n",
     5404                           strerror(ret)));
     5405                /*
     5406                 * Continue anyway, this should never happen
     5407                 */
     5408        }
     5409
     5410        /*
     5411         * fsp->smb2_close_request right now is a talloc grandchild of
     5412         * fsp. When we close_file(fsp), it would go with it. No chance to
     5413         * reply...
     5414         */
     5415        smbreq = talloc_move(talloc_tos(), &state->smbreq);
     5416
     5417        status = close_file(smbreq, state->fsp, NORMAL_CLOSE);
     5418        if (NT_STATUS_IS_OK(status)) {
     5419                reply_outbuf(smbreq, 0, 0);
     5420        } else {
     5421                reply_nterror(smbreq, status);
     5422        }
     5423        if (!srv_send_smb(smbreq->xconn,
     5424                        (char *)smbreq->outbuf,
     5425                        true,
     5426                        smbreq->seqnum+1,
     5427                        IS_CONN_ENCRYPTED(smbreq->conn)||smbreq->encrypted,
     5428                        NULL)) {
     5429                exit_server_cleanly("handle_aio_read_complete: srv_send_smb "
     5430                                    "failed.");
     5431        }
     5432        TALLOC_FREE(smbreq);
    48875433}
    48885434
     
    48975443        ssize_t nwritten = -1;
    48985444        NTSTATUS close_status = NT_STATUS_OK;
    4899         SMB_OFF_T startpos;
     5445        off_t startpos;
    49005446        const char *data;
    49015447        struct timespec mtime;
     
    49285474        data = (const char *)req->buf + 1;
    49295475
    4930         if (!fsp->print_file) {
     5476        if (fsp->print_file == NULL) {
    49315477                init_strict_lock_struct(fsp, (uint64_t)req->smbpid,
    49325478                    (uint64_t)startpos, (uint64_t)numtowrite, WRITE_LOCK,
     
    49425488        nwritten = write_file(req,fsp,data,startpos,numtowrite);
    49435489
     5490        if (fsp->print_file == NULL) {
     5491                SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
     5492        }
     5493
    49445494        set_close_write_time(fsp, mtime);
    49455495
     
    49495499         */
    49505500
     5501        DEBUG(3,("writeclose %s num=%d wrote=%d (numopen=%d)\n",
     5502                fsp_fnum_dbg(fsp), (int)numtowrite, (int)nwritten,
     5503                (numtowrite) ? conn->num_files_open - 1 : conn->num_files_open));
     5504
    49515505        if (numtowrite) {
    49525506                DEBUG(3,("reply_writeclose: zero length write doesn't close "
    49535507                         "file %s\n", fsp_str_dbg(fsp)));
    49545508                close_status = close_file(req, fsp, NORMAL_CLOSE);
    4955         }
    4956 
    4957         DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
    4958                  fsp->fnum, (int)numtowrite, (int)nwritten,
    4959                  conn->num_files_open));
     5509                fsp = NULL;
     5510        }
    49605511
    49615512        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
    49625513                reply_nterror(req, NT_STATUS_DISK_FULL);
    4963                 goto strict_unlock;
     5514                goto out;
    49645515        }
    49655516
    49665517        if(!NT_STATUS_IS_OK(close_status)) {
    49675518                reply_nterror(req, close_status);
    4968                 goto strict_unlock;
     5519                goto out;
    49695520        }
    49705521
     
    49735524        SSVAL(req->outbuf,smb_vwv0,nwritten);
    49745525
    4975 strict_unlock:
    4976         if (numtowrite && !fsp->print_file) {
    4977                 SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
    4978         }
     5526out:
    49795527
    49805528        END_PROFILE(SMBwriteclose);
     
    50155563        offset = (uint64_t)IVAL(req->vwv+3, 0);
    50165564
    5017         DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
    5018                  fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
     5565        DEBUG(3,("lock fd=%d %s offset=%.0f count=%.0f\n",
     5566                 fsp->fh->fd, fsp_fnum_dbg(fsp), (double)offset, (double)count));
    50195567
    50205568        br_lck = do_lock(req->sconn->msg_ctx,
     
    50275575                        False, /* Non-blocking lock. */
    50285576                        &status,
    5029                         NULL,
    50305577                        NULL);
    50315578
     
    50865633        }
    50875634
    5088         DEBUG( 3, ( "unlock fd=%d fnum=%d offset=%.0f count=%.0f\n",
    5089                     fsp->fh->fd, fsp->fnum, (double)offset, (double)count ) );
     5635        DEBUG( 3, ( "unlock fd=%d %s offset=%.0f count=%.0f\n",
     5636                    fsp->fh->fd, fsp_fnum_dbg(fsp), (double)offset, (double)count ) );
    50905637
    50915638        reply_outbuf(req, 0, 0);
     
    51055652void reply_tdis(struct smb_request *req)
    51065653{
     5654        NTSTATUS status;
    51075655        connection_struct *conn = req->conn;
     5656        struct smbXsrv_tcon *tcon;
     5657
    51085658        START_PROFILE(SMBtdis);
    51095659
    51105660        if (!conn) {
    51115661                DEBUG(4,("Invalid connection in tdis\n"));
    5112                 reply_nterror(req, NT_STATUS_NETWORK_NAME_DELETED);
     5662                reply_force_doserror(req, ERRSRV, ERRinvnid);
    51135663                END_PROFILE(SMBtdis);
    51145664                return;
    51155665        }
    51165666
    5117         conn->used = False;
    5118 
    5119         close_cnum(conn,req->vuid);
     5667        tcon = conn->tcon;
    51205668        req->conn = NULL;
     5669
     5670        /*
     5671         * TODO: cancel all outstanding requests on the tcon
     5672         */
     5673        status = smbXsrv_tcon_disconnect(tcon, req->vuid);
     5674        if (!NT_STATUS_IS_OK(status)) {
     5675                DEBUG(0, ("reply_tdis: "
     5676                          "smbXsrv_tcon_disconnect() failed: %s\n",
     5677                          nt_errstr(status)));
     5678                /*
     5679                 * If we hit this case, there is something completely
     5680                 * wrong, so we better disconnect the transport connection.
     5681                 */
     5682                END_PROFILE(SMBtdis);
     5683                exit_server(__location__ ": smbXsrv_tcon_disconnect failed");
     5684                return;
     5685        }
     5686
     5687        TALLOC_FREE(tcon);
    51215688
    51225689        reply_outbuf(req, 0, 0);
     
    51755742
    51765743                show_msg((char *)req->outbuf);
    5177                 if (!srv_send_smb(req->sconn,
     5744                if (!srv_send_smb(req->xconn,
    51785745                                (char *)req->outbuf,
    51795746                                true, req->seqnum+1,
     
    52355802        SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
    52365803
    5237         DEBUG(3,("openprint fd=%d fnum=%d\n",
    5238                  fsp->fh->fd, fsp->fnum));
     5804        DEBUG(3,("openprint fd=%d %s\n",
     5805                 fsp->fh->fd, fsp_fnum_dbg(fsp)));
    52395806
    52405807        END_PROFILE(SMBsplopen);
     
    52735840        }
    52745841
    5275         DEBUG(3,("printclose fd=%d fnum=%d\n",
    5276                  fsp->fh->fd,fsp->fnum));
     5842        DEBUG(3,("printclose fd=%d %s\n",
     5843                 fsp->fh->fd, fsp_fnum_dbg(fsp)));
    52775844
    52785845        status = close_file(req, fsp, NORMAL_CLOSE);
     
    53345901                NTSTATUS status;
    53355902                WERROR werr;
    5336                 const char *sharename = lp_servicename(SNUM(conn));
     5903                const char *sharename = lp_servicename(mem_ctx, SNUM(conn));
    53375904                struct rpc_pipe_client *cli = NULL;
    53385905                struct dcerpc_binding_handle *b = NULL;
     
    53485915
    53495916                status = rpc_pipe_open_interface(conn,
    5350                                                  &ndr_table_spoolss.syntax_id,
     5917                                                 &ndr_table_spoolss,
    53515918                                                 conn->session_info,
    5352                                                  &conn->sconn->client_id,
     5919                                                 conn->sconn->remote_address,
    53535920                                                 conn->sconn->msg_ctx,
    53545921                                                 &cli);
     
    54095976                        time_t qtime = spoolss_Time_to_time_t(&info[i].info2.submitted);
    54105977                        int qstatus;
     5978                        size_t len = 0;
    54115979                        uint16_t qrapjobid = pjobid_to_rap(sharename,
    54125980                                                        info[i].info2.job_id);
     
    54235991                        SIVAL(p, 7, info[i].info2.size);
    54245992                        SCVAL(p, 11, 0);
    5425                         srvstr_push(blob, req->flags2, p+12,
    5426                                     info[i].info2.notify_name, 16, STR_ASCII);
    5427 
     5993                        status = srvstr_push(blob, req->flags2, p+12,
     5994                                    info[i].info2.notify_name, 16, STR_ASCII, &len);
     5995                        if (!NT_STATUS_IS_OK(status)) {
     5996                                reply_nterror(req, status);
     5997                                goto out;
     5998                        }
    54285999                        if (message_push_blob(
    54296000                                    &req->outbuf,
     
    55066077        data = (const char *)req->buf + 3;
    55076078
    5508         if (write_file(req,fsp,data,(SMB_OFF_T)-1,numtowrite) != numtowrite) {
     6079        if (write_file(req,fsp,data,(off_t)-1,numtowrite) != numtowrite) {
    55096080                reply_nterror(req, map_nt_error_from_unix(errno));
    55106081                END_PROFILE(SMBsplwr);
     
    55126083        }
    55136084
    5514         DEBUG( 3, ( "printwrite fnum=%d num=%d\n", fsp->fnum, numtowrite ) );
     6085        DEBUG(3, ("printwrite %s num=%d\n", fsp_fnum_dbg(fsp), numtowrite));
    55156086
    55166087        END_PROFILE(SMBsplwr);
     
    55286099        char *directory = NULL;
    55296100        NTSTATUS status;
     6101        uint32_t ucf_flags = UCF_PREP_CREATEFILE |
     6102                        (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    55306103        TALLOC_CTX *ctx = talloc_tos();
    55316104
     
    55426115                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
    55436116                                 directory,
    5544                                  UCF_CREATING_FILE,
     6117                                 ucf_flags,
    55456118                                 NULL,
    55466119                                 &smb_dname);
     
    55986171        files_struct *fsp = NULL;
    55996172        int info = 0;
     6173        uint32_t ucf_flags = (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    56006174        struct smbd_server_connection *sconn = req->sconn;
    56016175
     
    56126186                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
    56136187                                 directory,
    5614                                  0,
     6188                                 ucf_flags,
    56156189                                 NULL,
    56166190                                 &smb_dname);
     
    56426216                FILE_ATTRIBUTE_DIRECTORY,               /* file_attributes */
    56436217                0,                                      /* oplock_request */
     6218                NULL,                                   /* lease */
    56446219                0,                                      /* allocation_size */
    56456220                0,                                      /* private_flags */
     
    56476222                NULL,                                   /* ea_list */
    56486223                &fsp,                                   /* result */
    5649                 &info);                                 /* pinfo */
     6224                &info,                                  /* pinfo */
     6225                NULL, NULL);                            /* create context */
    56506226
    56516227        if (!NT_STATUS_IS_OK(status)) {
    5652                 if (open_was_deferred(req->mid)) {
     6228                if (open_was_deferred(req->xconn, req->mid)) {
    56536229                        /* We have re-scheduled this call. */
    56546230                        goto out;
     
    56676243        if (!set_delete_on_close(fsp, true,
    56686244                        conn->session_info->security_token,
    5669                         &conn->session_info->utok)) {
     6245                        conn->session_info->unix_token)) {
    56706246                close_file(req, fsp, ERROR_CLOSE);
    56716247                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
     
    58236399static void rename_open_files(connection_struct *conn,
    58246400                              struct share_mode_lock *lck,
     6401                              struct file_id id,
    58256402                              uint32_t orig_name_hash,
    58266403                              const struct smb_filename *smb_fname_dst)
     
    58296406        bool did_rename = False;
    58306407        NTSTATUS status;
    5831         uint32_t new_name_hash;
    5832 
    5833         for(fsp = file_find_di_first(conn->sconn, lck->id); fsp;
     6408        uint32_t new_name_hash = 0;
     6409
     6410        for(fsp = file_find_di_first(conn->sconn, id); fsp;
    58346411            fsp = file_find_di_next(fsp)) {
    58356412                /* fsp_name is a relative path under the fsp. To change this for other
     
    58436420                        continue;
    58446421                }
    5845                 DEBUG(10, ("rename_open_files: renaming file fnum %d "
    5846                            "(file_id %s) from %s -> %s\n", fsp->fnum,
     6422                DEBUG(10, ("rename_open_files: renaming file %s "
     6423                           "(file_id %s) from %s -> %s\n", fsp_fnum_dbg(fsp),
    58476424                           file_id_string_tos(&fsp->file_id), fsp_str_dbg(fsp),
    58486425                           smb_fname_str_dbg(smb_fname_dst)));
     
    58576434        if (!did_rename) {
    58586435                DEBUG(10, ("rename_open_files: no open files on file_id %s "
    5859                            "for %s\n", file_id_string_tos(&lck->id),
     6436                           "for %s\n", file_id_string_tos(&id),
    58606437                           smb_fname_str_dbg(smb_fname_dst)));
    58616438        }
    58626439
    58636440        /* Send messages to all smbd's (not ourself) that the name has changed. */
    5864         rename_share_filename(conn->sconn->msg_ctx, lck, conn->connectpath,
     6441        rename_share_filename(conn->sconn->msg_ctx, lck, id, conn->connectpath,
    58656442                              orig_name_hash, new_name_hash,
    58666443                              smb_fname_dst);
     
    59086485        char *parent_dir_src = NULL;
    59096486        char *parent_dir_dst = NULL;
    5910         uint32 mask;
     6487        uint32_t mask;
    59116488
    59126489        mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
     
    59956572                        files_struct *fsp,
    59966573                        const struct smb_filename *smb_fname_dst_in,
    5997                         uint32 attrs,
     6574                        uint32_t attrs,
    59986575                        bool replace_if_exists)
    59996576{
     
    60166593        /* Make a copy of the dst smb_fname structs */
    60176594
    6018         status = copy_smb_filename(ctx, smb_fname_dst_in, &smb_fname_dst);
    6019         if (!NT_STATUS_IS_OK(status)) {
     6595        smb_fname_dst = cp_smb_filename(ctx, smb_fname_dst_in);
     6596        if (smb_fname_dst == NULL) {
     6597                status = NT_STATUS_NO_MEMORY;
    60206598                goto out;
    60216599        }
     
    60236601        /*
    60246602         * Check for special case with case preserving and not
    6025          * case sensitive. If the old last component differs from the original
     6603         * case sensitive. If the new last component differs from the original
    60266604         * last component only by case, then we should allow
    60276605         * the rename (user is trying to change the case of the
    60286606         * filename).
    60296607         */
    6030         if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
     6608        if (!conn->case_sensitive && conn->case_preserve &&
    60316609            strequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
    60326610            strequal(fsp->fsp_name->stream_name, smb_fname_dst->stream_name)) {
     
    60536631                 * component of the destination.
    60546632                 */
    6055                 status = create_synthetic_smb_fname_split(ctx,
    6056                     smb_fname_dst->original_lcomp, NULL,
    6057                     &smb_fname_orig_lcomp);
    6058                 if (!NT_STATUS_IS_OK(status)) {
     6633                smb_fname_orig_lcomp = synthetic_smb_fname_split(
     6634                        ctx, smb_fname_dst->original_lcomp, NULL);
     6635                if (smb_fname_orig_lcomp == NULL) {
     6636                        status = NT_STATUS_NO_MEMORY;
    60596637                        TALLOC_FREE(fname_dst_lcomp_base_mod);
    60606638                        goto out;
     
    61836761        }
    61846762
    6185         lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
    6186                                   NULL);
     6763        lck = get_existing_share_mode_lock(talloc_tos(), fsp->file_id);
    61876764
    61886765        /*
     
    61946771
    61956772        if(SMB_VFS_RENAME(conn, fsp->fsp_name, smb_fname_dst) == 0) {
    6196                 uint32 create_options = fsp->fh->private_options;
     6773                uint32_t create_options = fsp->fh->private_options;
    61976774
    61986775                DEBUG(3, ("rename_internals_fsp: succeeded doing rename on "
     
    62206797                              smb_fname_dst);
    62216798
    6222                 rename_open_files(conn, lck, fsp->name_hash, smb_fname_dst);
     6799                rename_open_files(conn, lck, fsp->file_id, fsp->name_hash,
     6800                                  smb_fname_dst);
    62236801
    62246802                /*
     
    62736851                        struct smb_filename *smb_fname_src,
    62746852                        struct smb_filename *smb_fname_dst,
    6275                         uint32 attrs,
     6853                        uint32_t attrs,
    62766854                        bool replace_if_exists,
    62776855                        bool src_has_wild,
     
    62886866        long offset = 0;
    62896867        int create_options = 0;
    6290         bool posix_pathnames = lp_posix_pathnames();
     6868        bool posix_pathnames = (req != NULL && req->posix_pathnames);
     6869        int rc;
    62916870
    62926871        /*
     
    63816960                ZERO_STRUCT(smb_fname_src->st);
    63826961                if (posix_pathnames) {
    6383                         SMB_VFS_LSTAT(conn, smb_fname_src);
     6962                        rc = SMB_VFS_LSTAT(conn, smb_fname_src);
    63846963                } else {
    6385                         SMB_VFS_STAT(conn, smb_fname_src);
     6964                        rc = SMB_VFS_STAT(conn, smb_fname_src);
     6965                }
     6966                if (rc == -1) {
     6967                        status = map_nt_error_from_unix_common(errno);
     6968                        goto out;
    63866969                }
    63876970
     
    64026985                        posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
    64036986                        0,                              /* oplock_request */
     6987                        NULL,                           /* lease */
    64046988                        0,                              /* allocation_size */
    64056989                        0,                              /* private_flags */
     
    64076991                        NULL,                           /* ea_list */
    64086992                        &fsp,                           /* result */
    6409                         NULL);                          /* pinfo */
     6993                        NULL,                           /* pinfo */
     6994                        NULL, NULL);                    /* create context */
    64106995
    64116996                if (!NT_STATUS_IS_OK(status)) {
     
    65487133                        posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
    65497134                        0,                              /* oplock_request */
     7135                        NULL,                           /* lease */
    65507136                        0,                              /* allocation_size */
    65517137                        0,                              /* private_flags */
     
    65537139                        NULL,                           /* ea_list */
    65547140                        &fsp,                           /* result */
    6555                         NULL);                          /* pinfo */
     7141                        NULL,                           /* pinfo */
     7142                        NULL, NULL);                    /* create context */
    65567143
    65577144                if (!NT_STATUS_IS_OK(status)) {
     
    66147201        char *newname = NULL;
    66157202        const char *p;
    6616         uint32 attrs;
     7203        uint32_t attrs;
    66177204        NTSTATUS status;
    66187205        bool src_has_wcard = False;
     
    66217208        struct smb_filename *smb_fname_src = NULL;
    66227209        struct smb_filename *smb_fname_dst = NULL;
    6623         uint32_t src_ucf_flags = lp_posix_pathnames() ? UCF_UNIX_NAME_LOOKUP : UCF_COND_ALLOW_WCARD_LCOMP;
    6624         uint32_t dst_ucf_flags = UCF_SAVE_LCOMP | (lp_posix_pathnames() ? 0 : UCF_COND_ALLOW_WCARD_LCOMP);
     7210        uint32_t src_ucf_flags = (req->posix_pathnames ?
     7211                (UCF_UNIX_NAME_LOOKUP|UCF_POSIX_PATHNAMES) :
     7212                UCF_COND_ALLOW_WCARD_LCOMP);
     7213        uint32_t dst_ucf_flags = UCF_SAVE_LCOMP |
     7214                (req->posix_pathnames ? UCF_POSIX_PATHNAMES :
     7215                 UCF_COND_ALLOW_WCARD_LCOMP);
    66257216        bool stream_rename = false;
    66267217
     
    66497240        }
    66507241
    6651         if (!lp_posix_pathnames()) {
     7242        if (!req->posix_pathnames) {
    66527243                /* The newname must begin with a ':' if the
    66537244                   name contains a ':'. */
     
    67167307                                  DELETE_ACCESS);
    67177308        if (!NT_STATUS_IS_OK(status)) {
    6718                 if (open_was_deferred(req->mid)) {
     7309                if (open_was_deferred(req->xconn, req->mid)) {
    67197310                        /* We have re-scheduled this call. */
    67207311                        goto out;
     
    67497340{
    67507341        struct smb_filename *smb_fname_dst_tmp = NULL;
    6751         SMB_OFF_T ret=-1;
     7342        off_t ret=-1;
    67527343        files_struct *fsp1,*fsp2;
    6753         uint32 dosattrs;
    6754         uint32 new_create_disposition;
     7344        uint32_t dosattrs;
     7345        uint32_t new_create_disposition;
    67557346        NTSTATUS status;
    67567347
    67577348
    6758         status = copy_smb_filename(ctx, smb_fname_dst, &smb_fname_dst_tmp);
    6759         if (!NT_STATUS_IS_OK(status)) {
    6760                 return status;
     7349        smb_fname_dst_tmp = cp_smb_filename(ctx, smb_fname_dst);
     7350        if (smb_fname_dst_tmp == NULL) {
     7351                return NT_STATUS_NO_MEMORY;
    67617352        }
    67627353
     
    68177408                FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
    68187409                INTERNAL_OPEN_ONLY,                     /* oplock_request */
     7410                NULL,                                   /* lease */
    68197411                0,                                      /* allocation_size */
    68207412                0,                                      /* private_flags */
     
    68227414                NULL,                                   /* ea_list */
    68237415                &fsp1,                                  /* result */
    6824                 NULL);                                  /* psbuf */
     7416                NULL,                                   /* psbuf */
     7417                NULL, NULL);                            /* create context */
    68257418
    68267419        if (!NT_STATUS_IS_OK(status)) {
     
    68467439                dosattrs,                               /* file_attributes */
    68477440                INTERNAL_OPEN_ONLY,                     /* oplock_request */
     7441                NULL,                                   /* lease */
    68487442                0,                                      /* allocation_size */
    68497443                0,                                      /* private_flags */
     
    68517445                NULL,                                   /* ea_list */
    68527446                &fsp2,                                  /* result */
    6853                 NULL);                                  /* psbuf */
     7447                NULL,                                   /* psbuf */
     7448                NULL, NULL);                            /* create context */
    68547449
    68557450        if (!NT_STATUS_IS_OK(status)) {
     
    68947489        }
    68957490
    6896         if (ret != (SMB_OFF_T)smb_fname_src->st.st_ex_size) {
     7491        if (ret != (off_t)smb_fname_src->st.st_ex_size) {
    68977492                status = NT_STATUS_DISK_FULL;
    68987493                goto out;
     
    69297524        bool dest_has_wild = False;
    69307525        NTSTATUS status;
     7526        uint32_t ucf_flags_src = UCF_COND_ALLOW_WCARD_LCOMP |
     7527                (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
     7528        uint32_t ucf_flags_dst = UCF_COND_ALLOW_WCARD_LCOMP |
     7529                (req->posix_pathnames ? UCF_POSIX_PATHNAMES : 0);
    69317530        TALLOC_CTX *ctx = talloc_tos();
    69327531
     
    69687567                                  req->flags2 & FLAGS2_DFS_PATHNAMES,
    69697568                                  fname_src,
    6970                                   UCF_COND_ALLOW_WCARD_LCOMP,
     7569                                  ucf_flags_src,
    69717570                                  &source_has_wild,
    69727571                                  &smb_fname_src);
     
    69847583                                  req->flags2 & FLAGS2_DFS_PATHNAMES,
    69857584                                  fname_dst,
    6986                                   UCF_COND_ALLOW_WCARD_LCOMP,
     7585                                  ucf_flags_dst,
    69877586                                  &dest_has_wild,
    69887587                                  &smb_fname_dst);
     
    72837882                count = (uint64_t)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
    72847883        } else {
    7285 
    7286 #if defined(HAVE_LONGLONG)
     7884                /*
     7885                 * No BVAL, this is reversed!
     7886                 */
    72877887                count = (((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
    72887888                        ((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
    7289 #else /* HAVE_LONGLONG */
    7290 
    7291                 /*
    7292                  * NT4.x seems to be broken in that it sends large file (64 bit)
    7293                  * lockingX calls even if the CAP_LARGE_FILES was *not*
    7294                  * negotiated. For boxes without large unsigned ints truncate the
    7295                  * lock count by dropping the top 32 bits.
    7296                  */
    7297 
    7298                 if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
    7299                         DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
    7300                                 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
    7301                                 (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
    7302                                 SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
    7303                 }
    7304 
    7305                 count = (uint64_t)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
    7306 #endif /* HAVE_LONGLONG */
    73077889        }
    73087890
    73097891        return count;
    73107892}
    7311 
    7312 #if !defined(HAVE_LONGLONG)
    7313 /****************************************************************************
    7314  Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
    7315 ****************************************************************************/
    7316 
    7317 static uint32 map_lock_offset(uint32 high, uint32 low)
    7318 {
    7319         unsigned int i;
    7320         uint32 mask = 0;
    7321         uint32 highcopy = high;
    7322 
    7323         /*
    7324          * Try and find out how many significant bits there are in high.
    7325          */
    7326 
    7327         for(i = 0; highcopy; i++)
    7328                 highcopy >>= 1;
    7329 
    7330         /*
    7331          * We use 31 bits not 32 here as POSIX
    7332          * lock offsets may not be negative.
    7333          */
    7334 
    7335         mask = (~0) << (31 - i);
    7336 
    7337         if(low & mask)
    7338                 return 0; /* Fail. */
    7339 
    7340         high <<= (31 - i);
    7341 
    7342         return (high|low);
    7343 }
    7344 #endif /* !defined(HAVE_LONGLONG) */
    73457893
    73467894/****************************************************************************
     
    73497897
    73507898uint64_t get_lock_offset(const uint8_t *data, int data_offset,
    7351                          bool large_file_format, bool *err)
     7899                         bool large_file_format)
    73527900{
    73537901        uint64_t offset = 0;
    7354 
    7355         *err = False;
    73567902
    73577903        if(!large_file_format) {
    73587904                offset = (uint64_t)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
    73597905        } else {
    7360 
    7361 #if defined(HAVE_LONGLONG)
     7906                /*
     7907                 * No BVAL, this is reversed!
     7908                 */
    73627909                offset = (((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
    73637910                                ((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
    7364 #else /* HAVE_LONGLONG */
    7365 
    7366                 /*
    7367                  * NT4.x seems to be broken in that it sends large file (64 bit)
    7368                  * lockingX calls even if the CAP_LARGE_FILES was *not*
    7369                  * negotiated. For boxes without large unsigned ints mangle the
    7370                  * lock offset by mapping the top 32 bits onto the lower 32.
    7371                  */
    7372 
    7373                 if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
    7374                         uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
    7375                         uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
    7376                         uint32 new_low = 0;
    7377 
    7378                         if((new_low = map_lock_offset(high, low)) == 0) {
    7379                                 *err = True;
    7380                                 return (uint64_t)-1;
    7381                         }
    7382 
    7383                         DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
    7384                                 (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
    7385                         SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
    7386                         SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
    7387                 }
    7388 
    7389                 offset = (uint64_t)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
    7390 #endif /* HAVE_LONGLONG */
    73917911        }
    73927912
     
    73987918                         uint8_t type,
    73997919                         int32_t timeout,
    7400                          uint16_t num_ulocks,
    7401                          struct smbd_lock_element *ulocks,
    74027920                         uint16_t num_locks,
    74037921                         struct smbd_lock_element *locks,
     
    74107928        *async = false;
    74117929
    7412         /* Data now points at the beginning of the list
    7413            of smb_unlkrng structs */
    7414         for(i = 0; i < (int)num_ulocks; i++) {
    7415                 struct smbd_lock_element *e = &ulocks[i];
    7416 
    7417                 DEBUG(10,("smbd_do_locking: unlock start=%.0f, len=%.0f for "
    7418                           "pid %u, file %s\n",
    7419                           (double)e->offset,
    7420                           (double)e->count,
    7421                           (unsigned int)e->smblctx,
    7422                           fsp_str_dbg(fsp)));
    7423 
    7424                 if (e->brltype != UNLOCK_LOCK) {
    7425                         /* this can only happen with SMB2 */
    7426                         return NT_STATUS_INVALID_PARAMETER;
    7427                 }
    7428 
    7429                 status = do_unlock(req->sconn->msg_ctx,
    7430                                 fsp,
    7431                                 e->smblctx,
    7432                                 e->count,
    7433                                 e->offset,
    7434                                 WINDOWS_LOCK);
    7435 
    7436                 DEBUG(10, ("smbd_do_locking: unlock returned %s\n",
    7437                     nt_errstr(status)));
    7438 
    7439                 if (!NT_STATUS_IS_OK(status)) {
    7440                         return status;
    7441                 }
    7442         }
    7443 
    74447930        /* Setup the timeout in seconds. */
    74457931
     
    74477933                timeout = 0;
    74487934        }
    7449 
    7450         /* Data now points at the beginning of the list
    7451            of smb_lkrng structs */
    74527935
    74537936        for(i = 0; i < (int)num_locks; i++) {
     
    74687951                                /*
    74697952                                 * MS-CIFS (2.2.4.32.1) states that a cancel is honored if and only
    7470                                  * if the lock vector contains one entry. When given mutliple cancel
     7953                                 * if the lock vector contains one entry. When given multiple cancel
    74717954                                 * requests in a single PDU we expect the server to return an
    74727955                                 * error. Windows servers seem to accept the request but only
     
    75137996                                                e->count,
    75147997                                                e->offset,
    7515                                                 WINDOWS_LOCK,
    7516                                                 blr);
     7998                                                WINDOWS_LOCK);
    75177999                } else {
    75188000                        bool blocking_lock = timeout ? true : false;
     
    75308012                                        blocking_lock,
    75318013                                        &status,
    7532                                         &block_smblctx,
    7533                                         NULL);
     8014                                        &block_smblctx);
    75348015
    75358016                        if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
     
    76218102        }
    76228103
    7623         DEBUG(3, ("smbd_do_locking: fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
    7624                   fsp->fnum, (unsigned int)type, num_locks, num_ulocks));
     8104        DEBUG(3, ("smbd_do_locking: %s type=%d num_locks=%d\n",
     8105                  fsp_fnum_dbg(fsp), (unsigned int)type, num_locks));
     8106
     8107        return NT_STATUS_OK;
     8108}
     8109
     8110NTSTATUS smbd_do_unlocking(struct smb_request *req,
     8111                           files_struct *fsp,
     8112                           uint16_t num_ulocks,
     8113                           struct smbd_lock_element *ulocks)
     8114{
     8115        int i;
     8116
     8117        for(i = 0; i < (int)num_ulocks; i++) {
     8118                struct smbd_lock_element *e = &ulocks[i];
     8119                NTSTATUS status;
     8120
     8121                DEBUG(10,("%s: unlock start=%.0f, len=%.0f for "
     8122                          "pid %u, file %s\n", __func__,
     8123                          (double)e->offset,
     8124                          (double)e->count,
     8125                          (unsigned int)e->smblctx,
     8126                          fsp_str_dbg(fsp)));
     8127
     8128                if (e->brltype != UNLOCK_LOCK) {
     8129                        /* this can only happen with SMB2 */
     8130                        return NT_STATUS_INVALID_PARAMETER;
     8131                }
     8132
     8133                status = do_unlock(req->sconn->msg_ctx,
     8134                                fsp,
     8135                                e->smblctx,
     8136                                e->count,
     8137                                e->offset,
     8138                                WINDOWS_LOCK);
     8139
     8140                DEBUG(10, ("%s: unlock returned %s\n", __func__,
     8141                           nt_errstr(status)));
     8142
     8143                if (!NT_STATUS_IS_OK(status)) {
     8144                        return status;
     8145                }
     8146        }
     8147
     8148        DEBUG(3, ("%s: %s num_ulocks=%d\n", __func__, fsp_fnum_dbg(fsp),
     8149                  num_ulocks));
    76258150
    76268151        return NT_STATUS_OK;
     
    76378162        unsigned char locktype;
    76388163        unsigned char oplocklevel;
    7639         uint16 num_ulocks;
    7640         uint16 num_locks;
    7641         int32 lock_timeout;
     8164        uint16_t num_ulocks;
     8165        uint16_t num_locks;
     8166        int32_t lock_timeout;
    76428167        int i;
    76438168        const uint8_t *data;
    76448169        bool large_file_format;
    7645         bool err;
    76468170        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
    76478171        struct smbd_lock_element *ulocks;
     
    76638187        num_locks = SVAL(req->vwv+7, 0);
    76648188        lock_timeout = IVAL(req->vwv+4, 0);
    7665         large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
     8189        large_file_format = ((locktype & LOCKING_ANDX_LARGE_FILES) != 0);
    76668190
    76678191        if (!check_fsp(conn, req, fsp)) {
     
    76848208           we have granted an oplock on.
    76858209        */
    7686         if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
     8210        if (locktype & LOCKING_ANDX_OPLOCK_RELEASE) {
    76878211                /* Client can insist on breaking to none. */
    76888212                bool break_to_none = (oplocklevel == 0);
     
    76908214
    76918215                DEBUG(5,("reply_lockingX: oplock break reply (%u) from client "
    7692                          "for fnum = %d\n", (unsigned int)oplocklevel,
    7693                          fsp->fnum ));
     8216                         "for %s\n", (unsigned int)oplocklevel,
     8217                         fsp_fnum_dbg(fsp)));
    76948218
    76958219                /*
     
    77078231
    77088232                        DEBUG(5,("reply_lockingX: Error : oplock break from "
    7709                                  "client for fnum = %d (oplock=%d) and no "
     8233                                 "client for %s (oplock=%d) and no "
    77108234                                 "oplock granted on this file (%s).\n",
    7711                                  fsp->fnum, fsp->oplock_type,
     8235                                 fsp_fnum_dbg(fsp), fsp->oplock_type,
    77128236                                 fsp_str_dbg(fsp)));
    77138237
     
    77388262                }
    77398263
    7740                 reply_to_oplock_break_requests(fsp);
    7741 
    77428264                /* if this is a pure oplock break request then don't send a
    77438265                 * reply */
     
    77458267                        /* Sanity check - ensure a pure oplock break is not a
    77468268                           chained request. */
    7747                         if(CVAL(req->vwv+0, 0) != 0xff)
     8269                        if (CVAL(req->vwv+0, 0) != 0xff) {
    77488270                                DEBUG(0,("reply_lockingX: Error : pure oplock "
    77498271                                         "break is a chained %d request !\n",
    77508272                                         (unsigned int)CVAL(req->vwv+0, 0)));
     8273                        }
    77518274                        END_PROFILE(SMBlockingX);
    77528275                        return;
     
    77808303                ulocks[i].smblctx = get_lock_pid(data, i, large_file_format);
    77818304                ulocks[i].count = get_lock_count(data, i, large_file_format);
    7782                 ulocks[i].offset = get_lock_offset(data, i, large_file_format, &err);
     8305                ulocks[i].offset = get_lock_offset(data, i, large_file_format);
    77838306                ulocks[i].brltype = UNLOCK_LOCK;
    7784 
    7785                 /*
    7786                  * There is no error code marked "stupid client bug".... :-).
    7787                  */
    7788                 if(err) {
    7789                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
    7790                         END_PROFILE(SMBlockingX);
    7791                         return;
    7792                 }
    77938307        }
    77948308
     
    78028316                locks[i].smblctx = get_lock_pid(data, i, large_file_format);
    78038317                locks[i].count = get_lock_count(data, i, large_file_format);
    7804                 locks[i].offset = get_lock_offset(data, i, large_file_format, &err);
     8318                locks[i].offset = get_lock_offset(data, i, large_file_format);
    78058319
    78068320                if (locktype & LOCKING_ANDX_SHARED_LOCK) {
     
    78178331                        }
    78188332                }
    7819 
    7820                 /*
    7821                  * There is no error code marked "stupid client bug".... :-).
    7822                  */
    7823                 if(err) {
    7824                         reply_nterror(req, NT_STATUS_ACCESS_DENIED);
    7825                         END_PROFILE(SMBlockingX);
    7826                         return;
    7827                 }
     8333        }
     8334
     8335        status = smbd_do_unlocking(req, fsp, num_ulocks, ulocks);
     8336        if (!NT_STATUS_IS_OK(status)) {
     8337                END_PROFILE(SMBlockingX);
     8338                reply_nterror(req, status);
     8339                return;
    78288340        }
    78298341
    78308342        status = smbd_do_locking(req, fsp,
    78318343                                 locktype, lock_timeout,
    7832                                  num_ulocks, ulocks,
    78338344                                 num_locks, locks,
    78348345                                 &async);
     
    78448355
    78458356        reply_outbuf(req, 2, 0);
    7846 
    7847         DEBUG(3, ("lockingX fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
    7848                   fsp->fnum, (unsigned int)locktype, num_locks, num_ulocks));
     8357        SSVAL(req->outbuf, smb_vwv0, 0xff); /* andx chain ends */
     8358        SSVAL(req->outbuf, smb_vwv1, 0);    /* no andx offset */
     8359
     8360        DEBUG(3, ("lockingX %s type=%d num_locks=%d num_ulocks=%d\n",
     8361                  fsp_fnum_dbg(fsp), (unsigned int)locktype, num_locks, num_ulocks));
    78498362
    78508363        END_PROFILE(SMBlockingX);
    7851         chain_reply(req);
    78528364}
    78538365
     
    79348446        }
    79358447
     8448        if (!(fsp->access_mask & FILE_WRITE_ATTRIBUTES)) {
     8449                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
     8450                goto out;
     8451        }
     8452
    79368453        status = smb_set_file_time(conn, fsp, fsp->fsp_name, &ft, true);
    79378454        if (!NT_STATUS_IS_OK(status)) {
     
    79408457        }
    79418458
    7942         DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u "
     8459        DEBUG( 3, ( "reply_setattrE %s actime=%u modtime=%u "
    79438460               " createtime=%u\n",
    7944                 fsp->fnum,
     8461                fsp_fnum_dbg(fsp),
    79458462                (unsigned int)ft.atime.tv_sec,
    79468463                (unsigned int)ft.mtime.tv_sec,
     
    80398556                SIVAL(req->outbuf, smb_vwv8, 0);
    80408557        } else {
    8041                 uint32 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &fsp->fsp_name->st);
    8042                 SIVAL(req->outbuf, smb_vwv6, (uint32)fsp->fsp_name->st.st_ex_size);
     8558                uint32_t allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &fsp->fsp_name->st);
     8559                SIVAL(req->outbuf, smb_vwv6, (uint32_t)fsp->fsp_name->st.st_ex_size);
    80438560                SIVAL(req->outbuf, smb_vwv8, allocation_size);
    80448561        }
    80458562        SSVAL(req->outbuf,smb_vwv10, mode);
    80468563
    8047         DEBUG( 3, ( "reply_getattrE fnum=%d\n", fsp->fnum));
     8564        DEBUG( 3, ( "reply_getattrE %s\n", fsp_fnum_dbg(fsp)));
    80488565
    80498566        END_PROFILE(SMBgetattrE);
Note: See TracChangeset for help on using the changeset viewer.