s3-build: avoid to globally include printing and spoolss headers.
[vlendec/samba-autobuild/.git] / source3 / smbd / reply.c
index 6c93bd19e7125225c5c47ca2113a89d34e434df7..ba4c90d55299dce791712866a87c508706659f96 100644 (file)
 */
 
 #include "includes.h"
+#include "printing.h"
 #include "smbd/globals.h"
+#include "../librpc/gen_ndr/cli_spoolss.h"
+#include "rpc_client/cli_spoolss.h"
+#include "rpc_client/init_spoolss.h"
 
 /****************************************************************************
  Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
@@ -322,11 +326,11 @@ size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
 bool check_fsp_open(connection_struct *conn, struct smb_request *req,
                    files_struct *fsp)
 {
-       if (!(fsp) || !(conn)) {
+       if ((fsp == NULL) || (conn == NULL)) {
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return False;
        }
-       if (((conn) != (fsp)->conn) || req->vuid != (fsp)->vuid) {
+       if ((conn != fsp->conn) || (req->vuid != fsp->vuid)) {
                reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                return False;
        }
@@ -343,15 +347,15 @@ bool check_fsp(connection_struct *conn, struct smb_request *req,
        if (!check_fsp_open(conn, req, fsp)) {
                return False;
        }
-       if ((fsp)->is_directory) {
+       if (fsp->is_directory) {
                reply_nterror(req, NT_STATUS_INVALID_DEVICE_REQUEST);
                return False;
        }
-       if ((fsp)->fh->fd == -1) {
+       if (fsp->fh->fd == -1) {
                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                return False;
        }
-       (fsp)->num_smb_operations++;
+       fsp->num_smb_operations++;
        return True;
 }
 
@@ -386,22 +390,6 @@ bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
        return true;
 }
 
-/****************************************************************************
- Check if we have a correct fsp. Replacement for the FSP_BELONGS_CONN macro
-****************************************************************************/
-
-bool fsp_belongs_conn(connection_struct *conn, struct smb_request *req,
-                     files_struct *fsp)
-{
-       if ((fsp) && (conn) && ((conn)==(fsp)->conn)
-           && (req->vuid == (fsp)->vuid)) {
-               return True;
-       }
-
-       reply_nterror(req, NT_STATUS_INVALID_HANDLE);
-       return False;
-}
-
 static bool netbios_session_retarget(const char *name, int name_type)
 {
        char *trim_name;
@@ -495,13 +483,12 @@ static bool netbios_session_retarget(const char *name, int name_type)
  Reply to a (netbios-level) special message.
 ****************************************************************************/
 
-void reply_special(char *inbuf)
+void reply_special(struct smbd_server_connection *sconn, char *inbuf)
 {
        int msg_type = CVAL(inbuf,0);
        int msg_flags = CVAL(inbuf,1);
        fstring name1,name2;
        char name_type1, name_type2;
-       struct smbd_server_connection *sconn = smbd_server_conn;
 
        /*
         * We only really use 4 bytes of the outbuf, but for the smb_setlen
@@ -2898,17 +2885,17 @@ static void sendfile_short_send(files_struct *fsp,
  Return a readbraw error (4 bytes of zero).
 ****************************************************************************/
 
-static void reply_readbraw_error(void)
+static void reply_readbraw_error(struct smbd_server_connection *sconn)
 {
        char header[4];
 
        SIVAL(header,0,0);
 
-       smbd_lock_socket(smbd_server_conn);
+       smbd_lock_socket(sconn);
        if (write_data(smbd_server_fd(),header,4) != 4) {
                fail_readraw();
        }
-       smbd_unlock_socket(smbd_server_conn);
+       smbd_unlock_socket(sconn);
 }
 
 /****************************************************************************
@@ -2922,6 +2909,7 @@ static void send_file_readbraw(connection_struct *conn,
                               size_t nread,
                               ssize_t mincount)
 {
+       struct smbd_server_connection *sconn = req->sconn;
        char *outbuf = NULL;
        ssize_t ret=0;
 
@@ -3004,7 +2992,7 @@ normal_readbraw:
        if (!outbuf) {
                DEBUG(0,("send_file_readbraw: TALLOC_ARRAY failed for size %u.\n",
                        (unsigned)(nread+4)));
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                return;
        }
 
@@ -3050,7 +3038,7 @@ void reply_readbraw(struct smb_request *req)
        }
 
        if (req->wct < 8) {
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3058,7 +3046,7 @@ void reply_readbraw(struct smb_request *req)
        if (sconn->smb1.echo_handler.trusted_fde) {
                DEBUG(2,("SMBreadbraw rejected with NOT_SUPPORTED because of "
                         "'async smb echo handler = yes'\n"));
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3086,7 +3074,7 @@ void reply_readbraw(struct smb_request *req)
                DEBUG(3,("reply_readbraw: fnum %d not valid "
                        "- cache prime?\n",
                        (int)SVAL(req->vwv+0, 0)));
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3097,7 +3085,7 @@ void reply_readbraw(struct smb_request *req)
                                (fsp->access_mask & FILE_EXECUTE)))) {
                DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
                                (int)SVAL(req->vwv+0, 0)));
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3135,7 +3123,7 @@ void reply_readbraw(struct smb_request *req)
                        DEBUG(0,("reply_readbraw: negative 64 bit "
                                "readraw offset (%.0f) !\n",
                                (double)startpos ));
-                       reply_readbraw_error();
+                       reply_readbraw_error(sconn);
                        END_PROFILE(SMBreadbraw);
                        return;
                }
@@ -3152,7 +3140,7 @@ void reply_readbraw(struct smb_request *req)
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_readbraw_error();
+               reply_readbraw_error(sconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3207,7 +3195,7 @@ void reply_lockread(struct smb_request *req)
        files_struct *fsp;
        struct byte_range_lock *br_lck = NULL;
        char *p = NULL;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBlockread);
 
@@ -3247,7 +3235,7 @@ void reply_lockread(struct smb_request *req)
         * Note that the requested lock size is unaffected by max_recv.
         */
 
-       br_lck = do_lock(smbd_messaging_context(),
+       br_lck = do_lock(req->sconn->msg_ctx,
                        fsp,
                        (uint64_t)req->smbpid,
                        (uint64_t)numtoread,
@@ -3317,7 +3305,7 @@ void reply_read(struct smb_request *req)
        int outsize = 0;
        files_struct *fsp;
        struct lock_struct lock;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBread);
 
@@ -3471,7 +3459,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
        if (!req_is_in_chain(req) &&
            !is_encrypted_packet(req->inbuf) && (fsp->base_fsp == NULL) &&
            (fsp->wcp == NULL) &&
-           lp_use_sendfile(SNUM(conn), smbd_server_conn->smb1.signing_state) ) {
+           lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
                uint8 headerbuf[smb_size + 12 * 2];
                DATA_BLOB header;
 
@@ -3661,7 +3649,7 @@ void reply_read_and_X(struct smb_request *req)
                                return;
                        }
                        /* We currently don't do this on signed or sealed data. */
-                       if (srv_is_signing_active(smbd_server_conn) ||
+                       if (srv_is_signing_active(req->sconn) ||
                            is_encrypted_packet(req->inbuf)) {
                                reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
                                END_PROFILE(SMBreadX);
@@ -3723,9 +3711,9 @@ void reply_read_and_X(struct smb_request *req)
                /* NT_STATUS_RETRY - fall back to sync read. */
        }
 
-       smbd_lock_socket(smbd_server_conn);
+       smbd_lock_socket(req->sconn);
        send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
-       smbd_unlock_socket(smbd_server_conn);
+       smbd_unlock_socket(req->sconn);
 
  out:
        END_PROFILE(SMBreadX);
@@ -3774,7 +3762,7 @@ void reply_writebraw(struct smb_request *req)
         */
        SCVAL(req->inbuf,smb_com,SMBwritec);
 
-       if (srv_is_signing_active(smbd_server_conn)) {
+       if (srv_is_signing_active(req->sconn)) {
                END_PROFILE(SMBwritebraw);
                exit_server_cleanly("reply_writebraw: SMB signing is active - "
                                "raw reads/writes are disallowed.");
@@ -3787,7 +3775,7 @@ void reply_writebraw(struct smb_request *req)
                return;
        }
 
-       if (smbd_server_conn->smb1.echo_handler.trusted_fde) {
+       if (req->sconn->smb1.echo_handler.trusted_fde) {
                DEBUG(2,("SMBwritebraw rejected with NOT_SUPPORTED because of "
                         "'async smb echo handler = yes'\n"));
                reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
@@ -4076,7 +4064,7 @@ void reply_writeunlock(struct smb_request *req)
        }
 
        if (numtowrite && !fsp->print_file) {
-               status = do_unlock(smbd_messaging_context(),
+               status = do_unlock(req->sconn->msg_ctx,
                                fsp,
                                (uint64_t)req->smbpid,
                                (uint64_t)numtowrite, 
@@ -4239,13 +4227,13 @@ strict_unlock:
                                                (2*14) + /* word count (including bcc) */ \
                                                1 /* pad byte */)
 
-bool is_valid_writeX_buffer(const uint8_t *inbuf)
+bool is_valid_writeX_buffer(struct smbd_server_connection *sconn,
+                           const uint8_t *inbuf)
 {
        size_t numtowrite;
        connection_struct *conn = NULL;
        unsigned int doff = 0;
        size_t len = smb_len_large(inbuf);
-       struct smbd_server_connection *sconn = smbd_server_conn;
 
        if (is_encrypted_packet(inbuf)) {
                /* Can't do this on encrypted
@@ -4864,7 +4852,7 @@ void reply_lock(struct smb_request *req)
        DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
                 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
 
-       br_lck = do_lock(smbd_messaging_context(),
+       br_lck = do_lock(req->sconn->msg_ctx,
                        fsp,
                        (uint64_t)req->smbpid,
                        count,
@@ -4919,7 +4907,7 @@ void reply_unlock(struct smb_request *req)
        count = (uint64_t)IVAL(req->vwv+1, 0);
        offset = (uint64_t)IVAL(req->vwv+3, 0);
 
-       status = do_unlock(smbd_messaging_context(),
+       status = do_unlock(req->sconn->msg_ctx,
                        fsp,
                        (uint64_t)req->smbpid,
                        count,
@@ -5069,7 +5057,7 @@ void reply_printopen(struct smb_request *req)
        }
 
        /* Open for exclusive use, write only. */
-       status = print_fsp_open(req, conn, NULL, req->vuid, fsp);
+       status = print_spool_open(fsp, NULL, req->vuid);
 
        if (!NT_STATUS_IS_OK(status)) {
                file_free(req, fsp);
@@ -5177,38 +5165,100 @@ 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_connect_spoolss_pipe(conn, &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;
+               }
+
+               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<first+num_to_get;i++) {
+               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;
                        }
                }
 
@@ -5220,9 +5270,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);
@@ -5255,7 +5311,7 @@ void reply_printwrite(struct smb_request *req)
                 return;
         }
 
-       if (!CAN_PRINT(conn)) {
+       if (!fsp->print_file) {
                reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplwr);
                return;
@@ -5369,7 +5425,7 @@ void reply_rmdir(struct smb_request *req)
        TALLOC_CTX *ctx = talloc_tos();
        files_struct *fsp = NULL;
        int info = 0;
-       struct smbd_server_connection *sconn = smbd_server_conn;
+       struct smbd_server_connection *sconn = req->sconn;
 
        START_PROFILE(SMBrmdir);
 
@@ -5625,7 +5681,7 @@ static void rename_open_files(connection_struct *conn,
        }
 
        /* Send messages to all smbd's (not ourself) that the name has changed. */
-       rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
+       rename_share_filename(conn->sconn->msg_ctx, lck, conn->connectpath,
                              smb_fname_dst);
 
 }
@@ -7099,7 +7155,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                        return NT_STATUS_INVALID_PARAMETER;
                }
 
-               status = do_unlock(smbd_messaging_context(),
+               status = do_unlock(req->sconn->msg_ctx,
                                fsp,
                                e->smblctx,
                                e->count,
@@ -7193,7 +7249,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                        struct byte_range_lock *br_lck;
                        uint64_t block_smblctx;
 
-                       br_lck = do_lock(smbd_messaging_context(),
+                       br_lck = do_lock(req->sconn->msg_ctx,
                                        fsp,
                                        e->smblctx,
                                        e->count,
@@ -7225,7 +7281,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                         *
                         * Replacement for do_lock_spin(). JRA. */
 
-                       if (!smbd_server_conn->using_smb2 &&
+                       if (!req->sconn->using_smb2 &&
                            br_lck && lp_blocking_locks(SNUM(conn)) &&
                            lp_lock_spin_time() && !blocking_lock &&
                            NT_STATUS_EQUAL((status),
@@ -7283,7 +7339,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                for(i--; i >= 0; i--) {
                        struct smbd_lock_element *e = &locks[i];
 
-                       do_unlock(smbd_messaging_context(),
+                       do_unlock(req->sconn->msg_ctx,
                                fsp,
                                e->smblctx,
                                e->count,