s3-printing: Add new printers to registry.
[ddiss/samba.git] / source3 / smbd / server_reload.c
index e1d563d09d0481707d3b47365c6d7a8f335318fd..6e0ab399127f730b464c6aa8273de81f2dc3f7f8 100644 (file)
 */
 
 #include "includes.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
-#include "librpc/gen_ndr/messaging.h"
 #include "nt_printing.h"
+#include "printing/pcap.h"
+#include "printing/load.h"
+#include "auth.h"
+#include "messages.h"
 
 /****************************************************************************
- Reload printers
+ purge stale printers and reload from pre-populated pcap cache
 **************************************************************************/
-void reload_printers(struct messaging_context *msg_ctx)
+void reload_printers(struct tevent_context *ev,
+                    struct messaging_context *msg_ctx)
 {
-       struct auth_serversupplied_info *server_info = NULL;
+       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;
 
-       pcap_cache_reload(server_event_context(), msg_ctx);
+       load_printers(ev, msg_ctx);
 
-       status = make_server_info_system(talloc_tos(), &server_info);
+       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);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("reload_printers: "
-                         "Could not create system server_info\n"));
+                         "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(server_info, server_info,
+                       if (is_printer_published(session_info, session_info,
                                                 msg_ctx,
                                                 NULL, lp_servicename(snum),
                                                 NULL, &pinfo2)) {
-                               nt_printer_publish(server_info,
-                                                  server_info,
+                               nt_printer_publish(session_info,
+                                                  session_info,
                                                   msg_ctx,
                                                   pinfo2,
                                                   DSPRINT_UNPUBLISH);
                                TALLOC_FREE(pinfo2);
                        }
-                       nt_printer_remove(server_info, server_info, msg_ctx,
+                       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);
                }
        }
 
-       load_printers(server_event_context(), msg_ctx);
+       /* Make sure deleted printers are gone */
+       load_printers(ev, msg_ctx);
 
-       TALLOC_FREE(server_info);
+       TALLOC_FREE(session_info);
 }
 
 /****************************************************************************
  Reload the services file.
 **************************************************************************/
 
-bool reload_services(struct messaging_context *msg_ctx, bool test)
+bool reload_services(struct messaging_context *msg_ctx, int smb_sock,
+                    bool test)
 {
        bool ret;
 
@@ -110,19 +138,17 @@ bool reload_services(struct messaging_context *msg_ctx, bool test)
 
        ret = lp_load(get_dyn_CONFIGFILE(), False, False, True, True);
 
-       reload_printers(msg_ctx);
-
        /* perhaps the config filename is now set */
        if (!test)
-               reload_services(msg_ctx, True);
+               reload_services(msg_ctx, smb_sock, True);
 
        reopen_logs();
 
        load_interfaces();
 
-       if (smbd_server_fd() != -1) {
-               set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
-               set_socket_options(smbd_server_fd(), lp_socket_options());
+       if (smb_sock != -1) {
+               set_socket_options(smb_sock,"SO_KEEPALIVE");
+               set_socket_options(smb_sock, lp_socket_options());
        }
 
        mangle_reset_cache();
@@ -133,3 +159,12 @@ bool reload_services(struct messaging_context *msg_ctx, bool test)
 
        return(ret);
 }
+
+/****************************************************************************
+ Notify smbds of new printcap data
+**************************************************************************/
+void reload_pcap_change_notify(struct tevent_context *ev,
+                              struct messaging_context *msg_ctx)
+{
+       message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
+}