2022-06-11 19:00:24 +02:00
# define _XOPEN_SOURCE 500
2022-05-01 16:23:07 +02:00
# include <why2/misc.h>
2022-04-22 18:39:48 +02:00
# include <string.h>
# include <unistd.h>
2022-05-30 19:09:47 +02:00
# include <sys/time.h>
2022-06-11 19:00:24 +02:00
# include <ftw.h>
2022-04-22 18:39:48 +02:00
# include <curl/curl.h>
# include <json-c/json.h>
2022-06-11 19:00:24 +02:00
# include <git2.h>
2022-04-22 18:39:48 +02:00
2022-05-01 16:23:07 +02:00
# include <why2/flags.h>
2022-04-22 18:39:48 +02:00
2022-06-19 17:19:03 +02:00
int unlink_cb ( const char * fpath , UNUSED const struct stat * sb , UNUSED int typeflag , UNUSED struct FTW * ftwbuf )
2022-06-11 19:00:24 +02:00
{
int rv = remove ( fpath ) ;
if ( rv ) perror ( fpath ) ;
return rv ;
}
int removeDirectory ( char * path )
{
return nftw ( path , unlink_cb , 64 , FTW_DEPTH | FTW_PHYS ) ;
}
char * replaceWord ( char * string , char * old , char * new ) //CODE FROM: https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word
{
char * result ;
int i , cnt = 0 ;
int newLen = strlen ( new ) ;
int oldLen = strlen ( old ) ;
for ( i = 0 ; string [ i ] ! = ' \0 ' ; i + + )
{
if ( strstr ( & string [ i ] , old ) = = & string [ i ] )
{
cnt + + ;
i + = oldLen - 1 ;
}
}
result = ( char * ) malloc ( i + cnt * ( newLen - oldLen ) + 1 ) ;
i = 0 ;
while ( * string )
{
// compare the substring with the result
if ( strstr ( string , old ) = = string )
{
strcpy ( & result [ i ] , new ) ;
i + = newLen ;
string + = oldLen ;
}
else result [ i + + ] = * string + + ;
}
result [ i ] = ' \0 ' ;
return result ;
}
2022-07-10 18:24:44 +02:00
unsigned char checkVersion ( )
2022-04-22 18:39:48 +02:00
{
2022-07-10 18:26:41 +02:00
if ( getFlags ( ) . noCheck ) return SUCCESS ;
2022-04-29 18:00:38 +02:00
2022-04-29 17:31:01 +02:00
//FILE-CHECK VARIABLES
int notFoundBuffer = 0 ;
2022-04-29 17:16:32 +02:00
//REMOVE versions.json
if ( access ( VERSIONS_NAME , F_OK ) = = 0 )
{
remove ( VERSIONS_NAME ) ;
}
2022-04-22 18:39:48 +02:00
//CURL VARIABLES
CURL * curl = curl_easy_init ( ) ;
FILE * fileBuffer = fopen ( VERSIONS_NAME , " w " ) ;
//GET versions.json
curl_easy_setopt ( curl , CURLOPT_URL , VERSIONS_URL ) ;
curl_easy_setopt ( curl , CURLOPT_WRITEDATA , fileBuffer ) ;
//DOWNLOAD versions.json
curl_easy_perform ( curl ) ;
//CLEANUP
curl_easy_cleanup ( curl ) ;
fclose ( fileBuffer ) ;
2022-04-29 17:31:01 +02:00
while ( access ( VERSIONS_NAME , R_OK ) ! = 0 )
{
notFoundBuffer + + ;
if ( notFoundBuffer = = NOT_FOUND_TRIES )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " %s'%s' not found! Exiting... \n " , CLEAR_SCREEN , VERSIONS_NAME ) ;
2022-06-12 16:34:19 +02:00
return DOWNLOAD_FAILED ;
2022-04-29 17:31:01 +02:00
}
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) printf ( " %s'%s' not found (%dx)! Trying again in a second. \n " , CLEAR_SCREEN , VERSIONS_NAME , notFoundBuffer ) ;
2022-04-29 17:31:01 +02:00
sleep ( 1 ) ;
}
2022-04-22 18:39:48 +02:00
//JSON VARIABLES
2022-05-24 17:51:19 +02:00
char * buffer ;
2022-04-22 18:39:48 +02:00
struct json_object * parsedJson ;
struct json_object * active ;
2022-05-29 18:40:33 +02:00
int bufferSize ;
2022-04-22 18:39:48 +02:00
2022-05-29 18:40:33 +02:00
//COUNT LENGTH OF buffer AND STORE IT IN bufferSize
2022-05-24 17:51:19 +02:00
fileBuffer = fopen ( VERSIONS_NAME , " r " ) ;
fseek ( fileBuffer , 0 , SEEK_END ) ;
2022-05-29 18:40:33 +02:00
bufferSize = ftell ( fileBuffer ) ;
2022-05-25 17:18:39 +02:00
rewind ( fileBuffer ) ; //REWIND fileBuffer (NO SHIT)
2022-05-24 17:51:19 +02:00
2022-05-29 18:40:33 +02:00
//SET LENGTH OF buffer
2022-06-13 18:13:27 +02:00
buffer = malloc ( bufferSize + 1 ) ;
2022-05-29 18:40:33 +02:00
2022-05-12 18:42:17 +02:00
//FIX buffer
strcpy ( buffer , " " ) ;
2022-04-22 18:39:48 +02:00
//LOAD jsonFile
2022-05-29 18:40:33 +02:00
fread ( buffer , bufferSize , 1 , fileBuffer ) ;
2022-04-22 18:39:48 +02:00
2022-04-22 18:53:26 +02:00
//CHECK FOR TEXT IN buffer
if ( strcmp ( buffer , " " ) = = 0 )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " You probably aren't connected to internet! This release could be unsafe! \n \n " ) ;
2022-04-22 18:53:26 +02:00
//WAIT FOR 5 SECONDS
sleep ( 5 ) ;
}
2022-04-22 18:39:48 +02:00
//CLEANUP
fclose ( fileBuffer ) ;
//GET
parsedJson = json_tokener_parse ( buffer ) ;
json_object_object_get_ex ( parsedJson , " active " , & active ) ;
if ( strcmp ( VERSION , json_object_get_string ( active ) ) ! = 0 )
{
2022-06-11 19:00:24 +02:00
//UPDATE
2022-07-10 18:26:41 +02:00
if ( getFlags ( ) . update )
2022-06-11 19:00:24 +02:00
{
//CHECK FOR ROOT PERMISSIONS
if ( getuid ( ) ! = 0 )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " You need to be root to update! \t [I DO NOT RECOMMEND USING THIS] \n " ) ;
2022-06-12 16:34:19 +02:00
return UPDATE_FAILED ;
2022-06-11 19:00:24 +02:00
}
//VARIABLES
git_repository * repo = NULL ;
int exitCode ;
char * installCommand ;
int installCode ;
//MESSAGE
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) printf ( " Your WHY2 version is outdated! \n Updating... \t [BETA] \n \n " ) ;
2022-06-11 19:00:24 +02:00
//CHECK IF WHY2 REPO ISN'T ALREADY FOUND IN 'UPDATE_NAME'
if ( access ( UPDATE_NAME , F_OK ) = = 0 )
{
removeDirectory ( UPDATE_NAME ) ;
}
git_libgit2_init ( ) ; //START GIT2
exitCode = git_clone ( & repo , UPDATE_URL , UPDATE_NAME , NULL ) ; //CLONE
git_libgit2_shutdown ( ) ; //STOP GIT2
//CHECK FOR ERRORS
if ( exitCode ! = 0 )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " Updating failed! (cloning) \n " ) ;
2022-06-12 16:34:19 +02:00
return UPDATE_FAILED ;
2022-06-11 19:00:24 +02:00
}
//COUNT installCommand LENGTH & ALLOCATE IT
installCommand = replaceWord ( UPDATE_COMMAND , " {DIR} " , UPDATE_NAME ) ;
installCode = system ( installCommand ) ; //INSTALL
//REMOVE versions.json - OTHERWISE WILL CAUSE SEGFAULT IN NEXT RUN
remove ( VERSIONS_NAME ) ;
2022-06-21 19:14:06 +02:00
free ( installCommand ) ;
2022-06-11 19:00:24 +02:00
//CHECK FOR ERRORS
if ( installCode ! = 0 )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " Updating failed! (installing) \n " ) ;
2022-06-12 16:34:19 +02:00
return UPDATE_FAILED ;
2022-06-11 19:00:24 +02:00
}
goto deallocation ; //GREAT SUCCESS!
}
2022-05-11 18:15:03 +02:00
//COUNT VERSIONS BEHIND
int versionsIndex = - 1 ;
int versionsBuffer = 0 ;
struct json_object * deprecated ;
json_object_object_get_ex ( parsedJson , " deprecated " , & deprecated ) ;
2022-05-21 15:46:19 +02:00
2022-05-11 18:15:03 +02:00
//COUNT versionsIndex
2022-06-19 17:19:03 +02:00
for ( int i = 0 ; i < ( int ) json_object_array_length ( deprecated ) ; i + + )
2022-05-11 18:15:03 +02:00
{
//IT'S A MATCH, BABY :D
if ( strcmp ( json_object_get_string ( json_object_array_get_idx ( deprecated , i ) ) , VERSION ) = = 0 )
{
versionsIndex = i ;
break ;
}
}
//versions.json DOESN'T CONTAIN VERSION (THIS WILL NOT HAPPEN IF YOU WILL NOT EDIT IT)
if ( versionsIndex = = - 1 )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) printf ( " Version %s not found! Check your flags. \n \n " , VERSION ) ;
2022-05-26 19:50:58 +02:00
2022-05-11 18:15:03 +02:00
free ( deprecated ) ;
2022-06-11 19:00:24 +02:00
goto deallocation ;
2022-05-11 18:15:03 +02:00
}
//COUNT versionsBuffer
versionsBuffer = json_object_array_length ( deprecated ) - versionsIndex ;
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " This release could be unsafe! You're %d versions behind! (%s/%s) \n \n " , versionsBuffer , VERSION , json_object_get_string ( active ) ) ;
2022-04-22 18:39:48 +02:00
//WAIT FOR 5 SECONDS
2022-05-11 18:15:03 +02:00
free ( deprecated ) ;
2022-04-22 18:39:48 +02:00
sleep ( 5 ) ;
}
2022-05-11 17:50:01 +02:00
2022-06-11 19:00:24 +02:00
deallocation :
2022-05-11 18:15:03 +02:00
2022-05-11 17:50:01 +02:00
//DEALLOCATION
free ( parsedJson ) ;
free ( active ) ;
2022-05-24 17:51:19 +02:00
free ( buffer ) ;
2022-06-12 16:34:19 +02:00
return SUCCESS ;
2022-04-22 18:39:48 +02:00
}
2022-04-23 18:07:45 +02:00
2022-05-28 19:38:46 +02:00
void generateTextKeyChain ( char * key , int * textKeyChain , int textKeyChainSize )
2022-04-27 18:07:07 +02:00
{
int numberBuffer ;
2022-05-28 19:41:57 +02:00
int numberBuffer2 ;
2022-05-21 15:46:19 +02:00
2022-04-27 18:07:07 +02:00
for ( int i = 0 ; i < textKeyChainSize ; i + + )
{
numberBuffer = i ;
2022-05-03 18:37:28 +02:00
//CHECK, IF numberBuffer ISN'T GREATER THAN keyLength AND CUT UNUSED LENGTH
2022-06-19 17:19:03 +02:00
while ( numberBuffer > = ( int ) getKeyLength ( ) )
2022-04-27 18:07:07 +02:00
{
2022-05-03 18:37:28 +02:00
numberBuffer - = getKeyLength ( ) ;
2022-04-27 18:07:07 +02:00
}
2022-05-28 19:45:26 +02:00
numberBuffer2 = getKeyLength ( ) - ( numberBuffer + ( i < textKeyChainSize ) ) ;
2022-05-28 19:41:57 +02:00
2022-04-27 18:07:07 +02:00
//FILL textKeyChain
if ( ( numberBuffer + 1 ) % 3 = = 0 )
{
2022-07-11 19:12:41 +02:00
textKeyChain [ textKeyChainSize - ( i + 1 ) ] = key [ numberBuffer ] * key [ numberBuffer2 ] ;
2022-06-10 19:48:45 +02:00
}
else if ( ( numberBuffer + 1 ) % 2 = = 0 )
2022-04-27 18:07:07 +02:00
{
2022-07-11 19:12:41 +02:00
textKeyChain [ textKeyChainSize - ( i + 1 ) ] = key [ numberBuffer ] - key [ numberBuffer2 ] ;
2022-06-10 19:48:45 +02:00
}
else
2022-04-27 18:07:07 +02:00
{
2022-07-11 19:12:41 +02:00
textKeyChain [ textKeyChainSize - ( i + 1 ) ] = key [ numberBuffer ] + key [ numberBuffer2 ] ;
2022-04-27 18:07:07 +02:00
}
}
}
2022-05-08 19:54:46 +02:00
void deallocateOutput ( outputFlags flags )
{
free ( flags . outputText ) ;
free ( flags . usedKey ) ;
}
2022-07-10 18:24:44 +02:00
unsigned char checkKey ( char * key )
2022-05-26 18:35:15 +02:00
{
if ( strlen ( key ) < getKeyLength ( ) )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " Key must be at least %lu characters long! \n " , getKeyLength ( ) ) ;
2022-06-12 16:35:43 +02:00
return INVALID_KEY ;
2022-05-26 18:35:15 +02:00
}
2022-06-12 16:36:41 +02:00
return SUCCESS ;
2022-05-26 18:35:15 +02:00
}
2022-07-10 18:24:44 +02:00
unsigned char checkText ( char * text )
2022-05-29 16:49:18 +02:00
{
if ( strcmp ( text , " " ) = = 0 )
{
2022-07-10 18:26:41 +02:00
if ( ! getFlags ( ) . noOutput ) fprintf ( stderr , " No text to encrypt! \n " ) ;
2022-06-12 16:35:43 +02:00
return INVALID_TEXT ;
2022-05-29 16:49:18 +02:00
}
2022-06-12 16:36:41 +02:00
return SUCCESS ;
2022-05-29 16:49:18 +02:00
}
2022-05-29 17:47:50 +02:00
unsigned long countIntLength ( int number )
2022-04-23 18:07:45 +02:00
{
int returning = 1 ;
int buffer = 10 ;
//CHECK FOR NEGATIVE NUMBER
if ( number < 0 )
{
returning + + ;
number * = - 1 ;
}
//COUNT LENGTH
while ( buffer < = number )
{
buffer * = 10 ;
returning + + ;
}
return returning ;
}
2022-05-30 18:01:21 +02:00
unsigned long countUnusedKeySize ( char * text , char * key )
{
unsigned long returning = 0 ;
if ( strlen ( key ) / 2 > strlen ( text ) )
{
returning = strlen ( key ) - 2 * strlen ( text ) ;
}
return returning ;
2022-05-30 19:09:47 +02:00
}
unsigned long compareTimeMicro ( struct timeval startTime , struct timeval finishTime )
{
2022-05-30 19:18:37 +02:00
return ( finishTime . tv_sec - startTime . tv_sec ) * 1000000 + finishTime . tv_usec - startTime . tv_usec ;
2022-05-30 18:01:21 +02:00
}