s3: Lift smbd_server_conn from file_find_di_first
[kai/samba.git] / source3 / smbd / reply.c
index fe0e717637585ebb87c709ffd4e264a6a0097fa5..c84c077d581b968ab9f35987c9c70b351934cad5 100644 (file)
 #include "includes.h"
 #include "printing.h"
 #include "smbd/globals.h"
+#include "fake_file.h"
 #include "../librpc/gen_ndr/cli_spoolss.h"
+#include "rpc_client/cli_spoolss.h"
+#include "rpc_client/init_spoolss.h"
+#include "rpc_server/rpc_ncacn_np.h"
 
 /****************************************************************************
  Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
@@ -388,7 +392,8 @@ bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
        return true;
 }
 
-static bool netbios_session_retarget(const char *name, int name_type)
+static bool netbios_session_retarget(struct smbd_server_connection *sconn,
+                                    const char *name, int name_type)
 {
        char *trim_name;
        char *trim_name_type;
@@ -402,7 +407,7 @@ static bool netbios_session_retarget(const char *name, int name_type)
        bool ret = false;
        uint8_t outbuf[10];
 
-       if (get_socket_port(smbd_server_fd()) != 139) {
+       if (get_socket_port(sconn->sock) != 139) {
                return false;
        }
 
@@ -465,7 +470,7 @@ static bool netbios_session_retarget(const char *name, int name_type)
        *(uint32_t *)(outbuf+4) = in_addr->sin_addr.s_addr;
        *(uint16_t *)(outbuf+8) = htons(retarget_port);
 
-       if (!srv_send_smb(smbd_server_fd(), (char *)outbuf, false, 0, false,
+       if (!srv_send_smb(sconn, (char *)outbuf, false, 0, false,
                          NULL)) {
                exit_server_cleanly("netbios_session_regarget: srv_send_smb "
                                    "failed.");
@@ -478,16 +483,13 @@ static bool netbios_session_retarget(const char *name, int name_type)
 }
 
 /****************************************************************************
- Reply to a (netbios-level) special message.
+ Reply to a (netbios-level) special message. 
 ****************************************************************************/
 
-void reply_special(struct smbd_server_connection *sconn, char *inbuf)
+void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inbuf_size)
 {
        int msg_type = CVAL(inbuf,0);
        int msg_flags = CVAL(inbuf,1);
-       fstring name1,name2;
-       char name_type1, name_type2;
-
        /*
         * We only really use 4 bytes of the outbuf, but for the smb_setlen
         * calculation & friends (srv_send_smb uses that) we need the full smb
@@ -495,14 +497,19 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf)
         */
        char outbuf[smb_size];
 
-       *name1 = *name2 = 0;
-
        memset(outbuf, '\0', sizeof(outbuf));
 
        smb_setlen(outbuf,0);
 
        switch (msg_type) {
        case 0x81: /* session request */
+       {
+               /* inbuf_size is guarenteed to be at least 4. */
+               fstring name1,name2;
+               int name_type1, name_type2;
+               int name_len1, name_len2;
+
+               *name1 = *name2 = 0;
 
                if (sconn->nbt.got_session) {
                        exit_server_cleanly("multiple session request not permitted");
@@ -510,20 +517,45 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf)
 
                SCVAL(outbuf,0,0x82);
                SCVAL(outbuf,3,0);
-               if (name_len(inbuf+4) > 50 || 
-                   name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
+
+               /* inbuf_size is guaranteed to be at least 4. */
+               name_len1 = name_len((unsigned char *)(inbuf+4),inbuf_size - 4);
+               if (name_len1 <= 0 || name_len1 > inbuf_size - 4) {
                        DEBUG(0,("Invalid name length in session request\n"));
-                       return;
+                       break;
+               }
+               name_len2 = name_len((unsigned char *)(inbuf+4+name_len1),inbuf_size - 4 - name_len1);
+               if (name_len2 <= 0 || name_len2 > inbuf_size - 4 - name_len1) {
+                       DEBUG(0,("Invalid name length in session request\n"));
+                       break;
+               }
+
+               name_type1 = name_extract((unsigned char *)inbuf,
+                               inbuf_size,(unsigned int)4,name1);
+               name_type2 = name_extract((unsigned char *)inbuf,
+                               inbuf_size,(unsigned int)(4 + name_len1),name2);
+
+               if (name_type1 == -1 || name_type2 == -1) {
+                       DEBUG(0,("Invalid name type in session request\n"));
+                       break;
                }
-               name_type1 = name_extract(inbuf,4,name1);
-               name_type2 = name_extract(inbuf,4 + name_len(inbuf + 4),name2);
+
                DEBUG(2,("netbios connect: name1=%s0x%x name2=%s0x%x\n",
                         name1, name_type1, name2, name_type2));
 
-               if (netbios_session_retarget(name1, name_type1)) {
+               if (netbios_session_retarget(sconn, name1, name_type1)) {
                        exit_server_cleanly("retargeted client");
                }
 
+               /*
+                * Windows NT/2k uses "*SMBSERVER" and XP uses
+                * "*SMBSERV" arrggg!!!
+                */
+               if (strequal(name1, "*SMBSERVER     ")
+                   || strequal(name1, "*SMBSERV       "))  {
+                       fstrcpy(name1, sconn->client_id.addr);
+               }
+
                set_local_machine_name(name1, True);
                set_remote_machine_name(name2, True);
 
@@ -545,11 +577,12 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf)
                        add_session_user(sconn, get_remote_machine_name());
                }
 
-               reload_services(True);
+               reload_services(sconn->msg_ctx, sconn->sock, True);
                reopen_logs();
 
                sconn->nbt.got_session = true;
                break;
+       }
 
        case 0x89: /* session keepalive request 
                      (some old clients produce this?) */
@@ -571,7 +604,7 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf)
        DEBUG(5,("init msg_type=0x%x msg_flags=0x%x\n",
                    msg_type, msg_flags));
 
-       srv_send_smb(smbd_server_fd(), outbuf, false, 0, false, NULL);
+       srv_send_smb(sconn, outbuf, false, 0, false, NULL);
        return;
 }
 
