s3-printing: Add new printers to registry.
[obnox/samba/samba-obnox.git] / source3 / printing / nt_printing.c
index 4eb375dddd320ced43aa673e8ae3894bec73cd62..92aa320b718c0b42af35a28b73451479225a507a 100644 (file)
@@ -29,6 +29,7 @@
 #include "../libcli/security/security.h"
 #include "passdb/machine_sid.h"
 #include "smbd/smbd.h"
+#include "smbd/globals.h"
 #include "auth.h"
 #include "messages.h"
 #include "rpc_server/spoolss/srv_spoolss_nt.h"
@@ -615,7 +616,8 @@ static uint32 get_correct_cversion(struct auth_session_info *session_info,
                return -1;
        }
 
-       nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
+       nt_status = create_conn_struct(talloc_tos(), smbd_server_conn, &conn,
+                                      printdollar_snum,
                                       lp_pathname(printdollar_snum),
                                       session_info, &oldcwd);
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -744,7 +746,7 @@ static uint32 get_correct_cversion(struct auth_session_info *session_info,
                SMB_VFS_DISCONNECT(conn);
                conn_free(conn);
        }
-       if (!NT_STATUS_IS_OK(*perr)) {
+       if (!W_ERROR_IS_OK(*perr)) {
                cversion = -1;
        }
 
@@ -998,7 +1000,8 @@ WERROR move_driver_to_download_area(struct auth_session_info *session_info,
                return WERR_NO_SUCH_SHARE;
        }
 
-       nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
+       nt_status = create_conn_struct(talloc_tos(), smbd_server_conn, &conn,
+                                      printdollar_snum,
                                       lp_pathname(printdollar_snum),
                                       session_info, &oldcwd);
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -1401,8 +1404,7 @@ static bool trim_overlap_drv_files(TALLOC_CTX *mem_ctx,
 ****************************************************************************/
 
 bool printer_driver_files_in_use(TALLOC_CTX *mem_ctx,
-                                const struct auth_session_info *session_info,
-                                struct messaging_context *msg_ctx,
+                                struct dcerpc_binding_handle *b,
                                 struct spoolss_DriverInfo8 *info)
 {
        int                             i;
@@ -1412,7 +1414,6 @@ bool printer_driver_files_in_use(TALLOC_CTX *mem_ctx,
        uint32_t num_drivers;
        const char **drivers;
        WERROR result;
-       struct dcerpc_binding_handle *b;
 
        if ( !info )
                return False;
@@ -1425,14 +1426,6 @@ bool printer_driver_files_in_use(TALLOC_CTX *mem_ctx,
 
        /* get the list of drivers */
 
-       result = winreg_printer_binding_handle(mem_ctx,
-                                              session_info,
-                                              msg_ctx,
-                                              &b);
-       if (!W_ERROR_IS_OK(result)) {
-               return false;
-       }
-
        result = winreg_get_driver_list(mem_ctx, b,
                                        info->architecture, version,
                                        &num_drivers, &drivers);
@@ -1481,20 +1474,30 @@ bool printer_driver_files_in_use(TALLOC_CTX *mem_ctx,
 }
 
 static NTSTATUS driver_unlink_internals(connection_struct *conn,
-                                       const char *name)
+                                       const char *short_arch,
+                                       int vers,
+                                       const char *fname)
 {
+       TALLOC_CTX *tmp_ctx = talloc_new(conn);
        struct smb_filename *smb_fname = NULL;
-       NTSTATUS status;
+       char *print_dlr_path;
+       NTSTATUS status = NT_STATUS_NO_MEMORY;
+
+       print_dlr_path = talloc_asprintf(tmp_ctx, "%s/%d/%s",
+                                        short_arch, vers, fname);
+       if (print_dlr_path == NULL) {
+               goto err_out;
+       }
 
-       status = create_synthetic_smb_fname(talloc_tos(), name, NULL, NULL,
-           &smb_fname);
+       status = create_synthetic_smb_fname(tmp_ctx, print_dlr_path,
+                                           NULL, NULL, &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
-               return status;
+               goto err_out;
        }
 
        status = unlink_internals(conn, NULL, 0, smb_fname, false);
-
-       TALLOC_FREE(smb_fname);
+err_out:
+       talloc_free(tmp_ctx);
        return status;
 }
 
@@ -1507,9 +1510,7 @@ static NTSTATUS driver_unlink_internals(connection_struct *conn,
 bool delete_driver_files(const struct auth_session_info *session_info,
                         const struct spoolss_DriverInfo8 *r)
 {
-       int i = 0;
-       char *s;
-       const char *file;
+       const char *short_arch;
        connection_struct *conn;
        NTSTATUS nt_status;
        char *oldcwd;
@@ -1532,7 +1533,8 @@ bool delete_driver_files(const struct auth_session_info *session_info,
                return false;
        }
 
-       nt_status = create_conn_struct(talloc_tos(), &conn, printdollar_snum,
+       nt_status = create_conn_struct(talloc_tos(), smbd_server_conn, &conn,
+                                      printdollar_snum,
                                       lp_pathname(printdollar_snum),
                                       session_info, &oldcwd);
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -1560,55 +1562,40 @@ bool delete_driver_files(const struct auth_session_info *session_info,
                goto err_out;
        }
 
-       /* now delete the files; must strip the '\print$' string from
-          fron of path                                                */
+       short_arch = get_short_archi(r->architecture);
+       if (short_arch == NULL) {
+               DEBUG(0, ("bad architecture %s\n", r->architecture));
+               ret = false;
+               goto err_out;
+       }
+
+       /* now delete the files */
 
        if (r->driver_path && r->driver_path[0]) {
-               if ((s = strchr(&r->driver_path[1], '\\')) != NULL) {
-                       file = s;
-                       DEBUG(10,("deleting driverfile [%s]\n", s));
-                       driver_unlink_internals(conn, file);
-               }
+               DEBUG(10,("deleting driverfile [%s]\n", r->driver_path));
+               driver_unlink_internals(conn, short_arch, r->version, r->driver_path);
        }
 
        if (r->config_file && r->config_file[0]) {
-               if ((s = strchr(&r->config_file[1], '\\')) != NULL) {
-                       file = s;
-                       DEBUG(10,("deleting configfile [%s]\n", s));
-                       driver_unlink_internals(conn, file);
-               }
+               DEBUG(10,("deleting configfile [%s]\n", r->config_file));
+               driver_unlink_internals(conn, short_arch, r->version, r->config_file);
        }
 
        if (r->data_file && r->data_file[0]) {
-               if ((s = strchr(&r->data_file[1], '\\')) != NULL) {
-                       file = s;
-                       DEBUG(10,("deleting datafile [%s]\n", s));
-                       driver_unlink_internals(conn, file);
-               }
+               DEBUG(10,("deleting datafile [%s]\n", r->data_file));
+               driver_unlink_internals(conn, short_arch, r->version, r->data_file);
        }
 
        if (r->help_file && r->help_file[0]) {
-               if ((s = strchr(&r->help_file[1], '\\')) != NULL) {
-                       file = s;
-                       DEBUG(10,("deleting helpfile [%s]\n", s));
-                       driver_unlink_internals(conn, file);
-               }
+               DEBUG(10,("deleting helpfile [%s]\n", r->help_file));
+               driver_unlink_internals(conn, short_arch, r->version, r->help_file);
        }
 
-       /* check if we are done removing files */
-
        if (r->dependent_files) {
+               int i = 0;
                while (r->dependent_files[i] && r->dependent_files[i][0]) {
-                       char *p;
-
-                       /* bypass the "\print$" portion of the path */
-
-                       if ((p = strchr(r->dependent_files[i]+1, '\\')) != NULL) {
-                               file = p;
-                               DEBUG(10,("deleting dependent file [%s]\n", file));
-                               driver_unlink_internals(conn, file);
-                       }
-
+                       DEBUG(10,("deleting dependent file [%s]\n", r->dependent_files[i]));
+                       driver_unlink_internals(conn, short_arch, r->version, r->dependent_files[i]);
                        i++;
                }
        }
@@ -1858,7 +1845,22 @@ void nt_printer_remove(TALLOC_CTX *mem_ctx,
        result = winreg_delete_printer_key_internal(mem_ctx, session_info, msg_ctx,
                                           printer, "");
        if (!W_ERROR_IS_OK(result)) {
-               DEBUG(0, ("nt_printer_remove: failed to remove rpinter %s",
-                         printer));
+               DEBUG(0, ("nt_printer_remove: failed to remove printer %s: "
+               "%s\n", printer, win_errstr(result)));
+       }
+}
+
+void nt_printer_add(TALLOC_CTX *mem_ctx,
+                   const struct auth_session_info *session_info,
+                   struct messaging_context *msg_ctx,
+                   const char *printer)
+{
+       WERROR result;
+
+       result = winreg_create_printer_internal(mem_ctx, session_info, msg_ctx,
+                                               printer);
+       if (!W_ERROR_IS_OK(result)) {
+               DEBUG(0, ("nt_printer_add: failed to add printer %s: %s\n",
+                         printer, win_errstr(result)));
        }
 }