Advertisement
Guest User

yep

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