[Gpa-commits] r788 - in trunk: . src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Nov 12 20:31:22 CET 2007


Author: werner
Date: 2007-11-12 20:31:22 +0100 (Mon, 12 Nov 2007)
New Revision: 788

Modified:
   trunk/configure.ac
   trunk/src/ChangeLog
   trunk/src/gpaoperation.c
   trunk/src/gpaoperation.h
   trunk/src/gpastreamencryptop.c
   trunk/src/gpastreamencryptop.h
   trunk/src/server.c
Log:
Add server command PREP_ENCRYPT.


Modified: trunk/configure.ac
===================================================================
--- trunk/configure.ac	2007-10-04 12:28:01 UTC (rev 787)
+++ trunk/configure.ac	2007-11-12 19:31:22 UTC (rev 788)
@@ -197,7 +197,7 @@
 #
 AM_PATH_GPG_ERROR("$NEED_GPG_ERROR_VERSION",
                   have_gpg_error=yes,have_gpg_error=no)
-AC_DEFINE(GPG_ERR_SOURCE_DEFAULT, GPG_ERR_SOURCE_USER_4,
+AC_DEFINE(GPG_ERR_SOURCE_DEFAULT, 12,
           [The default error source for GPA.])
 
 #

Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2007-10-04 12:28:01 UTC (rev 787)
+++ trunk/src/ChangeLog	2007-11-12 19:31:22 UTC (rev 788)
@@ -1,3 +1,13 @@
+2007-11-12  Werner Koch  <wk at g10code.com>
+
+	* server.c (cmd_prep_encrypt, cont_prep_encrypt): New.
+	(register_commands): Register PREP_ENCRYPT command.
+	(copy_recipients): Take a copy of each string.
+	* gpaoperation.c (gpa_operation_write_status): New.
+	* gpastreamencryptop.c (gpa_stream_encrypt_operation_new): Add arg
+	SILENT and change all callers.
+	(parse_recipients): Write a status line.
+
 2007-10-02  Werner Koch  <wk at g10code.com>
 
 	* utils.c (translate_sys2libc_fd): New.

Modified: trunk/src/gpaoperation.c
===================================================================
--- trunk/src/gpaoperation.c	2007-10-04 12:28:01 UTC (rev 787)
+++ trunk/src/gpaoperation.c	2007-11-12 19:31:22 UTC (rev 788)
@@ -26,6 +26,7 @@
 #include "gpgmetools.h"
 #include "i18n.h"
 
+
 #ifndef G_PARAM_STATIC_STRINGS
 #define G_PARAM_STATIC_STRINGS (G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK \
                                 | G_PARAM_STATIC_BLURB)
@@ -225,3 +226,43 @@
       gpa_run_server_continuation (ctx, err);
     }
 }
+
+
+/* If running in server mode, write a status line names STATUSNAME
+   plus space delimited arguments.  */
+gpg_error_t
+gpa_operation_write_status (GpaOperation *op, const char *statusname, ...)
+{
+  gpg_error_t err = 0;
+
+  g_return_val_if_fail (op, gpg_error (GPG_ERR_BUG));
+  g_return_val_if_fail (GPA_IS_OPERATION (op), gpg_error (GPG_ERR_BUG));
+  if (op->server_ctx)
+    {
+      assuan_context_t ctx = op->server_ctx;
+      va_list arg_ptr;
+      char buf[950], *p;
+      const char *text;
+      size_t n;
+
+      va_start (arg_ptr, statusname);
+      
+      p = buf; 
+      n = 0;
+      while ( (text = va_arg (arg_ptr, const char *)) )
+        {
+          if (n)
+            {
+              *p++ = ' ';
+              n++;
+            }
+          for ( ; *text && n < DIM (buf)-2; n++)
+            *p++ = *text++;
+        }
+      *p = 0;
+      err = assuan_write_status (ctx, statusname, buf);
+      va_end (arg_ptr);
+    }
+
+  return err;
+}

Modified: trunk/src/gpaoperation.h
===================================================================
--- trunk/src/gpaoperation.h	2007-10-04 12:28:01 UTC (rev 787)
+++ trunk/src/gpaoperation.h	2007-11-12 19:31:22 UTC (rev 788)
@@ -65,5 +65,10 @@
    after this operation. */
 void gpa_operation_server_finish (GpaOperation *op, gpg_error_t err);
 
+/* If running in server mode, write a status line names STATUSNAME
+   plus space delimited arguments.  */
+gpg_error_t gpa_operation_write_status (GpaOperation *op, 
+                                        const char *statusname, ...);
 
