Files
pcp/src/pcp.c

674 lines
15 KiB
C
Raw Normal View History

2013-11-04 17:43:22 +01:00
/*
This file is part of Pretty Curved Privacy (pcp1).
2015-05-30 11:11:20 +02:00
Copyright (C) 2013-2015 T.Linden.
2013-11-04 17:43:22 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You can contact me by mail: <tlinden AT cpan DOT org>.
*/
2013-10-28 22:50:05 +01:00
#include "pcp.h"
#include "defines.h"
void usage(int error) {
2013-10-28 22:50:05 +01:00
fprintf(stderr, PCP_HELP_INTRO);
if(error == 0)
fprintf(stderr, PCP_HELP);
2013-10-28 22:50:05 +01:00
version();
exit(EXIT_FAILURE);
}
2013-10-28 22:50:05 +01:00
void version() {
fprintf(stderr, "pcp version %d.%d.%d, use --help to learn how to use.\n",
2013-10-28 22:50:05 +01:00
PCP_VERSION_MAJOR, PCP_VERSION_MINOR, PCP_VERSION_PATCH);
exit(0);
}
char *default_vault() {
char *path = ucmalloc(1024);;
snprintf(path, 1024, "%s/.pcpvault", getenv("HOME"));
return path;
}
2015-05-30 11:11:20 +02:00
char *altin(char *infile, int stdinused) {
if(infile == NULL && stdinused == 1) {
fprintf(stderr, "Error: cannot use <stdin> because -X had precedence!\n");
exit(1);
}
return infile;
}
2013-10-28 22:50:05 +01:00
int main (int argc, char **argv) {
2015-05-30 11:11:20 +02:00
int opt, mode, usevault, useid, userec, lo, armor, detach, \
signcrypt, exportformat, anon, xpf;
2013-10-28 22:50:05 +01:00
char *vaultfile = default_vault();
char *outfile = NULL;
char *infile = NULL;
char *sigfile = NULL;
2013-10-28 22:50:05 +01:00
char *keyid = NULL;
char *id = NULL;
char *xpass = NULL;
2015-05-30 11:11:20 +02:00
char *xpassfile = NULL;
2014-02-07 11:55:44 +01:00
char *extra = NULL;
2014-01-20 16:07:01 +01:00
plist_t *recipient = NULL;
2015-07-30 11:46:17 +02:00
char *askpass = NULL;
2013-10-28 22:50:05 +01:00
FILE *in;
errno = 0;
debug = 0;
mode = 0;
usevault = 0;
useid = 0;
userec = 0;
2013-11-18 21:48:24 +01:00
lo = 0;
armor = 0;
detach = 0;
2014-01-27 16:12:43 +01:00
signcrypt = 0;
anon = 0;
2015-05-30 11:11:20 +02:00
xpf = 0;
exportformat = EXP_FORMAT_NATIVE;
ptx = ptx_new();
2014-03-02 18:04:54 +01:00
2013-10-28 22:50:05 +01:00
static struct option longopts[] = {
/* generics */
{ "vault", required_argument, NULL, 'V' },
{ "outfile", required_argument, NULL, 'O' },
{ "infile", required_argument, NULL, 'I' },
{ "keyid", required_argument, NULL, 'i' },
{ "text", required_argument, NULL, 't' },
{ "xpass", required_argument, NULL, 'x' },
2015-05-30 11:11:20 +02:00
{ "password-file", required_argument, NULL, 'X' },
2015-07-30 11:46:17 +02:00
{ "extpass", required_argument, NULL, LONG_EXTPASS },
{ "recipient", required_argument, NULL, 'r' },
2013-10-28 22:50:05 +01:00
/* key management */
{ "keygen", no_argument, NULL, 'k' },
{ "listkeys", no_argument, NULL, 'l' },
2014-03-02 18:04:54 +01:00
{ "listkeys-verbose",no_argument, NULL, 'L' }, /* alias for -l -v */
{ "export-secret", no_argument, NULL, 's' },
{ "export-public", no_argument, NULL, 'p' },
2014-03-02 18:04:54 +01:00
{ "export", no_argument, NULL, 'p' }, /* alias -p */
{ "import", no_argument, NULL, 'K' }, /* alias -P */
{ "import-key", no_argument, NULL, 'K' }, /* alias -K */
{ "remove-key", no_argument, NULL, 'R' },
{ "edit-key", no_argument, NULL, 'E' },
{ "export-format", required_argument, NULL, 'F' },
2013-10-28 22:50:05 +01:00
/* crypto */
2014-03-02 18:04:54 +01:00
{ "encrypt", no_argument, NULL, 'e' },
{ "encrypt-sym", no_argument, NULL, 'm' },
2014-03-02 18:04:54 +01:00
{ "decrypt", no_argument, NULL, 'd' },
{ "anonymous", no_argument, NULL, 'A' },
{ "add-myself", no_argument, NULL, 'M' },
2015-07-22 07:59:28 +02:00
{ "checksum", no_argument, NULL, 'C' },
2013-10-28 22:50:05 +01:00
/* encoding */
2014-03-02 18:04:54 +01:00
{ "z85-encode", no_argument, NULL, 'z' },
{ "armor", no_argument, NULL, 'a' }, /* alias -z */
{ "textmode", no_argument, NULL, 'a' }, /* alias -z */
{ "z85-decode", no_argument, NULL, 'Z' },
{ "json-io", no_argument, NULL, 'j' },
2013-10-28 22:50:05 +01:00
/* globals */
{ "help", no_argument, NULL, 'h' },
2014-03-02 18:04:54 +01:00
{ "version", no_argument, NULL, '0' }, /* no short opt, FIXME: how to avoid? */
{ "verbose", no_argument, NULL, 'v' },
{ "debug", no_argument, NULL, 'D' },
/* signing */
{ "sign", no_argument, NULL, 'g' },
{ "check-signature", no_argument, NULL, 'c' },
{ "sigfile", required_argument, NULL, 'f' },
{ NULL, 0, NULL, 0 }
2013-10-28 22:50:05 +01:00
};
2015-07-22 07:59:28 +02:00
while ((opt = getopt_long(argc, argv, "klLV:vdehsO:i:I:pSPRtEx:DzaZr:gcmf:b1F:0KAMX:jC",
2013-10-28 22:50:05 +01:00
longopts, NULL)) != -1) {
2013-10-28 22:50:05 +01:00
switch (opt) {
case 0:
switch(lo) {
case 's':
printf("sign\n");
break;
}
break;
2013-10-28 22:50:05 +01:00
case 'k':
mode += PCP_MODE_KEYGEN;
usevault = 1;
break;
2014-03-02 18:04:54 +01:00
case 'L':
ptx->verbose = 1; /* no break by purpose, turn on -l */
2013-10-28 22:50:05 +01:00
case 'l':
mode += PCP_MODE_LISTKEYS;
usevault = 1;
break;
case 's':
mode += PCP_MODE_EXPORT_SECRET;
usevault = 1;
break;
case 'p':
mode += PCP_MODE_EXPORT_PUBLIC;
usevault = 1;
break;
case 'K':
mode += PCP_MODE_IMPORT;
2013-10-28 22:50:05 +01:00
usevault = 1;
break;
case 'R':
2013-10-28 22:50:05 +01:00
mode += PCP_MODE_DELETE_KEY;
usevault = 1;
break;
case 't':
mode += PCP_MODE_TEXT;
2013-11-18 21:48:24 +01:00
usevault = 0;
2013-10-28 22:50:05 +01:00
break;
case 'E':
mode += PCP_MODE_EDIT;
usevault = 1;
break;
case 'e':
mode += PCP_MODE_ENCRYPT;
usevault = 1;
break;
case 'm':
mode += PCP_MODE_ENCRYPT_ME;
break;
2013-10-28 22:50:05 +01:00
case 'd':
mode += PCP_MODE_DECRYPT;
usevault = 1;
break;
case 'z':
2014-03-02 18:04:54 +01:00
case 'a':
armor = 1;
2013-10-28 22:50:05 +01:00
break;
case 'Z':
2014-02-09 15:49:52 +01:00
armor = 2;
2013-10-28 22:50:05 +01:00
break;
case 'A':
anon = 1;
break;
case 'F':
if(strncmp(optarg, "pbp", 3) == 0) {
exportformat = EXP_FORMAT_PBP;
}
else if(strncmp(optarg, "pcp", 3) == 0) {
exportformat = EXP_FORMAT_NATIVE;
}
else {
warn("Unknown export format specified, using native\n");
exportformat = EXP_FORMAT_NATIVE;
}
break;
case 'j':
#ifdef HAVE_JSON
ptx->json = 1;
#else
fprintf(stderr, "WARN: -j set, but no JSON support compiled in. Recompile with --with-json\n");
#endif
break;
case 'g':
mode += PCP_MODE_SIGN;
usevault = 1;
break;
case 'c':
mode += PCP_MODE_VERIFY;
usevault = 1;
break;
case 'C':
mode += PCP_MODE_CHECKSUM;
break;
case 'f':
sigfile = ucmalloc(strlen(optarg)+1);
strncpy(sigfile, optarg, strlen(optarg)+1);
detach = 1;
break;
2013-10-28 22:50:05 +01:00
case 'V':
strncpy(vaultfile, optarg, 1024);
break;
case 'O':
2014-03-02 18:04:54 +01:00
if(strncmp(optarg, "-", 2) > 0) {
outfile = ucmalloc(strlen(optarg)+1);
strncpy(outfile, optarg, strlen(optarg)+1);
}
2013-10-28 22:50:05 +01:00
break;
case 'I':
2014-03-02 18:04:54 +01:00
if(strncmp(optarg, "-", 2) > 0) {
infile = ucmalloc(strlen(optarg)+1);
strncpy(infile, optarg, strlen(optarg)+1);
}
2013-10-28 22:50:05 +01:00
break;
2015-05-30 11:11:20 +02:00
case 'X':
xpassfile = ucmalloc(strlen(optarg)+1);
strncpy(xpassfile, optarg, strlen(optarg)+1);
xpf = 1;
break;
2013-10-28 22:50:05 +01:00
case 'i':
keyid = ucmalloc(19);
strncpy(keyid, optarg, 19);
useid = 1;
break;
case 'x':
2015-05-30 11:11:20 +02:00
xpass = smalloc(strlen(optarg)+1);
2013-10-28 22:50:05 +01:00
strncpy(xpass, optarg, strlen(optarg)+1);
if(strncmp(xpass, "n/a", 3) == 0)
xpass[0] = '\0';
2013-10-28 22:50:05 +01:00
break;
2015-07-30 11:46:17 +02:00
case LONG_EXTPASS:
askpass = malloc(strlen(optarg)+1);
strncpy(askpass, optarg, strlen(optarg)+1);
break;
case 'r':
2014-01-20 16:07:01 +01:00
p_add(&recipient, optarg);
userec = 1;
break;
case 'M':
p_add_me(&recipient);
userec = 1;
break;
2013-10-28 22:50:05 +01:00
case 'D':
debug = 1;
break;
2014-03-02 18:04:54 +01:00
case '0':
2013-10-28 22:50:05 +01:00
version();
2014-03-02 18:04:54 +01:00
case 'v':
ptx->verbose = 1;
2014-03-02 18:04:54 +01:00
break;
2013-10-28 22:50:05 +01:00
case 'h':
usage(0);
2013-10-28 22:50:05 +01:00
default:
usage(1);
2013-10-28 22:50:05 +01:00
}
}
argc -= optind;
argv += optind;
if(mode == 0) {
2014-02-09 15:49:52 +01:00
/* turn -z|-Z into a mode if there's nothing else specified */
if(armor == 1) {
mode = PCP_MODE_ZENCODE;
}
else if(armor == 2) {
mode = PCP_MODE_ZDECODE;
}
else {
version();
return 1;
}
}
2014-01-27 16:12:43 +01:00
if(mode == PCP_MODE_ENCRYPT + PCP_MODE_SIGN) {
mode = PCP_MODE_ENCRYPT;
signcrypt = 1;
}
if(mode == PCP_MODE_DECRYPT + PCP_MODE_VERIFY) {
mode = PCP_MODE_DECRYPT;
signcrypt = 1;
}
sodium_init(); /* FIXME: better called from the lib? */
#ifndef DEBUG
# ifdef HAVE_SETRLIMIT
setrlimit(RLIMIT_CORE, &(struct rlimit) {0, 0});
# endif
#endif
errno = 0; /* FIXME: workaround for https://github.com/jedisct1/libsodium/issues/114 */
if(mode == PCP_MODE_ENCRYPT && useid == 0 && userec == 0) {
usevault = 0;
mode = PCP_MODE_ENCRYPT_ME;
}
2014-02-07 11:55:44 +01:00
if(argc >= 1) {
/* ok, there are arguments left on the commandline.
treat it as filename or recipient, depending on
current mode and other given parameters */
extra = ucmalloc(strlen(argv[0])+1);
strncpy(extra, argv[0], strlen(argv[0])+1);
2014-08-06 01:23:32 +02:00
int useex = 0;
2014-02-07 11:55:44 +01:00
switch (mode) {
case PCP_MODE_DECRYPT:
2014-08-06 01:23:32 +02:00
if(infile == NULL) {
2014-02-07 11:55:44 +01:00
infile = extra;
2014-08-06 01:23:32 +02:00
useex = 1;
}
2014-02-07 11:55:44 +01:00
break;
case PCP_MODE_ENCRYPT:
2014-08-06 01:23:32 +02:00
if(infile == NULL) {
2014-02-07 11:55:44 +01:00
infile = extra;
2014-08-06 01:23:32 +02:00
useex = 1;
}
2014-02-07 11:55:44 +01:00
else if(userec == 0 && useid == 0) {
userec = 1;
int i;
for (i=0; i<argc; i++) {
p_add(&recipient, argv[i]);
}
}
break;
case PCP_MODE_IMPORT:
2014-08-06 01:23:32 +02:00
if(infile == NULL) {
2014-02-07 11:55:44 +01:00
infile = extra;
2014-08-06 01:23:32 +02:00
useex = 1;
}
2014-02-07 11:55:44 +01:00
break;
case PCP_MODE_EXPORT_SECRET:
case PCP_MODE_EXPORT_PUBLIC:
2014-08-06 01:23:32 +02:00
if(outfile == NULL) {
2014-02-07 11:55:44 +01:00
outfile = extra;
2014-08-06 01:23:32 +02:00
useex = 1;
}
2014-02-07 11:55:44 +01:00
else if(useid == 0 && userec == 0) {
p_add(&recipient, extra);
2014-08-06 01:23:32 +02:00
useex = 1;
2014-02-07 11:55:44 +01:00
userec = 1;
}
break;
case PCP_MODE_VERIFY:
2014-08-06 01:23:32 +02:00
if(infile == NULL) {
2014-02-07 11:55:44 +01:00
infile = extra;
2014-08-06 01:23:32 +02:00
useex = 1;
}
2014-02-07 11:55:44 +01:00
else if (useid == 0) {
id = extra;
useid = 1;
2014-08-06 01:23:32 +02:00
useex = 1;
2014-02-07 11:55:44 +01:00
}
break;
case PCP_MODE_SIGN:
2014-08-06 01:23:32 +02:00
if(infile == NULL) {
2014-02-07 11:55:44 +01:00
infile = extra;
2014-08-06 01:23:32 +02:00
useex = 1;
}
else if(outfile == NULL && detach == 0) {
2014-02-07 11:55:44 +01:00
outfile = extra;
2014-08-06 01:23:32 +02:00
useex = 1;
}
2014-02-07 11:55:44 +01:00
break;
}
2014-08-06 01:23:32 +02:00
if(useex)
free(extra);
2014-02-07 11:55:44 +01:00
}
2015-05-30 11:11:20 +02:00
if(xpassfile != NULL) {
2015-07-30 11:46:17 +02:00
if(pcp_readpass(ptx, &xpass, "passphrase", NULL, 0, xpassfile) != 0)
goto perr1;
2015-05-30 11:11:20 +02:00
if(xpassfile[0] != '-')
xpf = 0;
free(xpassfile);
}
2015-07-30 11:46:17 +02:00
if(askpass != NULL) {
if(pcp_readpass_fromprog(ptx, &xpass, askpass) != 0)
goto perr1;
}
2014-02-07 11:55:44 +01:00
/* check if there's some enviroment we could use */
if(usevault == 1) {
char *_vaultfile = getenv("PCP_VAULT");
if(_vaultfile != NULL) {
strncpy(vaultfile, _vaultfile, strlen(_vaultfile)+1);
}
}
if(debug == 0) {
char *_debug = getenv("PCP_DEBUG");
if(_debug != NULL) {
debug = 1;
}
}
2013-10-28 22:50:05 +01:00
if(usevault == 1) {
vault = pcpvault_init(ptx, vaultfile);
/* special case: ignore vault error in decrypt mode. sym decrypt doesn't
need it and asym will just fail without keys. */
if(vault == NULL && mode == PCP_MODE_DECRYPT) {
/* use an empty one */
vault = pcpvault_init(ptx, "/dev/null");
fatals_reset(ptx);
}
2013-10-28 22:50:05 +01:00
if(vault != NULL) {
switch (mode) {
case PCP_MODE_KEYGEN:
pcp_keygen(xpass);
2013-10-28 22:50:05 +01:00
break;
case PCP_MODE_LISTKEYS:
pcp_listkeys();
break;
case PCP_MODE_EXPORT_SECRET:
if(useid) {
id = pcp_normalize_id(keyid);
if(id != NULL) {
pcp_exportsecret(id, useid, outfile, armor, xpass);
2013-10-28 22:50:05 +01:00
}
}
else {
pcp_exportsecret(NULL, useid, outfile, armor, xpass);
2013-10-28 22:50:05 +01:00
}
break;
case PCP_MODE_EXPORT_PUBLIC:
if(useid) {
id = pcp_normalize_id(keyid);
if(id == NULL)
break;
2013-10-28 22:50:05 +01:00
}
pcp_exportpublic(id, xpass, outfile, exportformat, armor);
2013-10-28 22:50:05 +01:00
break;
case PCP_MODE_IMPORT:
2015-05-30 11:11:20 +02:00
if(infile == NULL) {
altin(NULL, xpf);
2013-10-28 22:50:05 +01:00
in = stdin;
2015-05-30 11:11:20 +02:00
}
2013-10-28 22:50:05 +01:00
else {
if((in = fopen(infile, "rb")) == NULL) {
fatal(ptx, "Could not open input file %s\n", infile);
2013-10-28 22:50:05 +01:00
break;
}
}
pcp_import(vault, in, xpass);
2013-10-28 22:50:05 +01:00
break;
case PCP_MODE_DELETE_KEY:
if(useid) {
id = pcp_normalize_id(keyid);
if(id != NULL) {
pcpdelete_key(id);
}
}
else {
fatal(ptx, "You need to specify a key id (--keyid)!\n");
2013-10-28 22:50:05 +01:00
}
break;
case PCP_MODE_EDIT:
if(useid) {
id = pcp_normalize_id(keyid);
if(id != NULL) {
pcpedit_key(id);
}
}
else {
fatal(ptx, "You need to specify a key id (--keyid)!\n");
2013-10-28 22:50:05 +01:00
}
break;
case PCP_MODE_ENCRYPT:
2014-01-20 16:07:01 +01:00
if(useid == 1 && userec == 0) {
/* one dst, FIXME: make id a list as well */
2013-10-28 22:50:05 +01:00
id = pcp_normalize_id(keyid);
2015-05-30 11:11:20 +02:00
pcpencrypt(id, altin(infile, xpf), outfile, xpass, NULL, signcrypt, armor, anon);
}
2014-01-20 16:07:01 +01:00
else if(useid == 0 && userec == 1) {
/* multiple dst */
2015-05-30 11:11:20 +02:00
pcpencrypt(NULL, altin(infile, xpf), outfile, xpass, recipient, signcrypt, armor, anon);
}
2013-10-28 22:50:05 +01:00
else {
/* -i and -r specified */
fatal(ptx, "You can't specify both -i and -r, use either -i or -r!\n");
2013-10-28 22:50:05 +01:00
}
2014-01-20 16:07:01 +01:00
2013-10-28 22:50:05 +01:00
break;
case PCP_MODE_DECRYPT:
if(useid) {
id = pcp_normalize_id(keyid);
if(id != NULL) {
2015-05-30 11:11:20 +02:00
pcpdecrypt(id, useid, altin(infile, xpf), outfile, xpass, signcrypt);
2013-10-28 22:50:05 +01:00
}
}
else {
2015-05-30 11:11:20 +02:00
pcpdecrypt(NULL, useid, altin(infile, xpf), outfile, xpass, signcrypt);
2013-10-28 22:50:05 +01:00
}
break;
case PCP_MODE_SIGN:
if(detach) {
if(outfile != NULL && sigfile != NULL)
fatal(ptx, "You can't both specify -O and -f, use -O for std signatures and -f for detached ones\n");
else
2015-05-30 11:11:20 +02:00
pcpsign(altin(infile, xpf), sigfile, xpass, armor, detach);
}
else
2015-05-30 11:11:20 +02:00
pcpsign(altin(infile, xpf), outfile, xpass, armor, detach);
break;
case PCP_MODE_VERIFY:
if(useid) {
id = pcp_normalize_id(keyid);
if(id != NULL) {
2015-05-30 11:11:20 +02:00
pcpverify(altin(infile, xpf), sigfile, id, detach);
}
}
else {
2015-05-30 11:11:20 +02:00
pcpverify(altin(infile, xpf), sigfile, NULL, detach);
}
break;
2013-10-28 22:50:05 +01:00
default:
/* */
2013-10-28 22:50:05 +01:00
goto ELSEMODE;
break;
}
pcpvault_close(ptx, vault);
2013-10-28 22:50:05 +01:00
}
}
else {
ELSEMODE:
switch (mode) {
case PCP_MODE_ZENCODE:
pcpz85_encode(infile, outfile);
break;
case PCP_MODE_ZDECODE:
pcpz85_decode(infile, outfile);
break;
case PCP_MODE_ENCRYPT_ME:
2015-05-30 11:11:20 +02:00
pcpencrypt(NULL, altin(infile, xpf), outfile, xpass, NULL, 0, armor, 0);
break;
case PCP_MODE_TEXT:
if(infile != NULL) {
pcptext_infile(infile);
}
else {
vault = pcpvault_init(ptx, vaultfile);
if(! useid && infile == NULL) {
pcptext_vault(vault);
2013-11-18 21:48:24 +01:00
}
else {
id = pcp_normalize_id(keyid);
if(id != NULL) {
pcptext_key(id);
2013-11-18 21:48:24 +01:00
}
}
pcpvault_close(ptx, vault);
}
break;
case PCP_MODE_CHECKSUM:
if(infile == NULL) {
if(argc == 0) {
char *list[1];
list[0] = NULL;
2015-07-21 14:18:03 +02:00
pcpchecksum(list, 1, xpass);
}
else {
2015-07-21 14:18:03 +02:00
pcpchecksum(argv, argc, xpass);
}
}
else {
char *list[1];
list[0] = infile;
2015-07-21 14:18:03 +02:00
pcpchecksum(list, 1, xpass);
}
break;
2013-10-28 22:50:05 +01:00
default:
/* mode params mixed */
fatal(ptx, "Sorry, invalid combination of commandline parameters (0x%04X)!\n", mode);
2013-10-28 22:50:05 +01:00
break;
}
}
2015-07-30 11:46:17 +02:00
perr1:
fatals_ifany(ptx);
int e = ptx->pcp_exit;
ptx_clean(ptx);
2014-08-06 01:23:32 +02:00
if(infile != NULL)
free(infile);
if(outfile != NULL)
free(outfile);
if(vaultfile != NULL)
free(vaultfile);
if(sigfile != NULL)
free(sigfile);
if(xpass != NULL)
2015-05-30 11:11:20 +02:00
sfree(xpass);
2015-07-30 11:46:17 +02:00
if(askpass != NULL)
free(askpass);
if(xpassfile != NULL)
free(xpassfile);
2014-08-06 01:23:32 +02:00
if(recipient != NULL)
p_clean(recipient);
if(id != NULL)
free(id);
2014-08-07 21:33:52 +02:00
if(keyid != NULL)
free(keyid);
return e;
2013-10-28 22:50:05 +01:00
}