2002-03-27 05:25:13 +00:00
/*
Python wrappers for DCERPC / SMB client routines .
Copyright ( C ) Tim Potter , 2002
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 2 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 , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
2002-03-28 04:14:43 +00:00
# include "python/py_spoolss.h"
2002-03-27 05:25:13 +00:00
/* Open a printer */
2002-03-28 04:14:43 +00:00
PyObject * spoolss_openprinter ( PyObject * self , PyObject * args , PyObject * kw )
2002-03-27 05:25:13 +00:00
{
2002-05-27 06:38:36 +00:00
char * unc_name , * server , * errstr ;
TALLOC_CTX * mem_ctx = NULL ;
2002-03-27 05:25:13 +00:00
POLICY_HND hnd ;
WERROR werror ;
PyObject * result = NULL , * creds = NULL ;
static char * kwlist [ ] = { " printername " , " creds " , " access " , NULL } ;
uint32 desired_access = MAXIMUM_ALLOWED_ACCESS ;
struct cli_state * cli ;
if ( ! PyArg_ParseTupleAndKeywords (
2002-05-28 02:09:54 +00:00
args , kw , " s|Oi " , kwlist , & unc_name , & creds ,
2002-05-16 04:00:31 +00:00
& desired_access ) )
2002-05-27 06:38:36 +00:00
return NULL ;
2002-03-27 05:25:13 +00:00
2002-05-27 06:38:36 +00:00
if ( unc_name [ 0 ] ! = ' \\ ' | | unc_name [ 1 ] ! = ' \\ ' ) {
2002-05-28 02:33:11 +00:00
PyErr_SetString ( PyExc_ValueError , " UNC name required " ) ;
2002-05-27 06:38:36 +00:00
return NULL ;
}
2002-03-27 05:25:13 +00:00
2005-06-27 22:39:09 +00:00
server = SMB_STRDUP ( unc_name + 2 ) ;
2002-03-27 05:25:13 +00:00
2002-05-16 04:00:31 +00:00
if ( strchr ( server , ' \\ ' ) ) {
char * c = strchr ( server , ' \\ ' ) ;
2002-03-27 05:25:13 +00:00
* c = 0 ;
}
2002-05-28 02:09:54 +00:00
if ( creds & & creds ! = Py_None & & ! PyDict_Check ( creds ) ) {
PyErr_SetString ( PyExc_TypeError ,
" credentials must be dictionary or None " ) ;
return NULL ;
}
2002-11-04 20:33:16 +00:00
if ( ! ( cli = open_pipe_creds ( server , creds , PI_SPOOLSS , & errstr ) ) ) {
2002-05-16 04:00:31 +00:00
PyErr_SetString ( spoolss_error , errstr ) ;
free ( errstr ) ;
2002-03-27 05:25:13 +00:00
goto done ;
2002-05-16 04:00:31 +00:00
}
2002-03-27 05:25:13 +00:00
2002-12-23 23:54:10 +00:00
if ( ! ( mem_ctx = talloc_init ( " spoolss_openprinter " ) ) ) {
2002-04-14 23:36:01 +00:00
PyErr_SetString ( spoolss_error ,
2002-05-16 05:24:06 +00:00
" unable to init talloc context \n " ) ;
2002-03-27 05:25:13 +00:00
goto done ;
}
2005-10-07 04:54:41 +00:00
werror = rpccli_spoolss_open_printer_ex (
2006-05-01 22:31:33 +00:00
cli - > pipe_list , mem_ctx , unc_name , " " , desired_access , server ,
2002-03-27 05:25:13 +00:00
" " , & hnd ) ;
if ( ! W_ERROR_IS_OK ( werror ) ) {
2002-04-05 04:53:20 +00:00
PyErr_SetObject ( spoolss_werror , py_werror_tuple ( werror ) ) ;
2002-03-27 05:25:13 +00:00
goto done ;
}
2002-04-14 23:42:54 +00:00
result = new_spoolss_policy_hnd_object ( cli , mem_ctx , & hnd ) ;
2002-03-27 05:25:13 +00:00
done :
2002-05-27 06:38:36 +00:00
if ( ! result ) {
if ( cli )
cli_shutdown ( cli ) ;
if ( mem_ctx )
talloc_destroy ( mem_ctx ) ;
}
2002-05-16 04:00:31 +00:00
SAFE_FREE ( server ) ;
2002-03-27 05:25:13 +00:00
return result ;
}
/* Close a printer */
2002-03-28 04:14:43 +00:00
PyObject * spoolss_closeprinter ( PyObject * self , PyObject * args )
2002-03-27 05:25:13 +00:00
{
PyObject * po ;
spoolss_policy_hnd_object * hnd ;
WERROR result ;
/* Parse parameters */
if ( ! PyArg_ParseTuple ( args , " O! " , & spoolss_policy_hnd_type , & po ) )
return NULL ;
hnd = ( spoolss_policy_hnd_object * ) po ;
/* Call rpc function */
2005-10-07 04:54:41 +00:00
result = rpccli_spoolss_close_printer (
hnd - > cli , hnd - > mem_ctx , & hnd - > pol ) ;
2002-03-27 05:25:13 +00:00
/* Return value */
Py_INCREF ( Py_None ) ;
return Py_None ;
}
/* Fetch printer information */
2002-05-15 06:22:53 +00:00
PyObject * spoolss_hnd_getprinter ( PyObject * self , PyObject * args , PyObject * kw )
2002-03-27 05:25:13 +00:00
{
spoolss_policy_hnd_object * hnd = ( spoolss_policy_hnd_object * ) self ;
WERROR werror ;
2002-04-05 04:53:20 +00:00
PyObject * result = NULL ;
2002-03-27 05:25:13 +00:00
PRINTER_INFO_CTR ctr ;
int level = 1 ;
static char * kwlist [ ] = { " level " , NULL } ;
2002-04-20 07:17:36 +00:00
2002-03-27 05:25:13 +00:00
/* Parse parameters */
if ( ! PyArg_ParseTupleAndKeywords ( args , kw , " |i " , kwlist , & level ) )
return NULL ;
2002-04-20 07:17:36 +00:00
ZERO_STRUCT ( ctr ) ;
2002-03-27 05:25:13 +00:00
/* Call rpc function */
2005-10-07 04:54:41 +00:00
werror = rpccli_spoolss_getprinter (
2005-07-20 15:35:29 +00:00
hnd - > cli , hnd - > mem_ctx , & hnd - > pol , level , & ctr ) ;
2002-03-27 05:25:13 +00:00
/* Return value */
2002-04-05 04:53:20 +00:00
if ( ! W_ERROR_IS_OK ( werror ) ) {
2002-05-08 04:22:17 +00:00
PyErr_SetObject ( spoolss_werror , py_werror_tuple ( werror ) ) ;
2002-04-05 04:53:20 +00:00
return NULL ;
}
2002-03-27 05:25:13 +00:00
2002-04-05 04:53:20 +00:00
result = Py_None ;
2002-03-27 05:25:13 +00:00
switch ( level ) {
2002-04-05 04:53:20 +00:00
2002-03-27 05:25:13 +00:00
case 0 :
2002-04-05 04:53:20 +00:00
py_from_PRINTER_INFO_0 ( & result , ctr . printers_0 ) ;
2002-03-27 05:25:13 +00:00
break ;
case 1 :
2002-04-05 04:53:20 +00:00
py_from_PRINTER_INFO_1 ( & result , ctr . printers_1 ) ;
2002-03-27 05:25:13 +00:00
break ;
case 2 :
2002-04-05 04:53:20 +00:00
py_from_PRINTER_INFO_2 ( & result , ctr . printers_2 ) ;
2002-03-27 05:25:13 +00:00
break ;
case 3 :
2002-04-05 04:53:20 +00:00
py_from_PRINTER_INFO_3 ( & result , ctr . printers_3 ) ;
2002-03-27 05:25:13 +00:00
break ;
}
2002-04-05 04:53:20 +00:00
2002-03-27 05:25:13 +00:00
Py_INCREF ( result ) ;
return result ;
}
/* Set printer information */
2002-05-15 06:22:53 +00:00
PyObject * spoolss_hnd_setprinter ( PyObject * self , PyObject * args , PyObject * kw )
2002-03-27 05:25:13 +00:00
{
spoolss_policy_hnd_object * hnd = ( spoolss_policy_hnd_object * ) self ;
WERROR werror ;
2002-05-17 02:39:07 +00:00
PyObject * info ;
2002-03-27 05:25:13 +00:00
PRINTER_INFO_CTR ctr ;
2002-04-05 04:53:20 +00:00
uint32 level ;
static char * kwlist [ ] = { " dict " , NULL } ;
2002-03-27 05:25:13 +00:00
union {
2002-05-27 06:38:36 +00:00
PRINTER_INFO_1 printers_1 ;
2002-03-27 05:25:13 +00:00
PRINTER_INFO_2 printers_2 ;
PRINTER_INFO_3 printers_3 ;
} pinfo ;
/* Parse parameters */
2002-05-16 04:00:31 +00:00
if ( ! PyArg_ParseTupleAndKeywords (
args , kw , " O! " , kwlist , & PyDict_Type , & info ) )
2002-03-27 05:25:13 +00:00
return NULL ;
2002-05-17 02:39:07 +00:00
if ( ! get_level_value ( info , & level ) ) {
PyErr_SetString ( spoolss_error , " invalid info level " ) ;
return NULL ;
}
2002-04-18 03:35:05 +00:00
2002-05-27 06:38:36 +00:00
if ( level < 1 & & level > 3 ) {
2002-05-17 02:39:07 +00:00
PyErr_SetString ( spoolss_error , " unsupported info level " ) ;
2002-04-05 04:53:20 +00:00
return NULL ;
}
2002-03-27 05:25:13 +00:00
/* Fill in printer info */
ZERO_STRUCT ( ctr ) ;
switch ( level ) {
2002-05-27 06:38:36 +00:00
case 1 :
ctr . printers_1 = & pinfo . printers_1 ;
2002-09-09 04:43:38 +00:00
if ( ! py_to_PRINTER_INFO_1 ( ctr . printers_1 , info ) ) {
2002-05-27 06:38:36 +00:00
PyErr_SetString ( spoolss_error ,
" error converting printer to info 1 " ) ;
return NULL ;
}
break ;
2002-04-18 03:35:05 +00:00
case 2 :
2002-04-05 04:53:20 +00:00
ctr . printers_2 = & pinfo . printers_2 ;
2002-09-09 04:43:38 +00:00
if ( ! py_to_PRINTER_INFO_2 ( ctr . printers_2 , info ,
2002-05-01 04:25:46 +00:00
hnd - > mem_ctx ) ) {
PyErr_SetString ( spoolss_error ,
" error converting printer to info 2 " ) ;
return NULL ;
}
break ;
case 3 :
ctr . printers_3 = & pinfo . printers_3 ;
2002-09-09 04:43:38 +00:00
if ( ! py_to_PRINTER_INFO_3 ( ctr . printers_3 , info ,
2002-05-01 04:25:46 +00:00
hnd - > mem_ctx ) ) {
PyErr_SetString ( spoolss_error ,
" error converting to printer info 3 " ) ;
return NULL ;
}
2002-04-05 04:53:20 +00:00
2002-03-27 05:25:13 +00:00
break ;
default :
2002-04-05 04:53:20 +00:00
PyErr_SetString ( spoolss_error , " unsupported info level " ) ;
return NULL ;
2002-03-27 05:25:13 +00:00
}
/* Call rpc function */
2005-10-07 04:54:41 +00:00
werror = rpccli_spoolss_setprinter (
hnd - > cli , hnd - > mem_ctx , & hnd - > pol , level , & ctr , 0 ) ;
2002-03-27 05:25:13 +00:00
/* Return value */
2002-04-05 04:53:20 +00:00
if ( ! W_ERROR_IS_OK ( werror ) ) {
PyErr_SetObject ( spoolss_werror , py_werror_tuple ( werror ) ) ;
return NULL ;
}
2002-03-27 05:25:13 +00:00
2002-04-05 04:53:20 +00:00
Py_INCREF ( Py_None ) ;
return Py_None ;
2002-03-27 05:25:13 +00:00
}
/* Enumerate printers */
2002-03-28 04:14:43 +00:00
PyObject * spoolss_enumprinters ( PyObject * self , PyObject * args , PyObject * kw )
2002-03-27 05:25:13 +00:00
{
WERROR werror ;
2002-05-16 05:24:06 +00:00
PyObject * result = NULL , * creds = NULL ;
2002-03-27 05:25:13 +00:00
PRINTER_INFO_CTR ctr ;
int level = 1 , flags = PRINTER_ENUM_LOCAL , i ;
2005-07-20 15:35:29 +00:00
uint32 num_printers ;
2002-05-27 06:38:36 +00:00
static char * kwlist [ ] = { " server " , " name " , " level " , " flags " ,
" creds " , NULL } ;
2002-05-16 05:24:06 +00:00
TALLOC_CTX * mem_ctx = NULL ;
struct cli_state * cli = NULL ;
2002-05-27 06:38:36 +00:00
char * server , * errstr , * name = NULL ;
2002-03-27 05:25:13 +00:00
/* Parse parameters */
2002-05-16 04:00:31 +00:00
if ( ! PyArg_ParseTupleAndKeywords (
2002-05-28 02:09:54 +00:00
args , kw , " s|siiO " , kwlist , & server , & name , & level ,
& flags , & creds ) )
2002-03-27 05:25:13 +00:00
return NULL ;
2002-05-28 02:33:11 +00:00
if ( server [ 0 ] ! = ' \\ ' | | server [ 1 ] ! = ' \\ ' ) {
PyErr_SetString ( PyExc_ValueError , " UNC name required " ) ;
return NULL ;
}
server + = 2 ;
2002-03-27 05:25:13 +00:00
2002-05-28 02:09:54 +00:00
if ( creds & & creds ! = Py_None & & ! PyDict_Check ( creds ) ) {
PyErr_SetString ( PyExc_TypeError ,
" credentials must be dictionary or None " ) ;
return NULL ;
}
2002-11-04 20:33:16 +00:00
if ( ! ( cli = open_pipe_creds ( server , creds , PI_SPOOLSS , & errstr ) ) ) {
2002-05-16 04:00:31 +00:00
PyErr_SetString ( spoolss_error , errstr ) ;
free ( errstr ) ;
2002-05-16 05:24:06 +00:00
goto done ;
2002-05-16 04:00:31 +00:00
}
2002-12-23 23:54:10 +00:00
if ( ! ( mem_ctx = talloc_init ( " spoolss_enumprinters " ) ) ) {
2002-05-16 04:00:31 +00:00
PyErr_SetString (
2002-05-16 05:24:06 +00:00
spoolss_error , " unable to init talloc context \n " ) ;
goto done ;
2002-05-16 04:00:31 +00:00
}
2002-03-27 05:25:13 +00:00
2002-05-27 06:38:36 +00:00
/* This RPC is weird. By setting the server name to different
values we can get different behaviour . If however the server
name is not specified , we default it to being the full server
name as this is probably what the caller intended . To pass a
NULL name , pass a value of " " */
if ( ! name )
name = server ;
else {
if ( ! name [ 0 ] )
name = NULL ;
}
2002-03-27 05:25:13 +00:00
/* Call rpc function */
2005-10-07 04:54:41 +00:00
werror = rpccli_spoolss_enum_printers (
2006-05-01 22:31:33 +00:00
cli - > pipe_list , mem_ctx , name , flags , level , & num_printers , & ctr ) ;
2002-03-27 05:25:13 +00:00
2002-04-05 04:53:20 +00:00
if ( ! W_ERROR_IS_OK ( werror ) ) {
2002-05-08 04:22:17 +00:00
PyErr_SetObject ( spoolss_werror , py_werror_tuple ( werror ) ) ;
2002-05-15 06:09:47 +00:00
goto done ;
2002-04-05 04:53:20 +00:00
}
2002-03-27 05:25:13 +00:00
2002-05-16 05:24:06 +00:00
/* Return value */
2002-03-27 05:25:13 +00:00
switch ( level ) {
case 0 :
2002-05-16 05:24:06 +00:00
result = PyDict_New ( ) ;
2002-03-27 05:25:13 +00:00
for ( i = 0 ; i < num_printers ; i + + ) {
PyObject * value ;
2003-01-15 22:18:33 +00:00
fstring s ;
2002-05-16 05:24:06 +00:00
2003-01-15 22:18:33 +00:00
rpcstr_pull ( s , ctr . printers_0 [ i ] . printername . buffer ,
2002-05-16 05:24:06 +00:00
sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
2002-03-27 05:25:13 +00:00
2002-04-05 04:53:20 +00:00
py_from_PRINTER_INFO_0 ( & value , & ctr . printers_0 [ i ] ) ;
2002-03-27 05:25:13 +00:00
2002-05-16 05:24:06 +00:00
PyDict_SetItemString (
value , " level " , PyInt_FromLong ( 0 ) ) ;
2003-01-15 22:18:33 +00:00
PyDict_SetItemString ( result , s , value ) ;
2002-03-27 05:25:13 +00:00
}
break ;
case 1 :
2002-05-16 05:24:06 +00:00
result = PyDict_New ( ) ;
2002-03-27 05:25:13 +00:00
for ( i = 0 ; i < num_printers ; i + + ) {
PyObject * value ;
2003-01-15 22:18:33 +00:00
fstring s ;
2002-05-16 05:24:06 +00:00
2003-01-15 22:18:33 +00:00
rpcstr_pull ( s , ctr . printers_1 [ i ] . name . buffer ,
2002-05-16 05:24:06 +00:00
sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
2002-03-27 05:25:13 +00:00
2002-04-05 04:53:20 +00:00
py_from_PRINTER_INFO_1 ( & value , & ctr . printers_1 [ i ] ) ;
2002-03-27 05:25:13 +00:00
2002-05-16 05:24:06 +00:00
PyDict_SetItemString (
value , " level " , PyInt_FromLong ( 1 ) ) ;
2003-01-15 22:18:33 +00:00
PyDict_SetItemString ( result , s , value ) ;
2002-03-27 05:25:13 +00:00
}
break ;
case 2 :
2002-05-16 05:24:06 +00:00
result = PyDict_New ( ) ;
2002-03-27 05:25:13 +00:00
for ( i = 0 ; i < num_printers ; i + + ) {
PyObject * value ;
2003-01-15 22:18:33 +00:00
fstring s ;
2002-03-27 05:25:13 +00:00
2003-01-15 22:18:33 +00:00
rpcstr_pull ( s , ctr . printers_2 [ i ] . printername . buffer ,
2002-05-16 05:24:06 +00:00
sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
2002-03-27 05:25:13 +00:00
2002-05-16 05:24:06 +00:00
py_from_PRINTER_INFO_2 ( & value , & ctr . printers_2 [ i ] ) ;
2002-03-27 05:25:13 +00:00
2002-05-16 05:24:06 +00:00
PyDict_SetItemString (
value , " level " , PyInt_FromLong ( 2 ) ) ;
2002-03-27 05:25:13 +00:00
2003-01-15 22:18:33 +00:00
PyDict_SetItemString ( result , s , value ) ;
2002-03-27 05:25:13 +00:00
}
break ;
2002-05-16 05:24:06 +00:00
default :
PyErr_SetString ( spoolss_error , " unknown info level " ) ;
goto done ;
2002-03-27 05:25:13 +00:00
}
2002-05-15 06:09:47 +00:00
done :
2002-05-16 05:24:06 +00:00
if ( cli )
cli_shutdown ( cli ) ;
if ( mem_ctx )
talloc_destroy ( mem_ctx ) ;
2002-05-15 06:09:47 +00:00
return result ;
}
/* Add a printer */
PyObject * spoolss_addprinterex ( PyObject * self , PyObject * args , PyObject * kw )
{
static char * kwlist [ ] = { " server " , " printername " , " info " , " creds " ,
NULL } ;
2002-05-16 04:00:31 +00:00
char * printername , * server , * errstr ;
2002-05-15 06:09:47 +00:00
PyObject * info , * result = NULL , * creds = NULL ;
struct cli_state * cli = NULL ;
TALLOC_CTX * mem_ctx = NULL ;
PRINTER_INFO_CTR ctr ;
PRINTER_INFO_2 info2 ;
WERROR werror ;
if ( ! PyArg_ParseTupleAndKeywords (
args , kw , " ssO!|O! " , kwlist , & server , & printername ,
& PyDict_Type , & info , & PyDict_Type , & creds ) )
return NULL ;
2002-11-04 20:33:16 +00:00
if ( ! ( cli = open_pipe_creds ( server , creds , PI_SPOOLSS , & errstr ) ) ) {
2002-05-16 04:00:31 +00:00
PyErr_SetString ( spoolss_error , errstr ) ;
free ( errstr ) ;
2002-05-15 06:09:47 +00:00
goto done ;
2002-05-16 04:00:31 +00:00
}
2002-05-15 06:09:47 +00:00
2002-12-23 23:54:10 +00:00
if ( ! ( mem_ctx = talloc_init ( " spoolss_addprinterex " ) ) ) {
2002-05-16 04:00:31 +00:00
PyErr_SetString (
2002-05-16 05:24:06 +00:00
spoolss_error , " unable to init talloc context \n " ) ;
2002-05-27 06:38:36 +00:00
goto done ;
2002-05-16 04:00:31 +00:00
}
2002-05-15 06:09:47 +00:00
if ( ! py_to_PRINTER_INFO_2 ( & info2 , info , mem_ctx ) ) {
PyErr_SetString ( spoolss_error ,
" error converting to printer info 2 " ) ;
goto done ;
}
ctr . printers_2 = & info2 ;
2006-05-01 22:31:33 +00:00
werror = rpccli_spoolss_addprinterex ( cli - > pipe_list , mem_ctx , 2 , & ctr ) ;
2002-05-15 06:09:47 +00:00
Py_INCREF ( Py_None ) ;
result = Py_None ;
done :
2002-05-27 06:38:36 +00:00
if ( cli )
cli_shutdown ( cli ) ;
if ( mem_ctx )
talloc_destroy ( mem_ctx ) ;
2002-05-15 06:09:47 +00:00
2002-03-27 05:25:13 +00:00
return result ;
}