s3-printing: Add new printers to registry.
[obnox/samba/samba-obnox.git] / source3 / smbd / server_reload.c
1 /*
2    Unix SMB/CIFS implementation.
3    Main SMB server routines
4    Copyright (C) Andrew Tridgell                1992-1998
5    Copyright (C) Martin Pool                    2002
6    Copyright (C) Jelmer Vernooij                2002-2003
7    Copyright (C) Volker Lendecke                1993-2007
8    Copyright (C) Jeremy Allison                 1993-2007
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "smbd/globals.h"
27 #include "nt_printing.h"
28 #include "printing/pcap.h"
29 #include "printing/load.h"
30 #include "auth.h"
31 #include "messages.h"
32 #include "lib/param/loadparm.h"
33
34 /**
35  * @brief Purge stale printers and reload from pre-populated pcap cache.
36  *
37  * This function should normally only be called as a callback on a successful
38  * pcap_cache_reload() or after a MSG_PRINTER_CAP message is received.
39  *
40  * This function can cause DELETION of printers and drivers from our registry,
41  * so calling it on a failed pcap reload may REMOVE permanently all printers
42  * and drivers.
43  *
44  * @param[in] ev        The event context.
45  *
46  * @param[in] msg_ctx   The messaging context.
47  */
48 void delete_and_reload_printers(struct tevent_context *ev,
49                                 struct messaging_context *msg_ctx)
50 {
51         struct auth_session_info *session_info = NULL;
52         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
53         int n_services;
54         int pnum;
55         int snum;
56         const char *pname;
57         const char *sname;
58         NTSTATUS status;
59
60         /* Get pcap printers updated */
61         load_printers(ev, msg_ctx);
62
63         n_services = lp_numservices();
64         pnum = lp_servicenumber(PRINTERS_NAME);
65
66         DEBUG(10, ("reloading printer services from pcap cache\n"));
67
68         status = make_session_info_system(talloc_tos(), &session_info);
69         if (!NT_STATUS_IS_OK(status)) {
70                 DEBUG(3, ("reload_printers: "
71                           "Could not create system session_info\n"));
72                 /* can't remove stale printers before we
73                  * are fully initilized */
74                 return;
75         }
76
77         /*
78          * Add default config for printers added to smb.conf file and remove
79          * stale printers
80          */
81         for (snum = 0; snum < n_services; snum++) {
82                 /* avoid removing PRINTERS_NAME */
83                 if (snum == pnum) {
84                         continue;
85                 }
86
87                 /* skip no-printer services */
88                 if (!(lp_snum_ok(snum) && lp_print_ok(snum))) {
89                         continue;
90                 }
91
92                 sname = lp_const_servicename(snum);
93                 pname = lp_printername(snum);
94
95                 /* check printer, but avoid removing non-autoloaded printers */
96                 if (!pcap_printername_ok(pname) && lp_autoloaded(snum)) {
97                         DEBUG(3, ("removing stale printer %s\n", pname));
98
99                         if (is_printer_published(session_info, session_info,
100                                                  msg_ctx,
101                                                  NULL, lp_servicename(snum),
102                                                  NULL, &pinfo2)) {
103                                 nt_printer_publish(session_info,
104                                                    session_info,
105                                                    msg_ctx,
106                                                    pinfo2,
107                                                    DSPRINT_UNPUBLISH);
108                                 TALLOC_FREE(pinfo2);
109                         }
110                         nt_printer_remove(session_info, session_info, msg_ctx,
111                                           pname);
112                         lp_killservice(snum);
113                 } else {
114                         DEBUG(8, ("Adding default registry entry for printer "
115                                   "[%s], if it doesn't exist.\n", sname));
116                         nt_printer_add(session_info, session_info, msg_ctx,
117                                        sname);
118                 }
119         }
120
121         /* Make sure deleted printers are gone */
122         load_printers(ev, msg_ctx);
123
124         TALLOC_FREE(session_info);
125 }
126
127 /****************************************************************************
128  Reload the services file.
129 **************************************************************************/
130
131 bool reload_services(struct smbd_server_connection *sconn,
132                      bool (*snumused) (struct smbd_server_connection *, int),
133                      bool test)
134 {
135         bool ret;
136
137         if (lp_loaded()) {
138                 char *fname = lp_configfile();
139                 if (file_exist(fname) &&
140                     !strcsequal(fname, get_dyn_CONFIGFILE())) {
141                         set_dyn_CONFIGFILE(fname);
142                         test = False;
143                 }
144         }
145
146         reopen_logs();
147
148         if (test && !lp_file_list_changed())
149                 return(True);
150
151         lp_killunused(sconn, snumused);
152
153         ret = lp_load(get_dyn_CONFIGFILE(), False, False, True, True);
154
155         /* perhaps the config filename is now set */
156         if (!test) {
157                 reload_services(sconn, snumused, true);
158         }
159
160         reopen_logs();
161
162         load_interfaces();
163
164         if (sconn != NULL) {
165                 set_socket_options(sconn->sock, "SO_KEEPALIVE");
166                 set_socket_options(sconn->sock, lp_socket_options());
167         }
168
169         mangle_reset_cache();
170         reset_stat_cache();
171
172         /* this forces service parameters to be flushed */
173         set_current_service(NULL,0,True);
174
175         return(ret);
176 }