1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2010 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
24 #include <sys/types.h>
49 #include "pwmd_error.h"
62 static gpg_error_t
do_lock_command(struct client_s
*client
);
64 static void *z_alloc(void *data
, unsigned items
, unsigned size
)
66 return gcry_calloc(items
, size
);
69 static void z_free(void *data
, void *p
)
74 static gpg_error_t
file_modified(struct client_s
*client
)
79 if (client
->state
!= STATE_OPEN
)
82 rc
= lock_file_mutex(client
);
87 if (lstat(client
->filename
, &st
) == 0 && client
->mtime
) {
88 if (client
->mtime
!= st
.st_mtime
)
89 return EPWMD_FILE_MODIFIED
;
96 static gpg_error_t
parse_xml(assuan_context_t ctx
)
98 struct client_s
*client
= assuan_get_pointer(ctx
);
100 client
->doc
= xmlReadMemory(client
->xml
, client
->len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
103 return EPWMD_LIBXML_ERROR
;
105 if (!client
->crypto
->fh
|| client
->crypto
->fh
->ver
.fh2
.version
>= 0x212)
108 return convert_elements(client
->doc
);
111 void unlock_file_mutex(struct client_s
*client
)
116 if (client
->has_lock
== FALSE
|| client
->pinentry
->status
!= PINENTRY_NONE
)
118 if (client
->has_lock
== FALSE
)
122 CACHE_LOCK(client
->ctx
);
124 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
131 client
->has_lock
= client
->is_lock_cmd
= FALSE
;
134 gpg_error_t
lock_file_mutex(struct client_s
*client
)
139 if (client
->has_lock
== TRUE
)
142 CACHE_LOCK(client
->ctx
);
144 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
151 if (client
->rc_on_locked
) {
152 if (!pth_mutex_acquire(m
, TRUE
, NULL
))
153 return GPG_ERR_LOCKED
;
159 MUTEX_TRYLOCK(client
, m
, rc
);
162 client
->has_lock
= TRUE
;
167 void free_client(struct client_s
*client
)
170 xmlFreeDoc(client
->doc
);
173 gcry_free(client
->xml
);
175 if (client
->filename
)
176 g_free(client
->filename
);
179 cleanup_crypto(&client
->crypto
);
181 if (client
->xml_error
)
182 xmlResetError(client
->xml_error
);
185 void cleanup_client(struct client_s
*client
)
187 assuan_context_t ctx
= client
->ctx
;
188 struct client_thread_s
*thd
= client
->thd
;
189 gboolean rc_on_locked
= client
->rc_on_locked
;
190 gboolean lock_on_open
= client
->lock_on_open
;
192 struct pinentry_s
*pin
= client
->pinentry
;
195 unlock_file_mutex(client
);
196 CACHE_LOCK(client
->ctx
);
197 cache_decr_refcount(client
->md5file
);
200 * This may be a new file so don't use a cache slot. save_command() will
201 * set this to FALSE on success.
203 if (client
->new == TRUE
)
204 cache_clear(client
->md5file
, 1);
208 memset(client
, 0, sizeof(struct client_s
));
209 client
->state
= STATE_CONNECTED
;
212 client
->freed
= TRUE
;
214 client
->pinentry
= pin
;
216 client
->rc_on_locked
= rc_on_locked
;
217 client
->lock_on_open
= lock_on_open
;
220 static void gz_cleanup(void *arg
)
222 struct gz_s
**gz
= (struct gz_s
**)arg
;
227 if (!(*gz
)->done
&& (*gz
)->out
)
228 gcry_free((*gz
)->out
);
230 if ((*gz
)->which
== STATUS_COMPRESS
) {
232 deflateEnd(&(*gz
)->z
);
236 inflateEnd(&(*gz
)->z
);
243 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gulong insize
,
244 gpointer
*out
, gulong
*outsize
, gint
*rc
)
250 gz
= g_malloc0(sizeof(struct gz_s
));
253 *rc
= gpg_error_from_errno(ENOMEM
);
257 pth_cleanup_push(gz_cleanup
, &gz
);
258 gz
->which
= STATUS_DECOMPRESS
;
259 gz
->z
.zalloc
= z_alloc
;
260 gz
->z
.zfree
= z_free
;
262 gz
->z
.avail_in
= (uInt
)insize
;
263 gz
->z
.avail_out
= zlib_bufsize
;
264 gz
->z
.next_out
= gz
->out
= gcry_malloc(zlib_bufsize
);
267 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
273 *rc
= inflateInit2(&gz
->z
, 47);
276 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
281 memset(&h
, 0, sizeof(gz_header
));
282 h
.comment
= (guchar
*)buf
;
283 h
.comm_max
= sizeof(buf
);
284 *rc
= inflateGetHeader(&gz
->z
, &h
);
287 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
292 *rc
= inflate(&gz
->z
, Z_BLOCK
);
295 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
301 insize
= (gulong
)strtol((gchar
*)h
.comment
, NULL
, 10);
306 *rc
= inflate(&gz
->z
, Z_FINISH
);
312 if (!gz
->z
.avail_out
) {
313 p
= gcry_realloc(gz
->out
, gz
->z
.total_out
+ zlib_bufsize
);
316 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
322 gz
->z
.next_out
= (guchar
*)gz
->out
+ gz
->z
.total_out
;
323 gz
->z
.avail_out
= zlib_bufsize
;
324 *rc
= send_status(ctx
, STATUS_DECOMPRESS
, "%li %li",
325 gz
->z
.total_out
, insize
);
337 } while (*rc
!= Z_STREAM_END
);
339 *rc
= send_status(ctx
, STATUS_DECOMPRESS
, "%li %li", gz
->z
.total_out
,
346 *outsize
= gz
->z
.total_out
;
353 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
358 file_header_internal_t
*read_file_header(const gchar
*filename
, gboolean v1
,
363 file_header_internal_t
*fh
= g_malloc0(sizeof(file_header_internal_t
));
370 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
371 *rc
= gpg_error_from_errno(ENOMEM
);
375 pth_cleanup_push(g_free
, fh
);
376 fh_size
= v1
? sizeof(fh
->ver
.fh1
) : sizeof(fh
->ver
.fh2
);
378 if (lstat(filename
, &fh
->st
) == -1) {
379 *rc
= gpg_error_from_syserror();
384 if (!S_ISREG(fh
->st
.st_mode
)) {
385 *rc
= GPG_ERR_ENOANO
;
390 fd
= open(filename
, O_RDONLY
);
393 *rc
= gpg_error_from_errno(errno
);
398 pth_cleanup_push(cleanup_fd_cb
, &fd
);
399 p
= v1
? (void *)&fh
->ver
.fh1
: (void *)&fh
->ver
.fh2
;
400 len
= pth_read(fd
, p
, fh_size
);
402 if (len
!= fh_size
) {
406 *rc
= gpg_error_from_errno(n
);
417 static gpg_error_t
open_command_finalize(assuan_context_t ctx
, guchar
*unused
,
420 struct client_s
*client
= assuan_get_pointer(ctx
);
423 guchar
*key
= client
->crypto
->key
;
426 if (!client
->crypto
->fh
) {
433 rc
= init_client_crypto2(client
->filename
, client
->crypto
);
436 cleanup_client(client
);
437 return send_error(ctx
, rc
);
440 rc
= try_xml_decrypt(ctx
, client
->crypto
, NULL
, NULL
);
443 cleanup_client(client
);
444 return send_error(ctx
, rc
);
448 CACHE_LOCK(client
->ctx
);
450 if (cached
== FALSE
) {
451 if (cache_update_key(client
->md5file
, key
) == FALSE
) {
452 cleanup_client(client
);
454 return send_syserror(ctx
, ENOMEM
);
457 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
458 cache_reset_timeout(client
->md5file
, timeout
);
461 cache_set_timeout(client
->md5file
, -2);
469 gcry_free(client
->xml
);
474 if (client
->new == FALSE
)
475 send_status_all(STATUS_CACHE
);
477 client
->state
= STATE_OPEN
;
480 if (!rc
&& client
->new == FALSE
&&
481 client
->crypto
->fh
->ver
.fh2
.iter
!= (guint64
)get_key_file_double(client
->filename
, "iterations")) {
482 MUTEX_LOCK(&rcfile_mutex
);
483 g_key_file_set_double(keyfileh
, client
->filename
, "iterations",
484 client
->crypto
->fh
->ver
.fh2
.iter
);
485 MUTEX_UNLOCK(&rcfile_mutex
);
486 send_status_all(STATUS_CONFIG
);
489 cleanup_crypto(&client
->crypto
);
491 if (!rc
&& client
->lock_on_open
)
492 return do_lock_command(client
);
494 return send_error(ctx
, rc
);
497 static void req_cleanup(void *arg
)
502 g_strfreev((gchar
**)arg
);
505 static gpg_error_t
parse_open_opt_lock(gpointer data
, gpointer value
)
507 struct client_s
*client
= data
;
509 client
->lock_on_open
= TRUE
;
513 static gpg_error_t
parse_opt_pinentry(gpointer data
, gpointer value
)
516 struct client_s
*client
= data
;
522 client
->pinentry
->enable
= -1;
523 client
->opts
&= ~(OPT_PINENTRY
);
527 n
= strtol(str
, &p
, 10);
529 if (*p
|| n
< 0 || n
> 1)
530 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
532 client
->pinentry
->enable
= n
== 0 ? FALSE
: TRUE
;
533 client
->opts
|= OPT_PINENTRY
;
534 log_write1("enable_pinentry=%i", n
);
539 static gint
open_command(assuan_context_t ctx
, gchar
*line
)
541 gboolean cached
= FALSE
;
543 struct client_s
*client
= assuan_get_pointer(ctx
);
545 gchar
*filename
= NULL
;
546 gsize hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
547 struct argv_s
*args
[] = {
548 &(struct argv_s
) { "lock", OPT_NOARG
, parse_open_opt_lock
},
549 &(struct argv_s
) { "pinentry", OPT_OPTARG
, parse_opt_pinentry
},
553 if (client
->state
== STATE_OPEN
)
554 cleanup_client(client
);
556 client
->lock_on_open
= FALSE
;
557 rc
= parse_options(&line
, args
, client
);
560 return send_error(ctx
, rc
);
562 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
565 pth_cleanup_push(req_cleanup
, req
);
567 if (!filename
|| !*filename
) {
569 return send_error(ctx
, GPG_ERR_SYNTAX
);
572 log_write2("ARGS=\"%s\" %s", filename
, req
[1] ? "<passphrase>" : "");
574 if (valid_filename(filename
) == FALSE
) {
576 return send_error(ctx
, GPG_ERR_INV_VALUE
);
579 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
580 CACHE_LOCK(client
->ctx
);
582 if (cache_has_file(client
->md5file
) == FALSE
) {
583 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
586 return send_syserror(ctx
, ENOMEM
);
590 cache_incr_refcount(client
->md5file
);
592 rc
= lock_file_mutex(client
);
596 return send_error(ctx
, rc
);
599 client
->freed
= FALSE
;
600 client
->crypto
= init_client_crypto();
602 if (!client
->crypto
) {
604 cleanup_client(client
);
605 return send_syserror(ctx
, ENOMEM
);
608 client
->crypto
->key
= gcry_malloc(hashlen
);
610 if (!client
->crypto
->key
) {
612 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
,
613 gpg_error_from_errno(ENOMEM
));
614 cleanup_client(client
);
615 return send_syserror(ctx
, ENOMEM
);
618 memset(client
->crypto
->key
, 0, hashlen
);
619 client
->crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
621 if (!client
->crypto
->fh
) {
622 if (gpg_err_code_to_errno(rc
) != ENOENT
) {
623 log_write("%s: %s", filename
, pwmd_strerror(rc
));
625 cleanup_client(client
);
626 return send_error(ctx
, rc
);
630 * New files don't need a key.
632 if ((client
->xml
= new_document()) == NULL
) {
633 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
635 cleanup_client(client
);
636 return send_syserror(ctx
, ENOMEM
);
639 client
->len
= xmlStrlen(client
->xml
);
641 client
->filename
= g_strdup(filename
);
643 if (!client
->filename
) {
645 cleanup_client(client
);
646 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
647 return send_syserror(ctx
, ENOMEM
);
650 if (req
[1] && *req
[1])
651 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, req
[1],
656 client
->pinentry
->filename
= g_strdup(client
->filename
);
658 if (!client
->pinentry
->filename
) {
659 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
660 cleanup_client(client
);
661 return send_syserror(ctx
, ENOMEM
);
664 return open_command_finalize(ctx
, NULL
, cached
);
667 client
->mtime
= client
->crypto
->fh
->st
.st_mtime
;
669 client
->filename
= g_strdup(filename
);
671 if (!client
->filename
) {
672 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
674 cleanup_client(client
);
675 return send_syserror(ctx
, ENOMEM
);
679 if (client
->pinentry
->filename
)
680 g_free(client
->pinentry
->filename
);
682 client
->pinentry
->filename
= g_strdup(client
->filename
);
684 if (!client
->pinentry
->filename
) {
685 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
687 cleanup_client(client
);
688 return send_syserror(ctx
, ENOMEM
);
692 if (client
->crypto
->fh
->ver
.fh2
.iter
<= 0ULL)
695 CACHE_LOCK(client
->ctx
);
696 cached
= cache_get_key(client
->md5file
, client
->crypto
->key
);
699 if (cached
== FALSE
) {
700 gchar
*tmp
= get_key_file_string(filename
, "key_file");
705 cleanup_client(client
);
706 return send_error(ctx
, GPG_ERR_WRONG_KEY_USAGE
);
710 * No key specified and no matching filename found in the cache. Use
711 * pinentry to retrieve the key. Cannot return assuan_process_done()
712 * here otherwise the command will be interrupted. The event loop in
713 * client_thread() will poll the file descriptor waiting for it to
714 * become ready to read a pinentry_key_s which will contain the
715 * entered key or an error code. It will then call
716 * open_command_finalize() to to finish the command.
718 if (!req
[1] || !*req
[1]) {
720 gboolean b
= get_key_file_boolean(filename
, "enable_pinentry");
722 /* From set_pinentry_defaults(). */
723 if (client
->pinentry
->enable
== FALSE
||
724 (client
->pinentry
->enable
== -1 && b
== FALSE
)) {
725 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
730 rc
= lock_pin_mutex(client
);
733 unlock_pin_mutex(client
->pinentry
);
734 cleanup_client(client
);
735 return send_error(ctx
, rc
);
738 client
->pinentry
->which
= PINENTRY_OPEN
;
739 rc
= pinentry_fork(ctx
);
742 unlock_pin_mutex(client
->pinentry
);
743 cleanup_client(client
);
744 return send_error(ctx
, rc
);
747 // Called from pinentry iterate.
748 client
->pinentry
->cb
= open_command_finalize
;
749 client
->pinentry
->status
= PINENTRY_INIT
;
752 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
757 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, req
[1],
760 else if (req
&& req
[1] && *req
[1]) {
761 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, req
[1],
762 strlen(req
[1]) ? strlen(req
[1]) : 1);
767 return open_command_finalize(ctx
, NULL
, cached
);
770 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
771 guint size
, gpointer
*out
, gulong
*outsize
, gint
*rc
)
776 gint cmd
= Z_NO_FLUSH
;
778 gz
= g_malloc0(sizeof(struct gz_s
));
781 *rc
= gpg_error_from_errno(ENOMEM
);
785 pth_cleanup_push(gz_cleanup
, &gz
);
786 gz
->which
= STATUS_COMPRESS
;
787 gz
->z
.zalloc
= z_alloc
;
788 gz
->z
.zfree
= z_free
;
789 gz
->z
.next_in
= data
;
790 gz
->z
.avail_in
= size
< zlib_bufsize
? (uInt
)size
: (uInt
)zlib_bufsize
;
791 gz
->z
.avail_out
= (uInt
)zlib_bufsize
;
792 gz
->z
.next_out
= gz
->out
= gcry_malloc(zlib_bufsize
);
795 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
801 *rc
= deflateInit2(&gz
->z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
804 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
809 /* Rather than store the size of the uncompressed data in the file header,
810 * store it in the comment field of the gzip header. Don't give anyone too
811 * much information. Not sure why really, but it seems the right way. :)
813 memset(&h
, 0, sizeof(gz_header
));
814 g_snprintf(buf
, sizeof(buf
), "%u", size
);
815 h
.comment
= (guchar
*)buf
;
816 *rc
= deflateSetHeader(&gz
->z
, &h
);
819 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
827 *rc
= deflate(&gz
->z
, cmd
);
833 if (!gz
->z
.avail_out
) {
834 p
= gcry_realloc(gz
->out
, gz
->z
.total_out
+ zlib_bufsize
);
837 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
843 gz
->z
.next_out
= (guchar
*)gz
->out
+ gz
->z
.total_out
;
844 gz
->z
.avail_out
= zlib_bufsize
;
847 if (!gz
->z
.avail_in
&& gz
->z
.total_in
< size
) {
848 if (gz
->z
.total_in
+ zlib_bufsize
> size
)
849 gz
->z
.avail_in
= size
- gz
->z
.total_in
;
851 gz
->z
.avail_in
= zlib_bufsize
;
853 *rc
= send_status(ctx
, STATUS_COMPRESS
, "%li %u",
854 gz
->z
.total_in
, size
);
860 if (gz
->z
.total_in
>= size
)
869 } while (*rc
!= Z_STREAM_END
);
871 *rc
= send_status(ctx
, STATUS_COMPRESS
, "%li %u", gz
->z
.total_in
, size
);
877 *outsize
= gz
->z
.total_out
;
884 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
889 #define CRYPTO_BLOCKSIZE(c) (c->blocksize * 1024)
892 * Useful for a large amount of data. Rather than doing all of the data in one
893 * iteration do it in chunks. This lets the command be cancelable rather than
894 * waiting for it to complete.
896 static gpg_error_t
iterate_crypto_once(struct client_s
*client
,
897 struct crypto_s
*crypto
, status_msg_t which
)
900 goffset len
= CRYPTO_BLOCKSIZE(crypto
);
901 gpointer p
= gcry_malloc(len
);
906 return gpg_err_code_from_errno(ENOMEM
);
908 if (crypto
->insize
< CRYPTO_BLOCKSIZE(crypto
))
909 len
= crypto
->insize
;
911 pth_cleanup_push(gcry_free
, p
);
914 inbuf
= (guchar
*)crypto
->inbuf
+ total
;
917 if (len
+ total
> crypto
->insize
)
918 len
= crypto
->blocksize
;
920 if (which
== STATUS_ENCRYPT
)
921 rc
= gcry_cipher_encrypt(crypto
->gh
, p
, len
, inbuf
, len
);
923 rc
= gcry_cipher_decrypt(crypto
->gh
, p
, len
, inbuf
, len
);
928 tmp
= (guchar
*)crypto
->inbuf
+ total
;
929 memmove(tmp
, p
, len
);
932 if (total
>= crypto
->insize
)
943 /* The crypto struct must be setup for iterations and .key. */
944 gpg_error_t
do_xml_encrypt(struct client_s
*client
,
945 struct crypto_s
*crypto
, const gchar
*filename
)
947 goffset len
= crypto
->insize
;
951 guint64 iter_progress
= 0ULL, n_iter
= 0ULL, xiter
= 0ULL;
952 gchar tmp
[FILENAME_MAX
];
955 gsize hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
957 if (!crypto
->fh
->ver
.fh2
.iter
) {
959 * cache_file_count() needs both .used == TRUE and a valid key in
960 * order for it to count as a used cache entry. Fixes CACHE status
963 memset(crypto
->key
, '!', hashlen
);
968 * Resize the existing xml buffer to the block size required by gcrypt
969 * rather than duplicating it and wasting memory.
971 len
= (crypto
->insize
/ crypto
->blocksize
) * crypto
->blocksize
;
973 if (crypto
->insize
% crypto
->blocksize
)
974 len
+= crypto
->blocksize
;
976 inbuf
= gcry_realloc(crypto
->inbuf
, len
);
979 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
980 return gpg_error_from_errno(ENOMEM
);
983 crypto
->inbuf
= inbuf
;
984 crypto
->insize
= len
;
985 gcry_create_nonce(crypto
->fh
->ver
.fh2
.iv
, crypto
->blocksize
);
988 gcry_free(crypto
->tkey
);
990 crypto
->tkey
= gcry_malloc(hashlen
);
993 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
994 return gpg_error_from_errno(ENOMEM
);
997 memcpy(crypto
->tkey
, crypto
->key
, hashlen
);
998 guchar
*tkey
= crypto
->tkey
;
1001 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->tkey
, crypto
->keysize
))) {
1002 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
1006 iter_progress
= (guint64
)get_key_file_double(
1007 client
? client
->filename
: "global", "iteration_progress");
1009 if (iter_progress
&& crypto
->fh
->ver
.fh2
.iter
>= iter_progress
) {
1010 rc
= send_status(client
? client
->ctx
: NULL
, STATUS_ENCRYPT
,
1011 "0 %llu", crypto
->fh
->ver
.fh2
.iter
);
1017 while (xiter
< crypto
->fh
->ver
.fh2
.iter
-1) {
1018 if (iter_progress
> 0ULL && xiter
>= iter_progress
) {
1019 if (!(xiter
% iter_progress
)) {
1020 rc
= send_status(client
? client
->ctx
: NULL
, STATUS_ENCRYPT
,
1021 "%llu %llu", ++n_iter
* iter_progress
,
1022 crypto
->fh
->ver
.fh2
.iter
);
1029 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->fh
->ver
.fh2
.iv
,
1030 crypto
->blocksize
))) {
1031 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
1035 rc
= iterate_crypto_once(client
, crypto
, STATUS_ENCRYPT
);
1038 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
1045 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->fh
->ver
.fh2
.iv
,
1046 crypto
->blocksize
))) {
1047 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
1051 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->key
, crypto
->keysize
))) {
1052 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
1056 rc
= iterate_crypto_once(client
, crypto
, STATUS_ENCRYPT
);
1061 if (iter_progress
&& crypto
->fh
->ver
.fh2
.iter
>= iter_progress
) {
1062 rc
= send_status(client
? client
->ctx
: NULL
, STATUS_ENCRYPT
,
1063 "%llu %llu", crypto
->fh
->ver
.fh2
.iter
, crypto
->fh
->ver
.fh2
.iter
);
1073 if (!client
&& !g_ascii_strcasecmp(filename
, "-")) {
1074 crypto
->fh
->fd
= STDOUT_FILENO
;
1078 if (lstat(filename
, &st
) == 0) {
1079 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
1081 if (!(mode
& S_IWUSR
))
1082 return gpg_error_from_errno(EACCES
);
1084 else if (errno
!= ENOENT
)
1085 return gpg_error_from_errno(errno
);
1087 g_snprintf(tmp
, sizeof(tmp
), "%s.XXXXXX", filename
);
1088 crypto
->fh
->fd
= mkstemp(tmp
);
1090 if (crypto
->fh
->fd
== -1) {
1092 p
= strrchr(tmp
, '/');
1094 log_write("%s: %s", p
, strerror(rc
));
1095 return gpg_error_from_errno(rc
);
1098 pth_cleanup_push(cleanup_unlink_cb
, tmp
);
1102 * xml_import() or convert_file() from command line.
1104 crypto
->fh
->fd
= STDOUT_FILENO
;
1107 crypto
->fh
->ver
.fh2
.magic
[0] = '\177';
1108 crypto
->fh
->ver
.fh2
.magic
[1] = 'P';
1109 crypto
->fh
->ver
.fh2
.magic
[2] = 'W';
1110 crypto
->fh
->ver
.fh2
.magic
[3] = 'M';
1111 crypto
->fh
->ver
.fh2
.magic
[4] = 'D';
1112 crypto
->fh
->ver
.fh2
.version
= VERSION_HEX
;
1113 len
= pth_write(crypto
->fh
->fd
, &crypto
->fh
->ver
.fh2
, sizeof(crypto
->fh
->ver
.fh2
));
1115 if (len
!= sizeof(crypto
->fh
->ver
.fh2
)) {
1121 return gpg_error_from_errno(len
);
1124 len
= pth_write(crypto
->fh
->fd
, crypto
->inbuf
, crypto
->insize
);
1126 if (len
!= crypto
->insize
) {
1132 return gpg_error_from_errno(len
);
1135 if (fsync(crypto
->fh
->fd
) == -1) {
1141 return gpg_error_from_errno(len
);
1148 if (mode
&& get_key_file_boolean(filename
, "backup") == TRUE
) {
1149 gchar tmp2
[FILENAME_MAX
];
1151 g_snprintf(tmp2
, sizeof(tmp2
), "%s.backup", filename
);
1153 acl
= acl_get_file(filename
, ACL_TYPE_ACCESS
);
1156 log_write("ACL: %s: %s", filename
, strerror(errno
));
1159 if (rename(filename
, tmp2
) == -1) {
1166 return gpg_error_from_errno(len
);
1171 acl
= acl_get_file(".", ACL_TYPE_DEFAULT
);
1174 log_write("ACL: %s: %s", filename
, strerror(errno
));
1178 if (rename(tmp
, filename
) == -1) {
1185 return gpg_error_from_errno(len
);
1191 chmod(filename
, mode
);
1194 if (acl
&& acl_set_file(filename
, ACL_TYPE_ACCESS
, acl
))
1195 log_write("ACL: %s: %s", filename
, strerror(errno
));
1202 if (client
&& lstat(filename
, &st
) == 0)
1203 client
->mtime
= st
.st_mtime
;
1208 gpg_error_t
update_save_flags(const gchar
*filename
,
1209 struct crypto_s
*crypto
)
1216 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1219 return GPG_ERR_ENOMEM
;
1222 rc
= init_client_crypto2(filename
, crypto
);
1227 if (filename
&& !crypto
->fh
->v1
) {
1228 iter
= (guint64
)get_key_file_double(filename
, "iterations");
1229 crypto
->fh
->ver
.fh2
.iter
= iter
< 0L ? 0UL : iter
;
1235 static gpg_error_t
save_command_finalize(assuan_context_t ctx
, guchar
*key
,
1238 struct client_s
*client
= assuan_get_pointer(ctx
);
1248 if (client
->crypto
->key
&& client
->crypto
->key
!= key
)
1249 gcry_free(client
->crypto
->key
);
1251 client
->crypto
->key
= key
;
1252 rc
= update_element_mtime(xmlDocGetRootElement(client
->doc
));
1255 return send_error(ctx
, rc
);
1257 xmlDocDumpFormatMemory(client
->doc
, (xmlChar
**)&xmlbuf
, (gint
*)&len
, 0);
1258 pth_cleanup_push(xmlFree
, xmlbuf
);
1259 clevel
= get_key_file_integer(client
->filename
, "compression_level");
1264 if (do_compress(ctx
, clevel
, xmlbuf
, len
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
1266 cleanup_crypto(&client
->crypto
);
1268 if (zrc
== Z_MEM_ERROR
)
1269 return send_syserror(ctx
, ENOMEM
);
1271 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
1279 client
->crypto
->inbuf
= xmlbuf
;
1280 client
->crypto
->insize
= len
;
1281 rc
= update_save_flags(client
->filename
, client
->crypto
);
1284 cleanup_crypto(&client
->crypto
);
1285 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(rc
));
1286 return send_error(ctx
, rc
);
1289 rc
= do_xml_encrypt(client
, client
->crypto
, client
->filename
);
1292 cleanup_crypto(&client
->crypto
);
1293 return send_error(ctx
, rc
);
1296 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
1297 CACHE_LOCK(client
->ctx
);
1300 cache_reset_timeout(client
->md5file
, timeout
);
1303 if (client
->new == TRUE
)
1304 send_status_all(STATUS_CACHE
);
1306 client
->new = FALSE
;
1307 cleanup_crypto(&client
->crypto
);
1308 return send_error(ctx
, 0);
1311 if (cache_update_key(client
->md5file
, client
->crypto
->key
) == FALSE
) {
1313 cleanup_crypto(&client
->crypto
);
1314 return send_syserror(ctx
, ENOMEM
);
1317 client
->new = FALSE
;
1318 cache_reset_timeout(client
->md5file
, timeout
);
1320 send_status_all(STATUS_CACHE
);
1321 cleanup_crypto(&client
->crypto
);
1322 return send_error(ctx
, 0);
1325 static gpg_error_t
parse_save_opt_iterations(gpointer data
, gpointer v
)
1327 struct client_s
*client
= data
;
1332 if (!client
->filename
)
1333 return EPWMD_NO_FILE
;
1335 if (!value
|| !*value
)
1339 n
= strtoul(value
, &p
, 10);
1341 if (errno
|| (p
&& *p
) || n
== G_MAXULONG
)
1342 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
1344 MUTEX_LOCK(&rcfile_mutex
);
1345 g_key_file_set_double(keyfileh
,
1346 client
->filename
? client
->filename
: "global", "iterations", n
);
1347 MUTEX_UNLOCK(&rcfile_mutex
);
1349 if (client
->filename
)
1350 client
->opts
|= OPT_ITERATIONS
;
1352 log_write1("iterations=%lu", n
);
1356 static gpg_error_t
parse_save_opt_cipher(gpointer data
, gpointer value
)
1358 struct client_s
*client
= data
;
1359 const gchar
*p
= value
;
1362 if (!client
->filename
)
1363 return EPWMD_NO_FILE
;
1368 flags
= pwmd_cipher_str_to_cipher(p
);
1371 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
1373 MUTEX_LOCK(&rcfile_mutex
);
1374 g_key_file_set_string(keyfileh
, client
->filename
, "cipher", p
);
1375 MUTEX_UNLOCK(&rcfile_mutex
);
1376 log_write1("cipher=%s", p
);
1384 static gpg_error_t
parse_save_opt_force(gpointer data
, gpointer value
)
1386 struct client_s
*client
= data
;
1388 CACHE_LOCK(client
->ctx
);
1389 cache_clear(client
->md5file
, 1);
1394 static gint
save_command(assuan_context_t ctx
, gchar
*line
)
1396 gboolean cached
= FALSE
;
1398 struct client_s
*client
= assuan_get_pointer(ctx
);
1399 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1401 struct argv_s
*args
[] = {
1402 &(struct argv_s
) { "iterations", OPT_OPTARG
, parse_save_opt_iterations
},
1403 &(struct argv_s
) { "cipher", OPT_OPTARG
, parse_save_opt_cipher
},
1404 &(struct argv_s
) { "pinentry", OPT_OPTARG
, parse_opt_pinentry
},
1405 &(struct argv_s
) { "force", OPT_NOARG
, parse_save_opt_force
},
1409 rc
= parse_options(&line
, args
, client
);
1412 return send_error(ctx
, rc
);
1415 log_write2("ARGS=%s", "<passphrase>");
1417 if (lstat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1418 return send_syserror(ctx
, errno
);
1420 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
)) {
1421 log_write("%s: %s", client
->filename
, pwmd_strerror(GPG_ERR_ENOANO
));
1422 return send_error(ctx
, GPG_ERR_ENOANO
);
1426 cached
= cache_iscached(client
->md5file
);
1430 * If a cache entry doesn't exist for this file and the file has a
1431 * "key_file" or "key" parameter, then it's an error. The reason is that
1432 * cache expiration would be useless.
1434 if (cached
== FALSE
) {
1435 gchar
*tmp
= get_key_file_string(client
->filename
, "key_file");
1439 return send_error(ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1446 if (!client
->crypto
) {
1447 client
->crypto
= init_client_crypto();
1449 if (!client
->crypto
) {
1450 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1451 return send_syserror(ctx
, ENOMEM
);
1455 client
->crypto
->key
= gcry_malloc(hashlen
);
1457 if (!client
->crypto
->key
) {
1458 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1459 cleanup_crypto(&client
->crypto
);
1460 return send_syserror(ctx
, ENOMEM
);
1463 memset(client
->crypto
->key
, '!', hashlen
);
1465 if (get_key_file_double(client
->filename
, "iterations") <= 0L &&
1469 if (!line
|| !*line
) {
1470 client
->crypto
->tkey
= gcry_malloc(hashlen
);
1472 if (!client
->crypto
->tkey
) {
1473 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1474 cleanup_crypto(&client
->crypto
);
1475 return send_syserror(ctx
, ENOMEM
);
1478 memset(client
->crypto
->tkey
, '!', hashlen
);
1481 if (cache_get_key(client
->md5file
, client
->crypto
->key
) == FALSE
||
1482 !memcmp(client
->crypto
->key
, client
->crypto
->tkey
, hashlen
)) {
1485 #ifdef WITH_PINENTRY
1488 if (client
->pinentry
->enable
== FALSE
||
1489 get_key_file_boolean(client
->filename
, "enable_pinentry") == FALSE
) {
1490 /* Empty keys are allowed. */
1491 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
1495 lock_pin_mutex(client
);
1496 client
->pinentry
->which
= PINENTRY_SAVE
;
1497 rc
= pinentry_fork(ctx
);
1500 unlock_pin_mutex(client
->pinentry
);
1501 cleanup_crypto(&client
->crypto
);
1502 return send_error(ctx
, rc
);
1505 client
->pinentry
->cb
= save_command_finalize
;
1506 client
->pinentry
->status
= PINENTRY_INIT
;
1509 /* Empty keys are allowed. */
1510 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
1520 if (get_key_file_double(client
->filename
, "iterations") <= 0L) {
1521 guint64 iter
= (guint64
)get_key_file_double(NULL
, "iterations");
1526 MUTEX_LOCK(&rcfile_mutex
);
1527 g_key_file_set_double(keyfileh
, client
->filename
, "iterations", iter
);
1528 MUTEX_UNLOCK(&rcfile_mutex
);
1529 client
->opts
|= OPT_ITERATIONS
;
1530 gpg_error_t rc
= send_status(ctx
, STATUS_CONFIG
, NULL
);
1533 return send_error(ctx
, rc
);
1536 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, line
,
1541 return save_command_finalize(ctx
, client
->crypto
->key
, cached
);
1544 static gint
delete_command(assuan_context_t ctx
, gchar
*line
)
1546 struct client_s
*client
= assuan_get_pointer(ctx
);
1551 log_write2("ARGS=\"%s\"", line
);
1553 if (strchr(line
, '\t'))
1554 req
= split_input_line(line
, "\t", -1);
1556 req
= split_input_line(line
, " ", -1);
1559 return send_error(ctx
, GPG_ERR_SYNTAX
);
1561 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1565 return send_error(ctx
, rc
);
1569 * No sub-node defined. Remove the entire node (root element).
1573 rc
= unlink_node(n
);
1578 return send_error(ctx
, rc
);
1581 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
1585 return send_error(ctx
, rc
);
1588 rc
= unlink_node(n
);
1592 return send_error(ctx
, rc
);
1596 * Don't return with assuan_process_done() here. This has been called from
1597 * assuan_process_next() and the command should be finished in
1600 static gint
store_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
1603 assuan_context_t ctx
= data
;
1604 struct client_s
*client
= assuan_get_pointer(ctx
);
1607 gpg_error_t rc
= file_modified(client
);
1609 if (assuan_rc
|| rc
) {
1612 return assuan_rc
? assuan_rc
: rc
;
1615 req
= split_input_line((gchar
*)line
, "\t", 0);
1619 return GPG_ERR_SYNTAX
;
1622 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1624 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
1625 rc
= new_root_element(client
->doc
, *req
);
1642 n
= create_elements_cb(n
, req
+1, &rc
, NULL
);
1644 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1645 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
, FALSE
);
1649 client
->inquire_status
= INQUIRE_DONE
;
1652 rc
= update_element_mtime(n
);
1657 static gint
store_command(assuan_context_t ctx
, gchar
*line
)
1659 struct client_s
*client
= assuan_get_pointer(ctx
);
1662 rc
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1665 return send_error(ctx
, rc
);
1667 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1668 client
->inquire_status
= INQUIRE_BUSY
;
1672 static void *send_data_cb(void *arg
)
1674 struct assuan_cmd_s
*data
= arg
;
1678 pth_cancel_state(PTH_CANCEL_ENABLE
|PTH_CANCEL_ASYNCHRONOUS
, &old
);
1679 rc
= g_malloc(sizeof(gpg_error_t
));
1680 *rc
= assuan_send_data(data
->ctx
, data
->line
, data
->line_len
);
1681 pth_cancel_state(old
, NULL
);
1686 /* For every assuan command that needs to be sent to the client, a timeout is
1687 * needed to determine if the client lost the connection. The timeout is the
1688 * same as the "keepalive" configuration parameter or a default if unset.
1690 gpg_error_t
do_assuan_command(assuan_context_t ctx
,
1691 void *(*cb
)(void *data
), void *data
)
1693 pth_attr_t attr
= pth_attr_new();
1696 gint to
= get_key_file_integer("global", "keepalive");
1697 pth_event_t ev
, tev
;
1702 pth_attr_init(attr
);
1703 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, TRUE
);
1704 tid
= pth_spawn(attr
, cb
, data
);
1706 pth_attr_destroy(attr
);
1709 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1710 _gpg_strerror(gpg_error_from_errno(n
)));
1711 return gpg_error_from_errno(n
);
1714 pth_cleanup_push(cleanup_cancel_cb
, tid
);
1715 to
= to
<= 0 ? DEFAULT_KEEPALIVE_TO
: to
;
1716 ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, tid
);
1717 tev
= to
? pth_event(PTH_EVENT_TIME
, pth_timeout(to
, 0)) : NULL
;
1718 ev
= pth_event_concat(ev
, tev
, NULL
);
1719 pth_cleanup_push(cleanup_ev_cb
, ev
);
1724 st
= pth_event_status(tev
);
1726 if (st
== PTH_STATUS_OCCURRED
) {
1729 return GPG_ERR_TIMEOUT
;
1733 st
= pth_event_status(ev
);
1735 if (st
== PTH_STATUS_FAILED
) {
1739 rc
= GPG_ERR_ASS_WRITE_ERROR
;
1741 else if (st
== PTH_STATUS_OCCURRED
) {
1743 rc
= *(gpg_error_t
*)p
;
1752 static gpg_error_t
xfer_data(assuan_context_t ctx
, const gchar
*line
,
1758 struct assuan_cmd_s data
;
1759 gint progress
= get_key_file_integer("global", "xfer_progress");
1762 progress
= progress
>0 ? (progress
/ASSUAN_LINELENGTH
)*ASSUAN_LINELENGTH
: 0;
1763 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1765 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1772 if (sent
+ to_send
> total
)
1773 to_send
= total
- sent
;
1775 data
.line
= flush
? NULL
: (gchar
*)line
+sent
;
1776 data
.line_len
= flush
? 0 : to_send
;
1777 rc
= do_assuan_command(ctx
, send_data_cb
, &data
);
1780 sent
+= flush
? 0 : to_send
;
1782 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1783 (sent
== total
&& flush
))
1784 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1786 if (!flush
&& !rc
&& sent
== total
) {
1791 } while (!rc
&& sent
< total
);
1796 static gint
get_command(assuan_context_t ctx
, gchar
*line
)
1798 struct client_s
*client
= assuan_get_pointer(ctx
);
1803 log_write2("ARGS=\"%s\"", line
);
1804 req
= split_input_line(line
, "\t", -1);
1806 if (!req
|| !*req
) {
1808 return send_error(ctx
, GPG_ERR_SYNTAX
);
1811 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1815 return send_error(ctx
, rc
);
1819 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
1824 return send_error(ctx
, rc
);
1826 if (!n
|| !n
->children
)
1827 return send_error(ctx
, GPG_ERR_NO_VALUE
);
1829 n
= find_text_node(n
->children
);
1831 if (!n
|| !n
->content
|| !*n
->content
)
1832 return send_error(ctx
, GPG_ERR_NO_VALUE
);
1834 rc
= xfer_data(ctx
, (gchar
*)n
->content
, xmlStrlen(n
->content
));
1835 return send_error(ctx
, rc
);
1838 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**target
,
1839 gpg_error_t
*rc
, gchar
**req_orig
, void *data
)
1841 gchar
*path
= *(gchar
**)data
;
1842 gchar
*tmp
= NULL
, *result
;
1846 *(gchar
**)data
= NULL
;
1849 path
= g_strjoinv("\t", target
);
1852 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1853 *rc
= gpg_error_from_errno(ENOMEM
);
1858 tmp
= g_strjoinv("\t", req_orig
);
1862 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1863 *rc
= gpg_error_from_errno(ENOMEM
);
1869 result
= g_strdup_printf("%s\t%s", path
, tmp
);
1871 result
= g_strdup(path
);
1874 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1875 *rc
= gpg_error_from_errno(ENOMEM
);
1883 *(gchar
**)data
= result
;
1887 static void list_command_cleanup1(void *arg
);
1888 static gint
realpath_command(assuan_context_t ctx
, gchar
*line
)
1891 struct client_s
*client
= assuan_get_pointer(ctx
);
1899 log_write2("ARGS=\"%s\"", line
);
1901 if (strchr(line
, '\t') != NULL
) {
1902 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1903 return send_error(ctx
, GPG_ERR_SYNTAX
);
1906 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1907 return send_error(ctx
, GPG_ERR_SYNTAX
);
1910 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1914 return send_error(ctx
, rc
);
1917 rp
= g_strjoinv("\t", req
);
1921 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1922 return send_syserror(ctx
, ENOMEM
);
1926 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1927 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, &rp
, FALSE
);
1932 return send_error(ctx
, rc
);
1936 string
= g_string_new(rp
);
1941 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1942 return send_syserror(ctx
, ENOMEM
);
1946 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++) {
1947 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+1) && *(t
+1) != '!')) {
1948 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1953 pth_cleanup_push(list_command_cleanup1
, string
);
1954 rc
= xfer_data(ctx
, string
->str
, string
->len
);
1956 return send_error(ctx
, rc
);
1959 static void list_command_cleanup1(void *arg
)
1961 g_string_free((GString
*)arg
, TRUE
);
1964 static void list_command_cleanup2(void *arg
)
1966 struct element_list_s
*elements
= arg
;
1969 if (elements
->list
) {
1970 gint total
= g_slist_length(elements
->list
);
1973 for (i
= 0; i
< total
; i
++) {
1974 gchar
*tmp
= g_slist_nth_data(elements
->list
, i
);
1978 g_slist_free(elements
->list
);
1981 if (elements
->prefix
)
1982 g_free(elements
->prefix
);
1985 g_strfreev(elements
->req
);
1991 static gpg_error_t
parse_list_opt_norecurse(gpointer data
, gpointer value
)
1993 struct element_list_s
*elements
= data
;
1995 elements
->recurse
= FALSE
;
1999 static gpg_error_t
parse_list_opt_verbose(gpointer data
, gpointer value
)
2001 struct element_list_s
*elements
= data
;
2003 elements
->verbose
= TRUE
;
2007 static gint
list_command(assuan_context_t ctx
, gchar
*line
)
2009 struct client_s
*client
= assuan_get_pointer(ctx
);
2011 struct element_list_s
*elements
= NULL
;
2013 struct argv_s
*args
[] = {
2014 &(struct argv_s
) { "no-recurse", OPT_NOARG
, parse_list_opt_norecurse
},
2015 &(struct argv_s
) { "verbose", OPT_NOARG
, parse_list_opt_verbose
},
2019 if (disable_list_and_dump
== TRUE
)
2020 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2022 elements
= g_malloc0(sizeof(struct element_list_s
));
2025 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2026 return gpg_err_code_from_errno(ENOMEM
);
2029 elements
->recurse
= TRUE
; // default
2030 pth_cleanup_push(list_command_cleanup2
, elements
);
2031 rc
= parse_options(&line
, args
, elements
);
2039 rc
= list_root_elements(client
->doc
, &str
, elements
->verbose
);
2043 return send_error(ctx
, rc
);
2046 pth_cleanup_push(list_command_cleanup1
, str
);
2047 rc
= xfer_data(ctx
, str
->str
, str
->len
);
2050 return send_error(ctx
, rc
);
2053 elements
->req
= split_input_line(line
, " ", 0);
2056 strv_printf(&elements
->req
, "%s", line
);
2058 rc
= create_path_list(client
->doc
, elements
, *elements
->req
);
2064 gint total
= g_slist_length(elements
->list
);
2069 rc
= GPG_ERR_NO_VALUE
;
2073 str
= g_string_new(NULL
);
2076 rc
= gpg_err_code_from_errno(ENOMEM
);
2077 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2081 for (i
= 0; i
< total
; i
++) {
2082 tmp
= g_slist_nth_data(elements
->list
, i
);
2083 g_string_append_printf(str
, "%s%s", tmp
, i
+1 == total
? "" : "\n");
2086 pth_cleanup_push(list_command_cleanup1
, str
);
2087 rc
= xfer_data(ctx
, str
->str
, str
->len
);
2091 rc
= GPG_ERR_NO_VALUE
;
2095 return send_error(ctx
, rc
);
2099 * req[0] - element path
2101 static gpg_error_t
attribute_list(assuan_context_t ctx
, gchar
**req
)
2103 struct client_s
*client
= assuan_get_pointer(ctx
);
2104 gchar
**attrlist
= NULL
;
2106 gchar
**path
= NULL
;
2112 if (!req
|| !req
[0])
2113 return GPG_ERR_SYNTAX
;
2115 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2117 * The first argument may be only a root element.
2119 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
2120 return GPG_ERR_SYNTAX
;
2123 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2131 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2132 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2142 for (a
= n
->properties
; a
; a
= a
->next
) {
2145 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
2147 g_strfreev(attrlist
);
2149 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2150 return gpg_error_from_errno(ENOMEM
);
2155 attrlist
[i
] = g_strdup_printf("%s %s", (gchar
*)a
->name
,
2156 an
&& an
->content
? (gchar
*)an
->content
: "");
2159 g_strfreev(attrlist
);
2160 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2161 return gpg_error_from_errno(ENOMEM
);
2164 attrlist
[++i
] = NULL
;
2168 return GPG_ERR_NO_VALUE
;
2170 line
= g_strjoinv("\n", attrlist
);
2173 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2174 g_strfreev(attrlist
);
2175 return gpg_error_from_errno(ENOMEM
);
2178 pth_cleanup_push(g_free
, line
);
2179 pth_cleanup_push(req_cleanup
, attrlist
);
2180 rc
= xfer_data(ctx
, line
, strlen(line
));
2187 * req[0] - attribute
2188 * req[1] - element path
2190 static gpg_error_t
attribute_delete(struct client_s
*client
, gchar
**req
)
2193 gchar
**path
= NULL
;
2196 if (!req
|| !req
[0] || !req
[1])
2197 return GPG_ERR_SYNTAX
;
2199 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2201 * The first argument may be only a root element.
2203 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2204 return GPG_ERR_SYNTAX
;
2208 * Don't remove the "_name" attribute for the root element. To remove an
2209 * root element use DELETE <name>.
2211 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"_name")) {
2212 rc
= GPG_ERR_SYNTAX
;
2216 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2222 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2223 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2229 rc
= delete_attribute(n
, (xmlChar
*)req
[0]);
2236 static xmlNodePtr
create_element_path(struct client_s
*client
, gchar
***path
,
2239 gchar
**src
= *path
;
2240 gchar
**src_orig
= g_strdupv(src
);
2241 xmlNodePtr n
= NULL
;
2246 *rc
= gpg_error_from_errno(ENOMEM
);
2247 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2252 n
= find_root_element(client
->doc
, &src
, rc
, NULL
, 0, FALSE
);
2255 if (*rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
2256 *rc
= new_root_element(client
->doc
, src
[0]);
2269 n
= create_target_elements_cb(n
, src
+1, rc
, NULL
);
2271 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
2272 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
, FALSE
);
2278 * Reset the position of the element tree now that the elements
2279 * have been created.
2284 n
= find_root_element(client
->doc
, &src
, rc
, NULL
, 0, FALSE
);
2289 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
2290 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2298 g_strfreev(src_orig
);
2305 * Creates a "target" attribute. When other commands encounter an element with
2306 * this attribute, the element path is modified to the target value. If the
2307 * source element path doesn't exist when using 'ATTR SET target', it is
2308 * created, but the destination element path must exist.
2310 * req[0] - source element path
2311 * req[1] - destination element path
2313 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
2315 gchar
**src
, **dst
, *line
= NULL
, **odst
= NULL
;
2319 if (!req
|| !req
[0] || !req
[1])
2320 return GPG_ERR_SYNTAX
;
2322 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2324 * The first argument may be only a root element.
2326 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
2327 return GPG_ERR_SYNTAX
;
2330 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2332 * The first argument may be only a root element.
2334 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
2335 rc
= GPG_ERR_SYNTAX
;
2340 odst
= g_strdupv(dst
);
2343 rc
= gpg_error_from_errno(ENOMEM
);
2347 n
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, FALSE
);
2350 * Make sure the destination element path exists.
2356 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
2357 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2363 n
= create_element_path(client
, &src
, &rc
);
2368 line
= g_strjoinv("\t", odst
);
2371 rc
= gpg_error_from_errno(ENOMEM
);
2372 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2376 rc
= add_attribute(n
, "target", line
);
2390 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
2396 tmp
= g_strdupv(req
);
2399 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2400 return gpg_error_from_errno(ENOMEM
);
2403 n
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
2409 if (g_utf8_collate(req
[0], req
[1]) == 0)
2413 * Will not overwrite an existing root.
2415 tmp
= g_strdupv(req
+1);
2418 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2419 return gpg_error_from_errno(ENOMEM
);
2422 n
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
2425 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2429 return GPG_ERR_AMBIGUOUS_NAME
;
2432 * Whitespace not allowed in root names.
2434 if (contains_whitespace(req
[1]) == TRUE
)
2435 return GPG_ERR_SYNTAX
;
2437 tmp
= g_strdupv(req
);
2440 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2441 return gpg_error_from_errno(ENOMEM
);
2444 n
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
2448 return GPG_ERR_ELEMENT_NOT_FOUND
;
2450 return add_attribute(n
, "_name", req
[1]);
2454 * req[0] - attribute
2455 * req[1] - element path
2457 static gpg_error_t
attribute_get(assuan_context_t ctx
, gchar
**req
)
2459 struct client_s
*client
= assuan_get_pointer(ctx
);
2465 if (!req
|| !req
[0] || !req
[1])
2466 return GPG_ERR_SYNTAX
;
2468 if (strchr(req
[1], '\t')) {
2469 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2470 return GPG_ERR_SYNTAX
;
2473 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2474 return GPG_ERR_SYNTAX
;
2477 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2483 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2484 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2492 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2493 return GPG_ERR_NOT_FOUND
;
2495 pth_cleanup_push(xmlFree
, a
);
2498 rc
= xfer_data(ctx
, (gchar
*)a
, xmlStrlen(a
));
2500 rc
= GPG_ERR_NO_VALUE
;
2511 * req[0] - attribute
2512 * req[1] - element path
2515 static gpg_error_t
attribute_set(struct client_s
*client
, gchar
**req
)
2517 gchar
**path
= NULL
;
2521 if (!req
|| !req
[0] || !req
[1])
2522 return GPG_ERR_SYNTAX
;
2525 * Reserved attribute names.
2527 if (!strcmp(req
[0], "_name")) {
2529 * Only reserved for the root element. Not the rest of the
2532 if (strchr(req
[1], '\t') == NULL
)
2533 return name_attribute(client
, req
+ 1);
2535 else if (!strcmp(req
[0], "target"))
2536 return target_attribute(client
, req
+ 1);
2538 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2540 * The first argument may be only a root element.
2542 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2543 return GPG_ERR_SYNTAX
;
2546 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2552 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2553 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2559 rc
= add_attribute(n
, req
[0], req
[2]);
2568 * req[1] - attribute name or element path if command is LIST
2569 * req[2] - element path
2570 * req[2] - element path or value
2572 static gint
attr_command(assuan_context_t ctx
, gchar
*line
)
2574 struct client_s
*client
= assuan_get_pointer(ctx
);
2578 log_write2("ARGS=\"%s\"", line
);
2579 req
= split_input_line(line
, " ", 4);
2581 if (!req
|| !req
[0] || !req
[1]) {
2583 return send_error(ctx
, GPG_ERR_SYNTAX
);
2586 pth_cleanup_push(req_cleanup
, req
);
2588 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2589 rc
= attribute_set(client
, req
+1);
2590 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2591 rc
= attribute_get(ctx
, req
+1);
2592 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2593 rc
= attribute_delete(client
, req
+1);
2594 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2595 rc
= attribute_list(ctx
, req
+1);
2597 rc
= GPG_ERR_SYNTAX
;
2600 return send_error(ctx
, rc
);
2603 static gint
iscached_command(assuan_context_t ctx
, gchar
*line
)
2605 gchar
**req
= split_input_line(line
, " ", 0);
2609 if (!req
|| !*req
) {
2611 return send_error(ctx
, GPG_ERR_SYNTAX
);
2614 log_write2("ARGS=\"%s\"", line
);
2616 if (!valid_filename(req
[0])) {
2618 return GPG_ERR_INV_VALUE
;
2621 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2624 if (cache_iscached(md5file
)) {
2627 return send_error(ctx
, 0);
2631 tmp
= get_key_file_string("global", "data_directory");
2635 return gpg_error_from_errno(ENOMEM
);
2638 path
= expand_homedir(tmp
);
2643 return gpg_error_from_errno(ENOMEM
);
2648 path
= g_strdup_printf("%s/%s", tmp
, req
[0]);
2653 return gpg_error_from_errno(ENOMEM
);
2656 if (access(path
, R_OK
) == -1) {
2657 gpg_error_t rc
= gpg_error_from_syserror();
2661 return send_error(ctx
, rc
);
2665 return send_error(ctx
, GPG_ERR_NOT_FOUND
);
2668 static gint
clearcache_command(assuan_context_t ctx
, gchar
*line
)
2670 struct client_s
*client
= assuan_get_pointer(ctx
);
2671 gchar
**req
= split_input_line(line
, " ", 0);
2674 log_write2("ARGS=\"%s\"", line
);
2677 if (!req
|| !*req
) {
2679 cache_clear(client
->md5file
, 2);
2681 return send_error(ctx
, 0);
2684 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2687 if (cache_clear(md5file
, 1) == FALSE
) {
2689 return send_error(ctx
, GPG_ERR_NOT_FOUND
);
2693 return send_error(ctx
, 0);
2696 static gint
cachetimeout_command(assuan_context_t ctx
, gchar
*line
)
2700 gchar
**req
= split_input_line(line
, " ", 0);
2703 if (!req
|| !*req
|| !req
[1]) {
2705 return send_error(ctx
, GPG_ERR_SYNTAX
);
2709 timeout
= strtol(req
[1], &p
, 10);
2711 if (errno
!= 0 || *p
!= 0 || timeout
< -1) {
2713 return send_error(ctx
, GPG_ERR_SYNTAX
);
2716 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2717 CACHE_LOCK(client
->ctx
);
2719 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2721 return send_error(ctx
, GPG_ERR_NOT_FOUND
);
2725 return send_error(ctx
, 0);
2728 static gint
dump_command(assuan_context_t ctx
, gchar
*line
)
2732 struct client_s
*client
= assuan_get_pointer(ctx
);
2735 if (disable_list_and_dump
== TRUE
)
2736 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2738 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2741 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2742 return send_syserror(ctx
, ENOMEM
);
2745 pth_cleanup_push(xmlFree
, xml
);
2746 rc
= xfer_data(ctx
, (gchar
*)xml
, len
);
2748 return send_error(ctx
, rc
);
2751 static gint
getconfig_command(assuan_context_t ctx
, gchar
*line
)
2753 struct client_s
*client
= assuan_get_pointer(ctx
);
2755 gchar filename
[255]={0}, param
[747]={0};
2756 gchar
*p
, *tmp
, *fp
= client
->filename
, *paramp
= line
;
2758 log_write2("ARGS=\"%s\"", line
);
2760 if (strchr(line
, ' ')) {
2761 sscanf(line
, " %254[^ ] %746c", filename
, param
);
2766 if (fp
&& !valid_filename(fp
))
2767 return send_error(ctx
, GPG_ERR_INV_VALUE
);
2769 paramp
= g_ascii_strdown(paramp
, -1);
2772 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2773 return send_syserror(ctx
, ENOMEM
);
2776 if (fp
&& !g_ascii_strcasecmp(paramp
, "iterations")) {
2777 if (!(client
->opts
& OPT_ITERATIONS
) || fp
!= client
->filename
) {
2778 file_header_internal_t
*fh
= read_file_header(fp
, FALSE
, &rc
);
2780 if (!fh
&& rc
!= GPG_ERR_ENOENT
)
2781 return send_error(ctx
, rc
);
2785 p
= g_strdup_printf("%lu", (unsigned long)fh
->ver
.fh2
.iter
);
2786 close_file_header(fh
);
2789 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2791 return send_syserror(ctx
, ENOMEM
);
2798 else if (!g_ascii_strcasecmp(paramp
, "enable_pinentry")) {
2799 #ifdef WITH_PINENTRY
2802 if (fp
== client
->filename
&& (client
->opts
& OPT_PINENTRY
))
2803 n
= client
->pinentry
->enable
;
2805 n
= get_key_file_boolean(fp
, "enable_pinentry");
2807 p
= g_strdup_printf("%s", n
? "true" : "false");
2810 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2812 return send_syserror(ctx
, ENOMEM
);
2817 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2820 else if (!g_ascii_strcasecmp(paramp
, "pinentry_timeout")) {
2821 #ifdef WITH_PINENTRY
2822 p
= g_strdup_printf("%i", get_key_file_integer(fp
, "pinentry_timeout"));
2825 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2827 return send_syserror(ctx
, ENOMEM
);
2832 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2836 p
= get_key_file_string(fp
? fp
: "global", paramp
);
2840 return send_error(ctx
, GPG_ERR_NO_VALUE
);
2842 tmp
= expand_homedir(p
);
2846 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2847 return send_syserror(ctx
, ENOMEM
);
2852 pth_cleanup_push(g_free
, p
);
2853 rc
= xfer_data(ctx
, p
, strlen(p
));
2855 return send_error(ctx
, rc
);
2859 xmlXPathContextPtr xp
;
2860 xmlXPathObjectPtr result
;
2865 static void xpath_command_cleanup(void *arg
)
2867 struct xpath_s
*xpath
= arg
;
2869 req_cleanup(xpath
->req
);
2872 xmlBufferFree(xpath
->buf
);
2875 xmlXPathFreeObject(xpath
->result
);
2878 xmlXPathFreeContext(xpath
->xp
);
2881 static gint
xpath_command(assuan_context_t ctx
, gchar
*line
)
2883 struct client_s
*client
= assuan_get_pointer(ctx
);
2885 struct xpath_s xpath
;
2887 log_write2("ARGS=\"%s\"", line
);
2889 if (disable_list_and_dump
== TRUE
)
2890 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2892 if (!line
|| !*line
)
2893 return send_error(ctx
, GPG_ERR_SYNTAX
);
2895 memset(&xpath
, 0, sizeof(struct xpath_s
));
2897 if ((xpath
.req
= split_input_line(line
, "\t", 2)) == NULL
) {
2898 if (strv_printf(&xpath
.req
, "%s", line
) == FALSE
)
2899 return send_syserror(ctx
, ENOMEM
);
2902 xpath
.xp
= xmlXPathNewContext(client
->doc
);
2905 xpath_command_cleanup(&xpath
);
2906 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2909 xpath
.result
= xmlXPathEvalExpression((xmlChar
*)xpath
.req
[0], xpath
.xp
);
2911 if (!xpath
.result
) {
2912 xpath_command_cleanup(&xpath
);
2913 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2916 if (xmlXPathNodeSetIsEmpty(xpath
.result
->nodesetval
)) {
2917 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2921 rc
= recurse_xpath_nodeset(client
->doc
, xpath
.result
->nodesetval
,
2922 (xmlChar
*)xpath
.req
[1], &xpath
.buf
, 0, NULL
);
2926 else if (!xpath
.req
[1] && !xmlBufferLength(xpath
.buf
)) {
2927 rc
= GPG_ERR_NO_VALUE
;
2930 else if (xpath
.req
[1])
2933 pth_cleanup_push(xpath_command_cleanup
, &xpath
);
2934 rc
= xfer_data(ctx
, (gchar
*)xmlBufferContent(xpath
.buf
),
2935 xmlBufferLength(xpath
.buf
));
2939 xpath_command_cleanup(&xpath
);
2940 return send_error(ctx
, rc
);
2943 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2944 static gint
xpathattr_command(assuan_context_t ctx
, gchar
*line
)
2946 struct client_s
*client
= assuan_get_pointer(ctx
);
2948 struct xpath_s xpath
;
2950 gboolean cmd
= FALSE
; //SET
2952 log_write2("ARGS=\"%s\"", line
);
2954 if (disable_list_and_dump
== TRUE
)
2955 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2957 if (!line
|| !*line
)
2958 return send_error(ctx
, GPG_ERR_SYNTAX
);
2960 memset(&xpath
, 0, sizeof(struct xpath_s
));
2962 if ((req
= split_input_line(line
, " ", 3)) == NULL
)
2963 return send_syserror(ctx
, ENOMEM
);
2966 rc
= GPG_ERR_SYNTAX
;
2970 if (!g_ascii_strcasecmp(req
[0], "SET"))
2972 else if (!g_ascii_strcasecmp(req
[0], "DELETE"))
2975 rc
= GPG_ERR_SYNTAX
;
2979 if (!req
[1] || !req
[2]) {
2980 rc
= GPG_ERR_SYNTAX
;
2984 if ((xpath
.req
= split_input_line(req
[2], "\t", 3)) == NULL
) {
2985 rc
= gpg_err_code_from_errno(ENOMEM
);
2989 if (!xpath
.req
[0] || (!xpath
.req
[1] && !cmd
) || (xpath
.req
[1] && cmd
)) {
2990 rc
= GPG_ERR_SYNTAX
;
2994 xpath
.xp
= xmlXPathNewContext(client
->doc
);
2997 rc
= EPWMD_LIBXML_ERROR
;
3001 xpath
.result
= xmlXPathEvalExpression((xmlChar
*)xpath
.req
[0], xpath
.xp
);
3003 if (!xpath
.result
) {
3004 rc
= EPWMD_LIBXML_ERROR
;
3008 if (xmlXPathNodeSetIsEmpty(xpath
.result
->nodesetval
)) {
3009 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
3013 rc
= recurse_xpath_nodeset(client
->doc
, xpath
.result
->nodesetval
,
3014 (xmlChar
*)xpath
.req
[1], &xpath
.buf
, cmd
, (xmlChar
*)req
[1]);
3018 xpath_command_cleanup(&xpath
);
3019 return send_error(ctx
, rc
);
3022 static gint
import_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
3025 struct client_s
*client
= assuan_get_pointer((assuan_context_t
)data
);
3026 gpg_error_t rc
= file_modified(client
);
3027 gchar
**req
, **path
= NULL
, **path_orig
= NULL
, *content
;
3028 xmlDocPtr doc
= NULL
;
3029 xmlNodePtr n
, root
, copy
;
3031 if (assuan_rc
|| rc
) {
3034 return assuan_rc
? assuan_rc
: rc
;
3037 req
= split_input_line((gchar
*)line
, "\t", 2);
3041 return GPG_ERR_SYNTAX
;
3044 path
= split_input_line(req
[1], "\t", 0);
3046 if (!content
|| !*content
) {
3047 rc
= GPG_ERR_SYNTAX
;
3051 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
3054 rc
= EPWMD_LIBXML_ERROR
;
3058 root
= xmlDocGetRootElement(doc
);
3059 rc
= validate_import(root
);
3065 path_orig
= g_strdupv(path
);
3068 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3069 rc
= gpg_error_from_errno(ENOMEM
);
3073 xmlChar
*a
= xmlGetProp(root
, (xmlChar
*)"_name");
3076 g_strfreev(path_orig
);
3077 rc
= gpg_error_from_errno(ENOMEM
);
3081 if (strv_printf(&path
, "%s", (gchar
*)a
) == FALSE
) {
3083 g_strfreev(path_orig
);
3084 rc
= gpg_error_from_errno(ENOMEM
);
3089 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
3091 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3092 g_strfreev(path_orig
);
3097 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, TRUE
);
3099 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3100 g_strfreev(path_orig
);
3104 xmlNodePtr parent
= n
->parent
;
3115 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
3116 n
= create_element_path(client
, &path
, &rc
);
3122 copy
= xmlCopyNodeList(root
);
3123 n
= xmlAddChildList(n
, copy
);
3126 rc
= EPWMD_LIBXML_ERROR
;
3129 /* Check if the content root element can create a DTD root element. */
3130 if (!xmlStrEqual((xmlChar
*)"element", root
->name
)) {
3131 rc
= GPG_ERR_SYNTAX
;
3137 if ((a
= xmlGetProp(root
, (xmlChar
*)"_name")) == NULL
) {
3138 rc
= GPG_ERR_SYNTAX
;
3142 gchar
*tmp
= g_strdup((gchar
*)a
);
3144 gboolean literal
= is_literal_element(&tmp
);
3146 if (!valid_xml_element((xmlChar
*)tmp
) || literal
) {
3148 rc
= GPG_ERR_INV_VALUE
;
3152 if (strv_printf(&path
, "%s", tmp
) == FALSE
) {
3154 rc
= gpg_error_from_errno(ENOMEM
);
3159 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, TRUE
);
3161 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3162 rc
= EPWMD_LIBXML_ERROR
;
3166 /* Overwriting the existing tree. */
3173 xmlSetProp(root
, (xmlChar
*)"_name", (xmlChar
*)path
[0]);
3174 n
= xmlCopyNode(root
, 1);
3175 n
= xmlAddChildList(xmlDocGetRootElement(client
->doc
), n
);
3179 rc
= update_element_mtime(n
->parent
);
3189 client
->inquire_status
= INQUIRE_DONE
;
3193 static gint
import_command(assuan_context_t ctx
, gchar
*line
)
3196 struct client_s
*client
= assuan_get_pointer(ctx
);
3198 rc
= assuan_inquire_ext(ctx
, "IMPORT", 0, import_command_finalize
, ctx
);
3201 return send_error(ctx
, rc
);
3203 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
3204 client
->inquire_status
= INQUIRE_BUSY
;
3208 static gpg_error_t
do_lock_command(struct client_s
*client
)
3210 gpg_error_t rc
= lock_file_mutex(client
);
3213 client
->is_lock_cmd
= TRUE
;
3215 return send_error(client
->ctx
, rc
);
3218 static gint
lock_command(assuan_context_t ctx
, gchar
*line
)
3220 struct client_s
*client
= assuan_get_pointer(ctx
);
3222 return do_lock_command(client
);
3225 static gint
unlock_command(assuan_context_t ctx
, gchar
*line
)
3227 struct client_s
*client
= assuan_get_pointer(ctx
);
3229 unlock_file_mutex(client
);
3230 return send_error(ctx
, 0);
3233 static gint
getpid_command(assuan_context_t ctx
, gchar
*line
)
3237 pid_t pid
= getpid();
3239 print_fmt(buf
, sizeof(buf
), "%i", pid
);
3240 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3241 return send_error(ctx
, rc
);
3244 static gint
version_command(assuan_context_t ctx
, gchar
*line
)
3249 print_fmt(buf
, sizeof(buf
), "0x%X", VERSION_HEX
);
3250 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3251 return send_error(ctx
, rc
);
3254 #ifdef WITH_PINENTRY
3255 static void set_option_value(gchar
**opt
, const gchar
*value
)
3263 *opt
= g_strdup(value
);
3267 static gint
set_unset_common(assuan_context_t ctx
, const gchar
*name
,
3270 struct client_s
*client
= assuan_get_pointer(ctx
);
3272 if (g_ascii_strcasecmp(name
, (gchar
*)"log_level") == 0) {
3276 l
= strtol(value
, NULL
, 10);
3279 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3282 log_write1("log_level=%li", l
);
3283 MUTEX_LOCK(&rcfile_mutex
);
3284 g_key_file_set_integer(keyfileh
, "global", "log_level", (gint
)l
);
3285 MUTEX_UNLOCK(&rcfile_mutex
);
3288 else if (g_ascii_strcasecmp(name
, (gchar
*)"rc_on_locked") == 0) {
3292 l
= strtol(value
, NULL
, 10);
3295 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3298 log_write1("rc_on_locked=%li", l
);
3299 client
->rc_on_locked
= l
? TRUE
: FALSE
;
3302 else if (g_ascii_strcasecmp(name
, (gchar
*)"NAME") == 0) {
3303 pth_attr_t attr
= pth_attr_of(pth_self());
3307 pth_attr_destroy(attr
);
3311 print_fmt(buf
, sizeof(buf
), "%s", value
);
3312 pth_attr_set(attr
, PTH_ATTR_NAME
, buf
);
3313 pth_attr_destroy(attr
);
3314 log_write1("name=%s", buf
);
3315 #ifdef WITH_PINENTRY
3316 if (client
->pinentry
->name
)
3317 g_free(client
->pinentry
->name
);
3319 client
->pinentry
->name
= g_strdup(buf
);
3321 if (!client
->pinentry
->name
)
3322 return gpg_error_from_errno(ENOMEM
);
3327 #ifdef WITH_PINENTRY
3328 else if (g_ascii_strcasecmp(name
, (gchar
*)"lc_messages") == 0)
3329 set_option_value(&client
->pinentry
->lcmessages
, value
);
3330 else if (g_ascii_strcasecmp(name
, (gchar
*)"lc_ctype") == 0)
3331 set_option_value(&client
->pinentry
->lcctype
, value
);
3332 else if (g_ascii_strcasecmp(name
, (gchar
*)"ttyname") == 0)
3333 set_option_value(&client
->pinentry
->ttyname
, value
);
3334 else if (g_ascii_strcasecmp(name
, (gchar
*)"ttytype") == 0)
3335 set_option_value(&client
->pinentry
->ttytype
, value
);
3336 else if (g_ascii_strcasecmp(name
, (gchar
*)"display") == 0)
3337 set_option_value(&client
->pinentry
->display
, value
);
3338 else if (g_ascii_strcasecmp(name
, (gchar
*)"pinentry_path") == 0)
3339 set_option_value(&client
->pinentry
->path
, value
);
3340 else if (g_ascii_strcasecmp(name
, (gchar
*)"title") == 0)
3341 set_option_value(&client
->pinentry
->title
, value
);
3342 else if (g_ascii_strcasecmp(name
, (gchar
*)"prompt") == 0)
3343 set_option_value(&client
->pinentry
->prompt
, value
);
3344 else if (g_ascii_strcasecmp(name
, (gchar
*)"desc") == 0)
3345 set_option_value(&client
->pinentry
->desc
, value
);
3346 else if (g_ascii_strcasecmp(name
, "pinentry_timeout") == 0) {
3353 n
= strtol(value
, &p
, 10);
3356 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3358 MUTEX_LOCK(&rcfile_mutex
);
3359 g_key_file_set_integer(keyfileh
, client
->filename
? client
->filename
:
3360 "global", "pinentry_timeout", n
);
3361 MUTEX_UNLOCK(&rcfile_mutex
);
3362 log_write1("pinentry_timeout=%i", n
);
3367 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
3369 log_write1("%s=%s", name
, value
? value
: "");
3375 static gint
unset_command(assuan_context_t ctx
, gchar
*line
)
3377 log_write2("ARGS=\"%s\"", line
);
3378 return send_error(ctx
, set_unset_common(ctx
, line
, NULL
));
3381 static gint
set_command(assuan_context_t ctx
, gchar
*line
)
3383 gchar name
[64] = {0}, value
[256] = {0};
3385 log_write2("ARGS=\"%s\"", line
);
3387 if (sscanf(line
, " %63[_a-zA-Z] = %255c", name
, value
) != 2)
3388 return send_error(ctx
, gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_SYNTAX
));
3390 return send_error(ctx
, set_unset_common(ctx
, name
, value
));
3393 static gint
rename_command(assuan_context_t ctx
, gchar
*line
)
3395 struct client_s
*client
= assuan_get_pointer(ctx
);
3397 gchar
**req
, **src
, *dst
;
3400 log_write2("ARGS=\"%s\"", line
);
3401 req
= split_input_line(line
, " ", -1);
3403 if (!req
|| !req
[0] || !req
[1]) {
3405 return send_error(ctx
, GPG_ERR_SYNTAX
);
3409 is_literal_element(&dst
);
3411 if (!valid_xml_element((xmlChar
*)dst
)) {
3413 return GPG_ERR_INV_VALUE
;
3416 if (strchr(req
[0], '\t'))
3417 src
= split_input_line(req
[0], "\t", -1);
3419 src
= split_input_line(req
[0], " ", -1);
3421 if (!src
|| !*src
) {
3422 rc
= GPG_ERR_SYNTAX
;
3426 n
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, FALSE
);
3429 n
= find_elements(client
->doc
, n
->children
, src
+1, &rc
, NULL
, NULL
,
3430 NULL
, FALSE
, 0, NULL
, FALSE
);
3436 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
3439 rc
= gpg_error_from_errno(ENOMEM
);
3443 /* To prevent unwanted effects:
3445 * <root name="a"><b/></root>
3449 if (xmlStrEqual(a
, (xmlChar
*)dst
)) {
3451 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3461 for (p
= src
; *p
; p
++) {
3465 strv_printf(&tmp
, "%s", *p
);
3469 strv_printf(&tmp
, "!%s", dst
);
3470 ndst
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
3472 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3478 ndst
= find_elements(client
->doc
, ndst
->children
, tmp
+1, &rc
, NULL
,
3479 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3483 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3488 /* Target may exist:
3491 * <root name="b" target="a"/>
3499 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3505 xmlFreeNodeList(ndst
);
3508 rc
= add_attribute(n
, "_name", dst
);
3513 return send_error(ctx
, rc
);
3516 static gint
copy_command(assuan_context_t ctx
, gchar
*line
)
3518 struct client_s
*client
= assuan_get_pointer(ctx
);
3520 gchar
**req
, **src
= NULL
, **dst
= NULL
;
3521 xmlNodePtr nsrc
, ndst
, new;
3523 log_write2("ARGS=\"%s\"", line
);
3524 req
= split_input_line(line
, " ", -1);
3526 if (!req
|| !req
[0] || !req
[1]) {
3528 return send_error(ctx
, GPG_ERR_SYNTAX
);
3531 if (strchr(req
[0], '\t'))
3532 src
= split_input_line(req
[0], "\t", -1);
3534 src
= split_input_line(req
[0], " ", -1);
3536 if (!src
|| !*src
) {
3537 rc
= GPG_ERR_SYNTAX
;
3541 if (strchr(req
[1], '\t'))
3542 dst
= split_input_line(req
[1], "\t", -1);
3544 dst
= split_input_line(req
[1], " ", -1);
3546 if (!dst
|| !*dst
) {
3547 rc
= GPG_ERR_SYNTAX
;
3551 nsrc
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, FALSE
);
3554 nsrc
= find_elements(client
->doc
, nsrc
->children
, src
+1, &rc
, NULL
,
3555 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3560 ndst
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, FALSE
);
3563 ndst
= find_elements(client
->doc
, ndst
->children
, dst
+1, &rc
, NULL
,
3564 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3566 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3569 new = xmlCopyNodeList(nsrc
);
3572 rc
= GPG_ERR_ENOMEM
;
3577 ndst
= create_element_path(client
, &dst
, &rc
);
3581 xmlFreeNodeList(new);
3585 /* Merge any attributes from the src node to the initial dst node. */
3586 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
) {
3587 if ((!src
[1] || !dst
[1]) && xmlStrEqual(attr
->name
, (xmlChar
*)"_name"))
3590 xmlAttrPtr a
= xmlHasProp(ndst
, attr
->name
);
3595 xmlChar
*tmp
= xmlNodeGetContent(attr
->children
);
3596 xmlNewProp(ndst
, attr
->name
, tmp
);
3600 xmlNodePtr n
= ndst
->children
;
3603 ndst
->children
= NULL
;
3605 if (!new->children
) {
3607 xmlFreeNodeList(new);
3611 n
= xmlCopyNodeList(new->children
);
3614 rc
= GPG_ERR_ENOMEM
;
3619 xmlFreeNodeList(new);
3620 n
= xmlAddChildList(ndst
, n
);
3623 rc
= GPG_ERR_ENOMEM
;
3627 rc
= update_element_mtime(xmlDocGetRootElement(client
->doc
) == ndst
->parent
? ndst
: ndst
->parent
);
3639 return send_error(ctx
, rc
);
3642 static gint
move_command(assuan_context_t ctx
, gchar
*line
)
3644 struct client_s
*client
= assuan_get_pointer(ctx
);
3646 gchar
**req
, **src
= NULL
, **dst
= NULL
;
3647 xmlNodePtr nsrc
, ndst
= NULL
;
3649 log_write2("ARGS=\"%s\"", line
);
3650 req
= split_input_line(line
, " ", -1);
3652 if (!req
|| !req
[0] || !req
[1]) {
3654 return send_error(ctx
, GPG_ERR_SYNTAX
);
3657 if (strchr(req
[0], '\t'))
3658 src
= split_input_line(req
[0], "\t", -1);
3660 src
= split_input_line(req
[0], " ", -1);
3662 if (!src
|| !*src
) {
3663 rc
= GPG_ERR_SYNTAX
;
3667 if (strchr(req
[1], '\t'))
3668 dst
= split_input_line(req
[1], "\t", -1);
3670 dst
= split_input_line(req
[1], " ", -1);
3672 nsrc
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, FALSE
);
3675 nsrc
= find_elements(client
->doc
, nsrc
->children
, src
+1, &rc
, NULL
,
3676 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3682 ndst
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, FALSE
);
3685 ndst
= find_elements(client
->doc
, ndst
->children
, dst
+1, &rc
, NULL
,
3686 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3689 ndst
= xmlDocGetRootElement(client
->doc
);
3691 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
) {
3693 rc
= GPG_ERR_CONFLICT
;
3698 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3704 xmlChar
*a
= node_has_attribute(nsrc
, (xmlChar
*)"_name");
3705 xmlNodePtr dup
= find_element(ndst
->children
, (gchar
*)a
, NULL
);
3713 if (ndst
== xmlDocGetRootElement(client
->doc
)) {
3714 xmlNodePtr n
= nsrc
;
3715 gboolean match
= FALSE
;
3717 while (n
->parent
&& n
->parent
!= ndst
)
3720 xmlChar
*a
= node_has_attribute(n
, (xmlChar
*)"_name");
3721 xmlChar
*b
= node_has_attribute(nsrc
, (xmlChar
*)"_name");
3723 if (xmlStrEqual(a
, b
)) {
3725 xmlUnlinkNode(nsrc
);
3735 xmlFreeNodeList(dup
);
3744 ndst
= create_element_path(client
, &dst
, &rc
);
3749 xmlUnlinkNode(nsrc
);
3750 ndst
= xmlAddChildList(ndst
, nsrc
);
3753 rc
= GPG_ERR_ENOMEM
;
3765 return send_error(ctx
, rc
);
3768 static int ls_command(assuan_context_t ctx
, gchar
*line
)
3770 log_write2("ARGS=\"%s\"", line
);
3772 gchar
*tmp
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
);
3773 gchar
*dir
= expand_homedir(tmp
);
3774 DIR *d
= opendir(dir
);
3781 return send_syserror(ctx
, n
);
3784 size_t len
= offsetof(struct dirent
, d_name
)+pathconf(dir
, _PC_NAME_MAX
)+1;
3785 struct dirent
*p
= g_malloc(len
), *cur
= NULL
;
3791 while (!readdir_r(d
, p
, &cur
) && cur
) {
3792 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3794 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.' && cur
->d_name
[2] == '\0')
3797 tmp
= g_strdup_printf("%s%s\n", list
? list
: "", cur
->d_name
);
3803 rc
= GPG_ERR_ENOMEM
;
3815 return send_error(ctx
, rc
);
3818 return send_error(ctx
, GPG_ERR_NO_VALUE
);
3820 list
[strlen(list
)-1] = 0;
3821 rc
= xfer_data(ctx
, list
, strlen(list
));
3823 return send_error(ctx
, rc
);
3826 static void bye_notify(assuan_context_t ctx
)
3828 struct client_s
*cl
= assuan_get_pointer(ctx
);
3830 /* This will let assuan_process_next() return. */
3831 fcntl(cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3834 static void reset_notify(assuan_context_t ctx
)
3836 struct client_s
*cl
= assuan_get_pointer(ctx
);
3843 * This is called before every Assuan command.
3845 gint
command_startup(assuan_context_t ctx
, const gchar
*name
)
3847 struct client_s
*cl
= assuan_get_pointer(ctx
);
3850 log_write1("%s", name
);
3852 if (!g_ascii_strcasecmp(name
, "ISCACHED") ||
3853 !g_ascii_strcasecmp(name
, "CLEARCACHE") ||
3854 !g_ascii_strcasecmp(name
, "CACHETIMEOUT") ||
3855 !g_ascii_strcasecmp(name
, "GETCONFIG") ||
3856 !g_ascii_strcasecmp(name
, "GETPID") ||
3857 !g_ascii_strcasecmp(name
, "VERSION") ||
3858 !g_ascii_strcasecmp(name
, "SET") ||
3859 !g_ascii_strcasecmp(name
, "BYE") ||
3860 !g_ascii_strcasecmp(name
, "NOP") ||
3861 !g_ascii_strcasecmp(name
, "CANCEL") ||
3862 !g_ascii_strcasecmp(name
, "RESET") ||
3863 !g_ascii_strcasecmp(name
, "END") ||
3864 !g_ascii_strcasecmp(name
, "HELP") ||
3865 !g_ascii_strcasecmp(name
, "OPTION") ||
3866 !g_ascii_strcasecmp(name
, "INPUT") ||
3867 !g_ascii_strcasecmp(name
, "OUTPUT") ||
3868 !g_ascii_strcasecmp(name
, "LS") ||
3869 !g_ascii_strcasecmp(name
, "UNSET"))
3872 #ifdef WITH_PINENTRY
3873 reset_pin_defaults(cl
->pinentry
);
3876 cl
->last_rc
= rc
= file_modified(cl
);
3879 if ((rc
== EPWMD_NO_FILE
|| rc
== EPWMD_FILE_MODIFIED
) &&
3880 !g_ascii_strcasecmp(name
, "OPEN"))
3888 * This is called after every Assuan command.
3890 void command_finalize(assuan_context_t ctx
, gint rc
)
3892 struct client_s
*client
= assuan_get_pointer(ctx
);
3894 if (!client
->is_lock_cmd
)
3895 unlock_file_mutex(client
);
3897 log_write1(N_("command completed (rc=%u)"), client
->last_rc
);
3900 gpg_error_t
register_commands(assuan_context_t ctx
)
3904 gint (*handler
)(assuan_context_t
, gchar
*line
);
3906 { "OPEN", open_command
},
3907 { "SAVE", save_command
},
3908 { "LIST", list_command
},
3909 { "REALPATH", realpath_command
},
3910 { "STORE", store_command
},
3911 { "DELETE", delete_command
},
3912 { "GET", get_command
},
3913 { "ATTR", attr_command
},
3914 { "ISCACHED", iscached_command
},
3915 { "CLEARCACHE", clearcache_command
},
3916 { "CACHETIMEOUT", cachetimeout_command
},
3917 { "GETCONFIG", getconfig_command
},
3918 { "DUMP", dump_command
},
3919 { "XPATH", xpath_command
},
3920 { "XPATHATTR", xpathattr_command
},
3921 { "IMPORT", import_command
},
3922 { "LOCK", lock_command
},
3923 { "UNLOCK", unlock_command
},
3924 { "GETPID", getpid_command
},
3925 { "VERSION", version_command
},
3926 { "SET", set_command
},
3927 { "UNSET", unset_command
},
3928 { "RENAME", rename_command
},
3929 { "COPY", copy_command
},
3930 { "LS", ls_command
},
3931 { "MOVE", move_command
},
3938 for (i
=0; table
[i
].name
; i
++) {
3939 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
3945 rc
= assuan_register_bye_notify(ctx
, bye_notify
);
3950 rc
= assuan_register_reset_notify(ctx
, reset_notify
);
3955 rc
= assuan_register_pre_cmd_notify(ctx
, command_startup
);
3960 return assuan_register_post_cmd_notify(ctx
, command_finalize
);
3963 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
,
3964 struct crypto_s
*crypto
, gpointer
*dst
, goffset
*dst_len
)
3966 goffset insize
, len
;
3967 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
3968 guint64 iter
= 0ULL, n_iter
= 0ULL, iter_progress
= 0ULL;
3972 gsize fh_size
= crypto
->fh
->v1
? sizeof(crypto
->fh
->ver
.fh1
) : sizeof(crypto
->fh
->ver
.fh2
);
3973 guint64 fh_iter
= crypto
->fh
->v1
? crypto
->fh
->ver
.fh1
.iter
: crypto
->fh
->ver
.fh2
.iter
;
3974 gsize hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
3976 lseek(crypto
->fh
->fd
, fh_size
, SEEK_SET
);
3977 insize
= crypto
->fh
->st
.st_size
- fh_size
;
3978 crypto
->iv
= gcry_malloc(crypto
->blocksize
);
3981 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3982 return gpg_error_from_errno(ENOMEM
);
3986 memcpy(crypto
->iv
, crypto
->fh
->ver
.fh1
.iv
, crypto
->blocksize
);
3988 memcpy(crypto
->iv
, crypto
->fh
->ver
.fh2
.iv
, crypto
->blocksize
);
3990 crypto
->inbuf
= gcry_malloc(insize
);
3992 if (!crypto
->inbuf
) {
3993 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3994 return gpg_error_from_errno(ENOMEM
);
3997 crypto
->insize
= insize
;
3998 len
= pth_read(crypto
->fh
->fd
, crypto
->inbuf
, crypto
->insize
);
4000 if (len
!= crypto
->insize
)
4001 return GPG_ERR_INV_LENGTH
;
4003 /* No encryption iterations. This is a plain (gzipped) file. */
4004 if ((crypto
->fh
->v1
&& (long)fh_iter
< 0L) ||
4005 (!crypto
->fh
->v1
&& fh_iter
<= 0L)) {
4007 * cache_file_count() needs both .used == TRUE and a valid key in
4008 * order for it to count as a used cache entry. Fixes CACHE status
4011 memset(crypto
->key
, '!', hashlen
);
4015 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->iv
, crypto
->blocksize
))) {
4016 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4020 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->key
, crypto
->keysize
))) {
4021 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4025 iter_progress
= (guint64
)get_key_file_double(client
&& client
->filename
?
4026 client
->filename
: "global", "iteration_progress");
4028 if (iter_progress
> 0ULL && fh_iter
>= iter_progress
) {
4029 rc
= send_status(ctx
, STATUS_DECRYPT
, "0 %llu", fh_iter
);
4035 rc
= iterate_crypto_once(client
, crypto
, STATUS_DECRYPT
);
4040 crypto
->tkey
= gcry_malloc(hashlen
);
4042 if (!crypto
->tkey
) {
4043 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
4044 return gpg_error_from_errno(ENOMEM
);
4047 memcpy(crypto
->tkey
, crypto
->key
, hashlen
);
4048 guchar
*tkey
= crypto
->tkey
;
4051 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->tkey
, crypto
->keysize
))) {
4052 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4056 while (iter
< (crypto
->fh
->v1
? fh_iter
: fh_iter
-1)) {
4057 if (iter_progress
> 0ULL && iter
>= iter_progress
) {
4058 if (!(iter
% iter_progress
)) {
4059 rc
= send_status(ctx
, STATUS_DECRYPT
, "%llu %llu",
4060 ++n_iter
* iter_progress
, fh_iter
);
4067 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->iv
, crypto
->blocksize
))) {
4068 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4072 rc
= iterate_crypto_once(client
, crypto
, STATUS_DECRYPT
);
4075 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4082 if (iter_progress
&& fh_iter
>= iter_progress
) {
4083 rc
= send_status(ctx
, STATUS_DECRYPT
, "%llu %llu", fh_iter
, fh_iter
);
4090 if (do_decompress(ctx
, crypto
->inbuf
, crypto
->insize
,
4091 (gpointer
*)&crypto
->outbuf
, &outsize
, &zrc
) == FALSE
) {
4092 if (zrc
== Z_MEM_ERROR
)
4093 return gpg_error_from_errno(ENOMEM
);
4095 return GPG_ERR_INV_PASSPHRASE
; // Not a valid gzip header. Must be a bad key.
4098 if (g_strncasecmp(crypto
->outbuf
, "<?xml ", 6) != 0) {
4099 gcry_free(crypto
->outbuf
);
4100 crypto
->outbuf
= NULL
;
4101 return GPG_ERR_INV_PASSPHRASE
;
4105 client
->xml
= crypto
->outbuf
;
4106 client
->len
= outsize
;
4107 crypto
->outbuf
= NULL
;
4110 *dst
= crypto
->outbuf
;
4112 crypto
->outbuf
= NULL
;
4115 /* The calling function should free the crypto struct. */