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