unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#33684: DocView bombs out upon password protected PDFs
@ 2018-12-09 10:52 積丹尼 Dan Jacobson
  2019-01-27  2:14 ` Federico Tedin
  0 siblings, 1 reply; 14+ messages in thread
From: 積丹尼 Dan Jacobson @ 2018-12-09 10:52 UTC (permalink / raw)
  To: 33684

When trying to view a password protected PDF:
DocView: process pdf/ps->png changed status to exited abnormally with code 1.
Maybe it should ask for the password.
emacs-version "25.2.2"





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2018-12-09 10:52 bug#33684: DocView bombs out upon password protected PDFs 積丹尼 Dan Jacobson
@ 2019-01-27  2:14 ` Federico Tedin
  2019-02-01  9:33   ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Federico Tedin @ 2019-01-27  2:14 UTC (permalink / raw)
  To: 積丹尼 Dan Jacobson, 33684

[-- Attachment #1: Type: text/plain, Size: 612 bytes --]

積丹尼 Dan Jacobson <jidanni@jidanni.org> writes:

> When trying to view a password protected PDF:
> DocView: process pdf/ps->png changed status to exited abnormally with code 1.
> Maybe it should ask for the password.
> emacs-version "25.2.2"

Would something like this make sense? A short patch that adds
`doc-view-pdf-password-protected-p', and an extra option to pass to
Ghostscript (-sPDFPassword=) when the command is invoked (only when the
PDF file is password protected). I tried it out by creating a
password-protected PDF using LibreOffice Writer, and then opening it
with find-file.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 2490 bytes --]

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index df8a9fc70f..8691d2a3a6 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -183,11 +183,16 @@ doc-view-pdf->png-converter-function
 (defcustom doc-view-ghostscript-options
   '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
     ;; sources.
-    "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
-    "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET")
+    "-dNOPAUSE" "-dTextAlphaBits=4" "-dBATCH"
+    "-dGraphicsAlphaBits=4" "-dQUIET")
   "A list of options to give to ghostscript."
   :type '(repeat string))
 
+(defcustom doc-view-ghostscript-device "png16m"
+  "Output device to give to ghostscript."
+  :type 'string
+  :version "27.1")
+
 (defcustom doc-view-resolution 100
   "Dots per inch resolution used to render the documents.
 Higher values result in larger images."
@@ -950,16 +955,30 @@ doc-view-dvi->pdf
 			    (list "-o" pdf dvi)
 			    callback)))
 
+(defun doc-view-pdf-password-protected-p (pdf)
+  "Using Ghostscript, check if a PDF file is password-protected."
+  (with-temp-buffer
+    (apply #'call-process doc-view-ghostscript-program nil (current-buffer)
+           nil `(,@doc-view-ghostscript-options
+                 "-sNODISPLAY"
+                 ,pdf))
+    (goto-char (point-min))
+    (search-forward "This file requires a password for access." nil t)))
+
 (defun doc-view-pdf->png-converter-ghostscript (pdf png page callback)
-  (doc-view-start-process
-   "pdf/ps->png" doc-view-ghostscript-program
-   `(,@doc-view-ghostscript-options
-     ,(format "-r%d" (round doc-view-resolution))
-     ,@(if page `(,(format "-dFirstPage=%d" page)))
-     ,@(if page `(,(format "-dLastPage=%d" page)))
-     ,(concat "-sOutputFile=" png)
-     ,pdf)
-   callback))
+  (let ((pdf-passwd (if (doc-view-pdf-password-protected-p pdf)
+                        (read-passwd "Enter password for PDF file: "))))
+    (doc-view-start-process
+     "pdf/ps->png" doc-view-ghostscript-program
+     `(,@doc-view-ghostscript-options
+       ,(concat "-sDEVICE=" doc-view-ghostscript-device)
+       ,(format "-r%d" (round doc-view-resolution))
+       ,@(if page `(,(format "-dFirstPage=%d" page)))
+       ,@(if page `(,(format "-dLastPage=%d" page)))
+       ,@(if pdf-passwd `(,(format "-sPDFPassword=%s" pdf-passwd)))
+       ,(concat "-sOutputFile=" png)
+       ,pdf)
+     callback)))
 
 (defalias 'doc-view-ps->png-converter-ghostscript
   'doc-view-pdf->png-converter-ghostscript)

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-01-27  2:14 ` Federico Tedin
@ 2019-02-01  9:33   ` Eli Zaretskii
  2019-02-01 10:12     ` Tassilo Horn
  0 siblings, 1 reply; 14+ messages in thread
From: Eli Zaretskii @ 2019-02-01  9:33 UTC (permalink / raw)
  To: Federico Tedin, Tassilo Horn; +Cc: 33684, jidanni

> From: Federico Tedin <federicotedin@gmail.com>
> Date: Sat, 26 Jan 2019 23:14:49 -0300

Thanks.

Tassilo, any comments on the proposal below?  Should I push it?

> 積丹尼 Dan Jacobson <jidanni@jidanni.org> writes:
> 
> > When trying to view a password protected PDF:
> > DocView: process pdf/ps->png changed status to exited abnormally with code 1.
> > Maybe it should ask for the password.
> > emacs-version "25.2.2"
> 
> Would something like this make sense? A short patch that adds
> `doc-view-pdf-password-protected-p', and an extra option to pass to
> Ghostscript (-sPDFPassword=) when the command is invoked (only when the
> PDF file is password protected). I tried it out by creating a
> password-protected PDF using LibreOffice Writer, and then opening it
> with find-file.
> 
> 
> diff --git a/lisp/doc-view.el b/lisp/doc-view.el
> index df8a9fc70f..8691d2a3a6 100644
> --- a/lisp/doc-view.el
> +++ b/lisp/doc-view.el
> @@ -183,11 +183,16 @@ doc-view-pdf->png-converter-function
>  (defcustom doc-view-ghostscript-options
>    '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
>      ;; sources.
> -    "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
> -    "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET")
> +    "-dNOPAUSE" "-dTextAlphaBits=4" "-dBATCH"
> +    "-dGraphicsAlphaBits=4" "-dQUIET")
>    "A list of options to give to ghostscript."
>    :type '(repeat string))
>  
> +(defcustom doc-view-ghostscript-device "png16m"
> +  "Output device to give to ghostscript."
> +  :type 'string
> +  :version "27.1")
> +
>  (defcustom doc-view-resolution 100
>    "Dots per inch resolution used to render the documents.
>  Higher values result in larger images."
> @@ -950,16 +955,30 @@ doc-view-dvi->pdf
>  			    (list "-o" pdf dvi)
>  			    callback)))
>  
> +(defun doc-view-pdf-password-protected-p (pdf)
> +  "Using Ghostscript, check if a PDF file is password-protected."
> +  (with-temp-buffer
> +    (apply #'call-process doc-view-ghostscript-program nil (current-buffer)
> +           nil `(,@doc-view-ghostscript-options
> +                 "-sNODISPLAY"
> +                 ,pdf))
> +    (goto-char (point-min))
> +    (search-forward "This file requires a password for access." nil t)))
> +
>  (defun doc-view-pdf->png-converter-ghostscript (pdf png page callback)
> -  (doc-view-start-process
> -   "pdf/ps->png" doc-view-ghostscript-program
> -   `(,@doc-view-ghostscript-options
> -     ,(format "-r%d" (round doc-view-resolution))
> -     ,@(if page `(,(format "-dFirstPage=%d" page)))
> -     ,@(if page `(,(format "-dLastPage=%d" page)))
> -     ,(concat "-sOutputFile=" png)
> -     ,pdf)
> -   callback))
> +  (let ((pdf-passwd (if (doc-view-pdf-password-protected-p pdf)
> +                        (read-passwd "Enter password for PDF file: "))))
> +    (doc-view-start-process
> +     "pdf/ps->png" doc-view-ghostscript-program
> +     `(,@doc-view-ghostscript-options
> +       ,(concat "-sDEVICE=" doc-view-ghostscript-device)
> +       ,(format "-r%d" (round doc-view-resolution))
> +       ,@(if page `(,(format "-dFirstPage=%d" page)))
> +       ,@(if page `(,(format "-dLastPage=%d" page)))
> +       ,@(if pdf-passwd `(,(format "-sPDFPassword=%s" pdf-passwd)))
> +       ,(concat "-sOutputFile=" png)
> +       ,pdf)
> +     callback)))
>  
>  (defalias 'doc-view-ps->png-converter-ghostscript
>    'doc-view-pdf->png-converter-ghostscript)





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-01  9:33   ` Eli Zaretskii
@ 2019-02-01 10:12     ` Tassilo Horn
  2019-02-01 10:34       ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Tassilo Horn @ 2019-02-01 10:12 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 33684, Federico Tedin, jidanni

Eli Zaretskii <eliz@gnu.org> writes:

Hi Federico, Dan & Eli,

>> From: Federico Tedin <federicotedin@gmail.com>
>> Date: Sat, 26 Jan 2019 23:14:49 -0300
>
> Thanks.
>
> Tassilo, any comments on the proposal below? Should I push it?

It would be great if we could support opening password protected PDFs
also when we're using mupdf as a converter.  That has a "-p" option for
the very same sake which could be set analogously in
`doc-view-pdf->png-converter-mupdf'.

So, yes, please push it with that small addition.

Bye,
Tassilo

>> 積丹尼 Dan Jacobson <jidanni@jidanni.org> writes:
>> 
>> > When trying to view a password protected PDF:
>> > DocView: process pdf/ps->png changed status to exited abnormally with code 1.
>> > Maybe it should ask for the password.
>> > emacs-version "25.2.2"
>> 
>> Would something like this make sense? A short patch that adds
>> `doc-view-pdf-password-protected-p', and an extra option to pass to
>> Ghostscript (-sPDFPassword=) when the command is invoked (only when the
>> PDF file is password protected). I tried it out by creating a
>> password-protected PDF using LibreOffice Writer, and then opening it
>> with find-file.
>> 
>> 
>> diff --git a/lisp/doc-view.el b/lisp/doc-view.el
>> index df8a9fc70f..8691d2a3a6 100644
>> --- a/lisp/doc-view.el
>> +++ b/lisp/doc-view.el
>> @@ -183,11 +183,16 @@ doc-view-pdf->png-converter-function
>>  (defcustom doc-view-ghostscript-options
>>    '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
>>      ;; sources.
>> -    "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
>> -    "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET")
>> +    "-dNOPAUSE" "-dTextAlphaBits=4" "-dBATCH"
>> +    "-dGraphicsAlphaBits=4" "-dQUIET")
>>    "A list of options to give to ghostscript."
>>    :type '(repeat string))
>>  
>> +(defcustom doc-view-ghostscript-device "png16m"
>> +  "Output device to give to ghostscript."
>> +  :type 'string
>> +  :version "27.1")
>> +
>>  (defcustom doc-view-resolution 100
>>    "Dots per inch resolution used to render the documents.
>>  Higher values result in larger images."
>> @@ -950,16 +955,30 @@ doc-view-dvi->pdf
>>  			    (list "-o" pdf dvi)
>>  			    callback)))
>>  
>> +(defun doc-view-pdf-password-protected-p (pdf)
>> +  "Using Ghostscript, check if a PDF file is password-protected."
>> +  (with-temp-buffer
>> +    (apply #'call-process doc-view-ghostscript-program nil (current-buffer)
>> +           nil `(,@doc-view-ghostscript-options
>> +                 "-sNODISPLAY"
>> +                 ,pdf))
>> +    (goto-char (point-min))
>> +    (search-forward "This file requires a password for access." nil t)))
>> +
>>  (defun doc-view-pdf->png-converter-ghostscript (pdf png page callback)
>> -  (doc-view-start-process
>> -   "pdf/ps->png" doc-view-ghostscript-program
>> -   `(,@doc-view-ghostscript-options
>> -     ,(format "-r%d" (round doc-view-resolution))
>> -     ,@(if page `(,(format "-dFirstPage=%d" page)))
>> -     ,@(if page `(,(format "-dLastPage=%d" page)))
>> -     ,(concat "-sOutputFile=" png)
>> -     ,pdf)
>> -   callback))
>> +  (let ((pdf-passwd (if (doc-view-pdf-password-protected-p pdf)
>> +                        (read-passwd "Enter password for PDF file: "))))
>> +    (doc-view-start-process
>> +     "pdf/ps->png" doc-view-ghostscript-program
>> +     `(,@doc-view-ghostscript-options
>> +       ,(concat "-sDEVICE=" doc-view-ghostscript-device)
>> +       ,(format "-r%d" (round doc-view-resolution))
>> +       ,@(if page `(,(format "-dFirstPage=%d" page)))
>> +       ,@(if page `(,(format "-dLastPage=%d" page)))
>> +       ,@(if pdf-passwd `(,(format "-sPDFPassword=%s" pdf-passwd)))
>> +       ,(concat "-sOutputFile=" png)
>> +       ,pdf)
>> +     callback)))
>>  
>>  (defalias 'doc-view-ps->png-converter-ghostscript
>>    'doc-view-pdf->png-converter-ghostscript)
>





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-01 10:12     ` Tassilo Horn
@ 2019-02-01 10:34       ` Eli Zaretskii
  2019-02-01 10:42         ` Federico Tedin
  2019-02-02 20:58         ` Federico Tedin
  0 siblings, 2 replies; 14+ messages in thread
From: Eli Zaretskii @ 2019-02-01 10:34 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: 33684, federicotedin, jidanni

> From: Tassilo Horn <tsdh@gnu.org>
> Cc: Federico Tedin <federicotedin@gmail.com>,  jidanni@jidanni.org,  33684@debbugs.gnu.org
> Date: Fri, 01 Feb 2019 11:12:51 +0100
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> Hi Federico, Dan & Eli,
> 
> >> From: Federico Tedin <federicotedin@gmail.com>
> >> Date: Sat, 26 Jan 2019 23:14:49 -0300
> >
> > Thanks.
> >
> > Tassilo, any comments on the proposal below? Should I push it?
> 
> It would be great if we could support opening password protected PDFs
> also when we're using mupdf as a converter.  That has a "-p" option for
> the very same sake which could be set analogously in
> `doc-view-pdf->png-converter-mupdf'.
> 
> So, yes, please push it with that small addition.

Thanks.

Federico, would you please like to propose a patch with the addition
requested by Tassilo?





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-01 10:34       ` Eli Zaretskii
@ 2019-02-01 10:42         ` Federico Tedin
  2019-02-02 20:58         ` Federico Tedin
  1 sibling, 0 replies; 14+ messages in thread
From: Federico Tedin @ 2019-02-01 10:42 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Tassilo Horn, 33684, jidanni

[-- Attachment #1: Type: text/plain, Size: 28 bytes --]

Sure! I’ll look into it.

[-- Attachment #2: Type: text/html, Size: 50 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-01 10:34       ` Eli Zaretskii
  2019-02-01 10:42         ` Federico Tedin
@ 2019-02-02 20:58         ` Federico Tedin
  2019-02-03  8:40           ` Tassilo Horn
  1 sibling, 1 reply; 14+ messages in thread
From: Federico Tedin @ 2019-02-02 20:58 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Tassilo Horn, 33684, jidanni

[-- Attachment #1: Type: text/plain, Size: 715 bytes --]

Here's the new version of the patch, which enables using MuPDF to open
password-protected PDF files.

One problem I encountered while writing it is that function
`doc-view-pdf->png-converter-mupdf' uses a small hack to add "draw" to
the arguments list passed to `doc-view-start-process', only when
`doc-view-pdfdraw-program' has the value "mutool". This is because the
"mudraw" command has been replaced at some point by the "mutool"
command, which requires passing "draw" as a subcommand to do the same
work. I ended up using the same hack in the new function I created, with
a reference to the original one, but I'm not sure this was the best
possible approach. Is there a cleaner way to solve this?

- Federico


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 4418 bytes --]

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index df8a9fc70f..b690fb1bd6 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -183,11 +183,16 @@ doc-view-pdf->png-converter-function
 (defcustom doc-view-ghostscript-options
   '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
     ;; sources.
-    "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
+    "-dNOPAUSE" "-dTextAlphaBits=4"
     "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET")
   "A list of options to give to ghostscript."
   :type '(repeat string))
 
+(defcustom doc-view-ghostscript-device "png16m"
+  "Output device to give to ghostscript."
+  :type 'string
+  :version "27.1")
+
 (defcustom doc-view-resolution 100
   "Dots per inch resolution used to render the documents.
 Higher values result in larger images."
@@ -950,16 +955,31 @@ doc-view-dvi->pdf
 			    (list "-o" pdf dvi)
 			    callback)))
 
+(defun doc-view-pdf-password-protected-ghostscript-p (pdf)
+  "Using Ghostscript, check if a PDF file is password-protected. If it
+is, return non-nil."
+  (with-temp-buffer
+    (apply #'call-process doc-view-ghostscript-program nil (current-buffer)
+           nil `(,@doc-view-ghostscript-options
+                 "-sNODISPLAY"
+                 ,pdf))
+    (goto-char (point-min))
+    (search-forward "This file requires a password for access." nil t)))
+
 (defun doc-view-pdf->png-converter-ghostscript (pdf png page callback)
-  (doc-view-start-process
-   "pdf/ps->png" doc-view-ghostscript-program
-   `(,@doc-view-ghostscript-options
-     ,(format "-r%d" (round doc-view-resolution))
-     ,@(if page `(,(format "-dFirstPage=%d" page)))
-     ,@(if page `(,(format "-dLastPage=%d" page)))
-     ,(concat "-sOutputFile=" png)
-     ,pdf)
-   callback))
+  (let ((pdf-passwd (if (doc-view-pdf-password-protected-ghostscript-p pdf)
+                        (read-passwd "Enter password for PDF file: "))))
+    (doc-view-start-process
+     "pdf/ps->png" doc-view-ghostscript-program
+     `(,@doc-view-ghostscript-options
+       ,(concat "-sDEVICE=" doc-view-ghostscript-device)
+       ,(format "-r%d" (round doc-view-resolution))
+       ,@(if page `(,(format "-dFirstPage=%d" page)))
+       ,@(if page `(,(format "-dLastPage=%d" page)))
+       ,@(if pdf-passwd `(,(format "-sPDFPassword=%s" pdf-passwd)))
+       ,(concat "-sOutputFile=" png)
+       ,pdf)
+     callback)))
 
 (defalias 'doc-view-ps->png-converter-ghostscript
   'doc-view-pdf->png-converter-ghostscript)
@@ -980,17 +1000,33 @@ doc-view-djvu->tiff-converter-ddjvu
      ,tiff)
    callback))
 
