1999-12-17 04:46:15 +03:00
/*
* Support code for the Common UNIX Printing System ( " CUPS " )
*
2003-01-28 04:58:38 +03:00
* Copyright 1999 - 2003 by Michael R Sweet .
2008-10-02 03:40:41 +04:00
* Copyright 2008 Jeremy Allison .
1999-12-17 04:46:15 +03: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
1999-12-17 04:46:15 +03:00
* ( at your option ) any later version .
2008-10-02 02:01:05 +04:00
*
1999-12-17 04:46:15 +03:00
* 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 .
2008-10-02 02:01:05 +04:00
*
1999-12-17 04:46:15 +03:00
* You should have received a copy of the GNU General Public License
2007-07-10 09:23:25 +04:00
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
1999-12-17 04:46:15 +03:00
*/
2008-10-02 03:40:41 +04:00
/*
* JRA . Converted to utf8 pull / push .
*/
2003-11-12 04:51:10 +03:00
# include "includes.h"
2001-03-16 08:55:30 +03:00
# include "printing.h"
2010-05-14 16:39:40 +04:00
# include "printing/pcap.h"
2011-03-08 18:36:03 +03:00
# include "librpc/gen_ndr/ndr_printcap.h"
2015-10-12 16:57:34 +03:00
# include "lib/util/sys_rw.h"
2020-08-07 21:17:34 +03:00
# include "lib/util/string_wrappers.h"
1999-12-17 04:46:15 +03:00
2001-08-23 23:06:20 +04:00
# ifdef HAVE_CUPS
1999-12-17 04:46:15 +03:00
# include <cups/cups.h>
# include <cups/language.h>
2016-12-06 11:44:28 +03:00
# include <cups/http.h>
/* CUPS prior to version 1.7 doesn't have HTTP_URI_STATUS_OK */
# if (CUPS_VERSION_MAJOR == 1) && (CUPS_VERSION_MINOR < 7)
# define HTTP_URI_STATUS_OK HTTP_URI_OK
# endif
1999-12-17 04:46:15 +03:00
2012-07-19 21:17:28 +04:00
# if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
# define HAVE_CUPS_1_6 1
# endif
# ifndef HAVE_CUPS_1_6
# define ippGetGroupTag(attr) attr->group_tag
# define ippGetName(attr) attr->name
# define ippGetValueTag(attr) attr->value_tag
# define ippGetStatusCode(ipp) ipp->request.status.status_code
# define ippGetInteger(attr, element) attr->values[element].integer
# define ippGetString(attr, element, language) attr->values[element].string.text
static ipp_attribute_t *
ippFirstAttribute ( ipp_t * ipp )
{
if ( ! ipp )
return ( NULL ) ;
return ( ipp - > current = ipp - > attrs ) ;
}
static ipp_attribute_t *
ippNextAttribute ( ipp_t * ipp )
{
if ( ! ipp | | ! ipp - > current )
return ( NULL ) ;
return ( ipp - > current = ipp - > current - > next ) ;
}
static int ippSetOperation ( ipp_t * ipp , ipp_op_t op )
{
ipp - > request . op . operation_id = op ;
return ( 1 ) ;
}
static int ippSetRequestId ( ipp_t * ipp , int request_id )
{
ipp - > request . any . request_id = request_id ;
return ( 1 ) ;
}
# endif
2008-09-24 03:54:05 +04:00
static SIG_ATOMIC_T gotalarm ;
/***************************************************************
Signal function to tell us we timed out .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2010-02-19 18:13:46 +03:00
static void gotalarm_sig ( int signum )
2008-09-24 03:54:05 +04:00
{
gotalarm = 1 ;
}
2006-12-18 23:37:26 +03:00
extern userdom_struct current_user_info ;
2001-03-16 08:55:30 +03:00
/*
* ' cups_passwd_cb ( ) ' - The CUPS password callback . . .
*/
2003-01-15 21:57:41 +03:00
static const char * /* O - Password or NULL */
2001-03-16 08:55:30 +03:00
cups_passwd_cb ( const char * prompt ) /* I - Prompt */
{
2005-01-21 03:29:38 +03:00
/*
* Always return NULL to indicate that no password is available . . .
*/
2001-03-16 08:55:30 +03:00
2005-01-21 03:29:38 +03:00
return ( NULL ) ;
2001-03-16 08:55:30 +03:00
}
2008-10-02 03:40:41 +04:00
static http_t * cups_connect ( TALLOC_CTX * frame )
2004-06-02 18:58:18 +04:00
{
2019-11-04 19:19:58 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2008-10-02 03:40:41 +04:00
http_t * http = NULL ;
char * server = NULL , * p = NULL ;
2006-08-25 02:10:59 +04:00
int port ;
2008-09-24 20:53:21 +04:00
int timeout = lp_cups_connection_timeout ( ) ;
2008-10-02 03:40:41 +04:00
size_t size ;
2008-09-24 03:54:05 +04:00
2019-11-04 19:19:58 +03:00
if ( lp_cups_server ( talloc_tos ( ) , lp_sub ) ! = NULL & & strlen ( lp_cups_server ( talloc_tos ( ) , lp_sub ) ) > 0 ) {
if ( ! push_utf8_talloc ( frame , & server , lp_cups_server ( talloc_tos ( ) , lp_sub ) , & size ) ) {
2008-10-02 03:40:41 +04:00
return NULL ;
}
2006-08-25 02:10:59 +04:00
} else {
2008-10-02 03:40:41 +04:00
server = talloc_strdup ( frame , cupsServer ( ) ) ;
}
2008-10-02 23:21:11 +04:00
if ( ! server ) {
2008-10-02 03:40:41 +04:00
return NULL ;
2006-08-25 02:10:59 +04:00
}
p = strchr ( server , ' : ' ) ;
if ( p ) {
port = atoi ( p + 1 ) ;
* p = ' \0 ' ;
} else {
port = ippPort ( ) ;
}
2008-09-24 03:54:05 +04:00
2006-08-25 02:10:59 +04:00
DEBUG ( 10 , ( " connecting to cups server %s:%d \n " ,
server , port ) ) ;
2008-10-02 03:40:41 +04:00
gotalarm = 0 ;
if ( timeout ) {
2010-02-19 18:13:46 +03:00
CatchSignal ( SIGALRM , gotalarm_sig ) ;
2008-10-02 03:40:41 +04:00
alarm ( timeout ) ;
}
2019-11-20 13:27:10 +03:00
# if defined(HAVE_HTTPCONNECT2)
http = httpConnect2 ( server ,
port ,
NULL ,
AF_UNSPEC ,
lp_cups_encrypt ( ) ?
HTTP_ENCRYPTION_ALWAYS :
HTTP_ENCRYPTION_IF_REQUESTED ,
1 , /* blocking */
30 * 1000 , /* timeout */
NULL ) ;
# elif defined(HAVE_HTTPCONNECTENCRYPT)
2009-03-27 15:05:00 +03:00
http = httpConnectEncrypt ( server , port , lp_cups_encrypt ( ) ) ;
# else
2008-09-24 03:54:05 +04:00
http = httpConnect ( server , port ) ;
2009-03-27 15:05:00 +03:00
# endif
2008-09-24 03:54:05 +04:00
2010-02-19 18:13:46 +03:00
CatchSignal ( SIGALRM , SIG_IGN ) ;
2008-09-24 03:54:05 +04:00
alarm ( 0 ) ;
if ( http = = NULL ) {
2015-03-04 12:15:10 +03:00
DEBUG ( 3 , ( " Unable to connect to CUPS server %s:%d - %s \n " ,
2006-08-25 02:10:59 +04:00
server , port , strerror ( errno ) ) ) ;
2004-06-02 18:58:18 +04:00
}
2006-08-25 02:10:59 +04:00
return http ;
2004-06-02 18:58:18 +04:00
}
2001-03-16 08:55:30 +03:00
2011-03-08 18:36:03 +03:00
static bool send_pcap_blob ( DATA_BLOB * pcap_blob , int fd )
2008-10-10 22:55:14 +04:00
{
2011-03-08 18:36:03 +03:00
size_t ret ;
ret = sys_write ( fd , & pcap_blob - > length , sizeof ( pcap_blob - > length ) ) ;
if ( ret ! = sizeof ( pcap_blob - > length ) ) {
return false ;
2008-10-10 22:55:14 +04:00
}
2011-03-08 18:36:03 +03:00
ret = sys_write ( fd , pcap_blob - > data , pcap_blob - > length ) ;
if ( ret ! = pcap_blob - > length ) {
return false ;
2008-10-10 22:55:14 +04:00
}
2011-03-08 18:36:03 +03:00
2011-04-22 04:22:44 +04:00
DEBUG ( 10 , ( " successfully sent blob of len %d \n " , ( int ) ret ) ) ;
2011-03-08 18:36:03 +03:00
return true ;
}
static bool recv_pcap_blob ( TALLOC_CTX * mem_ctx , int fd , DATA_BLOB * pcap_blob )
{
size_t blob_len ;
size_t ret ;
ret = sys_read ( fd , & blob_len , sizeof ( blob_len ) ) ;
if ( ret ! = sizeof ( blob_len ) ) {
return false ;
2008-10-10 22:55:14 +04:00
}
2011-03-08 18:36:03 +03:00
* pcap_blob = data_blob_talloc_named ( mem_ctx , NULL , blob_len ,
" cups pcap " ) ;
if ( pcap_blob - > length ! = blob_len ) {
return false ;
2008-10-10 22:55:14 +04:00
}
2011-03-08 18:36:03 +03:00
ret = sys_read ( fd , pcap_blob - > data , blob_len ) ;
if ( ret ! = blob_len ) {
talloc_free ( pcap_blob - > data ) ;
return false ;
}
2011-04-22 04:22:44 +04:00
DEBUG ( 10 , ( " successfully recvd blob of len %d \n " , ( int ) ret ) ) ;
2011-03-08 18:36:03 +03:00
return true ;
2008-10-10 22:55:14 +04:00
}
2011-03-09 16:05:39 +03:00
static bool process_cups_printers_response ( TALLOC_CTX * mem_ctx ,
ipp_t * response ,
struct pcap_data * pcap_data )
{
ipp_attribute_t * attr ;
char * name ;
char * info ;
2011-05-13 12:02:42 +04:00
char * location = NULL ;
2011-03-09 16:05:39 +03:00
struct pcap_printer * printer ;
bool ret_ok = false ;
2012-07-19 21:17:28 +04:00
for ( attr = ippFirstAttribute ( response ) ; attr ! = NULL ; ) {
2011-03-09 16:05:39 +03:00
/*
* Skip leading attributes until we hit a printer . . .
*/
2012-07-19 21:17:28 +04:00
while ( attr ! = NULL & & ippGetGroupTag ( attr ) ! = IPP_TAG_PRINTER )
attr = ippNextAttribute ( response ) ;
2011-03-09 16:05:39 +03:00
if ( attr = = NULL )
break ;
/*
* Pull the needed attributes from this printer . . .
*/
name = NULL ;
info = NULL ;
2012-07-19 21:17:28 +04:00
while ( attr ! = NULL & & ippGetGroupTag ( attr ) = = IPP_TAG_PRINTER ) {
2011-03-09 16:05:39 +03:00
size_t size ;
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " printer-name " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_NAME ) {
2011-03-09 16:05:39 +03:00
if ( ! pull_utf8_talloc ( mem_ctx ,
& name ,
2012-07-19 21:17:28 +04:00
ippGetString ( attr , 0 , NULL ) ,
2011-03-09 16:05:39 +03:00
& size ) ) {
goto err_out ;
}
}
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " printer-info " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_TEXT ) {
2011-03-09 16:05:39 +03:00
if ( ! pull_utf8_talloc ( mem_ctx ,
& info ,
2012-07-19 21:17:28 +04:00
ippGetString ( attr , 0 , NULL ) ,
2011-03-09 16:05:39 +03:00
& size ) ) {
goto err_out ;
}
}
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " printer-location " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_TEXT ) {
2011-05-13 12:02:42 +04:00
if ( ! pull_utf8_talloc ( mem_ctx ,
& location ,
2012-07-19 21:17:28 +04:00
ippGetString ( attr , 0 , NULL ) ,
2011-05-13 12:02:42 +04:00
& size ) ) {
goto err_out ;
}
}
2012-07-19 21:17:28 +04:00
attr = ippNextAttribute ( response ) ;
2011-03-09 16:05:39 +03:00
}
/*
* See if we have everything needed . . .
*/
if ( name = = NULL )
break ;
if ( pcap_data - > count = = 0 ) {
printer = talloc_array ( mem_ctx , struct pcap_printer , 1 ) ;
} else {
printer = talloc_realloc ( mem_ctx , pcap_data - > printers ,
struct pcap_printer ,
pcap_data - > count + 1 ) ;
}
if ( printer = = NULL ) {
goto err_out ;
}
pcap_data - > printers = printer ;
pcap_data - > printers [ pcap_data - > count ] . name = name ;
pcap_data - > printers [ pcap_data - > count ] . info = info ;
2011-05-13 12:02:42 +04:00
pcap_data - > printers [ pcap_data - > count ] . location = location ;
2011-03-09 16:05:39 +03:00
pcap_data - > count + + ;
}
ret_ok = true ;
err_out :
return ret_ok ;
}
/*
* request printer list from cups , send result back to up parent via fd .
2017-02-17 22:51:29 +03:00
* returns true if the ( possibly failed ) result was successfully sent to parent .
2011-03-09 16:05:39 +03:00
*/
2008-10-10 22:55:14 +04:00
static bool cups_cache_reload_async ( int fd )
1999-12-17 04:46:15 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2011-03-08 18:36:03 +03:00
struct pcap_data pcap_data ;
2005-01-21 03:29:38 +03:00
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
cups_lang_t * language = NULL ; /* Default language */
2001-03-16 08:55:30 +03:00
static const char * requested [ ] = /* Requested attributes */
{
" printer-name " ,
2011-05-13 12:02:42 +04:00
" printer-info " ,
" printer-location "
2008-10-02 02:01:05 +04:00
} ;
2007-10-19 04:40:25 +04:00
bool ret = False ;
2011-03-08 18:36:03 +03:00
enum ndr_err_code ndr_ret ;
DATA_BLOB pcap_blob ;
ZERO_STRUCT ( pcap_data ) ;
pcap_data . status = NT_STATUS_UNSUCCESSFUL ;
2001-03-16 08:55:30 +03:00
2005-01-05 19:20:35 +03:00
DEBUG ( 5 , ( " reloading cups printcap cache \n " ) ) ;
2001-03-16 08:55:30 +03:00
/*
* Make sure we don ' t ask for passwords . . .
*/
1999-12-17 04:46:15 +03:00
2001-03-16 08:55:30 +03:00
cupsSetPasswordCB ( cups_passwd_cb ) ;
1999-12-17 04:46:15 +03:00
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
1999-12-17 04:46:15 +03:00
/*
* Build a CUPS_GET_PRINTERS request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
2001-03-16 08:55:30 +03:00
* requested - attributes
1999-12-17 04:46:15 +03:00
*/
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , CUPS_GET_PRINTERS ) ;
ippSetRequestId ( request , 1 ) ;
1999-12-17 04:46:15 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
1999-12-17 04:46:15 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2001-03-16 08:55:30 +03:00
ippAddStrings ( request , IPP_TAG_OPERATION , IPP_TAG_NAME ,
" requested-attributes " ,
( sizeof ( requested ) / sizeof ( requested [ 0 ] ) ) ,
NULL , requested ) ;
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " / " ) ) = = NULL ) {
2001-03-16 08:55:30 +03:00
DEBUG ( 0 , ( " Unable to get printer list - %s \n " ,
ippErrorString ( cupsLastError ( ) ) ) ) ;
2005-01-21 03:29:38 +03:00
goto out ;
1999-12-17 04:46:15 +03:00
}
2011-03-09 16:05:39 +03:00
ret = process_cups_printers_response ( frame , response , & pcap_data ) ;
if ( ! ret ) {
DEBUG ( 0 , ( " failed to process cups response \n " ) ) ;
goto out ;
1999-12-17 04:46:15 +03:00
}
ippDelete ( response ) ;
2005-01-21 03:29:38 +03:00
response = NULL ;
2003-01-28 04:58:38 +03:00
/*
* Build a CUPS_GET_CLASSES request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* requested - attributes
*/
2005-01-21 21:14:31 +03:00
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , CUPS_GET_CLASSES ) ;
ippSetRequestId ( request , 1 ) ;
2003-01-28 04:58:38 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2003-01-28 04:58:38 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
ippAddStrings ( request , IPP_TAG_OPERATION , IPP_TAG_NAME ,
" requested-attributes " ,
( sizeof ( requested ) / sizeof ( requested [ 0 ] ) ) ,
NULL , requested ) ;
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " / " ) ) = = NULL ) {
2003-01-28 04:58:38 +03:00
DEBUG ( 0 , ( " Unable to get printer list - %s \n " ,
ippErrorString ( cupsLastError ( ) ) ) ) ;
2005-01-21 03:29:38 +03:00
goto out ;
2003-01-28 04:58:38 +03:00
}
2011-03-09 16:05:39 +03:00
ret = process_cups_printers_response ( frame , response , & pcap_data ) ;
if ( ! ret ) {
DEBUG ( 0 , ( " failed to process cups response \n " ) ) ;
goto out ;
2003-01-28 04:58:38 +03:00
}
2011-03-08 18:36:03 +03:00
pcap_data . status = NT_STATUS_OK ;
2005-01-21 03:29:38 +03:00
out :
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2003-01-28 04:58:38 +03:00
2011-03-09 16:05:39 +03:00
ret = false ;
2011-03-08 18:36:03 +03:00
ndr_ret = ndr_push_struct_blob ( & pcap_blob , frame , & pcap_data ,
( ndr_push_flags_fn_t ) ndr_push_pcap_data ) ;
if ( ndr_ret = = NDR_ERR_SUCCESS ) {
ret = send_pcap_blob ( & pcap_blob , fd ) ;
2008-10-10 22:55:14 +04:00
}
2011-03-08 18:36:03 +03:00
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2005-01-21 03:29:38 +03:00
return ret ;
1999-12-17 04:46:15 +03:00
}
2013-02-18 13:24:12 +04:00
static struct tevent_fd * cache_fd_event ;
2008-10-10 22:55:14 +04:00
2010-08-09 00:47:45 +04:00
static bool cups_pcap_load_async ( struct tevent_context * ev ,
struct messaging_context * msg_ctx ,
int * pfd )
2008-10-10 22:55:14 +04:00
{
int fds [ 2 ] ;
pid_t pid ;
2010-07-04 18:28:13 +04:00
NTSTATUS status ;
2008-10-10 22:55:14 +04:00
2008-10-11 03:29:14 +04:00
* pfd = - 1 ;
2008-10-10 22:55:14 +04:00
if ( cache_fd_event ) {
DEBUG ( 3 , ( " cups_pcap_load_async: already waiting for "
" a refresh event \n " ) ) ;
return false ;
}
DEBUG ( 5 , ( " cups_pcap_load_async: asynchronously loading cups printers \n " ) ) ;
if ( pipe ( fds ) = = - 1 ) {
return false ;
}
2012-03-24 23:17:08 +04:00
pid = fork ( ) ;
2008-10-10 22:55:14 +04:00
if ( pid = = ( pid_t ) - 1 ) {
DEBUG ( 10 , ( " cups_pcap_load_async: fork failed %s \n " ,
strerror ( errno ) ) ) ;
close ( fds [ 0 ] ) ;
close ( fds [ 1 ] ) ;
return false ;
}
if ( pid ) {
DEBUG ( 10 , ( " cups_pcap_load_async: child pid = %u \n " ,
( unsigned int ) pid ) ) ;
/* Parent. */
close ( fds [ 1 ] ) ;
* pfd = fds [ 0 ] ;
return true ;
}
/* Child. */
2009-01-06 00:47:34 +03:00
close_all_print_db ( ) ;
2022-12-03 19:04:33 +03:00
status = reinit_after_fork ( msg_ctx , ev , true ) ;
2010-07-04 18:28:13 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2008-10-11 03:43:13 +04:00
DEBUG ( 0 , ( " cups_pcap_load_async: reinit_after_fork() failed \n " ) ) ;
smb_panic ( " cups_pcap_load_async: reinit_after_fork() failed " ) ;
}
2008-10-10 22:55:14 +04:00
close ( fds [ 0 ] ) ;
cups_cache_reload_async ( fds [ 1 ] ) ;
close ( fds [ 1 ] ) ;
2014-04-11 11:09:49 +04:00
TALLOC_FREE ( msg_ctx ) ;
2008-10-10 22:55:14 +04:00
_exit ( 0 ) ;
}
2010-12-19 21:52:08 +03:00
struct cups_async_cb_args {
int pipe_fd ;
2013-02-18 12:58:10 +04:00
struct tevent_context * event_ctx ;
2010-12-19 21:52:08 +03:00
struct messaging_context * msg_ctx ;
2013-02-18 12:58:10 +04:00
void ( * post_cache_fill_fn ) ( struct tevent_context * ,
2010-12-19 21:52:08 +03:00
struct messaging_context * ) ;
} ;
2013-02-18 12:58:10 +04:00
static void cups_async_callback ( struct tevent_context * event_ctx ,
2013-02-18 13:24:12 +04:00
struct tevent_fd * event ,
2015-05-09 22:33:40 +03:00
uint16_t flags ,
2008-10-10 22:55:14 +04:00
void * p )
{
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2010-12-19 21:52:08 +03:00
struct cups_async_cb_args * cb_args = ( struct cups_async_cb_args * ) p ;
2008-10-10 22:55:14 +04:00
struct pcap_cache * tmp_pcap_cache = NULL ;
2011-03-08 18:36:03 +03:00
bool ret_ok ;
struct pcap_data pcap_data ;
DATA_BLOB pcap_blob ;
enum ndr_err_code ndr_ret ;
2020-01-09 16:42:43 +03:00
uint32_t i ;
2008-10-10 22:55:14 +04:00
DEBUG ( 5 , ( " cups_async_callback: callback received for printer data. "
2011-03-08 18:36:03 +03:00
" fd = %d \n " , cb_args - > pipe_fd ) ) ;
2008-10-10 22:55:14 +04:00
2011-03-08 18:36:03 +03:00
ret_ok = recv_pcap_blob ( frame , cb_args - > pipe_fd , & pcap_blob ) ;
if ( ! ret_ok ) {
DEBUG ( 0 , ( " failed to recv pcap blob \n " ) ) ;
goto err_out ;
}
2008-10-11 03:29:14 +04:00
2011-03-08 18:36:03 +03:00
ndr_ret = ndr_pull_struct_blob ( & pcap_blob , frame , & pcap_data ,
( ndr_pull_flags_fn_t ) ndr_pull_pcap_data ) ;
if ( ndr_ret ! = NDR_ERR_SUCCESS ) {
goto err_out ;
}
2008-10-11 03:29:14 +04:00
2011-03-08 18:36:03 +03:00
if ( ! NT_STATUS_IS_OK ( pcap_data . status ) ) {
2015-03-04 12:15:10 +03:00
DEBUG ( 3 , ( " failed to retrieve printer list: %s \n " ,
2011-03-08 18:36:03 +03:00
nt_errstr ( pcap_data . status ) ) ) ;
goto err_out ;
}
2008-10-10 22:55:14 +04:00
2011-03-08 18:36:03 +03:00
for ( i = 0 ; i < pcap_data . count ; i + + ) {
ret_ok = pcap_cache_add_specific ( & tmp_pcap_cache ,
pcap_data . printers [ i ] . name ,
2011-05-13 12:02:42 +04:00
pcap_data . printers [ i ] . info ,
pcap_data . printers [ i ] . location ) ;
2010-12-30 17:58:48 +03:00
if ( ! ret_ok ) {
DEBUG ( 0 , ( " failed to add to tmp pcap cache \n " ) ) ;
2011-03-09 17:18:22 +03:00
goto err_out ;
2010-12-30 17:58:48 +03:00
}
2008-10-10 22:55:14 +04:00
}
2011-03-09 17:18:22 +03:00
/* replace the system-wide pcap cache with a (possibly empty) new one */
ret_ok = pcap_cache_replace ( tmp_pcap_cache ) ;
2010-12-30 17:58:48 +03:00
if ( ! ret_ok ) {
2011-03-09 17:18:22 +03:00
DEBUG ( 0 , ( " failed to replace pcap cache \n " ) ) ;
} else if ( cb_args - > post_cache_fill_fn ! = NULL ) {
/* Caller requested post cache fill callback */
cb_args - > post_cache_fill_fn ( cb_args - > event_ctx ,
cb_args - > msg_ctx ) ;
2008-10-10 22:55:14 +04:00
}
2011-03-08 18:36:03 +03:00
err_out :
2011-03-09 17:18:22 +03:00
pcap_cache_destroy_specific ( & tmp_pcap_cache ) ;
2011-03-08 18:36:03 +03:00
TALLOC_FREE ( frame ) ;
2020-01-09 16:43:02 +03:00
TALLOC_FREE ( cache_fd_event ) ;
2011-03-08 18:36:03 +03:00
close ( cb_args - > pipe_fd ) ;
2010-12-19 21:52:08 +03:00
TALLOC_FREE ( cb_args ) ;
2008-10-10 22:55:14 +04:00
}
2010-08-09 00:50:28 +04:00
bool cups_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 * ) )
2008-10-10 22:55:14 +04:00
{
2010-12-19 21:52:08 +03:00
struct cups_async_cb_args * cb_args ;
int * p_pipe_fd ;
2008-10-11 02:55:49 +04:00
2011-06-07 05:38:41 +04:00
cb_args = talloc ( NULL , struct cups_async_cb_args ) ;
2010-12-19 21:52:08 +03:00
if ( cb_args = = NULL ) {
2008-10-11 02:55:49 +04:00
return false ;
}
2008-10-10 22:55:14 +04:00
2010-12-19 21:52:08 +03:00
cb_args - > post_cache_fill_fn = post_cache_fill_fn ;
cb_args - > event_ctx = ev ;
cb_args - > msg_ctx = msg_ctx ;
p_pipe_fd = & cb_args - > pipe_fd ;
2008-10-11 03:48:18 +04:00
* p_pipe_fd = - 1 ;
2008-10-10 22:55:14 +04:00
/* Set up an async refresh. */
2010-08-09 00:50:28 +04:00
if ( ! cups_pcap_load_async ( ev , msg_ctx , p_pipe_fd ) ) {
2010-12-19 21:52:08 +03:00
talloc_free ( cb_args ) ;
2008-10-10 22:55:14 +04:00
return false ;
}
2010-12-30 16:32:29 +03:00
DEBUG ( 10 , ( " cups_cache_reload: async read on fd %d \n " ,
* p_pipe_fd ) ) ;
/* Trigger an event when the pipe can be read. */
2013-02-18 13:53:02 +04:00
cache_fd_event = tevent_add_fd ( ev ,
2010-12-30 16:32:29 +03:00
NULL , * p_pipe_fd ,
2013-02-18 13:53:02 +04:00
TEVENT_FD_READ ,
2010-12-30 16:32:29 +03:00
cups_async_callback ,
( void * ) cb_args ) ;
if ( ! cache_fd_event ) {
close ( * p_pipe_fd ) ;
TALLOC_FREE ( cb_args ) ;
return false ;
2008-10-10 22:55:14 +04:00
}
2010-12-30 16:32:29 +03:00
2008-10-10 22:55:14 +04:00
return true ;
}
2001-03-16 08:55:30 +03:00
/*
* ' cups_job_delete ( ) ' - Delete a job .
*/
2005-09-30 21:13:37 +04:00
static int cups_job_delete ( const char * sharename , const char * lprm_command , struct printjob * pjob )
2001-03-16 08:55:30 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2005-01-21 03:29:38 +03:00
int ret = 1 ; /* Return value */
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
cups_lang_t * language = NULL ; /* Default language */
2008-10-02 03:40:41 +04:00
char * user = NULL ;
2016-11-09 21:05:49 +03:00
char uri [ HTTP_MAX_URI ] = { 0 } ; /* printer-uri attribute */
http_uri_status_t ustatus ;
2008-10-02 03:40:41 +04:00
size_t size ;
2001-03-16 08:55:30 +03:00
2005-09-30 21:13:37 +04:00
DEBUG ( 5 , ( " cups_job_delete(%s, %p (%d)) \n " , sharename , pjob , pjob - > sysjob ) ) ;
2001-03-16 08:55:30 +03:00
/*
* Make sure we don ' t ask for passwords . . .
*/
cupsSetPasswordCB ( cups_passwd_cb ) ;
/*
* Try to connect to the server . . .
*/
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Build an IPP_CANCEL_JOB request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* job - uri
* requesting - user - name
*/
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_CANCEL_JOB ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /jobs/%d " ,
pjob - > sysjob ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI , " job-uri " , NULL , uri ) ;
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & user , pjob - > user , & size ) ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME , " requesting-user-name " ,
2008-10-02 03:40:41 +04:00
NULL , user ) ;
2001-03-16 08:55:30 +03:00
/*
* Do the request and get back a response . . .
*/
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " /jobs " ) ) ! = NULL ) {
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2005-01-21 03:29:38 +03:00
DEBUG ( 0 , ( " Unable to cancel job %d - %s \n " , pjob - > sysjob ,
ippErrorString ( cupsLastError ( ) ) ) ) ;
} else {
ret = 0 ;
}
} else {
2001-03-16 08:55:30 +03:00
DEBUG ( 0 , ( " Unable to cancel job %d - %s \n " , pjob - > sysjob ,
2005-01-21 03:29:38 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2001-03-16 08:55:30 +03:00
}
2005-01-21 03:29:38 +03:00
out :
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2005-01-21 03:29:38 +03:00
return ret ;
2001-03-16 08:55:30 +03:00
}
/*
* ' cups_job_pause ( ) ' - Pause a job .
*/
2005-01-21 03:29:38 +03:00
static int cups_job_pause ( int snum , struct printjob * pjob )
2001-03-16 08:55:30 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2005-01-21 03:29:38 +03:00
int ret = 1 ; /* Return value */
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
cups_lang_t * language = NULL ; /* Default language */
2008-10-02 03:40:41 +04:00
char * user = NULL ;
2016-11-09 21:05:49 +03:00
char uri [ HTTP_MAX_URI ] = { 0 } ; /* printer-uri attribute */
http_uri_status_t ustatus ;
2008-10-02 03:40:41 +04:00
size_t size ;
2001-03-16 08:55:30 +03:00
DEBUG ( 5 , ( " cups_job_pause(%d, %p (%d)) \n " , snum , pjob , pjob - > sysjob ) ) ;
/*
* Make sure we don ' t ask for passwords . . .
*/
cupsSetPasswordCB ( cups_passwd_cb ) ;
/*
* Try to connect to the server . . .
*/
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Build an IPP_HOLD_JOB request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* job - uri
* requesting - user - name
*/
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_HOLD_JOB ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /jobs/%d " ,
pjob - > sysjob ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI , " job-uri " , NULL , uri ) ;
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & user , pjob - > user , & size ) ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME , " requesting-user-name " ,
2008-10-02 03:40:41 +04:00
NULL , user ) ;
2001-03-16 08:55:30 +03:00
/*
* Do the request and get back a response . . .
*/
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " /jobs " ) ) ! = NULL ) {
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2005-01-21 03:29:38 +03:00
DEBUG ( 0 , ( " Unable to hold job %d - %s \n " , pjob - > sysjob ,
ippErrorString ( cupsLastError ( ) ) ) ) ;
} else {
ret = 0 ;
}
} else {
2001-03-16 08:55:30 +03:00
DEBUG ( 0 , ( " Unable to hold job %d - %s \n " , pjob - > sysjob ,
2005-01-21 03:29:38 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2001-03-16 08:55:30 +03:00
}
2005-01-21 03:29:38 +03:00
out :
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2005-01-21 03:29:38 +03:00
return ret ;
2001-03-16 08:55:30 +03:00
}
/*
* ' cups_job_resume ( ) ' - Resume a paused job .
*/
2005-01-21 03:29:38 +03:00
static int cups_job_resume ( int snum , struct printjob * pjob )
2001-03-16 08:55:30 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2005-01-21 03:29:38 +03:00
int ret = 1 ; /* Return value */
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
cups_lang_t * language = NULL ; /* Default language */
2008-10-02 03:40:41 +04:00
char * user = NULL ;
2016-11-09 21:05:49 +03:00
char uri [ HTTP_MAX_URI ] = { 0 } ; /* printer-uri attribute */
http_uri_status_t ustatus ;
2008-10-02 03:40:41 +04:00
size_t size ;
2001-03-16 08:55:30 +03:00
DEBUG ( 5 , ( " cups_job_resume(%d, %p (%d)) \n " , snum , pjob , pjob - > sysjob ) ) ;
/*
* Make sure we don ' t ask for passwords . . .
*/
cupsSetPasswordCB ( cups_passwd_cb ) ;
/*
* Try to connect to the server . . .
*/
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Build an IPP_RELEASE_JOB request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* job - uri
* requesting - user - name
*/
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_RELEASE_JOB ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /jobs/%d " ,
pjob - > sysjob ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI , " job-uri " , NULL , uri ) ;
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & user , pjob - > user , & size ) ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME , " requesting-user-name " ,
2008-10-02 03:40:41 +04:00
NULL , user ) ;
2001-03-16 08:55:30 +03:00
/*
* Do the request and get back a response . . .
*/
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " /jobs " ) ) ! = NULL ) {
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2005-01-21 03:29:38 +03:00
DEBUG ( 0 , ( " Unable to release job %d - %s \n " , pjob - > sysjob ,
ippErrorString ( cupsLastError ( ) ) ) ) ;
} else {
ret = 0 ;
}
} else {
2001-03-16 08:55:30 +03:00
DEBUG ( 0 , ( " Unable to release job %d - %s \n " , pjob - > sysjob ,
2005-01-21 03:29:38 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2001-03-16 08:55:30 +03:00
}
2005-01-21 03:29:38 +03:00
out :
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2005-01-21 03:29:38 +03:00
return ret ;
2001-03-16 08:55:30 +03:00
}
/*
* ' cups_job_submit ( ) ' - Submit a job for printing .
*/
2012-02-08 21:47:11 +04:00
static int cups_job_submit ( int snum , struct printjob * pjob ,
enum printing_types printing_type ,
char * lpq_cmd )
2001-03-16 08:55:30 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2019-10-15 17:54:45 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2005-01-21 03:29:38 +03:00
int ret = 1 ; /* Return value */
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
2010-03-25 03:33:21 +03:00
ipp_attribute_t * attr_job_id = NULL ; /* IPP Attribute "job-id" */
2005-01-21 03:29:38 +03:00
cups_lang_t * language = NULL ; /* Default language */
2016-11-09 21:05:49 +03:00
char uri [ HTTP_MAX_URI ] = { 0 } ; /* printer-uri attribute */
http_uri_status_t ustatus ;
2007-11-22 00:56:36 +03:00
char * new_jobname = NULL ;
int num_options = 0 ;
2005-01-21 03:29:38 +03:00
cups_option_t * options = NULL ;
2008-10-02 03:40:41 +04:00
char * printername = NULL ;
char * user = NULL ;
char * jobname = NULL ;
char * cupsoptions = NULL ;
char * filename = NULL ;
size_t size ;
2001-03-16 08:55:30 +03:00
2010-03-25 03:33:21 +03:00
DEBUG ( 5 , ( " cups_job_submit(%d, %p) \n " , snum , pjob ) ) ;
2001-03-16 08:55:30 +03:00
/*
* Make sure we don ' t ask for passwords . . .
*/
cupsSetPasswordCB ( cups_passwd_cb ) ;
/*
* Try to connect to the server . . .
*/
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Build an IPP_PRINT_JOB request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* printer - uri
* requesting - user - name
* [ document - data ]
*/
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_PRINT_JOB ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2012-07-18 09:37:23 +04:00
if ( ! push_utf8_talloc ( frame , & printername ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2010-06-13 14:11:26 +04:00
& size ) ) {
2008-10-02 03:40:41 +04:00
goto out ;
}
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /printers/%s " ,
printername ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI ,
" printer-uri " , NULL , uri ) ;
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & user , pjob - > user , & size ) ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME , " requesting-user-name " ,
2008-10-02 03:40:41 +04:00
NULL , user ) ;
2001-03-16 08:55:30 +03:00
2003-02-05 09:37:27 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME ,
" job-originating-host-name " , NULL ,
2010-08-16 15:53:10 +04:00
pjob - > clientmachine ) ;
2003-02-05 09:37:27 +03:00
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & jobname , pjob - > jobname , & size ) ) {
goto out ;
}
new_jobname = talloc_asprintf ( frame ,
" %s%.8u %s " , PRINT_SPOOL_PREFIX ,
2012-01-30 16:44:33 +04:00
pjob - > jobid , jobname ) ;
2008-10-02 03:40:41 +04:00
if ( new_jobname = = NULL ) {
2007-11-22 00:56:36 +03:00
goto out ;
}
2003-11-25 22:16:35 +03:00
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME , " job-name " , NULL ,
2003-11-25 22:16:35 +03:00
new_jobname ) ;
2001-03-16 08:55:30 +03:00
2008-10-02 02:01:05 +04:00
/*
* add any options defined in smb . conf
2004-04-16 00:40:26 +04:00
*/
2012-07-18 09:37:23 +04:00
if ( ! push_utf8_talloc ( frame , & cupsoptions ,
2019-10-15 17:54:45 +03:00
lp_cups_options ( talloc_tos ( ) , lp_sub , snum ) , & size ) ) {
2008-10-02 03:40:41 +04:00
goto out ;
}
2004-04-16 00:40:26 +04:00
num_options = 0 ;
options = NULL ;
2008-10-02 03:40:41 +04:00
num_options = cupsParseOptions ( cupsoptions , num_options , & options ) ;
2004-04-16 00:40:26 +04:00
if ( num_options )
2008-10-02 02:01:05 +04:00
cupsEncodeOptions ( request , num_options , options ) ;
2004-04-16 00:40:26 +04:00
2001-03-16 08:55:30 +03:00
/*
* Do the request and get back a response . . .
*/
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /printers/%s " ,
printername ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & filename , pjob - > filename , & size ) ) {
goto out ;
}
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoFileRequest ( http , request , uri , pjob - > filename ) ) ! = NULL ) {
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2010-06-13 14:11:26 +04:00
DEBUG ( 0 , ( " Unable to print file to %s - %s \n " ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2001-03-16 08:55:30 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2005-01-21 03:29:38 +03:00
} else {
2001-03-16 08:55:30 +03:00
ret = 0 ;
2010-03-25 03:33:21 +03:00
attr_job_id = ippFindAttribute ( response , " job-id " , IPP_TAG_INTEGER ) ;
if ( attr_job_id ) {
2012-07-19 21:17:28 +04:00
pjob - > sysjob = ippGetInteger ( attr_job_id , 0 ) ;
2010-03-25 03:33:21 +03:00
DEBUG ( 5 , ( " cups_job_submit: job-id %d \n " , pjob - > sysjob ) ) ;
} else {
2023-08-07 07:47:10 +03:00
DEBUG ( 0 , ( " Missing job-id attribute in IPP response \n " ) ) ;
2010-03-25 03:33:21 +03:00
}
2005-01-21 03:29:38 +03:00
}
} else {
2010-06-13 14:11:26 +04:00
DEBUG ( 0 , ( " Unable to print file to `%s' - %s \n " ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2001-03-16 08:55:30 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2005-01-21 03:29:38 +03:00
}
2001-03-16 08:55:30 +03:00
2002-09-25 19:19:00 +04:00
if ( ret = = 0 )
unlink ( pjob - > filename ) ;
/* else print_job_end will do it for us */
2005-01-21 03:29:38 +03:00
out :
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2007-11-22 00:56:36 +03:00
2005-01-21 03:29:38 +03:00
return ret ;
2001-03-16 08:55:30 +03:00
}
/*
* ' cups_queue_get ( ) ' - Get all the jobs in the print queue .
*/
2005-02-12 17:41:00 +03:00
static int cups_queue_get ( const char * sharename ,
2004-10-19 21:05:01 +04:00
enum printing_types printing_type ,
char * lpq_command ,
2008-10-02 02:01:05 +04:00
print_queue_struct * * q ,
2004-10-19 21:05:01 +04:00
print_status_struct * status )
2001-03-16 08:55:30 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
char * printername = NULL ;
2005-01-21 03:29:38 +03:00
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
ipp_attribute_t * attr = NULL ; /* Current attribute */
cups_lang_t * language = NULL ; /* Default language */
2016-11-09 21:05:49 +03:00
char uri [ HTTP_MAX_URI ] = { 0 } ; /* printer-uri attribute */
http_uri_status_t ustatus ;
2005-01-21 03:29:38 +03:00
int qcount = 0 , /* Number of active queue entries */
qalloc = 0 ; /* Number of queue entries allocated */
print_queue_struct * queue = NULL , /* Queue entries */
2001-03-16 08:55:30 +03:00
* temp ; /* Temporary pointer for queue */
2008-10-02 03:40:41 +04:00
char * user_name = NULL , /* job-originating-user-name attribute */
* job_name = NULL ; /* job-name attribute */
2001-03-16 08:55:30 +03:00
int job_id ; /* job-id attribute */
int job_k_octets ; /* job-k-octets attribute */
time_t job_time ; /* time-at-creation attribute */
ipp_jstate_t job_status ; /* job-status attribute */
int job_priority ; /* job-priority attribute */
2008-10-02 03:40:41 +04:00
size_t size ;
2001-03-16 08:55:30 +03:00
static const char * jattrs [ ] = /* Requested job attributes */
{
" job-id " ,
" job-k-octets " ,
" job-name " ,
" job-originating-user-name " ,
" job-priority " ,
" job-state " ,
" time-at-creation " ,
} ;
static const char * pattrs [ ] = /* Requested printer attributes */
{
" printer-state " ,
" printer-state-message "
} ;
2005-01-21 03:29:38 +03:00
* q = NULL ;
2001-03-16 08:55:30 +03:00
2008-10-02 02:01:05 +04:00
/* HACK ALERT!!! The problem with support the 'printer name'
option is that we key the tdb off the sharename . So we will
overload the lpq_command string to pass in the printername
( which is basically what we do for non - cups printers . . . using
2005-02-12 17:41:00 +03:00
the lpq_command to get the queue listing ) . */
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & printername , lpq_command , & size ) ) {
goto out ;
}
DEBUG ( 5 , ( " cups_queue_get(%s, %p, %p) \n " , lpq_command , q , status ) ) ;
2001-03-16 08:55:30 +03:00
/*
* Make sure we don ' t ask for passwords . . .
*/
cupsSetPasswordCB ( cups_passwd_cb ) ;
/*
* Try to connect to the server . . .
*/
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Generate the printer URI . . .
*/
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /printers/%s " ,
printername ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
/*
* Build an IPP_GET_JOBS request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* requested - attributes
* printer - uri
*/
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_GET_JOBS ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2015-02-03 21:26:42 +03:00
ippAddStrings ( request , IPP_TAG_OPERATION , IPP_TAG_KEYWORD ,
2001-03-16 08:55:30 +03:00
" requested-attributes " ,
( sizeof ( jattrs ) / sizeof ( jattrs [ 0 ] ) ) ,
NULL , jattrs ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI ,
" printer-uri " , NULL , uri ) ;
/*
* Do the request and get back a response . . .
*/
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " / " ) ) = = NULL ) {
2001-03-16 08:55:30 +03:00
DEBUG ( 0 , ( " Unable to get jobs for %s - %s \n " , uri ,
ippErrorString ( cupsLastError ( ) ) ) ) ;
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2001-03-16 08:55:30 +03:00
DEBUG ( 0 , ( " Unable to get jobs for %s - %s \n " , uri ,
2012-07-19 21:17:28 +04:00
ippErrorString ( ippGetStatusCode ( response ) ) ) ) ;
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Process the jobs . . .
*/
qcount = 0 ;
qalloc = 0 ;
queue = NULL ;
2012-07-19 21:17:28 +04:00
for ( attr = ippFirstAttribute ( response ) ; attr ! = NULL ; attr = ippNextAttribute ( response ) ) {
2001-03-16 08:55:30 +03:00
/*
* Skip leading attributes until we hit a job . . .
*/
2012-07-19 21:17:28 +04:00
while ( attr ! = NULL & & ippGetGroupTag ( attr ) ! = IPP_TAG_JOB )
attr = ippNextAttribute ( response ) ;
2001-03-16 08:55:30 +03:00
if ( attr = = NULL )
break ;
/*
* Allocate memory as needed . . .
*/
2005-01-21 03:29:38 +03:00
if ( qcount > = qalloc ) {
2001-03-16 08:55:30 +03:00
qalloc + = 16 ;
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
queue = SMB_REALLOC_ARRAY ( queue , print_queue_struct , qalloc ) ;
2001-03-16 08:55:30 +03:00
r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
realloc can return NULL in one of two cases - (1) the realloc failed,
(2) realloc succeeded but the new size requested was zero, in which
case this is identical to a free() call.
The error paths dealing with these two cases should be different,
but mostly weren't. Secondly the standard idiom for dealing with
realloc when you know the new size is non-zero is the following :
tmp = realloc(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
However, there were *many* *many* places in Samba where we were
using the old (broken) idiom of :
p = realloc(p, size)
if (!p) {
return error;
}
which will leak the memory pointed to by p on realloc fail.
This commit (hopefully) fixes all these cases by moving to
a standard idiom of :
p = SMB_REALLOC(p, size)
if (!p) {
return error;
}
Where if the realloc returns null due to the realloc failing
or size == 0 we *guarentee* that the storage pointed to by p
has been freed. This allows me to remove a lot of code that
was dealing with the standard (more verbose) method that required
a tmp pointer. This is almost always what you want. When a
realloc fails you never usually want the old memory, you
want to free it and get into your error processing asap.
For the 11 remaining cases where we really do need to keep the
old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR,
which can be used as follows :
tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size);
if (!tmp) {
SAFE_FREE(p);
return error;
} else {
p = tmp;
}
SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the
pointer p, even on size == 0 or realloc fail. All this is
done by a hidden extra argument to Realloc(), BOOL free_old_on_error
which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR
macros (and their array counterparts).
It remains to be seen what this will do to our Coverity bug count :-).
Jeremy.
(This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0)
2006-03-07 09:31:04 +03:00
if ( queue = = NULL ) {
2023-08-07 07:47:10 +03:00
DEBUG ( 0 , ( " cups_queue_get: Not enough memory! \n " ) ) ;
2005-01-21 03:29:38 +03:00
qcount = 0 ;
goto out ;
2001-03-16 08:55:30 +03:00
}
}
temp = queue + qcount ;
memset ( temp , 0 , sizeof ( print_queue_struct ) ) ;
/*
* Pull the needed attributes from this job . . .
*/
job_id = 0 ;
job_priority = 50 ;
job_status = IPP_JOB_PENDING ;
job_time = 0 ;
job_k_octets = 0 ;
user_name = NULL ;
job_name = NULL ;
2012-07-19 21:17:28 +04:00
while ( attr ! = NULL & & ippGetGroupTag ( attr ) = = IPP_TAG_JOB ) {
if ( ippGetName ( attr ) = = NULL ) {
attr = ippNextAttribute ( response ) ;
2001-03-16 08:55:30 +03:00
break ;
}
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " job-id " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_INTEGER )
job_id = ippGetInteger ( attr , 0 ) ;
2001-03-16 08:55:30 +03:00
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " job-k-octets " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_INTEGER )
job_k_octets = ippGetInteger ( attr , 0 ) ;
2001-03-16 08:55:30 +03:00
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " job-priority " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_INTEGER )
job_priority = ippGetInteger ( attr , 0 ) ;
2001-03-16 08:55:30 +03:00
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " job-state " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_ENUM )
job_status = ( ipp_jstate_t ) ippGetInteger ( attr , 0 ) ;
2001-03-16 08:55:30 +03:00
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " time-at-creation " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_INTEGER )
job_time = ippGetInteger ( attr , 0 ) ;
2001-03-16 08:55:30 +03:00
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " job-name " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_NAME ) {
2008-10-02 03:40:41 +04:00
if ( ! pull_utf8_talloc ( frame ,
& job_name ,
2012-07-19 21:17:28 +04:00
ippGetString ( attr , 0 , NULL ) ,
2008-10-02 03:40:41 +04:00
& size ) ) {
goto out ;
}
}
2001-03-16 08:55:30 +03:00
2012-07-19 21:17:28 +04:00
if ( strcmp ( ippGetName ( attr ) , " job-originating-user-name " ) = = 0 & &
ippGetValueTag ( attr ) = = IPP_TAG_NAME ) {
2008-10-02 03:40:41 +04:00
if ( ! pull_utf8_talloc ( frame ,
& user_name ,
2012-07-19 21:17:28 +04:00
ippGetString ( attr , 0 , NULL ) ,
2008-10-02 03:40:41 +04:00
& size ) ) {
goto out ;
}
}
2001-03-16 08:55:30 +03:00
2012-07-19 21:17:28 +04:00
attr = ippNextAttribute ( response ) ;
2001-03-16 08:55:30 +03:00
}
/*
* See if we have everything needed . . .
*/
2005-01-21 03:29:38 +03:00
if ( user_name = = NULL | | job_name = = NULL | | job_id = = 0 ) {
if ( attr = = NULL )
break ;
else
continue ;
2001-03-16 08:55:30 +03:00
}
2012-01-27 15:33:27 +04:00
temp - > sysjob = job_id ;
2001-03-16 08:55:30 +03:00
temp - > size = job_k_octets * 1024 ;
temp - > status = job_status = = IPP_JOB_PENDING ? LPQ_QUEUED :
job_status = = IPP_JOB_STOPPED ? LPQ_PAUSED :
job_status = = IPP_JOB_HELD ? LPQ_PAUSED :
LPQ_PRINTING ;
temp - > priority = job_priority ;
temp - > time = job_time ;
2008-10-02 03:40:41 +04:00
strlcpy ( temp - > fs_user , user_name , sizeof ( temp - > fs_user ) ) ;
strlcpy ( temp - > fs_file , job_name , sizeof ( temp - > fs_file ) ) ;
2001-03-16 08:55:30 +03:00
qcount + + ;
if ( attr = = NULL )
2005-01-21 03:29:38 +03:00
break ;
2001-03-16 08:55:30 +03:00
}
ippDelete ( response ) ;
2005-01-21 03:29:38 +03:00
response = NULL ;
2001-03-16 08:55:30 +03:00
/*
* Build an IPP_GET_PRINTER_ATTRIBUTES request , which requires the
* following attributes :
*
* attributes - charset
* attributes - natural - language
* requested - attributes
* printer - uri
*/
2005-01-26 17:46:54 +03:00
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_GET_PRINTER_ATTRIBUTES ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
ippAddStrings ( request , IPP_TAG_OPERATION , IPP_TAG_NAME ,
" requested-attributes " ,
( sizeof ( pattrs ) / sizeof ( pattrs [ 0 ] ) ) ,
NULL , pattrs ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI ,
" printer-uri " , NULL , uri ) ;
/*
* Do the request and get back a response . . .
*/
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " / " ) ) = = NULL ) {
2005-02-12 17:41:00 +03:00
DEBUG ( 0 , ( " Unable to get printer status for %s - %s \n " , printername ,
2001-03-16 08:55:30 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2005-02-12 17:41:00 +03:00
DEBUG ( 0 , ( " Unable to get printer status for %s - %s \n " , printername ,
2012-07-19 21:17:28 +04:00
ippErrorString ( ippGetStatusCode ( response ) ) ) ) ;
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Get the current printer status and convert it to the SAMBA values .
*/
2005-01-21 03:29:38 +03:00
if ( ( attr = ippFindAttribute ( response , " printer-state " , IPP_TAG_ENUM ) ) ! = NULL ) {
2012-07-19 21:17:28 +04:00
if ( ippGetInteger ( attr , 0 ) = = IPP_PRINTER_STOPPED )
2001-03-16 08:55:30 +03:00
status - > status = LPSTAT_STOPPED ;
else
status - > status = LPSTAT_OK ;
}
if ( ( attr = ippFindAttribute ( response , " printer-state-message " ,
2008-10-02 03:40:41 +04:00
IPP_TAG_TEXT ) ) ! = NULL ) {
char * msg = NULL ;
if ( ! pull_utf8_talloc ( frame , & msg ,
2012-07-19 21:17:28 +04:00
ippGetString ( attr , 0 , NULL ) ,
2008-10-02 03:40:41 +04:00
& size ) ) {
2009-02-13 23:52:21 +03:00
SAFE_FREE ( queue ) ;
qcount = 0 ;
2008-10-02 03:40:41 +04:00
goto out ;
}
fstrcpy ( status - > message , msg ) ;
}
2001-03-16 08:55:30 +03:00
2009-06-20 04:20:00 +04:00
out :
2001-03-16 08:55:30 +03:00
/*
* Return the job queue . . .
*/
* q = queue ;
2005-01-21 03:29:38 +03:00
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2005-01-21 03:29:38 +03:00
return qcount ;
2001-03-16 08:55:30 +03:00
}
/*
* ' cups_queue_pause ( ) ' - Pause a print queue .
*/
2005-01-21 03:29:38 +03:00
static int cups_queue_pause ( int snum )
2001-03-16 08:55:30 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2019-11-04 14:14:34 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2005-01-21 03:29:38 +03:00
int ret = 1 ; /* Return value */
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
cups_lang_t * language = NULL ; /* Default language */
2008-10-02 03:40:41 +04:00
char * printername = NULL ;
char * username = NULL ;
2016-11-09 21:05:49 +03:00
char uri [ HTTP_MAX_URI ] = { 0 } ; /* printer-uri attribute */
http_uri_status_t ustatus ;
2008-10-02 03:40:41 +04:00
size_t size ;
2001-03-16 08:55:30 +03:00
DEBUG ( 5 , ( " cups_queue_pause(%d) \n " , snum ) ) ;
2001-07-23 23:50:36 +04:00
/*
* Make sure we don ' t ask for passwords . . .
*/
2001-03-16 08:55:30 +03:00
cupsSetPasswordCB ( cups_passwd_cb ) ;
2001-07-23 23:50:36 +04:00
/*
* Try to connect to the server . . .
*/
2001-03-16 08:55:30 +03:00
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
2001-07-23 23:50:36 +04:00
/*
* Build an IPP_PAUSE_PRINTER request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* printer - uri
* requesting - user - name
*/
2001-03-16 08:55:30 +03:00
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_PAUSE_PRINTER ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2012-07-18 09:37:23 +04:00
if ( ! push_utf8_talloc ( frame , & printername ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) , & size ) ) {
2008-10-02 03:40:41 +04:00
goto out ;
}
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /printers/%s " ,
printername ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI , " printer-uri " , NULL , uri ) ;
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & username , current_user_info . unix_name , & size ) ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME , " requesting-user-name " ,
2008-10-02 03:40:41 +04:00
NULL , username ) ;
2001-03-16 08:55:30 +03:00
/*
* Do the request and get back a response . . .
*/
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " /admin/ " ) ) ! = NULL ) {
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2010-06-13 14:11:26 +04:00
DEBUG ( 0 , ( " Unable to pause printer %s - %s \n " ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2005-01-21 03:29:38 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
} else {
ret = 0 ;
}
} else {
2010-06-13 14:11:26 +04:00
DEBUG ( 0 , ( " Unable to pause printer %s - %s \n " ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2005-01-21 03:29:38 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2001-03-16 08:55:30 +03:00
}
2005-01-21 03:29:38 +03:00
out :
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2005-01-21 03:29:38 +03:00
return ret ;
2001-03-16 08:55:30 +03:00
}
/*
* ' cups_queue_resume ( ) ' - Restart a print queue .
*/
2005-01-21 03:29:38 +03:00
static int cups_queue_resume ( int snum )
2001-03-16 08:55:30 +03:00
{
2008-10-02 03:40:41 +04:00
TALLOC_CTX * frame = talloc_stackframe ( ) ;
2019-11-04 14:14:34 +03:00
const struct loadparm_substitution * lp_sub =
loadparm_s3_global_substitution ( ) ;
2005-01-21 03:29:38 +03:00
int ret = 1 ; /* Return value */
http_t * http = NULL ; /* HTTP connection to server */
ipp_t * request = NULL , /* IPP Request */
* response = NULL ; /* IPP Response */
cups_lang_t * language = NULL ; /* Default language */
2008-10-02 03:40:41 +04:00
char * printername = NULL ;
char * username = NULL ;
2016-11-09 21:05:49 +03:00
char uri [ HTTP_MAX_URI ] = { 0 } ; /* printer-uri attribute */
http_uri_status_t ustatus ;
2008-10-02 03:40:41 +04:00
size_t size ;
2001-03-16 08:55:30 +03:00
DEBUG ( 5 , ( " cups_queue_resume(%d) \n " , snum ) ) ;
/*
* Make sure we don ' t ask for passwords . . .
*/
cupsSetPasswordCB ( cups_passwd_cb ) ;
/*
* Try to connect to the server . . .
*/
2008-10-02 03:40:41 +04:00
if ( ( http = cups_connect ( frame ) ) = = NULL ) {
2005-01-21 03:29:38 +03:00
goto out ;
2001-03-16 08:55:30 +03:00
}
/*
* Build an IPP_RESUME_PRINTER request , which requires the following
* attributes :
*
* attributes - charset
* attributes - natural - language
* printer - uri
* requesting - user - name
*/
request = ippNew ( ) ;
2012-07-19 21:17:28 +04:00
ippSetOperation ( request , IPP_RESUME_PRINTER ) ;
ippSetRequestId ( request , 1 ) ;
2001-03-16 08:55:30 +03:00
language = cupsLangDefault ( ) ;
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_CHARSET ,
2008-10-02 03:40:41 +04:00
" attributes-charset " , NULL , " utf-8 " ) ;
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_LANGUAGE ,
" attributes-natural-language " , NULL , language - > language ) ;
2019-11-04 14:14:34 +03:00
if ( ! push_utf8_talloc ( frame , & printername , lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2010-06-13 14:11:26 +04:00
& size ) ) {
2008-10-02 03:40:41 +04:00
goto out ;
}
2016-11-09 21:05:49 +03:00
ustatus = httpAssembleURIf ( HTTP_URI_CODING_ALL ,
uri ,
sizeof ( uri ) ,
" ipp " ,
NULL , /* username */
" localhost " ,
ippPort ( ) ,
" /printers/%s " ,
printername ) ;
if ( ustatus ! = HTTP_URI_STATUS_OK ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_URI , " printer-uri " , NULL , uri ) ;
2008-10-02 03:40:41 +04:00
if ( ! push_utf8_talloc ( frame , & username , current_user_info . unix_name , & size ) ) {
goto out ;
}
2001-03-16 08:55:30 +03:00
ippAddString ( request , IPP_TAG_OPERATION , IPP_TAG_NAME , " requesting-user-name " ,
2008-10-02 03:40:41 +04:00
NULL , username ) ;
2001-03-16 08:55:30 +03:00
/*
* Do the request and get back a response . . .
*/
2005-01-21 03:29:38 +03:00
if ( ( response = cupsDoRequest ( http , request , " /admin/ " ) ) ! = NULL ) {
2012-07-19 21:17:28 +04:00
if ( ippGetStatusCode ( response ) > = IPP_OK_CONFLICT ) {
2010-06-13 14:11:26 +04:00
DEBUG ( 0 , ( " Unable to resume printer %s - %s \n " ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2005-01-21 03:29:38 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
} else {
ret = 0 ;
}
} else {
2010-06-13 14:11:26 +04:00
DEBUG ( 0 , ( " Unable to resume printer %s - %s \n " ,
2019-11-04 14:14:34 +03:00
lp_printername ( talloc_tos ( ) , lp_sub , snum ) ,
2005-01-21 03:29:38 +03:00
ippErrorString ( cupsLastError ( ) ) ) ) ;
2001-03-16 08:55:30 +03:00
}
2005-01-21 03:29:38 +03:00
out :
if ( response )
ippDelete ( response ) ;
if ( language )
cupsLangFree ( language ) ;
if ( http )
httpClose ( http ) ;
2008-10-02 03:40:41 +04:00
TALLOC_FREE ( frame ) ;
2005-01-21 03:29:38 +03:00
return ret ;
2001-03-16 08:55:30 +03:00
}
2004-10-19 21:05:01 +04:00
/*******************************************************************
* CUPS printing interface definitions . . .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
struct printif cups_printif =
{
PRINT_CUPS ,
cups_queue_get ,
cups_queue_pause ,
cups_queue_resume ,
cups_job_delete ,
cups_job_pause ,
cups_job_resume ,
cups_job_submit ,
} ;
2001-03-16 08:55:30 +03:00
1999-12-17 04:46:15 +03:00
# else
/* this keeps fussy compilers happy */
2006-12-12 23:15:47 +03:00
void print_cups_dummy ( void ) ;
1999-12-17 04:46:15 +03:00
void print_cups_dummy ( void ) { }
2001-08-23 23:06:20 +04:00
# endif /* HAVE_CUPS */