[PATCH] Allow to set a context's keyserver

Leo Gaspard ekleog at gmail.com
Tue Aug 21 01:01:52 CEST 2012


 * Added gpgme_get_keyserver and gpgme_set_keyserver to get/set the
   keyserver of a particular context ;

 * Modified the context so that it can stock the keyserver ;

 * Updated functions so that they take into account this new parameter.
---
Sorry for spamming the mailing list, but I just noticed there were a few bugs on
the patch I sent on 07/29. Now, the repository after applying the patch both
compiles and runs flawlessly "make check".

HTH,

Leo

 src/context.h        |  3 +++
 src/engine-backend.h |  8 ++++----
 src/engine-gpg.c     | 32 ++++++++++++++++++++++----------
 src/engine-gpgsm.c   |  8 ++++----
 src/engine.c         | 17 +++++++++--------
 src/engine.h         | 11 +++++++----
 src/export.c         |  4 ++--
 src/gpgme.c          | 39 +++++++++++++++++++++++++++++++++++++++
 src/keylist.c        |  4 ++--
 9 files changed, 92 insertions(+), 34 deletions(-)

diff --git a/src/context.h b/src/context.h
index 69d8d9b..bec0af7 100644
--- a/src/context.h
+++ b/src/context.h
@@ -133,6 +133,9 @@ struct gpgme_context
      operation.  */
   struct fd_table fdt;
   struct gpgme_io_cbs io_cbs;
+
+  /* The keyserver in use for the context. */
+  char *keyserver;
 };
 
 #endif	/* CONTEXT_H */