@@ -2040,7 +2073,7 @@ void reply_ulogoffX(struct smb_request *req)
        /* in user level security we are supposed to close any files
                open by this user */
        if ((vuser != NULL) && (lp_security() != SEC_SHARE)) {
-               file_close_user(req->vuid);
+               file_close_user(sconn, req->vuid);
        }
 
        invalidate_vuid(sconn, req->vuid);
@@ -2807,7 +2840,18 @@ static ssize_t fake_sendfile(files_struct *fsp, SMB_OFF_T startpos,
                        memset(buf + ret, '\0', cur_read - ret);
                }
 
-               if (write_data(smbd_server_fd(),buf,cur_read) != cur_read) {
+               if (write_data(fsp->conn->sconn->sock, buf, cur_read)
+                   != cur_read) {
+                       char addr[INET6_ADDRSTRLEN];
+                       /*
+                        * Try and give an error message saying what
+                        * client failed.
+                        */
+                       DEBUG(0, ("write_data failed for client %s. "
+                                 "Error %s\n",
+                                 get_peer_addr(fsp->conn->sconn->sock, addr,
+                                               sizeof(addr)),
+                                 strerror(errno)));
                        SAFE_FREE(buf);
                        return -1;
                }
@@ -2868,9 +2912,21 @@ static void sendfile_short_send(files_struct *fsp,
                        size_t to_write;
 
                        to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread);
-                       if (write_data(smbd_server_fd(), buf, to_write) != to_write) {
+                       if (write_data(fsp->conn->sconn->sock, buf, to_write)
+                           != to_write) {
+                               char addr[INET6_ADDRSTRLEN];
+                               /*
+                                * Try and give an error message saying what
+                                * client failed.
+                                */
+                               DEBUG(0, ("write_data failed for client %s. "
+                                         "Error %s\n",
+                                         get_peer_addr(
+                                                 fsp->conn->sconn->sock, addr,
+                                                 sizeof(addr)),
+                                         strerror(errno)));
                                exit_server_cleanly("sendfile_short_send: "
-                                       "write_data failed");
+                                                   "write_data failed");
                        }
                        nread += to_write;
                }
@@ -2890,7 +2946,17 @@ static void reply_readbraw_error(struct smbd_server_connection *sconn)
        SIVAL(header,0,0);
 
        smbd_lock_socket(sconn);
