1 | #include <stdio.h>
2 | #include <stdlib.h>
3 | #include <string.h>
4 | #include <sys/wait.h>
5 | #include <unistd.h>
6 | #include <errno.h>
7 |
8 | #include "gpg.h"
9 |
10 | extern int spawn_job (char *path, char *argv[],
11 | int *in_fd, int *out_fd, int *err_fd);
12 |
13 | struct VerifySignObject verifySignObj;
14 | struct ReadCryptedObject readCryptedObj;
15 | struct ImportKeyObject importKeyObj;
16 |
17 | /* ------------------------------------------------- */
18 | void PA_VerifySignature(struct VerifySignObject *vSO) {
19 |
20 | char *strArgs[10];
21 | char Args0[100];
22 | char Args1[100], Args2[100], Args3[100], Args4[100], Args5[100];
23 | int gpg_pid;
24 | int gpg_in_fd, out_fd, err_fd;
25 | int status;
26 | char txt[LINE_LENGTH];
27 | char *keyStr;
28 |
29 | /* Copy the incoming object on the internal global object */
30 | memmove( &verifySignObj, vSO, sizeof(struct VerifySignObject) );
31 |
32 | strcpy(Args0, "--no-secmem-warning");
33 | strcpy(Args1, "--keyring");
34 | strcpy(Args2, verifySignObj.keyRing);
35 | strcpy(Args3, "--verify");
36 | if (!strcmp(verifySignObj.iSigFilename, "")) {
37 | strcpy(Args4, verifySignObj.iDocSigFilename);
38 | } else {
39 | strcpy(Args4, verifySignObj.iSigFilename);
40 | strcpy(Args5, verifySignObj.iDocSigFilename);
41 | }
42 |
43 | strArgs[0] = Args0;
44 | strArgs[1] = Args1;
45 | strArgs[2] = Args2;
46 | strArgs[3] = Args3;
47 | strArgs[4] = Args4;
48 | strArgs[5] = Args5;
49 | strArgs[6] = (char *)0;
50 |
51 |
52 | gpg_in_fd = INPUT_FD;
53 | out_fd = OUTPUT_FD;
54 | err_fd = ERROR_FD;
55 | if ( ( gpg_pid = spawn_job ("gpg", strArgs,
56 | &gpg_in_fd, &out_fd, &err_fd) ) < 0 )
57 | {
58 | printf ("could not spawn gpg");
59 | }
60 |
61 | if (waitpid (gpg_pid, &status, 0) < 0)
62 | {
63 | fprintf (stderr, "Error reaping child\t%s\n", ERRSTRING);
64 | printf ("could not reap gpg process");
65 | /* exit (1); */
66 | }
67 | if (status != 0)
68 | {
69 | fprintf (stderr, "Bad child status: %d\t%s\n", status, ERRSTRING);
70 | printf ("gpg failure");
71 | /* exit (1); */
72 | }
73 |
74 |
75 | /* Parsing gpg output */
76 | vSO->isValid = 0;
77 | while (fgets (txt, STRING_LENGTH - 1, stdin) != NULL)
78 | {
79 | /* printf ( "GPG output : %s\n", txt ); */
80 | if (strstr(txt, "Good signature") != NULL)
81 | vSO->isValid = 1;
82 | if ((keyStr = strstr(txt, "key ID")) != NULL) {
83 | keyStr += 7;
84 | sscanf(keyStr, "%8X\n", &vSO->keyID);
85 | }
86 | }
87 | close (INPUT_FD);
88 | }
89 |
90 | /* ------------------------------------------------- */
91 | void PA_Decrypt(struct ReadCryptedObject *rDO) {
92 |
93 | char *strArgs[9];
94 | char clearTextExtension[4] = ".gpg";
95 | char Args0[100] = "abracadabra";
96 | char Args1[100];
97 | char Args2[100];
98 | char Args3[100];
99 | char Args4[100];
100 | char Args5[100];
101 | char Args6[100];
102 | int gpg_pid;
103 | int gpg_in_fd, out_fd, err_fd;
104 | int status;
105 | char txt[LINE_LENGTH];
106 |
107 |
108 | /* Copy the incoming object on the internal global object */
109 | memmove( &readCryptedObj, rDO, sizeof(struct ReadCryptedObject) );
110 |
111 | strcpy(Args0, "--no-tty");
112 | strcpy(Args1, "--no-secmem-warning");
113 | strcpy(Args2, "--keyring");
114 | strcpy(Args3, readCryptedObj.keyRing);
115 | strcpy(Args4, "--output");
116 | strcpy(Args5, strcat(readCryptedObj.iFilename, clearTextExtension));
117 | strcpy(Args6, readCryptedObj.iFilename);
118 |
119 | strArgs[0] = Args0;
120 | strArgs[1] = Args1;
121 | strArgs[2] = Args2;
122 | strArgs[3] = Args3;
123 | strArgs[4] = Args4;
124 | strArgs[5] = Args5;
125 | strArgs[6] = Args6;
126 | strArgs[7] = (char *) 0;
127 |
128 | gpg_in_fd = INPUT_FD;
129 | out_fd = OUTPUT_FD;
130 | err_fd = ERROR_FD;
131 | if ( ( gpg_pid = spawn_job ("gpg", strArgs,
132 | &gpg_in_fd, &out_fd, &err_fd) ) < 0 )
133 | {
134 | printf ("could not spawn gpg");
135 | }
136 |
137 | if (waitpid (gpg_pid, &status, 0) < 0)
138 | {
139 | fprintf (stderr, "Error reaping child\t%s\n", ERRSTRING);
140 | printf ("could not reap gpg process");
141 | /* exit (1); */
142 | }
143 | if (status != 0)
144 | {
145 | fprintf (stderr, "Bad child status: %d\t%s\n", status, ERRSTRING);
146 | printf ("gpg failure");
147 | /* exit (1); */
148 | }
149 |
150 |
151 | /* Parsing gpg output */
152 | while (fgets (txt, STRING_LENGTH - 1, stdin) != NULL)
153 | {
154 | /* printf ( "GPG output : %s\n", txt ); */
155 |
156 | /* if ((keyStr = strstr(txt, "key")) != NULL) {
157 | keyStr += 4;
158 | sscanf(keyStr, "%8X\n", &iKO->keyID);
159 | } */
160 | }
161 | close (INPUT_FD);
162 | }
163 |
164 |
165 | /* ------------------------------------------------- */
166 | void PA_ImportKey(struct ImportKeyObject *iKO) {
167 |
168 | char *strArgs[9];
169 | char Args0[100] = "abracadabra";
170 | char Args1[100], Args2[100], Args3[100], Args4[100], Args5[100];
171 | int gpg_pid;
172 | int gpg_in_fd, out_fd, err_fd;
173 | int status;
174 | char txt[LINE_LENGTH];
175 | char *keyStr;
176 |
177 | /* Copy the incoming object on the internal global object */
178 | memmove( &importKeyObj, iKO, sizeof(struct ImportKeyObject) );
179 |
180 | importKeyObj.rc = iKO_GENERALFAILURE;
181 |
182 | strcpy(Args0, "--no-tty");
183 | strcpy(Args1, "--no-secmem-warning");
184 | strcpy(Args2, "--keyring");
185 | strcpy(Args3, importKeyObj.keyRing);
186 | strcpy(Args4, "--import");
187 | strcpy(Args5, importKeyObj.iFilename);
188 |
189 | strArgs[0] = Args0;
190 | strArgs[1] = Args1;
191 | strArgs[2] = Args2;
192 | strArgs[3] = Args3;
193 | strArgs[4] = Args4;
194 | strArgs[5] = Args5;
195 | strArgs[6] = (char *)0;
196 |
197 | gpg_in_fd = INPUT_FD;
198 | out_fd = OUTPUT_FD;
199 | err_fd = ERROR_FD;
200 | if ( ( gpg_pid = spawn_job ("/usr/local/bin/gpg", strArgs,
201 | &gpg_in_fd, &out_fd, &err_fd) ) < 0 )
202 | {
203 | printf ("could not spawn gpg");
204 | }
205 |
206 | if (waitpid (gpg_pid, &status, 0) < 0)
207 | {
208 | fprintf (stderr, "Error reaping child\t%s\n", ERRSTRING);
209 | printf ("could not reap gpg process");
210 | /* exit (1); */
211 | }
212 |
213 |
214 | if (status != 0)
215 | {
216 | fprintf (stderr, "Bad child status: %d\t%s\n", status, ERRSTRING);
217 | printf ("gpg failure");
218 | /* exit (1); */
219 | }
220 |
221 |
222 | /* Parsing gpg output */
223 | iKO->rc = iKO_GENERALFAILURE;
224 | while (fgets (txt, STRING_LENGTH - 1, stdin) != NULL)
225 | {
226 | /* printf ( "GPG output : %s\n", txt ); */
227 |
228 | if ((keyStr = strstr(txt, "imported")) != NULL) {
229 | iKO->rc = iKO_OK;
230 | }
231 | if ((keyStr = strstr(txt, "unchanged")) != NULL) {
232 | iKO->rc = iKO_UNCHANGED;
233 | }
234 |
235 | if ((keyStr = strstr(txt, "key")) != NULL) {
236 | keyStr += 4;
237 | sscanf(keyStr, "%8X\n", &iKO->keyID);
238 | }
239 | }
240 | close (INPUT_FD);
241 | }
242 |
243 |