📄 admin.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Intrface/Admin.cpp,v $
* $Date: 2003/05/13 18:42:05 $
*
Description:
\*____________________________________________________________________________*/
/* $SourceTop:$ */
#include <assert.h>
#include <fstream.h>
#include <ctype.h>
#include <stdio.h>
#include "QConfig.h"
#include "Pi3File.h"
#include "HTTPCore.h"
#include "PIStrStr.h"
#include "StrToken.h"
#include "DeQuote.h"
#include "Admin.h"
#include "PIStream.h"
#include "Base64.h"
#include "md5.h"
/*
** #define D { cerr << __FILE__ << ": " << __LINE__ << endl; }
*/
#define D
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
class _Admin
{
private:
int iRemote;
public:
PIObject *pParentObject;
ConnectionPool *pPool;
PIString sFileName;
PIString sHost;
int iPort;
PIString sUsername;
PIString sPassword;
PIString sRealm;
cbAuthData *cbGetAuthData;
void *pUserData;
private:
int iOK;
public:
_Admin(
PIObject *pParent,
const char *pHost,
int iThePort,
const char *pUsername,
const char *pPassword,
cbAuthData *pGetAuthData,
void *pTheUserData )
:
iRemote( 1 ),
pParentObject( pParent ),
pPool( 0 ),
sFileName(),
sHost( pHost ),
iPort( iThePort ),
sUsername( pUsername ),
sPassword( pPassword ),
cbGetAuthData( pGetAuthData ),
pUserData( pTheUserData ),
iOK( 0 )
{
HTTPCore_initClient();
PIObject *pIOObject = PIObject_loadFromLine(
PIObject_getDB( pParent ),
PIObject_getConfigurationDB( pParent ),
"ClientIOObject" );
if ( !pIOObject )
{ return; };
pPool = ConnectionPool_new( pIOObject, 0 );
iOK = 1;
};
_Admin( const char *pFileName )
:
iRemote( 0 ),
pParentObject( 0 ),
pPool( 0 ),
sFileName( pFileName ),
sHost(),
iPort( -1 ),
sUsername(),
sPassword(),
cbGetAuthData( 0 ),
pUserData( 0 ),
iOK( 1 )
{};
inline int IsOK() const { return iOK; };
inline int IsRemote() const { return iRemote; };
~_Admin()
{
if ( pPool )
{ ConnectionPool_delete( pPool ); };
if ( IsRemote() )
{ HTTPCore_cleanupClient(); };
};
};
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static int Internal_loadConfigFromFile( QConfig *pConfig,
const char *pFileName )
{
assert( pFileName );
fstream ifs( pFileName, ios::in | ios::nocreate );
if ( !ifs || ifs.eof() )
{ return -1; };
int iRet = Pi3File_readConfig( pConfig, ifs );
ifs.close();
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static int Internal_writeConfigToFile( QConfig *pConfig,
const char *pFileName )
{
assert( pFileName && pConfig );
ofstream ofs( pFileName, ios::out );
if ( ofs.bad() || ofs.eof() )
{ return -1; };
int iRet = Pi3File_writeConfig( pConfig, ofs );
ofs.close();
return iRet; /* success */
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static void Internal_BasicAuthentication( PIDB *pQ,
const char *pUsername, const char *pPassword )
{
PIString sTmp = pUsername;
sTmp.Concatenate(':');
sTmp.Concatenate(pPassword);
PIString sResult = "Basic ";
Base64Encode((const char *)sTmp, sResult);
PIDB_replace( pQ, PIDBTYPE_RFC822, KEY_HTTP_AUTHORIZATION,
(void *)(const char *)sResult, 0);
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static void Internal_DigestAuthentication( PIDB *pQ,
const char *pAuthenticate, const char *pUsername, const char *pPassword)
{
assert(pAuthenticate);
/*
** Check if Digest authentication applies
*/
if ( !strncmp( pAuthenticate, "Digest ", 7 ) )
{
PIString sNonce;
PIString sRealm;
const char *pURI = (const char *)PIDB_lookup( pQ,
PIDBTYPE_STRING, KEY_HTTP_URI, 0);
int iMethod = (int)PIDB_lookup( pQ,
PIDBTYPE_OPAQUE, KEY_HTTP_METHOD, 0);
/*
** Get the challenge data from server string
*/
StringTokenizer tTokens( pAuthenticate + 7, "," );
for(int i=0; i<tTokens.NumTokens(); i++)
{
const char *pToken = tTokens.GetToken(i);
/* --- skip leading whitespace --- */
for( ; *pToken && (isspace(*pToken)); pToken++ );
if (strstr(pToken,KEY_NONCE))
{
sNonce = (const char *)DeQuote(
strstr(pToken,KEY_NONCE)+sizeof(KEY_NONCE));
}
else if (strstr(pToken,KEY_REALM))
{
sRealm = (const char *)DeQuote(
strstr(pToken,KEY_REALM)+sizeof(KEY_REALM));
}
}
PIString sTmp = pUsername;
PIString sHA1;
if ( strstr(pPassword, "0x") != pPassword )
{
// A1
sTmp.Concatenate(':');
sTmp.Concatenate(sRealm);
sTmp.Concatenate(':');
sTmp.Concatenate(pPassword);
// H(A1)
MD5Encode((const char *)sTmp, sHA1);
}
else
{
sHA1 = pPassword+2;
};
// A2
sTmp = pMethodMap[iMethod];
sTmp.Concatenate(':');
sTmp.Concatenate(pURI);
// H(A2)
PIString sHA2;
MD5Encode((const char *)sTmp, sHA2);
// H(A1) : nonce : H(A2)
sTmp = sHA1;
sTmp.Concatenate(':');
sTmp.Concatenate(sNonce);
sTmp.Concatenate(':');
sTmp.Concatenate(sHA2);
PIString sResult = "Digest username=\"";
sResult.Concatenate(pUsername);
sResult.Concatenate("\",realm=\"");
sResult.Concatenate(sRealm);
sResult.Concatenate("\",nonce=\"");
sResult.Concatenate(sNonce);
sResult.Concatenate("\",uri=\"");
sResult.Concatenate(pURI);
sResult.Concatenate("\",response=\"");
MD5Encode((const char *)sTmp, sResult);
sResult.Concatenate('"');
PIDB_replace( pQ, PIDBTYPE_RFC822, KEY_HTTP_AUTHORIZATION,
(void *)(const char *)sResult, 0);
}
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static int Internal_GetAuthType( PIDB *pR )
{
const char *pAuthenticate = (const char *)PIDB_lookup(
pR, PIDBTYPE_RFC822, KEY_HTTP_WWW_AUTHENTICATE, 0 );
int iAuthType = 0;
if (pAuthenticate)
{
StringTokenizer tTokens( pAuthenticate, " " );
for(int i=0; i<tTokens.NumTokens(); i++)
{
const char *pToken = tTokens.GetToken(i);
/* --- skip leading whitespace --- */
for( ; *pToken && (isspace(*pToken)); pToken++ );
if (strstr(pToken,KEY_BASIC_AUTH))
{
iAuthType = ID_BASIC_AUTH;
}
else if (strstr(pToken,KEY_DGST_AUTH))
{
iAuthType = ID_DGST_AUTH;
}
else if (strstr(pToken,KEY_NTLM_AUTH))
{
iAuthType = ID_NTLM_AUTH;
}
}
}
return iAuthType;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
static const char *Internal_GetRealm( const char *pAuthenticate )
{
assert(pAuthenticate);
StringTokenizer tTokens( pAuthenticate, " " );
for(int i=0; i<tTokens.NumTokens(); i++)
{
const char *pToken = tTokens.GetToken(i);
/* --- skip leading whitespace --- */
for( ; *pToken && (isspace(*pToken)); pToken++ );
if (strstr(pToken,KEY_REALM))
{
char *pRealm = strstr(pToken,KEY_REALM)+sizeof(KEY_REALM);
if (pRealm[strlen(pRealm)-1] != '"')
{ pRealm[strlen(pRealm)-1] = '\0'; }
return (const char *)DeQuote(pRealm);
}
}
return NULL;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Returns a PIAPI succes code or an HTTP request status, in any case
0 on success, otherwise an error.
\*____________________________________________________________________________*/
static int Internal_makeHTTPRequest(
PIIOBuffer *pBuffer,
int iMethod,
const char *pAdminCommand,
const char *pHost,
int iPort,
const char *pContentType,
const char *pContentLength,
int (* fnPutData)( void *pParam ), void *pPutDataParam,
int (* fnGetData)( void *pParam ), void *pGetDataParam,
const char *pUsername,
const char *pPassword,
PIString &sAuthenticate,
int *iAuthType,
PIString &sError
)
{
assert( pBuffer );
const char *pCLF;
PIDB *pQ = PIDB_new( 0, "Q" );
PIDB *pR = PIDB_new( 0, "R" );
/*
** Add some headers
*/
PIDB_add( pQ, PIDBTYPE_OPAQUE, KEY_HTTP_PROTOCOL, (void *)PR_HTTP10, 0 );
PIDB_add( pQ, PIDBTYPE_OPAQUE, KEY_HTTP_CONNECTION, (void *)0, 0 );
PIDB_add( pQ, PIDBTYPE_OPAQUE, KEY_HTTP_METHOD, (void *)iMethod, 0 );
PIDB_add( pQ, PIDBTYPE_STRING, KEY_HTTP_URI, "/", 0 );
PIDB_add( pQ, PIDBTYPE_RFC822, KEY_HTTP_X_ADMINCOMMAND,
(void *)pAdminCommand, 0 );
if (atoi(pContentLength))
{
PIDB_add( pQ, PIDBTYPE_RFC822, KEY_HTTP_CONTENTTYPE,
(void *)pContentType, 0 );
PIDB_add( pQ, PIDBTYPE_RFC822, KEY_HTTP_CONTENTLENGTH,
(void *)pContentLength, 0 );
}
PIDB_add( pQ, PIDBTYPE_RFC822, "User-Agent",
(void *)"Pi3Web-Client/1.0", 0);
/* ---
Make sure the 'host' header is sent
--- */
PIOStrStream os;
os << pHost;
if ( iPort != HTTP_DEFAULT_PORT )
{
os << ":" << iPort;
};
os << ends;
PIDB_replace( pQ, PIDBTYPE_RFC822, KEY_HTTP_HOST,
(void *)os.str(), 0 );
/*
** Compute the authentication headers
*/
switch (*iAuthType)
{
case ID_BASIC_AUTH:
Internal_BasicAuthentication(
pQ,
pUsername,
pPassword
);
break;
case ID_DGST_AUTH:
Internal_DigestAuthentication(
pQ,
(const char *)sAuthenticate,
pUsername,
pPassword
);
break;
default:;
}
/*
** Make the request
*/
int iRet = HTTPUtil_doHTTPRequest( pBuffer, pQ );
if ( iRet != PIAPI_COMPLETED )
{
sError = "Failed to connect to remote server '";
sError.Concatenate( pHost );
sError.Concatenate( "'." );
return iRet;
};
/* --- more data to send? --- */
if ( fnPutData!=0 )
{
iRet = (fnPutData)( pPutDataParam );
if ( iRet )
{
sError = "Error sending data to remote server '";
sError.Concatenate( pHost );
sError.Concatenate( "'." );
return iRet;
};
};
/* --- send the whole thing on its way --- */
PIIOBuffer_flush( pBuffer );
/*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -