home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / zipcloak.c < prev    next >
C/C++ Source or Header  |  1997-10-15  |  12KB  |  395 lines

  1. /*
  2.    This code is not copyrighted and is put in the public domain. It
  3.    was originally written in Europe and can be freely distributed from
  4.    any country except the U.S.A. If this code is imported into the U.S.A,
  5.    it cannot be re-exported from the U.S.A to another country. (This
  6.    restriction might seem curious but this is what US law requires.)
  7.  */
  8.  
  9. #ifndef UTIL
  10. #define UTIL
  11. #endif
  12. #include "zip.h"
  13. #include "revision.h"
  14. #include "crypt.h"
  15. #include "ttyio.h"
  16. #include <signal.h>
  17. #ifndef NO_STDLIB_H
  18. #  include <stdlib.h>
  19. #endif
  20.  
  21.  
  22. #if CRYPT       /* defined (as TRUE or FALSE) in crypt.h */
  23.  
  24. int main OF((int argc, char **argv));
  25.  
  26. local void handler OF((int sig));
  27. local void license OF((void));
  28. local void help OF((void));
  29. local void version_info OF((void));
  30.  
  31. /* Temporary zip file name and file pointer */
  32. local char *tempzip;
  33. local FILE *tempzf;
  34.  
  35. /* Pointer to CRC-32 table (used for decryption/encryption) */
  36. ulg near *crc_32_tab;
  37.  
  38. /***********************************************************************
  39.  * Issue a message for the error, clean up files and memory, and exit.
  40.  */
  41. void ziperr(code, msg)
  42.     int code;               /* error code from the ZE_ class */
  43.     char *msg;              /* message about how it happened */
  44. {
  45.     if (PERR(code)) perror("zipcloak error");
  46.     fprintf(stderr, "zipcloak error: %s (%s)\n", errors[code-1], msg);
  47.     if (tempzf != NULL) fclose(tempzf);
  48.     if (tempzip != NULL) {
  49.         destroy(tempzip);
  50.         free((zvoid *)tempzip);
  51.     }
  52.     if (zipfile != NULL) free((zvoid *)zipfile);
  53.     EXIT(code);
  54. }
  55.  
  56. /***********************************************************************
  57.  * Print a warning message to stderr and return.
  58.  */
  59. void zipwarn(msg1, msg2)
  60.     char *msg1, *msg2;        /* message strings juxtaposed in output */
  61. {
  62.     fprintf(stderr, "zipcloak warning: %s%s\n", msg1, msg2);
  63. }
  64.  
  65.  
  66. /***********************************************************************
  67.  * Upon getting a user interrupt, turn echo back on for tty and abort
  68.  * cleanly using ziperr().
  69.  */
  70. local void handler(sig)
  71.     int sig;                  /* signal number (ignored) */
  72. {
  73. #if (!defined(MSDOS) && !defined(__human68k__) && !defined(RISCOS))
  74.     echon();
  75.     putc('\n', stderr);
  76. #endif
  77.     ziperr(ZE_ABORT +sig-sig, "aborting");
  78.     /* dummy usage of sig to avoid compiler warnings */
  79. }
  80.  
  81.  
  82. static ZCONST char *public[] = {
  83. "The encryption code of this program is not copyrighted and is put in the",
  84. "public domain. It was originally written in Europe and can be freely",
  85. "distributed from any country except the U.S.A. If this program is imported",
  86. "into the U.S.A, it cannot be re-exported from the U.S.A to another country.",
  87. "The copyright notice of the zip program applies to the rest of the code."
  88. };
  89.  
  90. /***********************************************************************
  91.  * Print license information to stdout.
  92.  */
  93. local void license()
  94. {
  95.     extent i;             /* counter for copyright array */
  96.  
  97.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  98.         puts(public[i]);
  99.     }
  100.     for (i = 0; i < sizeof(disclaimer)/sizeof(char *); i++) {
  101.         puts(disclaimer[i]);
  102.     }
  103. }
  104.  
  105.  
  106. static ZCONST char *help_info[] = {
  107. "",
  108. "ZipCloak %s (%s)",
  109. "Usage:  zipcloak [-d] [-b path] zipfile",
  110. "  the default action is to encrypt all unencrypted entries in the zip file",
  111. "  -d   decrypt--decrypt encrypted entries (copy if given wrong password)",
  112. "  -b   use \"path\" for the temporary zip file",
  113. "  -h   show this help    -v   show version info    -L   show software license"
  114.   };
  115.  
  116. /***********************************************************************
  117.  * Print help (along with license info) to stdout.
  118.  */
  119. local void help()
  120. {
  121.     extent i;             /* counter for help array */
  122.  
  123.     for (i = 0; i < sizeof(public)/sizeof(char *); i++) {
  124.         puts(public[i]);
  125.     }
  126.     for (i = 0; i < sizeof(help_info)/sizeof(char *); i++) {
  127.         printf(help_info[i], VERSION, REVDATE);
  128.         putchar('\n');
  129.     }
  130. }
  131.  
  132.  
  133. local void version_info()
  134. /* Print verbose info about program version and compile time options
  135.    to stdout. */
  136. {
  137.   extent i;             /* counter in text arrays */
  138.  
  139.   /* Options info array */
  140.   static ZCONST char *comp_opts[] = {
  141. #ifdef DEBUG
  142.     "DEBUG",
  143. #endif
  144. #if CRYPT && defined(PASSWD_FROM_STDIN)
  145.     "PASSWD_FROM_STDIN",
  146. #endif /* CRYPT && PASSWD_FROM_STDIN */
  147.     NULL
  148.   };
  149.  
  150.   for (i = 0; i < sizeof(copyright)/sizeof(char *); i++)
  151.   {
  152.     printf(copyright[i], "zipcloak");
  153.     putchar('\n');
  154.   }
  155.  
  156.   for (i = 0; i < sizeof(versinfolines)/sizeof(char *); i++)
  157.   {
  158.     printf(versinfolines[i], "ZipCloak", VERSION, REVDATE);
  159.     putchar('\n');
  160.   }
  161.  
  162.   version_local();
  163.  
  164.   puts("ZipCloak special compilation options:");
  165.   for (i = 0; (int)i < (int)(sizeof(comp_opts)/sizeof(char *) - 1); i++)
  166.   {
  167.     printf("\t%s\n",comp_opts[i]);
  168.   }
  169.   printf("\t[encryption, version %d.%d%s of %s]\n",
  170.             CR_MAJORVER, CR_MINORVER, CR_BETA_VER, CR_VERSION_DATE);
  171. }
  172.  
  173.  
  174. /***********************************************************************
  175.  * Encrypt or decrypt all of the entries in a zip file.  See the command
  176.  * help in help() above.
  177.  */
  178.  
  179. int main(argc, argv)
  180.     int argc;             /* number of tokens in command line */
  181.     char **argv;          /* command line tokens */
  182. {
  183.     int attr;             /* attributes of zip file */
  184.     ulg start_offset;     /* start of central directory */
  185.     int decrypt;          /* decryption flag */
  186.     int temp_path;        /* 1 if next argument is path for temp files */
  187.     char passwd[PWLEN+1]; /* password for encryption or decryption */
  188.     char verify[PWLEN+1]; /* password for encryption or decryption */
  189.     char *q;              /* steps through option arguments */
  190.     int r;                /* arg counter */
  191.     int res;              /* result code */
  192.     ulg length;           /* length of central directory */
  193.     FILE *inzip, *outzip; /* input and output zip files */
  194.     struct zlist far *z;  /* steps through zfiles linked list */
  195.  
  196.  
  197.     /* If no args, show help */
  198.     if (argc == 1) {
  199.         help();
  200.         EXIT(0);
  201.     }
  202.  
  203.     init_upper();               /* build case map table */
  204.  
  205.     crc_32_tab = (ulg near *)get_crc_table();
  206.                                 /* initialize crc table for crypt */
  207.  
  208.     /* Go through args */
  209.     zipfile = tempzip = NULL;
  210.     tempzf = NULL;
  211.     signal(SIGINT, handler);
  212. #ifdef SIGTERM                /* Some don't have SIGTERM */
  213.     signal(SIGTERM, handler);
  214. #endif
  215.     temp_path = decrypt = 0;
  216.     for (r = 1; r < argc; r++) {
  217.         if (*argv[r] == '-') {
  218.             if (!argv[r][1]) ziperr(ZE_PARMS, "zip file cannot be stdin");
  219.             for (q = argv[r]+1; *q; q++) {
  220.                 switch (*q) {
  221.                 case 'b':   /* Specify path for temporary file */
  222.                     if (temp_path) {
  223.                         ziperr(ZE_PARMS, "use -b before zip file name");
  224.                     }
  225.                     temp_path = 1;          /* Next non-option is path */
  226.                     break;
  227.                 case 'd':
  228.                     decrypt = 1;  break;
  229.                 case 'h':   /* Show help */
  230.                     help();
  231.                     EXIT(0);
  232.                 case 'l': case 'L':  /* Show copyright and disclaimer */
  233.                     license();
  234.                     EXIT(0);
  235.                 case 'v':   /* Show version info */
  236.                     version_info();
  237.                     EXIT(0);
  238.                 default:
  239.                     ziperr(ZE_PARMS, "unknown option");
  240.                 } /* switch */
  241.             } /* for */
  242.  
  243.         } else if (temp_path == 0) {
  244.             if (zipfile != NULL) {
  245.                 ziperr(ZE_PARMS, "can only specify one zip file");
  246.  
  247.             } else if ((zipfile = ziptyp(argv[r])) == NULL) {
  248.                 ziperr(ZE_MEM, "was processing arguments");
  249.             }
  250.         } else {
  251.             tempath = argv[r];
  252.             temp_path = 0;
  253.         } /* if */
  254.     } /* for */
  255.  
  256.     if (zipfile == NULL) ziperr(ZE_PARMS, "need to specify zip file");
  257.  
  258.     /* Read zip file */
  259.     if ((res = readzipfile()) != ZE_OK) ziperr(res, zipfile);
  260.     if (zfiles == NULL) ziperr(ZE_NAME, zipfile);
  261.  
  262.     /* Check for something to do */
  263.     for (z = zfiles; z != NULL; z = z->nxt) {
  264.         if (decrypt ? z->flg & 1 : !(z->flg & 1)) break;
  265.     }
  266.     if (z == NULL) {
  267.         ziperr(ZE_NONE, decrypt ? "no encrypted files"
  268.                        : "all files encrypted already");
  269.     }
  270.  
  271.     /* Before we get carried away, make sure zip file is writeable */
  272.     if ((inzip = fopen(zipfile, "a")) == NULL) ziperr(ZE_CREAT, zipfile);
  273.     fclose(inzip);
  274.     attr = getfileattr(zipfile);
  275.  
  276.     /* Open output zip file for writing */
  277.     if ((tempzf = outzip = fopen(tempzip = tempname(zipfile), FOPW)) == NULL) {
  278.         ziperr(ZE_TEMP, tempzip);
  279.     }
  280.  
  281.     /* Get password */
  282.     if (getp("Enter password: ", passwd, PWLEN+1) == NULL)
  283.         ziperr(ZE_PARMS,
  284.                "stderr is not a tty (you may never see this message!)");
  285.  
  286.     if (decrypt == 0) {
  287.         if (getp("Verify password: ", verify, PWLEN+1) == NULL)
  288.                ziperr(ZE_PARMS,
  289.                       "stderr is not a tty (you may never see this message!)");
  290.  
  291.         if (strcmp(passwd, verify))
  292.                ziperr(ZE_PARMS, "password verification failed");
  293.  
  294.         if (*passwd == '\0')
  295.                ziperr(ZE_PARMS, "zero length password not allowed");
  296.     }
  297.  
  298.     /* Open input zip file again, copy preamble if any */
  299.     if ((inzip = fopen(zipfile, FOPR)) == NULL) ziperr(ZE_NAME, zipfile);
  300.  
  301.     if (zipbeg && (res = fcopy(inzip, outzip, zipbeg)) != ZE_OK) {
  302.         ziperr(res, res == ZE_TEMP ? tempzip : zipfile);
  303.     }
  304.     tempzn = zipbeg;
  305.  
  306.     /* Go through local entries, copying, encrypting, or decrypting */
  307.     for (z = zfiles; z != NULL; z = z->nxt) {
  308.         if (decrypt && (z->flg & 1)) {
  309.             printf("decrypting: %s", z->zname);
  310.             fflush(stdout);
  311.             if ((res = zipbare(z, inzip, outzip, passwd)) != ZE_OK) {
  312.                 if (res != ZE_MISS) ziperr(res, "was decrypting an entry");
  313.                 printf(" (wrong password--just copying)");
  314.             }
  315.             putchar('\n');
  316.  
  317.         } else if ((!decrypt) && !(z->flg & 1)) {
  318.             printf("encrypting: %s\n", z->zname);
  319.             fflush(stdout);
  320.             if ((res = zipcloak(z, inzip, outzip, passwd)) != ZE_OK) {
  321.                 ziperr(res, "was encrypting an entry");
  322.             }
  323.         } else {
  324.             printf("   copying: %s\n", z->zname);
  325.             fflush(stdout);
  326.             if ((res = zipcopy(z, inzip, outzip)) != ZE_OK) {
  327.                 ziperr(res, "was copying an entry");
  328.             }
  329.         } /* if */
  330.     } /* for */
  331.     fclose(inzip);
  332.  
  333.     /* Write central directory and end of central directory */
  334.  
  335.     /* get start of central */
  336.     if ((start_offset = ftell(outzip)) == -1L) ziperr(ZE_TEMP, tempzip);
  337.  
  338.     for (z = zfiles; z != NULL; z = z->nxt) {
  339.         if ((res = putcentral(z, outzip)) != ZE_OK) ziperr(res, tempzip);
  340.     }
  341.  
  342.     /* get end of central */
  343.     if ((length = ftell(outzip)) == -1L) ziperr(ZE_TEMP, tempzip);
  344.  
  345.     length -= start_offset;               /* compute length of central */
  346.     if ((res = putend((int)zcount, length, start_offset, zcomlen,
  347.                        zcomment, outzip)) != ZE_OK) {
  348.         ziperr(res, tempzip);
  349.     }
  350.     tempzf = NULL;
  351.     if (fclose(outzip)) ziperr(ZE_TEMP, tempzip);
  352.     if ((res = replace(zipfile, tempzip)) != ZE_OK) {
  353.         zipwarn("new zip file left as: ", tempzip);
  354.         free((zvoid *)tempzip);
  355.         tempzip = NULL;
  356.         ziperr(res, "was replacing the original zip file");
  357.     }
  358.     free((zvoid *)tempzip);
  359.     tempzip = NULL;
  360.     setfileattr(zipfile, attr);
  361.     free((zvoid *)zipfile);
  362.     zipfile = NULL;
  363.  
  364.     /* Done! */
  365.     RETURN(0);
  366. }
  367. #else /* !CRYPT */
  368.  
  369. int main OF((void));
  370.  
  371. void zipwarn(msg1, msg2)
  372. char  *msg1, *msg2;
  373. {
  374.     /* Tell picky compilers to shut up about unused variables */
  375.     msg1 = msg1; msg2 = msg2;
  376. }
  377.  
  378. void ziperr(c, h)
  379. int  c;
  380. char *h;
  381. {
  382.     /* Tell picky compilers to shut up about unused variables */
  383.     c = c; h = h;
  384. }
  385.  
  386. int main()
  387. {
  388.     fprintf(stderr, "\
  389. This version of ZipCloak does not support encryption.  Get zcrypt27.zip (or\n\
  390. a later version) and recompile.  The Info-ZIP file `WHERE' lists sites.\n");
  391.     RETURN(1);
  392. }
  393.  
  394. #endif /* ?CRYPT */
  395.