Ported to libassuan 2.0. This version supports building a DSO so no
[pwmd.git] / src / commands.c
blob0f709d74151ca47bf2fbe275f618aac30f081bcd
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2009 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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <glib.h>
29 #include <gcrypt.h>
30 #include <zlib.h>
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
36 #ifdef WITH_LIBACL
37 #include <sys/acl.h>
38 #endif
40 #include "mem.h"
41 #include "xml.h"
42 #include "common.h"
44 #ifdef WITH_PINENTRY
45 #include "pinentry.h"
46 #endif
48 #include "pwmd_error.h"
49 #include "cache.h"
50 #include "misc.h"
51 #include "commands.h"
52 #include "lock.h"
54 struct gz_s {
55 z_stream z;
56 gpointer out;
57 gboolean done;
58 status_msg_t which;
61 static void *z_alloc(void *data, unsigned items, unsigned size)
63 return gcry_calloc(items, size);
66 static void z_free(void *data, void *p)
68 gcry_free(p);
71 static gpg_error_t file_modified(struct client_s *client)
73 struct stat st;
74 gpg_error_t rc;
76 if (client->state != STATE_OPEN)
77 return EPWMD_NO_FILE;
79 rc = lock_file_mutex(client);
81 if (rc)
82 return rc;
84 if (lstat(client->filename, &st) == 0 && client->mtime) {
85 if (client->mtime != st.st_mtime)
86 return EPWMD_FILE_MODIFIED;
89 pth_cancel_point();
90 return 0;
93 static gpg_error_t parse_xml(assuan_context_t ctx)
95 struct client_s *client = assuan_get_pointer(ctx);
97 client->doc = xmlReadMemory(client->xml, client->len, NULL, "UTF-8", XML_PARSE_NOBLANKS);
99 if (!client->doc)
100 return EPWMD_LIBXML_ERROR;
102 return 0;
105 void unlock_file_mutex(struct client_s *client)
107 pth_mutex_t *m;
109 #ifdef WITH_PINENTRY
110 if (client->has_lock == FALSE || client->pinentry->status != PINENTRY_NONE)
111 #else
112 if (client->has_lock == FALSE)
113 #endif
114 return;
116 CACHE_LOCK(client->ctx);
118 if (cache_get_mutex(client->md5file, &m) == FALSE) {
119 CACHE_UNLOCK;
120 return;
123 CACHE_UNLOCK;
124 MUTEX_UNLOCK(m);
125 client->has_lock = client->is_lock_cmd = FALSE;
128 gpg_error_t lock_file_mutex(struct client_s *client)
130 pth_mutex_t *m;
131 gpg_error_t rc = 0;
133 if (client->has_lock == TRUE)
134 return 0;
136 CACHE_LOCK(client->ctx);
138 if (cache_get_mutex(client->md5file, &m) == FALSE) {
139 CACHE_UNLOCK;
140 return 0;
143 CACHE_UNLOCK;
144 MUTEX_TRYLOCK(client->ctx, m, rc);
146 if (!rc)
147 client->has_lock = TRUE;
149 return rc;
152 void free_client(struct client_s *client)
154 if (client->doc)
155 xmlFreeDoc(client->doc);
157 if (client->xml)
158 gcry_free(client->xml);
160 if (client->filename)
161 g_free(client->filename);
163 if (client->crypto)
164 cleanup_crypto(&client->crypto);
166 if (client->xml_error)
167 xmlResetError(client->xml_error);
170 void cleanup_client(struct client_s *client)
172 assuan_context_t ctx = client->ctx;
173 struct client_thread_s *thd = client->thd;
174 gboolean has_lock = client->has_lock;
175 #ifdef WITH_PINENTRY
176 struct pinentry_s *pin = client->pinentry;
177 #endif
179 unlock_file_mutex(client);
180 CACHE_LOCK(client->ctx);
181 cache_decr_refcount(client->md5file);
184 * This may be a new file so don't use a cache slot. save_command() will
185 * set this to FALSE on success.
187 if (client->new == TRUE)
188 cache_clear(client->md5file, 1);
190 CACHE_UNLOCK;
191 free_client(client);
192 memset(client, 0, sizeof(struct client_s));
193 client->state = STATE_CONNECTED;
194 client->ctx = ctx;
195 client->thd = thd;
196 client->freed = TRUE;
197 #ifdef WITH_PINENTRY
198 client->pinentry = pin;
199 #endif
200 client->has_lock = has_lock;
203 static void gz_cleanup(void *arg)
205 struct gz_s **gz = (struct gz_s **)arg;
207 if (!gz)
208 return;
210 if (!(*gz)->done && (*gz)->out)
211 gcry_free((*gz)->out);
213 if ((*gz)->which == STATUS_COMPRESS) {
214 if ((*gz)->z.zalloc)
215 deflateEnd(&(*gz)->z);
217 else {
218 if ((*gz)->z.zalloc)
219 inflateEnd(&(*gz)->z);
222 g_free(*gz);
223 *gz = NULL;
226 gboolean do_decompress(assuan_context_t ctx, gpointer in, gulong insize,
227 gpointer *out, gulong *outsize, gint *rc)
229 struct gz_s *gz;
230 gz_header h;
231 gchar buf[17];
233 gz = g_malloc0(sizeof(struct gz_s));
235 if (!gz) {
236 *rc = gpg_error_from_errno(ENOMEM);
237 return FALSE;
240 pth_cleanup_push(gz_cleanup, &gz);
241 gz->which = STATUS_DECOMPRESS;
242 gz->z.zalloc = z_alloc;
243 gz->z.zfree = z_free;
244 gz->z.next_in = in;
245 gz->z.avail_in = (uInt)insize;
246 gz->z.avail_out = zlib_bufsize;
247 gz->z.next_out = gz->out = gcry_malloc(zlib_bufsize);
249 if (!gz->out) {
250 log_write("%s(%i): %s", __FUNCTION__, __LINE__, strerror(ENOMEM));
251 *rc = Z_MEM_ERROR;
252 pth_cleanup_pop(1);
253 return FALSE;
256 *rc = inflateInit2(&gz->z, 47);
258 if (*rc != Z_OK) {
259 log_write("%s(%i): %s", __FUNCTION__, __LINE__, gz->z.msg);
260 pth_cleanup_pop(1);
261 return FALSE;
264 memset(&h, 0, sizeof(gz_header));
265 h.comment = (guchar *)buf;
266 h.comm_max = sizeof(buf);
267 *rc = inflateGetHeader(&gz->z, &h);
269 if (*rc != Z_OK) {
270 log_write("%s(%i): %s", __FUNCTION__, __LINE__, gz->z.msg);
271 pth_cleanup_pop(1);
272 return FALSE;
275 *rc = inflate(&gz->z, Z_BLOCK);
277 if (*rc != Z_OK) {
278 log_write("%s(%i): %s", __FUNCTION__, __LINE__, gz->z.msg);
279 pth_cleanup_pop(1);
280 return FALSE;
283 if (h.comment)
284 insize = (gulong)strtol((gchar *)h.comment, NULL, 10);
286 do {
287 gpointer p;
289 *rc = inflate(&gz->z, Z_FINISH);
291 switch (*rc) {
292 case Z_OK:
293 break;
294 case Z_BUF_ERROR:
295 if (!gz->z.avail_out) {
296 p = gcry_realloc(gz->out, gz->z.total_out + zlib_bufsize);
298 if (!p) {
299 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
300 *rc = Z_MEM_ERROR;
301 goto fail;
304 gz->out = p;
305 gz->z.next_out = (guchar *)gz->out + gz->z.total_out;
306 gz->z.avail_out = zlib_bufsize;
307 *rc = send_status(ctx, STATUS_DECOMPRESS, "%li %li",
308 gz->z.total_out, insize);
310 if (*rc)
311 goto fail;
313 break;
314 case Z_STREAM_END:
315 break;
316 default:
317 goto fail;
318 break;
320 } while (*rc != Z_STREAM_END);
322 *rc = send_status(ctx, STATUS_DECOMPRESS, "%li %li", gz->z.total_out,
323 insize);
325 if (*rc)
326 goto fail;
328 *out = gz->out;
329 *outsize = gz->z.total_out;
330 gz->done = TRUE;
331 pth_cleanup_pop(1);
332 *rc = 0;
333 return TRUE;
335 fail:
336 log_write("%s(%i): %s", __FUNCTION__, __LINE__, gz->z.msg);
337 pth_cleanup_pop(1);
338 return FALSE;
341 file_header_internal_t *read_file_header(const gchar *filename, gboolean v1,
342 gpg_error_t *rc)
344 gint fd;
345 gsize len;
346 file_header_internal_t *fh = g_malloc0(sizeof(file_header_internal_t));
347 gsize fh_size;
348 gpointer p;
350 *rc = 0;
352 if (!fh) {
353 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
354 *rc = gpg_error_from_errno(ENOMEM);
355 return NULL;
358 pth_cleanup_push(g_free, fh);
359 fh_size = v1 ? sizeof(fh->ver.fh1) : sizeof(fh->ver.fh2);
361 if (lstat(filename, &fh->st) == -1) {
362 *rc = gpg_error_from_syserror();
363 pth_cleanup_pop(1);
364 return NULL;
367 if (!S_ISREG(fh->st.st_mode)) {
368 *rc = GPG_ERR_ENOANO;
369 pth_cleanup_pop(1);
370 return NULL;
373 fd = open(filename, O_RDONLY);
375 if (fd == -1) {
376 *rc = gpg_error_from_errno(errno);
377 pth_cleanup_pop(1);
378 return NULL;
381 pth_cleanup_push(cleanup_fd_cb, &fd);
382 p = v1 ? (void *)&fh->ver.fh1 : (void *)&fh->ver.fh2;
383 len = pth_read(fd, p, fh_size);
385 if (len != fh_size) {
386 gint n = errno;
387 pth_cleanup_pop(1);
388 pth_cleanup_pop(1);
389 *rc = gpg_error_from_errno(n);
390 return NULL;
393 fh->v1 = v1;
394 fh->fd = fd;
395 pth_cleanup_pop(0);
396 pth_cleanup_pop(0);
397 return fh;
401 * This is called before every Assuan command.
403 gpg_error_t command_startup(assuan_context_t ctx)
405 struct client_s *cl = assuan_get_pointer(ctx);
406 gpg_error_t rc;
407 const gchar *name = assuan_get_command_name(ctx);
409 if (!name)
410 return 0;
412 log_write1("%s", name);
414 if (!g_ascii_strcasecmp(name, "ISCACHED") ||
415 !g_ascii_strcasecmp(name, "CLEARCACHE") ||
416 !g_ascii_strcasecmp(name, "CACHETIMEOUT") ||
417 !g_ascii_strcasecmp(name, "GETCONFIG") ||
418 !g_ascii_strcasecmp(name, "GETPID") ||
419 !g_ascii_strcasecmp(name, "VERSION") ||
420 !g_ascii_strcasecmp(name, "SET") ||
421 !g_ascii_strcasecmp(name, "BYE") ||
422 !g_ascii_strcasecmp(name, "NOP") ||
423 !g_ascii_strcasecmp(name, "CANCEL") ||
424 !g_ascii_strcasecmp(name, "RESET") ||
425 !g_ascii_strcasecmp(name, "END") ||
426 !g_ascii_strcasecmp(name, "HELP") ||
427 !g_ascii_strcasecmp(name, "OPTION") ||
428 !g_ascii_strcasecmp(name, "INPUT") ||
429 !g_ascii_strcasecmp(name, "OUTPUT") ||
430 !g_ascii_strcasecmp(name, "UNSET"))
431 return 0;
433 cl->last_rc = rc = file_modified(cl);
435 if (rc) {
436 if ((rc == EPWMD_NO_FILE || rc == EPWMD_FILE_MODIFIED) &&
437 !g_ascii_strcasecmp(name, "OPEN"))
438 rc = 0;
441 return rc;
444 static gpg_error_t open_command_finalize(assuan_context_t ctx, guchar *key,
445 gboolean cached)
447 struct client_s *client = assuan_get_pointer(ctx);
448 gpg_error_t rc;
449 gint timeout;
451 /* New file. */
452 if (!client->crypto->fh) {
453 if (key[0])
454 goto update_cache;
456 goto done;
459 rc = init_client_crypto2(client->filename, client->crypto);
461 if (rc) {
462 cleanup_client(client);
463 return send_error(ctx, rc);
466 rc = try_xml_decrypt(ctx, key, client->crypto, NULL, NULL);
468 if (rc) {
469 cleanup_client(client);
470 return send_error(ctx, rc);
473 update_cache:
474 CACHE_LOCK(client->ctx);
476 if (cached == FALSE) {
477 if (cache_update_key(client->md5file, key) == FALSE) {
478 cleanup_client(client);
479 CACHE_UNLOCK;
480 return send_syserror(ctx, ENOMEM);
483 timeout = get_key_file_integer(client->filename, "cache_timeout");
484 cache_reset_timeout(client->md5file, timeout);
486 else
487 cache_set_timeout(client->md5file, -2);
489 CACHE_UNLOCK;
491 done:
492 rc = parse_xml(ctx);
494 if (client->xml) {
495 gcry_free(client->xml);
496 client->xml = NULL;
499 if (!rc) {
500 if (client->new == FALSE)
501 send_status_all(STATUS_CACHE);
503 client->state = STATE_OPEN;
506 if (!rc && client->new == FALSE &&
507 client->crypto->fh->ver.fh2.iter != (guint64)get_key_file_double(client->filename, "iterations")) {
508 MUTEX_LOCK(&rcfile_mutex);
509 g_key_file_set_double(keyfileh, client->filename, "iterations",
510 client->crypto->fh->ver.fh2.iter);
511 MUTEX_UNLOCK(&rcfile_mutex);
512 send_status_all(STATUS_CONFIG);
515 cleanup_crypto(&client->crypto);
516 return send_error(ctx, rc);
519 static void req_cleanup(void *arg)
521 if (!arg)
522 return;
524 g_strfreev((gchar **)arg);
527 static gpg_error_t open_command(assuan_context_t ctx, gchar *line)
529 gboolean cached = FALSE;
530 gpg_error_t rc;
531 struct client_s *client = assuan_get_pointer(ctx);
532 gchar **req;
533 gchar *filename = NULL;
534 guint hashlen = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
536 rc = command_startup(ctx);
538 if (rc)
539 return send_error(ctx, rc);
541 if ((req = split_input_line(line, " ", 2)) != NULL)
542 filename = req[0];
544 pth_cleanup_push(req_cleanup, req);
546 if (!filename || !*filename) {
547 pth_cleanup_pop(1);
548 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
551 log_write2("ARGS=\"%s\" %s", filename, req[1] ? "<passphrase>" : "");
553 if (valid_filename(filename) == FALSE) {
554 pth_cleanup_pop(1);
555 return send_error(ctx, EPWMD_INVALID_FILENAME);
558 if (client->state == STATE_OPEN)
559 cleanup_client(client);
561 gcry_md_hash_buffer(GCRY_MD_MD5, client->md5file, filename, strlen(filename));
562 CACHE_LOCK(client->ctx);
564 if (cache_has_file(client->md5file) == FALSE) {
565 if (cache_add_file(client->md5file, NULL) == FALSE) {
566 pth_cleanup_pop(1);
567 CACHE_UNLOCK;
568 return send_syserror(ctx, ENOMEM);
572 cache_incr_refcount(client->md5file);
573 CACHE_UNLOCK;
574 rc = lock_file_mutex(client);
576 if (rc) {
577 pth_cleanup_pop(1);
578 return send_error(ctx, rc);
581 client->freed = FALSE;
582 client->crypto = init_client_crypto();
584 if (!client->crypto) {
585 pth_cleanup_pop(1);
586 cleanup_client(client);
587 return send_syserror(ctx, ENOMEM);
590 client->crypto->key = gcry_malloc(hashlen);
592 if (!client->crypto->key) {
593 pth_cleanup_pop(1);
594 log_write("%s(%i): %s", __FUNCTION__, __LINE__,
595 gpg_error_from_errno(ENOMEM));
596 cleanup_client(client);
597 return send_syserror(ctx, ENOMEM);
600 memset(client->crypto->key, 0, hashlen);
601 client->crypto->fh = read_file_header(filename, FALSE, &rc);
603 if (!client->crypto->fh) {
604 if (gpg_err_code_to_errno(rc) != ENOENT) {
605 log_write("%s: %s", filename, pwmd_strerror(rc));
606 pth_cleanup_pop(1);
607 cleanup_client(client);
608 return send_error(ctx, rc);
612 * New files don't need a key.
614 if ((client->xml = new_document()) == NULL) {
615 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
616 pth_cleanup_pop(1);
617 cleanup_client(client);
618 return send_syserror(ctx, ENOMEM);
621 client->len = xmlStrlen(client->xml);
622 client->new = TRUE;
623 client->filename = g_strdup(filename);
625 if (!client->filename) {
626 pth_cleanup_pop(1);
627 cleanup_client(client);
628 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
629 return send_syserror(ctx, ENOMEM);
632 if (req[1] && *req[1])
633 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, req[1],
634 strlen(req[1]));
636 pth_cleanup_pop(1);
637 #ifdef WITH_PINENTRY
638 client->pinentry->filename = g_strdup(client->filename);
640 if (!client->pinentry->filename) {
641 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
642 cleanup_client(client);
643 return send_syserror(ctx, ENOMEM);
645 #endif
646 return open_command_finalize(ctx, client->crypto->key, cached);
648 else
649 client->mtime = client->crypto->fh->st.st_mtime;
651 client->filename = g_strdup(filename);
653 if (!client->filename) {
654 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
655 pth_cleanup_pop(1);
656 cleanup_client(client);
657 return send_syserror(ctx, ENOMEM);
660 #ifdef WITH_PINENTRY
661 client->pinentry->filename = g_strdup(client->filename);
663 if (!client->pinentry->filename) {
664 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
665 pth_cleanup_pop(1);
666 cleanup_client(client);
667 return send_syserror(ctx, ENOMEM);
669 #endif
671 if (client->crypto->fh->ver.fh2.iter <= 0ULL)
672 goto done;
674 CACHE_LOCK(client->ctx);
675 cached = cache_get_key(client->md5file, client->crypto->key);
676 CACHE_UNLOCK;
678 if (cached == FALSE) {
679 gchar *tmp = get_key_file_string(filename, "key_file");
681 if (tmp) {
682 g_free(tmp);
683 pth_cleanup_pop(1);
684 cleanup_client(client);
685 return send_error(ctx, GPG_ERR_WRONG_KEY_USAGE);
689 * No key specified and no matching filename found in the cache. Use
690 * pinentry to retrieve the key. Cannot return assuan_process_done()
691 * here otherwise the command will be interrupted. The event loop in
692 * client_thread() will poll the file descriptor waiting for it to
693 * become ready to read a pinentry_key_s which will contain the
694 * entered key or an error code. It will then call
695 * open_command_finalize() to to finish the command.
697 if (!req[1] || !*req[1]) {
698 #ifdef WITH_PINENTRY
699 gboolean b = get_key_file_boolean(filename, "enable_pinentry");
701 /* From set_pinentry_defaults(). */
702 if (client->pinentry->enable == FALSE ||
703 (client->pinentry->enable == -1 && b == FALSE)) {
704 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, "", 1);
705 goto done;
708 pth_cleanup_pop(1);
709 rc = lock_pin_mutex(client);
711 if (rc) {
712 unlock_pin_mutex(client->pinentry);
713 cleanup_client(client);
714 return send_error(ctx, rc);
717 client->pinentry->which = PINENTRY_OPEN;
718 rc = pinentry_fork(ctx);
720 if (rc) {
721 unlock_pin_mutex(client->pinentry);
722 cleanup_client(client);
723 return send_error(ctx, rc);
726 // Called from pinentry iterate.
727 client->pinentry->cb = open_command_finalize;
728 client->pinentry->status = PINENTRY_INIT;
729 return 0;
730 #else
731 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, "", 1);
732 goto done;
733 #endif
736 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, req[1],
737 strlen(req[1]));
739 else if (req && req[1] && *req[1]) {
740 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, req[1],
741 strlen(req[1]) ? strlen(req[1]) : 1);
744 done:
745 pth_cleanup_pop(1);
746 return open_command_finalize(ctx, client->crypto->key, cached);
749 gboolean do_compress(assuan_context_t ctx, gint level, gpointer data,
750 guint size, gpointer *out, gulong *outsize, gint *rc)
752 struct gz_s *gz;
753 gz_header h;
754 gchar buf[17];
755 gint cmd = Z_NO_FLUSH;
757 gz = g_malloc0(sizeof(struct gz_s));
759 if (!gz) {
760 *rc = gpg_error_from_errno(ENOMEM);
761 return FALSE;
764 pth_cleanup_push(gz_cleanup, &gz);
765 gz->which = STATUS_COMPRESS;
766 gz->z.zalloc = z_alloc;
767 gz->z.zfree = z_free;
768 gz->z.next_in = data;
769 gz->z.avail_in = size < zlib_bufsize ? (uInt)size : (uInt)zlib_bufsize;
770 gz->z.avail_out = (uInt)zlib_bufsize;
771 gz->z.next_out = gz->out = gcry_malloc(zlib_bufsize);
773 if (!gz->out) {
774 log_write("%s(%i): %s", __FUNCTION__, __LINE__, strerror(ENOMEM));
775 *rc = Z_MEM_ERROR;
776 pth_cleanup_pop(1);
777 return FALSE;
780 *rc = deflateInit2(&gz->z, level, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
782 if (*rc != Z_OK) {
783 log_write("%s(%i): %s", __FUNCTION__, __LINE__, gz->z.msg);
784 pth_cleanup_pop(1);
785 return FALSE;
788 /* Rather than store the size of the uncompressed data in the file header,
789 * store it in the comment field of the gzip header. Don't give anyone too
790 * much information. Not sure why really, but it seems the right way. :)
792 memset(&h, 0, sizeof(gz_header));
793 g_snprintf(buf, sizeof(buf), "%u", size);
794 h.comment = (guchar *)buf;
795 *rc = deflateSetHeader(&gz->z, &h);
797 if (*rc != Z_OK) {
798 log_write("%s(%i): %s", __FUNCTION__, __LINE__, gz->z.msg);
799 pth_cleanup_pop(1);
800 return FALSE;
803 do {
804 gpointer p;
806 *rc = deflate(&gz->z, cmd);
808 switch (*rc) {
809 case Z_OK:
810 break;
811 case Z_BUF_ERROR:
812 if (!gz->z.avail_out) {
813 p = gcry_realloc(gz->out, gz->z.total_out + zlib_bufsize);
815 if (!p) {
816 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
817 *rc = Z_MEM_ERROR;
818 goto fail;
821 gz->out = p;
822 gz->z.next_out = (guchar *)gz->out + gz->z.total_out;
823 gz->z.avail_out = zlib_bufsize;
826 if (!gz->z.avail_in && gz->z.total_in < size) {
827 if (gz->z.total_in + zlib_bufsize > size)
828 gz->z.avail_in = size - gz->z.total_in;
829 else
830 gz->z.avail_in = zlib_bufsize;
832 *rc = send_status(ctx, STATUS_COMPRESS, "%li %u",
833 gz->z.total_in, size);
835 if (*rc)
836 goto fail;
839 if (gz->z.total_in >= size)
840 cmd = Z_FINISH;
842 break;
843 case Z_STREAM_END:
844 break;
845 default:
846 goto fail;
848 } while (*rc != Z_STREAM_END);
850 *rc = send_status(ctx, STATUS_COMPRESS, "%li %u", gz->z.total_in, size);
852 if (*rc)
853 goto fail;
855 *out = gz->out;
856 *outsize = gz->z.total_out;
857 *rc = 0;
858 gz->done = TRUE;
859 pth_cleanup_pop(1);
860 return TRUE;
862 fail:
863 log_write("%s(%i): %s", __FUNCTION__, __LINE__, gz->z.msg);
864 pth_cleanup_pop(1);
865 return FALSE;
868 #define CRYPTO_BLOCKSIZE(c) (c->blocksize * 1024)
870 static gpg_error_t iterate_crypto_once(struct client_s *client,
871 struct client_crypto_s *crypto, status_msg_t which)
873 gpg_error_t rc = 0;
874 goffset len = CRYPTO_BLOCKSIZE(crypto);
875 gpointer p = gcry_malloc(len);
876 goffset total = 0;
877 gpointer inbuf;
879 if (!p)
880 return gpg_err_code_from_errno(ENOMEM);
882 if (crypto->insize < CRYPTO_BLOCKSIZE(crypto))
883 len = crypto->insize;
885 pth_cleanup_push(gcry_free, p);
887 for (;;) {
888 inbuf = (guchar *)crypto->inbuf + total;
889 guchar *tmp;
891 if (len + total > crypto->insize)
892 len = crypto->blocksize;
894 if (which == STATUS_ENCRYPT)
895 rc = gcry_cipher_encrypt(crypto->gh, p, len, inbuf, len);
896 else
897 rc = gcry_cipher_decrypt(crypto->gh, p, len, inbuf, len);
899 if (rc)
900 goto done;
902 tmp = (guchar *)crypto->inbuf + total;
903 memmove(tmp, p, len);
904 total += len;
906 if (total >= crypto->insize)
907 break;
909 pth_cancel_point();
912 done:
913 pth_cleanup_pop(1);
914 return rc;
917 /* The crypto struct must be setup for iterations and .key. */
918 gpg_error_t do_xml_encrypt(struct client_s *client,
919 struct client_crypto_s *crypto, const gchar *filename)
921 goffset len = crypto->insize;
922 gpointer inbuf;
923 gchar *p;
924 gpg_error_t rc;
925 guint64 iter_progress = 0ULL, n_iter = 0ULL, xiter = 0ULL;
926 gchar tmp[FILENAME_MAX];
927 struct stat st;
928 mode_t mode = 0;
929 guint hashlen = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
931 if (!crypto->fh->ver.fh2.iter) {
933 * cache_file_count() needs both .used == TRUE and a valid key in
934 * order for it to count as a used cache entry. Fixes CACHE status
935 * messages.
937 memset(crypto->key, '!', hashlen);
938 goto write_file;
942 * Resize the existing xml buffer to the block size required by gcrypt
943 * rather than duplicating it and wasting memory.
945 len = (crypto->insize / crypto->blocksize) * crypto->blocksize;
947 if (crypto->insize % crypto->blocksize)
948 len += crypto->blocksize;
950 inbuf = gcry_realloc(crypto->inbuf, len);
952 if (!inbuf) {
953 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
954 return gpg_error_from_errno(ENOMEM);
957 crypto->inbuf = inbuf;
958 crypto->insize = len;
959 gcry_create_nonce(crypto->fh->ver.fh2.iv, crypto->blocksize);
960 crypto->tkey = gcry_malloc(hashlen);
962 if (!crypto->tkey) {
963 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
964 return gpg_error_from_errno(ENOMEM);
967 memcpy(crypto->tkey, crypto->key, hashlen);
968 guchar *tkey = crypto->tkey;
969 tkey[0] ^= 1;
971 if ((rc = gcry_cipher_setkey(crypto->gh, crypto->tkey, crypto->keysize))) {
972 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
973 return rc;
976 iter_progress = (guint64)get_key_file_double(
977 client ? client->filename : "global", "iteration_progress");
979 if (iter_progress && crypto->fh->ver.fh2.iter >= iter_progress) {
980 rc = send_status(client ? client->ctx : NULL, STATUS_ENCRYPT,
981 "0 %llu", crypto->fh->ver.fh2.iter);
983 if (rc)
984 return rc;
987 while (xiter < crypto->fh->ver.fh2.iter-1) {
988 if (iter_progress > 0ULL && xiter >= iter_progress) {
989 if (!(xiter % iter_progress)) {
990 rc = send_status(client ? client->ctx : NULL, STATUS_ENCRYPT,
991 "%llu %llu", ++n_iter * iter_progress,
992 crypto->fh->ver.fh2.iter);
994 if (rc)
995 return rc;
999 if ((rc = gcry_cipher_setiv(crypto->gh, crypto->fh->ver.fh2.iv,
1000 crypto->blocksize))) {
1001 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
1002 return rc;
1005 rc = iterate_crypto_once(client, crypto, STATUS_ENCRYPT);
1007 if (rc) {
1008 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
1009 return rc;
1012 xiter++;
1015 if ((rc = gcry_cipher_setiv(crypto->gh, crypto->fh->ver.fh2.iv,
1016 crypto->blocksize))) {
1017 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
1018 return rc;
1021 if ((rc = gcry_cipher_setkey(crypto->gh, crypto->key, crypto->keysize))) {
1022 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
1023 return rc;
1026 rc = iterate_crypto_once(client, crypto, STATUS_ENCRYPT);
1028 if (rc)
1029 return rc;
1031 if (iter_progress && crypto->fh->ver.fh2.iter >= iter_progress) {
1032 rc = send_status(client ? client->ctx : NULL, STATUS_ENCRYPT,
1033 "%llu %llu", crypto->fh->ver.fh2.iter, crypto->fh->ver.fh2.iter);
1035 if (rc)
1036 return rc;
1039 write_file:
1040 tmp[0] = 0;
1042 if (filename) {
1043 if (!client && !g_ascii_strcasecmp(filename, "-")) {
1044 crypto->fh->fd = STDOUT_FILENO;
1045 goto do_write_file;
1048 if (lstat(filename, &st) == 0) {
1049 mode = st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO);
1051 if (!(mode & S_IWUSR))
1052 return gpg_error_from_errno(EACCES);
1054 else if (errno != ENOENT)
1055 return gpg_error_from_errno(errno);
1057 g_snprintf(tmp, sizeof(tmp), "%s.XXXXXX", filename);
1058 crypto->fh->fd = mkstemp(tmp);
1060 if (crypto->fh->fd == -1) {
1061 rc = errno;
1062 p = strrchr(tmp, '/');
1063 p++;
1064 log_write("%s: %s", p, strerror(rc));
1065 return gpg_error_from_errno(rc);
1068 pth_cleanup_push(cleanup_unlink_cb, tmp);
1070 else
1072 * xml_import() or convert_file() from command line.
1074 crypto->fh->fd = STDOUT_FILENO;
1076 do_write_file:
1077 crypto->fh->ver.fh2.magic[0] = '\177';
1078 crypto->fh->ver.fh2.magic[1] = 'P';
1079 crypto->fh->ver.fh2.magic[2] = 'W';
1080 crypto->fh->ver.fh2.magic[3] = 'M';
1081 crypto->fh->ver.fh2.magic[4] = 'D';
1082 crypto->fh->ver.fh2.version = VERSION_HEX;
1083 len = pth_write(crypto->fh->fd, &crypto->fh->ver.fh2, sizeof(crypto->fh->ver.fh2));
1085 if (len != sizeof(crypto->fh->ver.fh2)) {
1086 len = errno;
1088 if (tmp[0])
1089 pth_cleanup_pop(1);
1091 return gpg_error_from_errno(len);
1094 len = pth_write(crypto->fh->fd, crypto->inbuf, crypto->insize);
1096 if (len != crypto->insize) {
1097 len = errno;
1099 if (tmp[0])
1100 pth_cleanup_pop(1);
1102 return gpg_error_from_errno(len);
1105 if (fsync(crypto->fh->fd) == -1) {
1106 len = errno;
1108 if (tmp[0])
1109 pth_cleanup_pop(1);
1111 return gpg_error_from_errno(len);
1114 if (tmp[0]) {
1115 #ifdef WITH_LIBACL
1116 acl_t acl;
1117 #endif
1118 if (mode && get_key_file_boolean(filename, "backup") == TRUE) {
1119 gchar tmp2[FILENAME_MAX];
1121 g_snprintf(tmp2, sizeof(tmp2), "%s.backup", filename);
1122 #ifdef WITH_LIBACL
1123 acl = acl_get_file(filename, ACL_TYPE_ACCESS);
1125 if (!acl)
1126 log_write("ACL: %s: %s", filename, strerror(errno));
1127 #endif
1129 if (rename(filename, tmp2) == -1) {
1130 len = errno;
1131 pth_cleanup_pop(1);
1132 #ifdef WITH_LIBACL
1133 if (acl)
1134 acl_free(acl);
1135 #endif
1136 return gpg_error_from_errno(len);
1139 #ifdef WITH_LIBACL
1140 else {
1141 acl = acl_get_file(".", ACL_TYPE_DEFAULT);
1143 if (!acl)
1144 log_write("ACL: %s: %s", filename, strerror(errno));
1146 #endif
1148 if (rename(tmp, filename) == -1) {
1149 len = errno;
1150 pth_cleanup_pop(1);
1151 #ifdef WITH_LIBACL
1152 if (acl)
1153 acl_free(acl);
1154 #endif
1155 return gpg_error_from_errno(len);
1158 pth_cleanup_pop(0);
1160 if (mode)
1161 chmod(filename, mode);
1163 #ifdef WITH_LIBACL
1164 if (acl && acl_set_file(filename, ACL_TYPE_ACCESS, acl))
1165 log_write("ACL: %s: %s", filename, strerror(errno));
1167 if (acl)
1168 acl_free(acl);
1169 #endif
1172 if (client && lstat(filename, &st) == 0)
1173 client->mtime = st.st_mtime;
1175 return 0;
1178 gpg_error_t update_save_flags(const gchar *filename,
1179 struct client_crypto_s *crypto)
1181 gpg_error_t rc;
1182 guint64 iter;
1184 /* New file? */
1185 if (!crypto->fh) {
1186 crypto->fh = g_malloc0(sizeof(file_header_internal_t));
1188 if (!crypto->fh)
1189 return GPG_ERR_ENOMEM;
1192 rc = init_client_crypto2(filename, crypto);
1194 if (rc)
1195 return rc;
1197 if (filename && !crypto->fh->v1) {
1198 iter = (guint64)get_key_file_double(filename, "iterations");
1199 crypto->fh->ver.fh2.iter = iter < 0ULL ? 0ULL : iter;
1202 return 0;
1205 static gpg_error_t save_command_finalize(assuan_context_t ctx, guchar *key,
1206 gboolean cached)
1208 struct client_s *client = assuan_get_pointer(ctx);
1209 gpointer xmlbuf;
1210 gulong outsize = 0;
1211 guint len;
1212 gint clevel;
1213 gint timeout;
1214 gpointer outbuf;
1215 gint zrc;
1216 gpg_error_t rc;
1218 if (client->crypto->key && client->crypto->key != key)
1219 gcry_free(client->crypto->key);
1221 client->crypto->key = key;
1222 rc = update_timestamp(client->doc);
1224 if (rc)
1225 return send_error(ctx, rc);
1227 xmlDocDumpFormatMemory(client->doc, (xmlChar **)&xmlbuf, (gint *)&len, 0);
1228 pth_cleanup_push(xmlFree, xmlbuf);
1229 clevel = get_key_file_integer(client->filename, "compression_level");
1231 if (clevel < 0)
1232 clevel = 0;
1234 if (do_compress(ctx, clevel, xmlbuf, len, &outbuf, &outsize, &zrc) == FALSE) {
1235 pth_cleanup_pop(1);
1236 cleanup_crypto(&client->crypto);
1238 if (zrc == Z_MEM_ERROR)
1239 return send_syserror(ctx, ENOMEM);
1240 else
1241 return send_error(ctx, GPG_ERR_COMPR_ALGO);
1243 else {
1244 pth_cleanup_pop(1);
1245 xmlbuf = outbuf;
1246 len = outsize;
1249 client->crypto->inbuf = xmlbuf;
1250 client->crypto->insize = len;
1251 rc = update_save_flags(client->filename, client->crypto);
1253 if (rc) {
1254 cleanup_crypto(&client->crypto);
1255 log_write("%s(%i): %s", __FILE__, __LINE__, pwmd_strerror(rc));
1256 return send_error(ctx, rc);
1259 rc = do_xml_encrypt(client, client->crypto, client->filename);
1261 if (rc) {
1262 cleanup_crypto(&client->crypto);
1263 return send_error(ctx, rc);
1266 timeout = get_key_file_integer(client->filename, "cache_timeout");
1267 CACHE_LOCK(client->ctx);
1269 if (cached) {
1270 cache_reset_timeout(client->md5file, timeout);
1271 CACHE_UNLOCK;
1273 if (client->new == TRUE)
1274 send_status_all(STATUS_CACHE);
1276 client->new = FALSE;
1277 cleanup_crypto(&client->crypto);
1278 return send_error(ctx, 0);
1281 if (cache_update_key(client->md5file, client->crypto->key) == FALSE) {
1282 CACHE_UNLOCK;
1283 cleanup_crypto(&client->crypto);
1284 return send_syserror(ctx, ENOMEM);
1287 client->new = FALSE;
1288 cache_reset_timeout(client->md5file, timeout);
1289 CACHE_UNLOCK;
1290 send_status_all(STATUS_CACHE);
1291 cleanup_crypto(&client->crypto);
1292 return send_error(ctx, 0);
1295 static gpg_error_t save_command(assuan_context_t ctx, gchar *line)
1297 gboolean cached = FALSE;
1298 struct stat st;
1299 struct client_s *client = assuan_get_pointer(ctx);
1300 guint hashlen = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
1301 gpg_error_t rc;
1303 rc = command_startup(ctx);
1305 if (rc)
1306 return send_error(ctx, rc);
1308 if (line && *line)
1309 log_write2("ARGS=%s", "<passphrase>");
1311 if (lstat(client->filename, &st) == -1 && errno != ENOENT)
1312 return send_syserror(ctx, errno);
1314 if (errno != ENOENT && !S_ISREG(st.st_mode)) {
1315 log_write("%s: %s", client->filename, pwmd_strerror(GPG_ERR_ENOANO));
1316 return send_error(ctx, GPG_ERR_ENOANO);
1319 CACHE_LOCK(ctx);
1320 cached = cache_iscached(client->md5file);
1321 CACHE_UNLOCK;
1324 * If a cache entry doesn't exist for this file and the file has a
1325 * "key_file" or "key" parameter, then it's an error. The reason is that
1326 * cache expiration would be useless.
1328 if (cached == FALSE) {
1329 gchar *tmp = get_key_file_string(client->filename, "key_file");
1331 if (tmp) {
1332 g_free(tmp);
1333 return send_error(ctx, GPG_ERR_WRONG_KEY_USAGE);
1337 cached = FALSE;
1339 /* New file? */
1340 if (!client->crypto) {
1341 client->crypto = init_client_crypto();
1343 if (!client->crypto) {
1344 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1345 return send_syserror(ctx, ENOMEM);
1349 client->crypto->key = gcry_malloc(hashlen);
1351 if (!client->crypto->key) {
1352 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1353 cleanup_crypto(&client->crypto);
1354 return send_syserror(ctx, ENOMEM);
1357 memset(client->crypto->key, '!', hashlen);
1359 if (get_key_file_double(client->filename, "iterations") <= 0)
1360 goto done;
1362 if (!line || !*line) {
1363 client->crypto->tkey = gcry_malloc(hashlen);
1365 if (!client->crypto->tkey) {
1366 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1367 cleanup_crypto(&client->crypto);
1368 return send_syserror(ctx, ENOMEM);
1371 memset(client->crypto->tkey, '!', hashlen);
1372 CACHE_LOCK(ctx);
1374 if (cache_get_key(client->md5file, client->crypto->key) == FALSE ||
1375 !memcmp(client->crypto->key, client->crypto->tkey, hashlen)) {
1376 CACHE_UNLOCK;
1378 #ifdef WITH_PINENTRY
1379 gpg_error_t rc;
1381 if (client->pinentry->enable == FALSE ||
1382 get_key_file_boolean(client->filename, "enable_pinentry") == FALSE) {
1383 /* Empty keys are allowed. */
1384 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, "", 1);
1385 goto done;
1388 lock_pin_mutex(client);
1389 client->pinentry->which = PINENTRY_SAVE;
1390 rc = pinentry_fork(ctx);
1392 if (rc) {
1393 unlock_pin_mutex(client->pinentry);
1394 return send_error(ctx, rc);
1397 client->pinentry->cb = save_command_finalize;
1398 client->pinentry->status = PINENTRY_INIT;
1399 return 0;
1400 #else
1401 /* Empty keys are allowed. */
1402 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, "", 1);
1403 goto done;
1404 #endif
1406 else {
1407 CACHE_UNLOCK;
1408 cached = TRUE;
1411 else
1412 gcry_md_hash_buffer(GCRY_MD_SHA256, client->crypto->key, line,
1413 strlen(line));
1415 done:
1416 return save_command_finalize(ctx, client->crypto->key, cached);
1419 static gpg_error_t delete_command(assuan_context_t ctx, gchar *line)
1421 struct client_s *client = assuan_get_pointer(ctx);
1422 gchar **req;
1423 gpg_error_t rc;
1424 xmlNodePtr n;
1426 rc = command_startup(ctx);
1428 if (rc)
1429 return send_error(ctx, rc);
1431 log_write2("ARGS=\"%s\"", line);
1433 if (strchr(line, '\t'))
1434 req = split_input_line(line, "\t", -1);
1435 else
1436 req = split_input_line(line, " ", -1);
1438 if (!req || !*req)
1439 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
1441 n = find_account(client->doc, &req, &rc, NULL, 0);
1443 if (!n) {
1444 g_strfreev(req);
1445 return send_error(ctx, rc);
1449 * No sub-node defined. Remove the entire node (account).
1451 if (!req[1]) {
1452 if (n) {
1453 xmlUnlinkNode(n);
1454 xmlFreeNode(n);
1457 g_strfreev(req);
1458 return send_error(ctx, 0);
1461 n = find_elements(client->doc, n->children, req+1, &rc, NULL, NULL, NULL, FALSE, 0, NULL);
1462 g_strfreev(req);
1464 if (!n)
1465 return send_error(ctx, rc);
1467 if (n) {
1468 xmlUnlinkNode(n);
1469 xmlFreeNode(n);
1472 return send_error(ctx, 0);
1476 * Don't return with assuan_process_done() here. This has been called from
1477 * assuan_process_next() and the command should be finished in
1478 * client_thread().
1480 static gpg_error_t store_command_finalize(gpointer data, gpg_error_t assuan_rc,
1481 guchar *line, gsize len)
1483 assuan_context_t ctx = data;
1484 struct client_s *client = assuan_get_pointer(ctx);
1485 gchar **req;
1486 xmlNodePtr n;
1487 gpg_error_t rc = file_modified(client);
1489 if (assuan_rc || rc) {
1490 if (line)
1491 xfree(line);
1492 return assuan_rc ? assuan_rc : rc;
1495 req = split_input_line((gchar *)line, "\t", 0);
1496 xfree(line);
1498 if (!req || !*req)
1499 return EPWMD_COMMAND_SYNTAX;
1501 again:
1502 n = find_account(client->doc, &req, &rc, NULL, 0);
1504 if (rc && rc == EPWMD_ELEMENT_NOT_FOUND) {
1505 rc = new_account(client->doc, *req);
1507 if (rc) {
1508 g_strfreev(req);
1509 return rc;
1512 goto again;
1515 if (!n) {
1516 g_strfreev(req);
1517 return rc;
1520 if (req[1]) {
1521 if (!n->children)
1522 create_elements_cb(n, req+1, &rc, NULL);
1523 else
1524 find_elements(client->doc, n->children, req+1, &rc,
1525 NULL, NULL, create_elements_cb, FALSE, 0, NULL);
1528 g_strfreev(req);
1529 client->inquire_status = INQUIRE_DONE;
1530 return rc;
1533 static gpg_error_t store_command(assuan_context_t ctx, gchar *line)
1535 struct client_s *client = assuan_get_pointer(ctx);
1536 gpg_error_t rc;
1538 rc = command_startup(ctx);
1540 if (rc)
1541 return send_error(ctx, rc);
1543 rc = assuan_inquire_ext(ctx, "STORE", 0, store_command_finalize, ctx);
1545 if (rc)
1546 return send_error(ctx, rc);
1548 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1549 client->inquire_status = INQUIRE_BUSY;
1550 return 0;
1553 static void *send_data_cb(void *arg)
1555 struct assuan_cmd_s *data = arg;
1556 gint old;
1557 gpg_error_t rc;
1559 pth_cancel_state(PTH_CANCEL_ENABLE|PTH_CANCEL_ASYNCHRONOUS, &old);
1560 rc = assuan_send_data(data->ctx, data->line, data->line_len);
1561 pth_cancel_state(old, NULL);
1562 pth_exit((void *)rc);
1563 return NULL;
1566 /* For every assuan command that needs to be sent to the client, a timeout is
1567 * needed to determine if the client lost the connection. The timeout is the
1568 * same as the "keepalive" configuration parameter or a default if unset.
1570 gpg_error_t do_assuan_command(assuan_context_t ctx,
1571 void *(*cb)(void *data), void *data)
1573 pth_attr_t attr = pth_attr_new();
1574 pth_t tid;
1575 gint n;
1576 gint to = get_key_file_integer("global", "keepalive");
1577 pth_event_t ev, tev;
1578 pth_status_t st;
1579 gpg_error_t rc;
1581 pth_attr_init(attr);
1582 pth_attr_set(attr, PTH_ATTR_JOINABLE, TRUE);
1583 tid = pth_spawn(attr, cb, data);
1584 n = errno;
1585 pth_attr_destroy(attr);
1587 if (!tid) {
1588 log_write("%s(%i): pth_spawn(): %s", __FILE__, __LINE__,
1589 _gpg_strerror(gpg_error_from_errno(n)));
1590 return gpg_error_from_errno(n);
1593 pth_cleanup_push(cleanup_cancel_cb, tid);
1594 to = to <= 0 ? DEFAULT_KEEPALIVE_TO : to;
1595 ev = pth_event(PTH_EVENT_TID|PTH_UNTIL_TID_DEAD, tid);
1596 tev = pth_event(PTH_EVENT_TIME, pth_timeout(to, 0));
1597 ev = pth_event_concat(ev, tev, NULL);
1598 pth_cleanup_push(cleanup_ev_cb, ev);
1599 pth_yield(tid);
1600 pth_wait(ev);
1601 st = pth_event_status(ev);
1603 if (st == PTH_STATUS_FAILED) {
1604 pth_cancel(tid);
1605 rc = GPG_ERR_ASS_WRITE_ERROR;
1607 else if (st == PTH_STATUS_OCCURRED)
1608 pth_join(tid, (void **)&rc);
1609 else {
1610 st = pth_event_status(tev);
1612 if (st == PTH_STATUS_OCCURRED) {
1613 pth_cancel(tid);
1614 rc = GPG_ERR_ASS_WRITE_ERROR;
1618 pth_cleanup_pop(1);
1619 pth_cleanup_pop(0);
1620 return rc;
1623 static gpg_error_t xfer_data(assuan_context_t ctx, const gchar *line,
1624 gint total)
1626 gint to_send;
1627 gint sent = 0;
1628 gpg_error_t rc;
1629 struct assuan_cmd_s data;
1630 gint progress = get_key_file_integer("global", "xfer_progress");
1631 gint flush = 0;
1633 progress = progress>0 ? (progress/ASSUAN_LINELENGTH)*ASSUAN_LINELENGTH : 0;
1634 to_send = total < ASSUAN_LINELENGTH ? total : ASSUAN_LINELENGTH;
1635 data.ctx = ctx;
1636 rc = send_status(ctx, STATUS_XFER, "%li %li", sent, total);
1638 if (rc)
1639 return rc;
1641 again:
1642 do {
1643 if (sent + to_send > total)
1644 to_send = total - sent;
1646 data.line = flush ? NULL : (gchar *)line+sent;
1647 data.line_len = flush ? 0 : to_send;
1648 rc = do_assuan_command(ctx, send_data_cb, &data);
1650 if (!rc) {
1651 sent += flush ? 0 : to_send;
1653 if ((progress && !(sent % progress) && sent != total) ||
1654 (sent == total && flush))
1655 rc = send_status(ctx, STATUS_XFER, "%li %li", sent, total);
1657 if (!flush && !rc && sent == total) {
1658 flush = 1;
1659 goto again;
1662 } while (!rc && sent < total);
1664 return rc;
1667 static gpg_error_t get_command(assuan_context_t ctx, gchar *line)
1669 struct client_s *client = assuan_get_pointer(ctx);
1670 gchar **req;
1671 gpg_error_t rc;
1672 xmlNodePtr n;
1674 rc = command_startup(ctx);
1676 if (rc)
1677 return send_error(ctx, rc);
1679 log_write2("ARGS=\"%s\"", line);
1680 req = split_input_line(line, "\t", -1);
1682 if (!req || !*req) {
1683 g_strfreev(req);
1684 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
1687 n = find_account(client->doc, &req, &rc, NULL, 0);
1689 if (!n) {
1690 g_strfreev(req);
1691 return send_error(ctx, rc);
1694 if (req[1])
1695 n = find_elements(client->doc, n->children, req+1, &rc, NULL, NULL, NULL, FALSE, 0, NULL);
1697 g_strfreev(req);
1699 if (rc)
1700 return send_error(ctx, rc);
1702 if (!n || !n->children)
1703 return send_error(ctx, EPWMD_EMPTY_ELEMENT);
1705 n = find_text_node(n->children);
1707 if (!n || !n->content || !*n->content)
1708 return send_error(ctx, EPWMD_EMPTY_ELEMENT);
1710 rc = xfer_data(ctx, (gchar *)n->content, xmlStrlen(n->content));
1711 return send_error(ctx, rc);
1714 static xmlNodePtr realpath_elements_cb(xmlNodePtr node, gchar **target,
1715 gpg_error_t *rc, gchar **req_orig, void *data)
1717 gchar *path = *(gchar **)data;
1718 gchar *tmp = NULL, *result;
1720 if (path) {
1721 g_free(path);
1722 *(gchar **)data = NULL;
1725 path = g_strjoinv("\t", target);
1727 if (!path) {
1728 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1729 *rc = gpg_error_from_errno(ENOMEM);
1730 return NULL;
1733 if (req_orig) {
1734 tmp = g_strjoinv("\t", req_orig);
1736 if (!tmp) {
1737 g_free(path);
1738 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1739 *rc = gpg_error_from_errno(ENOMEM);
1740 return NULL;
1744 if (tmp && *tmp)
1745 result = g_strdup_printf("%s\t%s", path, tmp);
1746 else
1747 result = g_strdup(path);
1749 if (!result) {
1750 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1751 *rc = gpg_error_from_errno(ENOMEM);
1752 g_free(path);
1753 g_free(tmp);
1754 return NULL;
1757 g_free(path);
1758 g_free(tmp);
1759 *(gchar **)data = result;
1760 return node;
1763 static void list_command_cleanup1(void *arg);
1764 static gpg_error_t realpath_command(assuan_context_t ctx, gchar *line)
1766 gpg_error_t rc;
1767 struct client_s *client = assuan_get_pointer(ctx);
1768 gchar **req;
1769 gchar *t;
1770 gint i;
1771 xmlNodePtr n;
1772 GString *string;
1773 gchar *rp = NULL;
1775 rc = command_startup(ctx);
1777 if (rc)
1778 return send_error(ctx, rc);
1780 log_write2("ARGS=\"%s\"", line);
1782 if (strchr(line, '\t') != NULL) {
1783 if ((req = split_input_line(line, "\t", 0)) == NULL)
1784 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
1786 else {
1787 if ((req = split_input_line(line, " ", 0)) == NULL)
1788 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
1791 n = find_account(client->doc, &req, &rc, NULL, 0);
1793 if (!n) {
1794 g_strfreev(req);
1795 return send_error(ctx, rc);
1798 rp = g_strjoinv("\t", req);
1800 if (!rp) {
1801 g_strfreev(req);
1802 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1803 return send_syserror(ctx, ENOMEM);
1806 if (req[1]) {
1807 n = find_elements(client->doc, n->children, req+1, &rc,
1808 NULL, realpath_elements_cb, NULL, FALSE, 0, &rp);
1810 if (!n) {
1811 g_free(rp);
1812 g_strfreev(req);
1813 return send_error(ctx, rc);
1817 string = g_string_new(rp);
1818 g_free(rp);
1819 g_strfreev(req);
1821 if (!string) {
1822 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1823 return send_syserror(ctx, ENOMEM);
1826 again:
1827 for (i = 0, t = string->str + i; *t; t++, i++) {
1828 if ((!i && *t != '!') || (*t == '\t' && *(t+1) && *(t+1) != '!')) {
1829 string = g_string_insert_c(string, !i ? i++ : ++i, '!');
1830 goto again;
1834 pth_cleanup_push(list_command_cleanup1, string);
1835 rc = xfer_data(ctx, string->str, string->len);
1836 pth_cleanup_pop(1);
1837 return send_error(ctx, rc);
1840 static void list_command_cleanup1(void *arg)
1842 g_string_free((GString *)arg, TRUE);
1845 static void list_command_cleanup2(void *arg)
1847 struct element_list_s *elements = arg;
1849 if (elements) {
1850 gint total = g_slist_length(elements->list);
1851 gint i;
1853 for (i = 0; i < total; i++) {
1854 gchar *tmp = g_slist_nth_data(elements->list, i);
1855 g_free(tmp);
1858 g_slist_free(elements->list);
1860 if (elements->prefix)
1861 g_free(elements->prefix);
1863 g_free(elements);
1867 static gpg_error_t list_command(assuan_context_t ctx, gchar *line)
1869 struct client_s *client = assuan_get_pointer(ctx);
1870 gpg_error_t rc;
1871 struct element_list_s *elements = NULL;
1872 gchar *tmp;
1874 rc = command_startup(ctx);
1876 if (rc)
1877 return send_error(ctx, rc);
1879 if (disable_list_and_dump == TRUE)
1880 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
1882 log_write2("ARGS=\"%s\"", line);
1884 if (!*line) {
1885 GString *str;
1887 rc = list_accounts(client->doc, &str);
1889 if (rc)
1890 return send_error(ctx, rc);
1892 pth_cleanup_push(list_command_cleanup1, str);
1893 rc = xfer_data(ctx, str->str, str->len);
1894 pth_cleanup_pop(1);
1895 return send_error(ctx, rc);
1898 elements = g_malloc0(sizeof(struct element_list_s));
1900 if (!elements) {
1901 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1902 rc = gpg_err_code_from_errno(ENOMEM);
1903 goto fail;
1906 pth_cleanup_push(list_command_cleanup2, elements);
1907 rc = create_path_list(client->doc, elements, line);
1909 if (rc)
1910 goto fail;
1912 if (elements) {
1913 gint total = g_slist_length(elements->list);
1914 gint i;
1915 GString *str;
1917 if (!total) {
1918 rc = EPWMD_EMPTY_ELEMENT;
1919 goto fail;
1922 str = g_string_new(NULL);
1924 if (!str) {
1925 rc = gpg_err_code_from_errno(ENOMEM);
1926 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1927 goto fail;
1930 for (i = 0; i < total; i++) {
1931 tmp = g_slist_nth_data(elements->list, i);
1932 g_string_append_printf(str, "%s%s", tmp, i+1 == total ? "" : "\n");
1935 pth_cleanup_push(list_command_cleanup1, str);
1936 rc = xfer_data(ctx, str->str, str->len);
1937 pth_cleanup_pop(1);
1939 else
1940 rc = EPWMD_EMPTY_ELEMENT;
1942 fail:
1943 pth_cleanup_pop(1);
1944 return send_error(ctx, rc);
1948 * req[0] - element path
1950 static gint attribute_list(assuan_context_t ctx, gchar **req)
1952 struct client_s *client = assuan_get_pointer(ctx);
1953 gchar **attrlist = NULL;
1954 gint i = 0;
1955 gchar **path = NULL;
1956 xmlAttrPtr a;
1957 xmlNodePtr n, an;
1958 gchar *line;
1959 gpg_error_t rc;
1961 if (!req || !req[0])
1962 return EPWMD_COMMAND_SYNTAX;
1964 if ((path = split_input_line(req[0], "\t", 0)) == NULL) {
1966 * The first argument may be only an account.
1968 if ((path = split_input_line(req[0], " ", 0)) == NULL)
1969 return EPWMD_COMMAND_SYNTAX;
1972 n = find_account(client->doc, &path, &rc, NULL, 0);
1974 if (!n) {
1975 g_strfreev(path);
1976 return rc;
1979 if (path[1]) {
1980 n = find_elements(client->doc, n->children, path+1, &rc,
1981 NULL, NULL, NULL, FALSE, 0, NULL);
1983 if (!n) {
1984 g_strfreev(path);
1985 return rc;
1989 g_strfreev(path);
1991 for (a = n->properties; a; a = a->next) {
1992 gchar **pa;
1994 if ((pa = g_realloc(attrlist, (i + 2) * sizeof(gchar *))) == NULL) {
1995 if (attrlist)
1996 g_strfreev(attrlist);
1998 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
1999 return gpg_error_from_errno(ENOMEM);
2002 attrlist = pa;
2003 an = a->children;
2004 attrlist[i] = g_strdup_printf("%s %s", (gchar *)a->name, (gchar *)an->content);
2006 if (!attrlist[i]) {
2007 g_strfreev(attrlist);
2008 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2009 return gpg_error_from_errno(ENOMEM);
2012 attrlist[++i] = NULL;
2015 if (!attrlist)
2016 return EPWMD_EMPTY_ELEMENT;
2018 line = g_strjoinv("\n", attrlist);
2020 if (!line) {
2021 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2022 g_strfreev(attrlist);
2023 return gpg_error_from_errno(ENOMEM);
2026 pth_cleanup_push(g_free, line);
2027 pth_cleanup_push(req_cleanup, attrlist);
2028 rc = xfer_data(ctx, line, strlen(line));
2029 pth_cleanup_pop(1);
2030 pth_cleanup_pop(1);
2031 return rc;
2035 * req[0] - attribute
2036 * req[1] - element path
2038 static gint attribute_delete(struct client_s *client, gchar **req)
2040 xmlAttrPtr a;
2041 xmlNodePtr n;
2042 gchar **path = NULL;
2043 gpg_error_t rc;
2045 if (!req || !req[0] || !req[1])
2046 return EPWMD_COMMAND_SYNTAX;
2048 if ((path = split_input_line(req[1], "\t", 0)) == NULL) {
2050 * The first argument may be only an account.
2052 if ((path = split_input_line(req[1], " ", 0)) == NULL)
2053 return EPWMD_COMMAND_SYNTAX;
2057 * Don't remove the "name" attribute for the account element. To remove an
2058 * account use DELETE <account>.
2060 if (!path[1] && xmlStrEqual((xmlChar *)req[0], (xmlChar *)"name")) {
2061 rc = EPWMD_ATTR_SYNTAX;
2062 goto fail;
2065 n = find_account(client->doc, &path, &rc, NULL, 0);
2067 if (!n)
2068 goto fail;
2070 if (path[1]) {
2071 n = find_elements(client->doc, n->children, path+1, &rc,
2072 NULL, NULL, NULL, FALSE, 0, NULL);
2074 if (!n)
2075 goto fail;
2078 g_strfreev(path);
2080 if ((a = xmlHasProp(n, (xmlChar *)req[0])) == NULL)
2081 return EPWMD_ATTR_NOT_FOUND;
2083 if (xmlRemoveProp(a) == -1)
2084 return EPWMD_LIBXML_ERROR;
2086 return 0;
2088 fail:
2089 g_strfreev(path);
2090 return rc;
2093 static xmlNodePtr create_element_path(struct client_s *client, gchar ***path,
2094 gpg_error_t *rc)
2096 gchar **src = *path;
2097 gchar **src_orig = g_strdupv(src);
2098 xmlNodePtr n = NULL;
2100 *rc = 0;
2102 if (!src_orig) {
2103 *rc = gpg_error_from_errno(ENOMEM);
2104 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2105 goto fail;
2108 again:
2109 n = find_account(client->doc, &src, rc, NULL, 0);
2111 if (!n) {
2112 if (*rc == EPWMD_ELEMENT_NOT_FOUND) {
2113 *rc = new_account(client->doc, src[0]);
2115 if (*rc)
2116 goto fail;
2118 goto again;
2120 else
2121 goto fail;
2124 if (src[1]) {
2125 if (!n->children)
2126 n = create_target_elements_cb(n, src+1, rc, NULL);
2127 else
2128 n = find_elements(client->doc, n->children, src+1, rc,
2129 NULL, NULL, create_target_elements_cb, FALSE, 0, NULL);
2131 if (!n)
2132 goto fail;
2135 * Reset the position of the element tree now that the elements
2136 * have been created.
2138 g_strfreev(src);
2139 src = src_orig;
2140 src_orig = NULL;
2141 n = find_account(client->doc, &src, rc, NULL, 0);
2143 if (!n)
2144 goto fail;
2146 n = find_elements(client->doc, n->children, src+1, rc,
2147 NULL, NULL, NULL, FALSE, 0, NULL);
2149 if (!n)
2150 goto fail;
2153 fail:
2154 if (src_orig)
2155 g_strfreev(src_orig);
2157 *path = src;
2158 return n;
2162 * Creates a "target" attribute. When other commands encounter an element with
2163 * this attribute, the element path is modified to the target value. If the
2164 * source element path doesn't exist when using 'ATTR SET target', it is
2165 * created, but the destination element path must exist.
2167 * req[0] - source element path
2168 * req[1] - destination element path
2170 static gpg_error_t target_attribute(struct client_s *client, gchar **req)
2172 gchar **src, **dst, *line = NULL;
2173 gpg_error_t rc;
2174 xmlNodePtr n;
2176 if (!req || !req[0] || !req[1])
2177 return EPWMD_COMMAND_SYNTAX;
2179 if ((src = split_input_line(req[0], "\t", 0)) == NULL) {
2181 * The first argument may be only an account.
2183 if ((src = split_input_line(req[0], " ", 0)) == NULL)
2184 return EPWMD_COMMAND_SYNTAX;
2187 if ((dst = split_input_line(req[1], "\t", 0)) == NULL) {
2189 * The first argument may be only an account.
2191 if ((dst = split_input_line(req[1], " ", 0)) == NULL) {
2192 rc = EPWMD_COMMAND_SYNTAX;
2193 goto fail;
2197 n = find_account(client->doc, &dst, &rc, NULL, 0);
2200 * Make sure the destination element path exists.
2202 if (!n)
2203 goto fail;
2205 if (dst[1]) {
2206 n = find_elements(client->doc, n->children, dst+1, &rc,
2207 NULL, NULL, NULL, FALSE, 0, NULL);
2209 if (!n)
2210 goto fail;
2213 n = create_element_path(client, &src, &rc);
2215 if (rc)
2216 goto fail;
2218 line = g_strjoinv("\t", dst);
2220 if (!line) {
2221 rc = gpg_error_from_errno(ENOMEM);
2222 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2223 goto fail;
2226 rc = add_attribute(n, "target", line);
2228 fail:
2229 g_free(line);
2230 g_strfreev(src);
2231 g_strfreev(dst);
2232 return rc;
2236 * req[0] - account name
2237 * req[1] - new name
2239 static gpg_error_t name_attribute(struct client_s *client, gchar **req)
2241 gpg_error_t rc;
2242 gchar **tmp;
2243 xmlNodePtr n;
2245 tmp = g_strdupv(req);
2247 if (!tmp) {
2248 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2249 return gpg_error_from_errno(ENOMEM);
2252 n = find_account(client->doc, &tmp, &rc, NULL, 0);
2253 g_strfreev(tmp);
2255 if (!n)
2256 return rc;
2258 if (g_utf8_collate(req[0], req[1]) == 0)
2259 return 0;
2262 * Will not overwrite an existing account.
2264 tmp = g_strdupv(req+1);
2266 if (!tmp) {
2267 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2268 return gpg_error_from_errno(ENOMEM);
2271 n = find_account(client->doc, &tmp, &rc, NULL, 0);
2272 g_strfreev(tmp);
2274 if (rc && rc != EPWMD_ELEMENT_NOT_FOUND)
2275 return rc;
2277 if (n)
2278 return EPWMD_ACCOUNT_EXISTS;
2281 * Whitespace not allowed in account names.
2283 if (contains_whitespace(req[1]) == TRUE)
2284 return EPWMD_ATTR_SYNTAX;
2286 tmp = g_strdupv(req);
2288 if (!tmp) {
2289 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2290 return gpg_error_from_errno(ENOMEM);
2293 n = find_account(client->doc, &tmp, &rc, NULL, 0);
2294 g_strfreev(tmp);
2296 if (!n)
2297 return EPWMD_ELEMENT_NOT_FOUND;
2299 return add_attribute(n, "name", req[1]);
2303 * req[0] - attribute
2304 * req[1] - element path
2306 static gint attribute_get(assuan_context_t ctx, gchar **req)
2308 struct client_s *client = assuan_get_pointer(ctx);
2309 xmlNodePtr n;
2310 xmlChar *a;
2311 gchar **path= NULL;
2312 gpg_error_t rc;
2314 if (!req || !req[0] || !req[1])
2315 return EPWMD_COMMAND_SYNTAX;
2317 if (strchr(req[1], '\t')) {
2318 if ((path = split_input_line(req[1], "\t", 0)) == NULL)
2319 return EPWMD_COMMAND_SYNTAX;
2321 else {
2322 if ((path = split_input_line(req[1], " ", 0)) == NULL)
2323 return EPWMD_COMMAND_SYNTAX;
2326 n = find_account(client->doc, &path, &rc, NULL, 0);
2328 if (!n)
2329 goto fail;
2331 if (path[1]) {
2332 n = find_elements(client->doc, n->children, path+1, &rc,
2333 NULL, NULL, NULL, FALSE, 0, NULL);
2335 if (!n)
2336 goto fail;
2339 g_strfreev(path);
2341 if ((a = xmlGetProp(n, (xmlChar *)req[0])) == NULL)
2342 return EPWMD_ATTR_NOT_FOUND;
2344 pth_cleanup_push(xmlFree, a);
2345 rc = xfer_data(ctx, (gchar *)a, xmlStrlen(a));
2346 pth_cleanup_pop(1);
2347 return rc;
2349 fail:
2350 g_strfreev(path);
2351 return rc;
2355 * req[0] - attribute
2356 * req[1] - element path
2357 * req[2] - value
2359 static gint attribute_set(struct client_s *client, gchar **req)
2361 gchar **path = NULL;
2362 gpg_error_t rc;
2363 xmlNodePtr n;
2365 if (!req || !req[0] || !req[1] || !req[2])
2366 return EPWMD_COMMAND_SYNTAX;
2369 * Reserved attribute names.
2371 if (g_utf8_collate(req[0], "name") == 0) {
2373 * Only reserved for the account element. Not the rest of the
2374 * document.
2376 if (strchr(req[1], '\t') == NULL)
2377 return name_attribute(client, req + 1);
2379 else if (g_utf8_collate(req[0], "target") == 0)
2380 return target_attribute(client, req + 1);
2382 if ((path = split_input_line(req[1], "\t", 0)) == NULL) {
2384 * The first argument may be only an account.
2386 if ((path = split_input_line(req[1], " ", 0)) == NULL)
2387 return EPWMD_COMMAND_SYNTAX;
2390 n = find_account(client->doc, &path, &rc, NULL, 0);
2392 if (!n)
2393 goto fail;
2395 if (path[1]) {
2396 n = find_elements(client->doc, n->children, path+1, &rc,
2397 NULL, NULL, NULL, FALSE, 0, NULL);
2399 if (!n)
2400 goto fail;
2403 g_strfreev(path);
2404 return add_attribute(n, req[0], req[2]);
2406 fail:
2407 g_strfreev(path);
2408 return rc;
2412 * req[0] - command
2413 * req[1] - attribute name or element path if command is LIST
2414 * req[2] - element path
2415 * req[2] - element path or value
2417 static gpg_error_t attr_command(assuan_context_t ctx, gchar *line)
2419 struct client_s *client = assuan_get_pointer(ctx);
2420 gchar **req;
2421 gpg_error_t rc = 0;
2423 rc = command_startup(ctx);
2425 if (rc)
2426 return send_error(ctx, rc);
2428 log_write2("ARGS=\"%s\"", line);
2429 req = split_input_line(line, " ", 4);
2431 if (!req || !req[0] || !req[1]) {
2432 g_strfreev(req);
2433 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
2436 pth_cleanup_push(req_cleanup, req);
2438 if (g_ascii_strcasecmp(req[0], "SET") == 0)
2439 rc = attribute_set(client, req+1);
2440 else if (g_ascii_strcasecmp(req[0], "GET") == 0)
2441 rc = attribute_get(ctx, req+1);
2442 else if (g_ascii_strcasecmp(req[0], "DELETE") == 0)
2443 rc = attribute_delete(client, req+1);
2444 else if (g_ascii_strcasecmp(req[0], "LIST") == 0)
2445 rc = attribute_list(ctx, req+1);
2446 else
2447 rc = EPWMD_COMMAND_SYNTAX;
2449 pth_cleanup_pop(1);
2450 return send_error(ctx, rc);
2453 static gpg_error_t iscached_command(assuan_context_t ctx, gchar *line)
2455 gpg_error_t rc = command_startup(ctx);
2457 if (rc)
2458 return send_error(ctx, rc);
2460 gchar **req = split_input_line(line, " ", 0);
2461 guchar md5file[16];
2462 gchar *path, *tmp;
2464 if (!req || !*req) {
2465 g_strfreev(req);
2466 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
2469 log_write2("ARGS=\"%s\"", line);
2471 if (!valid_filename(req[0])) {
2472 g_strfreev(req);
2473 return EPWMD_INVALID_FILENAME;
2476 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, req[0], strlen(req[0]));
2477 CACHE_LOCK(ctx);
2479 if (cache_iscached(md5file)) {
2480 g_strfreev(req);
2481 CACHE_UNLOCK;
2482 return send_error(ctx, 0);
2485 CACHE_UNLOCK;
2486 tmp = get_key_file_string("global", "data_directory");
2488 if (!tmp) {
2489 g_strfreev(req);
2490 return gpg_error_from_errno(ENOMEM);
2493 path = expand_homedir(tmp);
2495 if (!path) {
2496 g_strfreev(req);
2497 g_free(tmp);
2498 return gpg_error_from_errno(ENOMEM);
2501 g_free(tmp);
2502 tmp = path;
2503 path = g_strdup_printf("%s/%s", tmp, req[0]);
2504 g_free(tmp);
2506 if (!path) {
2507 g_strfreev(req);
2508 return gpg_error_from_errno(ENOMEM);
2511 if (access(path, R_OK) == -1) {
2512 gpg_error_t rc = gpg_error_from_syserror();
2514 g_free(path);
2515 g_strfreev(req);
2516 return send_error(ctx, rc);
2519 g_free(path);
2520 return send_error(ctx, EPWMD_CACHE_NOT_FOUND);
2523 static gpg_error_t clearcache_command(assuan_context_t ctx, gchar *line)
2525 gpg_error_t rc = command_startup(ctx);
2527 if (rc)
2528 return send_error(ctx, rc);
2530 struct client_s *client = assuan_get_pointer(ctx);
2531 gchar **req = split_input_line(line, " ", 0);
2532 guchar md5file[16];
2534 log_write2("ARGS=\"%s\"", line);
2535 CACHE_LOCK(ctx);
2537 if (!req || !*req) {
2538 g_strfreev(req);
2539 cache_clear(client->md5file, 2);
2540 CACHE_UNLOCK;
2541 return send_error(ctx, 0);
2544 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, req[0], strlen(req[0]));
2545 g_strfreev(req);
2547 if (cache_clear(md5file, 1) == FALSE) {
2548 CACHE_UNLOCK;
2549 return send_error(ctx, EPWMD_CACHE_NOT_FOUND);
2552 CACHE_UNLOCK;
2553 return send_error(ctx, 0);
2556 static gpg_error_t cachetimeout_command(assuan_context_t ctx, gchar *line)
2558 gpg_error_t rc = command_startup(ctx);
2560 if (rc)
2561 return send_error(ctx, rc);
2563 guchar md5file[16];
2564 glong timeout;
2565 gchar **req = split_input_line(line, " ", 0);
2566 gchar *p;
2568 if (!req || !*req || !req[1]) {
2569 g_strfreev(req);
2570 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
2573 errno = 0;
2574 timeout = strtol(req[1], &p, 10);
2576 if (errno != 0 || *p != 0 || timeout < -1) {
2577 g_strfreev(req);
2578 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
2581 gcry_md_hash_buffer(GCRY_MD_MD5, md5file, req[0], strlen(req[0]));
2582 CACHE_LOCK(client->ctx);
2584 if (cache_set_timeout(md5file, timeout) == FALSE) {
2585 CACHE_UNLOCK;
2586 return send_error(ctx, EPWMD_CACHE_NOT_FOUND);
2589 CACHE_UNLOCK;
2590 return send_error(ctx, 0);
2593 static gpg_error_t dump_command(assuan_context_t ctx, gchar *line)
2595 xmlChar *xml;
2596 gint len;
2597 struct client_s *client = assuan_get_pointer(ctx);
2598 gpg_error_t rc;
2600 rc = command_startup(ctx);
2602 if (rc)
2603 return send_error(ctx, rc);
2605 if (disable_list_and_dump == TRUE)
2606 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
2608 xmlDocDumpFormatMemory(client->doc, &xml, &len, 1);
2610 if (!xml) {
2611 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2612 return send_syserror(ctx, ENOMEM);
2615 pth_cleanup_push(xmlFree, xml);
2616 rc = xfer_data(ctx, (gchar *)xml, len);
2617 pth_cleanup_pop(1);
2618 return send_error(ctx, rc);
2621 static gpg_error_t getconfig_command(assuan_context_t ctx, gchar *line)
2623 struct client_s *client = assuan_get_pointer(ctx);
2624 gpg_error_t rc = 0;
2625 gchar filename[255]={0}, param[747]={0};
2626 gchar *p, *tmp, *fp = client->filename, *paramp = line;
2628 rc = command_startup(ctx);
2630 if (rc)
2631 return send_error(ctx, rc);
2633 log_write2("ARGS=\"%s\"", line);
2635 if (strchr(line, ' ')) {
2636 sscanf(line, " %254[^ ] %746c", filename, param);
2637 paramp = param;
2638 fp = filename;
2641 if (fp && !valid_filename(fp))
2642 return send_error(ctx, EPWMD_INVALID_FILENAME);
2644 paramp = g_ascii_strdown(paramp, -1);
2646 if (!paramp) {
2647 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2648 return send_syserror(ctx, ENOMEM);
2651 if (fp && !g_ascii_strcasecmp(paramp, "iterations")) {
2652 if (!(client->opts & OPT_ITERATIONS) || fp != client->filename) {
2653 file_header_internal_t *fh = read_file_header(fp, FALSE, &rc);
2655 if (!fh && rc != GPG_ERR_ENOENT)
2656 return send_error(ctx, rc);
2658 if (!rc) {
2659 g_free(paramp);
2660 p = g_strdup_printf("%llu", fh->ver.fh2.iter);
2661 close_file_header(fh);
2663 if (!p) {
2664 log_write("%s(%i): %s", __FILE__, __LINE__,
2665 strerror(ENOMEM));
2666 return send_syserror(ctx, ENOMEM);
2669 goto done;
2673 else if (!g_ascii_strcasecmp(paramp, "enable_pinentry")) {
2674 #ifdef WITH_PINENTRY
2675 gboolean n;
2677 if (fp == client->filename && (client->opts & OPT_PINENTRY))
2678 n = client->pinentry->enable;
2679 else
2680 n = get_key_file_boolean(fp, "enable_pinentry");
2682 p = g_strdup_printf("%s", n ? "true" : "false");
2684 if (!p) {
2685 log_write("%s(%i): %s", __FILE__, __LINE__,
2686 strerror(ENOMEM));
2687 return send_syserror(ctx, ENOMEM);
2690 goto done;
2691 #else
2692 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
2693 #endif
2695 else if (!g_ascii_strcasecmp(paramp, "pinentry_timeout")) {
2696 #ifdef WITH_PINENTRY
2697 if (fp == client->filename && (client->opts & OPT_PINENTRY_TO))
2698 p = g_strdup_printf("%i", client->pinentry->timeout);
2699 else
2700 p = g_strdup_printf("%i",
2701 get_key_file_integer(fp, "pinentry_timeout"));
2703 if (!p) {
2704 log_write("%s(%i): %s", __FILE__, __LINE__,
2705 strerror(ENOMEM));
2706 return send_syserror(ctx, ENOMEM);
2709 goto done;
2710 #else
2711 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
2712 #endif
2715 p = get_key_file_string(fp ? fp : "global", paramp);
2716 g_free(paramp);
2718 if (!p)
2719 return send_error(ctx, GPG_ERR_NO_VALUE);
2721 tmp = expand_homedir(p);
2722 g_free(p);
2724 if (!tmp) {
2725 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2726 return send_syserror(ctx, ENOMEM);
2729 p = tmp;
2730 done:
2731 pth_cleanup_push(g_free, p);
2732 rc = xfer_data(ctx, p, strlen(p));
2733 pth_cleanup_pop(1);
2734 return send_error(ctx, rc);
2737 struct xpath_s {
2738 xmlXPathContextPtr xp;
2739 xmlXPathObjectPtr result;
2740 xmlBufferPtr buf;
2741 gchar **req;
2744 static void xpath_command_cleanup(void *arg)
2746 struct xpath_s *xpath = arg;
2748 req_cleanup(xpath->req);
2750 if (xpath->buf)
2751 xmlBufferFree(xpath->buf);
2753 if (xpath->result)
2754 xmlXPathFreeObject(xpath->result);
2756 if (xpath->xp)
2757 xmlXPathFreeContext(xpath->xp);
2760 static gpg_error_t xpath_command(assuan_context_t ctx, gchar *line)
2762 struct client_s *client = assuan_get_pointer(ctx);
2763 gpg_error_t rc;
2764 struct xpath_s xpath;
2765 rc = command_startup(ctx);
2767 if (rc)
2768 return send_error(ctx, rc);
2771 log_write2("ARGS=\"%s\"", line);
2773 if (disable_list_and_dump == TRUE)
2774 return send_error(ctx, GPG_ERR_NOT_IMPLEMENTED);
2776 if (!line || !*line)
2777 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
2779 memset(&xpath, 0, sizeof(struct xpath_s));
2781 if ((xpath.req = split_input_line(line, "\t", 2)) == NULL) {
2782 if (strv_printf(&xpath.req, "%s", line) == FALSE)
2783 return send_syserror(ctx, ENOMEM);
2786 xpath.xp = xmlXPathNewContext(client->doc);
2788 if (!xpath.xp) {
2789 xpath_command_cleanup(&xpath);
2790 return send_error(ctx, EPWMD_LIBXML_ERROR);
2793 xpath.result = xmlXPathEvalExpression((xmlChar *)xpath.req[0], xpath.xp);
2795 if (!xpath.result) {
2796 xpath_command_cleanup(&xpath);
2797 return send_error(ctx, EPWMD_LIBXML_ERROR);
2800 if (xmlXPathNodeSetIsEmpty(xpath.result->nodesetval)) {
2801 rc = EPWMD_EMPTY_ELEMENT;
2802 goto fail;
2805 rc = recurse_xpath_nodeset(client->doc, xpath.result->nodesetval,
2806 (xmlChar *)xpath.req[1], &xpath.buf);
2808 if (rc)
2809 goto fail;
2810 else if (!xpath.req[1] && !xmlBufferLength(xpath.buf)) {
2811 rc = EPWMD_EMPTY_ELEMENT;
2812 goto fail;
2814 else if (xpath.req[1])
2815 goto fail;
2817 pth_cleanup_push(xpath_command_cleanup, &xpath);
2818 rc = xfer_data(ctx, (gchar *)xmlBufferContent(xpath.buf),
2819 xmlBufferLength(xpath.buf));
2820 pth_cleanup_pop(0);
2822 fail:
2823 xpath_command_cleanup(&xpath);
2824 return send_error(ctx, rc);
2827 static gpg_error_t import_command_finalize(gpointer data,
2828 gpg_error_t assuan_rc, guchar *line, gsize len)
2830 struct client_s *client = assuan_get_pointer((assuan_context_t)data);
2831 gpg_error_t rc = file_modified(client);
2832 gchar **req, **path = NULL, **path_orig = NULL, *content;
2833 xmlDocPtr doc;
2834 xmlNodePtr n, root, copy;
2836 if (assuan_rc || rc) {
2837 if (line)
2838 xfree(line);
2839 return assuan_rc ? assuan_rc : rc;
2842 req = split_input_line((gchar *)line, " ", 2);
2843 xfree(line);
2845 if (!req || !*req)
2846 return EPWMD_COMMAND_SYNTAX;
2848 if ((path = split_input_line(req[0], "\t", 0)) == NULL) {
2849 if ((path = split_input_line(req[0], " ", 0)) == NULL)
2850 return EPWMD_COMMAND_SYNTAX;
2853 content = req[1];
2855 if (!content || !*content) {
2856 rc = EPWMD_COMMAND_SYNTAX;
2857 goto fail;
2860 doc = xmlReadDoc((xmlChar *)content, NULL, "UTF-8", XML_PARSE_NOBLANKS);
2862 if (!doc) {
2863 rc = EPWMD_LIBXML_ERROR;
2864 goto fail;
2867 root = xmlDocGetRootElement(doc);
2868 path_orig = g_strdupv(path);
2870 if (!path_orig) {
2871 xmlFreeDoc(doc);
2872 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
2873 rc = gpg_error_from_errno(ENOMEM);
2874 goto fail;
2877 if (strv_printf(&path, "%s", (gchar *)root->name) == FALSE) {
2878 g_strfreev(path_orig);
2879 xmlFreeDoc(doc);
2880 rc = gpg_error_from_errno(ENOMEM);
2881 goto fail;
2884 n = find_account(client->doc, &path, &rc, NULL, 0);
2886 if (rc && rc != EPWMD_ELEMENT_NOT_FOUND) {
2887 g_strfreev(path_orig);
2888 xmlFreeDoc(doc);
2889 goto fail;
2891 else if (!rc) {
2892 n = find_elements(client->doc, n->children, path+1, &rc, NULL, NULL, NULL, FALSE, 0, NULL);
2894 if (rc && rc != EPWMD_ELEMENT_NOT_FOUND) {
2895 g_strfreev(path_orig);
2896 xmlFreeDoc(doc);
2897 goto fail;
2899 else if (!rc) {
2900 xmlNodePtr parent = n->parent;
2902 xmlUnlinkNode(n);
2903 xmlFreeNode(n);
2904 n = parent;
2908 g_strfreev(path);
2909 path = path_orig;
2911 if (rc == EPWMD_ELEMENT_NOT_FOUND) {
2912 n = create_element_path(client, &path, &rc);
2914 if (rc) {
2915 xmlFreeDoc(doc);
2916 goto fail;
2920 copy = xmlCopyNode(root, 1);
2921 n = xmlAddChild(n, copy);
2922 xmlFreeDoc(doc);
2924 if (!n)
2925 rc = EPWMD_LIBXML_ERROR;
2927 fail:
2928 g_strfreev(path);
2929 g_strfreev(req);
2930 client->inquire_status = INQUIRE_DONE;
2931 return rc;
2934 static gpg_error_t import_command(assuan_context_t ctx, gchar *line)
2936 gpg_error_t rc;
2937 struct client_s *client = assuan_get_pointer(ctx);
2939 rc = command_startup(ctx);
2941 if (rc)
2942 return send_error(ctx, rc);
2944 rc = assuan_inquire_ext(ctx, "IMPORT", 0, import_command_finalize, ctx);
2946 if (rc)
2947 return send_error(ctx, rc);
2949 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
2950 client->inquire_status = INQUIRE_BUSY;
2951 return 0;
2954 static gpg_error_t lock_command(assuan_context_t ctx, gchar *line)
2956 gpg_error_t rc;
2957 struct client_s *client = assuan_get_pointer(ctx);
2959 rc = command_startup(ctx);
2961 if (rc)
2962 return send_error(ctx, rc);
2964 rc = lock_file_mutex(client);
2966 if (!rc)
2967 client->is_lock_cmd = TRUE;
2969 return send_error(ctx, rc);
2972 static gpg_error_t unlock_command(assuan_context_t ctx, gchar *line)
2974 struct client_s *client = assuan_get_pointer(ctx);
2976 gpg_error_t rc = command_startup(ctx);
2978 if (rc)
2979 return send_error(ctx, rc);
2981 unlock_file_mutex(client);
2982 return send_error(ctx, 0);
2985 static gpg_error_t getpid_command(assuan_context_t ctx, gchar *line)
2987 gpg_error_t rc;
2988 gchar buf[32];
2989 pid_t pid = getpid();
2991 rc = command_startup(ctx);
2993 if (rc)
2994 return send_error(ctx, rc);
2996 print_fmt(buf, sizeof(buf), "%i", pid);
2997 rc = xfer_data(ctx, buf, strlen(buf));
2998 return send_error(ctx, rc);
3001 static gpg_error_t version_command(assuan_context_t ctx, gchar *line)
3003 gpg_error_t rc;
3004 gchar buf[32];
3006 rc = command_startup(ctx);
3008 if (rc)
3009 return send_error(ctx, rc);
3011 print_fmt(buf, sizeof(buf), "0x%X", VERSION_HEX);
3012 rc = xfer_data(ctx, buf, strlen(buf));
3013 return send_error(ctx, rc);
3016 #ifdef WITH_PINENTRY
3017 static void set_option_value(gchar **opt, const gchar *value)
3019 if (opt)
3020 g_free(*opt);
3022 *opt = NULL;
3024 if (value)
3025 *opt = g_strdup(value);
3027 #endif
3029 static gint set_unset_common(assuan_context_t ctx, const gchar *name,
3030 const gchar *value)
3032 struct client_s *client = assuan_get_pointer(ctx);
3034 if (g_ascii_strcasecmp(name, (gchar *)"log_level") == 0) {
3035 glong l = 0;
3037 if (value) {
3038 l = strtol(value, NULL, 10);
3040 if (l < 0 || l > 2)
3041 return gpg_err_make(PWMD_ERR_SOURCE, GPG_ERR_INV_VALUE);
3044 log_write1("log_level=%li", l);
3045 MUTEX_LOCK(&rcfile_mutex);
3046 g_key_file_set_integer(keyfileh, "global", "log_level", (gint)l);
3047 MUTEX_UNLOCK(&rcfile_mutex);
3048 goto done;
3050 else if (g_ascii_strcasecmp(name, (gchar *)"cipher") == 0) {
3051 guint64 flags;
3052 const gchar *p = value;
3054 if (!client->filename)
3055 return EPWMD_NO_FILE;
3057 if (value) {
3058 flags = pwmd_cipher_str_to_cipher(value);
3060 if (!flags)
3061 return gpg_err_make(PWMD_ERR_SOURCE, GPG_ERR_INV_VALUE);
3063 else if (!value)
3064 p = get_key_file_string("global", "cipher");
3066 MUTEX_LOCK(&rcfile_mutex);
3067 g_key_file_set_string(keyfileh, client->filename, "cipher", p);
3068 MUTEX_UNLOCK(&rcfile_mutex);
3069 log_write1("cipher=%s", p);
3071 if (!value)
3072 g_free((gchar *)p);
3074 goto done;
3076 else if (g_ascii_strcasecmp(name, (gchar *)"iterations") == 0) {
3077 gdouble n;
3078 gchar *p = NULL;
3080 if (!client->filename)
3081 return EPWMD_NO_FILE;
3083 if (!value) {
3084 MUTEX_LOCK(&rcfile_mutex);
3085 g_key_file_set_double(keyfileh, client->filename, "iterations",
3086 get_key_file_double("global", "iterations"));
3087 MUTEX_UNLOCK(&rcfile_mutex);
3088 log_write1("iterations=%llu",
3089 get_key_file_double(client->filename, "iterations"));
3090 goto done;
3093 errno = 0;
3094 n = strtoll(value, &p, 10);
3096 if (errno || (p && *p) || n < 0ULL)
3097 return gpg_err_make(PWMD_ERR_SOURCE, GPG_ERR_INV_VALUE);
3099 MUTEX_LOCK(&rcfile_mutex);
3100 g_key_file_set_double(keyfileh,
3101 client->filename ? client->filename : "global", "iterations", n);
3102 MUTEX_UNLOCK(&rcfile_mutex);
3104 if (client->filename)
3105 client->opts |= OPT_ITERATIONS;
3107 log_write1("iterations=%llu", n);
3108 goto done;
3110 else if (g_ascii_strcasecmp(name, (gchar *)"NAME") == 0) {
3111 pth_attr_t attr = pth_attr_of(pth_self());
3112 gchar buf[41];
3114 if (!value) {
3115 pth_attr_destroy(attr);
3116 goto done;
3119 print_fmt(buf, sizeof(buf), "%s", value);
3120 pth_attr_set(attr, PTH_ATTR_NAME, buf);
3121 pth_attr_destroy(attr);
3122 log_write1("name=%s", buf);
3123 #ifdef WITH_PINENTRY
3124 if (client->pinentry->name)
3125 g_free(client->pinentry->name);
3127 client->pinentry->name = g_strdup(buf);
3129 if (!client->pinentry->name)
3130 return gpg_error_from_errno(ENOMEM);
3131 #endif
3133 goto done;
3135 #ifdef WITH_PINENTRY
3136 else if (g_ascii_strcasecmp(name, (gchar *)"lc_messages") == 0)
3137 set_option_value(&client->pinentry->lcmessages, value);
3138 else if (g_ascii_strcasecmp(name, (gchar *)"lc_ctype") == 0)
3139 set_option_value(&client->pinentry->lcctype, value);
3140 else if (g_ascii_strcasecmp(name, (gchar *)"ttyname") == 0)
3141 set_option_value(&client->pinentry->ttyname, value);
3142 else if (g_ascii_strcasecmp(name, (gchar *)"ttytype") == 0)
3143 set_option_value(&client->pinentry->ttytype, value);
3144 else if (g_ascii_strcasecmp(name, (gchar *)"display") == 0)
3145 set_option_value(&client->pinentry->display, value);
3146 else if (g_ascii_strcasecmp(name, (gchar *)"pinentry_path") == 0)
3147 set_option_value(&client->pinentry->path, value);
3148 else if (g_ascii_strcasecmp(name, (gchar *)"title") == 0)
3149 set_option_value(&client->pinentry->title, value);
3150 else if (g_ascii_strcasecmp(name, (gchar *)"prompt") == 0)
3151 set_option_value(&client->pinentry->prompt, value);
3152 else if (g_ascii_strcasecmp(name, (gchar *)"desc") == 0)
3153 set_option_value(&client->pinentry->desc, value);
3154 else if (g_ascii_strcasecmp(name, "pinentry_timeout") == 0) {
3155 gchar *p = NULL;
3156 gint n;
3158 if (!value) {
3159 client->pinentry->timeout =
3160 get_key_file_integer(client->filename, "pinentry_timeout");
3161 client->opts &= ~(OPT_PINENTRY_TO);
3162 log_write1("pinentry_timeout=%i",
3163 get_key_file_integer(client->filename, "pinentry_timeout"));
3164 goto done;
3167 n = strtol(value, &p, 10);
3169 if (*p || n < 0)
3170 return gpg_err_make(PWMD_ERR_SOURCE, GPG_ERR_INV_VALUE);
3172 client->pinentry->timeout = n;
3173 client->opts |= OPT_PINENTRY_TO;
3174 log_write1("pinentry_timeout=%i", n);
3175 goto done;
3177 else if (g_ascii_strcasecmp(name, "enable_pinentry") == 0) {
3178 gchar *p = NULL;
3179 gint n;
3181 if (!value) {
3182 client->pinentry->enable = -1;
3183 client->opts &= ~(OPT_PINENTRY);
3184 goto done;
3187 n = strtol(value, &p, 10);
3189 if (*p || n < 0 || n > 1)
3190 return gpg_err_make(PWMD_ERR_SOURCE, GPG_ERR_INV_VALUE);
3192 client->pinentry->enable = n == 0 ? FALSE : TRUE;
3193 client->opts |= OPT_PINENTRY;
3194 log_write1("enable_pinentry=%i", n);
3195 goto done;
3197 #endif
3198 else
3199 return gpg_err_make(PWMD_ERR_SOURCE, GPG_ERR_UNKNOWN_OPTION);
3201 log_write1("%s=%s", name, value ? value : "");
3203 done:
3204 return 0;
3207 static gpg_error_t unset_command(assuan_context_t ctx, gchar *line)
3209 gpg_error_t rc = command_startup(ctx);
3211 if (rc)
3212 return send_error(ctx, rc);
3214 log_write2("ARGS=\"%s\"", line);
3215 return send_error(ctx, set_unset_common(ctx, line, NULL));
3218 static gpg_error_t set_command(assuan_context_t ctx, gchar *line)
3220 gchar name[64] = {0}, value[256] = {0};
3222 gpg_error_t rc = command_startup(ctx);
3224 if (rc)
3225 return send_error(ctx, rc);
3227 log_write2("ARGS=\"%s\"", line);
3229 if (sscanf(line, " %63[_a-zA-Z] = %255c", name, value) != 2)
3230 return send_error(ctx, gpg_err_make(PWMD_ERR_SOURCE, GPG_ERR_SYNTAX));
3232 return send_error(ctx, set_unset_common(ctx, name, value));
3235 static gpg_error_t rename_command(assuan_context_t ctx, gchar *line)
3237 struct client_s *client = assuan_get_pointer(ctx);
3238 gpg_error_t rc;
3239 gchar **req, **src, *dst;
3240 xmlNodePtr n;
3242 rc = command_startup(ctx);
3244 if (rc)
3245 return send_error(ctx, rc);
3247 log_write2("ARGS=\"%s\"", line);
3248 req = split_input_line(line, " ", -1);
3250 if (!req || !req[0] || !req[1]) {
3251 g_strfreev(req);
3252 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
3255 dst = req[1];
3257 if (!valid_xml_element((xmlChar *)dst)) {
3258 g_strfreev(req);
3259 return EPWMD_INVALID_ELEMENT;
3262 if (strchr(req[0], '\t'))
3263 src = split_input_line(req[0], "\t", -1);
3264 else
3265 src = split_input_line(req[0], " ", -1);
3267 if (!src || !*src) {
3268 rc = EPWMD_COMMAND_SYNTAX;
3269 goto fail;
3272 n = find_account(client->doc, &src, &rc, NULL, 0);
3274 if (src[1] && n)
3275 n = find_elements(client->doc, n->children, src+1, &rc, NULL, NULL,
3276 NULL, FALSE, 0, NULL);
3278 if (!n)
3279 goto fail;
3281 if (src[1])
3282 xmlNodeSetName(n, (xmlChar *)dst);
3283 else
3284 rc = add_attribute(n, "name", dst);
3286 fail:
3287 g_strfreev(req);
3288 g_strfreev(src);
3289 return send_error(ctx, rc);
3292 static gpg_error_t copy_command(assuan_context_t ctx, gchar *line)
3294 struct client_s *client = assuan_get_pointer(ctx);
3295 gpg_error_t rc;
3296 gchar **req, **src = NULL, **dst = NULL;
3297 xmlNodePtr nsrc, ndst, new, n;
3299 rc = command_startup(ctx);
3301 if (rc)
3302 return send_error(ctx, rc);
3304 log_write2("ARGS=\"%s\"", line);
3305 req = split_input_line(line, " ", -1);
3307 if (!req || !req[0] || !req[1]) {
3308 g_strfreev(req);
3309 return send_error(ctx, EPWMD_COMMAND_SYNTAX);
3312 if (strchr(req[0], '\t'))
3313 src = split_input_line(req[0], "\t", -1);
3314 else
3315 src = split_input_line(req[0], " ", -1);
3317 if (!src || !*src) {
3318 rc = EPWMD_COMMAND_SYNTAX;
3319 goto fail;
3322 if (strchr(req[1], '\t'))
3323 dst = split_input_line(req[1], "\t", -1);
3324 else
3325 dst = split_input_line(req[1], " ", -1);
3327 if (!dst || !*dst) {
3328 rc = EPWMD_COMMAND_SYNTAX;
3329 goto fail;
3332 nsrc = find_account(client->doc, &src, &rc, NULL, 0);
3334 if (nsrc && src[1])
3335 nsrc = find_elements(client->doc, nsrc->children, src+1, &rc, NULL,
3336 NULL, NULL, FALSE, 0, NULL);
3338 if (!nsrc)
3339 goto fail;
3341 ndst = find_account(client->doc, &dst, &rc, NULL, 0);
3343 if (ndst && dst[1])
3344 ndst = find_elements(client->doc, ndst->children, dst+1, &rc, NULL,
3345 NULL, NULL, FALSE, 0, NULL);
3347 if (!ndst && rc != EPWMD_ELEMENT_NOT_FOUND)
3348 goto fail;
3350 new = xmlCopyNode(nsrc->children, 1);
3352 if (!new) {
3353 rc = GPG_ERR_ENOMEM;
3354 goto fail;
3357 if (!ndst) {
3358 ndst = create_element_path(client, &dst, &rc);
3360 if (!ndst)
3361 goto fail;
3364 n = ndst->children;
3365 xmlUnlinkNode(ndst->children);
3366 xmlFreeNodeList(n);
3367 xmlAddChild(ndst, new);
3369 fail:
3370 if (req)
3371 g_strfreev(req);
3373 if (src)
3374 g_strfreev(src);
3376 if (dst)
3377 g_strfreev(dst);
3379 return send_error(ctx, rc);
3382 static gpg_error_t bye_notify(assuan_context_t ctx, gchar *data)
3384 struct client_s *cl = assuan_get_pointer(ctx);
3386 /* This will let assuan_process_next() return. */
3387 fcntl(cl->thd->fd, F_SETFL, O_NONBLOCK);
3388 return 0;
3391 static gpg_error_t reset_notify(assuan_context_t ctx, gchar *data)
3393 struct client_s *cl = assuan_get_pointer(ctx);
3395 if (cl)
3396 cleanup_client(cl);
3398 return 0;
3402 * This is called after every Assuan command.
3404 void command_finalize(assuan_context_t ctx, gpg_error_t rc)
3406 struct client_s *client = assuan_get_pointer(ctx);
3408 if (!client->is_lock_cmd)
3409 unlock_file_mutex(client);
3411 log_write1(N_("command completed (rc=%u)"), client->last_rc);
3414 gpg_error_t register_commands(assuan_context_t ctx)
3416 static struct {
3417 const gchar *name;
3418 gpg_error_t (*handler)(assuan_context_t, gchar *line);
3419 } table[] = {
3420 { "OPEN", open_command },
3421 { "SAVE", save_command },
3422 { "LIST", list_command },
3423 { "REALPATH", realpath_command },
3424 { "STORE", store_command },
3425 { "DELETE", delete_command },
3426 { "GET", get_command },
3427 { "ATTR", attr_command },
3428 { "ISCACHED", iscached_command },
3429 { "CLEARCACHE", clearcache_command },
3430 { "CACHETIMEOUT", cachetimeout_command },
3431 { "GETCONFIG", getconfig_command },
3432 { "DUMP", dump_command },
3433 { "XPATH", xpath_command },
3434 { "IMPORT", import_command },
3435 { "LOCK", lock_command },
3436 { "UNLOCK", unlock_command },
3437 { "GETPID", getpid_command },
3438 { "VERSION", version_command },
3439 { "SET", set_command },
3440 { "UNSET", unset_command },
3441 { "RENAME", rename_command },
3442 { "COPY", copy_command },
3443 { "INPUT", NULL },
3444 { "OUTPUT", NULL },
3445 { NULL, NULL }
3447 gint i, rc;
3449 for (i=0; table[i].name; i++) {
3450 rc = assuan_register_command (ctx, table[i].name, table[i].handler, NULL);
3452 if (rc)
3453 return rc;
3456 rc = assuan_register_bye_notify(ctx, bye_notify);
3458 if (rc)
3459 return rc;
3461 rc = assuan_register_reset_notify(ctx, reset_notify);
3463 if (rc)
3464 return rc;
3466 return assuan_register_post_cmd_notify(ctx, command_finalize);
3469 gpg_error_t try_xml_decrypt(assuan_context_t ctx, guchar *key,
3470 struct client_crypto_s *crypto, gpointer *dst, goffset *dst_len)
3472 goffset insize, len;
3473 struct client_s *client = ctx ? assuan_get_pointer(ctx) : NULL;
3474 guint64 iter = 0ULL, n_iter = 0ULL, iter_progress = 0ULL;
3475 gint zrc = 0;
3476 gulong outsize = 0;
3477 gpg_error_t rc;
3478 gsize fh_size = crypto->fh->v1 ? sizeof(crypto->fh->ver.fh1) : sizeof(crypto->fh->ver.fh2);
3479 guint64 fh_iter = crypto->fh->v1 ? crypto->fh->ver.fh1.iter : crypto->fh->ver.fh2.iter;
3480 guint hashlen = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
3482 lseek(crypto->fh->fd, fh_size, SEEK_SET);
3483 insize = crypto->fh->st.st_size - fh_size;
3484 crypto->iv = gcry_malloc(crypto->blocksize);
3486 if (!crypto->iv) {
3487 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
3488 return gpg_error_from_errno(ENOMEM);
3491 if (crypto->fh->v1)
3492 memcpy(crypto->iv, crypto->fh->ver.fh1.iv, crypto->blocksize);
3493 else
3494 memcpy(crypto->iv, crypto->fh->ver.fh2.iv, crypto->blocksize);
3496 crypto->inbuf = gcry_malloc(insize);
3498 if (!crypto->inbuf) {
3499 log_write("%s(%i): %s", __FILE__, __LINE__, strerror(ENOMEM));
3500 return gpg_error_from_errno(ENOMEM);
3503 crypto->insize = insize;
3504 len = pth_read(crypto->fh->fd, crypto->inbuf, crypto->insize);
3506 if (len != crypto->insize)
3507 return GPG_ERR_INV_LENGTH;
3509 /* No encryption iterations. This is a plain (gzipped) file. */
3510 if ((crypto->fh->v1 && (long)fh_iter < 0L) ||
3511 (!crypto->fh->v1 && fh_iter <= 0ULL)) {
3513 * cache_file_count() needs both .used == TRUE and a valid key in
3514 * order for it to count as a used cache entry. Fixes CACHE status
3515 * messages.
3517 memset(key, '!', hashlen);
3518 goto decompress;
3521 if ((rc = gcry_cipher_setiv(crypto->gh, crypto->iv, crypto->blocksize))) {
3522 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
3523 return rc;
3526 if ((rc = gcry_cipher_setkey(crypto->gh, key, crypto->keysize))) {
3527 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
3528 return rc;
3531 iter_progress = (guint64)get_key_file_double(client && client->filename ?
3532 client->filename : "global", "iteration_progress");
3534 if (iter_progress > 0ULL && fh_iter >= iter_progress) {
3535 rc = send_status(ctx, STATUS_DECRYPT, "0 %llu", fh_iter);
3537 if (rc)
3538 return rc;
3541 rc = iterate_crypto_once(client, crypto, STATUS_DECRYPT);
3543 if (rc)
3544 return rc;
3546 crypto->tkey = gcry_malloc(hashlen);
3548 if (!crypto->tkey) {
3549 log_write("%s(%i): %s", __FUNCTION__, __LINE__, strerror(ENOMEM));
3550 return gpg_error_from_errno(ENOMEM);
3553 memcpy(crypto->tkey, key, hashlen);
3554 guchar *tkey = crypto->tkey;
3555 tkey[0] ^= 1;
3557 if ((rc = gcry_cipher_setkey(crypto->gh, crypto->tkey, crypto->keysize))) {
3558 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
3559 return rc;
3562 while (iter < (crypto->fh->v1 ? fh_iter : fh_iter-1)) {
3563 if (iter_progress > 0ULL && iter >= iter_progress) {
3564 if (!(iter % iter_progress)) {
3565 rc = send_status(ctx, STATUS_DECRYPT, "%llu %llu",
3566 ++n_iter * iter_progress, fh_iter);
3568 if (rc)
3569 return rc;
3573 if ((rc = gcry_cipher_setiv(crypto->gh, crypto->iv, crypto->blocksize))) {
3574 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
3575 return rc;
3578 rc = iterate_crypto_once(client, crypto, STATUS_DECRYPT);
3580 if (rc) {
3581 log_write("%s(%i): %s", __FUNCTION__, __LINE__, _gpg_strerror(rc));
3582 return rc;
3585 iter++;
3588 if (iter_progress && fh_iter >= iter_progress) {
3589 rc = send_status(ctx, STATUS_DECRYPT, "%llu %llu", fh_iter, fh_iter);
3591 if (rc)
3592 return rc;
3595 decompress:
3596 if (do_decompress(ctx, crypto->inbuf, crypto->insize,
3597 (gpointer *)&crypto->outbuf, &outsize, &zrc) == FALSE) {
3598 if (zrc == Z_MEM_ERROR)
3599 return gpg_error_from_errno(ENOMEM);
3600 else
3601 return EPWMD_BADKEY; // Not a valid gzip header. Must be a bad key.
3604 if (g_strncasecmp(crypto->outbuf, "<?xml ", 6) != 0) {
3605 gcry_free(crypto->outbuf);
3606 crypto->outbuf = NULL;
3607 return EPWMD_BADKEY;
3610 if (ctx) {
3611 client->xml = crypto->outbuf;
3612 client->len = outsize;
3613 crypto->outbuf = NULL;
3615 else if (dst) {
3616 *dst = crypto->outbuf;
3617 *dst_len = outsize;
3618 crypto->outbuf = NULL;
3621 /* The calling function should free the crypto struct. */
3622 return 0;