1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
printcap parsing
1998-01-22 16:27:43 +03:00
Copyright ( C ) Karl Auer 1993 - 1998
1996-05-04 11:50:46 +04:00
Re - working by Martin Kiff , 1994
Re - written again by Andrew Tridgell
1997-12-03 08:08:07 +03:00
Modified for SVID support by Norm Jacobs , 1997
1999-12-13 16:27:58 +03:00
Modified for CUPS support by Michael Sweet , 1999
1996-05-04 11:50:46 +04:00
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( 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
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1996-05-04 11:50:46 +04:00
*/
/*
1997-12-03 08:08:07 +03:00
* Modified to call SVID / XPG4 support if printcap name is set to " lpstat "
* in smb . conf under Solaris .
1999-12-13 16:27:58 +03:00
*
* Modified to call CUPS support if printcap name is set to " cups "
* in smb . conf .
2005-09-21 03:28:22 +04:00
*
* Modified to call iPrint support if printcap name is set to " iprint "
* in smb . conf .
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2010-05-14 16:39:40 +04:00
# include "printing/pcap.h"
2010-05-14 02:42:55 +04:00
# include "printer_list.h"
2005-01-05 19:20:35 +03:00
2008-10-10 22:55:14 +04:00
struct pcap_cache {
2005-01-05 19:20:35 +03:00
char * name ;
char * comment ;
2011-05-13 12:02:42 +04:00
char * location ;
2005-01-05 19:20:35 +03:00
struct pcap_cache * next ;
2008-10-10 22:55:14 +04:00
} ;
2005-01-05 19:20:35 +03:00
2011-05-13 12:02:42 +04:00
bool pcap_cache_add_specific ( struct pcap_cache * * ppcache , const char * name , const char * comment , const char * location )
1996-05-04 11:50:46 +04:00
{
2008-10-10 22:55:14 +04:00
struct pcap_cache * p ;
2005-01-05 19:20:35 +03:00
2008-10-10 22:55:14 +04:00
if ( name = = NULL | | ( ( p = SMB_MALLOC_P ( struct pcap_cache ) ) = = NULL ) )
return false ;
2005-01-05 19:20:35 +03:00
p - > name = SMB_STRDUP ( name ) ;
p - > comment = ( comment & & * comment ) ? SMB_STRDUP ( comment ) : NULL ;
2011-05-13 12:02:42 +04:00
p - > location = ( location & & * location ) ? SMB_STRDUP ( location ) : NULL ;
2005-01-05 19:20:35 +03:00
2011-05-13 12:02:42 +04:00
DEBUG ( 11 , ( " pcap_cache_add_specific: Adding name %s info %s, location: %s \n " ,
p - > name , p - > comment ? p - > comment : " " ,
p - > location ? p - > location : " " ) ) ;
2008-10-10 22:55:14 +04:00
p - > next = * ppcache ;
* ppcache = p ;
2005-01-05 19:20:35 +03:00
2008-10-10 22:55:14 +04:00
return true ;
1996-05-04 11:50:46 +04:00
}
2005-01-05 19:20:35 +03:00
2008-10-10 22:55:14 +04:00
void pcap_cache_destroy_specific ( struct pcap_cache * * pp_cache )
1996-05-04 11:50:46 +04:00
{
2008-10-10 22:55:14 +04:00
struct pcap_cache * p , * next ;
1996-05-04 11:50:46 +04:00
2008-10-10 22:55:14 +04:00
for ( p = * pp_cache ; p ! = NULL ; p = next ) {
2005-01-05 19:20:35 +03:00
next = p - > next ;
SAFE_FREE ( p - > name ) ;
SAFE_FREE ( p - > comment ) ;
2011-05-13 12:02:42 +04:00
SAFE_FREE ( p - > location ) ;
2005-01-05 19:20:35 +03:00
SAFE_FREE ( p ) ;
1996-05-04 11:50:46 +04:00
}
2008-10-10 22:55:14 +04:00
* pp_cache = NULL ;
}
2014-07-23 14:12:34 +04:00
bool pcap_cache_loaded ( time_t * _last_change )
1996-05-04 11:50:46 +04:00
{
2010-05-14 02:42:55 +04:00
NTSTATUS status ;
time_t last ;
status = printer_list_get_last_refresh ( & last ) ;
2014-07-23 14:12:34 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return false ;
}
if ( _last_change ! = NULL ) {
* _last_change = last ;
}
return true ;
1996-05-04 11:50:46 +04:00
}
1998-07-29 07:08:05 +04:00
2010-12-28 16:55:01 +03:00
bool pcap_cache_replace ( const struct pcap_cache * pcache )
2008-10-10 22:55:14 +04:00
{
const struct pcap_cache * p ;
2010-12-28 16:55:01 +03:00
NTSTATUS status ;
2014-07-25 14:18:54 +04:00
time_t t = time_mono ( NULL ) ;
2010-12-28 16:55:01 +03:00
status = printer_list_mark_reload ( ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Failed to mark printer list for reload! \n " ) ) ;
return false ;
}
2008-10-10 22:55:14 +04:00
for ( p = pcache ; p ; p = p - > next ) {
2014-07-25 14:18:54 +04:00
status = printer_list_set_printer ( talloc_tos ( ) , p - > name ,
p - > comment , p - > location , t ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
return false ;
}
2008-10-10 22:55:14 +04:00
}
2010-12-28 16:55:01 +03:00
status = printer_list_clean_old ( ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Failed to cleanup printer list! \n " ) ) ;
return false ;
}
return true ;
2008-10-10 22:55:14 +04:00
}
2010-08-09 00:53:02 +04:00
void pcap_cache_reload ( struct tevent_context * ev ,
2010-12-19 21:52:08 +03:00
struct messaging_context * msg_ctx ,
void ( * post_cache_fill_fn ) ( struct tevent_context * ,
struct messaging_context * ) )
1996-05-04 11:50:46 +04:00
{
2005-01-05 19:20:35 +03:00
const char * pcap_name = lp_printcapname ( ) ;
2007-10-19 04:40:25 +04:00
bool pcap_reloaded = False ;
2010-12-19 21:52:08 +03:00
bool post_cache_fill_fn_handled = false ;
2014-07-22 22:17:38 +04:00
struct pcap_cache * pcache = NULL ;
2005-01-05 19:20:35 +03:00
DEBUG ( 3 , ( " reloading printcap cache \n " ) ) ;
2019-01-29 03:55:04 +03:00
if ( ! lp_load_printers ( ) ) {
DBG_NOTICE ( " skipping reload - load printers disabled \n " ) ;
return ;
}
2005-01-05 19:20:35 +03:00
/* only go looking if no printcap name supplied */
if ( pcap_name = = NULL | | * pcap_name = = 0 ) {
DEBUG ( 0 , ( " No printcap file name configured! \n " ) ) ;
return ;
}
2001-08-23 23:06:20 +04:00
# ifdef HAVE_CUPS
2005-01-05 19:20:35 +03:00
if ( strequal ( pcap_name , " cups " ) ) {
2010-12-19 21:52:08 +03:00
pcap_reloaded = cups_cache_reload ( ev , msg_ctx ,
post_cache_fill_fn ) ;
/*
* cups_cache_reload ( ) is async and calls post_cache_fill_fn ( )
* on successful completion
*/
post_cache_fill_fn_handled = true ;
2005-01-05 19:20:35 +03:00
goto done ;
}
# endif
1999-12-13 16:27:58 +03:00
2005-09-21 03:28:22 +04:00
# ifdef HAVE_IPRINT
if ( strequal ( pcap_name , " iprint " ) ) {
2014-07-22 22:17:38 +04:00
pcap_reloaded = iprint_cache_reload ( & pcache ) ;
2005-09-21 03:28:22 +04:00
goto done ;
}
# endif
2005-03-08 01:10:27 +03:00
# if defined(SYSV) || defined(HPUX)
2005-01-05 19:20:35 +03:00
if ( strequal ( pcap_name , " lpstat " ) ) {
2014-07-22 22:17:38 +04:00
pcap_reloaded = sysv_cache_reload ( & pcache ) ;
2005-01-05 19:20:35 +03:00
goto done ;
}
1997-12-03 08:08:07 +03:00
# endif
1996-05-04 11:50:46 +04:00
# ifdef AIX
2005-01-05 19:20:35 +03:00
if ( strstr_m ( pcap_name , " /qconfig " ) ! = NULL ) {
2014-07-22 22:17:38 +04:00
pcap_reloaded = aix_cache_reload ( & pcache ) ;
2005-01-05 19:20:35 +03:00
goto done ;
}
1996-05-04 11:50:46 +04:00
# endif
1997-12-03 08:08:07 +03:00
2014-07-22 22:17:38 +04:00
pcap_reloaded = std_pcap_cache_reload ( pcap_name , & pcache ) ;
2005-01-05 19:20:35 +03:00
2014-03-14 12:07:16 +04:00
/* Fix silly compiler warning about done not being used if none of the above
* ifdefs are used */
# if defined(HAVE_CUPS) || defined(HAVE_IPRINT) || defined(SYSV) || defined(HPUX) || defined(AIX)
2005-01-05 19:20:35 +03:00
done :
2014-03-14 12:07:16 +04:00
# endif
2005-01-05 19:20:35 +03:00
DEBUG ( 3 , ( " reload status: %s \n " , ( pcap_reloaded ) ? " ok " : " error " ) ) ;
2010-12-28 16:55:01 +03:00
if ( ( pcap_reloaded ) & & ( post_cache_fill_fn_handled = = false ) ) {
2010-05-14 02:42:55 +04:00
/* cleanup old entries only if the operation was successful,
* otherwise keep around the old entries until we can
2011-12-08 13:30:16 +04:00
* successfully reload */
2014-07-22 22:17:38 +04:00
if ( ! pcap_cache_replace ( pcache ) ) {
DEBUG ( 0 , ( " Failed to replace printer list! \n " ) ) ;
2010-05-14 02:42:55 +04:00
}
2014-07-22 22:17:38 +04:00
2010-12-28 16:55:01 +03:00
if ( post_cache_fill_fn ! = NULL ) {
2010-12-19 21:52:08 +03:00
post_cache_fill_fn ( ev , msg_ctx ) ;
}
2005-01-05 19:20:35 +03:00
}
2014-07-22 22:17:38 +04:00
pcap_cache_destroy_specific ( & pcache ) ;
2005-01-05 19:20:35 +03:00
return ;
1996-05-04 11:50:46 +04:00
}
/***************************************************************************
2008-10-10 22:55:14 +04:00
run a function on each printer name in the printcap file .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2008-10-10 22:55:14 +04:00
void pcap_printer_fn_specific ( const struct pcap_cache * pc ,
2011-05-13 12:02:42 +04:00
void ( * fn ) ( const char * , const char * , const char * , void * ) ,
2008-10-10 22:55:14 +04:00
void * pdata )
1996-05-04 11:50:46 +04:00
{
2008-10-10 22:55:14 +04:00
const struct pcap_cache * p ;
1996-05-04 11:50:46 +04:00
2008-10-10 22:55:14 +04:00
for ( p = pc ; p ! = NULL ; p = p - > next )
2011-05-13 12:02:42 +04:00
fn ( p - > name , p - > comment , p - > location , pdata ) ;
1996-05-04 11:50:46 +04:00
2005-01-05 19:20:35 +03:00
return ;
1996-05-04 11:50:46 +04:00
}