/* ENCRYPT Do the actual encryption process. Takes the plaintext from the INPUT command, writes the ciphertext to the file descriptor set with the OUTPUT command, take the recipients from all the recipients set so far with RECIPIENTS. If this command fails the clients should try to delete all output currently done or otherwise mark it as invalid. GPG does ensure that there won't be any security problem with leftover data on the output in this case. In most cases this command won't fail because most necessary checks have been done while setting the recipients. However some checks can only be done right here and thus error may occur anyway (for example, no recipients at all). The input, output and message pipes are closed after this command. */ static gpg_error_t cmd_encrypt (assuan_context_t ctx, char *line) { ctrl_t ctrl = assuan_get_pointer (ctx); gpg_error_t err; int inp_fd, out_fd; (void)line; /* LINE is not used. */ if ( !ctrl->server_local->recplist ) { write_status_text (STATUS_NO_RECP, "0"); err = gpg_error (GPG_ERR_NO_USER_ID); goto leave; } inp_fd = translate_sys2libc_fd (assuan_get_input_fd (ctx), 0); if (inp_fd == -1) { err = set_error (GPG_ERR_ASS_NO_INPUT, NULL); goto leave; } out_fd = translate_sys2libc_fd (assuan_get_output_fd (ctx), 1); if (out_fd == -1) { err = set_error (GPG_ERR_ASS_NO_OUTPUT, NULL); goto leave; } /* Fixme: Check that we are using real files and not pipes if in PGP-2 mode. Do all the other checks we do in gpg.c for aEncr. Maybe we should drop the PGP2 compatibility. */ /* FIXME: GPGSM does this here: Add all encrypt-to marked recipients from the default list. */ /* fixme: err = ctrl->audit? 0 : start_audit_session (ctrl);*/ err = encrypt_crypt (ctrl, inp_fd, NULL, NULL, 0, ctrl->server_local->recplist, out_fd); leave: /* Release the recipient list on success. */ if (!err) { release_pk_list (ctrl->server_local->recplist); ctrl->server_local->recplist = NULL; } /* Close and reset the fds. */ close_message_fd (ctrl); assuan_close_input_fd (ctx); assuan_close_output_fd (ctx); if (err) log_error ("command '%s' failed: %s\n", "ENCRYPT", gpg_strerror (err)); return err; }
void encrypt_crypt_files (ctrl_t ctrl, int nfiles, char **files, strlist_t remusr) { int rc = 0; if (opt.outfile) { log_error(_("--output doesn't work for this command\n")); return; } if (!nfiles) { char line[2048]; unsigned int lno = 0; while ( 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); return; } line[strlen(line)-1] = '\0'; print_file_status(STATUS_FILE_START, line, 2); rc = encrypt_crypt (ctrl, -1, line, remusr, 0, NULL, -1); if (rc) log_error ("encryption of '%s' failed: %s\n", print_fname_stdin(line), gpg_strerror (rc) ); write_status( STATUS_FILE_DONE ); } } else { while (nfiles--) { print_file_status(STATUS_FILE_START, *files, 2); if ( (rc = encrypt_crypt (ctrl, -1, *files, remusr, 0, NULL, -1)) ) log_error("encryption of '%s' failed: %s\n", print_fname_stdin(*files), gpg_strerror (rc) ); write_status( STATUS_FILE_DONE ); files++; } } }