188 lines
5.9 KiB
C
188 lines
5.9 KiB
C
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#ifdef WIN32
|
|
#include <time.h>
|
|
#include "windows.h"
|
|
#else
|
|
#include <sys/time.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
#include <errno.h>
|
|
|
|
#include "eyeutils.h"
|
|
#include "eyeinterface.h"
|
|
//#include "eyecontainer.h"
|
|
//#include "eyering.h"
|
|
|
|
#ifdef WIN32
|
|
const char *getError(int err);
|
|
#endif
|
|
|
|
/**
|
|
* Usage: ionu_pbkdf2 [--open-browser [--host host]] [password] [user]
|
|
* If omitted, password defaults to "password"
|
|
* Prints out the hash, the input password and the elapsed time.
|
|
*/
|
|
int main(int argc, char** argv){
|
|
//printf("pbkdf2\n");
|
|
|
|
unsigned char buf[32];
|
|
int argIdx = 1;
|
|
bool browse = false;
|
|
char host[256];
|
|
|
|
strcpy(host, "http://localhost:8080");
|
|
|
|
// args
|
|
bool bHelp = false;
|
|
if (argc <= 1) {
|
|
bHelp = true;
|
|
}
|
|
if (argIdx < argc && strcmp("--open-browser", argv[argIdx]) == 0){
|
|
browse = true;
|
|
++argIdx;
|
|
//printf("argv[%d] = %s\n", argIdx, argv[argIdx]);
|
|
if (argIdx < argc && strcmp("--host", argv[argIdx])==0){
|
|
//printf("Using host %s\n", argv[argIdx+1]);
|
|
strcpy(host, argv[++argIdx]);
|
|
argIdx++;
|
|
}
|
|
}
|
|
if (bHelp) {
|
|
printf("Usage: ionu_pbkdf2 [--open-browser [--host host]] [password] [user]\n");
|
|
printf("If omitted, password defaults to \"password\"\n");
|
|
printf("Prints out the hash, the input password and the elapsed time.\n");
|
|
exit(0);
|
|
}
|
|
//printf("Will open page at: %s\n", host);
|
|
//printf("argv[%d] = %s\n", argIdx, argv[argIdx]);
|
|
|
|
char* pass = (argIdx < argc) ? argv[argIdx++] : (char*)"password";
|
|
char* user = (argIdx < argc) ? argv[argIdx++] : (char*)0;
|
|
//printf("User: %s password: %s\n", user, pass);
|
|
|
|
#ifdef WIN32
|
|
clock_t time_in_mill, time_in_mill2;
|
|
time_in_mill = clock() / (CLOCKS_PER_SEC / 1000);
|
|
#else
|
|
struct timeval tv, tv2;
|
|
gettimeofday(&tv, NULL);
|
|
long time_in_mill =
|
|
(tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ; // convert tv_sec & tv_usec to millisecond
|
|
#endif
|
|
|
|
ionu_derive_key(pass, buf);
|
|
|
|
char base64buf[256];
|
|
sequencelogic::Base64Encode(buf, 32, base64buf);
|
|
|
|
#ifdef WIN32
|
|
time_in_mill2 = clock() / (CLOCKS_PER_SEC / 1000);
|
|
#else
|
|
gettimeofday(&tv2, NULL);
|
|
long time_in_mill2 =
|
|
(tv2.tv_sec) * 1000 + (tv2.tv_usec) / 1000 ; // convert tv_sec & tv_usec to millisecond
|
|
#endif
|
|
|
|
printf("%s %s %ld ms\n", base64buf, pass, (time_in_mill2-time_in_mill));
|
|
|
|
if (browse){
|
|
printf("Opening default browser...\n");
|
|
#ifdef WIN32
|
|
char url[256];
|
|
sprintf(url, "http://%s/cgws/office.jsp?pbkdf2=%s&user=%s", host, base64buf, user);
|
|
printf("Execute browser process: %s\n", url);
|
|
int e = (int)::ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOW);
|
|
if (e < 32) {
|
|
printf("[ERROR] Unable to exec program; errno=%d (%s)\n", e, getError(e));
|
|
}
|
|
}
|
|
#else // not WIN32 use fork/exec/open
|
|
pid_t pid = fork();
|
|
if (pid < 0){
|
|
printf("[ERROR] Unable to fork() process\n");
|
|
exit(1);
|
|
} else if (pid == 0){
|
|
// child, exec
|
|
char* program = new char[80];
|
|
#if defined(__APPLE__)
|
|
strcpy(program, "open"); // Darwin
|
|
#else
|
|
strcpy(program, "xdg-open"); // http://askubuntu.com/questions/8252/how-to-launch-default-web-browser-from-the-terminal
|
|
#endif
|
|
char url[256];
|
|
sprintf(url, "%s/cgws/office.jsp?pbkdf2=%s&user=%s", host, base64buf, user);
|
|
char* args[] = {program, url, (char*)0};
|
|
printf("Execute browser process: %s %s\n", program, args[1]);
|
|
int e = execvp(program, args);
|
|
if (e != 0){
|
|
printf("[ERROR] Unable to exec program; errno=%d (%s)\n", errno, strerror(errno));
|
|
}
|
|
} else {
|
|
printf("Browser process %d has been spawned\n", pid);
|
|
}
|
|
}
|
|
#endif // not WIN32
|
|
exit(0);
|
|
}
|
|
|
|
#ifdef WIN32
|
|
const char *getError(int err)
|
|
{
|
|
switch (err)
|
|
{
|
|
case 0:
|
|
return "The operating system is out of memory or resources.";
|
|
break;
|
|
case ERROR_FILE_NOT_FOUND:
|
|
return "The specified file was not found.";
|
|
break;
|
|
case ERROR_PATH_NOT_FOUND:
|
|
return "The specified path was not found.";
|
|
break;
|
|
case ERROR_BAD_FORMAT:
|
|
return "The .exe file is invalid (non-Win32 .exe or error in .exe image).";
|
|
break;
|
|
case SE_ERR_ACCESSDENIED:
|
|
return "The operating system denied access to the specified file.";
|
|
break;
|
|
case SE_ERR_ASSOCINCOMPLETE:
|
|
return "The file name association is incomplete or invalid.";
|
|
break;
|
|
case SE_ERR_DDEBUSY:
|
|
return "The DDE transaction could not be completed because other DDE transactions were being processed.";
|
|
break;
|
|
case SE_ERR_DDEFAIL:
|
|
return "The DDE transaction failed.";
|
|
break;
|
|
case SE_ERR_DDETIMEOUT:
|
|
return "The DDE transaction could not be completed because the request timed out.";
|
|
break;
|
|
case SE_ERR_DLLNOTFOUND:
|
|
return "The specified DLL was not found.";
|
|
break;
|
|
// case SE_ERR_FNF:
|
|
// return "The specified file was not found.";
|
|
// break;
|
|
case SE_ERR_NOASSOC:
|
|
return "There is no application associated with the given file name extension.";
|
|
break;
|
|
case SE_ERR_OOM:
|
|
return "There was not enough memory to complete the operation.";
|
|
break;
|
|
// case SE_ERR_PNF:
|
|
// return "The specified path was not found.";
|
|
// break;
|
|
case SE_ERR_SHARE:
|
|
return "A sharing violation occurred.";
|
|
break;
|
|
default:
|
|
return "Unknown error.";
|
|
break;
|
|
}
|
|
}
|
|
#endif
|