2010-05-27 09:18:58 +04:00
/*
Unix SMB / CIFS implementation .
Main SMB server routines
Copyright ( C ) Andrew Tridgell 1992 - 1998
Copyright ( C ) Martin Pool 2002
Copyright ( C ) Jelmer Vernooij 2002 - 2003
Copyright ( C ) Volker Lendecke 1993 - 2007
Copyright ( C ) Jeremy Allison 1993 - 2007
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
2011-03-22 18:57:01 +03:00
# include "smbd/smbd.h"
2010-05-27 09:18:58 +04:00
# include "smbd/globals.h"
2010-07-31 02:47:20 +04:00
# include "nt_printing.h"
2010-12-19 21:52:08 +03:00
# include "printing/pcap.h"
2021-03-13 16:34:35 +03:00
# include "printing/printer_list.h"
2011-02-22 21:24:31 +03:00
# include "printing/load.h"
2011-03-24 15:46:20 +03:00
# include "auth.h"
2011-03-24 17:31:06 +03:00
# include "messages.h"
2011-06-29 09:33:54 +04:00
# include "lib/param/loadparm.h"
2010-05-27 09:18:58 +04:00
2014-07-23 16:42:00 +04:00
/*
* The persistent pcap cache is populated by the background print process . Per
* client smbds should only reload their printer share inventories if this
* information has changed . Use reload_last_pcap_time to detect this .
*/
static time_t reload_last_pcap_time = 0 ;
2014-08-05 20:45:24 +04:00
bool snum_is_shared_printer ( int snum )
2012-02-10 17:00:05 +04:00
{
2014-02-02 17:48:41 +04:00
return ( lp_browseable ( snum ) & & lp_snum_ok ( snum ) & & lp_printable ( snum ) ) ;
2012-02-10 17:00:05 +04:00
}
2011-09-05 16:35:55 +04:00
/**
2014-08-01 18:25:59 +04:00
* @ brief Purge stale printer shares and reload from pre - populated pcap cache .
2011-09-05 16:35:55 +04:00
*
* This function should normally only be called as a callback on a successful
2014-08-01 18:25:59 +04:00
* pcap_cache_reload ( ) , or on client enumeration .
2011-09-05 16:35:55 +04:00
*/
2018-05-23 17:35:20 +03:00
void delete_and_reload_printers ( void )
2010-05-27 09:18:58 +04:00
{
2012-02-07 14:41:54 +04:00
int n_services ;
int pnum ;
2010-05-27 09:18:58 +04:00
int snum ;
const char * pname ;
2014-07-23 16:42:00 +04:00
bool ok ;
time_t pcap_last_update ;
2021-11-08 14:11:17 +03:00
TALLOC_CTX * frame = NULL ;
2019-11-04 14:14:34 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2014-07-23 16:42:00 +04:00
2021-11-08 14:11:17 +03:00
if ( ! lp_load_printers ( ) ) {
DBG_DEBUG ( " skipping printer reload: disabled \n " ) ;
return ;
}
frame = talloc_stackframe ( ) ;
2014-07-23 16:42:00 +04:00
ok = pcap_cache_loaded ( & pcap_last_update ) ;
if ( ! ok ) {
DEBUG ( 1 , ( " pcap cache not loaded \n " ) ) ;
2014-08-01 18:25:59 +04:00
talloc_free ( frame ) ;
2014-07-23 16:42:00 +04:00
return ;
}
if ( reload_last_pcap_time = = pcap_last_update ) {
DEBUG ( 5 , ( " skipping printer reload, already up to date. \n " ) ) ;
2014-08-01 18:25:59 +04:00
talloc_free ( frame ) ;
2014-07-23 16:42:00 +04:00
return ;
}
reload_last_pcap_time = pcap_last_update ;
2010-05-27 09:18:58 +04:00
2012-02-07 14:41:54 +04:00
/* Get pcap printers updated */
2018-05-23 17:35:20 +03:00
load_printers ( ) ;
2012-02-07 14:41:54 +04:00
n_services = lp_numservices ( ) ;
pnum = lp_servicenumber ( PRINTERS_NAME ) ;
2010-12-19 21:52:08 +03:00
DEBUG ( 10 , ( " reloading printer services from pcap cache \n " ) ) ;
2010-05-27 09:18:58 +04:00
2012-02-07 14:41:54 +04:00
/*
* Add default config for printers added to smb . conf file and remove
* stale printers
*/
for ( snum = 0 ; snum < n_services ; snum + + ) {
/* avoid removing PRINTERS_NAME */
if ( snum = = pnum ) {
continue ;
}
/* skip no-printer services */
2012-02-10 17:00:05 +04:00
if ( ! snum_is_shared_printer ( snum ) ) {
2010-05-27 09:18:58 +04:00
continue ;
2012-02-07 14:41:54 +04:00
}
2010-05-27 09:18:58 +04:00
2019-11-04 14:14:34 +03:00
pname = lp_printername ( frame , lp_sub , snum ) ;
2012-02-07 14:41:54 +04:00
/* check printer, but avoid removing non-autoloaded printers */
2021-03-13 16:34:35 +03:00
if ( lp_autoloaded ( snum ) & &
! printer_list_printername_exists ( pname ) ) {
2010-05-27 09:18:58 +04:00
lp_killservice ( snum ) ;
}
}
2012-02-07 14:41:54 +04:00
/* Make sure deleted printers are gone */
2018-05-23 17:35:20 +03:00
load_printers ( ) ;
2010-04-27 01:34:24 +04:00
2014-08-01 18:25:59 +04:00
talloc_free ( frame ) ;
2010-05-27 09:18:58 +04:00
}
/****************************************************************************
Reload the services file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-12-14 16:25:20 +04:00
bool reload_services ( struct smbd_server_connection * sconn ,
bool ( * snumused ) ( struct smbd_server_connection * , int ) ,
2010-08-15 18:13:00 +04:00
bool test )
2010-05-27 09:18:58 +04:00
{
2019-11-04 18:50:41 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2014-05-22 01:23:34 +04:00
struct smbXsrv_connection * xconn = NULL ;
2010-05-27 09:18:58 +04:00
bool ret ;
if ( lp_loaded ( ) ) {
2019-11-04 18:50:41 +03:00
char * fname = lp_next_configfile ( talloc_tos ( ) , lp_sub ) ;
2010-05-27 09:18:58 +04:00
if ( file_exist ( fname ) & &
! strcsequal ( fname , get_dyn_CONFIGFILE ( ) ) ) {
set_dyn_CONFIGFILE ( fname ) ;
test = False ;
}
2012-06-01 02:06:58 +04:00
TALLOC_FREE ( fname ) ;
2010-05-27 09:18:58 +04:00
}
reopen_logs ( ) ;
if ( test & & ! lp_file_list_changed ( ) )
return ( True ) ;
2011-12-14 16:25:20 +04:00
lp_killunused ( sconn , snumused ) ;
2010-05-27 09:18:58 +04:00
2014-07-30 18:53:59 +04:00
ret = lp_load_with_shares ( get_dyn_CONFIGFILE ( ) ) ;
2010-05-27 09:18:58 +04:00
/* perhaps the config filename is now set */
2011-12-14 16:25:20 +04:00
if ( ! test ) {
reload_services ( sconn , snumused , true ) ;
}
2010-05-27 09:18:58 +04:00
reopen_logs ( ) ;
load_interfaces ( ) ;
2014-06-10 17:47:26 +04:00
if ( sconn ! = NULL & & sconn - > client ! = NULL ) {
xconn = sconn - > client - > connections ;
}
for ( ; xconn ! = NULL ; xconn = xconn - > next ) {
2014-05-22 01:23:34 +04:00
set_socket_options ( xconn - > transport . sock , " SO_KEEPALIVE " ) ;
set_socket_options ( xconn - > transport . sock , lp_socket_options ( ) ) ;
2010-05-27 09:18:58 +04:00
}
mangle_reset_cache ( ) ;
2018-05-19 06:51:58 +03:00
flush_dfree_cache ( ) ;
2010-05-27 09:18:58 +04:00
return ( ret ) ;
}