+
 #endif

Modified: trunk/src/gpastreamencryptop.c
===================================================================
--- trunk/src/gpastreamencryptop.c	2007-10-04 12:28:01 UTC (rev 787)
+++ trunk/src/gpastreamencryptop.c	2007-11-12 19:31:22 UTC (rev 788)
@@ -58,6 +58,7 @@
     {
       g_slist_foreach (op->recipients, free_func, NULL);
       g_slist_free (op->recipients);
+      op->recipients = NULL;
     }
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
@@ -350,6 +351,7 @@
   int found_idx;
   gpgme_key_t key, key2;
   const char *name;
+  gpgme_protocol_t used_proto = GPGME_PROTOCOL_UNKNOWN;
 
   *r_found = NULL;
   *r_unknown = NULL;
@@ -389,6 +391,8 @@
         {
           gpgme_key_ref (key);
           found[found_idx++] = key;
+          if (used_proto == GPGME_PROTOCOL_UNKNOWN)
+            used_proto = key->protocol;
         }
       else if (key)
 	{
@@ -408,9 +412,20 @@
     }
   gpgme_release (ctx);
 
+  if (err)
+    ;
+  else if (used_proto == GPGME_PROTOCOL_OpenPGP)
+    err = gpa_operation_write_status (GPA_OPERATION (op), "PROTOCOL",
+                                      "OpenPGP", NULL);
+  else if (used_proto == GPGME_PROTOCOL_CMS)
+    err = gpa_operation_write_status (GPA_OPERATION (op), "PROTOCOL",
+                                      "CMS", NULL);
+  else 
+    err = 0;
+
   *r_found = found;
   *r_unknown = unknown;
-  return 0;
+  return err;
 }
 
 
@@ -425,6 +440,7 @@
   GpaStreamEncryptOperation *op = user_data;
   gpgme_key_t *keys;
   GSList *unknown_recp;
+  int prep_only = 0;
 
   gtk_widget_hide (GTK_WIDGET (dialog));
   
@@ -447,10 +463,18 @@
   if (err)
     goto leave;
 
-
   /* Our streams work all in ascii armored mode (Either PGP or PEM) */
-  gpgme_set_armor (GPA_OPERATION (op)->context->ctx, 1);
-  err = start_encryption (op, keys);
+  if (GPA_STREAM_OPERATION (op)->input_stream)
+    {
+      gpgme_set_armor (GPA_OPERATION (op)->context->ctx, 1);
+      err = start_encryption (op, keys);
+    }
+  else
+    {
+      /* We are just preparing an encryption. */
+      prep_only = 1;
+      err = 0;
+    }
 
  leave:
   if (keys)
@@ -465,7 +489,7 @@
   g_slist_free (unknown_recp);
   g_slist_foreach (recipients, free_func, NULL);
   g_slist_free (recipients);
-  if (err)
+  if (err || prep_only)
     {
       gpa_operation_server_finish (GPA_OPERATION (op), err);
       g_signal_emit_by_name (GPA_OPERATION (op), "completed");
@@ -511,11 +535,23 @@
 
 /* API */
 
+/* Start encrypting INPUT_STREAM to OUTPUT_STREAM using SERVER_CTX and
+   WINDOW.  RECIPIENTS gives a list of recipients and the function
+   matches them with existing keys and selects appropriate keys.  If
+   it is not possible to unambigiously select keys and SILENT is not
+   given, a key selection dialog offers the user a way to manually
+   input keys.  INPUT_STREAM and OUTPUT_STREAM may be given as NULL in
+   which case the function skips the actual encryption step and just
+   verifies the recipients.  */
+/* FIXME: We need to offer a way to return the actual selected list of
+   recipients so that repeating this command with that list instantly
+   starts the decryption.  */
 GpaStreamEncryptOperation*
 gpa_stream_encrypt_operation_new (GtkWidget *window,
                                   gpgme_data_t input_stream,
                                   gpgme_data_t output_stream,
                                   GSList *recipients,
+                                  int silent,
                                   void *server_ctx)
 {
   GpaStreamEncryptOperation *op;

Modified: trunk/src/gpastreamencryptop.h
===================================================================
--- trunk/src/gpastreamencryptop.h	2007-10-04 12:28:01 UTC (rev 787)
+++ trunk/src/gpastreamencryptop.h	2007-11-12 19:31:22 UTC (rev 788)
@@ -80,6 +80,7 @@
                                   gpgme_data_t input_stream,
                                   gpgme_data_t output_stream,
                                   GSList *recipients,
+                                  int silent,
                                   void *server_ctx);
 
 

Modified: trunk/src/server.c
===================================================================
--- trunk/src/server.c	2007-10-04 12:28:01 UTC (rev 787)
+++ trunk/src/server.c	2007-11-12 19:31:22 UTC (rev 788)
@@ -214,7 +214,7 @@
   
   newlist= NULL;
   for (recp = ctrl->recipients; recp; recp = g_slist_next (recp))
-    newlist = g_slist_append (newlist, recp->data);
+    newlist = g_slist_append (newlist, xstrdup (recp->data));
 
   return newlist;
 }
