File: pgp.c

package info (click to toggle)
elmo 1.3.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,244 kB
  • ctags: 3,673
  • sloc: ansic: 24,299; sh: 4,169; lex: 3,626; cpp: 2,739; perl: 1,047; pascal: 811; makefile: 359; yacc: 318
file content (430 lines) | stat: -rw-r--r-- 11,178 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* 
   elmo - ELectronic Mail Operator

   Copyright (C) 2004 rzyjontko

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

   ----------------------------------------------------------------------

*/
/****************************************************************************
 *    IMPLEMENTATION HEADERS
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif

#ifdef GPG_SUPPORT
# ifdef HAVE_GPGME4 
#  include <gpgme4/gpgme.h>
# else
#  include <gpgme.h>
# endif
#endif

#include <string.h>
#include <unistd.h>

#include "pgp.h"
#include "read.h"
#include "gettext.h"
#include "ask.h"
#include "mime.h"
#include "xmalloc.h"
#include "file.h"
#include "mlex.h"
#include "rmime.h"
#include "hash.h"
#include "error.h"
#include "gettext.h"

/****************************************************************************
 *    IMPLEMENTATION PRIVATE DEFINITIONS / ENUMERATIONS / SIMPLE TYPEDEFS
 ****************************************************************************/

#define PREAMBLE do { if (! initialized) return; } while (0)

/****************************************************************************
 *    IMPLEMENTATION PRIVATE CLASS PROTOTYPES / EXTERNAL CLASS REFERENCES
 ****************************************************************************/
/****************************************************************************
 *    IMPLEMENTATION PRIVATE STRUCTURES / UTILITY CLASSES
 ****************************************************************************/

struct pass_list {
        struct pass_list *next;
        char             *hint;
        char             *pass;
};

/****************************************************************************
 *    IMPLEMENTATION REQUIRED EXTERNAL REFERENCES (AVOID)
 ****************************************************************************/
/****************************************************************************
 *    IMPLEMENTATION PRIVATE DATA
 ****************************************************************************/

#ifdef GPG_SUPPORT
/* GPGME context */
static gpgme_ctx_t ctx;
#endif

#ifdef GPG_SUPPORT
/* How should we hide the password. */
static hide_mode_t hide = HIDE_ASTERISK;
#endif

static int initialized = 0;

/* List of passwords read by user. */
static struct pass_list *passwords = NULL;

/****************************************************************************
 *    INTERFACE DATA
 ****************************************************************************/
/****************************************************************************
 *    IMPLEMENTATION PRIVATE FUNCTION PROTOTYPES
 ****************************************************************************/
/****************************************************************************
 *    IMPLEMENTATION PRIVATE FUNCTIONS
 ****************************************************************************/

static void
destroy_passwords (struct pass_list *list)
{
        if (list == NULL)
                return;

        destroy_passwords (list->next);

        xfree (list->hint);
        xfree (list->pass);
        xfree (list);
}


#ifdef GPG_SUPPORT

static void
add_password (const char *hint, const char *pass)
{
        struct pass_list *list = xmalloc (sizeof (struct pass_list));

        list->hint = xstrdup (hint);
        list->pass = xstrdup (pass);

        list->next = passwords;
        passwords  = list;
}


static char *
find_password (struct pass_list *list, const char *hint)
{
        if (list == NULL)
                return NULL;

        if (strcmp (list->hint, hint) == 0)
                return list->pass;

        return find_password (list->next, hint);
}


static gpgme_error_t
passphrase_cb (void *hook, const char *hint, const char *info,
               int prev_bad, int fd)
{
        char *answer = NULL;

        if (! prev_bad){
                answer = find_password (passwords, hint);
        }

        if (answer == NULL){
                answer = read_argument (_("Password: "), NULL, COMPLETE_NONE,
                                        hide);
        }
               
        if (answer == NULL){
                write (fd, "\n", 1);
                return GPG_ERR_CANCELED;
        }

        write (fd, answer, strlen (answer));
        write (fd, "\n", 1);

        add_password (hint, answer);

        return GPG_ERR_NO_ERROR;
}


static gpgme_data_t
data_from_mime (const char *fname, mime_t *mime)
{
        gpgme_data_t result;

        gpgme_data_new_from_filepart (& result, fname, NULL, mime->off_start,
                                      mime->off_end - mime->off_start);
        return result;
}


static gpgme_data_t
data_from_mime_with_header (const char *fname, mime_t *mime)
{
        gpgme_data_t result;

        gpgme_data_new_from_filepart (& result, fname, NULL, mime->off_header,
                                      mime->off_end - mime->off_header);
        return result;
}



static gpgme_data_t
data_from_tmp_file (FILE **fp, char **fname)
{
        gpgme_data_t result;
        
        *fp = file_temp_file (fname);
        gpgme_data_new_from_stream (& result, *fp);

        return result;
}


