libmsrpc/libmsrpc.c

説明を見る。
00001 
00002 /* 
00003  *  Unix SMB/CIFS implementation.
00004  *  MS-RPC client library implementation
00005  *  Copyright (C) Chris Nicholls              2005.
00006  *  
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *  
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *  
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #include "libmsrpc.h"
00023 #include "libmsrpc_internal.h"
00024 #include "libsmbclient.h"
00025 #include "libsmb_internal.h"
00026 
00027 int cac_InitHandleData( CacServerHandle * hnd );
00028 
00029 /*this function is based on code found in smbc_init_context() (libsmb/libsmbclient.c)*/
00030 void cac_Init( int debug )
00031 {
00032         if ( debug < 0 || debug > 99 )
00033                 debug = 0;
00034 
00035         DEBUGLEVEL = debug;
00036 
00037         setup_logging( "libmsrpc", True );
00038 }
00039 
00040 int cac_InitHandleMem( CacServerHandle * hnd )
00041 {
00042         hnd->username = SMB_MALLOC_ARRAY( char, sizeof( fstring ) );
00043 
00044         if ( !hnd->username )
00045                 return CAC_FAILURE;
00046 
00047         hnd->username[0] = '\0';
00048 
00049         hnd->domain = SMB_MALLOC_ARRAY( char, sizeof( fstring ) );
00050         if ( !hnd->domain )
00051                 return CAC_FAILURE;
00052 
00053         hnd->domain[0] = '\0';
00054 
00055         hnd->netbios_name = SMB_MALLOC_ARRAY( char, sizeof( fstring ) );
00056         if ( !hnd->netbios_name )
00057                 return CAC_FAILURE;
00058 
00059         hnd->netbios_name[0] = '\0';
00060 
00061         hnd->password = SMB_MALLOC_ARRAY( char, sizeof( fstring ) );
00062         if ( !hnd->password )
00063                 return CAC_FAILURE;
00064 
00065         hnd->password[0] = '\0';
00066 
00067         hnd->server = SMB_MALLOC_ARRAY( char, sizeof( fstring ) );
00068         if ( !hnd->server )
00069                 return CAC_FAILURE;
00070 
00071         hnd->server[0] = '\0';
00072 
00073         return CAC_SUCCESS;
00074 }
00075 
00076 CacServerHandle *cac_NewServerHandle( BOOL allocate_fields )
00077 {
00078         CacServerHandle *hnd;
00079 
00080         hnd = SMB_MALLOC_P( CacServerHandle );
00081 
00082         if ( !hnd ) {
00083                 errno = ENOMEM;
00084                 return NULL;
00085         }
00086 
00087         ZERO_STRUCTP( hnd );
00088 
00089         if ( allocate_fields == True ) {
00090                 if ( !cac_InitHandleMem( hnd ) ) {
00091                         SAFE_FREE( hnd );
00092                         return NULL;
00093                 }
00094         }
00095 
00096         hnd->_internal.ctx = smbc_new_context(  );
00097         if ( !hnd->_internal.ctx ) {
00098                 cac_FreeHandle( hnd );
00099                 return NULL;
00100         }
00101 
00102         hnd->_internal.ctx->callbacks.auth_fn = cac_GetAuthDataFn;
00103 
00104         /*add defaults */
00105         hnd->debug = 0;
00106 
00107         /*start at the highest and it will fall down after trying the functions */
00108         hnd->_internal.srv_level = SRV_WIN_2K3;
00109 
00110         hnd->_internal.user_supplied_ctx = False;
00111 
00112         return hnd;
00113 }
00114 
00115 int cac_InitHandleData( CacServerHandle * hnd )
00116 {
00117         /*store any automatically initialized values */
00118         if ( !hnd->netbios_name ) {
00119                 hnd->netbios_name =
00120                         SMB_STRDUP( hnd->_internal.ctx->netbios_name );
00121         } else if ( hnd->netbios_name[0] == '\0' ) {
00122                 strncpy( hnd->netbios_name, hnd->_internal.ctx->netbios_name,
00123                          sizeof( fstring ) );
00124         }
00125 
00126         if ( !hnd->username ) {
00127                 hnd->username = SMB_STRDUP( hnd->_internal.ctx->user );
00128         } else if ( hnd->username[0] == '\0' ) {
00129                 strncpy( hnd->username, hnd->_internal.ctx->user,
00130                          sizeof( fstring ) );
00131         }
00132 
00133         if ( !hnd->domain ) {
00134                 hnd->domain = SMB_STRDUP( hnd->_internal.ctx->workgroup );
00135         } else if ( hnd->domain[0] == '\0' ) {
00136                 strncpy( hnd->domain, hnd->_internal.ctx->workgroup,
00137                          sizeof( fstring ) );
00138         }
00139 
00140         return CAC_SUCCESS;
00141 }
00142 
00143 void cac_SetAuthDataFn( CacServerHandle * hnd, smbc_get_auth_data_fn auth_fn )
00144 {
00145         hnd->_internal.ctx->callbacks.auth_fn = auth_fn;
00146 }
00147 
00148 void cac_SetSmbcContext( CacServerHandle * hnd, SMBCCTX * ctx )
00149 {
00150 
00151         SAFE_FREE( hnd->_internal.ctx );
00152 
00153         hnd->_internal.user_supplied_ctx = True;
00154 
00155         hnd->_internal.ctx = ctx;
00156 
00157    /*_try_ to avoid any problems that might occur if cac_Connect() isn't called*/
00158         /*cac_InitHandleData(hnd); */
00159 }
00160 
00161 /*used internally*/
00162 SMBCSRV *cac_GetServer( CacServerHandle * hnd )
00163 {
00164         SMBCSRV *srv;
00165 
00166         if ( !hnd || !hnd->_internal.ctx ) {
00167                 return NULL;
00168         }
00169 
00170         srv = smbc_attr_server( hnd->_internal.ctx, hnd->server, "IPC$",
00171                                 hnd->domain, hnd->username, hnd->password,
00172                                 NULL );
00173         if ( !srv ) {
00174                 hnd->status = NT_STATUS_UNSUCCESSFUL;
00175                 DEBUG( 1,
00176                        ( "cac_GetServer: Could not find server connection.\n" ) );
00177         }
00178 
00179         return srv;
00180 }
00181 
00182 
00183 int cac_Connect( CacServerHandle * hnd, const char *srv )
00184 {
00185         if ( !hnd ) {
00186                 return CAC_FAILURE;
00187         }
00188 
00189         /*these values should be initialized by the user */
00190         if ( !hnd->server && !srv ) {
00191                 return CAC_FAILURE;
00192         }
00193 
00194 
00195         /*change the server name in the server handle if necessary */
00196         if ( srv && hnd->server && strcmp( hnd->server, srv ) == 0 ) {
00197                 SAFE_FREE( hnd->server );
00198                 hnd->server = SMB_STRDUP( srv );
00199         }
00200 
00201 
00202         /*first see if the context has already been setup */
00203         if ( !( hnd->_internal.ctx->internal->_initialized ) ) {
00204                 hnd->_internal.ctx->debug = hnd->debug;
00205 
00206                 /*initialize the context */
00207                 if ( !smbc_init_context( hnd->_internal.ctx ) ) {
00208                         return CAC_FAILURE;
00209                 }
00210         }
00211 
00212         /*copy any uninitialized values out of the smbc context into the handle */
00213         if ( !cac_InitHandleData( hnd ) ) {
00214                 return CAC_FAILURE;
00215         }
00216 
00217         DEBUG( 3, ( "cac_Connect: Username:     %s\n", hnd->username ) );
00218         DEBUG( 3, ( "cac_Connect: Domain:       %s\n", hnd->domain ) );
00219         DEBUG( 3, ( "cac_Connect: Netbios Name: %s\n", hnd->netbios_name ) );
00220 
00221         if ( !cac_GetServer( hnd ) ) {
00222                 return CAC_FAILURE;
00223         }
00224 
00225         return CAC_SUCCESS;
00226 
00227 }
00228 
00229 
00230 void cac_FreeHandle( CacServerHandle * hnd )
00231 {
00232         if ( !hnd )
00233                 return;
00234 
00235         /*only free the context if we created it */
00236         if ( !hnd->_internal.user_supplied_ctx ) {
00237                 smbc_free_context( hnd->_internal.ctx, True );
00238         }
00239 
00240         SAFE_FREE( hnd->netbios_name );
00241         SAFE_FREE( hnd->domain );
00242         SAFE_FREE( hnd->username );
00243         SAFE_FREE( hnd->password );
00244         SAFE_FREE( hnd->server );
00245         SAFE_FREE( hnd );
00246 
00247 }
00248 
00249 void cac_InitCacTime( CacTime * cactime, NTTIME nttime )
00250 {
00251         float high, low;
00252         uint32 sec;
00253 
00254         if ( !cactime )
00255                 return;
00256 
00257         ZERO_STRUCTP( cactime );
00258 
00259         /*this code is taken from display_time() found in rpcclient/cmd_samr.c */
00260         if ( nttime == 0 )
00261                 return;
00262 
00263         if ( nttime == 0x80000000000000LL )
00264                 return;
00265 
00266         high = 65536;
00267         high = high / 10000;
00268         high = high * 65536;
00269         high = high / 1000;
00270         high = high * ( ~( nttime >> 32 ) );
00271 
00272         low = ~( nttime & 0xFFFFFFFF );
00273         low = low / ( 1000 * 1000 * 10 );
00274 
00275         sec = high + low;
00276 
00277         cactime->days = sec / ( 60 * 60 * 24 );
00278         cactime->hours =
00279                 ( sec - ( cactime->days * 60 * 60 * 24 ) ) / ( 60 * 60 );
00280         cactime->minutes =
00281                 ( sec - ( cactime->days * 60 * 60 * 24 ) -
00282                   ( cactime->hours * 60 * 60 ) ) / 60;
00283         cactime->seconds =
00284                 sec - ( cactime->days * 60 * 60 * 24 ) -
00285                 ( cactime->hours * 60 * 60 ) - ( cactime->minutes * 60 );
00286 }
00287 
00288 void cac_GetAuthDataFn( const char *pServer,
00289                         const char *pShare,
00290                         char *pWorkgroup,
00291                         int maxLenWorkgroup,
00292                         char *pUsername,
00293                         int maxLenUsername,
00294                         char *pPassword, int maxLenPassword )
00295 {
00296         char temp[sizeof( fstring )];
00297 
00298         static char authUsername[sizeof( fstring )];
00299         static char authWorkgroup[sizeof( fstring )];
00300         static char authPassword[sizeof( fstring )];
00301         static char authSet = 0;
00302 
00303         char *pass = NULL;
00304 
00305 
00306         if ( authSet ) {
00307                 strncpy( pWorkgroup, authWorkgroup, maxLenWorkgroup - 1 );
00308                 strncpy( pUsername, authUsername, maxLenUsername - 1 );
00309                 strncpy( pPassword, authPassword, maxLenPassword - 1 );
00310         } else {
00311                 d_printf( "Domain: [%s] ", pWorkgroup );
00312                 fgets( temp, sizeof( fstring ), stdin );
00313 
00314                 if ( temp[strlen( temp ) - 1] == '\n' ) {       /* A new line? */
00315                         temp[strlen( temp ) - 1] = '\0';
00316                 }
00317 
00318 
00319                 if ( temp[0] != '\0' ) {
00320                         strncpy( pWorkgroup, temp, maxLenWorkgroup - 1 );
00321                         strncpy( authWorkgroup, temp, maxLenWorkgroup - 1 );
00322                 }
00323 
00324                 d_printf( "Username: [%s] ", pUsername );
00325                 fgets( temp, sizeof( fstring ), stdin );
00326 
00327                 if ( temp[strlen( temp ) - 1] == '\n' ) {       /* A new line? */
00328                         temp[strlen( temp ) - 1] = '\0';
00329                 }
00330 
00331                 if ( temp[0] != '\0' ) {
00332                         strncpy( pUsername, temp, maxLenUsername - 1 );
00333                         strncpy( authUsername, pUsername,
00334                                  maxLenUsername - 1 );
00335                 }
00336 
00337                 pass = getpass( "Password: " );
00338                 if ( pass )
00339                         fstrcpy( temp, pass );
00340                 if ( temp[strlen( temp ) - 1] == '\n' ) {       /* A new line? */
00341                         temp[strlen( temp ) - 1] = '\0';
00342                 }
00343                 if ( temp[0] != '\0' ) {
00344                         strncpy( pPassword, temp, maxLenPassword - 1 );
00345                         strncpy( authPassword, pPassword,
00346                                  maxLenPassword - 1 );
00347                 }
00348                 authSet = 1;
00349         }
00350 }

Sambaに対してSat Aug 29 21:23:01 2009に生成されました。  doxygen 1.4.7