@@ -279,22 +279,27 @@
 {
   conn_ctrl_t ctrl = assuan_get_pointer (ctx);
   gpg_error_t err;
-  gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
+  gpgme_protocol_t protocol = 0;
   GpaStreamEncryptOperation *op;
   gpgme_data_t input_data = NULL;
   gpgme_data_t output_data = NULL;
 
 
   if (has_option (line, "--protocol=OpenPGP"))
-    ; /* This is the default.  */
+    protocol = GPGME_PROTOCOL_OpenPGP;
   else if (has_option (line, "--protocol=CMS"))
     protocol = GPGME_PROTOCOL_CMS;
-  else  if (has_option_name (line, "--protocol"))
+  else if (has_option_name (line, "--protocol"))
     {
       err = set_error (GPG_ERR_ASS_PARAMETER, "invalid protocol");
       goto leave;
     }
-  
+  else 
+    {
+      err = set_error (GPG_ERR_ASS_PARAMETER, "no protocol specified");
+      goto leave;
+    }
+
   line = skip_options (line);
   if (*line)
     {
@@ -353,7 +358,7 @@
 
   ctrl->cont_cmd = cont_encrypt;
   op = gpa_stream_encrypt_operation_new (NULL, input_data, output_data,
-                                         copy_recipients (ctrl), ctx);
+                                         copy_recipients (ctrl), 0, ctx);
   input_data = output_data = NULL;
   g_signal_connect (G_OBJECT (op), "completed",
                     G_CALLBACK (g_object_unref), NULL);
@@ -378,7 +383,61 @@
 }
 
 
+
+/* Continuation for cmd_prep_encrypt.  */
+void
+cont_prep_encrypt (assuan_context_t ctx, gpg_error_t err)
+{
+  g_debug ("cont_prep_encrypt called with with ERR=%s <%s>",
+           gpg_strerror (err), gpg_strsource (err));
 
+  assuan_process_done (ctx, err);
+}
+
+
+/* PREP_ENCRYPT [--protocol=OPENPGP|CMS]
+
+   Dummy encryption command used to check whether the given recipients
+   are all valid and to tell the cleint the preferred protocol.  */
+static int 
+cmd_prep_encrypt (assuan_context_t ctx, char *line)
+{
+  conn_ctrl_t ctrl = assuan_get_pointer (ctx);
+  gpg_error_t err;
+  gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
+  GpaStreamEncryptOperation *op;
+
+  if (has_option (line, "--protocol=OpenPGP"))
+    ; /* This is the default.  */
+  else if (has_option (line, "--protocol=CMS"))
+    protocol = GPGME_PROTOCOL_CMS;
+  else if (has_option_name (line, "--protocol"))
+    {
+      err = set_error (GPG_ERR_ASS_PARAMETER, "invalid protocol");
+      goto leave;
+    }
+
+  line = skip_options (line);
+  if (*line)
+    {
+      err = set_error (GPG_ERR_ASS_SYNTAX, NULL);
+      goto leave;
+    }
+
+  ctrl->cont_cmd = cont_prep_encrypt;
+  op = gpa_stream_encrypt_operation_new (NULL, NULL, NULL,
+                                         copy_recipients (ctrl), 0, ctx);
+  g_signal_connect (G_OBJECT (op), "completed",
+                    G_CALLBACK (g_object_unref), NULL);
+  return gpg_error (GPG_ERR_UNFINISHED);
+
+ leave:
+  return assuan_process_done (ctx, err);
+}
+
+
+
+
 
 /* GETINFO <what>
 
@@ -447,6 +506,7 @@
     { "INPUT",     NULL },
     { "OUTPUT",    NULL },
     { "ENCRYPT",   cmd_encrypt },
+    { "PREP_ENCRYPT", cmd_prep_encrypt },
     { "GETINFO",   cmd_getinfo },
     { NULL }
   };



More information about the Gpa-commits mailing list