254 lines
7.4 KiB
C++

/*
* INTEL CONFIDENTIAL
* Copyright (c) 2007 Intel Corporation. All Rights Reserved.
* The source code contained or described herein and all documents related
* to the source code ("Material") are owned by Intel Corporation or its
* suppliers or licensors. Title to the Material remains with Intel Corporation
* or its suppliers and licensors. The Material contains trade secrets and
* proprietary and confidential information of Intel or its suppliers and l
* icensors. The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used, copied,
* reproduced, modified, published, uploaded, posted, transmitted, distributed,
* or disclosed in any way without Intel's prior express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be
* expressed and approved by Intel in writing.
*/
//----------------------------------------------------------------------------
//
// Copyright (C) Intel Corporation, 2004 - 2007.
//
// File: SOAPAuthentication.cpp
//
// Notes: This file contains the implementation of the SOAP Authentication
//
//
//----------------------------------------------------------------------------
#include <iostream>
#include <string.h>
#include <tchar.h>
#define _WINSOCKAPI_
#include <windows.h>
#include "SOAPAuthentication.h"
#include "CommonDefinitions.h"
#include "SoapCommonDefinitions.h"
#include "MPSAuthenticationSoapBinding.nsmap"
#include "Options.h"
#include "gSoapGeneratedCode\soapStub.h"
#include <sstream>
using namespace std;
/*
* Constants for the common use
*/
static const char * CMD_TLS = "tls";
static const char * CMD_TARGET = "target";
static const char * CMD_KRB = "krb";
static const char * CMD_VERBOSE = "verbose";
static const char * CMD_USER = "user";
static const char * CMD_PASS = "pass";
static const char * CMD_CERT_NAME= "cert";
static const char * CMD_PROXY = "proxy";
static const char * CMD_PROXY_USER_NAME = "proxyUserName";
static const char * CMD_PROXY_PASS = "proxyPass";
#define TRUE 1
#define FALSE 0
/*
* Print usage information for the user.
* Arguments:
* cmd - pointer to the string that holds the application name
*/
void Usage(string& errStr)
{
errStr=
"Parse connection string error:\nUsage:\n\t -target <target> [-krb] [-user <username> -pass <password>] [-cert <name>][-tls][-proxy <proxy> -proxyUserName <proxyUserName> -proxyPass <proxyPassword>] \n\nExample: -target http://hostname:7792/MPSAuthenticationService -user admin -pass Admin!123\n";
}
bool returnDll(string err_str, char* err, UINT32 err_len, bool ret)
{
//copy error string
if ((err_str.length() != 0 ) && (err_str.length() < err_len))
{
strcpy_s(err, err_len, err_str.c_str());
}
return ret;
}
// parse the commnad line arguments and set them in the options Map
bool Parse(int argc, char* argv[], Options* options)
{
const Options::FormatNode format[] =
{ {CMD_VERBOSE,true}, {CMD_USER,true}, {CMD_PASS,true}, {CMD_CERT_NAME,true},
{CMD_TARGET,true}, {CMD_TLS,false}, {CMD_KRB,false}, {CMD_PROXY,true} ,
{CMD_PROXY_USER_NAME,true}, {CMD_PROXY_PASS,true}};
if (!options->Parse(argc, const_cast<const char**>(argv), format,
sizeof(format)/sizeof(Options::FormatNode)))
{
return false;
}
//must have either userName & Password or must be Kerberose
if((!(options->OptionExists(CMD_USER) && options->OptionExists(CMD_PASS))) && (!options->OptionExists(CMD_KRB)))
{
return false;
}
// must have target
if (!options->OptionExists(CMD_TARGET))
{
return false;
}
// check that if proxy username or proxy password is given, than the other one is also available
// and that both are given only if proxy is available
int numOfProxyCount = 0;
if (options->OptionExists(CMD_PROXY_USER_NAME))
{
++numOfProxyCount;
}
if (options->OptionExists(CMD_PROXY_PASS))
{
++numOfProxyCount;
}
if ((numOfProxyCount == 1) || ((numOfProxyCount == 2) && (!options->OptionExists(CMD_PROXY))))
{
return false;
}
return true;
}
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
void delimitStringToVec(vector<string>& vec, const char * strToDelim)
{
const char delimit = '-';
stringstream s(strToDelim);
string strBuffer = "";
do{
string curWord;
s >> curWord;
if(curWord.length() != 0)
{
if (curWord.at(0) == delimit)
{
if (strBuffer.length() != 0)
{
vec.push_back(strBuffer);
}
vec.push_back(curWord);
strBuffer = "";
}
else
{
if (strBuffer.length() != 0)
{
strBuffer +=" ";
}
strBuffer +=curWord;
}
}
}while (s);
if (strBuffer.length() != 0)
{
vec.push_back(strBuffer);
}
}
UINT8 Authenticate(const char* user_name ,const char* user_password , const char* dll_params ,char* err_str, UINT32 err_len)
{
Options options;
int argc = 0;
char** argv ;
Soap* soap;
string user = user_name;
string pwd = user_password;
string tmp_err;
stringstream s(dll_params);
vector<string> vec;
delimitStringToVec(vec, dll_params);
argc =(int) vec.size()+1;
argv = new char*[argc];
argv[0] = "connection string";
for (int i = 1;i<argc;i++)
{
argv[i] =(char *)vec[i-1].c_str();
}
if(false == Parse(argc, argv, &options))
{
delete [] argv;
Usage(tmp_err);
return returnDll(tmp_err, err_str,err_len, FALSE);
}
soap = new Soap((char *)options.GetOption(CMD_TARGET), (char *)options.GetOption(CMD_CERT_NAME), (char *) options.GetOption(CMD_USER), (char *)options.GetOption(CMD_PASS), options.OptionExists(CMD_TLS), options.OptionExists(CMD_KRB));
soap->Init(namespaces);
//Validating user-name and password parameters.
if (strnlen_s(user_name, USER_MAX_LEN + 1) > USER_MAX_LEN)
{
printf("Invalid parameter: user-name can contain up to %u characters.\n", USER_MAX_LEN);
return returnDll(tmp_err, err_str, err_len, FALSE);
}
if(strnlen_s(user_password, PASS_MAX_LEN + 1) > PASS_MAX_LEN || (strnlen_s(user_password, PASS_MIN_LEN) < PASS_MIN_LEN && strnlen_s(user_password, PASS_MIN_LEN) != 0))
{
printf("Invalid parameter: password can contain between %u to %u characters.\n", PASS_MIN_LEN, PASS_MAX_LEN);
}
_mpa__AuthenticateRequest request;
request.AuthenticateId = user;
request.AuthenticatePassword = pwd;
_mpa__AuthenticateResponse response;
if(soap_call___mpa__Authenticate(soap->GetSoap(),soap->GetIp(),
NULL,&request,response))
{
tmp_err = "SOAP failure";
delete [] argv;
delete soap;
return returnDll(tmp_err,err_str, err_len,FALSE);
}
UINT8 res = (response.AuthenticateResult)? TRUE:FALSE;
delete [] argv;
delete soap;
return returnDll(tmp_err,err_str, err_len,res);
}