home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / pgp2 / src / c / pgp < prev    next >
Encoding:
Text File  |  1995-06-07  |  70.9 KB  |  2,441 lines

  1. /* #define TEMP_VERSION / * if defined, temporary experimental
  2.                        version of PGP */
  3. /* pgp.c -- main module for PGP.
  4.    PGP: Pretty Good(tm) Privacy - public key cryptography for the masses.
  5.  
  6.    Synopsis:  PGP uses public-key encryption to protect E-mail.
  7.    Communicate securely with people you've never met, with no secure
  8.    channels needed for prior exchange of keys.  PGP is well featured and
  9.    fast, with sophisticated key management, digital signatures, data
  10.    compression, and good ergonomic design.
  11.  
  12.    The original PGP version 1.0 was written by Philip Zimmermann, of
  13.    Phil's Pretty Good(tm) Software.  Many parts of later versions of
  14.    PGP were developed by an international collaborative effort,
  15.    involving a number of contributors, including major efforts by:
  16.    Branko Lankester <branko@hacktic.nl>
  17.    Hal Finney <74076.1041@compuserve.com>
  18.    Peter Gutmann <pgut1@cs.aukuni.ac.nz>
  19.    Other contributors who ported or translated or otherwise helped include:
  20.    Jean-loup Gailly in France
  21.    Hugh Kennedy in Germany
  22.    Lutz Frank in Germany
  23.    Cor Bosman in The Netherlands
  24.    Felipe Rodriquez Svensson in The Netherlands
  25.    Armando Ramos in Spain
  26.    Miguel Angel Gallardo Ortiz in Spain
  27.    Harry Bush and Maris Gabalins in Latvia
  28.    Zygimantas Cepaitis in Lithuania
  29.    Alexander Smishlajev
  30.    Peter Suchkow and Andrew Chernov in Russia
  31.    David Vincenzetti in Italy
  32.    ...and others.
  33.  
  34.  
  35.    (c) Copyright 1990-1994 by Philip Zimmermann.  All rights reserved.
  36.    The author assumes no liability for damages resulting from the use
  37.    of this software, even if the damage results from defects in this
  38.    software.  No warranty is expressed or implied.
  39.  
  40.    Note that while most PGP source modules bear Philip Zimmermann's
  41.    copyright notice, many of them have been revised or entirely written
  42.    by contributors who frequently failed to put their names in their
  43.    code.  Code that has been incorporated into PGP from other authors
  44.    was either originally published in the public domain or is used with
  45.    permission from the various authors.
  46.  
  47.    PGP is available for free to the public under certain restrictions.
  48.    See the PGP User's Guide (included in the release package) for
  49.    important information about licensing, patent restrictions on
  50.    certain algorithms, trademarks, copyrights, and export controls.
  51.  
  52.  
  53.    Philip Zimmermann may be reached at:
  54.    Boulder Software Engineering
  55.    3021 Eleventh Street
  56.    Boulder, Colorado 80304  USA
  57.    (303) 541-0140  (voice or FAX)
  58.    email:  prz@acm.org
  59.  
  60.  
  61.    PGP will run on MSDOS, Sun Unix, VAX/VMS, Ultrix, Atari ST,
  62.    Commodore Amiga, and OS/2.  Note:  Don't try to do anything with
  63.    this source code without looking at the PGP User's Guide.
  64.  
  65.    PGP combines the convenience of the Rivest-Shamir-Adleman (RSA)
  66.    public key cryptosystem with the speed of fast conventional
  67.    cryptographic algorithms, fast message digest algorithms, data
  68.    compression, and sophisticated key management.  And PGP performs
  69.    the RSA functions faster than most other software implementations.
  70.    PGP is RSA public key cryptography for the masses.
  71.  
  72.    Uses RSA Data Security, Inc. MD5 Message Digest Algorithm
  73.    as a hash for signatures.  Uses the ZIP algorithm for compression.
  74.    Uses the ETH IPES/IDEA algorithm for conventional encryption.
  75.  
  76.    PGP generally zeroes its used stack and memory areas before exiting.
  77.    This avoids leaving sensitive information in RAM where other users
  78.    could find it later.  The RSA library and keygen routines also
  79.    sanitize their own stack areas.  This stack sanitizing has not been
  80.    checked out under all the error exit conditions, when routines exit
  81.    abnormally.  Also, we must find a way to clear the C I/O library
  82.    file buffers, the disk buffers, and and cache buffers.
  83.  
  84.    Revisions:
  85.    Version 1.0 -  5 Jun 91
  86.    Version 1.4 - 19 Jan 92
  87.    Version 1.5 - 12 Feb 92
  88.    Version 1.6 - 24 Feb 92
  89.    Version 1.7 - 29 Mar 92
  90.    Version 1.8 - 23 May 92
  91.    Version 2.0 -  2 Sep 92
  92.    Version 2.1 -  6 Dec 92
  93.    Version 2.2 -  6 Mar 93
  94.    Version 2.3 - 13 Jun 93
  95.    Version 2.3a-  1 Jul 93
  96.    Version 2.4 -  6 Nov 93
  97.    Version 2.5 -  5 May 94
  98.    Version 2.6 - 22 May 94
  99.    Version 2.6.1 - 29 Aug 94
  100.    Version 2.6.2 - 11 Oct 94
  101.    Version 2.6.2i - 7 May 95
  102.  
  103.    Modified: 12-Nov-92 HAJK
  104.    Add FDL stuff for VAX/VMS local mode.
  105.  */
  106.  
  107.  
  108. #include <ctype.h>
  109. #ifndef AMIGA
  110. #include <signal.h>
  111. #endif
  112. #include <stdio.h>
  113. #include <stdlib.h>
  114. #include <string.h>
  115.  
  116. #ifdef __QNX__
  117. #include <sys/stat.h>
  118. #endif
  119.  
  120. #include "system.h"
  121. #include "mpilib.h"
  122. #include "random.h"
  123. #include "crypto.h"
  124. #include "fileio.h"
  125. #include "keymgmt.h"
  126. #include "language.h"
  127. #include "pgp.h"
  128. #include "exitpgp.h"
  129. #include "charset.h"
  130. #include "getopt.h"
  131. #include "config.h"
  132. #include "keymaint.h"
  133. #include "keyadd.h"
  134. #include "rsaglue.h"
  135. #ifdef  M_XENIX
  136. char *strstr();
  137. long time();
  138. #endif
  139.  
  140. #ifdef MSDOS
  141. #ifdef __ZTC__            /* Extend stack for Zortech C */
  142. unsigned _stack_ = 24 * 1024;
  143. #endif
  144. #ifdef __TURBOC__
  145. unsigned _stklen = 24 * 1024;
  146. #endif
  147. #endif
  148. #define    STACK_WIPE    4096
  149.  
  150. #ifdef AMIGA
  151. #ifdef __SASC_60
  152. /* Let the compiler allocate us an appropriate stack. */
  153. extern long __stack = 32768L;
  154. #endif
  155.  
  156.  /* Add the appropriate AmigaOS version string, depending on the
  157.   * compiler flags.
  158.   */
  159. #ifdef MIT
  160. static const char __DOSVer[] = "$VER: PGP 2.6.2 (07.05.95)"
  161. #  ifdef _M68020
  162.         " Amiga 68020 version by Rob Knop <rknop@mop.caltech.edu>";
  163. #  else
  164.         " Amiga 68000 version by Rob Knop <rknop@mop.caltech.edu>";
  165. #  endif
  166. #else
  167. static const char __DOSVer[] = "$VER: PGP 2.6.2i (07.05.95)"
  168. #  ifdef _M68020
  169.         " Amiga 68020 version by Peter Simons <simons@peti.rhein.de>";
  170. #  else
  171.         " Amiga 68000 version by Peter Simons <simons@peti.rhein.de>";
  172. #  endif
  173. #endif /* MIT */
  174. #endif /* AMIGA */
  175.  
  176. /* Global filenames and system-wide file extensions... */
  177. #ifdef MIT
  178. char rel_version[] = _LANG("2.6.2");    /* release version */
  179. static char rel_date[] = "11 Oct 94";    /* release date */
  180. #else
  181. char rel_version[] = _LANG("2.6.2i");    /* release version */
  182. static char rel_date[] = "7 May 95";    /* release date */
  183. #endif
  184. #ifdef RISC_OS
  185. /* As usual, RISC OS's stupid use of the period causes trouble. -- GJM */
  186. char PGP_EXTENSION[] = "/pgp";
  187. char ASC_EXTENSION[] = "/asc";
  188. char SIG_EXTENSION[] = "/sig";
  189. char BAK_EXTENSION[] = "/bak";
  190. static char HLP_EXTENSION[] = "/hlp";
  191. char CONSOLE_FILENAME[] = "_CONSOLE";
  192. static char HELP_FILENAME[] = "pgp/hlp";
  193. #else
  194. char PGP_EXTENSION[] = ".pgp";
  195. char ASC_EXTENSION[] = ".asc";
  196. char SIG_EXTENSION[] = ".sig";
  197. char BAK_EXTENSION[] = ".bak";
  198. static char HLP_EXTENSION[] = ".hlp";
  199. char CONSOLE_FILENAME[] = "_CONSOLE";
  200. static char HELP_FILENAME[] = "pgp.hlp";
  201. #endif
  202.  
  203. /* These files use the environmental variable PGPPATH as a default path: */
  204. char globalPubringName[MAX_PATH];
  205. char globalSecringName[MAX_PATH];
  206. char globalRandseedName[MAX_PATH];
  207. char globalCommentString[128];
  208.  
  209. /* Flags which are global across the driver code files */
  210. boolean verbose = FALSE;    /* -l option: display maximum information */
  211. FILE *pgpout;            /* Place for routine messages */
  212.  
  213. static void usage(void);
  214. static void key_usage(void);
  215. static void arg_error(void);
  216. static void initsigs(void);
  217. static int do_keyopt(char);
  218. static int do_decrypt(char *);
  219. static void do_armorfile(char *);
  220.  
  221.  
  222. /* Various compression signatures: PKZIP, Zoo, GIF, Arj, and HPACK.
  223.    Lha(rc) is handled specially in the code; it is missing from the
  224.    compressSig structure intentionally.  If more formats are added,
  225.    put them before lharc to keep the code consistent.
  226.  */
  227. static char *compressSig[] =
  228. {"PK\03\04", "ZOO ", "GIF8", "\352\140",
  229.  "HPAK", "\037\213", "\037\235", "\032\013", "\032HP%"
  230.     /* lharc is special, must be last */ };
  231. static char *compressName[] =
  232. {"PKZIP", "Zoo", "GIF", "Arj",
  233.  "Hpack", "gzip", "compressed", "PAK", "Hyper",
  234.  "LHarc"};
  235. static char *compressExt[] =
  236. #ifdef RISC_OS
  237. /* Sigh. -- GJM */
  238. {"/zip", "/zoo", "/gif", "/arj",
  239.  "/hpk", "/gz", "/z", "/pak", "/hyp",
  240.  "/lzh"};
  241. #else
  242. {".zip", ".zoo", ".gif", ".arj",
  243.  ".hpk", ".gz", ".Z", ".pak", ".hyp",
  244.  ".lzh"};
  245. #endif
  246.  
  247. /* "\032\0??", "ARC", ".arc" */
  248.  
  249. /* Returns file signature type from a number of popular compression formats
  250.    or -1 if no match */
  251. int compressSignature(byte * header)
  252. {
  253.     int i;
  254.  
  255.     for (i = 0; i < sizeof(compressSig) / sizeof(*compressSig); i++)
  256.     if (!strncmp((char *) header, compressSig[i], strlen(compressSig[i])))
  257.         return i;
  258.     /* Special check for lharc files */
  259.     if (header[2] == '-' && header[3] == 'l' &&
  260.     (header[4] == 'z' || header[4] == 'h') &&
  261.     header[6] == '-')
  262.     return i;
  263.     return -1;
  264. }                /* compressSignature */
  265.  
  266. /* returns TRUE iff file is likely to be compressible */
  267. static boolean file_compressible(char *filename)
  268. {
  269.     byte header[8];
  270.     get_header_info_from_file(filename, header, 8);
  271.     if (compressSignature(header) >= 0)
  272.     return FALSE;        /* probably not compressible */
  273.     return TRUE;        /* possibly compressible */
  274. }                /* compressible */
  275.  
  276.  
  277. /* Possible error exit codes - not all of these are used.  Note that we
  278.    don't use the ANSI EXIT_SUCCESS and EXIT_FAILURE.  To make things
  279.    easier for compilers which don't support enum we use #defines */
  280.  
  281. #define EXIT_OK                0
  282. #define INVALID_FILE_ERROR        1
  283. #define FILE_NOT_FOUND_ERROR        2
  284. #define UNKNOWN_FILE_ERROR        3
  285. #define NO_BATCH            4
  286. #define BAD_ARG_ERROR            5
  287. #define INTERRUPT            6
  288. #define OUT_OF_MEM            7
  289.  
  290. /* Keyring errors: Base value = 10 */
  291. #define KEYGEN_ERROR            10
  292. #define NONEXIST_KEY_ERROR        11
  293. #define KEYRING_ADD_ERROR        12
  294. #define KEYRING_EXTRACT_ERROR        13
  295. #define KEYRING_EDIT_ERROR        14
  296. #define KEYRING_VIEW_ERROR        15
  297. #define KEYRING_REMOVE_ERROR        16
  298. #define KEYRING_CHECK_ERROR        17
  299. #define KEY_SIGNATURE_ERROR        18
  300. #define KEYSIG_REMOVE_ERROR        19
  301.  
  302. /* Encode errors: Base value = 20 */
  303. #define SIGNATURE_ERROR            20
  304. #define RSA_ENCR_ERROR            21
  305. #define ENCR_ERROR            22
  306. #define COMPRESS_ERROR            23
  307.  
  308. /* Decode errors: Base value = 30 */
  309. #define SIGNATURE_CHECK_ERROR        30
  310. #define RSA_DECR_ERROR            31
  311. #define DECR_ERROR            32
  312. #define DECOMPRESS_ERROR        33
  313.  
  314.  
  315. #ifdef SIGINT
  316.  
  317. /* This function is called if a BREAK signal is sent to the program.  In this
  318.    case we zap the temporary files.
  319.  */
  320. void breakHandler(int sig)
  321. {
  322. #ifdef UNIX
  323.     if (sig == SIGPIPE) {
  324.     signal(SIGPIPE, SIG_IGN);
  325.     exitPGP(INTERRUPT);
  326.     }
  327.     if (sig != SIGINT)
  328.     fprintf(stderr, "\nreceived signal %d\n", sig);
  329.     else
  330. #endif
  331.     fprintf(pgpout, LANG("\nStopped at user request\n"));
  332.     exitPGP(INTERRUPT);
  333. }
  334. #endif
  335.  
  336. /* Clears screen and homes the cursor. */
  337. static void clearscreen(void)
  338. {
  339.     fprintf(pgpout, "\n\033[0;0H\033[J\r           \r");  /* ANSI sequence. */
  340.     fflush(pgpout);
  341. }
  342.  
  343. /* We had to process the config file first to possibly select the
  344.    foreign language to translate the sign-on line that follows... */
  345. static void signon_msg(void)
  346. {
  347.     word32 tstamp;
  348.     /* display message only once to allow calling multiple times */
  349.     static boolean printed = FALSE;
  350.  
  351.     if (quietmode || printed)
  352.     return;
  353.     printed = TRUE;
  354.     fprintf(stderr,
  355. LANG("Pretty Good Privacy(tm) %s - Public-key encryption for the masses.\n"),
  356.         rel_version);
  357. #ifdef TEMP_VERSION
  358.     fputs(
  359. "Internal development version only - not for general release.\n", stderr);
  360. #endif
  361.     fprintf(stderr,
  362. #ifdef MIT
  363. LANG("(c) 1990-1994 Philip Zimmermann, Phil's Pretty Good Software. %s\n"),
  364. #else
  365. LANG("(c) 1990-1995 Philip Zimmermann, Phil's Pretty Good Software. %s\n"),
  366. #endif
  367.         rel_date);
  368. #ifdef RISC_OS
  369. /* So everyone knows whom to blame: -- GJM */
  370. fputs("(RISC OS version by Gareth McCaughan, June 1995)\n",stderr);
  371. #endif
  372. #ifdef MIT
  373.     fputs(LANG(signon_legalese), stderr);
  374. #endif
  375.     fputs(
  376. #ifdef MIT
  377. LANG("Export of this software may be restricted by the U.S. government.\n"),
  378. #else
  379. LANG("International version - not for use in the USA. Does not use RSAREF.\n"),
  380. #endif
  381.       stderr);
  382.  
  383.     get_timestamp((byte *) & tstamp);    /* timestamp points to tstamp */
  384. #ifdef MIT
  385.     fprintf(pgpout, "Current time: %s\n", ctdate(&tstamp));
  386. #else
  387.     fprintf(pgpout, LANG("Current time: %s\n"), ctdate(&tstamp));
  388. #endif
  389. }
  390.  
  391.  
  392. #ifdef TEMP_VERSION        /* temporary experimental version of PGP */
  393. #include <time.h>
  394. #define CREATION_DATE 0x2DC079A4ul
  395.                 /* CREATION_DATE is
  396.                    Fri Apr 29 03:06:12 1994 UTC */
  397. #define LIFESPAN    ((unsigned long) 60L * (unsigned long) 86400L)
  398.                 /* LIFESPAN is 60 days */
  399.  
  400. /* If this is an experimental version of PGP, cut its life short */
  401. void check_expiration_date(void)
  402. {
  403.     if (get_timestamp(NULL) > (CREATION_DATE + LIFESPAN)) {
  404.     fprintf(stderr,
  405.         "\n\007This experimental version of PGP has expired.\n");
  406.     exit(-1);        /* error exit */
  407.     }
  408. }                /* check_expiration_date */
  409. #else                /* no expiration date */
  410. #define check_expiration_date()    /* null statement */
  411. #endif                /* TEMP_VERSION */
  412.  
  413. /* -f means act as a unix-style filter */
  414. /* -i means internalize extended file attribute information, only supported
  415.  *          between like (or very compatible) operating systems. */
  416. /* -l means show longer more descriptive diagnostic messages */
  417. /* -m means display plaintext output on screen, like unix "more" */
  418. /* -d means decrypt only, leaving inner signature wrapping intact */
  419. /* -t means treat as pure text and convert to canonical text format */
  420.  
  421. /* Used by getopt function... */
  422. #define OPTIONS "abcdefghiklmo:prstu:vwxz:ABCDEFGHIKLMO:PRSTU:VWX?"
  423. extern int optind;
  424. extern char *optarg;
  425.  
  426. boolean emit_radix_64 = FALSE;    /* set by config file */
  427. static boolean sign_flag = FALSE;
  428. boolean moreflag = FALSE;
  429. boolean filter_mode = FALSE;
  430. static boolean preserve_filename = FALSE;
  431. static boolean decrypt_only_flag = FALSE;
  432. static boolean de_armor_only = FALSE;
  433. static boolean strip_sig_flag = FALSE;
  434. boolean clear_signatures = TRUE;
  435. boolean strip_spaces;
  436. static boolean c_flag = FALSE;
  437. static boolean u_flag = FALSE;        /* Did I get my_name from -u? */
  438. boolean encrypt_to_self = FALSE; /* should I encrypt messages to myself? */
  439. boolean batchmode = FALSE;    /* if TRUE: don't ask questions */
  440. boolean quietmode = FALSE;
  441. boolean force_flag = FALSE;    /* overwrite existing file without asking */
  442. #ifdef VMS            /* kludge for those stupid VMS variable-length
  443.                    text records */
  444. char literal_mode = MODE_TEXT;    /* MODE_TEXT or MODE_BINARY for literal
  445.                    packet */
  446. #else                /* not VMS */
  447. char literal_mode = MODE_BINARY; /* MODE_TEXT or MODE_BINARY for literal
  448.                     packet */
  449. #endif                /* not VMS */
  450. /* my_name is substring of default userid for secret key to make signatures */
  451. char my_name[256] = "\0";    /* null my_name means take first userid
  452.                    in ring */
  453. boolean keepctx = FALSE;    /* TRUE means keep .ctx file on decrypt */
  454. /* Ask for each key separately if it should be added to the keyring */
  455. boolean interactive_add = FALSE;
  456. boolean compress_enabled = TRUE; /* attempt compression before encryption */
  457. long timeshift = 0L;        /* seconds from GMT timezone */
  458. int version_byte = VERSION_BYTE_NEW;
  459. boolean nomanual = 0;
  460. /* If non-zero, initialize file to this many random bytes */
  461. int makerandom = 0;
  462.  
  463.  
  464. static char *outputfile = NULL;
  465. static int errorLvl = EXIT_OK;
  466. static char mcguffin[256];    /* userid search tag */
  467. boolean signature_checked = FALSE;
  468. char plainfile[MAX_PATH];
  469. int myArgc = 2;
  470. char **myArgv;
  471. struct hashedpw *passwds = 0, *keypasswds = 0;
  472. static struct hashedpw **passwdstail = &passwds;
  473.  
  474. int main(int argc, char *argv[])
  475. {
  476.     int status, opt;
  477.     char *inputfile = NULL;
  478.     char **recipient = NULL;
  479.     char **mcguffins;
  480.     char *workfile, *tempf;
  481.     boolean nestflag = FALSE;
  482.     boolean decrypt_mode = FALSE;
  483.     boolean wipeflag = FALSE;
  484.     boolean armor_flag = FALSE;    /* -a option */
  485.     boolean separate_signature = FALSE;
  486.     boolean keyflag = FALSE;
  487.     boolean encrypt_flag = FALSE;
  488.     boolean conventional_flag = FALSE;
  489.     boolean attempt_compression; /* attempt compression before encryption */
  490.     boolean output_stdout;    /* Output goes to stdout */
  491.     char *clearfile = NULL;
  492.     char *literal_file = NULL;
  493.     char literal_file_name[MAX_PATH];
  494.     char cipherfile[MAX_PATH];
  495.     char keychar = '\0';
  496.     char *p;
  497.     byte ctb;
  498.     struct hashedpw *hpw;
  499.  
  500.     /* Initial messages to stderr */
  501.     pgpout = stderr;
  502.  
  503. #ifdef    DEBUG1
  504.     verbose = TRUE;
  505. #endif
  506.     /* The various places one can get passwords from.
  507.      * We accumulate them all into two lists.  One is
  508.      * to try on keys only, and is stored in no particular
  509.      * order, while the other is of unknown purpose so
  510.      * far (they may be used for conventional encryption
  511.      * or decryption as well), and are kept in a specific
  512.      * order.  If any password in the general list is found
  513.      * to decode a key, it is moved to the key list.
  514.      * The general list is not grown after initialization,
  515.      * so the tail pointer is not used after this.
  516.      */
  517.  
  518.     if ((p = getenv("PGPPASS")) != NULL) {
  519.     hpw = xmalloc(sizeof(struct hashedpw));
  520.     hashpass(p, strlen(p), hpw->hash);
  521.     /* Add to linked list of key passwords */
  522.     hpw->next = keypasswds;
  523.     keypasswds = hpw;
  524.     }
  525.     /* The -z "password" option should be used instead of PGPPASS if
  526.      * the environment can be displayed with the ps command (eg. BSD).
  527.      * If the system does not allow overwriting of the command line
  528.      * argument list but if it has a "hidden" environment, PGPPASS
  529.      * should be used.
  530.      */
  531.     for (opt = 1; opt < argc; ++opt) {
  532.     p = argv[opt];
  533.     if (p[0] != '-' || p[1] != 'z')
  534.         continue;
  535.     /* Accept either "-zpassword" or "-z password" */
  536.     p += 2;
  537.     if (!*p)
  538.         p = argv[++opt];
  539.     /* p now points to password */
  540.     if (!p)
  541.         break;        /* End of arg list - ignore */
  542.     hpw = xmalloc(sizeof(struct hashedpw));
  543.     hashpass(p, strlen(p), hpw->hash);
  544.     /* Wipe password */
  545.     while (*p)
  546.         *p++ = ' ';
  547.     /* Add to tail of linked list of passwords */
  548.     hpw->next = 0;
  549.     *passwdstail = hpw;
  550.     passwdstail = &hpw->next;
  551.     }
  552.     /*
  553.      * If PGPPASSFD is set in the environment try to read the password
  554.      * from this file descriptor.  If you set PGPPASSFD to 0 pgp will
  555.      * use the first line read from stdin as password.
  556.      */
  557.     if ((p = getenv("PGPPASSFD")) != NULL) {
  558.     int passfd;
  559.     if (*p && (passfd = atoi(p)) >= 0) {
  560.         char pwbuf[256];
  561.         p = pwbuf;
  562. #ifdef RISC_OS
  563. /* I don't really believe any RISC OS user will try to use this
  564.  * feature anyway. But if they do... Well, stdin etc don't have
  565.  * file descriptors (handles). We check specifically for |passfd==0|
  566.  * and special-case that. -- GJM */
  567.         if (passfd==0)
  568.           while (fread(p,1,1,stdin)==1 && *p!='\n') ++p;
  569.         else
  570. #endif
  571.         while (read(passfd, p, 1) == 1 && *p != '\n')
  572.         ++p;
  573.         hpw = xmalloc(sizeof(struct hashedpw));
  574.         hashpass(pwbuf, p - pwbuf, hpw->hash);
  575.         memset(pwbuf, 0, p - pwbuf);
  576.         /* Add to tail of linked list of passwords */
  577.         hpw->next = 0;
  578.         *passwdstail = hpw;
  579.         passwdstail = &hpw->next;
  580.     }
  581.     }
  582.     /* Process the config file.  The following override each other:
  583.        - Hard-coded defualts
  584.        - The system config file
  585.        - Hard-coded defaults for security-critical things
  586.        - The user's config file
  587.        - Environment variables
  588.        - Command-line options.
  589.      */
  590.     opt = 0;            /* Number of config files read */
  591. #ifdef PGP_SYSTEM_DIR
  592.     strcpy(mcguffin, PGP_SYSTEM_DIR);
  593. #ifdef RISC_OS
  594.     /* yeah, yeah, yeah. -- GJM */
  595.     strcat(mcguffin, "config/txt");
  596. #else
  597.     strcat(mcguffin, "config.txt");
  598. #endif
  599.     if (access(mcguffin, 0) == 0) {
  600.     opt++;
  601.     /*
  602.      * Note: errors here are NOT fatal, so that people
  603.      * can use PGP with a corrputed system file.
  604.      */
  605.     processConfigFile(mcguffin);
  606.     }
  607. #endif
  608.  
  609.     /*
  610.      * These must be personal; the system config file may not
  611.      * influence them.
  612.      */
  613. #ifdef RISC_OS
  614.     /* Guess what? -- GJM
  615.      * NB that names have been *shortened* too. Grrrrr. */
  616.     buildfilename(globalPubringName, "pubrng/pgp");
  617.     buildfilename(globalSecringName, "secrng/pgp");
  618.     buildfilename(globalRandseedName, "randsd/bin");
  619. #else
  620.     buildfilename(globalPubringName, "pubring.pgp");
  621.     buildfilename(globalSecringName, "secring.pgp");
  622.     buildfilename(globalRandseedName, "randseed.bin");
  623. #endif
  624.     my_name[0] = '\0';
  625.  
  626.     /* Process the config file first.  Any command-line arguments will
  627.        override the config file settings */
  628. #if defined(UNIX) || defined(MSDOS) || defined(OS2)
  629.     /* Try "pgp.ini" on MS-DOS or ".pgprc" on Unix */
  630. #ifdef UNIX
  631.     buildfilename(mcguffin, ".pgprc");
  632. #else
  633.     buildfilename(mcguffin, "pgp.ini");
  634. #endif
  635.     if (access(mcguffin, 0) != 0)
  636.     buildfilename(mcguffin, "config.txt");
  637. #else
  638. #ifdef RISC_OS
  639.     /* Aaaarrrggghhh! -- GJM */
  640.     buildfilename(mcguffin, "config/txt");
  641. #else
  642.     buildfilename(mcguffin, "config.txt");
  643. #endif
  644. #endif
  645.     if (access(mcguffin, 0) == 0) {
  646.     opt++;
  647.     if (processConfigFile(mcguffin) < 0)
  648.         exit(BAD_ARG_ERROR);
  649.     }
  650.     if (!opt)
  651.     fprintf(pgpout, LANG("\007No configuration file found.\n"));
  652.  
  653.     init_charset();
  654.  
  655. #ifdef MSDOS            /* only on MSDOS systems */
  656.     if ((p = getenv("TZ")) == NULL || *p == '\0') {
  657.     fprintf(pgpout,LANG("\007WARNING: Environmental variable TZ is not \
  658. defined, so GMT timestamps\n\
  659. may be wrong.  See the PGP User's Guide to properly define TZ\n\
  660. in AUTOEXEC.BAT file.\n"));
  661.     }
  662. #endif                /* MSDOS */
  663.  
  664. #ifdef VMS
  665. #define TEMP "SYS$SCRATCH"
  666. #else
  667. #define TEMP "TMP"
  668. #endif                /* VMS */
  669.     if ((p = getenv(TEMP)) != NULL && *p != '\0')
  670.     settmpdir(p);
  671.  
  672.     if ((myArgv = (char **) malloc((argc + 2) * sizeof(char **))) == NULL) {
  673.     fprintf(stderr, LANG("\n\007Out of memory.\n"));
  674.     exitPGP(7);
  675.     }
  676.     myArgv[0] = NULL;
  677.     myArgv[1] = NULL;
  678.  
  679.     /* Process all the command-line option switches: */
  680.     while (optind < argc) {
  681.     /*
  682.      * Allow random order of options and arguments (like GNU getopt)
  683.      * NOTE: this does not work with GNU getopt, use getopt.c from
  684.      * the PGP distribution.
  685.      */
  686.     if ((opt = pgp_getopt(argc, argv, OPTIONS)) == EOF) {
  687.         if (optind == argc)    /* -- at end */
  688.         break;
  689.         myArgv[myArgc++] = argv[optind++];
  690.         continue;
  691.     }
  692.     opt = to_lower(opt);
  693.     if (keyflag && (keychar == '\0' || (keychar == 'v' && opt == 'v'))) {
  694.         if (keychar == 'v')
  695.         keychar = 'V';
  696.         else
  697.         keychar = opt;
  698.         continue;
  699.     }
  700.     switch (opt) {
  701.     case 'a':
  702.         armor_flag = TRUE;
  703.         emit_radix_64 = 1;
  704.         break;
  705.     case 'b':
  706.         separate_signature = strip_sig_flag = TRUE;
  707.         break;
  708.     case 'c':
  709.         encrypt_flag = conventional_flag = TRUE;
  710.         c_flag = TRUE;
  711.         break;
  712.     case 'd':
  713.         decrypt_only_flag = TRUE;
  714.         break;
  715.     case 'e':
  716.         encrypt_flag = TRUE;
  717.         break;
  718.     case 'f':
  719.         filter_mode = TRUE;
  720.         break;
  721.     case '?':
  722.     case 'h':
  723.         usage();
  724.         break;
  725. #ifdef VMS
  726.     case 'i':
  727.         literal_mode = MODE_LOCAL;
  728.         break;
  729. #endif                /* VMS */
  730.     case 'k':
  731.         keyflag = TRUE;
  732.         break;
  733.     case 'l':
  734.         verbose = TRUE;
  735.         break;
  736.     case 'm':
  737.         moreflag = TRUE;
  738.         break;
  739.     case 'p':
  740.         preserve_filename = TRUE;
  741.         break;
  742.     case 'o':
  743.         outputfile = optarg;
  744.         break;
  745.     case 's':
  746.         sign_flag = TRUE;
  747.         break;
  748.     case 't':
  749.         literal_mode = MODE_TEXT;
  750.         break;
  751.     case 'u':
  752.         strncpy(my_name, optarg, sizeof(my_name) - 1);
  753.         CONVERT_TO_CANONICAL_CHARSET(my_name);
  754.         u_flag = TRUE;
  755.         break;
  756.     case 'w':
  757.         wipeflag = TRUE;
  758.         break;
  759.     case 'z':
  760.         break;
  761.         /* '+' special option: does not require - */
  762.     case '+':
  763.         if (processConfigLine(optarg) == 0)
  764.         break;
  765.         fprintf(stderr, "\n");
  766.         /* fallthrough */
  767.     default:
  768.         arg_error();
  769.     }
  770.     }
  771.     myArgv[myArgc] = NULL;    /* Just to make it NULL terminated */
  772.  
  773.     if (keyflag && keychar == '\0')
  774.     key_usage();
  775.  
  776.     signon_msg();
  777.     check_expiration_date();    /* hobble any experimental version */
  778.  
  779.     /*
  780.      * Write to stdout if explicitly asked to, or in filter mode and
  781.      * no explicit file name was given.
  782.      */
  783.     output_stdout = outputfile ? strcmp(outputfile, "-")  == 0 : filter_mode;
  784.  
  785. #if 1
  786.     /* At request of Peter Simons, use stderr always. Sounds reasonable. */
  787.     /* JIS: Put this code back in... removing it broke too many things */
  788.     if (!output_stdout)
  789.     pgpout = stdout;
  790. #endif
  791.  
  792.  
  793. #if defined(UNIX) || defined(VMS)
  794.     umask(077);            /* Make files default to private */
  795. #endif
  796.  
  797.     initsigs();            /* Catch signals */
  798.     noise();            /* Start random number generation */
  799.  
  800.     if (keyflag) {
  801.     status = do_keyopt(keychar);
  802.     if (status < 0)
  803.         user_error();
  804.     exitPGP(status);
  805.     }
  806.     /* -db means break off signature certificate into separate file */
  807.     if (decrypt_only_flag && strip_sig_flag)
  808.     decrypt_only_flag = FALSE;
  809.  
  810.     if (decrypt_only_flag && armor_flag)
  811.     decrypt_mode = de_armor_only = TRUE;
  812.  
  813.     if (outputfile != NULL)
  814.     preserve_filename = FALSE;
  815.  
  816.     if (!sign_flag && !encrypt_flag && !conventional_flag && !armor_flag) {
  817.     if (wipeflag) {        /* wipe only */
  818.         if (myArgc != 3)
  819.         arg_error();    /* need one argument */
  820.         if (wipefile(myArgv[2]) == 0 && remove(myArgv[2]) == 0) {
  821.         fprintf(pgpout,
  822.             LANG("\nFile %s wiped and deleted. "), myArgv[2]);
  823.         fprintf(pgpout, "\n");
  824.         exitPGP(EXIT_OK);
  825.         }
  826.         exitPGP(UNKNOWN_FILE_ERROR);
  827.     }
  828.     /* decrypt if none of the -s -e -c -a -w options are specified */
  829.     decrypt_mode = TRUE;
  830.     }
  831.     if (myArgc == 2) {        /* no arguments */
  832. #ifdef UNIX
  833.     if (!filter_mode && !isatty(fileno(stdin))) {
  834.         /* piping to pgp without arguments and no -f:
  835.          * switch to filter mode but don't write output to stdout
  836.          * if it's a tty, use the preserved filename */
  837.         if (!moreflag)
  838.         pgpout = stderr;
  839.         filter_mode = TRUE;
  840.         if (isatty(fileno(stdout)) && !moreflag)
  841.         preserve_filename = TRUE;
  842.     }
  843. #endif
  844.     if (!filter_mode) {
  845.         if (quietmode) {
  846.         quietmode = FALSE;
  847.         signon_msg();
  848.         }
  849.         fprintf(pgpout,
  850. LANG("\nFor details on licensing and distribution, see the PGP User's Guide.\
  851. \nFor other cryptography products and custom development services, contact:\
  852. \nPhilip Zimmermann, 3021 11th St, Boulder CO 80304 USA, \
  853. phone +1 303 541-0140\n"));
  854.         if (strcmp((p = LANG("@translator@")), "@translator@"))
  855.         fprintf(pgpout, p);
  856.         fprintf(pgpout, LANG("\nFor a usage summary, type:  pgp -h\n"));
  857.         exit(BAD_ARG_ERROR);    /* error exit */
  858.     }
  859.     } else {
  860.     if (filter_mode) {
  861.         recipient = &myArgv[2];
  862.     } else {
  863.         inputfile = myArgv[2];
  864.         recipient = &myArgv[3];
  865.     }
  866.     }
  867.  
  868.  
  869.     if (filter_mode) {
  870.     inputfile = "stdin";
  871.     } else if (makerandom > 0) {    /* Create the input file */
  872.     /*
  873.      * +makerandom=<bytes>: Create an input file consisting of <bytes>
  874.      * cryptographically strong random bytes, before applying the
  875.      * encryption options of PGP.  This is an advanced option, so
  876.      * assume the user knows what he's doing and don't bother about
  877.      * overwriting questions.  E.g.
  878.      * pgp +makerandom=24 foofile
  879.      *    Create "foofile" with 24 random bytes in it.
  880.      * pgp +makerandom=24 -ea foofile recipient
  881.      *    The same, but also encrypt it to "recipient", creating
  882.      *    foofile.asc as well.
  883.      * This feature was created to allow PGP to create and send keys
  884.      * around for other applications to use.
  885.      */
  886.     status = cryptRandWriteFile(inputfile, (struct IdeaCfbContext *)0,
  887.                            (unsigned)makerandom);
  888.     if (status < 0) {
  889.         fprintf(stderr,"Error writing file \"%s\"\n",inputfile);
  890.         exitPGP(INVALID_FILE_ERROR);
  891.     }
  892.     fprintf(pgpout, LANG("File %s created containing %d random bytes.\n"),
  893.         inputfile, makerandom);
  894.     /* If we aren't encrypting, don't bother trying to decrypt this! */
  895.     if (decrypt_mode)
  896.         exitPGP(EXIT_OK);
  897.  
  898.     /* This is obviously NOT a text file */
  899.     literal_mode = MODE_BINARY;
  900.     } else {
  901.     if (decrypt_mode && no_extension(inputfile)) {
  902.         strcpy(cipherfile, inputfile);
  903.         force_extension(cipherfile, ASC_EXTENSION);
  904.         if (file_exists(cipherfile)) {
  905.         inputfile = cipherfile;
  906.         } else {
  907.         force_extension(cipherfile, PGP_EXTENSION);
  908.         if (file_exists(cipherfile)) {
  909.             inputfile = cipherfile;
  910.         } else {
  911.             force_extension(cipherfile, SIG_EXTENSION);
  912.             if (file_exists(cipherfile))
  913.             inputfile = cipherfile;
  914.         }
  915.         }
  916.     }
  917.     if (!file_exists(inputfile)) {
  918.         fprintf(pgpout,
  919.             LANG("\007File [%s] does not exist.\n"), inputfile);
  920.         errorLvl = FILE_NOT_FOUND_ERROR;
  921.         user_error();
  922.     }
  923.     }
  924.  
  925.     if (strlen(inputfile) >= (unsigned) MAX_PATH - 4) {
  926.     fprintf(pgpout,
  927.         LANG("\007Invalid filename: [%s] too long\n"), inputfile);
  928.     errorLvl = INVALID_FILE_ERROR;
  929.     user_error();
  930.     }
  931.     strcpy(plainfile, inputfile);
  932.  
  933.     if (filter_mode) {
  934.     setoutdir(NULL);    /* NULL means use tmpdir */
  935.     } else {
  936.     if (outputfile)
  937.         setoutdir(outputfile);
  938.     else
  939.         setoutdir(inputfile);
  940.     }
  941.  
  942.     if (filter_mode) {
  943.     workfile = tempfile(TMP_WIPE | TMP_TMPDIR);
  944.     readPhantomInput(workfile);
  945.     } else {
  946.     workfile = inputfile;
  947.     }
  948.  
  949.     get_header_info_from_file(workfile, &ctb, 1);
  950.     if (decrypt_mode) {
  951.     strip_spaces = FALSE;
  952.     if (!is_ctb(ctb) && is_armor_file(workfile, 0L))
  953.         do_armorfile(workfile);
  954.     else if (do_decrypt(workfile) < 0)
  955.         user_error();
  956.     if (batchmode && !signature_checked)
  957.         exitPGP(1);        /* alternate success, file did not have sig. */
  958.     else
  959.         exitPGP(EXIT_OK);
  960.     }
  961.     /*
  962.      * See if plaintext input file was actually created by PGP earlier--
  963.      * If it was, maybe we should NOT encapsulate it in a literal packet.
  964.      * (nestflag = TRUE).  Otherwise, always encapsulate it (default).
  965.      * (Why test for filter_mode???)
  966.      */
  967.     if (!batchmode && !filter_mode && legal_ctb(ctb)) {
  968.     /*      Special case--may be a PGP-created packet, so
  969.        do we inhibit encapsulation in literal packet? */
  970.     fprintf(pgpout,
  971. LANG("\n\007Input file '%s' looks like it may have been created by PGP. "),
  972.         inputfile);
  973.     fprintf(pgpout,
  974. LANG("\nIs it safe to assume that it was created by PGP (y/N)? "));
  975.     nestflag = getyesno('n');
  976.     } else if (force_flag && makerandom == 0 && legal_ctb(ctb)) {
  977.     nestflag = TRUE;
  978.     }
  979.  
  980.     if (moreflag && makerandom == 0) {
  981.     /* special name to cause printout on decrypt */
  982.     strcpy(literal_file_name, CONSOLE_FILENAME);
  983.     literal_mode = MODE_TEXT;    /* will check for text file later */
  984.     } else {
  985.     strcpy(literal_file_name, file_tail(inputfile));
  986. #ifdef MSDOS
  987.     strlwr(literal_file_name);
  988. #endif
  989.     }
  990.     literal_file = literal_file_name;
  991.  
  992.     /*      Make sure non-text files are not accidentally converted
  993.        to canonical text.  This precaution should only be followed
  994.        for US ASCII text files, since European text files may have
  995.        8-bit character codes and still be legitimate text files
  996.        suitable for conversion to canonical (CR/LF-terminated)
  997.        text format. */
  998.     if (literal_mode == MODE_TEXT && !is_text_file(workfile)) {
  999.     fprintf(pgpout,
  1000. LANG("\nNote: '%s' is not a pure text file.\n\
  1001. File will be treated as binary data.\n"),
  1002.         workfile);
  1003.     literal_mode = MODE_BINARY;    /* now expect straight binary */
  1004.     }
  1005.     if (moreflag && literal_mode == MODE_BINARY) {
  1006.     /* For eyes only?  Can't display binary file. */
  1007.     fprintf(pgpout,
  1008. LANG("\n\007Error: Only text files may be sent as display-only.\n"));
  1009.     errorLvl = INVALID_FILE_ERROR;
  1010.     user_error();
  1011.     }
  1012.  
  1013.     /*
  1014.      * See if plainfile looks like it might be incompressible,
  1015.      * by examining its contents for compression headers for
  1016.      * commonly-used compressed file formats like PKZIP, etc.
  1017.      * Remember this information for later, when we are deciding
  1018.      * whether to attempt compression before encryption.
  1019.      *
  1020.      * Naturally, don't bother if we are making a separate signature or
  1021.      * clear-signed message.  Also, don't bother trying to compress a
  1022.      * PGP message, as it's probably already compressed.
  1023.      */
  1024.     attempt_compression = compress_enabled && !separate_signature &&
  1025.                           !nestflag && !clearfile && makerandom == 0 &&
  1026.                           file_compressible(plainfile);
  1027.  
  1028.     if (sign_flag) {
  1029.     if (!filter_mode && !quietmode)
  1030.         fprintf(pgpout,
  1031. LANG("\nA secret key is required to make a signature. "));
  1032.     if (!quietmode && my_name[0] == '\0') {
  1033.         fprintf(pgpout,
  1034. LANG("\nYou specified no user ID to select your secret key,\n\
  1035. so the default user ID and key will be the most recently\n\
  1036. added key on your secret keyring.\n"));
  1037.     }
  1038.     strip_spaces = FALSE;
  1039.     clearfile = NULL;
  1040.     if (literal_mode == MODE_TEXT) {
  1041.         /* Text mode requires becoming canonical */
  1042.         tempf = tempfile(TMP_WIPE | TMP_TMPDIR);
  1043.         /* +clear means output file with signature in the clear,
  1044.            only in combination with -t and -a, not with -e or -b */
  1045.         if (!encrypt_flag && !separate_signature &&
  1046.         emit_radix_64 && clear_signatures) {
  1047.         clearfile = workfile;
  1048.         strip_spaces = TRUE;
  1049.         }
  1050.         make_canonical(workfile, tempf);
  1051.         if (!clearfile)
  1052.         rmtemp(workfile);
  1053.         workfile = tempf;
  1054.     }
  1055.     if (attempt_compression || encrypt_flag || emit_radix_64 ||
  1056.         output_stdout)
  1057.         tempf = tempfile(TMP_WIPE | TMP_TMPDIR);
  1058.     else
  1059.         tempf = tempfile(TMP_WIPE);
  1060.     /* for clear signatures we create a separate signature */
  1061.     status = signfile(nestflag, separate_signature || (clearfile != NULL),
  1062.            my_name, workfile, tempf, literal_mode, literal_file);
  1063.     rmtemp(workfile);
  1064.     workfile = tempf;
  1065.  
  1066.     if (status < 0) {    /* signfile failed */
  1067.         fprintf(pgpout, LANG("\007Signature error\n"));
  1068.         errorLvl = SIGNATURE_ERROR;
  1069.         user_error();
  1070.     }
  1071.     } else if (!nestflag) {    /* !sign_file */
  1072.     /*      Prepend CTB_LITERAL byte to plaintext file.
  1073.        --sure wish this pass could be optimized away. */
  1074.     if (attempt_compression || encrypt_flag || emit_radix_64 ||
  1075.         output_stdout)
  1076.         tempf = tempfile(TMP_WIPE | TMP_TMPDIR);
  1077.     else
  1078.         tempf = tempfile(TMP_WIPE);
  1079.     /* for clear signatures we create a separate signature */
  1080.     status = make_literal(workfile, tempf, literal_mode, literal_file);
  1081.     rmtemp(workfile);
  1082.     workfile = tempf;
  1083.     }
  1084.  
  1085.     if (encrypt_flag) {
  1086.         if (emit_radix_64 || output_stdout)
  1087.         tempf = tempfile(TMP_WIPE | TMP_TMPDIR);
  1088.     else
  1089.         tempf = tempfile(TMP_WIPE);
  1090.     if (!conventional_flag) {
  1091.         if (!filter_mode && !quietmode)
  1092.         fprintf(pgpout,
  1093. LANG("\n\nRecipients' public key(s) will be used to encrypt. "));
  1094.         if (recipient == NULL || *recipient == NULL ||
  1095.         **recipient == '\0') {
  1096.         /* no recipient specified on command line */
  1097.         fprintf(pgpout,
  1098. LANG("\nA user ID is required to select the recipient's public key. "));
  1099.         fprintf(pgpout, LANG("\nEnter the recipient's user ID: "));
  1100. #ifdef AMIGA
  1101.                 requesterdesc=LANG("\nEnter the recipient's user ID: ");
  1102. #endif
  1103.         getstring(mcguffin, 255, TRUE);        /* echo keyboard */
  1104.         if ((mcguffins = (char **) malloc(2 * sizeof(char *))) == NULL)
  1105.         {
  1106.             fprintf(stderr, LANG("\n\007Out of memory.\n"));
  1107.             exitPGP(7);
  1108.         }
  1109.         mcguffins[0] = mcguffin;
  1110.         mcguffins[1] = "";
  1111.         } else {
  1112.         /* recipient specified on command line */
  1113.         mcguffins = recipient;
  1114.         }
  1115.         for (recipient = mcguffins; *recipient != NULL &&
  1116.          **recipient != '\0'; recipient++) {
  1117.         CONVERT_TO_CANONICAL_CHARSET(*recipient);
  1118.         }
  1119.         status = encryptfile(mcguffins, workfile, tempf,
  1120.                  attempt_compression);
  1121.     } else {
  1122.         status = idea_encryptfile(workfile, tempf, attempt_compression);
  1123.     }
  1124.  
  1125.     rmtemp(workfile);
  1126.     workfile = tempf;
  1127.  
  1128.     if (status < 0) {
  1129.         fprintf(pgpout, LANG("\007Encryption error\n"));
  1130.         errorLvl = (conventional_flag ? ENCR_ERROR : RSA_ENCR_ERROR);
  1131.         user_error();
  1132.     }
  1133.     } else if (attempt_compression && !separate_signature && !clearfile) {
  1134.     /*
  1135.      * PGP used to be parsimonious about compressin; originally, it only
  1136.      * did it for files that were being encrypted (to reduce the
  1137.      * redundancy in the plaintext), but it should really do it for
  1138.      * anything where it's not a bad idea.
  1139.      */
  1140.         if (emit_radix_64 || output_stdout)
  1141.         tempf = tempfile(TMP_WIPE | TMP_TMPDIR);
  1142.     else
  1143.         tempf = tempfile(TMP_WIPE);
  1144.     squish_file(workfile, tempf);
  1145.     rmtemp(workfile);
  1146.     workfile = tempf;
  1147.     }
  1148.  
  1149.     /*
  1150.      * Write to stdout if explicitly asked to, or in filter mode and
  1151.      * no explicit file name was given.
  1152.      */
  1153.     if (output_stdout) {
  1154.     if (emit_radix_64) {
  1155.         /* NULL for outputfile means write to stdout */
  1156.         if (armor_file(workfile, NULL, inputfile, clearfile) != 0) {
  1157.         errorLvl = UNKNOWN_FILE_ERROR;
  1158.         user_error();
  1159.         }
  1160.         if (clearfile)
  1161.         rmtemp(clearfile);
  1162.     } else {
  1163.         if (writePhantomOutput(workfile) < 0) {
  1164.         errorLvl = UNKNOWN_FILE_ERROR;
  1165.         user_error();
  1166.         }
  1167.     }
  1168.     rmtemp(workfile);
  1169.     } else {
  1170.     char name[MAX_PATH];
  1171.     if (outputfile) {
  1172.         strcpy(name, outputfile);
  1173.     } else {
  1174.         strcpy(name, inputfile);
  1175.         drop_extension(name);
  1176. #ifdef MIT
  1177.     }
  1178.     if (no_extension(name)) {
  1179. #endif
  1180.         if (emit_radix_64)
  1181.         force_extension(name, ASC_EXTENSION);
  1182.         else if (sign_flag && separate_signature)
  1183.         force_extension(name, SIG_EXTENSION);
  1184.         else
  1185.         force_extension(name, PGP_EXTENSION);
  1186.     }
  1187.     if (emit_radix_64) {
  1188.         if (armor_file(workfile, name, inputfile, clearfile) != 0) {
  1189.         errorLvl = UNKNOWN_FILE_ERROR;
  1190.         user_error();
  1191.         }
  1192.         if (clearfile)
  1193.         rmtemp(clearfile);
  1194.     } else {
  1195.         if ((outputfile = savetemp(workfile, name)) == NULL) {
  1196.         errorLvl = UNKNOWN_FILE_ERROR;
  1197.         user_error();
  1198.         }
  1199.         if (!quietmode) {
  1200.         if (encrypt_flag)
  1201.             fprintf(pgpout,
  1202.                 LANG("\nCiphertext file: %s\n"), outputfile);
  1203.         else if (sign_flag)
  1204.             fprintf(pgpout,
  1205.                 LANG("\nSignature file: %s\n"), outputfile);
  1206.         }
  1207.     }
  1208.     }
  1209.  
  1210.     if (wipeflag) {
  1211.     /* destroy every trace of plaintext */
  1212.     if (wipefile(inputfile) == 0) {
  1213.         remove(inputfile);
  1214.         fprintf(pgpout, LANG("\nFile %s wiped and deleted. "), inputfile);
  1215.         fprintf(pgpout, "\n");
  1216.     }
  1217.     }
  1218.     exitPGP(EXIT_OK);
  1219.     return 0;            /* to shut up lint and some compilers */
  1220. }                /* main */
  1221.  
  1222. #ifdef MSDOS
  1223. #include <dos.h>
  1224. static char *dos_errlst[] =
  1225. {
  1226.     "Write protect error",    /* LANG ("Write protect error") */
  1227.     "Unknown unit",
  1228.     "Drive not ready",        /* LANG ("Drive not ready") */
  1229.     "3", "4", "5", "6", "7", "8", "9",
  1230.     "Write error",        /* LANG ("Write error") */
  1231.     "Read error",        /* LANG ("Read error") */
  1232.     "General failure",
  1233. };
  1234.  
  1235. /* handler for msdos 'harderrors' */
  1236. #ifndef OS2
  1237. #ifdef __TURBOC__        /* Turbo C 2.0 */
  1238. static int dostrap(int errval)
  1239. #else
  1240. static void dostrap(unsigned deverr, unsigned errval)
  1241. #endif
  1242. {
  1243.     char errbuf[64];
  1244.     int i;
  1245.     sprintf(errbuf, "\r\nDOS error: %s\r\n", dos_errlst[errval]);
  1246.     i = 0;
  1247.     do
  1248.     bdos(2, (unsigned int) errbuf[i], 0);
  1249.     while (errbuf[++i]);
  1250. #ifdef __TURBOC__
  1251.     return 0;            /* ignore (fopen will return NULL) */
  1252. #else
  1253.     return;
  1254. #endif
  1255. }
  1256. #endif                /* MSDOS */
  1257. #endif
  1258.  
  1259. static void initsigs()
  1260. {
  1261. #ifdef MSDOS
  1262. #ifndef OS2
  1263. #ifdef __TURBOC__
  1264.     harderr(dostrap);
  1265. #else                /* MSC */
  1266. #ifndef __GNUC__        /* DJGPP's not MSC */
  1267.     _harderr(dostrap);
  1268. #endif
  1269. #endif
  1270. #endif
  1271. #endif                /* MSDOS */
  1272. #ifdef SIGINT
  1273.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  1274.     signal(SIGINT, breakHandler);
  1275. #if defined(UNIX) || defined(VMS) || defined(ATARI)
  1276.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  1277.     signal(SIGHUP, breakHandler);
  1278.     if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
  1279.     signal(SIGQUIT, breakHandler);
  1280. #ifdef UNIX
  1281.     signal(SIGPIPE, breakHandler);
  1282. #endif
  1283.     signal(SIGTERM, breakHandler);
  1284. #ifndef DEBUG
  1285.     signal(SIGTRAP, breakHandler);
  1286.     signal(SIGSEGV, breakHandler);
  1287.     signal(SIGILL, breakHandler);
  1288. #ifdef SIGBUS
  1289.     signal(SIGBUS, breakHandler);
  1290. #endif
  1291. #endif                /* DEBUG */
  1292. #endif                /* UNIX */
  1293. #endif                /* SIGINT */
  1294. }                /* initsigs */
  1295.  
  1296.  
  1297. static void do_armorfile(char *armorfile)
  1298. {
  1299.     char *tempf;
  1300.     char cipherfile[MAX_PATH];
  1301.     long linepos = 0;
  1302.     int status;
  1303.     int success = 0;
  1304.  
  1305.     for (;;) {
  1306.     /* Handle transport armor stripping */
  1307.     tempf = tempfile(0);
  1308.     strip_spaces = FALSE;    /* de_armor_file() sets
  1309.                    this for clear signature files */
  1310.     status = de_armor_file(armorfile, tempf, &linepos);
  1311.     if (status) {
  1312.         fprintf(pgpout,
  1313. LANG("\n\007Error: Transport armor stripping failed for file %s\n"),
  1314.             armorfile);
  1315.         errorLvl = INVALID_FILE_ERROR;
  1316.         user_error();    /* Bad file */
  1317.     }
  1318.     if (keepctx || de_armor_only) {
  1319.         if (outputfile && de_armor_only) {
  1320.         if (strcmp(outputfile, "-") == 0) {
  1321.             writePhantomOutput(tempf);
  1322.             rmtemp(tempf);
  1323.             return;
  1324.         }
  1325.         strcpy(cipherfile, outputfile);
  1326.         } else {
  1327.         strcpy(cipherfile, file_tail(armorfile));
  1328.         force_extension(cipherfile, PGP_EXTENSION);
  1329.         }
  1330.         if ((tempf = savetemp(tempf, cipherfile)) == NULL) {
  1331.         errorLvl = UNKNOWN_FILE_ERROR;
  1332.         user_error();
  1333.         }
  1334.         if (!quietmode)
  1335.         fprintf(pgpout,
  1336. LANG("Stripped transport armor from '%s', producing '%s'.\n"),
  1337.             armorfile, tempf);
  1338.         /* -da flag: don't decrypt */
  1339.         if (de_armor_only || do_decrypt(tempf) >= 0)
  1340.         ++success;
  1341.     } else {
  1342.         if (do_decrypt(tempf) >= 0)
  1343.         ++success;
  1344.         rmtemp(tempf);
  1345.     }
  1346.  
  1347.     if (!is_armor_file(armorfile, linepos)) {
  1348.         if (!success)    /* print error msg if we didn't
  1349.                    decrypt anything */
  1350.         user_error();
  1351.         return;
  1352.     }
  1353.     fprintf(pgpout,
  1354.         LANG("\nLooking for next packet in '%s'...\n"), armorfile);
  1355.     }
  1356. }                /* do_armorfile */
  1357.  
  1358.  
  1359. static int do_decrypt(char *cipherfile)
  1360. {
  1361.     char *outfile = NULL;
  1362.     int status, i;
  1363.     boolean nested_info = FALSE;
  1364.     char ringfile[MAX_PATH];
  1365.     byte ctb;
  1366.     byte header[8];        /* used to classify file type at the end. */
  1367.     char preserved_name[MAX_PATH];
  1368.     char *newname;
  1369.  
  1370.     /* will be set to the original file name after processing a
  1371.        literal packet */
  1372.     preserved_name[0] = '\0';
  1373.  
  1374.     do {            /* while nested parsable info present */
  1375.     if (nested_info) {
  1376.         rmtemp(cipherfile);    /* never executed on first pass */
  1377.         cipherfile = outfile;
  1378.     }
  1379.     if (get_header_info_from_file(cipherfile, &ctb, 1) < 0) {
  1380.         fprintf(pgpout,
  1381. LANG("\n\007Can't open ciphertext file '%s'\n"), cipherfile);
  1382.         errorLvl = FILE_NOT_FOUND_ERROR;
  1383.         return -1;
  1384.     }
  1385.     if (!is_ctb(ctb))    /* not a real CTB -- complain */
  1386.         break;
  1387.  
  1388.     if (moreflag)
  1389.         outfile = tempfile(TMP_WIPE | TMP_TMPDIR);
  1390.     else
  1391.         outfile = tempfile(TMP_WIPE);
  1392.  
  1393.     /* PKE is Public Key Encryption */
  1394.     if (is_ctb_type(ctb, CTB_PKE_TYPE)) {
  1395.  
  1396.         if (!quietmode)
  1397.         fprintf(pgpout,
  1398. LANG("\nFile is encrypted.  Secret key is required to read it. "));
  1399.  
  1400.         /* Decrypt to scratch file since we may have a LITERAL2 */
  1401.         status = decryptfile(cipherfile, outfile);
  1402.  
  1403.         if (status < 0) {    /* error return */
  1404.         errorLvl = RSA_DECR_ERROR;
  1405.         return -1;
  1406.         }
  1407.         nested_info = (status > 0);
  1408.  
  1409.     } else if (is_ctb_type(ctb, CTB_SKE_TYPE)) {
  1410.  
  1411.         if (decrypt_only_flag) {
  1412.         /* swap file names instead of just copying the file */
  1413.         rmtemp(outfile);
  1414.         outfile = cipherfile;
  1415.         cipherfile = NULL;
  1416.         if (!quietmode)
  1417.             fprintf(pgpout,
  1418. LANG("\nThis file has a signature, which will be left in place.\n"));
  1419.         nested_info = FALSE;
  1420.         break;        /* Do no more */
  1421.         }
  1422.         if (!quietmode)
  1423.         fprintf(pgpout,
  1424. LANG("\nFile has signature.  Public key is required to check signature. "));
  1425.  
  1426.         status = check_signaturefile(cipherfile, outfile,
  1427.                      strip_sig_flag, preserved_name);
  1428.  
  1429.         if (status < 0) {    /* error return */
  1430.         errorLvl = SIGNATURE_CHECK_ERROR;
  1431.         return -1;
  1432.         }
  1433.         nested_info = (status > 0);
  1434.  
  1435.         if (strcmp(preserved_name, "/dev/null") == 0) {
  1436.         rmtemp(outfile);
  1437.         fprintf(pgpout, "\n");
  1438.         return 0;
  1439.         }
  1440.     } else if (is_ctb_type(ctb, CTB_CKE_TYPE)) {
  1441.  
  1442.         /* Conventional Key Encrypted ciphertext. */
  1443.         /* Tell user it's encrypted here, and prompt for
  1444.            password in subroutine. */
  1445.         if (!quietmode)
  1446.         fprintf(pgpout, LANG("\nFile is conventionally encrypted.  "));
  1447.         /* Decrypt to scratch file since it may be a LITERAL2 */
  1448.         status = idea_decryptfile(cipherfile, outfile);
  1449.         if (status < 0) {    /* error return */
  1450.         errorLvl = DECR_ERROR;
  1451.         return -1;    /* error exit status */
  1452.         }
  1453.         nested_info = (status > 0);
  1454.  
  1455.     } else if (is_ctb_type(ctb, CTB_COMPRESSED_TYPE)) {
  1456.  
  1457.         /* Compressed text. */
  1458.         status = decompress_file(cipherfile, outfile);
  1459.         if (status < 0) {    /* error return */
  1460.         errorLvl = DECOMPRESS_ERROR;
  1461.         return -1;
  1462.         }
  1463.         /* Always assume nested information... */
  1464.         nested_info = TRUE;
  1465.  
  1466.     } else if (is_ctb_type(ctb, CTB_LITERAL_TYPE) ||
  1467.            is_ctb_type(ctb, CTB_LITERAL2_TYPE)) { /* Raw plaintext.
  1468.                                  Just copy it.
  1469.                                  No more nesting.
  1470.                                */
  1471.  
  1472.         /* Strip off CTB_LITERAL prefix byte from file: */
  1473.         /* strip_literal may alter plainfile; will set mode */
  1474.         status = strip_literal(cipherfile, outfile,
  1475.                    preserved_name, &literal_mode);
  1476.         if (status < 0) {    /* error return */
  1477.         errorLvl = UNKNOWN_FILE_ERROR;
  1478.         return -1;
  1479.         }
  1480.         nested_info = FALSE;
  1481.     } else if (ctb == CTB_CERT_SECKEY || ctb == CTB_CERT_PUBKEY) {
  1482.  
  1483.         rmtemp(outfile);
  1484.         if (decrypt_only_flag) {
  1485.         /* swap file names instead of just copying the file */
  1486.         outfile = cipherfile;
  1487.         cipherfile = NULL;
  1488.         nested_info = FALSE;    /* No error */
  1489.         break;        /* no further processing */
  1490.         }
  1491.         /* Key ring.  View it. */
  1492.         fprintf(pgpout,
  1493. LANG("\nFile contains key(s).  Contents follow..."));
  1494.         if (view_keyring(NULL, cipherfile, TRUE, FALSE) < 0) {
  1495.         errorLvl = KEYRING_VIEW_ERROR;
  1496.         return -1;
  1497.         }
  1498.         /* filter mode explicit requested with -f */
  1499.         if (filter_mode && !preserve_filename)
  1500.         return 0;    /*    No output file */
  1501.         if (batchmode)
  1502.         return 0;
  1503.         if (ctb == CTB_CERT_SECKEY)
  1504.         strcpy(ringfile, globalSecringName);
  1505.         else
  1506.         strcpy(ringfile, globalPubringName);
  1507.         /*      Ask if it should be put on key ring */
  1508.         fprintf(pgpout,
  1509. LANG("\nDo you want to add this keyfile to keyring '%s' (y/N)? "), ringfile);
  1510.         if (!getyesno('n'))
  1511.         return 0;
  1512.         status = addto_keyring(cipherfile, ringfile);
  1513.         if (status < 0) {
  1514.         fprintf(pgpout, LANG("\007Keyring add error. "));
  1515.         errorLvl = KEYRING_ADD_ERROR;
  1516.         return -1;
  1517.         }
  1518.         return 0;        /*    No output file */
  1519.  
  1520.     } else {        /* Unrecognized CTB */
  1521.         break;
  1522.     }
  1523.  
  1524.     } while (nested_info);
  1525.     /* No more nested parsable information */
  1526.  
  1527.     /* Stopped early due to error */
  1528.     if (nested_info) {
  1529.     fprintf(pgpout,
  1530. "\7\nERROR: Nested data has unexpected format.  CTB=0x%02X\n", ctb);
  1531.     if (outfile)
  1532.         rmtemp(outfile);
  1533.     if (cipherfile)
  1534.         rmtemp(cipherfile);
  1535.     errorLvl = UNKNOWN_FILE_ERROR;
  1536.     return -1;
  1537.     }
  1538.     if (outfile == NULL) {    /* file was not encrypted */
  1539.     if (!filter_mode && !moreflag) {
  1540.         fprintf(pgpout,
  1541. LANG("\007\nError: '%s' is not a ciphertext, signature, or key file.\n"),
  1542.             cipherfile);
  1543.         errorLvl = UNKNOWN_FILE_ERROR;
  1544.         return -1;
  1545.     }
  1546.     outfile = cipherfile;
  1547.     } else {
  1548.     if (cipherfile)
  1549.         rmtemp(cipherfile);
  1550.     }
  1551.  
  1552.     if (moreflag || (strcmp(preserved_name, CONSOLE_FILENAME) == 0)) {
  1553.     /* blort to screen */
  1554.     if (strcmp(preserved_name, CONSOLE_FILENAME) == 0) {
  1555.         fprintf(pgpout,
  1556. LANG("\n\nThis message is marked \"For your eyes only\".  Display now \
  1557. (Y/n)? "));
  1558.         if (batchmode || !getyesno('y')) {
  1559.         /* no -- abort display, and clean up */
  1560.         rmtemp(outfile);
  1561.         return 0;
  1562.         }
  1563.     }
  1564.     if (!quietmode)
  1565.         fprintf(pgpout, LANG("\n\nPlaintext message follows...\n"));
  1566.     else
  1567.         putc('\n', pgpout);
  1568.     fprintf(pgpout, "------------------------------\n");
  1569.     more_file(outfile);
  1570.     /* Disallow saving to disk if outfile is console-only: */
  1571.     if (strcmp(preserved_name, CONSOLE_FILENAME) == 0) {
  1572.         clearscreen();    /* remove all evidence */
  1573.     } else if (!quietmode && !batchmode) {
  1574.         fprintf(pgpout, LANG("Save this file permanently (y/N)? "));
  1575.         if (getyesno('n')) {
  1576.         char moreFilename[256];
  1577.         fprintf(pgpout, LANG("Enter filename to save file as: "));
  1578. #ifdef AMIGA
  1579.                 requesterdesc=LANG("Enter filename to save file as: ");
  1580. #endif
  1581.                 if (preserved_name[0]) {
  1582.             fprintf(pgpout, "[%s]: ", file_tail(preserved_name));
  1583. #ifdef AMIGA
  1584.                     strcat(requesterdesc, "[");
  1585.                     strcat(requesterdesc, file_tail(preserved_name));
  1586.                     strcat(requesterdesc, "]:");
  1587. #endif
  1588.                 }
  1589.         getstring(moreFilename, 255, TRUE);
  1590.         if (*moreFilename == '\0') {
  1591.             if (*preserved_name != '\0')
  1592.             savetemp(outfile, file_tail(preserved_name));
  1593.             else
  1594.             rmtemp(outfile);
  1595.         } else
  1596.             savetemp(outfile, moreFilename);
  1597.         return 0;
  1598.         }
  1599.     }
  1600.     rmtemp(outfile);
  1601.     return 0;
  1602.     }                /* blort to screen */
  1603.     if (outputfile) {
  1604.     if (!strcmp(outputfile, "/dev/null")) {
  1605.         rmtemp(outfile);
  1606.         return 0;
  1607.     }
  1608.     filter_mode = (strcmp(outputfile, "-") == 0);
  1609.     strcpy(plainfile, outputfile);
  1610.     } else {
  1611. #ifdef VMS
  1612.     /* VMS null extension has to be ".", not "" */
  1613.     force_extension(plainfile, ".");
  1614. #else                /* not VMS */
  1615.     drop_extension(plainfile);
  1616. #endif                /* not VMS */
  1617.     }
  1618.  
  1619.     if (!preserve_filename && filter_mode) {
  1620.     if (writePhantomOutput(outfile) < 0) {
  1621.         errorLvl = UNKNOWN_FILE_ERROR;
  1622.         return -1;
  1623.     }
  1624.     rmtemp(outfile);
  1625.     return 0;
  1626.     }
  1627.     if (preserve_filename && preserved_name[0] != '\0')
  1628.     strcpy(plainfile, file_tail(preserved_name));
  1629.  
  1630.     if (quietmode) {
  1631.     if (savetemp(outfile, plainfile) == NULL) {
  1632.         errorLvl = UNKNOWN_FILE_ERROR;
  1633.         return -1;
  1634.     }
  1635.     return 0;
  1636.     }
  1637.     if (!verbose)           /* if other filename messages were suppressed */
  1638.     fprintf(pgpout, LANG("\nPlaintext filename: %s"), plainfile);
  1639.  
  1640.  
  1641. /*---------------------------------------------------------*/
  1642.  
  1643.     /*      One last thing-- let's attempt to classify some of the more
  1644.        frequently occurring cases of plaintext output files, as an
  1645.        aid to the user.
  1646.  
  1647.        For example, if output file is a public key, it should have
  1648.        the right extension on the filename.
  1649.  
  1650.        Also, it will likely be common to encrypt files created by
  1651.        various archivers, so they should be renamed with the archiver
  1652.        extension.
  1653.      */
  1654.     get_header_info_from_file(outfile, header, 8);
  1655.  
  1656.     newname = NULL;
  1657.     if (header[0] == CTB_CERT_PUBKEY) {
  1658.     /* Special case--may be public key, worth renaming */
  1659.     fprintf(pgpout,
  1660. LANG("\nPlaintext file '%s' looks like it contains a public key."),
  1661.         plainfile);
  1662.     newname = maybe_force_extension(plainfile, PGP_EXTENSION);
  1663.     }
  1664.     /* Possible public key output file */
  1665.     else if ((i = compressSignature(header)) >= 0) {
  1666.     /*      Special case--may be an archived/compressed file,
  1667.         worth renaming
  1668.     */
  1669.     fprintf(pgpout, LANG("\nPlaintext file '%s' looks like a %s file."),
  1670.         plainfile, compressName[i]);
  1671.     newname = maybe_force_extension(plainfile, compressExt[i]);
  1672.     } else if (is_ctb(header[0]) &&
  1673.            (is_ctb_type(header[0], CTB_PKE_TYPE)
  1674.         || is_ctb_type(header[0], CTB_SKE_TYPE)
  1675.         || is_ctb_type(header[0], CTB_CKE_TYPE))) {
  1676.     /* Special case--may be another ciphertext file, worth renaming */
  1677.     fprintf(pgpout,
  1678. LANG("\n\007Output file '%s' may contain more ciphertext or signature."),
  1679.         plainfile);
  1680.     newname = maybe_force_extension(plainfile, PGP_EXTENSION);
  1681.     }                /* Possible ciphertext output file */
  1682.     if (savetemp(outfile, (newname ? newname : plainfile)) == NULL) {
  1683.     errorLvl = UNKNOWN_FILE_ERROR;
  1684.     return -1;
  1685.     }
  1686.     fprintf(pgpout, "\n");
  1687.     return 0;
  1688. }                /* do_decrypt */
  1689.  
  1690. static int do_keyopt(char keychar)
  1691. {
  1692.     char keyfile[MAX_PATH];
  1693.     char ringfile[MAX_PATH];
  1694.     char *workfile;
  1695.     int status;
  1696.  
  1697.     if ((filter_mode || batchmode)
  1698.     && (keychar == 'g' || keychar == 'e' || keychar == 'd'
  1699.         || (keychar == 'r' && sign_flag))) {
  1700.     errorLvl = NO_BATCH;
  1701.     arg_error();        /* interactive process, no go in batch mode */
  1702.     }
  1703.     /*
  1704.      * If we're not doing anything that uses stdout, produce output there,
  1705.      * in case user wants to redirect it.
  1706.      */
  1707.     if (!filter_mode)
  1708.     pgpout = stdout;
  1709.  
  1710.     switch (keychar) {
  1711.  
  1712. /*-------------------------------------------------------*/
  1713.     case 'g':
  1714.     {        /*      Key generation
  1715.                Arguments: bitcount, bitcount
  1716.              */
  1717.         char keybits[6], ebits[6], *username = NULL;
  1718.  
  1719.         /*
  1720.          * Why all this code?
  1721.          *
  1722.          * Some people may object to PGP insisting on finding the
  1723.          * manual somewhere in the neighborhood to generate a key.
  1724.          * They bristle against this seemingly authoritarian
  1725.          * attitude.  Some people have even modified PGP to defeat
  1726.          * this feature, and redistributed their hotwired version to
  1727.          * others.  That creates problems for me (PRZ).
  1728.          *
  1729.          * Here is the problem.  Before I added this feature, there
  1730.          * were maimed versions of the PGP distribution package
  1731.          * floating around that lacked the manual.  One of them was
  1732.          * uploaded to Compuserve, and was distributed to countless
  1733.          * users who called me on the phone to ask me why such a
  1734.          * complicated program had no manual.  It spread out to BBS
  1735.          * systems around the country.  And a freeware distributor got
  1736.          * hold of the package from Compuserve and enshrined it on
  1737.          * CD-ROM, distributing thousands of copies without the
  1738.          * manual.  What a mess.
  1739.          *
  1740.          * Please don't make my life harder by modifying PGP to
  1741.          * disable this feature so that others may redistribute PGP
  1742.          * without the manual.  If you run PGP on a palmtop with no
  1743.          * memory for the manual, is it too much to ask that you type
  1744.          * one little extra word on the command line to do a key
  1745.          * generation, a command that is seldom used by people who
  1746.          * already know how to use PGP?  If you can't stand even this
  1747.          * trivial inconvenience, can you suggest a better method of
  1748.          * reducing PGP's distribution without the manual?
  1749.          *
  1750.          * PLEASE DO NOT DISABLE THIS CHECK IN THE SOURCE CODE
  1751.          * WITHOUT AT LEAST CALLING PHILIP ZIMMERMANN
  1752.          * (+1 303 541-0140, or prz@acm.org) TO DISCUSS IT.
  1753.          */
  1754.         if (!nomanual && manuals_missing()) {
  1755.         char const *const *dir;
  1756.         fputs(LANG("\a\nError: PGP User's Guide not found.\n\
  1757. PGP looked for it in the following directories:\n"), pgpout);
  1758.         for (dir = manual_dirs; *dir; dir++)
  1759.             fprintf(pgpout, "\t\"%s\"\n", *dir);
  1760.         fputs(
  1761. LANG("and the doc subdirectory of each of the above.  Please put a copy of\n\
  1762. both volumes of the User's Guide in one of these directories.\n\
  1763. \n\
  1764. Under NO CIRCUMSTANCES should PGP ever be distributed without the PGP\n\
  1765. User's Guide, which is included in the standard distribution package.\n\
  1766. If you got a copy of PGP without the manual, please inform whomever you\n\
  1767. got it from that this is an incomplete package that should not be\n\
  1768. distributed further.\n\
  1769. \n\
  1770. PGP will not generate a key without finding the User's Guide.\n\
  1771. There is a simple way to override this restriction.  See the\n\
  1772. PGP User's Guide for details on how to do it.\n\
  1773. \n"), pgpout);
  1774.         return KEYGEN_ERROR;
  1775.         }
  1776.         if (myArgc > 2)
  1777.         strncpy(keybits, myArgv[2], sizeof(keybits) - 1);
  1778.         else
  1779.         keybits[0] = '\0';
  1780.  
  1781.         if (myArgc > 3)
  1782.         strncpy(ebits, myArgv[3], sizeof(ebits) - 1);
  1783.         else
  1784.         ebits[0] = '\0';
  1785.  
  1786.         /* If the -u option is given, use that username */
  1787.         if (u_flag && my_name != NULL && *my_name != '\0')
  1788.         username = my_name;
  1789.  
  1790.         /* dokeygen writes the keys out to the key rings... */
  1791.         status = dokeygen(keybits, ebits, username);
  1792.  
  1793.         if (status < 0) {
  1794.         fprintf(pgpout, LANG("\007Keygen error. "));
  1795.         errorLvl = KEYGEN_ERROR;
  1796.         }
  1797.         return status;
  1798.     }            /* Key generation */
  1799.  
  1800. /*-------------------------------------------------------*/
  1801.     case 'c':
  1802.     {            /*      Key checking
  1803.                    Arguments: userid, ringfile
  1804.                  */
  1805.  
  1806.         if (myArgc < 3) {    /* Default to all user ID's */
  1807.         mcguffin[0] = '\0';
  1808.         } else {
  1809.         strcpy(mcguffin, myArgv[2]);
  1810.         if (strcmp(mcguffin, "*") == 0)
  1811.             mcguffin[0] = '\0';
  1812.         }
  1813.         CONVERT_TO_CANONICAL_CHARSET(mcguffin);
  1814.  
  1815.         if (myArgc < 4)    /* default key ring filename */
  1816.         strcpy(ringfile, globalPubringName);
  1817.         else
  1818.         strncpy(ringfile, myArgv[3], sizeof(ringfile) - 1);
  1819.  
  1820.         if ((myArgc < 4 && myArgc > 2)     /* Allow just key file as arg */
  1821.         &&has_extension(myArgv[2], PGP_EXTENSION)) {
  1822.         strcpy(ringfile, myArgv[2]);
  1823.         mcguffin[0] = '\0';
  1824.         }
  1825.         status = dokeycheck(mcguffin, ringfile, CHECK_ALL);
  1826.  
  1827.         if (status < 0) {
  1828.         fprintf(pgpout, LANG("\007Keyring check error.\n"));
  1829.         errorLvl = KEYRING_CHECK_ERROR;
  1830.         }
  1831.         if (status >= 0 && mcguffin[0] != '\0')
  1832.         return status;    /* just checking a single user,
  1833.                    dont do maintenance */
  1834.  
  1835.         if ((status = maint_check(ringfile, 0)) < 0 && status != -7) {
  1836.         fprintf(pgpout, LANG("\007Maintenance pass error. "));
  1837.         errorLvl = KEYRING_CHECK_ERROR;
  1838.         }
  1839.         return status == -7 ? 0 : status;
  1840.     }            /* Key check */
  1841.  
  1842. /*-------------------------------------------------------*/
  1843.     case 'm':
  1844.     {            /*      Maintenance pass
  1845.                    Arguments: ringfile
  1846.                  */
  1847.  
  1848.         if (myArgc < 3)    /* default key ring filename */
  1849.         strcpy(ringfile, globalPubringName);
  1850.         else
  1851.         strcpy(ringfile, myArgv[2]);
  1852.  
  1853. #ifdef MSDOS
  1854.         strlwr(ringfile);
  1855. #endif
  1856.         if (!file_exists(ringfile))
  1857.         default_extension(ringfile, PGP_EXTENSION);
  1858.  
  1859.         if ((status = maint_check(ringfile,
  1860.               MAINT_VERBOSE | (c_flag ? MAINT_CHECK : 0))) < 0) {
  1861.         if (status == -7)
  1862.             fprintf(pgpout,
  1863.                 LANG("File '%s' is not a public keyring\n"),
  1864.                 ringfile);
  1865.         fprintf(pgpout, LANG("\007Maintenance pass error. "));
  1866.         errorLvl = KEYRING_CHECK_ERROR;
  1867.         }
  1868.         return status;
  1869.     }            /* Maintenance pass */
  1870.  
  1871. /*-------------------------------------------------------*/
  1872.     case 's':
  1873.     {            /*      Key signing
  1874.                    Arguments: her_id, keyfile
  1875.                  */
  1876.  
  1877.         if (myArgc >= 4)
  1878.         strncpy(keyfile, myArgv[3], sizeof(keyfile) - 1);
  1879.         else
  1880.         strcpy(keyfile, globalPubringName);
  1881.  
  1882.         if (myArgc >= 3) {
  1883.         strcpy(mcguffin, myArgv[2]);    /* Userid to sign */
  1884.         } else {
  1885.         fprintf(pgpout,
  1886. LANG("\nA user ID is required to select the public key you want to sign. "));
  1887.         if (batchmode)    /* not interactive, userid
  1888.                    must be on command line */
  1889.             return -1;
  1890.         fprintf(pgpout, LANG("\nEnter the public key's user ID: "));
  1891. #ifdef AMIGA
  1892.                 requesterdesc=LANG("\nEnter the public key's user ID: ");
  1893. #endif
  1894.         getstring(mcguffin, 255, TRUE);        /* echo keyboard */
  1895.         }
  1896.         CONVERT_TO_CANONICAL_CHARSET(mcguffin);
  1897.  
  1898.         if (my_name[0] == '\0') {
  1899.         fprintf(pgpout,
  1900. LANG("\nA secret key is required to make a signature. "));
  1901.         fprintf(pgpout,
  1902. LANG("\nYou specified no user ID to select your secret key,\n\
  1903. so the default user ID and key will be the most recently\n\
  1904. added key on your secret keyring.\n"));
  1905.         }
  1906.         status = signkey(mcguffin, my_name, keyfile);
  1907.  
  1908.         if (status >= 0) {
  1909.         status = maint_update(keyfile, 0);
  1910.         if (status == -7) { /* ringfile is a keyfile or
  1911.                        secret keyring */
  1912.             fprintf(pgpout,
  1913.                 "Warning: '%s' is not a public keyring\n",
  1914.                 keyfile);
  1915.             return 0;
  1916.         }
  1917.         if (status < 0)
  1918.             fprintf(pgpout, LANG("\007Maintenance pass error. "));
  1919.         }
  1920.         if (status < 0) {
  1921.         fprintf(pgpout, LANG("\007Key signature error. "));
  1922.         errorLvl = KEY_SIGNATURE_ERROR;
  1923.         }
  1924.         return status;
  1925.     }            /* Key signing */
  1926.  
  1927.  
  1928. /*-------------------------------------------------------*/
  1929.     case 'd':
  1930.     {            /*      disable/revoke key
  1931.                    Arguments: userid, keyfile
  1932.                  */
  1933.  
  1934.         if (myArgc >= 4)
  1935.         strncpy(keyfile, myArgv[3], sizeof(keyfile) - 1);
  1936.         else
  1937.         strcpy(keyfile, globalPubringName);
  1938.  
  1939.         if (myArgc >= 3) {
  1940.         strcpy(mcguffin, myArgv[2]);    /* Userid to sign */
  1941.         } else {
  1942.         fprintf(pgpout,
  1943. LANG("\nA user ID is required to select the key you want to revoke or \
  1944. disable. "));
  1945.         fprintf(pgpout, LANG("\nEnter user ID: "));
  1946. #ifdef AMIGA
  1947.                 requesterdesc=LANG("\nEnter user ID: ");
  1948. #endif
  1949.         getstring(mcguffin, 255, TRUE);        /* echo keyboard */
  1950.         }
  1951.         CONVERT_TO_CANONICAL_CHARSET(mcguffin);
  1952.  
  1953.         status = disable_key(mcguffin, keyfile);
  1954.  
  1955.         if (status >= 0) {
  1956.         status = maint_update(keyfile, 0);
  1957.         if (status == -7) { /* ringfile is a keyfile or
  1958.                        secret keyring */
  1959.             fprintf(pgpout, "Warning: '%s' is not a public keyring\n",
  1960.                 keyfile);
  1961.             return 0;
  1962.         }
  1963.         if (status < 0)
  1964.             fprintf(pgpout, LANG("\007Maintenance pass error. "));
  1965.         }
  1966.         if (status < 0)
  1967.         errorLvl = KEY_SIGNATURE_ERROR;
  1968.         return status;
  1969.     }            /* Key compromise */
  1970.  
  1971. /*-------------------------------------------------------*/
  1972.     case 'e':
  1973.     {            /*      Key editing
  1974.                    Arguments: userid, ringfile
  1975.                  */
  1976.  
  1977.         if (myArgc >= 4)
  1978.         strncpy(ringfile, myArgv[3], sizeof(ringfile) - 1);
  1979.         else        /* default key ring filename */
  1980.         strcpy(ringfile, globalPubringName);
  1981.  
  1982.         if (myArgc >= 3) {
  1983.         strcpy(mcguffin, myArgv[2]);    /* Userid to edit */
  1984.         } else {
  1985.         fprintf(pgpout,
  1986. LANG("\nA user ID is required to select the key you want to edit. "));
  1987.         fprintf(pgpout, LANG("\nEnter the key's user ID: "));
  1988. #ifdef AMIGA
  1989.                 requesterdesc=LANG("\nEnter the key's user ID: ");
  1990. #endif
  1991.         getstring(mcguffin, 255, TRUE);        /* echo keyboard */
  1992.         }
  1993.         CONVERT_TO_CANONICAL_CHARSET(mcguffin);
  1994.  
  1995.         status = dokeyedit(mcguffin, ringfile);
  1996.  
  1997.         if (status >= 0) {
  1998.         status = maint_update(ringfile, 0);
  1999.         if (status == -7)
  2000.             status = 0;    /* ignore "not a public keyring" error */
  2001.         if (status < 0)
  2002.             fprintf(pgpout, LANG("\007Maintenance pass error. "));
  2003.         }
  2004.         if (status < 0) {
  2005.         fprintf(pgpout, LANG("\007Keyring edit error. "));
  2006.         errorLvl = KEYRING_EDIT_ERROR;
  2007.         }
  2008.         return status;
  2009.     }            /* Key edit */
  2010.  
  2011. /*-------------------------------------------------------*/
  2012.     case 'a':
  2013.     {            /*      Add key to key ring
  2014.                    Arguments: keyfile, ringfile
  2015.                  */
  2016.  
  2017.         if (myArgc < 3 && !filter_mode)
  2018.         arg_error();
  2019.  
  2020.         if (!filter_mode) {    /* Get the keyfile from args */
  2021.         strncpy(keyfile, myArgv[2], sizeof(keyfile) - 1);
  2022.  
  2023. #ifdef MSDOS
  2024.         strlwr(keyfile);
  2025. #endif
  2026.         if (!file_exists(keyfile))
  2027.             default_extension(keyfile, PGP_EXTENSION);
  2028.  
  2029.         if (!file_exists(keyfile)) {
  2030.             fprintf(pgpout,
  2031.                 LANG("\n\007Key file '%s' does not exist.\n"),
  2032.                 keyfile);
  2033.             errorLvl = NONEXIST_KEY_ERROR;
  2034.             return -1;
  2035.         }
  2036.         workfile = keyfile;
  2037.  
  2038.         } else {
  2039.         workfile = tempfile(TMP_WIPE | TMP_TMPDIR);
  2040.         readPhantomInput(workfile);
  2041.         }
  2042.  
  2043.         if (myArgc < (filter_mode ? 3 : 4)) { /* default key ring
  2044.                              filename */
  2045.         byte ctb;
  2046.         get_header_info_from_file(workfile, &ctb, 1);
  2047.         if (ctb == CTB_CERT_SECKEY)
  2048.             strcpy(ringfile, globalSecringName);
  2049.         else
  2050.             strcpy(ringfile, globalPubringName);
  2051.         } else {
  2052.         strncpy(ringfile, myArgv[(filter_mode ? 2 : 3)],
  2053.             sizeof(ringfile) - 1);
  2054.         default_extension(ringfile, PGP_EXTENSION);
  2055.         }
  2056. #ifdef MSDOS
  2057.         strlwr(ringfile);
  2058. #endif
  2059.  
  2060.         status = addto_keyring(workfile, ringfile);
  2061.  
  2062.         if (filter_mode)
  2063.         rmtemp(workfile);
  2064.  
  2065.         if (status < 0) {
  2066.         fprintf(pgpout, LANG("\007Keyring add error. "));
  2067.         errorLvl = KEYRING_ADD_ERROR;
  2068.         }
  2069.         return status;
  2070.     }            /* Add key to key ring */
  2071.  
  2072. /*-------------------------------------------------------*/
  2073.     case 'x':
  2074.     {            /*      Extract key from key ring
  2075.                    Arguments: mcguffin, keyfile, ringfile
  2076.                  */
  2077.  
  2078.         if (myArgc >= (filter_mode ? 4 : 5)) /* default key ring
  2079.                             filename */
  2080.         strncpy(ringfile, myArgv[(filter_mode ? 3 : 4)],
  2081.             sizeof(ringfile) - 1);
  2082.         else
  2083.         strcpy(ringfile, globalPubringName);
  2084.  
  2085.         if (myArgc >= (filter_mode ? 2 : 3)) {
  2086.         if (myArgv[2])
  2087.             /* Userid to extract */
  2088.             strcpy(mcguffin, myArgv[2]);
  2089.         else
  2090.             strcpy(mcguffin, "");
  2091.         } else {
  2092.         fprintf(pgpout,
  2093. LANG("\nA user ID is required to select the key you want to extract. "));
  2094.         if (batchmode)    /* not interactive, userid
  2095.                    must be on command line */
  2096.             return -1;
  2097.         fprintf(pgpout, LANG("\nEnter the key's user ID: "));
  2098. #ifdef AMIGA
  2099.                 requesterdesc=LANG("\nEnter the key's user ID: ");
  2100. #endif
  2101.         getstring(mcguffin, 255, TRUE);        /* echo keyboard */
  2102.         }
  2103.         CONVERT_TO_CANONICAL_CHARSET(mcguffin);
  2104.  
  2105.         if (!filter_mode) {
  2106.         if (myArgc >= 4)
  2107.             strncpy(keyfile, myArgv[3], sizeof(keyfile) - 1);
  2108.         else
  2109.             keyfile[0] = '\0';
  2110.  
  2111.         workfile = keyfile;
  2112.         } else {
  2113.         workfile = tempfile(TMP_WIPE | TMP_TMPDIR);
  2114.         }
  2115.  
  2116. #ifdef MSDOS
  2117.         strlwr(workfile);
  2118.         strlwr(ringfile);
  2119. #endif
  2120.  
  2121.         default_extension(ringfile, PGP_EXTENSION);
  2122.  
  2123.         status = extract_from_keyring(mcguffin, workfile,
  2124.                       ringfile, (filter_mode ? FALSE :
  2125.                              emit_radix_64));
  2126.  
  2127.         if (status < 0) {
  2128.         fprintf(pgpout, LANG("\007Keyring extract error. "));
  2129.         errorLvl = KEYRING_EXTRACT_ERROR;
  2130.         if (filter_mode)
  2131.             rmtemp(workfile);
  2132.         return status;
  2133.         }
  2134.         if (filter_mode && !status) {
  2135.         if (emit_radix_64) {
  2136.             /* NULL for outputfile means write to stdout */
  2137.             if (armor_file(workfile, NULL, NULL, NULL) != 0) {
  2138.             errorLvl = UNKNOWN_FILE_ERROR;
  2139.             return -1;
  2140.             }
  2141.         } else {
  2142.             if (writePhantomOutput(workfile) < 0) {
  2143.             errorLvl = UNKNOWN_FILE_ERROR;
  2144.             return -1;
  2145.             }
  2146.         }
  2147.         rmtemp(workfile);
  2148.         }
  2149.         return 0;
  2150.     }            /* Extract key from key ring */
  2151.  
  2152. /*-------------------------------------------------------*/
  2153.     case 'r':
  2154.     {    /*      Remove keys or selected key signatures from userid keys
  2155.             Arguments: userid, ringfile
  2156.          */
  2157.  
  2158.         if (myArgc >= 4)
  2159.         strcpy(ringfile, myArgv[3]);
  2160.         else        /* default key ring filename */
  2161.         strcpy(ringfile, globalPubringName);
  2162.  
  2163.         if (myArgc >= 3) {
  2164.         strcpy(mcguffin, myArgv[2]);    /* Userid to work on */
  2165.         } else {
  2166.         if (sign_flag) {
  2167.             fprintf(pgpout,
  2168. LANG("\nA user ID is required to select the public key you want to\n\
  2169. remove certifying signatures from. "));
  2170.         } else {
  2171.             fprintf(pgpout,
  2172. LANG("\nA user ID is required to select the key you want to remove. "));
  2173.         }
  2174.         if (batchmode)    /* not interactive, userid must be on
  2175.                    command line */
  2176.             return -1;
  2177.         fprintf(pgpout, LANG("\nEnter the key's user ID: "));
  2178. #ifdef AMIGA
  2179.                 requesterdesc=LANG("\nEnter the key's user ID: ");
  2180. #endif
  2181.         getstring(mcguffin, 255, TRUE);        /* echo keyboard */
  2182.         }
  2183.         CONVERT_TO_CANONICAL_CHARSET(mcguffin);
  2184.  
  2185. #ifdef MSDOS
  2186.         strlwr(ringfile);
  2187. #endif
  2188.         if (!file_exists(ringfile))
  2189.         default_extension(ringfile, PGP_EXTENSION);
  2190.  
  2191.         if (sign_flag) {    /* Remove signatures */
  2192.         if (remove_sigs(mcguffin, ringfile) < 0) {
  2193.             fprintf(pgpout, LANG("\007Key signature remove error. "));
  2194.             errorLvl = KEYSIG_REMOVE_ERROR;
  2195.             return -1;
  2196.         }
  2197.         } else {        /* Remove keyring */
  2198.         if (remove_from_keyring(NULL, mcguffin, ringfile,
  2199.                     (boolean) (myArgc < 4)) < 0) {
  2200.             fprintf(pgpout, LANG("\007Keyring remove error. "));
  2201.             errorLvl = KEYRING_REMOVE_ERROR;
  2202.             return -1;
  2203.         }
  2204.         }
  2205.         return 0;
  2206.     }            /* remove key signatures from userid */
  2207.  
  2208. /*-------------------------------------------------------*/
  2209.     case 'v':
  2210.     case 'V':            /* -kvv */
  2211.     {            /* View or remove key ring entries,
  2212.                    with userid match
  2213.                    Arguments: userid, ringfile
  2214.                  */
  2215.  
  2216.         if (myArgc < 4)    /* default key ring filename */
  2217.         strcpy(ringfile, globalPubringName);
  2218.         else
  2219.         strcpy(ringfile, myArgv[3]);
  2220.  
  2221.         if (myArgc > 2) {
  2222.         strcpy(mcguffin, myArgv[2]);
  2223.         if (strcmp(mcguffin, "*") == 0)
  2224.             mcguffin[0] = '\0';
  2225.         } else {
  2226.         *mcguffin = '\0';
  2227.         }
  2228.  
  2229.         if ((myArgc == 3) && has_extension(myArgv[2], PGP_EXTENSION)) {
  2230.         strcpy(ringfile, myArgv[2]);
  2231.         mcguffin[0] = '\0';
  2232.         }
  2233.         CONVERT_TO_CANONICAL_CHARSET(mcguffin);
  2234.  
  2235. #ifdef MSDOS
  2236.         strlwr(ringfile);
  2237. #endif
  2238.         if (!file_exists(ringfile))
  2239.         default_extension(ringfile, PGP_EXTENSION);
  2240.  
  2241.         /* If a second 'v' (keychar = V), show signatures too */
  2242.         status = view_keyring(mcguffin, ringfile,
  2243.                   (boolean) (keychar == 'V'), c_flag);
  2244.         if (status < 0) {
  2245.         fprintf(pgpout, LANG("\007Keyring view error. "));
  2246.         errorLvl = KEYRING_VIEW_ERROR;
  2247.         }
  2248.         return status;
  2249.     }            /* view key ring entries, with userid match */
  2250.  
  2251.     default:
  2252.     arg_error();
  2253.     }
  2254.     return 0;
  2255. }                /* do_keyopt */
  2256.  
  2257. /* comes here if user made a boo-boo. */
  2258. void user_error()
  2259. {
  2260.     fprintf(pgpout, LANG("\nFor a usage summary, type:  pgp -h\n"));
  2261.     fprintf(pgpout,
  2262.         LANG("For more detailed help, consult the PGP User's Guide.\n"));
  2263.     exitPGP(errorLvl ? errorLvl : 127);        /* error exit */
  2264. }
  2265.  
  2266. #if defined(DEBUG) && defined(linux)
  2267. #include <malloc.h>
  2268. #endif
  2269.  
  2270. /*
  2271.  * exitPGP: wipes and removes temporary files, also tries to wipe
  2272.  * the stack.
  2273.  */
  2274. void exitPGP(int returnval)
  2275. {
  2276.     char buf[STACK_WIPE];
  2277.     struct hashedpw *hpw;
  2278.  
  2279.     if (verbose)
  2280.     fprintf(pgpout, "exitPGP: exitcode = %d\n", returnval);
  2281.     for (hpw = passwds; hpw; hpw = hpw->next)
  2282.     memset(hpw->hash, 0, sizeof(hpw->hash));
  2283.     for (hpw = keypasswds; hpw; hpw = hpw->next)
  2284.     memset(hpw->hash, 0, sizeof(hpw->hash));
  2285.     cleanup_tmpf();
  2286.     /* Merge any entropy we collected into the randseed.bin file */
  2287.     if (cryptRandOpen((struct IdeaCfbContext *)0) >= 0)
  2288.         cryptRandSave((struct IdeaCfbContext *)0);
  2289. #if defined(DEBUG) && defined(linux)
  2290.     if (verbose) {
  2291.     struct mstats mstat;
  2292.     mstat = mstats();
  2293.     printf("%d chunks used (%d bytes)  %d bytes total\n",
  2294.            mstat.chunks_used, mstat.bytes_used, mstat.bytes_total);
  2295.     }
  2296. #endif
  2297.     memset(buf, 0, sizeof(buf));    /* wipe stack */
  2298. #ifdef VMS
  2299. /*
  2300.  * Fake VMS style error returns with severity in bottom 3 bits
  2301.  */
  2302.     if (returnval)
  2303.     returnval = (returnval << 3) | 0x10000002;
  2304.     else
  2305.     returnval = 0x10000001;
  2306. #endif                /* VMS */
  2307.     exit(returnval);
  2308. }
  2309.  
  2310. static void arg_error()
  2311. {
  2312.     signon_msg();
  2313.     fprintf(pgpout, LANG("\nInvalid arguments.\n"));
  2314.     errorLvl = BAD_ARG_ERROR;
  2315.     user_error();
  2316. }
  2317.  
  2318. /*
  2319.  * Check for language specific help files in PGPPATH, then the system
  2320.  * directory.  If that fails, check for the default pgp.hlp, again
  2321.  * firat a private copy, then the system-wide one.
  2322.  *
  2323.  * System-wide copies currently only exist on Unix.
  2324.  */
  2325. static void build_helpfile(char *helpfile, char const *extra)
  2326. {
  2327.     if (strcmp(language, "en")) {
  2328.     buildfilename(helpfile, language);
  2329.     strcat(helpfile, extra);
  2330.     force_extension(helpfile, HLP_EXTENSION);
  2331.     if (file_exists(helpfile))
  2332.         return;
  2333. #ifdef PGP_SYSTEM_DIR
  2334.     strcpy(helpfile, PGP_SYSTEM_DIR);
  2335.     strcat(helpfile, language);
  2336.     strcat(helpfile, extra);
  2337.     force_extension(helpfile, HLP_EXTENSION);
  2338.     if (file_exists(helpfile))
  2339.         return;
  2340. #endif
  2341.     }
  2342.     buildfilename(helpfile, "pgp");
  2343.     strcat(helpfile, extra);
  2344.     force_extension(helpfile, HLP_EXTENSION);
  2345. #ifdef PGP_SYSTEM_DIR
  2346.     if (file_exists(helpfile))
  2347.     return;
  2348.     strcpy(helpfile, PGP_SYSTEM_DIR);
  2349.     strcat(helpfile, "pgp");
  2350.     strcat(helpfile, extra);
  2351.     force_extension(helpfile, HLP_EXTENSION);
  2352. #endif
  2353. }
  2354.  
  2355. static void usage()
  2356. {
  2357.     char helpfile[MAX_PATH];
  2358.     char *tmphelp = helpfile;
  2359.     extern unsigned char *ext_c_ptr;
  2360.  
  2361.     signon_msg();
  2362.     build_helpfile(helpfile, "");
  2363.  
  2364.     if (ext_c_ptr) {
  2365.     /* conversion to external format necessary */
  2366.     tmphelp = tempfile(TMP_TMPDIR);
  2367.     CONVERSION = EXT_CONV;
  2368.     if (copyfiles_by_name(helpfile, tmphelp) < 0) {
  2369.         rmtemp(tmphelp);
  2370.         tmphelp = helpfile;
  2371.     }
  2372.     CONVERSION = NO_CONV;
  2373.     }
  2374.     /* built-in help if pgp.hlp is not available */
  2375.     if (more_file(tmphelp) < 0)
  2376.     fprintf(pgpout, LANG("\nUsage summary:\
  2377. \nTo encrypt a plaintext file with recipent's public key, type:\
  2378. \n   pgp -e textfile her_userid [other userids] (produces textfile.pgp)\
  2379. \nTo sign a plaintext file with your secret key:\
  2380. \n   pgp -s textfile [-u your_userid]           (produces textfile.pgp)\
  2381. \nTo sign a plaintext file with your secret key, and then encrypt it\
  2382. \n   with recipent's public key, producing a .pgp file:\
  2383. \n   pgp -es textfile her_userid [other userids] [-u your_userid]\
  2384. \nTo encrypt with conventional encryption only:\
  2385. \n   pgp -c textfile\
  2386. \nTo decrypt or check a signature for a ciphertext (.pgp) file:\
  2387. \n   pgp ciphertextfile [plaintextfile]\
  2388. \nTo produce output in ASCII for email, add the -a option to other options.\
  2389. \nTo generate your own unique public/secret key pair:  pgp -kg\
  2390. \nFor help on other key management functions, type:   pgp -k\n"));
  2391.     if (ext_c_ptr)
  2392.     rmtemp(tmphelp);
  2393.     exit(BAD_ARG_ERROR);    /* error exit */
  2394. }
  2395.  
  2396. static void key_usage()
  2397. {
  2398.     char helpfile[MAX_PATH];
  2399.     char *tmphelp = helpfile;
  2400.     extern unsigned char *ext_c_ptr;
  2401.  
  2402.     signon_msg();
  2403.     build_helpfile(helpfile, "key");
  2404.  
  2405.     if (ext_c_ptr) {
  2406.     /* conversion to external format necessary */
  2407.     tmphelp = tempfile(TMP_TMPDIR);
  2408.     CONVERSION = EXT_CONV;
  2409.     if (copyfiles_by_name(helpfile, tmphelp) < 0) {
  2410.         rmtemp(tmphelp);
  2411.         tmphelp = helpfile;
  2412.     }
  2413.     CONVERSION = NO_CONV;
  2414.     }
  2415.     /* built-in help if pgp.hlp is not available */
  2416.     if (more_file(tmphelp) < 0)
  2417.     /* only use built-in help if there is no helpfile */
  2418.     fprintf(pgpout, LANG("\nKey management functions:\
  2419. \nTo generate your own unique public/secret key pair:\
  2420. \n   pgp -kg\
  2421. \nTo add a key file's contents to your public or secret key ring:\
  2422. \n   pgp -ka keyfile [keyring]\
  2423. \nTo remove a key or a user ID from your public or secret key ring:\
  2424. \n   pgp -kr userid [keyring]\
  2425. \nTo edit your user ID or pass phrase:\
  2426. \n   pgp -ke your_userid [keyring]\
  2427. \nTo extract (copy) a key from your public or secret key ring:\
  2428. \n   pgp -kx userid keyfile [keyring]\
  2429. \nTo view the contents of your public key ring:\
  2430. \n   pgp -kv[v] [userid] [keyring]\
  2431. \nTo check signatures on your public key ring:\
  2432. \n   pgp -kc [userid] [keyring]\
  2433. \nTo sign someone else's public key on your public key ring:\
  2434. \n   pgp -ks her_userid [-u your_userid] [keyring]\
  2435. \nTo remove selected signatures from a userid on a keyring:\
  2436. \n   pgp -krs userid [keyring]\
  2437. \n"));
  2438.  
  2439.     exit(BAD_ARG_ERROR);    /* error exit */
  2440. }
  2441.