static mime_t *
parse_file (FILE *fp)
{
        mime_t *mime;
        
        rewind (fp);
        yyin = fp;

        if (mlex_scan_file (0) != NEXT_MAIL)
                return NULL;

        mime                = newmail->mime->mime;
        newmail->mime->mime = NULL;

        mail_destroy (newmail, BOX_INVALID);
        return mime;
}


static void
decrypt (mime_info_t *mime_info)
{
        gpgme_data_t  in, out;
        FILE         *fp;
        char         *fname;

        in  = data_from_mime (mime_info->file,
                              mime_info->mime->parts->array[1]);
        out = data_from_tmp_file (& fp, & fname);

        if (gpgme_op_decrypt (ctx, in, out) == GPG_ERR_NO_ERROR){
                mime_info->decrypted = parse_file (fp);
                mime_info->d_file    = fname;
        }
        else {
                xfree (fname);
                destroy_passwords (passwords);
        }

        fclose (fp);
        
        gpgme_data_release (in);
        gpgme_data_release (out);
}



static char *
sig_from_fpr (const char *fpr)
{
        gpgme_key_t   key;
        str_t        *str;
        char         *name;
        
        if (gpgme_get_key (ctx, fpr, & key, 0) != GPG_ERR_NO_ERROR)
                return NULL;

        name = mime_decode_utf8 (key->uids->name);
        str  = str_create ();
        str_sprintf (str, "%s <%s>", name, key->uids->email);
        xfree (name);

        gpgme_key_unref (key);
        return str_finished (str);
}


static void
get_sig_info (mime_info_t *mime)
{
        gpgme_verify_result_t result  = gpgme_op_verify_result (ctx);
        gpgme_error_t         error   = result->signatures->status;
        gpgme_sigsum_t        summary = result->signatures->summary;
        
        mime->sig_by = sig_from_fpr (result->signatures->fpr);
        
        if (summary & GPGME_SIGSUM_VALID){
                mime->sig = SIG_OK;
        }
        else if (summary & GPGME_SIGSUM_GREEN){
                mime->sig      = SIG_WARN;
                mime->sig_text = xstrdup (gpgme_strerror (error));
        }
        else if (summary == 0){
                mime->sig      = SIG_WARN;
                mime->sig_text = xstrdup (gpgme_strerror (error));
        }
        else {
                mime->sig      = SIG_FAIL;
                mime->sig_text = xstrdup (gpgme_strerror (error));
        }
}



static void
verify (mime_info_t *mime_info)
{
        gpgme_data_t  mail, sig;
        gpgme_error_t err;

        if (mime_info->sig != SIG_NOT_SIGNED)
                return;
        
        sig  = data_from_mime (mime_info->file,
                               mime_info->mime->parts->array[1]);
        mail = data_from_mime_with_header (mime_info->file,
                                           mime_info->mime->parts->array[0]);

        err = gpgme_op_verify (ctx, sig, mail, NULL);
        
        if (err == GPG_ERR_NO_ERROR){
                get_sig_info (mime_info);
        }
        else {
                error_ (0, _("couldn't verify signature: %s"),
                        gpgme_strerror (err));
        }
        
        gpgme_data_release (sig);
        gpgme_data_release (mail);
}

#else

static void
decrypt (mime_info_t *mime)
{
}


static void
verify (mime_info_t *mime)
{
}

#endif

/****************************************************************************
 *    INTERFACE FUNCTIONS
 ****************************************************************************/

void
pgp_init (void)
{
#if defined (GPG_SUPPORT) && defined (HAVE_LOCALE_H)
        char *shide;
        
        gpgme_check_version (NULL);
        gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
        gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));

        shide = ask_for_default ("hide_password", NULL);
        
        if (shide && strcasecmp (shide, "yes") == 0)
                hide = HIDE_EMPTY;
        
        if (gpgme_new (& ctx) == GPG_ERR_NO_ERROR){
                gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
                gpgme_set_armor (ctx, 1);
                
                gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);

                initialized = 1;
        }
#endif
}


void
pgp_free_resources (void)
{
        PREAMBLE;

#ifdef GPG_SUPPORT
        gpgme_release (ctx);
        destroy_passwords (passwords);
#endif
        initialized = 0;
        passwords   = NULL;
}



void
pgp_decrypt_verify (mime_info_t *mime_info)
{
        PREAMBLE;

        if (strcasecmp (mime_info->mime->type, "multipart/encrypted") == 0){
                decrypt (mime_info);
        }
        else if (strcasecmp (mime_info->mime->type, "multipart/signed") == 0){
                verify (mime_info);
        }
}



void
pgp_forget_passphrase (void)
{
        destroy_passwords (passwords);
        passwords = NULL;

        error_ (0, "%s", _("passwords have been forgotten"));
}

/****************************************************************************
 *    INTERFACE CLASS BODIES
 ****************************************************************************/
/****************************************************************************
 *
 *    END MODULE pgp.c
 *
 ****************************************************************************/