]> git.notmuchmail.org Git - notmuch/blob - crypto.c
cli/insert: clean up sync_dir
[notmuch] / crypto.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2012 Jameson Rollins
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Authors: Jameson Rollins <jrollins@finestructure.net>
19  */
20
21 #include "notmuch-client.h"
22
23 #ifdef GMIME_ATLEAST_26
24
25 /* Create a GPG context (GMime 2.6) */
26 static notmuch_crypto_context_t *
27 create_gpg_context (void)
28 {
29     notmuch_crypto_context_t *gpgctx;
30
31     /* TODO: GMimePasswordRequestFunc */
32     gpgctx = g_mime_gpg_context_new (NULL, "gpg");
33     if (! gpgctx)
34         return NULL;
35
36     g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) gpgctx, TRUE);
37     g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
38
39     return gpgctx;
40 }
41
42 #else /* GMIME_ATLEAST_26 */
43
44 /* Create a GPG context (GMime 2.4) */
45 static notmuch_crypto_context_t *
46 create_gpg_context (void)
47 {
48     GMimeSession *session;
49     notmuch_crypto_context_t *gpgctx;
50
51     session = g_object_new (g_mime_session_get_type (), NULL);
52     gpgctx = g_mime_gpg_context_new (session, "gpg");
53     g_object_unref (session);
54
55     if (! gpgctx)
56         return NULL;
57
58     g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) gpgctx, FALSE);
59
60     return gpgctx;
61 }
62
63 #endif /* GMIME_ATLEAST_26 */
64
65 /* for the specified protocol return the context pointer (initializing
66  * if needed) */
67 notmuch_crypto_context_t *
68 notmuch_crypto_get_context (notmuch_crypto_t *crypto, const char *protocol)
69 {
70     notmuch_crypto_context_t *cryptoctx = NULL;
71
72     if (! protocol) {
73         fprintf (stderr, "Cryptographic protocol is empty.\n");
74         return cryptoctx;
75     }
76
77     /* As per RFC 1847 section 2.1: "the [protocol] value token is
78      * comprised of the type and sub-type tokens of the Content-Type".
79      * As per RFC 1521 section 2: "Content-Type values, subtypes, and
80      * parameter names as defined in this document are
81      * case-insensitive."  Thus, we use strcasecmp for the protocol.
82      */
83     if (strcasecmp (protocol, "application/pgp-signature") == 0 ||
84         strcasecmp (protocol, "application/pgp-encrypted") == 0) {
85         if (! crypto->gpgctx) {
86             crypto->gpgctx = create_gpg_context ();
87             if (! crypto->gpgctx)
88                 fprintf (stderr, "Failed to construct gpg context.\n");
89         }
90         cryptoctx = crypto->gpgctx;
91     } else {
92         fprintf (stderr, "Unknown or unsupported cryptographic protocol.\n");
93     }
94
95     return cryptoctx;
96 }
97
98 int
99 notmuch_crypto_cleanup (notmuch_crypto_t *crypto)
100 {
101     if (crypto->gpgctx) {
102         g_object_unref (crypto->gpgctx);
103         crypto->gpgctx = NULL;
104     }
105
106     return 0;
107 }