-/* import.c - encrypt functions
- * Copyright (C) 2000 Werner Koch (dd9jn)
- * Copyright (C) 2001, 2002 g10 Code GmbH
- *
- * This file is part of GPGME.
- *
- * GPGME 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 2 of the License, or
- * (at your option) any later version.
- *
- * GPGME 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
- */
-
+/* import.c - Import a key.
+ Copyright (C) 2000 Werner Koch (dd9jn)
+ Copyright (C) 2001, 2002, 2003 g10 Code GmbH
+
+ This file is part of GPGME.
+
+ GPGME 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 2 of the License, or
+ (at your option) any later version.
+
+ GPGME 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 GPGME; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
#include <config.h>
-#include <stdio.h>
+#endif
#include <stdlib.h>
+#include <errno.h>
#include <string.h>
-#include <assert.h>
-#include "util.h"
+#include "gpgme.h"
#include "context.h"
#include "ops.h"
-
-struct import_result_s
+\f
+typedef struct
{
- int nr_imported;
- int nr_considered;
- GpgmeData xmlinfo;
-};
+ struct _gpgme_op_import_result result;
+ /* A pointer to the next pointer of the last import status in the
+ list. This makes appending new imports painless while preserving
+ the order. */
+ GpgmeImportStatus *lastp;
+} *op_data_t;
-void
-_gpgme_release_import_result (ImportResult result)
+
+static void
+release_op_data (void *hook)
{
- if (!result)
- return;
- gpgme_data_release (result->xmlinfo);
- xfree (result);
+ op_data_t opd = (op_data_t) hook;
+ GpgmeImportStatus import = opd->result.imports;
+
+ while (import)
+ {
+ GpgmeImportStatus next = import->next;
+ free (import->fpr);
+ free (import);
+ import = next;
+ }
}
-/* Parse the args and append the information to the XML structure in
- the data buffer. With args of NULL the xml structure is
- closed. */
-static void
-append_xml_impinfo (GpgmeData *rdh, GpgmeStatusCode code, char *args)
+GpgmeImportResult
+gpgme_op_import_result (GpgmeCtx ctx)
{
-#define MAX_IMPORTED_FIELDS 14
- static const char *const imported_fields[MAX_IMPORTED_FIELDS]
- = { "keyid", "username", 0 };
- static const char *const imported_fields_x509[MAX_IMPORTED_FIELDS]
- = { "fpr", 0 };
- static const char *const import_res_fields[MAX_IMPORTED_FIELDS]
- = { "count", "no_user_id", "imported", "imported_rsa",
- "unchanged", "n_uids", "n_subk", "n_sigs", "s_sigsn_revoc",
- "sec_read", "sec_imported", "sec_dups", "skipped_new", 0 };
- const char *field[MAX_IMPORTED_FIELDS];
- const char *const *field_name = 0;
- GpgmeData dh;
- int i;
-
- /* Verify that we can use the args. */
- if (code != GPGME_STATUS_EOF)
- {
- if (!args)
- return;
+ op_data_t opd;
+ GpgmeError err;
- if (code == GPGME_STATUS_IMPORTED)
- field_name = imported_fields;
- else if (code == GPGME_STATUS_IMPORT_RES)
- field_name = import_res_fields;
- else
- return;
+ err = _gpgme_op_data_lookup (ctx, OPDATA_IMPORT, (void **) &opd, -1, NULL);
+ if (err || !opd)
+ return NULL;
- for (i = 0; field_name[i]; i++)
- {
- field[i] = args;
- if (field_name[i + 1])
- {
- args = strchr (args, ' ');
- if (!args)
- return; /* Invalid line. */
- *args++ = '\0';
- }
- }
-
- /* gpgsm does not print a useful user ID and uses a fingerprint
- instead of the key ID. */
- if (code == GPGME_STATUS_IMPORTED && field[0] && strlen (field[0]) > 16)
- field_name = imported_fields_x509;
- }
+ return &opd->result;
+}
- /* Initialize the data buffer if necessary. */
- if (!*rdh)
+\f
+static GpgmeError
+parse_import (char *args, GpgmeImportStatus *import_status, int problem)
+{
+ GpgmeImportStatus import;
+ char *tail;
+ long int nr;
+
+ import = malloc (sizeof (*import));
+ if (!import)
+ return GPGME_Out_Of_Core;
+ import->next = NULL;
+
+ errno = 0;
+ nr = strtol (args, &tail, 0);
+ if (errno || args == tail || *tail != ' ')
{
- if (gpgme_data_new (rdh))
- return; /* FIXME: We are ignoring out-of-core. */
- dh = *rdh;
- _gpgme_data_append_string (dh, "<GnupgOperationInfo>\n");
+ /* The crypto backend does not behave. */
+ free (import);
+ return GPGME_General_Error;
}
- else
- dh = *rdh;
-
- if (code == GPGME_STATUS_EOF)
+ args = tail;
+
+ if (problem)
{
- /* Just close the XML containter. */
- _gpgme_data_append_string (dh, "</GnupgOperationInfo>\n");
+ switch (nr)
+ {
+ case 0:
+ case 4:
+ default:
+ import->result = GPGME_Unknown_Reason;
+ break;
+
+ case 1:
+ import->result = GPGME_Invalid_Key;
+ break;
+
+ case 2:
+ import->result = GPGME_Issuer_Missing;
+ break;
+
+ case 3:
+ import->result = GPGME_Chain_Too_Long;
+ break;
+ }
+ import->status = 0;
}
else
{
- if (code == GPGME_STATUS_IMPORTED)
- _gpgme_data_append_string (dh, " <import>\n");
- else if (code == GPGME_STATUS_IMPORT_RES)
- _gpgme_data_append_string (dh, " <importResult>\n");
+ import->result = GPGME_No_Error;
+ import->status = nr;
+ }
- for (i = 0; field_name[i]; i++)
- {
- _gpgme_data_append_string (dh, " <");
- _gpgme_data_append_string (dh, field_name[i]);
- _gpgme_data_append_string (dh, ">");
- _gpgme_data_append_string (dh, field[i]);
- _gpgme_data_append_string (dh, "</");
- _gpgme_data_append_string (dh, field_name[i]);
- _gpgme_data_append_string (dh, ">\n");
- }
+ while (*args == ' ')
+ args++;
+ tail = strchr (args, ' ');
+ if (tail)
+ *tail = '\0';
- if (code == GPGME_STATUS_IMPORTED)
- _gpgme_data_append_string (dh, " </import>\n");
- else if (code == GPGME_STATUS_IMPORT_RES)
- _gpgme_data_append_string (dh, " </importResult>\n");
+ import->fpr = strdup (args);
+ if (!import->fpr)
+ {
+ free (import);
+ return GPGME_Out_Of_Core;
}
+
+ *import_status = import;
+ return 0;
}
-static void
-import_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
+
+GpgmeError
+parse_import_res (char *args, GpgmeImportResult result)
{
- if (ctx->error)
- return;
- test_and_allocate_result (ctx, import);
+ char *tail;
+
+ errno = 0;
+
+#define PARSE_NEXT(x) \
+ (x) = strtol (args, &tail, 0); \
+ if (errno || args == tail || *tail != ' ') \
+ /* The crypto backend does not behave. */ \
+ return GPGME_General_Error; \
+ args = tail;
+
+ PARSE_NEXT (result->considered);
+ PARSE_NEXT (result->no_user_id);
+ PARSE_NEXT (result->imported);
+ PARSE_NEXT (result->imported_rsa);
+ PARSE_NEXT (result->unchanged);
+ PARSE_NEXT (result->new_user_ids);
+ PARSE_NEXT (result->new_sub_keys);
+ PARSE_NEXT (result->new_signatures);
+ PARSE_NEXT (result->new_revocations);
+ PARSE_NEXT (result->secret_read);
+ PARSE_NEXT (result->secret_imported);
+ PARSE_NEXT (result->secret_unchanged);
+ PARSE_NEXT (result->not_imported);
+
+ return 0;
+}
+
+
+static GpgmeError
+import_status_handler (void *priv, GpgmeStatusCode code, char *args)
+{
+ GpgmeCtx ctx = (GpgmeCtx) priv;
+ GpgmeError err;
+ op_data_t opd;
+
+ err = _gpgme_op_data_lookup (ctx, OPDATA_IMPORT, (void **) &opd,
+ -1, NULL);
+ if (err)
+ return err;
switch (code)
{
- case GPGME_STATUS_EOF:
- if (ctx->result.import->xmlinfo)
- {
- append_xml_impinfo (&ctx->result.import->xmlinfo, code, NULL);
- _gpgme_set_op_info (ctx, ctx->result.import->xmlinfo);
- ctx->result.import->xmlinfo = NULL;
- }
- /* XXX Calculate error value. */
- break;
-
- case GPGME_STATUS_IMPORTED:
- ctx->result.import->nr_imported++;
- append_xml_impinfo (&ctx->result.import->xmlinfo, code, args);
+ case GPGME_STATUS_IMPORT_OK:
+ case GPGME_STATUS_IMPORT_PROBLEM:
+ err = parse_import (args, opd->lastp,
+ code == GPGME_STATUS_IMPORT_OK ? 0 : 1);
+ if (err)
+ return err;
+
+ opd->lastp = &(*opd->lastp)->next;
break;
case GPGME_STATUS_IMPORT_RES:
- ctx->result.import->nr_considered = strtol (args, 0, 0);
- append_xml_impinfo (&ctx->result.import->xmlinfo, code, args);
+ err = parse_import_res (args, &opd->result);
break;
default:
break;
}
+ return 0;
}
static GpgmeError
_gpgme_op_import_start (GpgmeCtx ctx, int synchronous, GpgmeData keydata)
{
- int err = 0;
+ GpgmeError err;
+ op_data_t opd;
err = _gpgme_op_reset (ctx, synchronous);
if (err)
- goto leave;
+ return err;
+
+ err = _gpgme_op_data_lookup (ctx, OPDATA_IMPORT, (void **) &opd,
+ sizeof (*opd), release_op_data);
+ if (err)
+ return err;
+ opd->lastp = &opd->result.imports;
- /* Check the supplied data */
if (!keydata)
- {
- err = mk_error (No_Data);
- goto leave;
- }
+ return GPGME_No_Data;
_gpgme_engine_set_status_handler (ctx->engine, import_status_handler, ctx);
- _gpgme_engine_set_verbosity (ctx->engine, ctx->verbosity);
-
- _gpgme_engine_op_import (ctx->engine, keydata);
-
- if (!err)
- err = _gpgme_engine_start (ctx->engine, ctx);
- leave:
- if (err)
- {
- ctx->pending = 0;
- _gpgme_engine_release (ctx->engine);
- ctx->engine = NULL;
- }
- return err;
+ return _gpgme_engine_op_import (ctx->engine, keydata);
}
return _gpgme_op_import_start (ctx, 0, keydata);
}
-/**
- * gpgme_op_import:
- * @c: Context
- * @keydata: Data object
- * @nr: Will contain number of considered keys.
- *
- * Import all key material from @keydata into the key database.
- *
- * Return value: 0 on success or an error code.
- **/
+
+/* Import the key in KEYDATA into the keyring. */
GpgmeError
-gpgme_op_import_ext (GpgmeCtx ctx, GpgmeData keydata, int *nr)
+gpgme_op_import (GpgmeCtx ctx, GpgmeData keydata)
{
GpgmeError err = _gpgme_op_import_start (ctx, 1, keydata);
if (!err)
err = _gpgme_wait_one (ctx);
- if (!err && nr)
- {
- if (ctx->result.import)
- *nr = ctx->result.import->nr_considered;
- else
- *nr = 0;
- }
return err;
}
+
GpgmeError
-gpgme_op_import (GpgmeCtx ctx, GpgmeData keydata)
+gpgme_op_import_ext (GpgmeCtx ctx, GpgmeData keydata, int *nr)
{
- return gpgme_op_import_ext (ctx, keydata, 0);
+ GpgmeError err = gpgme_op_import (ctx, keydata);
+ if (!err && nr)
+ {
+ GpgmeImportResult result = gpgme_op_import_result (ctx);
+ *nr = result->considered;
+ }
+ return err;
}
-