From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Federico Tedin Newsgroups: gmane.emacs.bugs Subject: bug#33684: DocView bombs out upon password protected PDFs Date: Sat, 02 Feb 2019 17:58:00 -0300 Message-ID: <875zu1x607.fsf@gmail.com> References: <87d0qb3rb1.fsf@jidanni.org> <87sgxesv6u.fsf@gmail.com> <83womjyhrw.fsf@gnu.org> <87h8dn7r6k.fsf@gnu.org> <83pnsbyeyt.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="107302"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: Tassilo Horn , 33684@debbugs.gnu.org, jidanni@jidanni.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat Feb 02 21:59:20 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gq2Ni-000RmC-2O for geb-bug-gnu-emacs@m.gmane.org; Sat, 02 Feb 2019 21:59:18 +0100 Original-Received: from localhost ([127.0.0.1]:47878 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gq2Ng-0002VS-Ve for geb-bug-gnu-emacs@m.gmane.org; Sat, 02 Feb 2019 15:59:17 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:43095) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gq2NX-0002VH-7Z for bug-gnu-emacs@gnu.org; Sat, 02 Feb 2019 15:59:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gq2NV-0000h8-0l for bug-gnu-emacs@gnu.org; Sat, 02 Feb 2019 15:59:07 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57343) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gq2NU-0000gq-JE for bug-gnu-emacs@gnu.org; Sat, 02 Feb 2019 15:59:04 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1gq2NS-0005mQ-0P for bug-gnu-emacs@gnu.org; Sat, 02 Feb 2019 15:59:04 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Federico Tedin Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 02 Feb 2019 20:59:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33684 X-GNU-PR-Package: emacs Original-Received: via spool by 33684-submit@debbugs.gnu.org id=B33684.154914109322159 (code B ref 33684); Sat, 02 Feb 2019 20:59:01 +0000 Original-Received: (at 33684) by debbugs.gnu.org; 2 Feb 2019 20:58:13 +0000 Original-Received: from localhost ([127.0.0.1]:56624 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gq2Me-0005lL-Mf for submit@debbugs.gnu.org; Sat, 02 Feb 2019 15:58:13 -0500 Original-Received: from mail-qt1-f179.google.com ([209.85.160.179]:33045) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gq2Mc-0005l8-Hc for 33684@debbugs.gnu.org; Sat, 02 Feb 2019 15:58:10 -0500 Original-Received: by mail-qt1-f179.google.com with SMTP id l11so11734304qtp.0 for <33684@debbugs.gnu.org>; Sat, 02 Feb 2019 12:58:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=ITR5IlfZ3IzL6OTlQPXGvW6lKBIirM23Tywe6ov0ik8=; b=Jq2R4ixZL5yHMTF+vo5mD8pk3efJ+fBhHIRvuspA3O0yQjfQGgJNJZ2wa8Ylv3ui4g hUoAYEulmJZH4FtJ+tchbpuFyYg/sGCfyK9VfbsDQOKXMl0lvxWsOwYz9iGo82HSbQoV 96Gn24Hx8OupezY47i0zI5WHzUMK0rRtm6KanCNxsQO3HuEtZf0RdmxvmTEkcr1OoKFU IA7llPht0GCUy6sGc4gFVtyiiF9ww9Ei/DkSz3eyNhJfSpI/fkFTZ0exmyPkIr3D4Uk4 2b2G6HNhsyvc1KiMAcOmK6Z4vWF6QMTuKT1OVSxwZwVx4XWw4C1oPTIb5dybQqe4dF0E ligA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=ITR5IlfZ3IzL6OTlQPXGvW6lKBIirM23Tywe6ov0ik8=; b=ialac5rnx4svkAOonVZY7VqVBHEGivVqy04EIHd7VmxGjAQk7HA0cK2zFwnWSBUosX HIorFpxwM4MIfIddSFJvEVhHIC0gx7vkX6LAdxSt/cVDtn64UDcK5kR0du8Y5F+SPV3Q 61JwVOxdhD3Dpl7qtsr/gqFm/WVPQ0H+9Cm/QIKt5ynNnAK1zPBhqNEPN2AIpCbl4NI6 Kdd3s8j4qoq1QoGD6HeEAvKsBHqXjkTovcNAO540ZPIZXvjuf9vakEufqpvnIkgpvsm/ /AJzGzKbzaSTr+AT+9cN1sSqJxf2L83Bhni3NKJmlTUhl1Ux41dCM43fzUvLW4po6UT7 Ju/g== X-Gm-Message-State: AHQUAuZQXXWbU74RbKM4wuubo1RGtJUrhq7FfIula5HqVvw22SDxFpgp 5SZrUEzt0gG5fqtpodpG2rc= X-Google-Smtp-Source: AHgI3IZluAKPfeRZrv4+lJHClrvltV76izveAcGWJeMwbYoSwceYj6QSYCghMJ22dfLHcvpspnfNKQ== X-Received: by 2002:ac8:191d:: with SMTP id t29mr850337qtj.387.1549141083878; Sat, 02 Feb 2019 12:58:03 -0800 (PST) Original-Received: from hideo ([181.167.176.220]) by smtp.gmail.com with ESMTPSA id f33sm1036069qta.91.2019.02.02.12.58.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 02 Feb 2019 12:58:03 -0800 (PST) In-Reply-To: <83pnsbyeyt.fsf@gnu.org> (Eli Zaretskii's message of "Fri, 01 Feb 2019 12:34:34 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:155035 Archived-At: --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=pdf.patch Content-Description: patch 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. --=-=-=--