1 /* import.c - Import functions.
2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GPGME; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 struct import_result_s
41 _gpgme_release_import_result (ImportResult result)
45 gpgme_data_release (result->xmlinfo);
50 /* Parse the args and append the information to the XML structure in
51 the data buffer. With args of NULL the xml structure is
54 append_xml_impinfo (GpgmeData *rdh, GpgmeStatusCode code, char *args)
56 #define MAX_IMPORTED_FIELDS 14
57 static const char *const imported_fields[MAX_IMPORTED_FIELDS]
58 = { "keyid", "username", 0 };
59 static const char *const imported_fields_x509[MAX_IMPORTED_FIELDS]
61 static const char *const import_res_fields[MAX_IMPORTED_FIELDS]
62 = { "count", "no_user_id", "imported", "imported_rsa",
63 "unchanged", "n_uids", "n_subk", "n_sigs", "s_sigsn_revoc",
64 "sec_read", "sec_imported", "sec_dups", "skipped_new", 0 };
65 const char *field[MAX_IMPORTED_FIELDS];
66 const char *const *field_name = 0;
70 /* Verify that we can use the args. */
71 if (code != GPGME_STATUS_EOF)
76 if (code == GPGME_STATUS_IMPORTED)
77 field_name = imported_fields;
78 else if (code == GPGME_STATUS_IMPORT_RES)
79 field_name = import_res_fields;
83 for (i = 0; field_name[i]; i++)
86 if (field_name[i + 1])
88 args = strchr (args, ' ');
90 return; /* Invalid line. */
95 /* gpgsm does not print a useful user ID and uses a fingerprint
96 instead of the key ID. */
97 if (code == GPGME_STATUS_IMPORTED && field[0] && strlen (field[0]) > 16)
98 field_name = imported_fields_x509;
101 /* Initialize the data buffer if necessary. */
104 if (gpgme_data_new (rdh))
105 return; /* FIXME: We are ignoring out-of-core. */
107 _gpgme_data_append_string (dh, "<GnupgOperationInfo>\n");
112 if (code == GPGME_STATUS_EOF)
114 /* Just close the XML containter. */
115 _gpgme_data_append_string (dh, "</GnupgOperationInfo>\n");
119 if (code == GPGME_STATUS_IMPORTED)
120 _gpgme_data_append_string (dh, " <import>\n");
121 else if (code == GPGME_STATUS_IMPORT_RES)
122 _gpgme_data_append_string (dh, " <importResult>\n");
124 for (i = 0; field_name[i]; i++)
126 _gpgme_data_append_string (dh, " <");
127 _gpgme_data_append_string (dh, field_name[i]);
128 _gpgme_data_append_string (dh, ">");
129 _gpgme_data_append_string_for_xml (dh, field[i]);
130 _gpgme_data_append_string (dh, "</");
131 _gpgme_data_append_string (dh, field_name[i]);
132 _gpgme_data_append_string (dh, ">\n");
135 if (code == GPGME_STATUS_IMPORTED)
136 _gpgme_data_append_string (dh, " </import>\n");
137 else if (code == GPGME_STATUS_IMPORT_RES)
138 _gpgme_data_append_string (dh, " </importResult>\n");
144 import_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
146 test_and_allocate_result (ctx, import);
150 case GPGME_STATUS_EOF:
151 if (ctx->result.import->xmlinfo)
153 append_xml_impinfo (&ctx->result.import->xmlinfo, code, NULL);
154 _gpgme_set_op_info (ctx, ctx->result.import->xmlinfo);
155 ctx->result.import->xmlinfo = NULL;
157 /* XXX Calculate error value. */
160 case GPGME_STATUS_IMPORTED:
161 ctx->result.import->nr_imported++;
162 append_xml_impinfo (&ctx->result.import->xmlinfo, code, args);
165 case GPGME_STATUS_IMPORT_RES:
166 ctx->result.import->nr_considered = strtol (args, 0, 0);
167 append_xml_impinfo (&ctx->result.import->xmlinfo, code, args);
178 _gpgme_op_import_start (GpgmeCtx ctx, int synchronous, GpgmeData keydata)
182 err = _gpgme_op_reset (ctx, synchronous);
186 /* Check the supplied data */
193 _gpgme_engine_set_status_handler (ctx->engine, import_status_handler, ctx);
194 _gpgme_engine_set_verbosity (ctx->engine, ctx->verbosity);
196 err = _gpgme_engine_op_import (ctx->engine, keydata);
202 _gpgme_engine_release (ctx->engine);
210 gpgme_op_import_start (GpgmeCtx ctx, GpgmeData keydata)
212 return _gpgme_op_import_start (ctx, 0, keydata);
218 * @keydata: Data object
219 * @nr: Will contain number of considered keys.
221 * Import all key material from @keydata into the key database.
223 * Return value: 0 on success or an error code.
226 gpgme_op_import_ext (GpgmeCtx ctx, GpgmeData keydata, int *nr)
228 GpgmeError err = _gpgme_op_import_start (ctx, 1, keydata);
230 err = _gpgme_wait_one (ctx);
233 if (ctx->result.import)
234 *nr = ctx->result.import->nr_considered;
242 gpgme_op_import (GpgmeCtx ctx, GpgmeData keydata)
244 return gpgme_op_import_ext (ctx, keydata, 0);