265 lines
6.0 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: Authentication.cpp
//
// Notes: This file contains the implementation of the DSGI Authentication
//
//
//----------------------------------------------------------------------------
#include <sstream>
#include <iostream>
#include <fstream>
#include <iostream>
#include <string.h>
#include <tchar.h>
#include <windows.h>
#include <vector>
#include "Authentication.h"
#include "Options.h"
using namespace std;
/*
* Constants for the common use
*/
static const char * CMD_FILE = "file";
#define TRUE 1
#define FALSE 0
#define MAX_STRING_LEN 1024
#define DELIMITER ':'
/**
* Description - removes trailing '\n' or '\r''\n' from char*
* IN toChomp - string to chomp
* Max string size is MAX_STRING_LEN
* return: void
*/
void Chomp(char * toChomp) {
size_t toChompLen = strnlen_s(toChomp, MAX_STRING_LEN);
if ((toChompLen > 0) && (toChomp[toChompLen-1] == '\n')) {
if ((toChompLen-1 > 0) && (toChomp[toChompLen-2] == '\r')) {
toChomp[toChompLen-2] = '\0';
}
else {
toChomp[toChompLen-1] = '\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 -file <file>\n\nExample: -file AuthFile.txt";
}
// parse the command line arguments and set them in the options Map
bool Parse(int argc, char* argv[], Options* options)
{
const Options::FormatNode format[] =
{{CMD_FILE,true}};
if (!options->Parse(argc, const_cast<const char**>(argv), format,
sizeof(format)/sizeof(Options::FormatNode)))
{
return false;
}
// must have file
if (!options->OptionExists(CMD_FILE))
{
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 = NULL ;
string user = user_name;
string pwd = user_password;
string tmp_err;
vector<string> vec;
UINT8 status = FALSE;
do{
delimitStringToVec(vec, dll_params);
argc =(int) vec.size() + 1;
argv = new char*[argc];
if (argv == NULL)
{
tmp_err = "Memory allocation error";
break;
}
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))
{
Usage(tmp_err);
break;
}
// this delim is specified as not a usable letter for password
size_t delimIndex;
char buffer[MAX_STRING_LEN];
string strBuffer, fUsername, fPassword;
// file to be append (currently only a const file name)
FILE * pFile =NULL;
const char* fileName = options.GetOption(CMD_FILE);
pFile = fopen(fileName, "r" );
if (pFile == NULL) {
tmp_err = "Error opening file.\n";
}
else {
while (fgets(buffer, MAX_STRING_LEN, pFile) != NULL) {
Chomp(buffer);
strBuffer = buffer;
delimIndex = strBuffer.find(DELIMITER);
if (delimIndex == string::npos) {
tmp_err = "File in not correct format, delimiter character missing.\n";
continue;
}
fUsername = strBuffer.substr(0, delimIndex);
fPassword = strBuffer.substr(delimIndex + 1 , strBuffer.length() - delimIndex);
if ((fUsername.compare(user) == 0) &&
(fPassword.compare(pwd) == 0) ) {
status = TRUE;
break;
}
}
if(status != TRUE)
{
tmp_err = "Wrong user name or password.\n";
}
fclose (pFile);
}
}while(0);
if(argv)
delete []argv;
//copy error string
if ((tmp_err.length() != 0 ) && (tmp_err.length() < err_len))
{
strcpy_s(err_str, err_len, tmp_err.c_str());
}
return status;
}