smbd: Simplify downgrade_lease
[bbaumbach/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 #include "lib/param/loadparm.h"
33
34 /*
35  * The persistent pcap cache is populated by the background print process. Per
36  * client smbds should only reload their printer share inventories if this
37  * information has changed. Use reload_last_pcap_time to detect this.
38  */
39 static time_t reload_last_pcap_time = 0;
40
41 bool snum_is_shared_printer(int snum)
42 {
43         return (lp_browseable(snum) && lp_snum_ok(snum) && lp_printable(snum));
44 }
45
46 /**
47  * @brief Purge stale printer shares and reload from pre-populated pcap cache.
48  *
49  * This function should normally only be called as a callback on a successful
50  * pcap_cache_reload(), or on client enumeration.
51  */
52 void delete_and_reload_printers(void)
53 {
54         int n_services;
55         int pnum;
56         int snum;
57         const char *pname;
58         bool ok;
59         time_t pcap_last_update;
60         TALLOC_CTX *frame = talloc_stackframe();
61
62         ok = pcap_cache_loaded(&pcap_last_update);
63         if (!ok) {
64                 DEBUG(1, ("pcap cache not loaded\n"));
65                 talloc_free(frame);
66                 return;
67         }
68
69         if (reload_last_pcap_time == pcap_last_update) {
70                 DEBUG(5, ("skipping printer reload, already up to date.\n"));
71                 talloc_free(frame);
72                 return;
73         }
74         reload_last_pcap_time = pcap_last_update;
75
76         /* Get pcap printers updated */
77         load_printers();
78
79         n_services = lp_numservices();
80         pnum = lp_servicenumber(PRINTERS_NAME);
81
82         DEBUG(10, ("reloading printer services from pcap cache\n"));
83
84         /*
85          * Add default config for printers added to smb.conf file and remove
86          * stale printers
87          */
88         for (snum = 0; snum < n_services; snum++) {
89                 /* avoid removing PRINTERS_NAME */
90                 if (snum == pnum) {
91                         continue;
92                 }
93
94                 /* skip no-printer services */
95                 if (!snum_is_shared_printer(snum)) {
96                         continue;
97                 }
98
99                 pname = lp_printername(frame, snum);
100
101                 /* check printer, but avoid removing non-autoloaded printers */
102                 if (lp_autoloaded(snum) && !pcap_printername_ok(pname)) {
103                         lp_killservice(snum);
104                 }
105         }
106
107         /* Make sure deleted printers are gone */
108         load_printers();
109
110         talloc_free(frame);
111 }
112
113 /****************************************************************************
114  Reload the services file.
115 **************************************************************************/
116
117 bool reload_services(struct smbd_server_connection *sconn,
118                      bool (*snumused) (struct smbd_server_connection *, int),
119                      bool test)
120 {
121         struct smbXsrv_connection *xconn = NULL;
122         bool ret;
123
124         if (lp_loaded()) {
125                 char *fname = lp_next_configfile(talloc_tos());
126                 if (file_exist(fname) &&
127                     !strcsequal(fname, get_dyn_CONFIGFILE())) {
128                         set_dyn_CONFIGFILE(fname);
129                         test = False;
130                 }
131                 TALLOC_FREE(fname);
132         }
133
134         reopen_logs();
135
136         if (test && !lp_file_list_changed())
137                 return(True);
138
139         lp_killunused(sconn, snumused);
140
141         ret = lp_load_with_shares(get_dyn_CONFIGFILE());
142
143         /* perhaps the config filename is now set */
144         if (!test) {
145                 reload_services(sconn, snumused, true);
146         }
147
148         reopen_logs();
149
150         load_interfaces();
151
152         if (sconn != NULL && sconn->client != NULL) {
153                 xconn = sconn->client->connections;
154         }
155         for (;xconn != NULL; xconn = xconn->next) {
156                 set_socket_options(xconn->transport.sock, "SO_KEEPALIVE");
157                 set_socket_options(xconn->transport.sock, lp_socket_options());
158         }
159
160         mangle_reset_cache();
161         reset_stat_cache();
162         flush_dfree_cache();
163
164         return(ret);
165 }