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