2002-03-26 11:08:09 +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 .
*/
# include "python/py_spoolss.h"
/* Enumerate ports */
2002-03-28 04:14:43 +00:00
PyObject * spoolss_enumports ( PyObject * self , PyObject * args , PyObject * kw )
2002-03-26 11:08:09 +00:00
{
WERROR werror ;
2002-05-16 05:19:30 +00:00
PyObject * result = NULL , * creds = NULL ;
2002-10-21 04:17:43 +00:00
uint32 level = 1 ;
2002-03-27 05:28:42 +00:00
uint32 i , needed , num_ports ;
2002-03-26 11:08:09 +00:00
static char * kwlist [ ] = { " server " , " level " , " creds " , NULL } ;
TALLOC_CTX * mem_ctx = NULL ;
struct cli_state * cli = NULL ;
2002-05-16 04:00:31 +00:00
char * server , * errstr ;
2002-03-26 11:08:09 +00:00
PORT_INFO_CTR ctr ;
/* Parse parameters */
2002-05-16 04:00:31 +00:00
if ( ! PyArg_ParseTupleAndKeywords (
2002-05-28 02:09:54 +00:00
args , kw , " s|iO " , kwlist , & server , & level , & creds ) )
2002-03-26 11:08:09 +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-26 11:08:09 +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-10-17 04:45:25 +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:19:30 +00:00
goto done ;
2002-05-16 04:00:31 +00:00
}
if ( ! ( mem_ctx = talloc_init ( ) ) ) {
PyErr_SetString (
2002-05-16 05:19:30 +00:00
spoolss_error , " unable to init talloc context \n " ) ;
goto done ;
2002-05-16 04:00:31 +00:00
}
2002-03-26 11:08:09 +00:00
/* Call rpc function */
werror = cli_spoolss_enum_ports (
cli , mem_ctx , 0 , & needed , level , & num_ports , & ctr ) ;
if ( W_ERROR_V ( werror ) = = ERRinsufficientbuffer )
werror = cli_spoolss_enum_ports (
cli , mem_ctx , needed , NULL , level ,
& num_ports , & ctr ) ;
2002-05-16 05:19:30 +00:00
if ( ! W_ERROR_IS_OK ( werror ) ) {
PyErr_SetObject ( spoolss_werror , py_werror_tuple ( werror ) ) ;
2002-03-26 11:08:09 +00:00
goto done ;
2002-05-16 05:19:30 +00:00
}
2002-03-26 11:08:09 +00:00
2002-05-16 05:19:30 +00:00
/* Return value */
2002-03-26 11:08:09 +00:00
switch ( level ) {
case 1 :
2002-05-16 05:19:30 +00:00
result = PyDict_New ( ) ;
2002-03-26 11:08:09 +00:00
for ( i = 0 ; i < num_ports ; i + + ) {
PyObject * value ;
2002-05-16 05:19:30 +00:00
fstring name ;
rpcstr_pull ( name , ctr . port . info_1 [ i ] . port_name . buffer ,
sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
2002-03-26 11:08:09 +00:00
2002-04-05 05:48:01 +00:00
py_from_PORT_INFO_1 ( & value , & ctr . port . info_1 [ i ] ) ;
2002-03-26 11:08:09 +00:00
2002-05-16 05:19:30 +00:00
PyDict_SetItemString (
value , " level " , PyInt_FromLong ( 1 ) ) ;
PyDict_SetItemString ( result , name , value ) ;
2002-03-26 11:08:09 +00:00
}
break ;
case 2 :
2002-05-16 05:19:30 +00:00
result = PyDict_New ( ) ;
2002-03-26 11:08:09 +00:00
for ( i = 0 ; i < num_ports ; i + + ) {
PyObject * value ;
2002-05-16 05:19:30 +00:00
fstring name ;
rpcstr_pull ( name , ctr . port . info_2 [ i ] . port_name . buffer ,
sizeof ( fstring ) , - 1 , STR_TERMINATE ) ;
2002-03-26 11:08:09 +00:00
2002-04-05 05:48:01 +00:00
py_from_PORT_INFO_2 ( & value , & ctr . port . info_2 [ i ] ) ;
2002-03-26 11:08:09 +00:00
2002-05-16 05:19:30 +00:00
PyDict_SetItemString (
value , " level " , PyInt_FromLong ( 2 ) ) ;
PyDict_SetItemString ( result , name , value ) ;
2002-03-26 11:08:09 +00:00
}
break ;
2002-05-16 05:19:30 +00:00
default :
PyErr_SetString ( spoolss_error , " unknown info level " ) ;
goto done ;
2002-03-26 11:08:09 +00:00
}
done :
2002-05-16 05:19:30 +00:00
if ( cli )
cli_shutdown ( cli ) ;
if ( mem_ctx )
talloc_destroy ( mem_ctx ) ;
2002-03-26 11:08:09 +00:00
return result ;
}