2013-11-04 17:43:22 +01:00
|
|
|
/*
|
|
|
|
|
This file is part of Pretty Curved Privacy (pcp1).
|
|
|
|
|
|
2016-05-09 22:24:13 +02:00
|
|
|
Copyright (C) 2013-2016 T.v.Dein.
|
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>.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-03-10 17:00:31 +01:00
|
|
|
#define _GNU_SOURCE /* vasprintf() linux */
|
2013-11-04 17:43:22 +01:00
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
#include "context.h"
|
2014-03-10 17:00:31 +01:00
|
|
|
|
2013-10-28 22:50:05 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
PCPCTX *ptx_new() {
|
|
|
|
|
PCPCTX *p = ucmalloc(sizeof(PCPCTX));
|
|
|
|
|
p->pcp_err = NULL;
|
|
|
|
|
p->pcp_errset = 0;
|
|
|
|
|
p->pcp_exit = 0;
|
|
|
|
|
p->verbose = 0;
|
2015-07-06 23:02:04 +02:00
|
|
|
#ifdef HAVE_JSON
|
|
|
|
|
p->json = 0;
|
|
|
|
|
#endif
|
2014-05-04 17:11:03 +02:00
|
|
|
p->pcpkey_hash = NULL;
|
|
|
|
|
p->pcppubkey_hash = NULL;
|
|
|
|
|
p->pcpkeysig_hash = NULL;
|
2013-12-01 16:15:41 +01:00
|
|
|
|
2015-11-15 17:02:51 +01:00
|
|
|
if(sodium_init() == -1) {
|
|
|
|
|
perror("failed to initialize libsodium");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2015-04-18 20:26:31 +02:00
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ptx_clean(PCPCTX *ptx) {
|
2014-05-06 11:49:59 +02:00
|
|
|
if(ptx->pcp_errset) {
|
|
|
|
|
if(ptx->pcp_err != NULL)
|
|
|
|
|
free(ptx->pcp_err);
|
|
|
|
|
|
|
|
|
|
}
|
2014-05-04 17:11:03 +02:00
|
|
|
pcphash_clean(ptx);
|
|
|
|
|
|
|
|
|
|
free(ptx);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 11:49:59 +02:00
|
|
|
void ptx_dump(PCPCTX *ptx) {
|
|
|
|
|
fprintf(stderr, "ERRSET: %d\n", ptx->pcp_errset);
|
|
|
|
|
if(ptx->pcp_errset)
|
|
|
|
|
fprintf(stderr, " ERR: %s\n", ptx->pcp_err);
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, " sk:\n");
|
|
|
|
|
pcp_key_t *k = NULL;
|
|
|
|
|
pcphash_iterate(ptx, k) {
|
|
|
|
|
fprintf(stderr, " id: 0x%s\n", k->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, " pk:\n");
|
|
|
|
|
pcp_pubkey_t *p = NULL;
|
|
|
|
|
pcphash_iteratepub(ptx, p) {
|
|
|
|
|
fprintf(stderr, " id: 0x%s\n", p->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2014-05-04 17:11:03 +02:00
|
|
|
|
|
|
|
|
void fatal(PCPCTX *ptx, const char * fmt, ...) {
|
2013-10-28 22:50:05 +01:00
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, fmt);
|
2014-05-04 17:11:03 +02:00
|
|
|
|
2014-05-05 12:00:56 +02:00
|
|
|
char *err = NULL;//ptx->pcp_err;
|
2013-10-28 22:50:05 +01:00
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
if(vasprintf(&err, fmt, ap) >= 0) {
|
2014-03-10 17:00:31 +01:00
|
|
|
va_end(ap);
|
2014-05-04 17:11:03 +02:00
|
|
|
ptx->pcp_errset = 1;
|
2014-05-05 12:00:56 +02:00
|
|
|
if(ptx->pcp_err != NULL) {
|
|
|
|
|
free(ptx->pcp_err);
|
|
|
|
|
}
|
|
|
|
|
ptx->pcp_err = err;
|
2014-03-10 17:00:31 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
fprintf(stderr, "Could not store fatal error message %s!\n", fmt);
|
2014-05-04 17:11:03 +02:00
|
|
|
ptx->pcp_errset = 1;
|
2014-03-10 17:00:31 +01:00
|
|
|
}
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
void fatals_reset(PCPCTX *ptx) {
|
|
|
|
|
ptx->pcp_errset = 0;
|
2014-08-07 00:02:43 +02:00
|
|
|
if(ptx->pcp_err != NULL) {
|
|
|
|
|
free(ptx->pcp_err);
|
|
|
|
|
ptx->pcp_err = NULL;
|
|
|
|
|
}
|
2013-11-02 11:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
void fatals_ifany(PCPCTX *ptx) {
|
|
|
|
|
if(ptx->pcp_errset == 1) {
|
|
|
|
|
fprintf(stderr, "%s", ptx->pcp_err);
|
2013-10-28 22:50:05 +01:00
|
|
|
if(errno) {
|
|
|
|
|
fprintf(stderr, "Error: %s\n", strerror(errno));
|
|
|
|
|
}
|
2014-05-04 17:11:03 +02:00
|
|
|
ptx->pcp_exit = 1;
|
2013-10-28 22:50:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-02-13 20:19:32 +01:00
|
|
|
|
2014-05-04 17:11:03 +02:00
|
|
|
void final(const char * fmt, ...) {
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
|
|
char *err = NULL;
|
|
|
|
|
|
|
|
|
|
if(vasprintf(&err, fmt, ap) >= 0) {
|
|
|
|
|
va_end(ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "ABORT: %s", err);
|
|
|
|
|
|
|
|
|
|
abort();
|
2014-02-13 20:19:32 +01:00
|
|
|
}
|