Debugging for encyption/permissions

This commit is contained in:
John Whitington 2014-10-24 17:24:29 +01:00
parent 898776ee73
commit f689fa8f20
1 changed files with 30 additions and 15 deletions

View File

@ -326,6 +326,7 @@ type args =
mutable no_assemble : bool; mutable no_assemble : bool;
mutable no_hq_print : bool; mutable no_hq_print : bool;
mutable debug : bool; mutable debug : bool;
mutable debugcrypt : bool;
mutable boxes : bool; mutable boxes : bool;
mutable encrypt_metadata : bool; mutable encrypt_metadata : bool;
mutable retain_numbering : bool; mutable retain_numbering : bool;
@ -406,6 +407,7 @@ let args =
no_assemble = false; no_assemble = false;
no_hq_print = false; no_hq_print = false;
debug = false; debug = false;
debugcrypt = false;
boxes = false; boxes = false;
encrypt_metadata = true; encrypt_metadata = true;
retain_numbering = false; retain_numbering = false;
@ -481,6 +483,7 @@ let reset_arguments () =
args.no_assemble <- false; args.no_assemble <- false;
args.no_hq_print <- false; args.no_hq_print <- false;
args.debug <- false; args.debug <- false;
args.debugcrypt <- false;
args.boxes <- false; args.boxes <- false;
args.encrypt_metadata <- true; args.encrypt_metadata <- true;
args.retain_numbering <- false; args.retain_numbering <- false;
@ -539,6 +542,7 @@ let operation_allowed banlist op =
| Some op -> not (banned banlist op) | Some op -> not (banned banlist op)
let rec decrypt_if_necessary (_, _, _, user_pw, owner_pw) op pdf = let rec decrypt_if_necessary (_, _, _, user_pw, owner_pw) op pdf =
if args.debugcrypt then
begin match op with begin match op with
None -> flprint "decrypt_if_necessary: op = None\n" None -> flprint "decrypt_if_necessary: op = None\n"
| Some x -> Printf.printf "decrypt_if_necessary: op = %s\n" (string_of_op x) | Some x -> Printf.printf "decrypt_if_necessary: op = %s\n" (string_of_op x)
@ -546,12 +550,12 @@ let rec decrypt_if_necessary (_, _, _, user_pw, owner_pw) op pdf =
if not (Pdfcrypt.is_encrypted pdf) then pdf else if not (Pdfcrypt.is_encrypted pdf) then pdf else
match Pdfcrypt.decrypt_pdf_owner owner_pw pdf with match Pdfcrypt.decrypt_pdf_owner owner_pw pdf with
| Some pdf -> | Some pdf ->
Printf.printf "Managed to decrypt with owner password"; pdf if args.debugcrypt then Printf.printf "Managed to decrypt with owner password\n"; pdf
| _ -> | _ ->
Printf.printf "Couldn't decrypt with owner password %s\n" owner_pw; if args.debugcrypt then Printf.printf "Couldn't decrypt with owner password %s\n" owner_pw;
match Pdfcrypt.decrypt_pdf user_pw pdf with match Pdfcrypt.decrypt_pdf user_pw pdf with
| Some pdf, permissions -> | Some pdf, permissions ->
Printf.printf "Managed to decrypt with user password\n"; if args.debugcrypt then Printf.printf "Managed to decrypt with user password\n";
if operation_allowed permissions op if operation_allowed permissions op
then pdf then pdf
else soft_error "User password cannot give permission for this operation" else soft_error "User password cannot give permission for this operation"
@ -1057,6 +1061,9 @@ let setdebug () =
set Pdfcrypt.crypt_debug; set Pdfcrypt.crypt_debug;
args.debug <- true args.debug <- true
let setdebugcrypt () =
args.debugcrypt <- true
let setboxes () = let setboxes () =
args.boxes <- true args.boxes <- true
@ -1792,6 +1799,7 @@ and specs =
("-flat-kids", Arg.Unit setflatkids, ""); ("-flat-kids", Arg.Unit setflatkids, "");
("-gs", Arg.String setgspath, ""); ("-gs", Arg.String setgspath, "");
("-debug", Arg.Unit setdebug, ""); ("-debug", Arg.Unit setdebug, "");
("-debug-crypt", Arg.Unit setdebugcrypt, "");
("-fix-prince", Arg.Unit (setop RemoveUnusedResources), ""); ("-fix-prince", Arg.Unit (setop RemoveUnusedResources), "");
("-extract-text", Arg.Unit (setop ExtractText), "")] ("-extract-text", Arg.Unit (setop ExtractText), "")]
@ -1888,14 +1896,21 @@ let really_write_pdf ?(encryption = None) mk_id pdf outname =
then Filename.temp_file "cpdflin" ".pdf" then Filename.temp_file "cpdflin" ".pdf"
else outname else outname
in in
begin if args.recrypt && args.was_encrypted then begin
if args.recrypt && args.was_encrypted then
begin
if args.debugcrypt then Printf.printf "Recrypting in really_write_pdf\n";
Pdfwrite.pdf_to_file_recrypting Pdfwrite.pdf_to_file_recrypting
(get_single_pdf_nodecrypt false) pdf args.user outname' (get_single_pdf_nodecrypt false) pdf args.user outname'
end
else else
begin
if args.debugcrypt then Printf.printf "Pdf to file in really_write_pdf\n";
Pdfwrite.pdf_to_file_options Pdfwrite.pdf_to_file_options
~preserve_objstm:args.preserve_objstm ~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm ~generate_objstm:args.create_objstm
false encryption mk_id pdf outname' false encryption mk_id pdf outname'
end
end; end;
begin begin
if args.linearize then if args.linearize then