6e0ab399127f730b464c6aa8273de81f2dc3f7f8
[ddiss/samba.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
33 /****************************************************************************
34  purge stale printers and reload from pre-populated pcap cache
35 **************************************************************************/
36 void reload_printers(struct tevent_context *ev,
37                      struct messaging_context *msg_ctx)
38 {
39         struct auth_serversupplied_info *session_info = NULL;
40         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
41         int n_services;
42         int pnum;
43         int snum;
44         const char *pname;
45         const char *sname;
46         NTSTATUS status;
47
48         load_printers(ev, msg_ctx);
49
50         n_services = lp_numservices();
51         pnum = lp_servicenumber(PRINTERS_NAME);
52
53         DEBUG(10, ("reloading printer services from pcap cache\n"));
54
55         status = make_session_info_system(talloc_tos(), &session_info);
56         if (!NT_STATUS_IS_OK(status)) {
57                 DEBUG(3, ("reload_printers: "
58                           "Could not create system session_info\n"));
59                 /* can't remove stale printers before we
60                  * are fully initilized */
61                 return;
62         }
63
64         /*
65          * Add default config for printers added to smb.conf file and remove
66          * stale printers
67          */
68         for (snum = 0; snum < n_services; snum++) {
69                 /* avoid removing PRINTERS_NAME */
70                 if (snum == pnum) {
71                         continue;
72                 }
73
74                 /* skip no-printer services */
75                 if (!(lp_snum_ok(snum) && lp_print_ok(snum))) {
76                         continue;
77                 }
78
79                 sname = lp_const_servicename(snum);
80                 pname = lp_printername(snum);
81
82                 /* check printer, but avoid removing non-autoloaded printers */
83                 if (!pcap_printername_ok(pname) && lp_autoloaded(snum)) {
84                         DEBUG(3, ("removing stale printer %s\n", pname));
85
86                         if (is_printer_published(session_info, session_info,
87                                                  msg_ctx,
88                                                  NULL, lp_servicename(snum),
89                                                  NULL, &pinfo2)) {
90                                 nt_printer_publish(session_info,
91                                                    session_info,
92                                                    msg_ctx,
93                                                    pinfo2,
94                                                    DSPRINT_UNPUBLISH);
95                                 TALLOC_FREE(pinfo2);
96                         }
97                         nt_printer_remove(session_info, session_info, msg_ctx,
98                                           pname);
99                         lp_killservice(snum);
100                 } else {
101                         DEBUG(8, ("Adding default registry entry for printer "
102                                   "[%s], if it doesn't exist.\n", sname));
103                         nt_printer_add(session_info, session_info, msg_ctx,
104                                        sname);
105                 }
106         }
107
108         /* Make sure deleted printers are gone */
109         load_printers(ev, msg_ctx);
110
111         TALLOC_FREE(session_info);
112 }
113
114 /****************************************************************************
115  Reload the services file.
116 **************************************************************************/
117
118 bool reload_services(struct messaging_context *msg_ctx, int smb_sock,
119                      bool test)
120 {
121         bool ret;
122
123         if (lp_loaded()) {
124                 char *fname = lp_configfile();
125                 if (file_exist(fname) &&
126                     !strcsequal(fname, get_dyn_CONFIGFILE())) {
127                         set_dyn_CONFIGFILE(fname);
128                         test = False;
129                 }
130         }
131
132         reopen_logs();
133
134         if (test && !lp_file_list_changed())
135                 return(True);
136
137         lp_killunused(conn_snum_used);
138
139         ret = lp_load(get_dyn_CONFIGFILE(), False, False, True, True);
140
141         /* perhaps the config filename is now set */
142         if (!test)
143                 reload_services(msg_ctx, smb_sock, True);
144
145         reopen_logs();
146
147         load_interfaces();
148
149         if (smb_sock != -1) {
150                 set_socket_options(smb_sock,"SO_KEEPALIVE");
151                 set_socket_options(smb_sock, lp_socket_options());
152         }
153
154         mangle_reset_cache();
155         reset_stat_cache();
156
157         /* this forces service parameters to be flushed */
158         set_current_service(NULL,0,True);
159
160         return(ret);
161 }
162
163 /****************************************************************************
164  Notify smbds of new printcap data
165 **************************************************************************/
166 void reload_pcap_change_notify(struct tevent_context *ev,
167                                struct messaging_context *msg_ctx)
168 {
169         message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
170 }