s3-param: Rename loadparm_s3_context -> loadparm_s3_helpers
[kai/samba.git] / source3 / printing / printspoolss.c
index 62182865bf047bf521bc241a5f65f95920c07eea..b94314e4204f78dd8cc5aca29fbe2c70c156af2b 100644 (file)
 
 #include "includes.h"
 #include "printing.h"
-#include "../librpc/gen_ndr/cli_spoolss.h"
+#include "rpc_client/rpc_client.h"
+#include "../librpc/gen_ndr/ndr_spoolss_c.h"
+#include "rpc_server/rpc_ncacn_np.h"
 #include "smbd/globals.h"
+#include "../libcli/security/security.h"
+
+struct print_file_data {
+       char *svcname;
+       char *docname;
+       char *filename;
+       struct policy_handle handle;
+       uint32_t jobid;
+       uint16 rap_jobid;
+};
+
+uint16_t print_spool_rap_jobid(struct print_file_data *print_file)
+{
+       if (print_file == NULL) {
+               return 0;
+       }
+
+       return print_file->rap_jobid;
+}
 
 void print_spool_terminate(struct connection_struct *conn,
                           struct print_file_data *print_file);
@@ -36,12 +57,12 @@ void print_spool_terminate(struct connection_struct *conn,
 
 NTSTATUS print_spool_open(files_struct *fsp,
                          const char *fname,
-                         uint16_t current_vuid)
+                         uint64_t current_vuid)
 {
        NTSTATUS status;
        TALLOC_CTX *tmp_ctx;
        struct print_file_data *pf;
-       struct rpc_pipe_client *cli;
+       struct dcerpc_binding_handle *b = NULL;
        struct spoolss_DevmodeContainer devmode_ctr;
        union spoolss_DocumentInfo info;
        int fd = -1;
@@ -79,7 +100,8 @@ NTSTATUS print_spool_open(files_struct *fsp,
                }
        }
 
-       /* Ok, now we have to open an actual file.
+       /*
+        * Ok, now we have to open an actual file.
         * Here is the reason:
         * We want to write the spool job to this file in
         * smbd for scalability reason (and also because
@@ -89,9 +111,13 @@ NTSTATUS print_spool_open(files_struct *fsp,
         * to spoolss in output_file so it can monitor and
         * take over once we call EndDocPrinter().
         * Of course we will not start writing until
-        * StartDocPrinter() actually gives the ok. */
+        * StartDocPrinter() actually gives the ok.
+        * smbd spooler files do not include a print jobid
+        * path component, as the jobid is only known after
+        * calling StartDocPrinter().
+        */
 
