Initialize libpth2.
[pwmd.git] / src / common.h
blob933e76116f335b6846720eea39600785b10da713
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2011 Ben Kibbey <bjk@luxsci.net>
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 2 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
19 #ifndef COMMON_H
20 #define COMMON_H
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <pth.h>
25 #include <gcrypt.h>
26 #include <glib/gi18n.h>
28 #define _ASSUAN_ONLY_GPG_ERRORS 1
29 #include <assuan.h>
31 #include "status.h"
33 enum {
34 STATE_CONNECTED,
35 STATE_OPEN
38 typedef enum {
39 INQUIRE_INIT,
40 INQUIRE_BUSY,
41 INQUIRE_DONE
42 } inquire_status_t;
44 typedef enum {
45 PINENTRY_OPEN,
46 PINENTRY_SAVE
47 } pinentry_cmd_t;
49 #ifdef WITH_PINENTRY
50 typedef struct {
51 size_t len;
52 void *buf;
53 } membuf_t;
55 typedef enum {
56 PINENTRY_NONE,
57 PINENTRY_INIT,
58 PINENTRY_PID,
59 PINENTRY_RUNNING,
60 PINENTRY_TIMEOUT
61 } pinentry_status_t;
63 typedef struct {
64 gint fd;
65 gpg_error_t error;
66 pinentry_status_t status;
67 union {
68 gchar key[ASSUAN_LINELENGTH];
69 pid_t pid;
70 } what;
71 } pinentry_key_s;
73 typedef gpg_error_t (*pinentry_finalize_cb)(assuan_context_t, guchar *, gboolean);
75 struct pinentry_s {
76 pth_t tid;
77 pth_mutex_t status_mutex;
78 pth_mutex_t cond_mutex;
79 pth_cond_t cond;
80 pinentry_cmd_t which;
81 gchar *filename;
82 assuan_context_t ctx;
83 pinentry_finalize_cb cb;
84 pid_t pid;
85 pid_t pin_pid;
86 gint fd;
87 pinentry_status_t status;
88 gchar *name;
89 gchar *title;
90 gchar *desc;
91 gchar *prompt;
92 gchar *ttyname;
93 gchar *ttytype;
94 gchar *display;
95 gchar *path;
96 gchar *lcctype;
97 gchar *lcmessages;
98 gint timeout;
99 gboolean has_lock;
100 gint enable;
101 membuf_t data;
102 assuan_error_t (*inquire_cb)(void *data, const char *line);
103 void *inquire_data;
105 #endif
107 typedef struct {
108 guint8 magic[5];
109 guint16 version;
110 guint64 iter;
111 guint64 flags;
112 guint8 iv[16];
113 } file_header_t;
115 typedef struct {
116 gint iter;
117 guint8 iv[16];
118 } file_header_v1_t;
120 typedef struct {
121 gboolean v1;
122 gint fd;
123 struct stat st;
124 gpointer doc;
125 goffset len;
127 union {
128 file_header_v1_t fh1;
129 file_header_t fh2;
130 } ver;
131 } file_header_internal_t;
133 struct client_thread_s {
134 pth_t tid;
135 pth_msgport_t mp;
136 pth_t msg_tid;
137 pth_mutex_t mp_mutex;
138 gint fd;
139 struct client_s *cl;
142 struct assuan_cmd_s {
143 assuan_context_t ctx;
144 const gchar *line;
145 gint line_len;
146 const gchar *line2;
149 /* For use with .flags in the data file header. */
150 #define PWMD_CIPHER_OFFSET (1)
151 #define PWMD_CIPHER(n) (PWMD_CIPHER_OFFSET << n)
152 #define PWMD_CIPHER_AES128 PWMD_CIPHER(0)
153 #define PWMD_CIPHER_AES192 PWMD_CIPHER(1)
154 #define PWMD_CIPHER_AES256 PWMD_CIPHER(2)
155 #define PWMD_CIPHER_SERPENT128 PWMD_CIPHER(3)
156 #define PWMD_CIPHER_SERPENT192 PWMD_CIPHER(4)
157 #define PWMD_CIPHER_SERPENT256 PWMD_CIPHER(5)
158 #define PWMD_CIPHER_CAMELLIA128 PWMD_CIPHER(6)
159 #define PWMD_CIPHER_CAMELLIA192 PWMD_CIPHER(7)
160 #define PWMD_CIPHER_CAMELLIA256 PWMD_CIPHER(8)
161 #define PWMD_CIPHER_3DES PWMD_CIPHER(9)
162 #define PWMD_CIPHER_CAST5 PWMD_CIPHER(10)
163 #define PWMD_CIPHER_BLOWFISH PWMD_CIPHER(11)
164 #define PWMD_CIPHER_TWOFISH PWMD_CIPHER(12)
165 #define PWMD_CIPHER_TWOFISH128 PWMD_CIPHER(13)
167 #define PWMD_FLAG_OFFSET (1<<15)
168 #define PWMD_FLAG(n) (PWMD_FLAG_OFFSET << n)
170 struct crypto_s {
171 gpointer iv;
172 gpointer key;
173 gpointer tkey;
174 size_t tkey_len;
175 gpointer tkey2;
176 size_t tkey2_len;
177 gpointer inbuf;
178 goffset insize;
179 gpointer outbuf;
180 goffset outsize;
181 file_header_internal_t *fh;
182 gcry_cipher_hd_t gh;
183 gsize blocksize;
184 gsize keysize;
187 /* These are flags that are set by a client option via the SET command. */
188 #define OPT_ITERATIONS 0x1
189 #define OPT_PINENTRY 0x2
190 #define OPT_PINENTRY_TO 0x4
191 #define OPT_CIPHER 0x8
192 #define OPT_LOCK 0x10
193 #define OPT_INQUIRE 0x20
194 #define OPT_BASE64 0x40
196 struct client_s {
197 assuan_context_t ctx;
198 #ifdef WITH_PINENTRY
199 struct pinentry_s *pinentry;
200 #endif
201 gpointer doc; /* xmlDocPtr */
202 gpointer xml_error;
203 gpointer xml;
204 gint len;
205 gint state;
206 gchar *filename;
207 guchar md5file[16];
208 gboolean new;
209 gboolean freed;
210 time_t mtime;
211 gboolean has_lock;
212 gboolean is_lock_cmd;
213 inquire_status_t inquire_status;
214 struct client_thread_s *thd;
215 struct crypto_s *crypto;
216 guint opts;
217 gpg_error_t last_rc;
218 gboolean lock_on_open;
219 gboolean rc_on_locked;
222 GKeyFile *keyfileh;
223 gboolean log_syslog;
224 gint zlib_bufsize;
225 pth_mutex_t rcfile_mutex;
226 pth_mutex_t cn_mutex;
227 GSList *cn_thread_list;
229 void log_write(const gchar *fmt, ...);
230 gpg_error_t send_error(assuan_context_t ctx, gpg_error_t e);
231 gint open_file(const gchar *filename, struct stat *st);
232 gpg_error_t do_xml_encrypt(struct client_s *client, struct crypto_s *,
233 const gchar *filename);
234 gint get_key_file_integer(const gchar *section, const gchar *what);
235 gdouble get_key_file_double(const gchar *section, const gchar *what);
236 gboolean get_key_file_boolean(const gchar *section, const gchar *what);
237 gchar *get_key_file_string(const gchar *section, const gchar *what);
238 gchar *expand_homedir(gchar *str);
239 void free_client(struct client_s *client);
240 void cleanup_crypto(struct crypto_s **);
241 struct crypto_s *init_client_crypto();
242 gpg_error_t init_client_crypto2(const char *filename,
243 struct crypto_s *crypto);
244 gpg_error_t do_assuan_command(assuan_context_t ctx,
245 void *(*cb)(void *data), void *data);
246 void close_file_header(file_header_internal_t *fh);
247 void cleanup_ev_cb(void *arg);
248 void cleanup_mutex_cb(void *arg);
249 void cleanup_fd_cb(void *arg);
250 void cleanup_unlink_cb(void *arg);
251 void cleanup_attr_cb(void *arg);
252 void cleanup_cancel_cb(void *arg);
253 guint pwmd_cipher_str_to_cipher(const gchar *str);
254 const gchar *pwmd_cipher_to_str(guint64 flags);
255 file_header_internal_t *read_file_header(const gchar *filename, gboolean v1,
256 gpg_error_t *rc);
258 #endif