diff --git a/src/engine-backend.h b/src/engine-backend.h
index e540acb..bda55ad 100644
--- a/src/engine-backend.h
+++ b/src/engine-backend.h
@@ -76,19 +76,19 @@ struct engine_ops
 				 int use_armor, gpgme_ctx_t ctx /* FIXME */);
   gpgme_error_t (*export) (void *engine, const char *pattern,
 			   gpgme_export_mode_t mode, gpgme_data_t keydata,
-			   int use_armor);
+			   int use_armor, const char *keyserver);
   gpgme_error_t (*export_ext) (void *engine, const char *pattern[],
 			       gpgme_export_mode_t mode, gpgme_data_t keydata,
-			       int use_armor);
+			       int use_armor, const char *keyserver);
   gpgme_error_t (*genkey) (void *engine, gpgme_data_t help_data, int use_armor,
 			   gpgme_data_t pubkey, gpgme_data_t seckey);
   gpgme_error_t (*import) (void *engine, gpgme_data_t keydata,
                            gpgme_key_t *keyarray);
   gpgme_error_t (*keylist) (void *engine, const char *pattern,
-			    int secret_only, gpgme_keylist_mode_t mode);
+			    int secret_only, gpgme_keylist_mode_t mode, const char *keyserver);
   gpgme_error_t (*keylist_ext) (void *engine, const char *pattern[],
 				int secret_only, int reserved,
-				gpgme_keylist_mode_t mode);
+				gpgme_keylist_mode_t mode, const char *keyserver);
   gpgme_error_t (*sign) (void *engine, gpgme_data_t in, gpgme_data_t out,
 			 gpgme_sig_mode_t mode, int use_armor,
 			 int use_textmode, int include_certs,
diff --git a/src/engine-gpg.c b/src/engine-gpg.c
index cbb456e..badcaee 100644
--- a/src/engine-gpg.c
+++ b/src/engine-gpg.c
@@ -1719,7 +1719,7 @@ gpg_encrypt_sign (void *engine, gpgme_key_t recp[],
 
 static gpgme_error_t
 export_common (engine_gpg_t gpg, gpgme_export_mode_t mode,
-               gpgme_data_t keydata, int use_armor)
+               gpgme_data_t keydata, int use_armor, const char *keyserver)
 {
   gpgme_error_t err = 0;
 
@@ -1741,6 +1741,12 @@ export_common (engine_gpg_t gpg, gpgme_export_mode_t mode,
       err = add_arg (gpg, "--export");
       if (!err && use_armor)
         err = add_arg (gpg, "--armor");
+      if (!err && keyserver)
+        {
+          err = add_arg (gpg, "--keyserver");
+          if (!err)
+            err = add_arg (gpg, keyserver);
+        }
       if (!err)
         err = add_data (gpg, keydata, 1, 1);
     }
@@ -1753,12 +1759,12 @@ export_common (engine_gpg_t gpg, gpgme_export_mode_t mode,
 
 static gpgme_error_t
 gpg_export (void *engine, const char *pattern, gpgme_export_mode_t mode,
-	    gpgme_data_t keydata, int use_armor)
+	    gpgme_data_t keydata, int use_armor, const char *keyserver)
 {
   engine_gpg_t gpg = engine;
   gpgme_error_t err;
 
-  err = export_common (gpg, mode, keydata, use_armor);
+  err = export_common (gpg, mode, keydata, use_armor, keyserver);
 
   if (!err && pattern && *pattern)
     err = add_arg (gpg, pattern);
@@ -1772,12 +1778,12 @@ gpg_export (void *engine, const char *pattern, gpgme_export_mode_t mode,
 
 static gpgme_error_t
 gpg_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode,
-		gpgme_data_t keydata, int use_armor)
+		gpgme_data_t keydata, int use_armor, const char *keyserver)
 {
   engine_gpg_t gpg = engine;
   gpgme_error_t err;
 
-  err = export_common (gpg, mode, keydata, use_armor);
+  err = export_common (gpg, mode, keydata, use_armor, keyserver);
 
   if (pattern)
     {
@@ -2129,7 +2135,7 @@ gpg_keylist_preprocess (char *line, char **r_line)
 
 static gpg_error_t
 gpg_keylist_build_options (engine_gpg_t gpg, int secret_only,
-                           gpgme_keylist_mode_t mode)
+                           gpgme_keylist_mode_t mode, const char *keyserver)
 {
   gpg_error_t err;
 
@@ -2171,6 +2177,12 @@ gpg_keylist_build_options (engine_gpg_t gpg, int secret_only,
               err = add_arg (gpg, "--search-keys");
               gpg->colon.preprocess_fnc = gpg_keylist_preprocess;
             }
+          if (!err && keyserver)
+            {
+              err = add_arg (gpg, "--keyserver");
+              if (!err)
+                err = add_arg (gpg, keyserver);
+            }
 	}
       else
         {
@@ -2188,12 +2200,12 @@ gpg_keylist_build_options (engine_gpg_t gpg, int secret_only,
 
 static gpgme_error_t
 gpg_keylist (void *engine, const char *pattern, int secret_only,
-	     gpgme_keylist_mode_t mode)
+	     gpgme_keylist_mode_t mode, const char *keyserver)
 {
   engine_gpg_t gpg = engine;
   gpgme_error_t err;
 
-  err = gpg_keylist_build_options (gpg, secret_only, mode);
+  err = gpg_keylist_build_options (gpg, secret_only, mode, keyserver);
 
   if (!err && pattern && *pattern)
     err = add_arg (gpg, pattern);
@@ -2207,7 +2219,7 @@ gpg_keylist (void *engine, const char *pattern, int secret_only,
 
 static gpgme_error_t
 gpg_keylist_ext (void *engine, const char *pattern[], int secret_only,
-		 int reserved, gpgme_keylist_mode_t mode)
+		 int reserved, gpgme_keylist_mode_t mode, const char *keyserver)
 {
   engine_gpg_t gpg = engine;
   gpgme_error_t err;
@@ -2215,7 +2227,7 @@ gpg_keylist_ext (void *engine, const char *pattern[], int secret_only,
   if (reserved)
     return gpg_error (GPG_ERR_INV_VALUE);
 
-  err = gpg_keylist_build_options (gpg, secret_only, mode);
+  err = gpg_keylist_build_options (gpg, secret_only, mode, keyserver);
 
   if (pattern)
     {
diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c
index 0620458..0489773 100644
--- a/src/engine-gpgsm.c
+++ b/src/engine-gpgsm.c
@@ -1264,7 +1264,7 @@ gpgsm_encrypt (void *engine, gpgme_key_t recp[], gpgme_encrypt_flags_t flags,
 
 static gpgme_error_t
 gpgsm_export (void *engine, const char *pattern, gpgme_export_mode_t mode,
-	      gpgme_data_t keydata, int use_armor)
+	      gpgme_data_t keydata, int use_armor, const char *keyserver)
 {
   engine_gpgsm_t gpgsm = engine;
   gpgme_error_t err = 0;
@@ -1302,7 +1302,7 @@ gpgsm_export (void *engine, const char *pattern, gpgme_export_mode_t mode,
 
 static gpgme_error_t
 gpgsm_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode,
-		  gpgme_data_t keydata, int use_armor)
+		  gpgme_data_t keydata, int use_armor, const char *keyserver)
 {
   engine_gpgsm_t gpgsm = engine;
   gpgme_error_t err = 0;
@@ -1526,7 +1526,7 @@ gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray)
 
 static gpgme_error_t
 gpgsm_keylist (void *engine, const char *pattern, int secret_only,
-	       gpgme_keylist_mode_t mode)
+	       gpgme_keylist_mode_t mode, const char *keyserver)
 {
   engine_gpgsm_t gpgsm = engine;
   char *line;
@@ -1608,7 +1608,7 @@ gpgsm_keylist (void *engine, const char *pattern, int secret_only,
 
 static gpgme_error_t
 gpgsm_keylist_ext (void *engine, const char *pattern[], int secret_only,
-		   int reserved, gpgme_keylist_mode_t mode)
+		   int reserved, gpgme_keylist_mode_t mode, const char *keyserver)
 {
   engine_gpgsm_t gpgsm = engine;
   char *line;
diff --git a/src/engine.c b/src/engine.c
index 1244dd3..2474b44 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -662,7 +662,7 @@ _gpgme_engine_op_encrypt_sign (engine_t engine, gpgme_key_t recp[],
 gpgme_error_t
 _gpgme_engine_op_export (engine_t engine, const char *pattern,
 			 gpgme_export_mode_t mode, gpgme_data_t keydata,
-			 int use_armor)
+			 int use_armor, const char *keyserver)
 {
   if (!engine)
     return gpg_error (GPG_ERR_INV_VALUE);
@@ -671,14 +671,14 @@ _gpgme_engine_op_export (engine_t engine, const char *pattern,
     return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
 
   return (*engine->ops->export) (engine->engine, pattern, mode,
-				 keydata, use_armor);
+				 keydata, use_armor, keyserver);
 }
 
 
 gpgme_error_t
 _gpgme_engine_op_export_ext (engine_t engine, const char *pattern[],
 			     unsigned int reserved, gpgme_data_t keydata,
-			     int use_armor)
+			     int use_armor, const char *keyserver)
 {
   if (!engine)
     return gpg_error (GPG_ERR_INV_VALUE);
@@ -687,7 +687,7 @@ _gpgme_engine_op_export_ext (engine_t engine, const char *pattern[],
     return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
 
   return (*engine->ops->export_ext) (engine->engine, pattern, reserved,
-				     keydata, use_armor);
+				     keydata, use_armor, keyserver);
 }
 
 
@@ -723,7 +723,7 @@ _gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata,
 
 gpgme_error_t
 _gpgme_engine_op_keylist (engine_t engine, const char *pattern,
-			  int secret_only, gpgme_keylist_mode_t mode)
+			  int secret_only, gpgme_keylist_mode_t mode, const char *keyserver)
 {
   if (!engine)
     return gpg_error (GPG_ERR_INV_VALUE);
@@ -731,14 +731,15 @@ _gpgme_engine_op_keylist (engine_t engine, const char *pattern,
   if (!engine->ops->keylist)
     return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
 
-  return (*engine->ops->keylist) (engine->engine, pattern, secret_only, mode);
+  return (*engine->ops->keylist) (engine->engine, pattern, secret_only, mode,
+                  keyserver);
 }
 
 
 gpgme_error_t
 _gpgme_engine_op_keylist_ext (engine_t engine, const char *pattern[],
 			      int secret_only, int reserved,
-			      gpgme_keylist_mode_t mode)
+			      gpgme_keylist_mode_t mode, const char *keyserver)
 {
   if (!engine)
     return gpg_error (GPG_ERR_INV_VALUE);
@@ -747,7 +748,7 @@ _gpgme_engine_op_keylist_ext (engine_t engine, const char *pattern[],
     return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
 
   return (*engine->ops->keylist_ext) (engine->engine, pattern, secret_only,
-				      reserved, mode);
+				      reserved, mode, keyserver);
 }
 
 
diff --git a/src/engine.h b/src/engine.h
index dfc73ac..2db7fdb 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -97,12 +97,13 @@ gpgme_error_t _gpgme_engine_op_encrypt_sign (engine_t engine,
 					     gpgme_ctx_t ctx /* FIXME */);
 gpgme_error_t _gpgme_engine_op_export (engine_t engine, const char *pattern,
 				       gpgme_export_mode_t mode,
-				       gpgme_data_t keydata, int use_armor);
+				       gpgme_data_t keydata, int use_armor, const char *keyserver);
 gpgme_error_t _gpgme_engine_op_export_ext (engine_t engine,
 					   const char *pattern[],
 					   gpgme_export_mode_t mode,
 					   gpgme_data_t keydata,
-					   int use_armor);
+					   int use_armor,
+                  const char *keyserver);
 gpgme_error_t _gpgme_engine_op_genkey (engine_t engine,
 				       gpgme_data_t help_data,
 				       int use_armor, gpgme_data_t pubkey,
@@ -113,12 +114,14 @@ gpgme_error_t _gpgme_engine_op_import (engine_t engine,
 gpgme_error_t _gpgme_engine_op_keylist (engine_t engine,
 					const char *pattern,
 					int secret_only,
-					gpgme_keylist_mode_t mode);
+					gpgme_keylist_mode_t mode,
+               const char *keyserver);
 gpgme_error_t _gpgme_engine_op_keylist_ext (engine_t engine,
 					    const char *pattern[],
 					    int secret_only,
 					    int reserved,
-					    gpgme_keylist_mode_t mode);
+					    gpgme_keylist_mode_t mode,
+                   const char *keyserver);
 gpgme_error_t _gpgme_engine_op_sign (engine_t engine, gpgme_data_t in,
 				     gpgme_data_t out, gpgme_sig_mode_t mode,
 				     int use_armor, int use_textmode,
diff --git a/src/export.c b/src/export.c
index 9c1c79c..4d1dd1c 100644
--- a/src/export.c
+++ b/src/export.c
@@ -67,7 +67,7 @@ export_start (gpgme_ctx_t ctx, int synchronous, const char *pattern,
   _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx);
 
   return _gpgme_engine_op_export (ctx->engine, pattern, mode, keydata,
-				  ctx->use_armor);
+				  ctx->use_armor, ctx->keyserver);
 }
 
 
@@ -137,7 +137,7 @@ export_ext_start (gpgme_ctx_t ctx, int synchronous, const char *pattern[],
   _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx);
 
   return _gpgme_engine_op_export_ext (ctx->engine, pattern, mode, keydata,
-				      ctx->use_armor);
+				      ctx->use_armor, ctx->keyserver);
 }
 
 
diff --git a/src/gpgme.c b/src/gpgme.c
index 927b2a0..d3210a5 100644
--- a/src/gpgme.c
+++ b/src/gpgme.c
@@ -210,6 +210,8 @@ gpgme_release (gpgme_ctx_t ctx)
   if (ctx->lc_messages)
     free (ctx->lc_messages);
   _gpgme_engine_info_release (ctx->engine_info);
+  if (ctx->keyserver)
+    free (ctx->keyserver);
   DESTROY_LOCK (ctx->lock);
   free (ctx);
 }
@@ -406,6 +408,43 @@ gpgme_get_armor (gpgme_ctx_t ctx)
 }
 
 
