Changes to make use of code taken from libassuan. This replaces the
[gnupg.git] / g10 / decrypt.c
index 9812756..9013087 100644 (file)
@@ -1,5 +1,6 @@
 /* decrypt.c - verify signed data
- *     Copyright (C) 1998, 2000 Free Software Foundation, Inc.
+ * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
+ *               2004 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
 #include <errno.h>
 #include <assert.h>
 
-#include <gcrypt.h>
 #include "options.h"
 #include "packet.h"
 #include "errors.h"
 #include "iobuf.h"
 #include "keydb.h"
+#include "memory.h"
 #include "util.h"
 #include "main.h"
+#include "status.h"
 #include "i18n.h"
 
 
@@ -50,16 +52,25 @@ decrypt_message( const char *filename )
 {
     IOBUF fp;
     armor_filter_context_t afx;
+    progress_filter_context_t pfx;
     int rc;
     int no_out=0;
 
     /* open the message file */
     fp = iobuf_open(filename);
+    if (fp && is_secured_file (iobuf_get_fd (fp)))
+      {
+        iobuf_close (fp);
+        fp = NULL;
+        errno = EPERM;
+      }
     if( !fp ) {
        log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
-       return GPGERR_OPEN_FILE;
+       return G10ERR_OPEN_FILE;
     }
 
+    handle_progress (&pfx, fp, filename);
+
     if( !opt.no_armor ) {
        if( use_armor_filter( fp ) ) {
            memset( &afx, 0, sizeof afx);
@@ -78,5 +89,99 @@ decrypt_message( const char *filename )
     return rc;
 }
 
+void
+decrypt_messages(int nfiles, char *files[])
+{
+  IOBUF fp;
+  armor_filter_context_t afx;  
+  progress_filter_context_t pfx;
+  char *p, *output = NULL;
+  int rc=0,use_stdin=0;
+  unsigned int lno=0;
+  
+  if (opt.outfile)
+    {
+      log_error(_("--output doesn't work for this command\n"));
+      return;
+        
+    }
+
+  if(!nfiles)
+    use_stdin=1;
+
+  for(;;)
+    {
+      char line[2048];
+      char *filename=NULL;
+
+      if(use_stdin)
+       {
+         if(fgets(line, DIM(line), stdin))
+           {
+             lno++;
+             if (!*line || line[strlen(line)-1] != '\n')
+               log_error("input line %u too long or missing LF\n", lno);
+             else
+               {
+                 line[strlen(line)-1] = '\0';
+                 filename=line;
+               }
+           }
+       }
+      else
+       {
+         if(nfiles)
+           {
+             filename=*files;
+             nfiles--;
+             files++;
+           }
+       }
+
+      if(filename==NULL)
+       break;
+
+      print_file_status(STATUS_FILE_START, filename, 3);      
+      output = make_outfile_name(filename);
+      if (!output)
+        goto next_file;
+      fp = iobuf_open(filename);
+      if (fp && is_secured_file (iobuf_get_fd (fp)))
+        {
+          iobuf_close (fp);
+          fp = NULL;
+          errno = EPERM;
+        }
+      if (!fp)
+        {
+          log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
+          goto next_file;
+        }
+
+      handle_progress (&pfx, fp, filename);
 
+      if (!opt.no_armor)
+        {
+          if (use_armor_filter(fp))
+            {
+              memset(&afx, 0, sizeof afx);
+              iobuf_push_filter(fp, armor_filter, &afx);
+            }
+        }
+      rc = proc_packets(NULL, fp);
+      iobuf_close(fp);
+      if (rc)
+        log_error("%s: decryption failed: %s\n", print_fname_stdin(filename),
+                  g10_errstr(rc));
+      p = get_last_passphrase();
+      set_next_passphrase(p);
+      m_free (p);
 
+    next_file:
+      /* Note that we emit file_done even after an error. */
+      write_status( STATUS_FILE_DONE );
+      m_free(output);
+    }
+
+  set_next_passphrase(NULL);  
+}