38 #include <openssl/md5.h>
39 #include <openssl/sha.h>
40 #ifndef windows_platform
41 #include <sys/types.h>
43 #include <sys/timeb.h>
57 #include "Unix2Nt.hpp"
58 #include "iRODSNtUtil.hpp"
62 #include <boost/filesystem.hpp>
63 #include <boost/system/error_code.hpp>
64 #include <boost/filesystem/operations.hpp>
65 #include <boost/filesystem/convenience.hpp>
66 #define TMP_FLAG "%TEMPORARY_PW%"
68 #define AUTH_FILENAME_DEFAULT ".irods/.irodsA"
92 printf(
"Usage: -d|-e\n" );
96 if ( strcmp( argv[1],
"-d" ) == 0 ) {
99 printf(
"val = %d \n", i );
103 if ( strcmp( argv[1],
"-e" ) == 0 ) {
106 printf(
"val = %d \n", i );
123 if ( envVar !=
NULL && *envVar !=
'\0' ) {
124 strcpy( fileName, envVar );
128 #ifdef windows_platform
132 tmpstr1 = iRODSNtGetServerConfigPath();
133 sprintf( fileName,
"%s\\irodsA.txt", tmpstr1 );
138 envVar = iRODSNt_gethome();
140 envVar = getenv(
"HOME" );
142 if ( envVar ==
NULL ) {
149 #ifdef windows_platform
150 if ( envVar !=
NULL ) {
200 printf(
"out:%s\n", myPwD );
219 boost::filesystem::path filePath( fileName );
222 printf(
"%s does not exist\n", fileName );
227 printf(
"Remove %s?:", fileName );
228 const char *fgets_ret = fgets( inbuf,
MAX_NAME_LEN, stdin );
229 if ( fgets_ret ==
NULL || strlen( inbuf ) < 1 || inbuf[0] !=
'y' ) {
233 boost::system::error_code
error;
235 if (
error.value() ) {
246 #ifndef windows_platform
249 struct irodsntstat statBuf;
251 int wval, fval, lval;
252 wval = write( fd,
" ", 1 );
256 #ifndef windows_platform
257 fval = fstat( fd, &statBuf );
259 fval = _fstat64( fd, &statBuf );
265 lval = lseek( fd, 0, SEEK_SET );
270 timeVal = statBuf.st_mtime & 0xffff;
281 obfSavePw(
int promptOpt,
int fileOpt,
int printOpt,
const char *pwArg ) {
282 using namespace boost::filesystem;
286 int i = 0, fd = 0, envVal = 0;
295 if ( pwArg ==
NULL || strlen( pwArg ) == 0 ) {
296 #ifdef windows_platform
297 iRODSNtGetUserPasswdInputInConsole( inbuf,
"Enter your current iRODS password:", promptOpt );
299 irods::termiosUtil tiosutl(STDIN_FILENO);
300 if ( promptOpt != 1 ) {
301 if ( !tiosutl.echoOff() )
303 printf(
"WARNING: Error %d disabling echo mode. Password will be displayed in plaintext.\n", tiosutl.getError() );
307 printf(
"Enter your current iRODS password:" );
310 if ( promptOpt != 1 ) {
312 if( tiosutl.getValid() && !tiosutl.echoOn() )
314 printf(
"Error reinstating echo mode.\n" );
317 if (fgets_return != inbuf || strlen(inbuf) < 2)
337 if ( inbuf[i - 1] ==
'\n' ) {
362 printf(
" in:%s out:%s\n", inbuf, myPw );
372 printf(
"Successfully wrote %s\n", fileName );
406 #ifndef windows_platform
409 struct irodsntstat statBuf;
413 #ifndef windows_platform
414 fval = stat( fileName, &statBuf );
416 fval = iRODSNt_stat( fileName, &statBuf );
424 timeVal = statBuf.st_mtime & 0xffff;
430 #ifdef windows_platform
431 int fd_in = iRODSNt_open( fileName, O_RDONLY, 1 );
433 int fd_in = open( fileName, O_RDONLY, 0 );
459 int i = 0, fd_out = 0;
462 fd_out = iRODSNt_open( fileName, O_CREAT | O_WRONLY | O_EXCL, 1 );
464 fd_out = open( fileName, O_CREAT | O_WRONLY | O_EXCL, 0600 );
468 if ( errno != EEXIST ) {
472 printf(
"Overwrite '%s'?:", fileName );
482 strcpy( inbuf,
"y" );
484 if ( inbuf[0] ==
'y' ) {
485 #ifdef windows_platform
486 fd_out = iRODSNt_open( fileName, O_CREAT | O_WRONLY | O_TRUNC, 1 );
488 fd_out = open( fileName, O_CREAT | O_WRONLY | O_TRUNC, 0600 );
505 wval = write( fd, pw, len + 1 );
506 if ( wval != len + 1 ) {
516 struct timeval nowtime;
518 ( void )gettimeofday( &nowtime, (
struct timezone * )0 );
519 sec = nowtime.tv_sec;
524 printf(
"val = %d %x\n", val, val );
540 struct timeval nowtime;
544 int wheel[26 + 26 + 10 + 15];
545 int j, addin, addin_i, found;
556 wheel_len = 26 + 26 + 10 + 15;
558 for ( i = 0; i < 10; i++ ) {
561 for ( i = 0; i < 26; i++ ) {
564 for ( i = 0; i < 26; i++ ) {
567 for ( i = 0; i < 15; i++ ) {
583 ( void )gettimeofday( &nowtime, (
struct timezone * )0 );
584 rval = nowtime.tv_usec & 0xf;
648 headstring[1] = ( ( now >> 4 ) & 0xf ) +
'a';
649 headstring[2] = ( now & 0xf ) +
'a';
650 headstring[3] = ( ( now >> 12 ) & 0xf ) +
'a';
651 headstring[4] = ( ( now >> 8 ) & 0xf ) +
'a';
652 headstring[5] =
'\0';
653 headstring[0] =
'S' - ( ( rval & 0x7 ) * 2 );
666 addin = ( seq >> addin_i ) & 0x1f;
674 if ( addin_i > 28 ) {
677 for ( i = 0; i < wheel_len; i++ ) {
678 if ( *my_in == (
char )
wheel[i] ) {
681 printf(
"j1=%d ", j );
685 printf(
"j2=%d \n", j );
693 if ( *my_in ==
'\0' ) {
713 delta = time1 - time2;
717 if ( delta < fudge ) {
721 if ( time1 < 65000 ) {
724 if ( time2 < 65000 ) {
728 delta = time1 - time2;
732 if ( delta < fudge ) {
749 int wheel[26 + 26 + 10 + 15];
750 int j, addin, addin_i, kpos, found, nout = 0;
755 int not_en, encodedTime;
760 wheel_len = 26 + 26 + 10 + 15;
774 for ( i = 0; i < 10; i++ ) {
777 for ( i = 0; i < 26; i++ ) {
780 for ( i = 0; i < 26; i++ ) {
783 for ( i = 0; i < 15; i++ ) {
788 for ( p1 = in, i = 0; i < 6; i++ ) {
789 if ( *p1++ ==
'\0' ) {
796 for ( i = 0; i < kpos; i++, p1++ ) {
802 if ( rval > 15 || rval < 0 || too_short == 1 ) {
803 while ( ( *
out++ = *in++ ) !=
'\0' ) {
870 if ( headstring[0] !=
'S' - ( ( rval & 0x7 ) * 2 ) ) {
877 encodedTime = ( ( headstring[1] -
'a' ) << 4 ) + ( headstring[2] -
'a' ) +
878 ( ( headstring[3] -
'a' ) << 12 ) + ( ( headstring[4] -
'a' ) << 8 );
889 printf(
"timeVal=%d encodedTime=%d\n",
timeVal, encodedTime );
894 while ( ( *
out++ = *in++ ) !=
'\0' ) {
903 addin = ( seq >> addin_i ) & 0x1f;
911 if ( addin_i > 28 ) {
914 for ( i = 0; i < wheel_len; i++ ) {
915 if ( *my_in == (
char )
wheel[i] ) {
918 printf(
"j=%d ", j );
925 printf(
"j2=%d \n", j );
928 *my_out++ = ( char )
wheel[j];
935 if ( *my_in ==
'\0' ) {
971 unsigned const char *inBuf,
int inBufSize,
unsigned char *outHash ) {
976 static char outBuf[50];
981 printf(
"obfMakeOneWayHash sha1\n" );
983 SHA1_Init( &shaContext );
984 SHA1_Update( &shaContext, inBuf, inBufSize );
985 SHA1_Final( outHash, &shaContext );
989 printf(
"obfMakeOneWayHash md5\n" );
991 MD5_Init( &md5Context );
992 MD5_Update( &md5Context, inBuf, inBufSize );
993 MD5_Final( outHash, &md5Context );
995 sprintf( outBuf,
"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
996 outHash[0], outHash[1], outHash[2], outHash[3],
997 outHash[4], outHash[5], outHash[6], outHash[7],
998 outHash[8], outHash[9], outHash[10], outHash[11],
999 outHash[12], outHash[13], outHash[14], outHash[15] );
1012 int wheel_len = 26 + 26 + 10 + 15;
1013 int wheel[26 + 26 + 10 + 15];
1018 unsigned char buffer[65];
1019 unsigned char keyBuf[101];
1022 unsigned char *cpKey;
1025 printf(
"obfEncodeByKey enter key:%s:in:%s\n", key, in );
1029 for ( i = 0; i < 10; i++ ) {
1032 for ( i = 0; i < 26; i++ ) {
1035 for ( i = 0; i < 26; i++ ) {
1038 for ( i = 0; i < 15; i++ ) {
1043 snprintf( (
char* )
keyBuf,
sizeof(
keyBuf ),
"%s", key );
1075 k = (
int ) * cpKey++;
1076 if ( cpKey >
buffer + 60 ) {
1080 for ( i = 0; i < wheel_len; i++ ) {
1081 if ( *cpIn == (
char )
wheel[i] ) {
1084 *cpOut++ = ( char )
wheel[j];
1086 pc = *( cpOut - 1 );
1094 if ( *cpIn ==
'\0' ) {
1096 if (
obfDebug ) printf(
"obfEncodeByKey key:%s in:%s out:%s\n",
1117 #define V2_Prefix "A.ObfV2"
1121 struct timeval nowtime;
1128 strncat( myIn, in, 150 );
1130 strncpy( myKey, key, 90 );
1132 strncat( myKey, key2, 100 );
1137 ( void )gettimeofday( &nowtime, (
struct timezone * )0 );
1138 rval = nowtime.tv_usec & 0x1f;
1159 int wheel_len = 26 + 26 + 10 + 15;
1160 int wheel[26 + 26 + 10 + 15];
1164 unsigned char buffer[65];
1165 unsigned char keyBuf[101];
1168 unsigned char *cpKey;
1173 printf(
"obfDecodeByKey enter key:%s: in:%s\n", key, in );
1176 if ( strncmp( in,
"sha1", 4 ) == 0 ) {
1179 printf(
"using sha1 for decodebykey\n" );
1185 printf(
"using md5 for decodebykey\n" );
1191 for ( i = 0; i < 10; i++ ) {
1194 for ( i = 0; i < 26; i++ ) {
1197 for ( i = 0; i < 26; i++ ) {
1200 for ( i = 0; i < 15; i++ ) {
1205 snprintf( (
char* )
keyBuf,
sizeof(
keyBuf ),
"%s", key );
1230 k = (
int ) * cpKey++;
1231 if ( cpKey >
buffer + 60 ) {
1235 for ( i = 0; i < wheel_len; i++ ) {
1236 if ( *cpIn == (
char )
wheel[i] ) {
1241 *cpOut++ = ( char )
wheel[j];
1251 if ( *cpIn ==
'\0' ) {
1253 if (
obfDebug ) printf(
"obfDecodeByKey key:%s: in:%s out: %s\n",
1270 static char myOut[200];
1271 int i, len, matches;
1275 strncpy( myKey, key, 90 );
1277 strncat( myKey, key2, 100 );
1287 for ( i = 1; i < len; i++ ) {
1288 if (
match[i] != myOut[i] ) {
1292 if ( matches == 0 ) {
1309 unsigned char buffer[30];
1310 unsigned char keyBuf[101];
1312 static char outBuf[50];
1315 snprintf( (
char* )
keyBuf,
sizeof(
keyBuf ),
"%s", stringToHash );
1323 sprintf( outBuf,
"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",