-       if (write_data(smbd_server_fd(),header,4) != 4) {
+       if (write_data(sconn->sock,header,4) != 4) {
+               char addr[INET6_ADDRSTRLEN];
+               /*
+                * Try and give an error message saying what
+                * client failed.
+                */
+               DEBUG(0, ("write_data failed for client %s. "
+                         "Error %s\n",
+                         get_peer_addr(sconn->sock, addr, sizeof(addr)),
+                         strerror(errno)));
+
                fail_readraw();
        }
        smbd_unlock_socket(sconn);
@@ -2929,8 +2995,10 @@ static void send_file_readbraw(connection_struct *conn,
                _smb_setlen(header,nread);
                header_blob = data_blob_const(header, 4);
 
-               if ((sendfile_read = SMB_VFS_SENDFILE(smbd_server_fd(), fsp,
-                               &header_blob, startpos, nread)) == -1) {
+               sendfile_read = SMB_VFS_SENDFILE(sconn->sock, fsp,
+                                                &header_blob, startpos,
+                                                nread);
+               if (sendfile_read == -1) {
                        /* Returning ENOSYS means no data at all was sent.
                         * Do this as a normal read. */
                        if (errno == ENOSYS) {
@@ -3006,8 +3074,20 @@ normal_readbraw:
        }
 
        _smb_setlen(outbuf,ret);
-       if (write_data(smbd_server_fd(),outbuf,4+ret) != 4+ret)
+       if (write_data(sconn->sock, outbuf, 4+ret) != 4+ret) {
+               char addr[INET6_ADDRSTRLEN];
+               /*
+                * Try and give an error message saying what
+                * client failed.
+                */
+               DEBUG(0, ("write_data failed for client %s. "
+                         "Error %s\n",
+                         get_peer_addr(fsp->conn->sconn->sock, addr,
+                                       sizeof(addr)),
+                         strerror(errno)));
+
                fail_readraw();
+       }
 
        TALLOC_FREE(outbuf);
 }
@@ -3472,7 +3552,9 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                construct_reply_common_req(req, (char *)headerbuf);
                setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
 
-               if ((nread = SMB_VFS_SENDFILE(smbd_server_fd(), fsp, &header, startpos, smb_maxcnt)) == -1) {
+               nread = SMB_VFS_SENDFILE(req->sconn->sock, fsp, &header,
+                                        startpos, smb_maxcnt);
+               if (nread == -1) {
                        /* Returning ENOSYS means no data at all was sent.
                           Do this as a normal read. */
                        if (errno == ENOSYS) {
@@ -3547,8 +3629,20 @@ normal_read:
                setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
 
                /* Send out the header. */
-               if (write_data(smbd_server_fd(), (char *)headerbuf,
+               if (write_data(req->sconn->sock, (char *)headerbuf,
                               sizeof(headerbuf)) != sizeof(headerbuf)) {
+
+                       char addr[INET6_ADDRSTRLEN];
+                       /*
+                        * Try and give an error message saying what
+                        * client failed.
+                        */
+                       DEBUG(0, ("write_data failed for client %s. "
+                                 "Error %s\n",
+                                 get_peer_addr(req->sconn->sock, addr,
+                                               sizeof(addr)),
+                                 strerror(errno)));
+
                        DEBUG(0,("send_file_readX: write_data failed for file "
                                 "%s (%s). Terminating\n", fsp_str_dbg(fsp),
                                 strerror(errno)));
@@ -3732,6 +3826,43 @@ void error_to_writebrawerr(struct smb_request *req)
        TALLOC_FREE(old_outbuf);
 }
 
+/****************************************************************************
+ Read 4 bytes of a smb packet and return the smb length of the packet.
+ Store the result in the buffer. This version of the function will
+ never return a session keepalive (length of zero).
+ Timeout is in milliseconds.
+****************************************************************************/
+
+static NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout,
+                               size_t *len)
+{
+       uint8_t msgtype = SMBkeepalive;
+
+       while (msgtype == SMBkeepalive) {
+               NTSTATUS status;
+
+               status = read_smb_length_return_keepalive(fd, inbuf, timeout,
+                                                         len);
+               if (!NT_STATUS_IS_OK(status)) {
+                       char addr[INET6_ADDRSTRLEN];
+                       /* Try and give an error message
+                        * saying what client failed. */
+                       DEBUG(0, ("read_fd_with_timeout failed for "
+                                 "client %s read error = %s.\n",
+                                 get_peer_addr(fd,addr,sizeof(addr)),
+                                 nt_errstr(status)));
+                       return status;
+               }
+
+               msgtype = CVAL(inbuf, 0);
+       }
+
+       DEBUG(10,("read_smb_length: got smb length of %lu\n",
+                 (unsigned long)len));
+
+       return NT_STATUS_OK;
+}
+
 /****************************************************************************
  Reply to a writebraw (core+ or LANMAN1.0 protocol).
 ****************************************************************************/
@@ -3865,7 +3996,7 @@ void reply_writebraw(struct smb_request *req)
        SCVAL(buf,smb_com,SMBwritebraw);
        SSVALS(buf,smb_vwv0,0xFFFF);
        show_msg(buf);
-       if (!srv_send_smb(smbd_server_fd(),
+       if (!srv_send_smb(req->sconn,
                          buf,
                          false, 0, /* no signing */
                          IS_CONN_ENCRYPTED(conn),
@@ -3875,7 +4006,7 @@ void reply_writebraw(struct smb_request *req)
        }
 
        /* Now read the raw data into the buffer and write it */
-       status = read_smb_length(smbd_server_fd(), buf, SMB_SECONDARY_WAIT,
+       status = read_smb_length(req->sconn->sock, buf, SMB_SECONDARY_WAIT,
                                 &numtowrite);
        if (!NT_STATUS_IS_OK(status)) {
                exit_server_cleanly("secondary writebraw failed");
@@ -3899,12 +4030,17 @@ void reply_writebraw(struct smb_request *req)
                                (int)tcount,(int)nwritten,(int)numtowrite));
                }
 
-               status = read_data(smbd_server_fd(), buf+4, numtowrite);
+               status = read_data(req->sconn->sock, buf+4, numtowrite);
 
                if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(0,("reply_writebraw: Oversize secondary write "
-                                "raw read failed (%s). Terminating\n",
-                                nt_errstr(status)));
+                       char addr[INET6_ADDRSTRLEN];
+                       /* Try and give an error message
+                        * saying what client failed. */
+                       DEBUG(0, ("reply_writebraw: Oversize secondary write "
+                                 "raw read failed (%s) for client %s. "
+                                 "Terminating\n", nt_errstr(status),
+                                 get_peer_addr(req->sconn->sock, addr,
+                                               sizeof(addr))));
                        exit_server_cleanly("secondary writebraw failed");
                }
 
@@ -3959,7 +4095,7 @@ void reply_writebraw(struct smb_request *req)
                 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this.
                 * JRA.
                 */
-               if (!send_keepalive(smbd_server_fd())) {
+               if (!send_keepalive(req->sconn->sock)) {
                        exit_server_cleanly("reply_writebraw: send of "
                                "keepalive failed");
                }
@@ -4637,7 +4773,7 @@ void reply_exit(struct smb_request *req)
 {
        START_PROFILE(SMBexit);
 
-       file_close_pid(req->smbpid, req->vuid);
+       file_close_pid(req->sconn, req->smbpid, req->vuid);
 
        reply_outbuf(req, 0, 0);
 
@@ -5007,7 +5143,7 @@ void reply_echo(struct smb_request *req)
                SSVAL(req->outbuf,smb_vwv0,seq_num);
 
                show_msg((char *)req->outbuf);
-               if (!srv_send_smb(smbd_server_fd(),
+               if (!srv_send_smb(req->sconn,
                                (char *)req->outbuf,
                                true, req->seqnum+1,
                                IS_CONN_ENCRYPTED(conn)||req->encrypted,
@@ -5163,38 +5299,105 @@ void reply_printqueue(struct smb_request *req)
                 start_index, max_count));
 
        {
-               print_queue_struct *queue = NULL;
-               print_status_struct status;
-               int count = print_queue_status(SNUM(conn), &queue, &status);
-               int num_to_get = ABS(max_count);
-               int first = (max_count>0?start_index:start_index+max_count+1);
-               int i;
-
-               if (first >= count)
-                       num_to_get = 0;
-               else
-                       num_to_get = MIN(num_to_get,count-first);
+               TALLOC_CTX *mem_ctx = talloc_tos();
+               NTSTATUS status;
+               WERROR werr;
+               const char *sharename = lp_servicename(SNUM(conn));
+               struct rpc_pipe_client *cli = NULL;
+               struct policy_handle handle;
+               struct spoolss_DevmodeContainer devmode_ctr;
+               union spoolss_JobInfo *info;
+               uint32_t count;
+               uint32_t num_to_get;
+               uint32_t first;
+               uint32_t i;
+
+               ZERO_STRUCT(handle);
+
+               status = rpc_pipe_open_interface(conn,
+                                                &ndr_table_spoolss.syntax_id,
+                                                conn->server_info,
+                                                &conn->sconn->client_id,
+                                                conn->sconn->msg_ctx,
+                                                &cli);
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("reply_printqueue: "
+                                 "could not connect to spoolss: %s\n",
+                                 nt_errstr(status)));
+                       reply_nterror(req, status);
+                       goto out;
+               }
+
+               ZERO_STRUCT(devmode_ctr);
+
+               status = rpccli_spoolss_OpenPrinter(cli, mem_ctx,
+                                               sharename,
+                                               NULL, devmode_ctr,
+                                               SEC_FLAG_MAXIMUM_ALLOWED,
+                                               &handle,
+                                               &werr);
+               if (!NT_STATUS_IS_OK(status)) {
+                       reply_nterror(req, status);
+                       goto out;
+               }
+               if (!W_ERROR_IS_OK(werr)) {
+                       reply_nterror(req, werror_to_ntstatus(werr));
+                       goto out;
+               }
 
+               werr = rpccli_spoolss_enumjobs(cli, mem_ctx,
+                                              &handle,
+                                              0, /* firstjob */
+                                              0xff, /* numjobs */
+                                              2, /* level */
+                                              0, /* offered */
+                                              &count,
+                                              &info);
+               if (!W_ERROR_IS_OK(werr)) {
+                       reply_nterror(req, werror_to_ntstatus(werr));
+                       goto out;
+               }
 
-               for (i=first;i<first+num_to_get;i++) {
+               if (max_count > 0) {
+                       first = start_index;
+               } else {
+                       first = start_index + max_count + 1;
+               }
+
+               if (first >= count) {
+                       num_to_get = first;
+               } else {
+                       num_to_get = first + MIN(ABS(max_count), count - first);
+               }
+
+               for (i = first; i < num_to_get; i++) {
                        char blob[28];
                        char *p = blob;
+                       time_t qtime = spoolss_Time_to_time_t(&info[i].info2.submitted);
+                       int qstatus;
+                       uint16_t qrapjobid = pjobid_to_rap(sharename,
+                                                       info[i].info2.job_id);
 
-                       srv_put_dos_date2(p,0,queue[i].time);
-                       SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
-                       SSVAL(p,5, queue[i].job);
-                       SIVAL(p,7,queue[i].size);
-                       SCVAL(p,11,0);
+                       if (info[i].info2.status == JOB_STATUS_PRINTING) {
+                               qstatus = 2;
+                       } else {
+                               qstatus = 3;
+                       }
+
+                       srv_put_dos_date2(p, 0, qtime);
+                       SCVAL(p, 4, qstatus);
+                       SSVAL(p, 5, qrapjobid);
+                       SIVAL(p, 7, info[i].info2.size);
+                       SCVAL(p, 11, 0);
                        srvstr_push(blob, req->flags2, p+12,
-                                   queue[i].fs_user, 16, STR_ASCII);
+                                   info[i].info2.notify_name, 16, STR_ASCII);
 
                        if (message_push_blob(
                                    &req->outbuf,
                                    data_blob_const(
                                            blob, sizeof(blob))) == -1) {
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
-                               END_PROFILE(SMBsplretq);
-                               return;
+                               goto out;
                        }
                }
 
@@ -5206,9 +5409,15 @@ void reply_printqueue(struct smb_request *req)
                        SSVAL(smb_buf(req->outbuf),1,28*count);
                }
 
-               SAFE_FREE(queue);
 
-               DEBUG(3,("%d entries returned in queue\n",count));
+               DEBUG(3, ("%u entries returned in queue\n",
+                         (unsigned)count));
+
+out:
+               if (cli && is_valid_policy_hnd(&handle)) {
+                       rpccli_spoolss_ClosePrinter(cli, mem_ctx, &handle, NULL);
+               }
+
        }
 
        END_PROFILE(SMBsplretq);
@@ -5584,7 +5793,7 @@ static void rename_open_files(connection_struct *conn,
        bool did_rename = False;
        NTSTATUS status;
 
-       for(fsp = file_find_di_first(lck->id); fsp;
+       for(fsp = file_find_di_first(conn->sconn, lck->id); fsp;
            fsp = file_find_di_next(fsp)) {
                /* fsp_name is a relative path under the fsp. To change this for other
                   sharepaths we need to manipulate relative paths. */
@@ -5859,7 +6068,8 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
        if (dst_exists) {
                struct file_id fileid = vfs_file_id_from_sbuf(conn,
                    &smb_fname_dst->st);
-               files_struct *dst_fsp = file_find_di_first(fileid);
+               files_struct *dst_fsp = file_find_di_first(conn->sconn,
+                                                          fileid);
                /* The file can be open when renaming a stream */
                if (dst_fsp && !new_is_stream) {
                        DEBUG(3, ("rename_internals_fsp: Target file open\n"));
@@ -5906,8 +6116,9 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                          "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
 
-               if (lp_map_archive(SNUM(conn)) ||
-                   lp_store_dos_attributes(SNUM(conn))) {
+               if (!lp_posix_pathnames() &&
+                   (lp_map_archive(SNUM(conn)) ||
+                   lp_store_dos_attributes(SNUM(conn)))) {
                        /* We must set the archive bit on the newly
                           renamed file. */
                        if (SMB_VFS_STAT(conn, smb_fname_dst) == 0) {