s3-printing: Add new printers to registry.
authorBjörn Baumbach <bb@sernet.de>
Fri, 28 Oct 2011 03:43:05 +0000 (05:43 +0200)
committerKarolin Seeger <kseeger@samba.org>
Thu, 10 May 2012 13:28:54 +0000 (15:28 +0200)
This fixes bug #8554, #8612 and #8748.

Pair-Programmed-With: Stefan Metzmacher <metze@samba.org>
Signed-off-by: Andreas Schneider <asn@samba.org>
source3/include/nt_printing.h
source3/printing/nt_printing.c
source3/smbd/server_reload.c

index 4f8515983b5b52a4d5f3ffad38f85621285ddaf2..16c46584c732ff2c080f08e082a343eb699e4720 100644 (file)
@@ -176,5 +176,9 @@ void nt_printer_remove(TALLOC_CTX *mem_ctx,
                        const struct auth_serversupplied_info *server_info,
                        struct messaging_context *msg_ctx,
                        const char *printer);
+void nt_printer_add(TALLOC_CTX *mem_ctx,
+                   const struct auth_serversupplied_info *server_info,
+                   struct messaging_context *msg_ctx,
+                   const char *printer);
 
 #endif /* NT_PRINTING_H_ */
index b0d4e81fa12ad6c0fb706d1ac3dfb893c4515581..05c53ec4aa0ab4834ee4d16d58fd9cc91675a106 100644 (file)
@@ -1863,7 +1863,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_serversupplied_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)));
        }
 }
index 82b0cb03e95f66ccdcb0893b7e0a9f32055b8749..6e0ab399127f730b464c6aa8273de81f2dc3f7f8 100644 (file)
@@ -38,14 +38,18 @@ void reload_printers(struct tevent_context *ev,
 {
        struct auth_serversupplied_info *session_info = NULL;
        struct spoolss_PrinterInfo2 *pinfo2 = NULL;
+       int n_services;
+       int pnum;
        int snum;
-       int n_services = lp_numservices();
-       int pnum = lp_servicenumber(PRINTERS_NAME);
        const char *pname;
+       const char *sname;
        NTSTATUS status;
-       bool skip = false;
 
-       SMB_ASSERT(pcap_cache_loaded());
+       load_printers(ev, msg_ctx);
+
+       n_services = lp_numservices();
+       pnum = lp_servicenumber(PRINTERS_NAME);
+
        DEBUG(10, ("reloading printer services from pcap cache\n"));
 
        status = make_session_info_system(talloc_tos(), &session_info);
@@ -54,18 +58,29 @@ void reload_printers(struct tevent_context *ev,
                          "Could not create system session_info\n"));
                /* can't remove stale printers before we
                 * are fully initilized */
-               skip = true;
+               return;
        }
 
-       /* remove stale printers */
-       for (snum = 0; skip == false && snum < n_services; snum++) {
-               /* avoid removing PRINTERS_NAME or non-autoloaded printers */
-               if (snum == pnum || !(lp_snum_ok(snum) && lp_print_ok(snum) &&
-                                     lp_autoloaded(snum)))
+       /*
+        * Add default config for printers added to smb.conf file and remove
+        * stale printers
+        */
+       for (snum = 0; snum < n_services; snum++) {
+               /* avoid removing PRINTERS_NAME */
+               if (snum == pnum) {
+                       continue;
+               }
+
+               /* skip no-printer services */
+               if (!(lp_snum_ok(snum) && lp_print_ok(snum))) {
                        continue;
+               }
 
+               sname = lp_const_servicename(snum);
                pname = lp_printername(snum);
-               if (!pcap_printername_ok(pname)) {
+
+               /* check printer, but avoid removing non-autoloaded printers */
+               if (!pcap_printername_ok(pname) && lp_autoloaded(snum)) {
                        DEBUG(3, ("removing stale printer %s\n", pname));
 
                        if (is_printer_published(session_info, session_info,
@@ -82,9 +97,15 @@ void reload_printers(struct tevent_context *ev,
                        nt_printer_remove(session_info, session_info, msg_ctx,
                                          pname);
                        lp_killservice(snum);
+               } else {
+                       DEBUG(8, ("Adding default registry entry for printer "
+                                 "[%s], if it doesn't exist.\n", sname));
+                       nt_printer_add(session_info, session_info, msg_ctx,
+                                      sname);
                }
        }
 
+       /* Make sure deleted printers are gone */
        load_printers(ev, msg_ctx);
 
        TALLOC_FREE(session_info);