-       pf->filename = talloc_asprintf(pf, "%s/%s.XXXXXX",
+       pf->filename = talloc_asprintf(pf, "%s/%sXXXXXX",
                                        lp_pathname(SNUM(fsp->conn)),
                                        PRINT_SPOOL_PREFIX);
        if (!pf->filename) {
@@ -123,18 +149,18 @@ NTSTATUS print_spool_open(files_struct *fsp,
 
        status = rpc_pipe_open_interface(fsp->conn,
                                         &ndr_table_spoolss.syntax_id,
-                                        fsp->conn->server_info,
-                                        &fsp->conn->sconn->client_id,
+                                        fsp->conn->session_info,
+                                        fsp->conn->sconn->remote_address,
                                         fsp->conn->sconn->msg_ctx,
                                         &fsp->conn->spoolss_pipe);
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
        }
-       cli = fsp->conn->spoolss_pipe;
+       b = fsp->conn->spoolss_pipe->binding_handle;
 
        ZERO_STRUCT(devmode_ctr);
 
-       status = rpccli_spoolss_OpenPrinter(cli, pf, pf->svcname,
+       status = dcerpc_spoolss_OpenPrinter(b, pf, pf->svcname,
                                            "RAW", devmode_ctr,
                                            SEC_FLAG_MAXIMUM_ALLOWED,
                                            &pf->handle, &werr);
@@ -155,7 +181,7 @@ NTSTATUS print_spool_open(files_struct *fsp,
        info.info1->output_file = pf->filename;
        info.info1->datatype = "RAW";
 
-       status = rpccli_spoolss_StartDocPrinter(cli, tmp_ctx, &pf->handle,
+       status = dcerpc_spoolss_StartDocPrinter(b, tmp_ctx, &pf->handle,
                                                1, info, &pf->jobid, &werr);
        if (!NT_STATUS_IS_OK(status)) {
                goto done;
@@ -186,7 +212,6 @@ NTSTATUS print_spool_open(files_struct *fsp,
        }
 
        fsp->file_id = vfs_file_id_from_sbuf(fsp->conn, &fsp->fsp_name->st);
-       fsp->mode = fsp->fsp_name->st.st_ex_mode;
        fsp->fh->fd = fd;
 
        fsp->vuid = current_vuid;
@@ -206,7 +231,9 @@ done:
        if (!NT_STATUS_IS_OK(status)) {
                if (fd != -1) {
                        close(fd);
-                       unlink(fsp->print_file->filename);
+                       if (fsp->print_file) {
+                               unlink(fsp->print_file->filename);
+                       }
                }
                /* We need to delete the job from spoolss too */
                if (pf->jobid) {
@@ -219,7 +246,7 @@ done:
 
 int print_spool_write(files_struct *fsp,
                      const char *data, uint32_t size,
-                     SMB_OFF_T offset, uint32_t *written)
+                     off_t offset, uint32_t *written)
 {
        SMB_STRUCT_STAT st;
        ssize_t n;
@@ -268,29 +295,17 @@ int print_spool_write(files_struct *fsp,
 
 void print_spool_end(files_struct *fsp, enum file_close_type close_type)
 {
-       struct rpc_pipe_client *cli;
        NTSTATUS status;
        WERROR werr;
+       struct dcerpc_binding_handle *b = NULL;
 
-       status = rpc_pipe_open_interface(fsp->conn,
-                                        &ndr_table_spoolss.syntax_id,
-                                        fsp->conn->server_info,
-                                        &fsp->conn->sconn->client_id,
-                                        fsp->conn->sconn->msg_ctx,
-                                        &fsp->conn->spoolss_pipe);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("print_spool_end: "
-                         "Failed to get spoolss pipe [%s]\n",
-                         nt_errstr(status)));
-               return;
-       }
-       cli = fsp->conn->spoolss_pipe;
+       b = fsp->conn->spoolss_pipe->binding_handle;
 
        switch (close_type) {
        case NORMAL_CLOSE:
        case SHUTDOWN_CLOSE:
                /* this also automatically calls spoolss_EndDocPrinter */
-               status = rpccli_spoolss_ClosePrinter(cli, fsp->print_file,
+               status = dcerpc_spoolss_ClosePrinter(b, fsp->print_file,
                                                &fsp->print_file->handle,
                                                &werr);
                if (!NT_STATUS_IS_OK(status) ||
@@ -309,16 +324,16 @@ void print_spool_end(files_struct *fsp, enum file_close_type close_type)
 void print_spool_terminate(struct connection_struct *conn,
                           struct print_file_data *print_file)
 {
-       struct rpc_pipe_client *cli;
        NTSTATUS status;
        WERROR werr;
+       struct dcerpc_binding_handle *b = NULL;
 
        rap_jobid_delete(print_file->svcname, print_file->jobid);
 
        status = rpc_pipe_open_interface(conn,
                                         &ndr_table_spoolss.syntax_id,
-                                        conn->server_info,
-                                        &conn->sconn->client_id,
+                                        conn->session_info,
+                                        conn->sconn->remote_address,
                                         conn->sconn->msg_ctx,
                                         &conn->spoolss_pipe);
        if (!NT_STATUS_IS_OK(status)) {
@@ -327,9 +342,9 @@ void print_spool_terminate(struct connection_struct *conn,
                          nt_errstr(status)));
                return;
        }
-       cli = &conn->spoolss_pipe;
+       b = conn->spoolss_pipe->binding_handle;
 
-       status = rpccli_spoolss_SetJob(cli, print_file,
+       status = dcerpc_spoolss_SetJob(b, print_file,
                                        &print_file->handle,
                                        print_file->jobid,
                                        NULL, SPOOLSS_JOB_CONTROL_DELETE,
@@ -340,7 +355,7 @@ void print_spool_terminate(struct connection_struct *conn,
                          print_file->jobid, nt_errstr(status)));
                return;
        }
-       status = rpccli_spoolss_ClosePrinter(cli, print_file,
+       status = dcerpc_spoolss_ClosePrinter(b, print_file,
                                             &print_file->handle,
                                             &werr);
        if (!NT_STATUS_IS_OK(status) ||