2017-04-28 03:06:33 +02:00
|
|
|
|
/* wks-utils.c - Common helper functions for wks tools
|
2016-07-03 00:41:30 +02:00
|
|
|
|
* Copyright (C) 2016 g10 Code GmbH
|
2017-06-19 12:37:52 +02:00
|
|
|
|
* Copyright (C) 2016 Bundesamt für Sicherheit in der Informationstechnik
|
2016-07-03 00:41:30 +02:00
|
|
|
|
*
|
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
|
*
|
2017-06-19 12:37:52 +02:00
|
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
|
* the License, or (at your option) any later version.
|
2016-07-03 00:41:30 +02:00
|
|
|
|
*
|
2017-06-19 12:37:52 +02:00
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2016-07-03 00:41:30 +02:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-19 12:37:52 +02:00
|
|
|
|
* GNU Lesser General Public License for more details.
|
2016-07-03 00:41:30 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2017-03-07 12:21:23 +01:00
|
|
|
|
#include "../common/util.h"
|
|
|
|
|
#include "../common/status.h"
|
|
|
|
|
#include "../common/ccparray.h"
|
|
|
|
|
#include "../common/exectool.h"
|
|
|
|
|
#include "../common/mbox-util.h"
|
2016-07-03 00:41:30 +02:00
|
|
|
|
#include "mime-maker.h"
|
|
|
|
|
#include "send-mail.h"
|
|
|
|
|
#include "gpg-wks.h"
|
|
|
|
|
|
2016-12-08 17:55:36 +01:00
|
|
|
|
/* The stream to output the status information. Output is disabled if
|
|
|
|
|
this is NULL. */
|
|
|
|
|
static estream_t statusfp;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Set the status FD. */
|
|
|
|
|
void
|
|
|
|
|
wks_set_status_fd (int fd)
|
|
|
|
|
{
|
|
|
|
|
static int last_fd = -1;
|
|
|
|
|
|
|
|
|
|
if (fd != -1 && last_fd == fd)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (statusfp && statusfp != es_stdout && statusfp != es_stderr)
|
|
|
|
|
es_fclose (statusfp);
|
|
|
|
|
statusfp = NULL;
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (fd == 1)
|
|
|
|
|
statusfp = es_stdout;
|
|
|
|
|
else if (fd == 2)
|
|
|
|
|
statusfp = es_stderr;
|
|
|
|
|
else
|
|
|
|
|
statusfp = es_fdopen (fd, "w");
|
|
|
|
|
if (!statusfp)
|
|
|
|
|
{
|
|
|
|
|
log_fatal ("can't open fd %d for status output: %s\n",
|
|
|
|
|
fd, gpg_strerror (gpg_error_from_syserror ()));
|
|
|
|
|
}
|
|
|
|
|
last_fd = fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Write a status line with code NO followed by the outout of the
|
|
|
|
|
* printf style FORMAT. The caller needs to make sure that LFs and
|
|
|
|
|
* CRs are not printed. */
|
|
|
|
|
void
|
|
|
|
|
wks_write_status (int no, const char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list arg_ptr;
|
|
|
|
|
|
|
|
|
|
if (!statusfp)
|
|
|
|
|
return; /* Not enabled. */
|
|
|
|
|
|
|
|
|
|
es_fputs ("[GNUPG:] ", statusfp);
|
|
|
|
|
es_fputs (get_status_string (no), statusfp);
|
|
|
|
|
if (format)
|
|
|
|
|
{
|
|
|
|
|
es_putc (' ', statusfp);
|
|
|
|
|
va_start (arg_ptr, format);
|
|
|
|
|
es_vfprintf (statusfp, format, arg_ptr);
|
|
|
|
|
va_end (arg_ptr);
|
|
|
|
|
}
|
|
|
|
|
es_putc ('\n', statusfp);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-03 00:41:30 +02:00
|
|
|
|
|
2017-09-18 11:16:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Append UID to LIST and return the new item. On success LIST is
|
|
|
|
|
* updated. On error ERRNO is set and NULL returned. */
|
|
|
|
|
static uidinfo_list_t
|
2017-09-18 11:31:36 +02:00
|
|
|
|
append_to_uidinfo_list (uidinfo_list_t *list, const char *uid, time_t created)
|
2017-09-18 11:16:07 +02:00
|
|
|
|
{
|
|
|
|
|
uidinfo_list_t r, sl;
|
|
|
|
|
|
|
|
|
|
sl = xtrymalloc (sizeof *sl + strlen (uid));
|
|
|
|
|
if (!sl)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
strcpy (sl->uid, uid);
|
2017-09-18 11:31:36 +02:00
|
|
|
|
sl->created = created;
|
2017-09-18 11:16:07 +02:00
|
|
|
|
sl->mbox = mailbox_from_userid (uid);
|
|
|
|
|
sl->next = NULL;
|
|
|
|
|
if (!*list)
|
|
|
|
|
*list = sl;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (r = *list; r->next; r = r->next )
|
|
|
|
|
;
|
|
|
|
|
r->next = sl;
|
|
|
|
|
}
|
|
|
|
|
return sl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Free the list of uid infos at LIST. */
|
|
|
|
|
void
|
|
|
|
|
free_uidinfo_list (uidinfo_list_t list)
|
|
|
|
|
{
|
|
|
|
|
while (list)
|
|
|
|
|
{
|
|
|
|
|
uidinfo_list_t tmp = list->next;
|
|
|
|
|
xfree (list->mbox);
|
|
|
|
|
xfree (list);
|
|
|
|
|
list = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-02-20 15:23:19 +01:00
|
|
|
|
|
|
|
|
|
struct get_key_status_parm_s
|
|
|
|
|
{
|
|
|
|
|
const char *fpr;
|
|
|
|
|
int found;
|
|
|
|
|
int count;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
get_key_status_cb (void *opaque, const char *keyword, char *args)
|
|
|
|
|
{
|
|
|
|
|
struct get_key_status_parm_s *parm = opaque;
|
|
|
|
|
|
|
|
|
|
/*log_debug ("%s: %s\n", keyword, args);*/
|
|
|
|
|
if (!strcmp (keyword, "EXPORTED"))
|
|
|
|
|
{
|
|
|
|
|
parm->count++;
|
|
|
|
|
if (!ascii_strcasecmp (args, parm->fpr))
|
|
|
|
|
parm->found = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get a key by fingerprint from gpg's keyring and make sure that the
|
|
|
|
|
* mail address ADDRSPEC is included in the key. If EXACT is set the
|
|
|
|
|
* returned user id must match Addrspec exactly and not just in the
|
|
|
|
|
* addr-spec (mailbox) part. The key is returned as a new memory
|
|
|
|
|
* stream at R_KEY. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
wks_get_key (estream_t *r_key, const char *fingerprint, const char *addrspec,
|
|
|
|
|
int exact)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
ccparray_t ccp;
|
|
|
|
|
const char **argv = NULL;
|
|
|
|
|
estream_t key = NULL;
|
|
|
|
|
struct get_key_status_parm_s parm;
|
|
|
|
|
char *filterexp = NULL;
|
|
|
|
|
|
|
|
|
|
memset (&parm, 0, sizeof parm);
|
|
|
|
|
|
|
|
|
|
*r_key = NULL;
|
|
|
|
|
|
|
|
|
|
key = es_fopenmem (0, "w+b");
|
|
|
|
|
if (!key)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("error allocating memory buffer: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Prefix the key with the MIME content type. */
|
|
|
|
|
es_fputs ("Content-Type: application/pgp-keys\n"
|
|
|
|
|
"\n", key);
|
|
|
|
|
|
|
|
|
|
filterexp = es_bsprintf ("keep-uid=%s=%s", exact? "uid":"mbox", addrspec);
|
|
|
|
|
if (!filterexp)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("error allocating memory buffer: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccparray_init (&ccp, 0);
|
|
|
|
|
|
|
|
|
|
ccparray_put (&ccp, "--no-options");
|
|
|
|
|
if (!opt.verbose)
|
|
|
|
|
ccparray_put (&ccp, "--quiet");
|
|
|
|
|
else if (opt.verbose > 1)
|
|
|
|
|
ccparray_put (&ccp, "--verbose");
|
|
|
|
|
ccparray_put (&ccp, "--batch");
|
|
|
|
|
ccparray_put (&ccp, "--status-fd=2");
|
|
|
|
|
ccparray_put (&ccp, "--always-trust");
|
|
|
|
|
ccparray_put (&ccp, "--armor");
|
|
|
|
|
ccparray_put (&ccp, "--export-options=export-minimal");
|
|
|
|
|
ccparray_put (&ccp, "--export-filter");
|
|
|
|
|
ccparray_put (&ccp, filterexp);
|
|
|
|
|
ccparray_put (&ccp, "--export");
|
|
|
|
|
ccparray_put (&ccp, "--");
|
|
|
|
|
ccparray_put (&ccp, fingerprint);
|
|
|
|
|
|
|
|
|
|
ccparray_put (&ccp, NULL);
|
|
|
|
|
argv = ccparray_get (&ccp, NULL);
|
|
|
|
|
if (!argv)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
parm.fpr = fingerprint;
|
|
|
|
|
err = gnupg_exec_tool_stream (opt.gpg_program, argv, NULL,
|
|
|
|
|
NULL, key,
|
|
|
|
|
get_key_status_cb, &parm);
|
|
|
|
|
if (!err && parm.count > 1)
|
|
|
|
|
err = gpg_error (GPG_ERR_TOO_MANY);
|
|
|
|
|
else if (!err && !parm.found)
|
|
|
|
|
err = gpg_error (GPG_ERR_NOT_FOUND);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("export failed: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
es_rewind (key);
|
|
|
|
|
*r_key = key;
|
|
|
|
|
key = NULL;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
es_fclose (key);
|
|
|
|
|
xfree (argv);
|
|
|
|
|
xfree (filterexp);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-12-08 16:11:42 +01:00
|
|
|
|
|
2017-09-18 12:52:20 +02:00
|
|
|
|
/* Helper for wks_list_key and wks_filter_uid. */
|
2016-12-08 16:11:42 +01:00
|
|
|
|
static void
|
2017-09-18 12:52:20 +02:00
|
|
|
|
key_status_cb (void *opaque, const char *keyword, char *args)
|
2016-12-08 16:11:42 +01:00
|
|
|
|
{
|
|
|
|
|
(void)opaque;
|
|
|
|
|
|
|
|
|
|
if (DBG_CRYPTO)
|
|
|
|
|
log_debug ("gpg status: %s %s\n", keyword, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Run gpg on KEY and store the primary fingerprint at R_FPR and the
|
|
|
|
|
* list of mailboxes at R_MBOXES. Returns 0 on success; on error NULL
|
2017-09-18 12:52:20 +02:00
|
|
|
|
* is stored at R_FPR and R_MBOXES and an error code is returned.
|
|
|
|
|
* R_FPR may be NULL if the fingerprint is not needed. */
|
2016-12-08 16:11:42 +01:00
|
|
|
|
gpg_error_t
|
2017-09-18 11:16:07 +02:00
|
|
|
|
wks_list_key (estream_t key, char **r_fpr, uidinfo_list_t *r_mboxes)
|
2016-12-08 16:11:42 +01:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
ccparray_t ccp;
|
|
|
|
|
const char **argv;
|
|
|
|
|
estream_t listing;
|
|
|
|
|
char *line = NULL;
|
|
|
|
|
size_t length_of_line = 0;
|
|
|
|
|
size_t maxlen;
|
|
|
|
|
ssize_t len;
|
|
|
|
|
char **fields = NULL;
|
|
|
|
|
int nfields;
|
|
|
|
|
int lnr;
|
|
|
|
|
char *fpr = NULL;
|
2017-09-18 11:16:07 +02:00
|
|
|
|
uidinfo_list_t mboxes = NULL;
|
2016-12-08 16:11:42 +01:00
|
|
|
|
|
2017-09-18 12:52:20 +02:00
|
|
|
|
if (r_fpr)
|
|
|
|
|
*r_fpr = NULL;
|
2016-12-08 16:11:42 +01:00
|
|
|
|
*r_mboxes = NULL;
|
|
|
|
|
|
|
|
|
|
/* Open a memory stream. */
|
|
|
|
|
listing = es_fopenmem (0, "w+b");
|
|
|
|
|
if (!listing)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("error allocating memory buffer: %s\n", gpg_strerror (err));
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccparray_init (&ccp, 0);
|
|
|
|
|
|
|
|
|
|
ccparray_put (&ccp, "--no-options");
|
|
|
|
|
if (!opt.verbose)
|
|
|
|
|
ccparray_put (&ccp, "--quiet");
|
|
|
|
|
else if (opt.verbose > 1)
|
|
|
|
|
ccparray_put (&ccp, "--verbose");
|
|
|
|
|
ccparray_put (&ccp, "--batch");
|
|
|
|
|
ccparray_put (&ccp, "--status-fd=2");
|
|
|
|
|
ccparray_put (&ccp, "--always-trust");
|
|
|
|
|
ccparray_put (&ccp, "--with-colons");
|
|
|
|
|
ccparray_put (&ccp, "--dry-run");
|
|
|
|
|
ccparray_put (&ccp, "--import-options=import-minimal,import-show");
|
|
|
|
|
ccparray_put (&ccp, "--import");
|
|
|
|
|
|
|
|
|
|
ccparray_put (&ccp, NULL);
|
|
|
|
|
argv = ccparray_get (&ccp, NULL);
|
|
|
|
|
if (!argv)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
err = gnupg_exec_tool_stream (opt.gpg_program, argv, key,
|
|
|
|
|
NULL, listing,
|
2017-09-18 12:52:20 +02:00
|
|
|
|
key_status_cb, NULL);
|
2016-12-08 16:11:42 +01:00
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("import failed: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
es_rewind (listing);
|
|
|
|
|
lnr = 0;
|
|
|
|
|
maxlen = 2048; /* Set limit. */
|
|
|
|
|
while ((len = es_read_line (listing, &line, &length_of_line, &maxlen)) > 0)
|
|
|
|
|
{
|
|
|
|
|
lnr++;
|
|
|
|
|
if (!maxlen)
|
|
|
|
|
{
|
|
|
|
|
log_error ("received line too long\n");
|
|
|
|
|
err = gpg_error (GPG_ERR_LINE_TOO_LONG);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* Strip newline and carriage return, if present. */
|
|
|
|
|
while (len > 0
|
|
|
|
|
&& (line[len - 1] == '\n' || line[len - 1] == '\r'))
|
|
|
|
|
line[--len] = '\0';
|
|
|
|
|
/* log_debug ("line '%s'\n", line); */
|
|
|
|
|
|
|
|
|
|
xfree (fields);
|
|
|
|
|
fields = strtokenize (line, ":");
|
|
|
|
|
if (!fields)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("strtokenize failed: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
for (nfields = 0; fields[nfields]; nfields++)
|
|
|
|
|
;
|
|
|
|
|
if (!nfields)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_INV_ENGINE);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (!strcmp (fields[0], "sec"))
|
|
|
|
|
{
|
|
|
|
|
/* gpg may return "sec" as the first record - but we do not
|
|
|
|
|
* accept secret keys. */
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_PUBKEY);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (lnr == 1 && strcmp (fields[0], "pub"))
|
|
|
|
|
{
|
|
|
|
|
/* First record is not a public key. */
|
|
|
|
|
err = gpg_error (GPG_ERR_INV_ENGINE);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (lnr > 1 && !strcmp (fields[0], "pub"))
|
|
|
|
|
{
|
|
|
|
|
/* More than one public key. */
|
|
|
|
|
err = gpg_error (GPG_ERR_TOO_MANY);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
if (!strcmp (fields[0], "sub") || !strcmp (fields[0], "ssb"))
|
|
|
|
|
break; /* We can stop parsing here. */
|
|
|
|
|
|
|
|
|
|
if (!strcmp (fields[0], "fpr") && nfields > 9 && !fpr)
|
|
|
|
|
{
|
|
|
|
|
fpr = xtrystrdup (fields[9]);
|
|
|
|
|
if (!fpr)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (fields[0], "uid") && nfields > 9)
|
|
|
|
|
{
|
|
|
|
|
/* Fixme: Unescape fields[9] */
|
2017-09-18 11:31:36 +02:00
|
|
|
|
if (!append_to_uidinfo_list (&mboxes, fields[9],
|
|
|
|
|
parse_timestamp (fields[5], NULL)))
|
2016-12-08 16:11:42 +01:00
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (len < 0 || es_ferror (listing))
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("error reading memory stream\n");
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 12:52:20 +02:00
|
|
|
|
if (!fpr)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_NO_PUBKEY);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (r_fpr)
|
|
|
|
|
{
|
|
|
|
|
*r_fpr = fpr;
|
|
|
|
|
fpr = NULL;
|
|
|
|
|
}
|
2016-12-08 16:11:42 +01:00
|
|
|
|
*r_mboxes = mboxes;
|
|
|
|
|
mboxes = NULL;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
xfree (fpr);
|
2017-09-18 11:16:07 +02:00
|
|
|
|
free_uidinfo_list (mboxes);
|
2016-12-08 16:11:42 +01:00
|
|
|
|
xfree (fields);
|
|
|
|
|
es_free (line);
|
|
|
|
|
xfree (argv);
|
|
|
|
|
es_fclose (listing);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-18 12:52:20 +02:00
|
|
|
|
/* Run gpg as a filter on KEY and write the output to a new stream
|
2018-02-20 11:45:58 +01:00
|
|
|
|
* stored at R_NEWKEY. The new key will contain only the user id UID.
|
|
|
|
|
* Returns 0 on success. Only one key is expected in KEY. If BINARY
|
|
|
|
|
* is set the resulting key is returned as a binary (non-armored)
|
|
|
|
|
* keyblock. */
|
2017-09-18 12:52:20 +02:00
|
|
|
|
gpg_error_t
|
2018-02-20 11:45:58 +01:00
|
|
|
|
wks_filter_uid (estream_t *r_newkey, estream_t key, const char *uid,
|
|
|
|
|
int binary)
|
2017-09-18 12:52:20 +02:00
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
ccparray_t ccp;
|
|
|
|
|
const char **argv = NULL;
|
|
|
|
|
estream_t newkey;
|
|
|
|
|
char *filterexp = NULL;
|
|
|
|
|
|
|
|
|
|
*r_newkey = NULL;
|
|
|
|
|
|
|
|
|
|
/* Open a memory stream. */
|
|
|
|
|
newkey = es_fopenmem (0, "w+b");
|
|
|
|
|
if (!newkey)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("error allocating memory buffer: %s\n", gpg_strerror (err));
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Prefix the key with the MIME content type. */
|
2018-02-20 11:45:58 +01:00
|
|
|
|
if (!binary)
|
|
|
|
|
es_fputs ("Content-Type: application/pgp-keys\n"
|
|
|
|
|
"\n", newkey);
|
2017-09-18 12:52:20 +02:00
|
|
|
|
|
|
|
|
|
filterexp = es_bsprintf ("keep-uid=uid=%s", uid);
|
|
|
|
|
if (!filterexp)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
log_error ("error allocating memory buffer: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccparray_init (&ccp, 0);
|
|
|
|
|
|
|
|
|
|
ccparray_put (&ccp, "--no-options");
|
|
|
|
|
if (!opt.verbose)
|
|
|
|
|
ccparray_put (&ccp, "--quiet");
|
|
|
|
|
else if (opt.verbose > 1)
|
|
|
|
|
ccparray_put (&ccp, "--verbose");
|
|
|
|
|
ccparray_put (&ccp, "--batch");
|
|
|
|
|
ccparray_put (&ccp, "--status-fd=2");
|
|
|
|
|
ccparray_put (&ccp, "--always-trust");
|
2018-02-20 11:45:58 +01:00
|
|
|
|
if (!binary)
|
|
|
|
|
ccparray_put (&ccp, "--armor");
|
2017-09-18 12:52:20 +02:00
|
|
|
|
ccparray_put (&ccp, "--import-options=import-export");
|
|
|
|
|
ccparray_put (&ccp, "--import-filter");
|
|
|
|
|
ccparray_put (&ccp, filterexp);
|
|
|
|
|
ccparray_put (&ccp, "--import");
|
|
|
|
|
|
|
|
|
|
ccparray_put (&ccp, NULL);
|
|
|
|
|
argv = ccparray_get (&ccp, NULL);
|
|
|
|
|
if (!argv)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
err = gnupg_exec_tool_stream (opt.gpg_program, argv, key,
|
|
|
|
|
NULL, newkey,
|
|
|
|
|
key_status_cb, NULL);
|
|
|
|
|
if (err)
|
|
|
|
|
{
|
|
|
|
|
log_error ("import/export failed: %s\n", gpg_strerror (err));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
es_rewind (newkey);
|
|
|
|
|
*r_newkey = newkey;
|
|
|
|
|
newkey = NULL;
|
|
|
|
|
|
|
|
|
|
leave:
|
|
|
|
|
xfree (filterexp);
|
|
|
|
|
xfree (argv);
|
|
|
|
|
es_fclose (newkey);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-07-03 00:41:30 +02:00
|
|
|
|
/* Helper to write mail to the output(s). */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
wks_send_mime (mime_maker_t mime)
|
|
|
|
|
{
|
|
|
|
|
gpg_error_t err;
|
|
|
|
|
estream_t mail;
|
|
|
|
|
|
|
|
|
|
/* Without any option we take a short path. */
|
|
|
|
|
if (!opt.use_sendmail && !opt.output)
|
2017-03-08 17:48:55 +01:00
|
|
|
|
{
|
|
|
|
|
es_set_binary (es_stdout);
|
|
|
|
|
return mime_maker_make (mime, es_stdout);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-03 00:41:30 +02:00
|
|
|
|
|
|
|
|
|
mail = es_fopenmem (0, "w+b");
|
|
|
|
|
if (!mail)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = mime_maker_make (mime, mail);
|
|
|
|
|
|
|
|
|
|
if (!err && opt.output)
|
|
|
|
|
{
|
|
|
|
|
es_rewind (mail);
|
|
|
|
|
err = send_mail_to_file (mail, opt.output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!err && opt.use_sendmail)
|
|
|
|
|
{
|
|
|
|
|
es_rewind (mail);
|
|
|
|
|
err = send_mail (mail);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
es_fclose (mail);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2016-09-02 16:54:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse the policy flags by reading them from STREAM and storing them
|
|
|
|
|
* into FLAGS. If IGNORE_UNKNOWN is iset unknown keywords are
|
|
|
|
|
* ignored. */
|
|
|
|
|
gpg_error_t
|
|
|
|
|
wks_parse_policy (policy_flags_t flags, estream_t stream, int ignore_unknown)
|
|
|
|
|
{
|
|
|
|
|
enum tokens {
|
2018-02-20 09:00:00 +01:00
|
|
|
|
TOK_SUBMISSION_ADDRESS,
|
2016-09-02 16:54:42 +02:00
|
|
|
|
TOK_MAILBOX_ONLY,
|
|
|
|
|
TOK_DANE_ONLY,
|
|
|
|
|
TOK_AUTH_SUBMIT,
|
2017-09-12 18:05:00 +02:00
|
|
|
|
TOK_MAX_PENDING,
|
|
|
|
|
TOK_PROTOCOL_VERSION
|
2016-09-02 16:54:42 +02:00
|
|
|
|
};
|
|
|
|
|
static struct {
|
|
|
|
|
const char *name;
|
|
|
|
|
enum tokens token;
|
|
|
|
|
} keywords[] = {
|
2018-02-20 09:00:00 +01:00
|
|
|
|
{ "submission-address", TOK_SUBMISSION_ADDRESS },
|
2016-09-02 16:54:42 +02:00
|
|
|
|
{ "mailbox-only", TOK_MAILBOX_ONLY },
|
|
|
|
|
{ "dane-only", TOK_DANE_ONLY },
|
|
|
|
|
{ "auth-submit", TOK_AUTH_SUBMIT },
|
2017-09-12 18:05:00 +02:00
|
|
|
|
{ "max-pending", TOK_MAX_PENDING },
|
|
|
|
|
{ "protocol-version", TOK_PROTOCOL_VERSION }
|
2016-09-02 16:54:42 +02:00
|
|
|
|
};
|
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
|
int lnr = 0;
|
|
|
|
|
char line[1024];
|
|
|
|
|
char *p, *keyword, *value;
|
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
|
|
memset (flags, 0, sizeof *flags);
|
|
|
|
|
|
|
|
|
|
while (es_fgets (line, DIM(line)-1, stream) )
|
|
|
|
|
{
|
|
|
|
|
lnr++;
|
|
|
|
|
n = strlen (line);
|
|
|
|
|
if (!n || line[n-1] != '\n')
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (*line? GPG_ERR_LINE_TOO_LONG
|
|
|
|
|
: GPG_ERR_INCOMPLETE_LINE);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
trim_trailing_spaces (line);
|
|
|
|
|
/* Skip empty and comment lines. */
|
|
|
|
|
for (p=line; spacep (p); p++)
|
|
|
|
|
;
|
|
|
|
|
if (!*p || *p == '#')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (*p == ':')
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_SYNTAX);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keyword = p;
|
|
|
|
|
value = NULL;
|
|
|
|
|
if ((p = strchr (p, ':')))
|
|
|
|
|
{
|
|
|
|
|
/* Colon found: Keyword with value. */
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
for (; spacep (p); p++)
|
|
|
|
|
;
|
|
|
|
|
if (!*p)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_MISSING_VALUE);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
value = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i=0; i < DIM (keywords); i++)
|
|
|
|
|
if (!ascii_strcasecmp (keywords[i].name, keyword))
|
|
|
|
|
break;
|
|
|
|
|
if (!(i < DIM (keywords)))
|
|
|
|
|
{
|
|
|
|
|
if (ignore_unknown)
|
|
|
|
|
continue;
|
|
|
|
|
err = gpg_error (GPG_ERR_INV_NAME);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (keywords[i].token)
|
|
|
|
|
{
|
2018-02-20 09:00:00 +01:00
|
|
|
|
case TOK_SUBMISSION_ADDRESS:
|
|
|
|
|
if (!value || !*value)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_SYNTAX);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
xfree (flags->submission_address);
|
|
|
|
|
flags->submission_address = xtrystrdup (value);
|
|
|
|
|
if (!flags->submission_address)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2016-09-02 16:54:42 +02:00
|
|
|
|
case TOK_MAILBOX_ONLY: flags->mailbox_only = 1; break;
|
|
|
|
|
case TOK_DANE_ONLY: flags->dane_only = 1; break;
|
|
|
|
|
case TOK_AUTH_SUBMIT: flags->auth_submit = 1; break;
|
|
|
|
|
case TOK_MAX_PENDING:
|
|
|
|
|
if (!value)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_SYNTAX);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
/* FIXME: Define whether these are seconds, hours, or days
|
|
|
|
|
* and decide whether to allow other units. */
|
|
|
|
|
flags->max_pending = atoi (value);
|
|
|
|
|
break;
|
2017-09-12 18:05:00 +02:00
|
|
|
|
case TOK_PROTOCOL_VERSION:
|
|
|
|
|
if (!value)
|
|
|
|
|
{
|
|
|
|
|
err = gpg_error (GPG_ERR_SYNTAX);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
flags->protocol_version = atoi (value);
|
|
|
|
|
break;
|
2016-09-02 16:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!err && !es_feof (stream))
|
|
|
|
|
err = gpg_error_from_syserror ();
|
2016-12-08 13:04:06 +01:00
|
|
|
|
|
|
|
|
|
leave:
|
2016-09-02 16:54:42 +02:00
|
|
|
|
if (err)
|
|
|
|
|
log_error ("error reading '%s', line %d: %s\n",
|
|
|
|
|
es_fname_get (stream), lnr, gpg_strerror (err));
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2018-02-20 09:00:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
wks_free_policy (policy_flags_t policy)
|
|
|
|
|
{
|
|
|
|
|
if (policy)
|
|
|
|
|
{
|
|
|
|
|
xfree (policy->submission_address);
|
|
|
|
|
memset (policy, 0, sizeof *policy);
|
|
|
|
|
}
|
|
|
|
|
}
|