+/* Defines the keyserver for all keyserver-related operations. */
+gpgme_error_t
+gpgme_set_keyserver (gpgme_ctx_t ctx, const char *keyserver)
+{
+  TRACE_BEG1 (DEBUG_CTX, "gpgme_set_keyserver", ctx, "keyserver=\"%s\"",
+	  keyserver);
+
+  if (!ctx)
+    return gpg_error (GPG_ERR_INV_VALUE);
+
+  char *keyserver_save = ctx->keyserver;
+
+  // FIXME: Add some checks before setting the keyserver ? (e.g. it is a valid
+  // keyserver, online, pingable... or even a valid url ?)
+  ctx->keyserver = strdup (keyserver);
+
+  if (!ctx->keyserver)
+    {
+      ctx->keyserver = keyserver_save;
+      return TRACE_ERR (gpg_error_from_errno (errno));
+    }
+
+  if (keyserver_save)
+    free (keyserver_save);
+  return 0;
+}
+
+/* Return the keyserver in use. Its content should *not* be modified. */
+const char *
+gpgme_get_keyserver (gpgme_ctx_t ctx)
+{
+  TRACE1 (DEBUG_CTX, "gpgme_get_armor", ctx, "ctx->keyserver=\"%s\"",
+	  ctx->keyserver);
+  return ctx->keyserver;
+}
+
+
 /* Enable or disable the use of the special textmode.  Textmode is for
   example used for the RFC2015 signatures; note that the updated RFC
   3156 mandates that the MUA does some preparations so that textmode
diff --git a/src/keylist.c b/src/keylist.c
index 29e30a0..2ec5886 100644
--- a/src/keylist.c
+++ b/src/keylist.c
@@ -884,7 +884,7 @@ gpgme_op_keylist_start (gpgme_ctx_t ctx, const char *pattern, int secret_only)
     return TRACE_ERR (err);
 
   err = _gpgme_engine_op_keylist (ctx->engine, pattern, secret_only,
-				  ctx->keylist_mode);
+				  ctx->keylist_mode, ctx->keyserver);
   return TRACE_ERR (err);
 }
 
@@ -923,7 +923,7 @@ gpgme_op_keylist_ext_start (gpgme_ctx_t ctx, const char *pattern[],
     return TRACE_ERR (err);
 
   err = _gpgme_engine_op_keylist_ext (ctx->engine, pattern, secret_only,
-				      reserved, ctx->keylist_mode);
+				      reserved, ctx->keylist_mode, ctx->keyserver);
   return TRACE_ERR (err);
 }
 
-- 
1.7.11.5




More information about the Gnupg-devel mailing list