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