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 ;
}
2011-05-13 12:02:42 +04:00
bool pcap_cache_add ( const char * name , const char * comment , const char * location )
2008-10-10 22:55:14 +04:00
{
2010-05-14 02:42:55 +04:00
NTSTATUS status ;
2010-09-15 20:23:50 +04:00
time_t t = time_mono ( NULL ) ;
2010-05-14 02:42:55 +04:00
2011-05-13 12:02:42 +04:00
status = printer_list_set_printer ( talloc_tos ( ) , name , comment , location , t ) ;
2010-05-14 02:42:55 +04:00
return NT_STATUS_IS_OK ( status ) ;
1996-05-04 11:50:46 +04:00
}
2007-10-19 04:40:25 +04:00
bool pcap_cache_loaded ( void )
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 ) ;
return NT_STATUS_IS_OK ( status ) ;
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 ;
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 ) {
2011-05-13 12:02:42 +04:00
pcap_cache_add ( p - > name , p - > comment , p - > location ) ;
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-05-14 02:42:55 +04:00
NTSTATUS status ;
2010-12-19 21:52:08 +03:00
bool post_cache_fill_fn_handled = false ;
2005-01-05 19:20:35 +03:00
DEBUG ( 3 , ( " reloading printcap cache \n " ) ) ;
/* only go looking if no printcap name supplied */
if ( pcap_name = = NULL | | * pcap_name = = 0 ) {
DEBUG ( 0 , ( " No printcap file name configured! \n " ) ) ;
return ;
}
2010-05-14 02:42:55 +04:00
status = printer_list_mark_reload ( ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Failed to mark printer list for reload! \n " ) ) ;
return ;
}
1997-12-03 08:08:07 +03:00
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 " ) ) {
pcap_reloaded = iprint_cache_reload ( ) ;
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 " ) ) {
pcap_reloaded = sysv_cache_reload ( ) ;
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 ) {
pcap_reloaded = aix_cache_reload ( ) ;
goto done ;
}
1996-05-04 11:50:46 +04:00
# endif
1997-12-03 08:08:07 +03:00
2010-05-13 22:23:23 +04:00
pcap_reloaded = std_pcap_cache_reload ( pcap_name ) ;
2005-01-05 19:20:35 +03:00
done :
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 */
2010-05-14 02:42:55 +04:00
status = printer_list_clean_old ( ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 0 , ( " Failed to cleanup printer list! \n " ) ) ;
}
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
}
return ;
1996-05-04 11:50:46 +04:00
}
2007-10-19 04:40:25 +04:00
bool pcap_printername_ok ( const char * printername )
2005-01-05 19:20:35 +03:00
{
2010-05-14 02:42:55 +04:00
NTSTATUS status ;
2005-01-05 19:20:35 +03:00
2011-05-13 12:02:42 +04:00
status = printer_list_get_printer ( talloc_tos ( ) , printername , NULL , NULL , 0 ) ;
2010-05-14 02:42:55 +04:00
return NT_STATUS_IS_OK ( status ) ;
2005-01-05 19:20:35 +03:00
}
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
}
2008-10-10 22:55:14 +04:00
2011-05-13 12:02:42 +04:00
void pcap_printer_fn ( void ( * fn ) ( const char * , const char * , const char * , void * ) , void * pdata )
2008-10-10 22:55:14 +04:00
{
2010-05-14 02:42:55 +04:00
NTSTATUS status ;
status = printer_list_run_fn ( fn , pdata ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
DEBUG ( 3 , ( " Failed to run fn for all printers! \n " ) ) ;
}
return ;
2008-10-10 22:55:14 +04:00
}