Advertisement
Guest User

yep

a guest
May 5th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @mainpage Yep Utility.
  3.  *
  4.  * Software for encryption/decryption of private text data.\n
  5.  * System requirements:
  6.  * - Compiler: cc/gcc
  7.  * - Compile options: -ansi -std=c99
  8.  *
  9.  * Contact: valsorym.e@gmail.com
  10.  * Copyright (c) 2012 valsorym. All rights reserved.
  11.  */
  12.  
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdarg.h>
  18. #include <termios.h>
  19. #include <sys/time.h>
  20. #include <unistd.h>
  21.  
  22. /* DEFINES */
  23. /*---------------------------------------------------------------------------*/
  24. /**
  25.  * @def ARGV_HELP
  26.  * @brief
  27.  * The list of arguments to display help information.
  28.  *
  29.  * @def ARGV_NEWPASS
  30.  * @brief
  31.  * The list of arguments to change the password on the specified file.
  32.  *
  33.  * @def ARGV_TEMPDIR
  34.  * @brief
  35.  * The list of arguments to specify a directory for storing temporary files.
  36.  *
  37.  * @def ARGV_HOOKWORD
  38.  * @brief
  39.  * The list of arguments to specify the protection word.
  40.  */
  41. #define ARGV_HELP     2, "-i", "-h"
  42. #define ARGV_NEWPASS  1, "-p"
  43. #define ARGV_TEMPDIR  1, "-d"
  44. #define ARGV_HOOKWORD 1, "-w"
  45.  
  46. /**
  47.  * @def DEFAULT_HOOKWORD
  48.  * @brief
  49.  * Protection word by default. Use if not specified arguments
  50.  * are \a ARGV_HOOKWORD.
  51.  *
  52.  * @def DEFAULT_TEMPDIR
  53.  * @brief
  54.  * Directory for storing temporary files by default. Use if not specified
  55.  * arguments are \a ARGV_TEMPDIR.
  56.  */
  57. #define DEFAULT_HOOKWORD  "howdy"
  58. #define DEFAULT_TEMPDIR   "/tmp/yep"
  59.  
  60. /**
  61.  * @def MAXLEN_BUF
  62.  * @brief
  63.  * The maximum length of text buffer. The buffer used to read lines
  64.  * from a file, encode/decode strings.
  65.  *
  66.  * @def MAXLEN_PATH
  67.  * @brief
  68.  * The maximum permissible length of the path to the file  including the
  69.  * file name.
  70.  *
  71.  * @def MAXLEN_SYSKEY
  72.  * @brief
  73.  * Maximum length of keywords: passwords, hookword.
  74.  *
  75.  * @def MAXLEN_TEMPNAME
  76.  * @brief
  77.  * The maximum permissible length of the temporary file name.
  78.  */
  79. #define MAXLEN_BUF      4096
  80. #define MAXLEN_PATH     255
  81. #define MAXLEN_SYSKEY   15
  82. #define MAXLEN_TEMPNAME 10
  83.  
  84. /* NEWTYPES */
  85. /*---------------------------------------------------------------------------*/
  86. /**
  87.  * @enum bool
  88.  * @brief
  89.  * Boolean data type. etc. \e c99 _Bool.
  90.  */
  91. typedef enum {false=0, true} bool;
  92.  
  93. /**
  94.  * @enum cmethod
  95.  * @brief
  96.  * Direction of encryption: encrypt, decrypt.
  97.  */
  98. typedef enum {
  99.     encode=1,   /**< Encrypt a file. */
  100.     decode=-1   /**< Decrypt a file. */
  101. } cmethod;
  102.  
  103. /**
  104.  * @enum cstatus
  105.  * @brief
  106.  * The status of the encrypting/decrypting the file.
  107.  */
  108. typedef enum {
  109.     no_err=0,     /**< Encryption/decryption is successful. */
  110.     err_hookword, /**< Decrypted file can not be obtained because of
  111.                    * not correct password/hookword. */
  112.     o_err_read,   /**< Can not open for reading the original file. */
  113.     t_err_read,   /**< Can not open for reading the temp file. */
  114.     o_err_write,  /**< Can not open for writing the original file. */
  115.     t_err_write,  /**< Can not open for writing the temp file. */
  116.     o_err_save,   /**< Can not save the original file.
  117.                    * Error function fclose(). */
  118.     t_err_save,   /**< Can not save the temp file.
  119.                    * Error function fclose(). */
  120.     o_err_create, /**< Can not create the original file.
  121.                    * Error function createyep(). */
  122.     t_err_create  /**< Can not create the temp file.
  123.                    * Error function createyep(). */
  124. } cstatus;
  125.  
  126. /**
  127.  * @enum maintype
  128.  * @brief
  129.  * The way to open the file.
  130.  */
  131. typedef enum {
  132.     editfile,   /**< The file is open to change/read. */
  133.     changepass  /**< The file is opened to change the password. */
  134. } maintype;
  135.  
  136. /* SYSTEM FUNCTIONS */
  137. /*---------------------------------------------------------------------------*/
  138. extern bool tsarg(const char *, int , ...);
  139. extern void errexit(const char *, ...);
  140. extern char *geths(char [], int );
  141. extern int intrand(int );
  142. extern char *strrand(char [], int );
  143. extern char csymbol(const char , int );
  144. extern bool cstr(char [], int , const char *, const char *, cmethod );
  145. extern bool createyep(const char *, const char *, const char *);
  146. extern cstatus decodingfile(const char *, const char *,
  147.     const char *, const char *);
  148. extern cstatus encodingfile(const char *, const char *,
  149.     const char *, const char *);
  150. extern int terminal(const char *, ...);
  151.  
  152. /* SCREEN FUNCTIONS */
  153. /*---------------------------------------------------------------------------*/
  154. extern void s_help(void);
  155. extern void s_main(const char *, const char *, const char *,
  156.     const char *, maintype );
  157.  
  158. /* VIRTUAL SCREEN FUNCTIONS */
  159. extern void vs_newpass(char [], char [], const char *);
  160.  
  161. /* MAIN */
  162. /*---------------------------------------------------------------------------*/
  163. /**
  164.  * @brief
  165.  * Yep main function.
  166.  *
  167.  * @param argc
  168.  * Count of arguments passed.
  169.  * @param argv
  170.  * The list of arguments.
  171.  *
  172.  * @return
  173.  * Returns zero on success of the program.
  174.  */ //*
  175. int
  176. main(int argc, char **argv) {
  177. // "editor" - text editor software.
  178. // "tempdir" - store temporary files.
  179. // "hookword" - indicator for file recovery.
  180.     char *editor=NULL,
  181.          *tempdir=NULL,
  182.          *hookword=NULL;
  183.  
  184. // The program requires access to the UNIX shell.
  185.     if (!system(NULL))
  186.         errexit("The UNIX shell is not available.\n");
  187.    
  188. // Must be at least one external argument.
  189.     if (argc < 2)
  190.         errexit("See the help page on the program.\n");
  191.    
  192. // Read the arguments list.
  193.     for (int i=1; i < argc; ++i)
  194.         if (tsarg(argv[i], ARGV_HELP))
  195.         // Show help screen.
  196.             s_help();
  197.         else if (tsarg(argv[i], ARGV_HOOKWORD)) {
  198.         // Read hookword.
  199.             if (++i < argc)
  200.                 hookword = argv[i];
  201.             else
  202.                 errexit("Hookword parameter is not specified.\n");
  203.         } else if (tsarg(argv[i], ARGV_TEMPDIR)) {
  204.         // Read tempdir.
  205.             if (++i < argc)
  206.                 tempdir = argv[i];
  207.             else
  208.                 errexit("Tempdir parameter is not specified.\n");
  209.         } else if (tsarg(argv[i], ARGV_NEWPASS)) {
  210.         // Set new password on file.
  211.             if (++i < argc) {
  212.                 hookword = (hookword ? (char*)hookword
  213.                     : (char*)DEFAULT_HOOKWORD);
  214.                 tempdir = (tempdir ? (char*)tempdir
  215.                     : (char*)DEFAULT_TEMPDIR);
  216.  
  217.                 s_main(hookword, editor, argv[i], tempdir, changepass);
  218.             } else
  219.                 errexit("Don't specify a file for change the password.\n");
  220.         } else {
  221.         // Get the editor and a filename.
  222.             editor = argv[i];
  223.             if (++i < argc) {
  224.                 if (i+1 < argc)
  225.                     errexit("Too many arguments.\n");
  226.  
  227.                 hookword = (hookword ? (char*)hookword
  228.                     : (char*)DEFAULT_HOOKWORD);
  229.                 tempdir = (tempdir ? (char*)tempdir
  230.                     : (char*)DEFAULT_TEMPDIR);
  231.  
  232.                 s_main(hookword, editor, argv[i], tempdir, editfile);
  233.             } else
  234.                 errexit("The file is not specified.\n");
  235.         } // end if ().
  236.  
  237.     return 0;
  238. } // end main(). */
  239.  
  240. /* SYSTEM FUNCTIONS */
  241. /*---------------------------------------------------------------------------*/
  242. /**
  243.  * @brief
  244.  * To determine whether an \a arg list of available string arguments \a (...).
  245.  *
  246.  * @param arg
  247.  * Argument (string) for comparison.
  248.  * @param count
  249.  * Number of arguments in the list of comparisons.
  250.  * @param ...
  251.  * List of comparisons.
  252.  *
  253.  * @return
  254.  * - true - If the argument \a arg corresponds to at least one argument
  255.  * from the argument list.
  256.  * - false - If the argument \a arg does not correspond to any argument
  257.  * from the argument list.
  258.  */ //*
  259. bool
  260. tsarg(const char *arg, int count, ...)
  261. {
  262.     va_list ptr;
  263.     char *str=NULL;
  264.     bool result=false;
  265.  
  266. // The argument list is empty.
  267.     if (count < 1)
  268.         return result;
  269.    
  270. // Find the argument.
  271.     va_start(ptr, count);
  272.    
  273.     while ((str=va_arg(ptr, char *))
  274.         && !result && count-- > 0)
  275.         if (!strcmp(arg, str))
  276.             result = true;
  277.  
  278.     va_end(ptr);
  279.    
  280.     return result;
  281. } // end tsarg(). */
  282.  
  283. /**
  284.  * @brief
  285.  * Exit on error.
  286.  *
  287.  * @detailed
  288.  * This function takes cover those arguments as printf() function from the
  289.  * C standard library.
  290.  *
  291.  * @param format
  292.  * Error message.
  293.  * @param ...
  294.  * The data formatting.
  295.  *
  296.  * @return
  297.  * No data is returned.
  298.  */ //*
  299. void
  300. errexit(const char *format, ...)
  301. {
  302.     va_list ptr;
  303.     char buf[MAXLEN_BUF];
  304.  
  305. // Format the error message.
  306.     va_start(ptr, format);
  307.     sprintf(buf, "*** Error: %s.", format);
  308.     vfprintf(stderr, buf, ptr);
  309.     va_end(ptr);
  310.    
  311.     exit(1);
  312. } // end errexit(). */
  313.  
  314. /**
  315.  * @brief
  316.  * Read from standard input hidden line.
  317.  *
  318.  * @param buf
  319.  * The buffer where written to a string from stdin.
  320.  * @param bufsize
  321.  * The size of the buffer.
  322.  *
  323.  * @return
  324.  * Returns a pointer to the line of input that is placed in the buffer \a buf.
  325.  */ //*
  326. char
  327. *geths(char buf[], int bufsize)
  328. {
  329.     struct termios term, oldterm;
  330.     int i=0, max=bufsize-1, ch;
  331.        
  332. // Do not display the characters as you type.
  333.     tcgetattr(0, &term);
  334.     oldterm = term;
  335.     term.c_lflag &= ~ECHO;          // Does not display the characters.
  336.     term.c_lflag &= ~ICANON;        // Do not expect the press enter.
  337.     tcsetattr(0, TCSANOW, &term);
  338.  
  339. // Read the hidden characters.
  340.     while ((ch=getchar()) != EOF && ch != '\n')
  341.         if (ch == 127 || ch == 8 || ch == '\b') {
  342.             if (i > 0) i--;
  343.         } else if (i < max)
  344.             buf[i++] = ch;
  345.  
  346.     buf[i++] = '\0';
  347.    
  348. // Restore the old display options.
  349.     term = oldterm;
  350.     tcsetattr(0, TCSANOW, &term);
  351.  
  352.     return buf;
  353. } // end geths(). */
  354.  
  355. /**
  356.  * @brief
  357.  * Generate random number.
  358.  *
  359.  * @param max
  360.  * The right border of the generated numbers.
  361.  *
  362.  * @return
  363.  * The function returns a random number between \a 0 and \a max. When
  364.  * the function returns an error number \a -1.
  365.  *
  366.  * @see
  367.  * strrand().
  368.  */ //*
  369. int
  370. intrand(int max)
  371. {
  372.     struct timeval tp;
  373.     struct timezone tzp;
  374.     unsigned stime;
  375.    
  376. // There is an extreme right edge.
  377.     max = abs(max);
  378.     if (max >= 32767)
  379.         return -1;
  380.  
  381. // Random number depends on the microseconds.
  382.     if (gettimeofday(&tp, &tzp)==-1)
  383.         return -1;
  384.    
  385.     stime = (unsigned)tp.tv_usec;
  386.     srand(stime);
  387.    
  388. // Return a random number in the range 0 ... max.
  389.  
  390.     return abs(rand()%max);
  391. } // end intrand(). */
  392.  
  393. /**
  394.  * @brief
  395.  * Create a random string.
  396.  *
  397.  * @param buf
  398.  * A random sequence of characters will be written into this buffer.
  399.  * @param bufsize
  400.  * The size of the buffer.
  401.  *
  402.  * @return
  403.  * Returns a pointer to a random string that was placed in the buffer \a buf.
  404.  *
  405.  * @see
  406.  * intrand().
  407.  */ //*
  408. char
  409. *strrand(char buf[], int bufsize)
  410. {
  411.     char *alphabet="QWERTYUIOPASDFGHJKLZXCVBNM"
  412.         "qwertyuiopasdfghjklzxcvbnm"
  413.         "1234567890";
  414.     int alphsize=strlen(alphabet);
  415.  
  416. // The minimum buffer size - 1 bytes.
  417.     if (bufsize <= 0)
  418.         return NULL;
  419.  
  420. // Generate random string.
  421.     for (int i=0; i<bufsize; ++i) {
  422.         int rn = intrand(alphsize);
  423.         if (rn >= 0 && rn < alphsize)
  424.             buf[i] = *(alphabet+rn);
  425.         else
  426.             if (i > 0)
  427.                 --i;
  428.     } // end for ().
  429.  
  430.     buf[bufsize-1] = '\0';
  431.    
  432.     return buf;
  433. } // end strrand(). */
  434.  
  435. /**
  436.  * @brief
  437.  * Encode a character offset.
  438.  *
  439.  * @param ch
  440.  * The symbol to encode.
  441.  * @param seek
  442.  * The shift to encode.
  443.  *
  444.  * @return
  445.  * Returns the encoded (If  \a seek variable  > = 0)/decoded (If \a seek
  446.  * variable <0) symbol. If symbol failed to encode/decode, returns the
  447.  * character \a ch variable .
  448.  *        
  449.  * @see
  450.  * cstr().
  451.  */ //*
  452. char
  453. csymbol(const char ch, int seek)
  454. {
  455.     char result=ch;
  456.     char *alphabet="QWERTYUIOPASDFGHJKLZXCVBNM"
  457.         "qwertyuiopasdfghjklzxcvbnm"
  458.         "1234567890 !@#$%^&*()_-+{}:";
  459.     int alphsize=strlen(alphabet);
  460.    
  461. // Determine the symbol.
  462.     for (int i=0; i < alphsize; ++i)
  463.         if (ch == *(alphabet+i)) {
  464.             seek += i;
  465.             if (seek >= alphsize)
  466.                 while (seek >= alphsize)
  467.                     seek -= alphsize;
  468.             else if (seek < 0)
  469.                 while (seek < 0)
  470.                     seek += alphsize;
  471.  
  472.             if (seek >= 0 && seek < alphsize)
  473.                 result = *(alphabet+seek);
  474.                
  475.             break;
  476.         } // end if ().
  477.  
  478.     return result;
  479. } // end csymbol(). */
  480.  
  481. /**
  482.  * @brief
  483.  * Cryptography string.
  484.  *
  485.  * @param buf
  486.  * Encoded string is placed in the buffer.
  487.  * @param bufsize
  488.  * The size of the buffer.
  489.  * @param str
  490.  * The string to encode.
  491.  * @param pass
  492.  * Password to encrypt the string.
  493.  * @param method
  494.  * Method: encode/decode.
  495.  *
  496.  * @return
  497.  * - true - If the string \a str has been successfully encoded/decoded
  498.  *   and placed in buffer \a buf.
  499.  * - false - If you encode/decode the string is impossible.
  500.  *
  501.  * @see
  502.  * csymbol().
  503.  */ //*
  504. bool
  505. cstr(char buf[], int bufsize, const char *str, const char *pass,
  506.     cmethod method)
  507. {
  508.     int len=strlen(pass), seek=0;
  509.     bool result=true;
  510.    
  511. // Determine the offset.
  512.     for (int i=0; i < len; ++i)
  513.         seek = seek + ((int)(char)*(pass+i)) * i;
  514.    
  515. // Can I put the original in the buffer?
  516. // Determine the count of processed symbols.
  517.     len=strlen(str);
  518.     if (len > bufsize-1)
  519.         result = false;
  520.    
  521. // encode || decode str.
  522.     if (result) {
  523.         for (int i=0; i < len; ++i)
  524.             buf[i] = csymbol((char)*(str+i), (seek+i)*method);
  525.         buf[len] = '\0';
  526.     } else
  527.         buf[0] = '\0';
  528.  
  529.     return result;
  530. } // end cstr(). */
  531.  
  532. /**
  533.  * @brief
  534.  * Creates an empty yep-file recording it encoded a keyword (hookword).
  535.  *
  536.  * @param hookword
  537.  * Word of the key.
  538.  * @param pass
  539.  * Password.
  540.  * @param file
  541.  * The file to be created.
  542.  *
  543.  * @return
  544.  * - true - If the file is successfully created.
  545.  * - false - An error occurred while creating the file.
  546.  */ //*
  547. bool
  548. createyep(const char *hookword, const char *pass,
  549.     const char *file)
  550. {
  551.     FILE *f_file;
  552.     char buf[MAXLEN_BUF];
  553.    
  554.     if (!(f_file=fopen(file, "w")))
  555.         return false;
  556.  
  557.     cstr(buf, MAXLEN_BUF, hookword, pass, encode);
  558.     strncat(buf, "\n", MAXLEN_BUF);
  559.     fputs(buf, f_file);
  560.    
  561.     if(fclose(f_file))
  562.         return false;
  563.  
  564.     return true;
  565. } // end createyep(). */
  566.  
  567. /**
  568.  * @brief
  569.  * Вуcode file.
  570.  *
  571.  * @param hookword
  572.  * Word of the key.
  573.  * @param pass
  574.  * Password.
  575.  * @param file
  576.  * File for decoding.
  577.  * @param tempfile
  578.  * The temporary file.
  579.  *
  580.  * @return
  581.  * - no_err - If the operation is executed without error.
  582.  * - o_err_read - Can not open file \a file for reading.
  583.  * - t_err_write - Can not open file \a tempfile for writing.
  584.  * - err_hookword - Wrong password or secret word.
  585.  * - o_err_save - Unable to save file \a file.
  586.  * - t_err_save - Unable to save file \a tempfile.
  587.  *
  588.  * @see
  589.  * encodingfile().
  590.  */ //*
  591. cstatus
  592. decodingfile(const char *hookword, const char *pass, const char *file,
  593.     const char *tempfile)
  594. {
  595.     FILE *f_file, *f_tempfile;
  596.     char buf[MAXLEN_BUF], str[MAXLEN_BUF];
  597.     bool hw_tested=false, result=no_err;
  598.    
  599.     if (!(f_file=fopen(file, "r")))
  600.         return o_err_read;
  601.        
  602.     if (!(f_tempfile=fopen(tempfile, "w")))
  603.         return t_err_write;
  604.  
  605. // Encoding file.
  606.     while(!feof(f_file)) {
  607.         if (!hw_tested) {
  608.         // Read hookword.
  609.             hw_tested=true;
  610.            
  611.             if(fgets(buf, MAXLEN_BUF, f_file)) {
  612.                 buf[strlen(buf)-1] = '\0'; // Remove "\n" symbol.
  613.                 cstr(str, MAXLEN_BUF, buf, pass, decode);
  614.                
  615.                 if (strcmp(str, hookword)) {
  616.                     result = err_hookword;
  617.                     break;
  618.                 } // end if ().
  619.             } else {
  620.                 result = err_hookword;
  621.                 break;
  622.             }
  623.         } else {
  624.         // Read body.
  625.             buf[0] = '\0'; str[0] = '\0';
  626.             if(fgets(buf, MAXLEN_BUF, f_file)) {
  627.                 cstr(str, MAXLEN_BUF, buf, pass, decode);
  628.                 fputs(str, f_tempfile);
  629.             } // end if ().
  630.         } // end if ().
  631.     } // end while ().
  632.    
  633.     if(fclose(f_file))
  634.         return o_err_save;
  635.        
  636.     if(fclose(f_tempfile))
  637.         return t_err_save;
  638.  
  639.     return result;
  640. } // end decodingfile(). */
  641.  
  642. /**
  643.  * @brief
  644.  * Encode file.
  645.  *
  646.  * @param hookword
  647.  * Word of the key.
  648.  * @param pass
  649.  * Password.
  650.  * @param file
  651.  * File for decoding.
  652.  * @param tempfile
  653.  * The temporary file.
  654.  *
  655.  * @return
  656.  * - no_err - If the operation is executed without error.
  657.  * - o_err_create - If the function createyep() caused the error,
  658.  *   etc. can not create file \a file.
  659.  * - o_err_write - Can not open file for writing \a file.
  660.  * - t_err_read - Can not open file for reading \a tempfile.
  661.  * - o_err_save - Unable to close file \a file.
  662.  * - t_err_save - Unable to close file \a tempfile.
  663.  *
  664.  * @see
  665.  * decodingfile().
  666.  */ //*
  667. cstatus
  668. encodingfile(const char *hookword, const char *pass, const char *file,
  669.     const char *tempfile)
  670. {
  671.     FILE *f_file, *f_tempfile;
  672.     char buf[MAXLEN_BUF], str[MAXLEN_BUF];
  673.     bool result=no_err;
  674.  
  675.     if (!createyep(hookword, pass, file))
  676.         return o_err_create;
  677.    
  678.     if (!(f_file=fopen(file, "a")))
  679.         return o_err_write;
  680.        
  681.     if (!(f_tempfile=fopen(tempfile, "r")))
  682.         return t_err_read;
  683.  
  684. // Decoding file.
  685.     while(!feof(f_tempfile)) {
  686.     // Write body.
  687.         buf[0] = '\0'; str[0] = '\0';
  688.         if(fgets(buf, MAXLEN_BUF, f_tempfile)) {
  689.             cstr(str, MAXLEN_BUF, buf, pass, encode);
  690.             fputs(str, f_file);
  691.         }
  692.     } // end while ().
  693.    
  694.     if(fclose(f_file))
  695.         return o_err_save;
  696.        
  697.     if(fclose(f_tempfile))
  698.         return t_err_save;
  699.  
  700.     return result;
  701. } // end encodingfile(). */
  702.  
  703. /**
  704.  * @brief
  705.  * Executing shell commands.
  706.  *
  707.  * @detailed
  708.  * This function takes cover those arguments as printf() function from the
  709.  * C standard library.
  710.  *
  711.  * @param format
  712.  * Command for the terminal.
  713.  * @param ...
  714.  * Formatting options.
  715.  *
  716.  * @return
  717.  * Returns integer - the result of a shell command.
  718.  */ //*
  719. int
  720. terminal(const char *format, ...)
  721. {
  722.     va_list ptr;
  723.     char buf[MAXLEN_BUF];
  724.  
  725.     va_start(ptr, format);
  726.     vsprintf(buf, format, ptr);
  727.     va_end(ptr);
  728.  
  729.     return system(buf);
  730. } // end terminal(). */
  731.  
  732. /* SCREEN FUNCTIONS */
  733. /*---------------------------------------------------------------------------*/
  734. /**
  735.  * @brief
  736.  * Show help page.
  737.  *
  738.  * @return
  739.  * No data is returned.
  740.  */ //*
  741. void
  742. s_help(void) {
  743. /**  */
  744.     printf("\nNAME\n"
  745.         "\tyep - encrypted text files.\n\n"
  746.         "SYNOPSIS\n"
  747.         "\tyep [-w hookword] [-d tempdir] editor file\n"
  748.         "\tyep [-h | -i]\n"
  749.         "\tyep [-w hookword] [-d tempdir] -p file\n\n"
  750.         "DESCRIPTION\n"
  751.         "\tyep program allows you to encrypt text files using the "
  752.         "public key.\n\n"
  753.         "OPTIONS\n"
  754.         "\t-w hookword\n"
  755.         "\t\tEstablishes additional protection for the word file. This \n"
  756.         "\t\tword should be that you could safely recover an encrypted \n"
  757.         "\t\tfile. If the word is not specified - the word will be used \n"
  758.         "\t\tby default.\n"
  759.         "\t-p file\n"
  760.         "\t\tThe flag allows you to change the previously set password,\n"
  761.         "\t\tand the protection word (hookword).\n"
  762.         "\t-d tempdir\n"
  763.         "\t\tSets the directory for storing temporary files.\n"
  764.         "\t-h, -i\n"
  765.         "\t\tDisplay help information about the program.\n"
  766.         "\teditor\n"
  767.         "\t\tSpecify a text editor synchronous: vi, vim, ee ... Use \n"
  768.         "\t\tasynchronous editors (geany &, emacs & ...) - is impossible.\n"
  769.         "\tfile\n"
  770.         "\t\tThe file to be encrypted / decrypted. If the file does not \n"
  771.         "\t\texist - it is created.\n\n"
  772.         "BUGS\n"
  773.         "\tEmail  bug  reports  to  iam@valsorym.com.  Be sure to include \n"
  774.         "\tthe word \"yep bugs\" somewhere in the \"Subject:\" field.\n");
  775.            
  776.     exit(0);
  777. } // end help(). */
  778.  
  779. /**
  780.  * @brief
  781.  * The main function of encoding, decoding, and change the password on file.
  782.  *
  783.  * @param hookword
  784.  * Secret word.
  785.  * @param editor
  786.  * Synchronous text editor.
  787.  * @param file
  788.  * File manipulation.
  789.  * @param tempdir
  790.  * The directory for storing temporary files.
  791.  * @param type
  792.  * The method of handling file: encoding/decoding or change your password.
  793.  *
  794.  * @return
  795.  * No data is returned.
  796.  */ //*
  797. void
  798. s_main(const char *hookword, const char *editor, const char *file,
  799.     const char *tempdir, maintype type)
  800. {
  801.     char newhookword[MAXLEN_SYSKEY],
  802.          passagain[MAXLEN_SYSKEY],
  803.          pass[MAXLEN_SYSKEY];
  804.     bool existfile=false,
  805.          existeditor=false;
  806.     char tempname[MAXLEN_TEMPNAME],
  807.          tempfile[MAXLEN_PATH];
  808.     int  len=0;
  809.  
  810. // Create a random filename for a temporary file.
  811.     do {
  812.         if (!strrand(tempname, MAXLEN_TEMPNAME))
  813.             errexit("Unable to create a temporary name.\n");
  814.         len = strlen(tempname);
  815.     } while (len < MAXLEN_TEMPNAME/2);
  816.  
  817.     if (strlen(tempdir) + MAXLEN_TEMPNAME > MAXLEN_PATH)
  818.         errexit("A very long name for a temporary file.\n");
  819.  
  820.     sprintf(tempfile, "%s/%s", tempdir, tempname);
  821.    
  822. // Create a YEP temporary directory.
  823.     if (terminal("ls -l -d %s | grep ^d", tempdir))
  824.         if (terminal("mkdir -p %s", tempdir))
  825.             errexit("Can not create a directory for storing "
  826.                 "temporary files.\n");
  827.  
  828. // Check for the file.
  829.     if (!(existfile=!terminal("ls -l -d %s | grep ^-", file)))
  830.         if (!terminal("ls -l -d %s | grep ^d", file)) {
  831.         // There exists a directory with that name: filename.
  832.             terminal("clear");
  833.             errexit("There exists a directory with that name: %s.\n", file);
  834.         }
  835.    
  836. // Checking the editor.
  837.     existeditor = !terminal("which %s", editor);
  838.     terminal("clear");
  839.    
  840. // Create interface.
  841.     printf("This will open the file with the following parameters:\n");
  842.     printf("    Filename%s:\t%s\n", (existfile ? (char*)"" :
  843.         (char*)" (file not exist)"), file);
  844.    
  845.     if (type == editfile)
  846.         printf("    Editor%s:\t%s\n", (existeditor ? (char*)"" :
  847.             (char*)" (editor is not installed)"), editor);
  848.    
  849.     printf("    Hookword%s:\t%s\n\n", (!strcmp(DEFAULT_HOOKWORD, hookword) ?
  850.         (char*)" (default word protection)" : (char*)""), hookword);
  851.  
  852.     if (type == editfile) {
  853.     // Just open file.
  854.         if (!existeditor)
  855.             errexit("Editor is not installed: %s.\n", editor);
  856.     } else if (type == changepass){
  857.     // Change password.
  858.         if (!existfile)
  859.             errexit("Сan not change the password on this file: %s.\n", file);
  860.     } else
  861.     // Bad type parameter.
  862.         errexit("Is not valid action.\n");
  863.     // end if ().
  864.  
  865. // Read the password.
  866.     printf("\nTo exit, press Ctrl+C.\n");
  867.     while (true) {
  868.         printf("\nPassword: ");
  869.         geths(pass, MAXLEN_SYSKEY);
  870.        
  871.         if (!existfile) {
  872.             printf("\nEnter password again: ");
  873.             geths(passagain, MAXLEN_SYSKEY);
  874.            
  875.             if (strcmp(pass, passagain))
  876.                 printf("\nPasswords do not match. Please, enter again.\n");
  877.             else {
  878.             // Need create file.
  879.                 if (!createyep(hookword, pass, file))
  880.                     errexit("The file can not be created: %s.\n", file);
  881.                 break;
  882.             }
  883.         } else
  884.             break;
  885.         // end if ().
  886.     } // end while ().
  887.     printf("\n");
  888.    
  889. // Decoding.
  890.     switch ((int)decodingfile(hookword, pass, file, tempfile)) {
  891.         case (int)o_err_read:
  892.             errexit("Can not open file: %s.\n", file);
  893.             break;
  894.         case (int)t_err_write:
  895.             errexit("Unable to open temporary file for writing: %s.\n",
  896.                 tempfile);
  897.             break;
  898.         case (int)err_hookword:
  899.             errexit("Hookword was not recognized.\n");
  900.             break;
  901.         case (int)o_err_save:
  902.             errexit("Unable to close file: %s.\n", file);
  903.             break;
  904.         case (int)t_err_save:
  905.             errexit("Unable to close file: %s.\n", tempfile);
  906.             break;
  907.     } // end switch ().
  908.  
  909.     if (type == editfile) {
  910.     // Open editor.
  911.         if (terminal("%s %s", editor, tempfile))
  912.             errexit("Can not open file: %s.\n", tempfile);
  913.     } else if (type == changepass){
  914.     // Change password.
  915.         sprintf(newhookword, "%s", hookword);
  916.         vs_newpass(newhookword, pass, file);
  917.         hookword = newhookword;
  918.     } // end if ().
  919.  
  920. // Encoding again.
  921.     switch ((int)encodingfile(hookword, pass, file, tempfile)) {
  922.         case (int)o_err_create:
  923.             errexit("Can not create file: %s.\n", file);
  924.             break;
  925.         case (int)o_err_write:
  926.             errexit("Unable to write data to file: %s.\n", file);
  927.             break;
  928.         case (int)t_err_read:
  929.             errexit("Unable to read data from the temporary file: %s.\n",
  930.                 tempfile);
  931.             break;
  932.         case (int)o_err_save:
  933.             errexit("Can not save the file: %s.\n", file);
  934.             break;
  935.         case (int)t_err_save:
  936.             errexit("Can not save the file: %s.\n", tempfile);
  937.             break;
  938.     } // end switch ().
  939.  
  940. // Remove temp file.
  941.     if (terminal("rm -f %s", tempfile))
  942.         printf("\nThe temporary file was not deleted: %s.\n", tempfile);
  943.  
  944.     exit(0);
  945. } // end s_main(). */
  946.  
  947. /**
  948.  * @brief
  949.  * Function-interface to change password.
  950.  *
  951.  * @param hookword
  952.  * Buffer storage keyword.
  953.  * @param pass
  954.  * Buffer storage password.
  955.  * @param file
  956.  * The file to change the password.
  957.  *
  958.  * @return
  959.  * No data is returned.
  960.  */ //*
  961. void
  962. vs_newpass(char hookword[], char pass[], const char *file)
  963. {
  964.     char cmd,
  965.          passagain[MAXLEN_SYSKEY];
  966.    
  967. // Go-go-go.    
  968.     while (true) {
  969.         terminal("clear");
  970.         printf("You are trying to change the system "
  971.             "data to a file: %s.\n", file);
  972.         printf("Choose one of the actions:\n");
  973.         printf("    1. Change password (*******).\n");
  974.         printf("    2. Change hookword (%s).\n", hookword);
  975.         printf("    3. Save and exit.");
  976.         printf("\nTo exit, press Ctrl+C.\n");
  977.  
  978.         printf("\nYour choice: ");
  979.         scanf("%c", &cmd);
  980.         while (getchar() != '\n'); // Clear stdin buffer. (Bad method. :( )
  981.        
  982.         if (cmd == '1') {
  983.         // Change password.
  984.             while (true) {
  985.                 printf("\nEnter new password: ");
  986.                 geths(pass, MAXLEN_SYSKEY);
  987.                
  988.                 printf("\nEnter password again: ");
  989.                 geths(passagain, MAXLEN_SYSKEY);
  990.            
  991.                 if (strcmp(pass, passagain))
  992.                     printf("\nPasswords do not match. Please, enter again.\n");
  993.                 else
  994.                     break;
  995.             } // end while ().
  996.         } else if (cmd == '2') {
  997.         // Change hookword.
  998.             char hwformat[15];
  999.             sprintf(hwformat, "%%%ds", MAXLEN_SYSKEY-1);
  1000.             printf("\nEnter new hookword: ");
  1001.             scanf(hwformat, hookword);
  1002.             while (getchar() != '\n'); // Clear stdin buffer. (Bad method. :( )
  1003.  
  1004.         } else if (cmd == '3') {
  1005.         // Save and exit;
  1006.             terminal("clear");
  1007.             printf("\nThe changes take effect.\n");
  1008.             return;
  1009.         } // end if ().        
  1010.     } // end while ().
  1011.  
  1012.     exit(0);
  1013. } // end vs_newpass(). */
  1014.  
  1015. /* The End. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement