e4b039d18929710cd697c68412f2b1abbc66aa0d
[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/globals.h"
26 #include "librpc/gen_ndr/messaging.h"
27 #include "nt_printing.h"
28 #include "printing/pcap.h"
29 #include "printing/load.h"
30
31 /****************************************************************************
32  purge stale printers and reload from pre-populated pcap cache
33 **************************************************************************/
34 void reload_printers(struct tevent_context *ev,
35                      struct messaging_context *msg_ctx)
36 {
37         struct auth_serversupplied_info *session_info = NULL;
38         struct spoolss_PrinterInfo2 *pinfo2 = NULL;
39         int snum;
40         int n_services = lp_numservices();
41         int pnum = lp_servicenumber(PRINTERS_NAME);
42         const char *pname;
43         NTSTATUS status;
44         bool skip = false;
45
46         SMB_ASSERT(pcap_cache_loaded());
47         DEBUG(10, ("reloading printer services from pcap cache\n"));
48
49         status = make_session_info_system(talloc_tos(), &session_info);
50         if (!NT_STATUS_IS_OK(status)) {
51                 DEBUG(3, ("reload_printers: "
52                           "Could not create system session_info\n"));
53                 /* can't remove stale printers before we
54                  * are fully initilized */
55                 skip = true;
56         }
57
58         /* remove stale printers */
59         for (snum = 0; skip == false && snum < n_services; snum++) {
60                 /* avoid removing PRINTERS_NAME or non-autoloaded printers */
61                 if (snum == pnum || !(lp_snum_ok(snum) && lp_print_ok(snum) &&
62                                       lp_autoloaded(snum)))
63                         continue;
64
65                 pname = lp_printername(snum);
66                 if (!pcap_printername_ok(pname)) {
67                         DEBUG(3, ("removing stale printer %s\n", pname));
68
69                         if (is_printer_published(session_info, session_info,
70                                                  msg_ctx,
71                                                  NULL, lp_servicename(snum),
72                                                  NULL, &pinfo2)) {
73                                 nt_printer_publish(session_info,
74                                                    session_info,
75                                                    msg_ctx,
76                                                    pinfo2,
77                                                    DSPRINT_UNPUBLISH);
78                                 TALLOC_FREE(pinfo2);
79                         }
80                         nt_printer_remove(session_info, session_info, msg_ctx,
81                                           pname);
82                         lp_killservice(snum);
83                 }
84         }
85
86         load_printers(ev, msg_ctx);
87
88         TALLOC_FREE(session_info);
89 }
90
91 /****************************************************************************
92  Reload the services file.
93 **************************************************************************/
94
95 bool reload_services(struct messaging_context *msg_ctx, int smb_sock,
96                      bool test)
97 {
98         bool ret;
99
100         if (lp_loaded()) {
101                 char *fname = lp_configfile();
102                 if (file_exist(fname) &&
103                     !strcsequal(fname, get_dyn_CONFIGFILE())) {
104                         set_dyn_CONFIGFILE(fname);
105                         test = False;
106                 }
107         }
108
109         reopen_logs();
110
111         if (test && !lp_file_list_changed())
112                 return(True);
113
114         lp_killunused(conn_snum_used);
115
116         ret = lp_load(get_dyn_CONFIGFILE(), False, False, True, True);
117
118         /* perhaps the config filename is now set */
119         if (!test)
120                 reload_services(msg_ctx, smb_sock, True);
121
122         reopen_logs();
123
124         load_interfaces();
125
126         if (smb_sock != -1) {
127                 set_socket_options(smb_sock,"SO_KEEPALIVE");
128                 set_socket_options(smb_sock, lp_socket_options());
129         }
130
131         mangle_reset_cache();
132         reset_stat_cache();
133
134         /* this forces service parameters to be flushed */
135         set_current_service(NULL,0,True);
136
137         return(ret);
138 }
139
140 /****************************************************************************
141  Notify smbds of new printcap data
142 **************************************************************************/
143 void reload_pcap_change_notify(struct tevent_context *ev,
144                                struct messaging_context *msg_ctx)
145 {
146         message_send_all(msg_ctx, MSG_PRINTER_PCAP, NULL, 0, NULL);
147 }