+(defun doc-view-pdf-password-protected-pdfdraw-p (pdf)
+  "Using MuPDF, check if a PDF file is password-protected. If it is,
+return non-nil."
+  (with-temp-buffer
+    (apply #'call-process doc-view-pdfdraw-program nil (current-buffer) nil
+           ;; FIXME: See FIXME comment in `doc-view-pdf->png-converter-mupdf'.
+           `(,@(if (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program) '("draw"))
+             ,(concat "-o" null-device)
+             ;; In case PDF isn't password-protected, "draw" only one page.
+             ,pdf "1"))
+    (goto-char (point-min))
+    (search-forward "error: cannot authenticate password" nil t)))
+
 (defun doc-view-pdf->png-converter-mupdf (pdf png page callback)
-  (doc-view-start-process
-   "pdf->png" doc-view-pdfdraw-program
-   ;; FIXME: Ugly hack: recent mupdf distribution replaced "mudraw" with
-   ;; "mutool draw".
-   `(,@(if (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program) '("draw"))
-     ,(concat "-o" png)
-     ,(format "-r%d" (round doc-view-resolution))
-     ,pdf
-     ,@(if page `(,(format "%d" page))))
-   callback))
+  (let ((pdf-passwd (if (doc-view-pdf-password-protected-pdfdraw-p pdf)
+                        (read-passwd "Enter password for PDF file: "))))
+    (doc-view-start-process
+     "pdf->png" doc-view-pdfdraw-program
+     ;; FIXME: Ugly hack: recent mupdf distribution replaced "mudraw" with
+     ;; "mutool draw".
+     `(,@(if (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program) '("draw"))
+       ,(concat "-o" png)
+       ,(format "-r%d" (round doc-view-resolution))
+       ,@(if pdf-passwd `("-p" ,pdf-passwd))
+       ,pdf
+       ,@(if page `(,(format "%d" page))))
+     callback)))
 
 (defun doc-view-odf->pdf-converter-unoconv (odf callback)
   "Convert ODF to PDF asynchronously and call CALLBACK when finished.

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-02 20:58         ` Federico Tedin
@ 2019-02-03  8:40           ` Tassilo Horn
  2019-02-03 16:55             ` Federico Tedin
  0 siblings, 1 reply; 14+ messages in thread
From: Tassilo Horn @ 2019-02-03  8:40 UTC (permalink / raw)
  To: Federico Tedin; +Cc: 33684, jidanni

Federico Tedin <federicotedin@gmail.com> writes:

Hi Federico,

> Here's the new version of the patch, which enables using MuPDF to open
> password-protected PDF files.
>
> One problem I encountered while writing it is that function
> `doc-view-pdf->png-converter-mupdf' uses a small hack to add "draw" to
> the arguments list passed to `doc-view-start-process', only when
> `doc-view-pdfdraw-program' has the value "mutool". This is because the
> "mudraw" command has been replaced at some point by the "mutool"
> command, which requires passing "draw" as a subcommand to do the same
> work. I ended up using the same hack in the new function I created,
> with a reference to the original one, but I'm not sure this was the
> best possible approach. Is there a cleaner way to solve this?

You could have extracted that into its own function, e.g.,

(defun doc-view-pdfdraw-program-subcommand ()
  "Return the mutool subcommand replacing mudraw.

Recent mupdf distribution replaced mudraw with `mutool draw'."
  (when (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program)
    '("draw")))

and use that at those two places.

Could you please commit the patch locally (including the ChangeLog style
commit message) and send it exported with "git format-patch"?

Bye,
Tassilo

PS: I had acually also accepted using the ghostscript password check
with mupdf, too.  But since you don't need ghostscript for PDFs if you
have mupdf, your approach is even better.






^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-03  8:40           ` Tassilo Horn
@ 2019-02-03 16:55             ` Federico Tedin
  2019-02-04  5:44               ` Tassilo Horn
  0 siblings, 1 reply; 14+ messages in thread
From: Federico Tedin @ 2019-02-03 16:55 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: 33684, jidanni

[-- Attachment #1: Type: text/plain, Size: 157 bytes --]

Hi Tassilo, thanks for your feedback. I've created a new patch with the
commit message, and also added an entry to NEWS. I'm attaching it here.

- Federico


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 5772 bytes --]

From 0fb8d45a3ab0868e0f1d81f00b0f43771510deb1 Mon Sep 17 00:00:00 2001
From: Federico Tedin <federicotedin@gmail.com>
Date: Sun, 3 Feb 2019 13:48:31 -0300
Subject: [PATCH 1/1] Allow doc-view to open password-protected PDF files

- lisp/doc-view.el (doc-view-ghostscript-options): Removed "-sDEVICE"
  option.
  (doc-view-ghostscript-device): New customizable variable, passed as
  "-sDEVICE" option to GhostScript.
  (doc-view-pdf-password-protected-ghostscript-p): New function.
  (doc-view-pdf->png-converter-ghostscript): Can now open
  password-protected PDF files.
  (doc-view-pdfdraw-program-subcommand): New function.
  (doc-view-pdf-password-protected-pdfdraw-p): New function.
  (doc-view-pdf->png-converter-mupdf): Can now open password-protected
  PDF files.
- etc/NEWS: Mention new doc-view-mode feature.
---
 etc/NEWS         |  1 +
 lisp/doc-view.el | 79 ++++++++++++++++++++++++++++++++++++------------
 2 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index cac379fe7e..8aa646776c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -329,6 +329,7 @@ the node "(emacs) Directory Variables" of the user manual.
 
 ** doc-view-mode
 *** New commands doc-view-presentation and doc-view-fit-window-to-page
+*** Added support for password-protected PDF files
 
 ** map.el
 *** Now also understands plists.
diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index df8a9fc70f..a5fdb91a36 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -183,11 +183,16 @@ doc-view-pdf->png-converter-function
 (defcustom doc-view-ghostscript-options
   '("-dSAFER" ;; Avoid security problems when rendering files from untrusted
     ;; sources.
-    "-dNOPAUSE" "-sDEVICE=png16m" "-dTextAlphaBits=4"
+    "-dNOPAUSE" "-dTextAlphaBits=4"
     "-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET")
   "A list of options to give to ghostscript."
   :type '(repeat string))
 
+(defcustom doc-view-ghostscript-device "png16m"
+  "Output device to give to ghostscript."
+  :type 'string
+  :version "27.1")
+
 (defcustom doc-view-resolution 100
   "Dots per inch resolution used to render the documents.
 Higher values result in larger images."
@@ -950,16 +955,31 @@ doc-view-dvi->pdf
 			    (list "-o" pdf dvi)
 			    callback)))
 
+(defun doc-view-pdf-password-protected-ghostscript-p (pdf)
+  "Using Ghostscript, check if a PDF file is password-protected. If it
+is, return non-nil."
+  (with-temp-buffer
+    (apply #'call-process doc-view-ghostscript-program nil (current-buffer)
+           nil `(,@doc-view-ghostscript-options
+                 "-sNODISPLAY"
+                 ,pdf))
+    (goto-char (point-min))
+    (search-forward "This file requires a password for access." nil t)))
+
 (defun doc-view-pdf->png-converter-ghostscript (pdf png page callback)
-  (doc-view-start-process
-   "pdf/ps->png" doc-view-ghostscript-program
-   `(,@doc-view-ghostscript-options
-     ,(format "-r%d" (round doc-view-resolution))
-     ,@(if page `(,(format "-dFirstPage=%d" page)))
-     ,@(if page `(,(format "-dLastPage=%d" page)))
-     ,(concat "-sOutputFile=" png)
-     ,pdf)
-   callback))
+  (let ((pdf-passwd (if (doc-view-pdf-password-protected-ghostscript-p pdf)
+                        (read-passwd "Enter password for PDF file: "))))
+    (doc-view-start-process
+     "pdf/ps->png" doc-view-ghostscript-program
+     `(,@doc-view-ghostscript-options
+       ,(concat "-sDEVICE=" doc-view-ghostscript-device)
+       ,(format "-r%d" (round doc-view-resolution))
+       ,@(if page `(,(format "-dFirstPage=%d" page)))
+       ,@(if page `(,(format "-dLastPage=%d" page)))
+       ,@(if pdf-passwd `(,(format "-sPDFPassword=%s" pdf-passwd)))
+       ,(concat "-sOutputFile=" png)
+       ,pdf)
+     callback)))
 
 (defalias 'doc-view-ps->png-converter-ghostscript
   'doc-view-pdf->png-converter-ghostscript)
@@ -980,17 +1000,36 @@ doc-view-djvu->tiff-converter-ddjvu
      ,tiff)
    callback))
 
+(defun doc-view-pdfdraw-program-subcommand ()
+  "Return the mutool subcommand replacing mudraw.
+Recent MuPDF distributions replaced 'mudraw' with 'mutool draw'."
+  (when (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program)
+    '("draw")))
+
+(defun doc-view-pdf-password-protected-pdfdraw-p (pdf)
+  "Using MuPDF, check if a PDF file is password-protected. If it is,
+return non-nil."
+  (with-temp-buffer
+    (apply #'call-process doc-view-pdfdraw-program nil (current-buffer) nil
+           `(,@(doc-view-pdfdraw-program-subcommand)
+             ,(concat "-o" null-device)
+             ;; In case PDF isn't password-protected, "draw" only one page.
+             ,pdf "1"))
+    (goto-char (point-min))
+    (search-forward "error: cannot authenticate password" nil t)))
+
 (defun doc-view-pdf->png-converter-mupdf (pdf png page callback)
-  (doc-view-start-process
-   "pdf->png" doc-view-pdfdraw-program
-   ;; FIXME: Ugly hack: recent mupdf distribution replaced "mudraw" with
-   ;; "mutool draw".
-   `(,@(if (string-match "mutool[^/\\]*$" doc-view-pdfdraw-program) '("draw"))
-     ,(concat "-o" png)
-     ,(format "-r%d" (round doc-view-resolution))
-     ,pdf
-     ,@(if page `(,(format "%d" page))))
-   callback))
+  (let ((pdf-passwd (if (doc-view-pdf-password-protected-pdfdraw-p pdf)
+                        (read-passwd "Enter password for PDF file: "))))
+    (doc-view-start-process
+     "pdf->png" doc-view-pdfdraw-program
+     `(,@(doc-view-pdfdraw-program-subcommand)
+       ,(concat "-o" png)
+       ,(format "-r%d" (round doc-view-resolution))
+       ,@(if pdf-passwd `("-p" ,pdf-passwd))
+       ,pdf
+       ,@(if page `(,(format "%d" page))))
+     callback)))
 
 (defun doc-view-odf->pdf-converter-unoconv (odf callback)
   "Convert ODF to PDF asynchronously and call CALLBACK when finished.
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-03 16:55             ` Federico Tedin
@ 2019-02-04  5:44               ` Tassilo Horn
  2019-02-04  6:29                 ` Eli Zaretskii
  0 siblings, 1 reply; 14+ messages in thread
From: Tassilo Horn @ 2019-02-04  5:44 UTC (permalink / raw)
  To: Federico Tedin; +Cc: 33684, jidanni

Federico Tedin <federicotedin@gmail.com> writes:

Hi Federico,

> Hi Tassilo, thanks for your feedback. I've created a new patch with
> the commit message, and also added an entry to NEWS. I'm attaching it
> here.

Looks perfect.  Small nitpick: we use * for bullet points instead of -
in the commit message, but I can amend that.  I can't push it from work,
so I'll do it this evening.

Thanks a lot for improving doc-view!
Tassilo





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-04  5:44               ` Tassilo Horn
@ 2019-02-04  6:29                 ` Eli Zaretskii
  2019-02-04  6:52                   ` Tassilo Horn
  2019-02-04 15:49                   ` Tassilo Horn
  0 siblings, 2 replies; 14+ messages in thread
From: Eli Zaretskii @ 2019-02-04  6:29 UTC (permalink / raw)
  To: Tassilo Horn, Federico Tedin; +Cc: 33684, jidanni

On February 4, 2019 7:44:43 AM GMT+02:00, Tassilo Horn <tsdh@gnu.org> wrote:
> Federico Tedin <federicotedin@gmail.com> writes:
> 
> Hi Federico,
> 
> > Hi Tassilo, thanks for your feedback. I've created a new patch with
> > the commit message, and also added an entry to NEWS. I'm attaching
> it
> > here.
> 
> Looks perfect.  Small nitpick: we use * for bullet points instead of -
> in the commit message, but I can amend that.  I can't push it from
> work,
> so I'll do it this evening.


When you do, please be sure to amend the log message to mention the bug number.

Also, the doc strings of 2 of the new functions need to be reformatted to have the first line be a complete sentence.  (Just move the second sentence to the next line, I'd say.)

Thanks.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-04  6:29                 ` Eli Zaretskii
@ 2019-02-04  6:52                   ` Tassilo Horn
  2019-02-04 15:49                   ` Tassilo Horn
  1 sibling, 0 replies; 14+ messages in thread
From: Tassilo Horn @ 2019-02-04  6:52 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 33684, Federico Tedin, jidanni

Eli Zaretskii <eliz@gnu.org> writes:

>> Looks perfect.  Small nitpick: we use * for bullet points instead of -
>> in the commit message, but I can amend that.  I can't push it from
>> work, so I'll do it this evening.
>
> When you do, please be sure to amend the log message to mention the
> bug number.

Ah, right!

> Also, the doc strings of 2 of the new functions need to be reformatted
> to have the first line be a complete sentence.  (Just move the second
> sentence to the next line, I'd say.)

Will do.

Bye,
Tassilo





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-04  6:29                 ` Eli Zaretskii
  2019-02-04  6:52                   ` Tassilo Horn
@ 2019-02-04 15:49                   ` Tassilo Horn
  2019-02-04 21:24                     ` Federico Tedin
  1 sibling, 1 reply; 14+ messages in thread
From: Tassilo Horn @ 2019-02-04 15:49 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Federico Tedin, 33684-done, jidanni

Eli Zaretskii <eliz@gnu.org> writes:

>> > Hi Tassilo, thanks for your feedback. I've created a new patch with
>> > the commit message, and also added an entry to NEWS. I'm attaching
>> it
>> > here.
>> 
>> Looks perfect.  Small nitpick: we use * for bullet points instead of
>> - in the commit message, but I can amend that.  I can't push it from
>> work, so I'll do it this evening.
>
> When you do, please be sure to amend the log message to mention the
> bug number.
>
> Also, the doc strings of 2 of the new functions need to be reformatted
> to have the first line be a complete sentence.  (Just move the second
> sentence to the next line, I'd say.)

Pushed with the aforementioned changes.

Thanks again, Federico!
Tassilo





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#33684: DocView bombs out upon password protected PDFs
  2019-02-04 15:49                   ` Tassilo Horn
@ 2019-02-04 21:24                     ` Federico Tedin
  0 siblings, 0 replies; 14+ messages in thread
From: Federico Tedin @ 2019-02-04 21:24 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: 33684-done, jidanni

> Pushed with the aforementioned changes.
>
> Thanks again, Federico!
> Tassilo

No problem! Thank you.





^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2019-02-04 21:24 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-12-09 10:52 bug#33684: DocView bombs out upon password protected PDFs 積丹尼 Dan Jacobson
2019-01-27  2:14 ` Federico Tedin
2019-02-01  9:33   ` Eli Zaretskii
2019-02-01 10:12     ` Tassilo Horn
2019-02-01 10:34       ` Eli Zaretskii
2019-02-01 10:42         ` Federico Tedin
2019-02-02 20:58         ` Federico Tedin
2019-02-03  8:40           ` Tassilo Horn
2019-02-03 16:55             ` Federico Tedin
2019-02-04  5:44               ` Tassilo Horn
2019-02-04  6:29                 ` Eli Zaretskii
2019-02-04  6:52                   ` Tassilo Horn
2019-02-04 15:49                   ` Tassilo Horn
2019-02-04 21:24                     ` Federico Tedin

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).