31 |
* |
* |
32 |
*******************************************************************/ |
*******************************************************************/ |
33 |
#include <iostream> |
#include <iostream> |
34 |
|
#include <gpgme.h> |
35 |
|
#include <math.h> |
36 |
|
#include <sys/wait.h> |
37 |
|
#include <signal.h> |
38 |
|
#include <stdio.h> |
39 |
|
#include <sys/types.h> |
40 |
|
#include <unistd.h> |
41 |
|
|
42 |
|
#include "misc.hh" |
43 |
#include "action.hh" |
#include "action.hh" |
44 |
#include "language/language.hh" |
#include "language/language.hh" |
45 |
|
|
46 |
int decrypt() |
#define MAXSIZE 2048 |
47 |
|
|
48 |
|
const char* GPG_PASSPHRASE; |
49 |
|
|
50 |
|
struct passphrase_cb_info_s { |
51 |
|
GpgmeCtx c; |
52 |
|
int did_it; |
53 |
|
}; |
54 |
|
static const char *passphrase_cb(void *hook, const char *desc, void **r_hd); |
55 |
|
|
56 |
|
static void change_ext(std::string path, std::string *outfilename) |
57 |
|
{ |
58 |
|
|
59 |
|
*outfilename = path; |
60 |
|
*outfilename += ".decrypt"; |
61 |
|
return ; |
62 |
|
} |
63 |
|
|
64 |
|
int decrypt_file(std::string path, std::string passphrase) |
65 |
{ |
{ |
66 |
|
/* int ret,cnt=0; |
67 |
|
char outfilename[MAXSIZE]; |
68 |
|
char *buf_ptr;*/ |
69 |
|
|
70 |
|
char buf[100]; |
71 |
|
FILE*outfile; |
72 |
|
GpgmeCtx ctx; |
73 |
|
GpgmeError err; |
74 |
|
GpgmeData in, out, pwdata = NULL; |
75 |
|
struct passphrase_cb_info_s info; |
76 |
|
// char c; |
77 |
|
size_t nread; |
78 |
std::cout << BEGINDECRYPT << std::endl; |
std::cout << BEGINDECRYPT << std::endl; |
79 |
std::cout << ENDDECRYPT << std::endl << std::endl; |
std::string outfilename; |
80 |
return 0; |
change_ext(path, &outfilename); |
81 |
|
std::cout << "---------------------->" << outfilename << std::endl; |
82 |
|
|
83 |
|
GPG_PASSPHRASE = passphrase.c_str(); |
84 |
|
|
85 |
|
err = gpgme_new (&ctx); |
86 |
|
if (err) exit(1); |
87 |
|
if ( !getenv("GPG_AGENT_INFO") ) |
88 |
|
{ |
89 |
|
memset ( &info, 0, sizeof info ); |
90 |
|
info.c = ctx; |
91 |
|
gpgme_set_passphrase_cb ( ctx, |
92 |
|
passphrase_cb, |
93 |
|
&info); |
94 |
|
} |
95 |
|
|
96 |
|
err = gpgme_data_new_from_file ( &in, path.c_str(), 1); |
97 |
|
if (err) exit(1); |
98 |
|
|
99 |
|
err = gpgme_data_new ( &out ); |
100 |
|
if (err) exit(1); |
101 |
|
|
102 |
|
err = gpgme_op_decrypt (ctx, in, out ); |
103 |
|
|
104 |
|
if (err) |
105 |
|
{ |
106 |
|
return(0); |
107 |
|
} |
108 |
|
|
109 |
|
fflush (NULL); |
110 |
|
|
111 |
|
err = gpgme_data_rewind ( out ); |
112 |
|
if (err) exit(1); |
113 |
|
outfile=fopen(outfilename.c_str(),"w"); |
114 |
|
while ( !(err = gpgme_data_read ( out, buf, 100, &nread )) ) |
115 |
|
{ |
116 |
|
|
117 |
|
fwrite ( buf, nread, 1, outfile ); |
118 |
|
} |
119 |
|
fclose(outfile); |
120 |
|
gpgme_data_release (in); |
121 |
|
gpgme_data_release (out); |
122 |
|
gpgme_data_release (pwdata); |
123 |
|
gpgme_release (ctx); |
124 |
|
// strcpy(filename,outfilename); |
125 |
|
std::cout << ENDDECRYPT << std::endl; |
126 |
|
return(0); |
127 |
|
} |
128 |
|
|
129 |
|
int decrypt(std::string path, std::string passphrase) |
130 |
|
{ |
131 |
|
/* TESTER si path ok */ |
132 |
|
if (check_path(path) == 0) |
133 |
|
return decrypt_file(path, passphrase); |
134 |
|
return 1; |
135 |
|
} |
136 |
|
|
137 |
|
static const char *passphrase_cb(void *hook, const char *desc, void **r_hd) |
138 |
|
{ |
139 |
|
// const char *pass; |
140 |
|
hook = NULL; |
141 |
|
r_hd = NULL; |
142 |
|
if ( !desc ) { |
143 |
|
return NULL; |
144 |
|
} |
145 |
|
return (GPG_PASSPHRASE); |
146 |
} |
} |