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 client
->lock_on_open
= FALSE
;
554 rc
= parse_options(&line
, args
, client
);
557 return send_error(ctx
, rc
);
559 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
562 pth_cleanup_push(req_cleanup
, req
);
564 if (!filename
|| !*filename
) {
566 return send_error(ctx
, GPG_ERR_SYNTAX
);
569 log_write2("ARGS=\"%s\" %s", filename
, req
[1] ? "<passphrase>" : "");
571 if (valid_filename(filename
) == FALSE
) {
573 return send_error(ctx
, GPG_ERR_INV_VALUE
);
576 if (client
->state
== STATE_OPEN
)
577 cleanup_client(client
);
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 gint
save_command(assuan_context_t ctx
, gchar
*line
)
1386 gboolean cached
= FALSE
;
1388 struct client_s
*client
= assuan_get_pointer(ctx
);
1389 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1391 struct argv_s
*args
[] = {
1392 &(struct argv_s
) { "iterations", OPT_OPTARG
, parse_save_opt_iterations
},
1393 &(struct argv_s
) { "cipher", OPT_OPTARG
, parse_save_opt_cipher
},
1394 &(struct argv_s
) { "pinentry", OPT_OPTARG
, parse_opt_pinentry
},
1398 rc
= parse_options(&line
, args
, client
);
1401 return send_error(ctx
, rc
);
1404 log_write2("ARGS=%s", "<passphrase>");
1406 if (lstat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1407 return send_syserror(ctx
, errno
);
1409 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
)) {
1410 log_write("%s: %s", client
->filename
, pwmd_strerror(GPG_ERR_ENOANO
));
1411 return send_error(ctx
, GPG_ERR_ENOANO
);
1415 cached
= cache_iscached(client
->md5file
);
1419 * If a cache entry doesn't exist for this file and the file has a
1420 * "key_file" or "key" parameter, then it's an error. The reason is that
1421 * cache expiration would be useless.
1423 if (cached
== FALSE
) {
1424 gchar
*tmp
= get_key_file_string(client
->filename
, "key_file");
1428 return send_error(ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1435 if (!client
->crypto
) {
1436 client
->crypto
= init_client_crypto();
1438 if (!client
->crypto
) {
1439 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1440 return send_syserror(ctx
, ENOMEM
);
1444 client
->crypto
->key
= gcry_malloc(hashlen
);
1446 if (!client
->crypto
->key
) {
1447 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1448 cleanup_crypto(&client
->crypto
);
1449 return send_syserror(ctx
, ENOMEM
);
1452 memset(client
->crypto
->key
, '!', hashlen
);
1454 if (get_key_file_double(client
->filename
, "iterations") <= 0L &&
1458 if (!line
|| !*line
) {
1459 client
->crypto
->tkey
= gcry_malloc(hashlen
);
1461 if (!client
->crypto
->tkey
) {
1462 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1463 cleanup_crypto(&client
->crypto
);
1464 return send_syserror(ctx
, ENOMEM
);
1467 memset(client
->crypto
->tkey
, '!', hashlen
);
1470 if (cache_get_key(client
->md5file
, client
->crypto
->key
) == FALSE
||
1471 !memcmp(client
->crypto
->key
, client
->crypto
->tkey
, hashlen
)) {
1474 #ifdef WITH_PINENTRY
1477 if (client
->pinentry
->enable
== FALSE
||
1478 get_key_file_boolean(client
->filename
, "enable_pinentry") == FALSE
) {
1479 /* Empty keys are allowed. */
1480 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
1484 lock_pin_mutex(client
);
1485 client
->pinentry
->which
= PINENTRY_SAVE
;
1486 rc
= pinentry_fork(ctx
);
1489 unlock_pin_mutex(client
->pinentry
);
1490 cleanup_crypto(&client
->crypto
);
1491 return send_error(ctx
, rc
);
1494 client
->pinentry
->cb
= save_command_finalize
;
1495 client
->pinentry
->status
= PINENTRY_INIT
;
1498 /* Empty keys are allowed. */
1499 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
1509 if (get_key_file_double(client
->filename
, "iterations") <= 0L) {
1510 guint64 iter
= (guint64
)get_key_file_double(NULL
, "iterations");
1515 MUTEX_LOCK(&rcfile_mutex
);
1516 g_key_file_set_double(keyfileh
, client
->filename
, "iterations", iter
);
1517 MUTEX_UNLOCK(&rcfile_mutex
);
1518 client
->opts
|= OPT_ITERATIONS
;
1519 gpg_error_t rc
= send_status(ctx
, STATUS_CONFIG
, NULL
);
1522 return send_error(ctx
, rc
);
1525 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, line
,
1530 return save_command_finalize(ctx
, client
->crypto
->key
, cached
);
1533 static gint
delete_command(assuan_context_t ctx
, gchar
*line
)
1535 struct client_s
*client
= assuan_get_pointer(ctx
);
1540 log_write2("ARGS=\"%s\"", line
);
1542 if (strchr(line
, '\t'))
1543 req
= split_input_line(line
, "\t", -1);
1545 req
= split_input_line(line
, " ", -1);
1548 return send_error(ctx
, GPG_ERR_SYNTAX
);
1550 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1554 return send_error(ctx
, rc
);
1558 * No sub-node defined. Remove the entire node (root element).
1561 xmlNodePtr parent
= n
->parent
;
1572 rc
= update_element_mtime(parent
);
1574 return send_error(ctx
, rc
);
1577 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
1581 return send_error(ctx
, rc
);
1584 xmlNodePtr parent
= n
->parent
;
1591 rc
= update_element_mtime(parent
);
1594 return send_error(ctx
, rc
);
1598 * Don't return with assuan_process_done() here. This has been called from
1599 * assuan_process_next() and the command should be finished in
1602 static gint
store_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
1605 assuan_context_t ctx
= data
;
1606 struct client_s
*client
= assuan_get_pointer(ctx
);
1609 gpg_error_t rc
= file_modified(client
);
1611 if (assuan_rc
|| rc
) {
1614 return assuan_rc
? assuan_rc
: rc
;
1617 req
= split_input_line((gchar
*)line
, "\t", 0);
1621 return GPG_ERR_SYNTAX
;
1624 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1626 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
1627 rc
= new_root_element(client
->doc
, *req
);
1644 n
= create_elements_cb(n
, req
+1, &rc
, NULL
);
1646 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1647 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
, FALSE
);
1651 client
->inquire_status
= INQUIRE_DONE
;
1654 rc
= update_element_mtime(n
);
1659 static gint
store_command(assuan_context_t ctx
, gchar
*line
)
1661 struct client_s
*client
= assuan_get_pointer(ctx
);
1664 rc
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1667 return send_error(ctx
, rc
);
1669 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1670 client
->inquire_status
= INQUIRE_BUSY
;
1674 static void *send_data_cb(void *arg
)
1676 struct assuan_cmd_s
*data
= arg
;
1680 pth_cancel_state(PTH_CANCEL_ENABLE
|PTH_CANCEL_ASYNCHRONOUS
, &old
);
1681 rc
= g_malloc(sizeof(gpg_error_t
));
1682 *rc
= assuan_send_data(data
->ctx
, data
->line
, data
->line_len
);
1683 pth_cancel_state(old
, NULL
);
1688 /* For every assuan command that needs to be sent to the client, a timeout is
1689 * needed to determine if the client lost the connection. The timeout is the
1690 * same as the "keepalive" configuration parameter or a default if unset.
1692 gpg_error_t
do_assuan_command(assuan_context_t ctx
,
1693 void *(*cb
)(void *data
), void *data
)
1695 pth_attr_t attr
= pth_attr_new();
1698 gint to
= get_key_file_integer("global", "keepalive");
1699 pth_event_t ev
, tev
;
1704 pth_attr_init(attr
);
1705 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, TRUE
);
1706 tid
= pth_spawn(attr
, cb
, data
);
1708 pth_attr_destroy(attr
);
1711 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1712 _gpg_strerror(gpg_error_from_errno(n
)));
1713 return gpg_error_from_errno(n
);
1716 pth_cleanup_push(cleanup_cancel_cb
, tid
);
1717 to
= to
<= 0 ? DEFAULT_KEEPALIVE_TO
: to
;
1718 ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, tid
);
1719 tev
= to
? pth_event(PTH_EVENT_TIME
, pth_timeout(to
, 0)) : NULL
;
1720 ev
= pth_event_concat(ev
, tev
, NULL
);
1721 pth_cleanup_push(cleanup_ev_cb
, ev
);
1726 st
= pth_event_status(tev
);
1728 if (st
== PTH_STATUS_OCCURRED
) {
1731 return GPG_ERR_TIMEOUT
;
1735 st
= pth_event_status(ev
);
1737 if (st
== PTH_STATUS_FAILED
) {
1741 rc
= GPG_ERR_ASS_WRITE_ERROR
;
1743 else if (st
== PTH_STATUS_OCCURRED
) {
1745 rc
= *(gpg_error_t
*)p
;
1754 static gpg_error_t
xfer_data(assuan_context_t ctx
, const gchar
*line
,
1760 struct assuan_cmd_s data
;
1761 gint progress
= get_key_file_integer("global", "xfer_progress");
1764 progress
= progress
>0 ? (progress
/ASSUAN_LINELENGTH
)*ASSUAN_LINELENGTH
: 0;
1765 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1767 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1774 if (sent
+ to_send
> total
)
1775 to_send
= total
- sent
;
1777 data
.line
= flush
? NULL
: (gchar
*)line
+sent
;
1778 data
.line_len
= flush
? 0 : to_send
;
1779 rc
= do_assuan_command(ctx
, send_data_cb
, &data
);
1782 sent
+= flush
? 0 : to_send
;
1784 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1785 (sent
== total
&& flush
))
1786 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1788 if (!flush
&& !rc
&& sent
== total
) {
1793 } while (!rc
&& sent
< total
);
1798 static gint
get_command(assuan_context_t ctx
, gchar
*line
)
1800 struct client_s
*client
= assuan_get_pointer(ctx
);
1805 log_write2("ARGS=\"%s\"", line
);
1806 req
= split_input_line(line
, "\t", -1);
1808 if (!req
|| !*req
) {
1810 return send_error(ctx
, GPG_ERR_SYNTAX
);
1813 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1817 return send_error(ctx
, rc
);
1821 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
1826 return send_error(ctx
, rc
);
1828 if (!n
|| !n
->children
)
1829 return send_error(ctx
, GPG_ERR_NO_VALUE
);
1831 n
= find_text_node(n
->children
);
1833 if (!n
|| !n
->content
|| !*n
->content
)
1834 return send_error(ctx
, GPG_ERR_NO_VALUE
);
1836 rc
= xfer_data(ctx
, (gchar
*)n
->content
, xmlStrlen(n
->content
));
1837 return send_error(ctx
, rc
);
1840 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**target
,
1841 gpg_error_t
*rc
, gchar
**req_orig
, void *data
)
1843 gchar
*path
= *(gchar
**)data
;
1844 gchar
*tmp
= NULL
, *result
;
1848 *(gchar
**)data
= NULL
;
1851 path
= g_strjoinv("\t", target
);
1854 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1855 *rc
= gpg_error_from_errno(ENOMEM
);
1860 tmp
= g_strjoinv("\t", req_orig
);
1864 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1865 *rc
= gpg_error_from_errno(ENOMEM
);
1871 result
= g_strdup_printf("%s\t%s", path
, tmp
);
1873 result
= g_strdup(path
);
1876 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1877 *rc
= gpg_error_from_errno(ENOMEM
);
1885 *(gchar
**)data
= result
;
1889 static void list_command_cleanup1(void *arg
);
1890 static gint
realpath_command(assuan_context_t ctx
, gchar
*line
)
1893 struct client_s
*client
= assuan_get_pointer(ctx
);
1901 log_write2("ARGS=\"%s\"", line
);
1903 if (strchr(line
, '\t') != NULL
) {
1904 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1905 return send_error(ctx
, GPG_ERR_SYNTAX
);
1908 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1909 return send_error(ctx
, GPG_ERR_SYNTAX
);
1912 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, FALSE
);
1916 return send_error(ctx
, rc
);
1919 rp
= g_strjoinv("\t", req
);
1923 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1924 return send_syserror(ctx
, ENOMEM
);
1928 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1929 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, &rp
, FALSE
);
1934 return send_error(ctx
, rc
);
1938 string
= g_string_new(rp
);
1943 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1944 return send_syserror(ctx
, ENOMEM
);
1948 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++) {
1949 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+1) && *(t
+1) != '!')) {
1950 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1955 pth_cleanup_push(list_command_cleanup1
, string
);
1956 rc
= xfer_data(ctx
, string
->str
, string
->len
);
1958 return send_error(ctx
, rc
);
1961 static void list_command_cleanup1(void *arg
)
1963 g_string_free((GString
*)arg
, TRUE
);
1966 static void list_command_cleanup2(void *arg
)
1968 struct element_list_s
*elements
= arg
;
1971 if (elements
->list
) {
1972 gint total
= g_slist_length(elements
->list
);
1975 for (i
= 0; i
< total
; i
++) {
1976 gchar
*tmp
= g_slist_nth_data(elements
->list
, i
);
1980 g_slist_free(elements
->list
);
1983 if (elements
->prefix
)
1984 g_free(elements
->prefix
);
1987 g_strfreev(elements
->req
);
1993 static gpg_error_t
parse_list_opt_norecurse(gpointer data
, gpointer value
)
1995 struct element_list_s
*elements
= data
;
1997 elements
->recurse
= FALSE
;
2001 static gpg_error_t
parse_list_opt_verbose(gpointer data
, gpointer value
)
2003 struct element_list_s
*elements
= data
;
2005 elements
->verbose
= TRUE
;
2009 static gint
list_command(assuan_context_t ctx
, gchar
*line
)
2011 struct client_s
*client
= assuan_get_pointer(ctx
);
2013 struct element_list_s
*elements
= NULL
;
2015 struct argv_s
*args
[] = {
2016 &(struct argv_s
) { "norecurse", OPT_NOARG
, parse_list_opt_norecurse
},
2017 &(struct argv_s
) { "verbose", OPT_NOARG
, parse_list_opt_verbose
},
2021 if (disable_list_and_dump
== TRUE
)
2022 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2024 elements
= g_malloc0(sizeof(struct element_list_s
));
2027 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2028 return gpg_err_code_from_errno(ENOMEM
);
2031 elements
->recurse
= TRUE
; // default
2032 pth_cleanup_push(list_command_cleanup2
, elements
);
2033 rc
= parse_options(&line
, args
, elements
);
2041 rc
= list_root_elements(client
->doc
, &str
, elements
->verbose
);
2045 return send_error(ctx
, rc
);
2048 pth_cleanup_push(list_command_cleanup1
, str
);
2049 rc
= xfer_data(ctx
, str
->str
, str
->len
);
2052 return send_error(ctx
, rc
);
2055 elements
->req
= split_input_line(line
, " ", 0);
2058 strv_printf(&elements
->req
, "%s", line
);
2060 rc
= create_path_list(client
->doc
, elements
, *elements
->req
);
2066 gint total
= g_slist_length(elements
->list
);
2071 rc
= GPG_ERR_NO_VALUE
;
2075 str
= g_string_new(NULL
);
2078 rc
= gpg_err_code_from_errno(ENOMEM
);
2079 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2083 for (i
= 0; i
< total
; i
++) {
2084 tmp
= g_slist_nth_data(elements
->list
, i
);
2085 g_string_append_printf(str
, "%s%s", tmp
, i
+1 == total
? "" : "\n");
2088 pth_cleanup_push(list_command_cleanup1
, str
);
2089 rc
= xfer_data(ctx
, str
->str
, str
->len
);
2093 rc
= GPG_ERR_NO_VALUE
;
2097 return send_error(ctx
, rc
);
2101 * req[0] - element path
2103 static gpg_error_t
attribute_list(assuan_context_t ctx
, gchar
**req
)
2105 struct client_s
*client
= assuan_get_pointer(ctx
);
2106 gchar
**attrlist
= NULL
;
2108 gchar
**path
= NULL
;
2114 if (!req
|| !req
[0])
2115 return GPG_ERR_SYNTAX
;
2117 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2119 * The first argument may be only a root element.
2121 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
2122 return GPG_ERR_SYNTAX
;
2125 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2133 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2134 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2144 for (a
= n
->properties
; a
; a
= a
->next
) {
2147 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
2149 g_strfreev(attrlist
);
2151 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2152 return gpg_error_from_errno(ENOMEM
);
2157 attrlist
[i
] = g_strdup_printf("%s %s", (gchar
*)a
->name
,
2158 an
&& an
->content
? (gchar
*)an
->content
: "");
2161 g_strfreev(attrlist
);
2162 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2163 return gpg_error_from_errno(ENOMEM
);
2166 attrlist
[++i
] = NULL
;
2170 return GPG_ERR_NO_VALUE
;
2172 line
= g_strjoinv("\n", attrlist
);
2175 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2176 g_strfreev(attrlist
);
2177 return gpg_error_from_errno(ENOMEM
);
2180 pth_cleanup_push(g_free
, line
);
2181 pth_cleanup_push(req_cleanup
, attrlist
);
2182 rc
= xfer_data(ctx
, line
, strlen(line
));
2189 * req[0] - attribute
2190 * req[1] - element path
2192 static gpg_error_t
attribute_delete(struct client_s
*client
, gchar
**req
)
2195 gchar
**path
= NULL
;
2198 if (!req
|| !req
[0] || !req
[1])
2199 return GPG_ERR_SYNTAX
;
2201 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2203 * The first argument may be only a root element.
2205 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2206 return GPG_ERR_SYNTAX
;
2210 * Don't remove the "_name" attribute for the root element. To remove an
2211 * root element use DELETE <name>.
2213 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"_name")) {
2214 rc
= GPG_ERR_SYNTAX
;
2218 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2224 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2225 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2231 rc
= delete_attribute(n
, (xmlChar
*)req
[0]);
2238 static xmlNodePtr
create_element_path(struct client_s
*client
, gchar
***path
,
2241 gchar
**src
= *path
;
2242 gchar
**src_orig
= g_strdupv(src
);
2243 xmlNodePtr n
= NULL
;
2248 *rc
= gpg_error_from_errno(ENOMEM
);
2249 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2254 n
= find_root_element(client
->doc
, &src
, rc
, NULL
, 0, FALSE
);
2257 if (*rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
2258 *rc
= new_root_element(client
->doc
, src
[0]);
2271 n
= create_target_elements_cb(n
, src
+1, rc
, NULL
);
2273 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
2274 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
, FALSE
);
2280 * Reset the position of the element tree now that the elements
2281 * have been created.
2286 n
= find_root_element(client
->doc
, &src
, rc
, NULL
, 0, FALSE
);
2291 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
2292 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2300 g_strfreev(src_orig
);
2307 * Creates a "target" attribute. When other commands encounter an element with
2308 * this attribute, the element path is modified to the target value. If the
2309 * source element path doesn't exist when using 'ATTR SET target', it is
2310 * created, but the destination element path must exist.
2312 * req[0] - source element path
2313 * req[1] - destination element path
2315 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
2317 gchar
**src
, **dst
, *line
= NULL
, **odst
= NULL
;
2321 if (!req
|| !req
[0] || !req
[1])
2322 return GPG_ERR_SYNTAX
;
2324 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2326 * The first argument may be only a root element.
2328 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
2329 return GPG_ERR_SYNTAX
;
2332 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2334 * The first argument may be only a root element.
2336 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
2337 rc
= GPG_ERR_SYNTAX
;
2342 odst
= g_strdupv(dst
);
2345 rc
= gpg_error_from_errno(ENOMEM
);
2349 n
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, FALSE
);
2352 * Make sure the destination element path exists.
2358 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
2359 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2365 n
= create_element_path(client
, &src
, &rc
);
2370 line
= g_strjoinv("\t", odst
);
2373 rc
= gpg_error_from_errno(ENOMEM
);
2374 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2378 rc
= add_attribute(n
, "target", line
);
2392 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
2398 tmp
= g_strdupv(req
);
2401 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2402 return gpg_error_from_errno(ENOMEM
);
2405 n
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
2411 if (g_utf8_collate(req
[0], req
[1]) == 0)
2415 * Will not overwrite an existing root.
2417 tmp
= g_strdupv(req
+1);
2420 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2421 return gpg_error_from_errno(ENOMEM
);
2424 n
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
2427 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2431 return GPG_ERR_AMBIGUOUS_NAME
;
2434 * Whitespace not allowed in root names.
2436 if (contains_whitespace(req
[1]) == TRUE
)
2437 return GPG_ERR_SYNTAX
;
2439 tmp
= g_strdupv(req
);
2442 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2443 return gpg_error_from_errno(ENOMEM
);
2446 n
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
2450 return GPG_ERR_ELEMENT_NOT_FOUND
;
2452 return add_attribute(n
, "_name", req
[1]);
2456 * req[0] - attribute
2457 * req[1] - element path
2459 static gpg_error_t
attribute_get(assuan_context_t ctx
, gchar
**req
)
2461 struct client_s
*client
= assuan_get_pointer(ctx
);
2467 if (!req
|| !req
[0] || !req
[1])
2468 return GPG_ERR_SYNTAX
;
2470 if (strchr(req
[1], '\t')) {
2471 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2472 return GPG_ERR_SYNTAX
;
2475 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2476 return GPG_ERR_SYNTAX
;
2479 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2485 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2486 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2494 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2495 return GPG_ERR_NOT_FOUND
;
2497 pth_cleanup_push(xmlFree
, a
);
2500 rc
= xfer_data(ctx
, (gchar
*)a
, xmlStrlen(a
));
2502 rc
= GPG_ERR_NO_VALUE
;
2513 * req[0] - attribute
2514 * req[1] - element path
2517 static gpg_error_t
attribute_set(struct client_s
*client
, gchar
**req
)
2519 gchar
**path
= NULL
;
2523 if (!req
|| !req
[0] || !req
[1])
2524 return GPG_ERR_SYNTAX
;
2527 * Reserved attribute names.
2529 if (!strcmp(req
[0], "_name")) {
2531 * Only reserved for the root element. Not the rest of the
2534 if (strchr(req
[1], '\t') == NULL
)
2535 return name_attribute(client
, req
+ 1);
2537 else if (!strcmp(req
[0], "target"))
2538 return target_attribute(client
, req
+ 1);
2540 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2542 * The first argument may be only a root element.
2544 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2545 return GPG_ERR_SYNTAX
;
2548 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
2554 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2555 NULL
, NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
2561 rc
= add_attribute(n
, req
[0], req
[2]);
2570 * req[1] - attribute name or element path if command is LIST
2571 * req[2] - element path
2572 * req[2] - element path or value
2574 static gint
attr_command(assuan_context_t ctx
, gchar
*line
)
2576 struct client_s
*client
= assuan_get_pointer(ctx
);
2580 log_write2("ARGS=\"%s\"", line
);
2581 req
= split_input_line(line
, " ", 4);
2583 if (!req
|| !req
[0] || !req
[1]) {
2585 return send_error(ctx
, GPG_ERR_SYNTAX
);
2588 pth_cleanup_push(req_cleanup
, req
);
2590 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2591 rc
= attribute_set(client
, req
+1);
2592 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2593 rc
= attribute_get(ctx
, req
+1);
2594 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2595 rc
= attribute_delete(client
, req
+1);
2596 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2597 rc
= attribute_list(ctx
, req
+1);
2599 rc
= GPG_ERR_SYNTAX
;
2602 return send_error(ctx
, rc
);
2605 static gint
iscached_command(assuan_context_t ctx
, gchar
*line
)
2607 gchar
**req
= split_input_line(line
, " ", 0);
2611 if (!req
|| !*req
) {
2613 return send_error(ctx
, GPG_ERR_SYNTAX
);
2616 log_write2("ARGS=\"%s\"", line
);
2618 if (!valid_filename(req
[0])) {
2620 return GPG_ERR_INV_VALUE
;
2623 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2626 if (cache_iscached(md5file
)) {
2629 return send_error(ctx
, 0);
2633 tmp
= get_key_file_string("global", "data_directory");
2637 return gpg_error_from_errno(ENOMEM
);
2640 path
= expand_homedir(tmp
);
2645 return gpg_error_from_errno(ENOMEM
);
2650 path
= g_strdup_printf("%s/%s", tmp
, req
[0]);
2655 return gpg_error_from_errno(ENOMEM
);
2658 if (access(path
, R_OK
) == -1) {
2659 gpg_error_t rc
= gpg_error_from_syserror();
2663 return send_error(ctx
, rc
);
2667 return send_error(ctx
, GPG_ERR_NOT_FOUND
);
2670 static gint
clearcache_command(assuan_context_t ctx
, gchar
*line
)
2672 struct client_s
*client
= assuan_get_pointer(ctx
);
2673 gchar
**req
= split_input_line(line
, " ", 0);
2676 log_write2("ARGS=\"%s\"", line
);
2679 if (!req
|| !*req
) {
2681 cache_clear(client
->md5file
, 2);
2683 return send_error(ctx
, 0);
2686 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2689 if (cache_clear(md5file
, 1) == FALSE
) {
2691 return send_error(ctx
, GPG_ERR_NOT_FOUND
);
2695 return send_error(ctx
, 0);
2698 static gint
cachetimeout_command(assuan_context_t ctx
, gchar
*line
)
2702 gchar
**req
= split_input_line(line
, " ", 0);
2705 if (!req
|| !*req
|| !req
[1]) {
2707 return send_error(ctx
, GPG_ERR_SYNTAX
);
2711 timeout
= strtol(req
[1], &p
, 10);
2713 if (errno
!= 0 || *p
!= 0 || timeout
< -1) {
2715 return send_error(ctx
, GPG_ERR_SYNTAX
);
2718 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2719 CACHE_LOCK(client
->ctx
);
2721 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2723 return send_error(ctx
, GPG_ERR_NOT_FOUND
);
2727 return send_error(ctx
, 0);
2730 static gint
dump_command(assuan_context_t ctx
, gchar
*line
)
2734 struct client_s
*client
= assuan_get_pointer(ctx
);
2737 if (disable_list_and_dump
== TRUE
)
2738 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2740 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2743 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2744 return send_syserror(ctx
, ENOMEM
);
2747 pth_cleanup_push(xmlFree
, xml
);
2748 rc
= xfer_data(ctx
, (gchar
*)xml
, len
);
2750 return send_error(ctx
, rc
);
2753 static gint
getconfig_command(assuan_context_t ctx
, gchar
*line
)
2755 struct client_s
*client
= assuan_get_pointer(ctx
);
2757 gchar filename
[255]={0}, param
[747]={0};
2758 gchar
*p
, *tmp
, *fp
= client
->filename
, *paramp
= line
;
2760 log_write2("ARGS=\"%s\"", line
);
2762 if (strchr(line
, ' ')) {
2763 sscanf(line
, " %254[^ ] %746c", filename
, param
);
2768 if (fp
&& !valid_filename(fp
))
2769 return send_error(ctx
, GPG_ERR_INV_VALUE
);
2771 paramp
= g_ascii_strdown(paramp
, -1);
2774 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2775 return send_syserror(ctx
, ENOMEM
);
2778 if (fp
&& !g_ascii_strcasecmp(paramp
, "iterations")) {
2779 if (!(client
->opts
& OPT_ITERATIONS
) || fp
!= client
->filename
) {
2780 file_header_internal_t
*fh
= read_file_header(fp
, FALSE
, &rc
);
2782 if (!fh
&& rc
!= GPG_ERR_ENOENT
)
2783 return send_error(ctx
, rc
);
2787 p
= g_strdup_printf("%lu", (unsigned long)fh
->ver
.fh2
.iter
);
2788 close_file_header(fh
);
2791 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2793 return send_syserror(ctx
, ENOMEM
);
2800 else if (!g_ascii_strcasecmp(paramp
, "enable_pinentry")) {
2801 #ifdef WITH_PINENTRY
2804 if (fp
== client
->filename
&& (client
->opts
& OPT_PINENTRY
))
2805 n
= client
->pinentry
->enable
;
2807 n
= get_key_file_boolean(fp
, "enable_pinentry");
2809 p
= g_strdup_printf("%s", n
? "true" : "false");
2812 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2814 return send_syserror(ctx
, ENOMEM
);
2819 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2822 else if (!g_ascii_strcasecmp(paramp
, "pinentry_timeout")) {
2823 #ifdef WITH_PINENTRY
2824 if (fp
== client
->filename
&& (client
->opts
& OPT_PINENTRY_TO
))
2825 p
= g_strdup_printf("%i", client
->pinentry
->timeout
);
2827 p
= g_strdup_printf("%i",
2828 get_key_file_integer(fp
, "pinentry_timeout"));
2831 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2833 return send_syserror(ctx
, ENOMEM
);
2838 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2842 p
= get_key_file_string(fp
? fp
: "global", paramp
);
2846 return send_error(ctx
, GPG_ERR_NO_VALUE
);
2848 tmp
= expand_homedir(p
);
2852 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2853 return send_syserror(ctx
, ENOMEM
);
2858 pth_cleanup_push(g_free
, p
);
2859 rc
= xfer_data(ctx
, p
, strlen(p
));
2861 return send_error(ctx
, rc
);
2865 xmlXPathContextPtr xp
;
2866 xmlXPathObjectPtr result
;
2871 static void xpath_command_cleanup(void *arg
)
2873 struct xpath_s
*xpath
= arg
;
2875 req_cleanup(xpath
->req
);
2878 xmlBufferFree(xpath
->buf
);
2881 xmlXPathFreeObject(xpath
->result
);
2884 xmlXPathFreeContext(xpath
->xp
);
2887 static gint
xpath_command(assuan_context_t ctx
, gchar
*line
)
2889 struct client_s
*client
= assuan_get_pointer(ctx
);
2891 struct xpath_s xpath
;
2893 log_write2("ARGS=\"%s\"", line
);
2895 if (disable_list_and_dump
== TRUE
)
2896 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2898 if (!line
|| !*line
)
2899 return send_error(ctx
, GPG_ERR_SYNTAX
);
2901 memset(&xpath
, 0, sizeof(struct xpath_s
));
2903 if ((xpath
.req
= split_input_line(line
, "\t", 2)) == NULL
) {
2904 if (strv_printf(&xpath
.req
, "%s", line
) == FALSE
)
2905 return send_syserror(ctx
, ENOMEM
);
2908 xpath
.xp
= xmlXPathNewContext(client
->doc
);
2911 xpath_command_cleanup(&xpath
);
2912 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2915 xpath
.result
= xmlXPathEvalExpression((xmlChar
*)xpath
.req
[0], xpath
.xp
);
2917 if (!xpath
.result
) {
2918 xpath_command_cleanup(&xpath
);
2919 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2922 if (xmlXPathNodeSetIsEmpty(xpath
.result
->nodesetval
)) {
2923 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2927 rc
= recurse_xpath_nodeset(client
->doc
, xpath
.result
->nodesetval
,
2928 (xmlChar
*)xpath
.req
[1], &xpath
.buf
, 0, NULL
);
2932 else if (!xpath
.req
[1] && !xmlBufferLength(xpath
.buf
)) {
2933 rc
= GPG_ERR_NO_VALUE
;
2936 else if (xpath
.req
[1])
2939 pth_cleanup_push(xpath_command_cleanup
, &xpath
);
2940 rc
= xfer_data(ctx
, (gchar
*)xmlBufferContent(xpath
.buf
),
2941 xmlBufferLength(xpath
.buf
));
2945 xpath_command_cleanup(&xpath
);
2946 return send_error(ctx
, rc
);
2949 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2950 static gint
xpathattr_command(assuan_context_t ctx
, gchar
*line
)
2952 struct client_s
*client
= assuan_get_pointer(ctx
);
2954 struct xpath_s xpath
;
2956 gboolean cmd
= FALSE
; //SET
2958 log_write2("ARGS=\"%s\"", line
);
2960 if (disable_list_and_dump
== TRUE
)
2961 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2963 if (!line
|| !*line
)
2964 return send_error(ctx
, GPG_ERR_SYNTAX
);
2966 memset(&xpath
, 0, sizeof(struct xpath_s
));
2968 if ((req
= split_input_line(line
, " ", 3)) == NULL
)
2969 return send_syserror(ctx
, ENOMEM
);
2972 rc
= GPG_ERR_SYNTAX
;
2976 if (!g_ascii_strcasecmp(req
[0], "SET"))
2978 else if (!g_ascii_strcasecmp(req
[0], "DELETE"))
2981 rc
= GPG_ERR_SYNTAX
;
2985 if (!req
[1] || !req
[2]) {
2986 rc
= GPG_ERR_SYNTAX
;
2990 if ((xpath
.req
= split_input_line(req
[2], "\t", 3)) == NULL
) {
2991 rc
= gpg_err_code_from_errno(ENOMEM
);
2995 if (!xpath
.req
[0] || (!xpath
.req
[1] && !cmd
) || (xpath
.req
[1] && cmd
)) {
2996 rc
= GPG_ERR_SYNTAX
;
3000 xpath
.xp
= xmlXPathNewContext(client
->doc
);
3003 rc
= EPWMD_LIBXML_ERROR
;
3007 xpath
.result
= xmlXPathEvalExpression((xmlChar
*)xpath
.req
[0], xpath
.xp
);
3009 if (!xpath
.result
) {
3010 rc
= EPWMD_LIBXML_ERROR
;
3014 if (xmlXPathNodeSetIsEmpty(xpath
.result
->nodesetval
)) {
3015 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
3019 rc
= recurse_xpath_nodeset(client
->doc
, xpath
.result
->nodesetval
,
3020 (xmlChar
*)xpath
.req
[1], &xpath
.buf
, cmd
, (xmlChar
*)req
[1]);
3024 xpath_command_cleanup(&xpath
);
3025 return send_error(ctx
, rc
);
3028 static gint
import_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
3031 struct client_s
*client
= assuan_get_pointer((assuan_context_t
)data
);
3032 gpg_error_t rc
= file_modified(client
);
3033 gchar
**req
, **path
= NULL
, **path_orig
= NULL
, *content
;
3034 xmlDocPtr doc
= NULL
;
3035 xmlNodePtr n
, root
, copy
;
3037 if (assuan_rc
|| rc
) {
3040 return assuan_rc
? assuan_rc
: rc
;
3043 req
= split_input_line((gchar
*)line
, "\t", 2);
3047 return GPG_ERR_SYNTAX
;
3050 path
= split_input_line(req
[1], "\t", 0);
3052 if (!content
|| !*content
) {
3053 rc
= GPG_ERR_SYNTAX
;
3057 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
3060 rc
= EPWMD_LIBXML_ERROR
;
3064 root
= xmlDocGetRootElement(doc
);
3065 rc
= validate_import(root
);
3071 path_orig
= g_strdupv(path
);
3074 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3075 rc
= gpg_error_from_errno(ENOMEM
);
3079 xmlChar
*a
= xmlGetProp(root
, (xmlChar
*)"_name");
3082 g_strfreev(path_orig
);
3083 rc
= gpg_error_from_errno(ENOMEM
);
3087 if (strv_printf(&path
, "%s", (gchar
*)a
) == FALSE
) {
3089 g_strfreev(path_orig
);
3090 rc
= gpg_error_from_errno(ENOMEM
);
3095 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, FALSE
);
3097 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3098 g_strfreev(path_orig
);
3103 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
, TRUE
);
3105 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3106 g_strfreev(path_orig
);
3110 xmlNodePtr parent
= n
->parent
;
3121 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
3122 n
= create_element_path(client
, &path
, &rc
);
3128 copy
= xmlCopyNodeList(root
);
3129 n
= xmlAddChildList(n
, copy
);
3132 rc
= EPWMD_LIBXML_ERROR
;
3135 /* Check if the content root element can create a DTD root element. */
3136 if (!xmlStrEqual((xmlChar
*)"element", root
->name
)) {
3137 rc
= GPG_ERR_SYNTAX
;
3143 if ((a
= xmlGetProp(root
, (xmlChar
*)"_name")) == NULL
) {
3144 rc
= GPG_ERR_SYNTAX
;
3148 gchar
*tmp
= g_strdup((gchar
*)a
);
3150 gboolean literal
= is_literal_element(&tmp
);
3152 if (!valid_xml_element((xmlChar
*)tmp
) || literal
) {
3154 rc
= GPG_ERR_INV_VALUE
;
3158 if (strv_printf(&path
, "%s", tmp
) == FALSE
) {
3160 rc
= gpg_error_from_errno(ENOMEM
);
3165 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, TRUE
);
3167 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3168 rc
= EPWMD_LIBXML_ERROR
;
3172 /* Overwriting the existing tree. */
3179 xmlSetProp(root
, (xmlChar
*)"_name", (xmlChar
*)path
[0]);
3180 n
= xmlCopyNode(root
, 1);
3181 n
= xmlAddChildList(xmlDocGetRootElement(client
->doc
), n
);
3192 client
->inquire_status
= INQUIRE_DONE
;
3196 static gint
import_command(assuan_context_t ctx
, gchar
*line
)
3199 struct client_s
*client
= assuan_get_pointer(ctx
);
3201 rc
= assuan_inquire_ext(ctx
, "IMPORT", 0, import_command_finalize
, ctx
);
3204 return send_error(ctx
, rc
);
3206 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
3207 client
->inquire_status
= INQUIRE_BUSY
;
3211 static gpg_error_t
do_lock_command(struct client_s
*client
)
3213 gpg_error_t rc
= lock_file_mutex(client
);
3216 client
->is_lock_cmd
= TRUE
;
3218 return send_error(client
->ctx
, rc
);
3221 static gint
lock_command(assuan_context_t ctx
, gchar
*line
)
3223 struct client_s
*client
= assuan_get_pointer(ctx
);
3225 return do_lock_command(client
);
3228 static gint
unlock_command(assuan_context_t ctx
, gchar
*line
)
3230 struct client_s
*client
= assuan_get_pointer(ctx
);
3232 unlock_file_mutex(client
);
3233 return send_error(ctx
, 0);
3236 static gint
getpid_command(assuan_context_t ctx
, gchar
*line
)
3240 pid_t pid
= getpid();
3242 print_fmt(buf
, sizeof(buf
), "%i", pid
);
3243 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3244 return send_error(ctx
, rc
);
3247 static gint
version_command(assuan_context_t ctx
, gchar
*line
)
3252 print_fmt(buf
, sizeof(buf
), "0x%X", VERSION_HEX
);
3253 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3254 return send_error(ctx
, rc
);
3257 #ifdef WITH_PINENTRY
3258 static void set_option_value(gchar
**opt
, const gchar
*value
)
3266 *opt
= g_strdup(value
);
3270 static gint
set_unset_common(assuan_context_t ctx
, const gchar
*name
,
3273 struct client_s
*client
= assuan_get_pointer(ctx
);
3275 if (g_ascii_strcasecmp(name
, (gchar
*)"log_level") == 0) {
3279 l
= strtol(value
, NULL
, 10);
3282 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3285 log_write1("log_level=%li", l
);
3286 MUTEX_LOCK(&rcfile_mutex
);
3287 g_key_file_set_integer(keyfileh
, "global", "log_level", (gint
)l
);
3288 MUTEX_UNLOCK(&rcfile_mutex
);
3291 else if (g_ascii_strcasecmp(name
, (gchar
*)"rc_on_locked") == 0) {
3295 l
= strtol(value
, NULL
, 10);
3298 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3301 log_write1("rc_on_locked=%li", l
);
3302 client
->rc_on_locked
= l
? TRUE
: FALSE
;
3305 else if (g_ascii_strcasecmp(name
, (gchar
*)"NAME") == 0) {
3306 pth_attr_t attr
= pth_attr_of(pth_self());
3310 pth_attr_destroy(attr
);
3314 print_fmt(buf
, sizeof(buf
), "%s", value
);
3315 pth_attr_set(attr
, PTH_ATTR_NAME
, buf
);
3316 pth_attr_destroy(attr
);
3317 log_write1("name=%s", buf
);
3318 #ifdef WITH_PINENTRY
3319 if (client
->pinentry
->name
)
3320 g_free(client
->pinentry
->name
);
3322 client
->pinentry
->name
= g_strdup(buf
);
3324 if (!client
->pinentry
->name
)
3325 return gpg_error_from_errno(ENOMEM
);
3330 #ifdef WITH_PINENTRY
3331 else if (g_ascii_strcasecmp(name
, (gchar
*)"lc_messages") == 0)
3332 set_option_value(&client
->pinentry
->lcmessages
, value
);
3333 else if (g_ascii_strcasecmp(name
, (gchar
*)"lc_ctype") == 0)
3334 set_option_value(&client
->pinentry
->lcctype
, value
);
3335 else if (g_ascii_strcasecmp(name
, (gchar
*)"ttyname") == 0)
3336 set_option_value(&client
->pinentry
->ttyname
, value
);
3337 else if (g_ascii_strcasecmp(name
, (gchar
*)"ttytype") == 0)
3338 set_option_value(&client
->pinentry
->ttytype
, value
);
3339 else if (g_ascii_strcasecmp(name
, (gchar
*)"display") == 0)
3340 set_option_value(&client
->pinentry
->display
, value
);
3341 else if (g_ascii_strcasecmp(name
, (gchar
*)"pinentry_path") == 0)
3342 set_option_value(&client
->pinentry
->path
, value
);
3343 else if (g_ascii_strcasecmp(name
, (gchar
*)"title") == 0)
3344 set_option_value(&client
->pinentry
->title
, value
);
3345 else if (g_ascii_strcasecmp(name
, (gchar
*)"prompt") == 0)
3346 set_option_value(&client
->pinentry
->prompt
, value
);
3347 else if (g_ascii_strcasecmp(name
, (gchar
*)"desc") == 0)
3348 set_option_value(&client
->pinentry
->desc
, value
);
3349 else if (g_ascii_strcasecmp(name
, "pinentry_timeout") == 0) {
3354 client
->pinentry
->timeout
=
3355 get_key_file_integer(client
->filename
, "pinentry_timeout");
3356 client
->opts
&= ~(OPT_PINENTRY_TO
);
3357 log_write1("pinentry_timeout=%i",
3358 get_key_file_integer(client
->filename
, "pinentry_timeout"));
3362 n
= strtol(value
, &p
, 10);
3365 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3367 client
->pinentry
->timeout
= n
;
3368 client
->opts
|= OPT_PINENTRY_TO
;
3369 log_write1("pinentry_timeout=%i", n
);
3374 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
3376 log_write1("%s=%s", name
, value
? value
: "");
3382 static gint
unset_command(assuan_context_t ctx
, gchar
*line
)
3384 log_write2("ARGS=\"%s\"", line
);
3385 return send_error(ctx
, set_unset_common(ctx
, line
, NULL
));
3388 static gint
set_command(assuan_context_t ctx
, gchar
*line
)
3390 gchar name
[64] = {0}, value
[256] = {0};
3392 log_write2("ARGS=\"%s\"", line
);
3394 if (sscanf(line
, " %63[_a-zA-Z] = %255c", name
, value
) != 2)
3395 return send_error(ctx
, gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_SYNTAX
));
3397 return send_error(ctx
, set_unset_common(ctx
, name
, value
));
3400 static gint
rename_command(assuan_context_t ctx
, gchar
*line
)
3402 struct client_s
*client
= assuan_get_pointer(ctx
);
3404 gchar
**req
, **src
, *dst
;
3407 log_write2("ARGS=\"%s\"", line
);
3408 req
= split_input_line(line
, " ", -1);
3410 if (!req
|| !req
[0] || !req
[1]) {
3412 return send_error(ctx
, GPG_ERR_SYNTAX
);
3416 is_literal_element(&dst
);
3418 if (!valid_xml_element((xmlChar
*)dst
)) {
3420 return GPG_ERR_INV_VALUE
;
3423 if (strchr(req
[0], '\t'))
3424 src
= split_input_line(req
[0], "\t", -1);
3426 src
= split_input_line(req
[0], " ", -1);
3428 if (!src
|| !*src
) {
3429 rc
= GPG_ERR_SYNTAX
;
3433 n
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, FALSE
);
3436 n
= find_elements(client
->doc
, n
->children
, src
+1, &rc
, NULL
, NULL
,
3437 NULL
, FALSE
, 0, NULL
, FALSE
);
3443 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
3446 rc
= gpg_error_from_errno(ENOMEM
);
3450 /* To prevent unwanted effects:
3452 * <root name="a"><b/></root>
3456 if (xmlStrEqual(a
, (xmlChar
*)dst
)) {
3458 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3468 for (p
= src
; *p
; p
++) {
3472 strv_printf(&tmp
, "%s", *p
);
3476 strv_printf(&tmp
, "!%s", dst
);
3477 ndst
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, FALSE
);
3479 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
3485 ndst
= find_elements(client
->doc
, ndst
->children
, tmp
+1, &rc
, NULL
,
3486 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3490 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3495 /* Target may exist:
3498 * <root name="b" target="a"/>
3506 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3511 xmlUnlinkNode(ndst
);
3512 xmlFreeNodeList(ndst
);
3515 rc
= add_attribute(n
, "_name", dst
);
3520 return send_error(ctx
, rc
);
3523 static gint
copy_command(assuan_context_t ctx
, gchar
*line
)
3525 struct client_s
*client
= assuan_get_pointer(ctx
);
3527 gchar
**req
, **src
= NULL
, **dst
= NULL
;
3528 xmlNodePtr nsrc
, ndst
, new;
3530 log_write2("ARGS=\"%s\"", line
);
3531 req
= split_input_line(line
, " ", -1);
3533 if (!req
|| !req
[0] || !req
[1]) {
3535 return send_error(ctx
, GPG_ERR_SYNTAX
);
3538 if (strchr(req
[0], '\t'))
3539 src
= split_input_line(req
[0], "\t", -1);
3541 src
= split_input_line(req
[0], " ", -1);
3543 if (!src
|| !*src
) {
3544 rc
= GPG_ERR_SYNTAX
;
3548 if (strchr(req
[1], '\t'))
3549 dst
= split_input_line(req
[1], "\t", -1);
3551 dst
= split_input_line(req
[1], " ", -1);
3553 if (!dst
|| !*dst
) {
3554 rc
= GPG_ERR_SYNTAX
;
3558 nsrc
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, FALSE
);
3561 nsrc
= find_elements(client
->doc
, nsrc
->children
, src
+1, &rc
, NULL
,
3562 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3567 ndst
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, FALSE
);
3570 ndst
= find_elements(client
->doc
, ndst
->children
, dst
+1, &rc
, NULL
,
3571 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3573 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3576 new = xmlCopyNodeList(nsrc
);
3579 rc
= GPG_ERR_ENOMEM
;
3584 ndst
= create_element_path(client
, &dst
, &rc
);
3588 xmlFreeNodeList(new);
3592 /* Merge any attributes from the src node to the initial dst node. */
3593 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
) {
3594 if ((!src
[1] || !dst
[1]) && xmlStrEqual(attr
->name
, (xmlChar
*)"_name"))
3597 xmlAttrPtr a
= xmlHasProp(ndst
, attr
->name
);
3602 xmlChar
*tmp
= xmlNodeGetContent(attr
->children
);
3603 xmlNewProp(ndst
, attr
->name
, tmp
);
3607 xmlNodePtr n
= ndst
->children
;
3610 ndst
->children
= NULL
;
3612 if (!new->children
) {
3614 xmlFreeNodeList(new);
3618 n
= xmlCopyNodeList(new->children
);
3621 rc
= GPG_ERR_ENOMEM
;
3626 xmlFreeNodeList(new);
3627 n
= xmlAddChildList(ndst
, n
);
3630 rc
= GPG_ERR_ENOMEM
;
3644 return send_error(ctx
, rc
);
3647 static gint
move_command(assuan_context_t ctx
, gchar
*line
)
3649 struct client_s
*client
= assuan_get_pointer(ctx
);
3651 gchar
**req
, **src
= NULL
, **dst
= NULL
;
3652 xmlNodePtr nsrc
, ndst
= NULL
;
3654 log_write2("ARGS=\"%s\"", line
);
3655 req
= split_input_line(line
, " ", -1);
3657 if (!req
|| !req
[0] || !req
[1]) {
3659 return send_error(ctx
, GPG_ERR_SYNTAX
);
3662 if (strchr(req
[0], '\t'))
3663 src
= split_input_line(req
[0], "\t", -1);
3665 src
= split_input_line(req
[0], " ", -1);
3667 if (!src
|| !*src
) {
3668 rc
= GPG_ERR_SYNTAX
;
3672 if (strchr(req
[1], '\t'))
3673 dst
= split_input_line(req
[1], "\t", -1);
3675 dst
= split_input_line(req
[1], " ", -1);
3677 nsrc
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, FALSE
);
3680 nsrc
= find_elements(client
->doc
, nsrc
->children
, src
+1, &rc
, NULL
,
3681 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3687 ndst
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, FALSE
);
3690 ndst
= find_elements(client
->doc
, ndst
->children
, dst
+1, &rc
, NULL
,
3691 NULL
, NULL
, FALSE
, 0, NULL
, FALSE
);
3694 ndst
= xmlDocGetRootElement(client
->doc
);
3696 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
) {
3698 rc
= GPG_ERR_CONFLICT
;
3703 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3709 xmlChar
*a
= node_has_attribute(nsrc
, (xmlChar
*)"_name");
3710 xmlNodePtr dup
= find_element(ndst
->children
, (gchar
*)a
, NULL
);
3718 if (ndst
== xmlDocGetRootElement(client
->doc
)) {
3719 xmlNodePtr n
= nsrc
;
3720 gboolean match
= FALSE
;
3722 while (n
->parent
&& n
->parent
!= ndst
)
3725 xmlChar
*a
= node_has_attribute(n
, (xmlChar
*)"_name");
3726 xmlChar
*b
= node_has_attribute(nsrc
, (xmlChar
*)"_name");
3728 if (xmlStrEqual(a
, b
)) {
3730 xmlUnlinkNode(nsrc
);
3740 xmlFreeNodeList(dup
);
3749 ndst
= create_element_path(client
, &dst
, &rc
);
3754 xmlUnlinkNode(nsrc
);
3755 ndst
= xmlAddChildList(ndst
, nsrc
);
3758 rc
= GPG_ERR_ENOMEM
;
3770 return send_error(ctx
, rc
);
3773 static int ls_command(assuan_context_t ctx
, gchar
*line
)
3775 log_write2("ARGS=\"%s\"", line
);
3777 gchar
*tmp
= g_key_file_get_string(keyfileh
, "global", "data_directory", NULL
);
3778 gchar
*dir
= expand_homedir(tmp
);
3779 DIR *d
= opendir(dir
);
3786 return send_syserror(ctx
, n
);
3789 size_t len
= offsetof(struct dirent
, d_name
)+pathconf(dir
, _PC_NAME_MAX
)+1;
3790 struct dirent
*p
= g_malloc(len
), *cur
= NULL
;
3796 while (!readdir_r(d
, p
, &cur
) && cur
) {
3797 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3799 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.' && cur
->d_name
[2] == '\0')
3802 tmp
= g_strdup_printf("%s%s\n", list
? list
: "", cur
->d_name
);
3808 rc
= GPG_ERR_ENOMEM
;
3820 return send_error(ctx
, rc
);
3823 return send_error(ctx
, GPG_ERR_NO_VALUE
);
3825 list
[strlen(list
)-1] = 0;
3826 rc
= xfer_data(ctx
, list
, strlen(list
));
3828 return send_error(ctx
, rc
);
3831 static void bye_notify(assuan_context_t ctx
)
3833 struct client_s
*cl
= assuan_get_pointer(ctx
);
3835 /* This will let assuan_process_next() return. */
3836 fcntl(cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3839 static void reset_notify(assuan_context_t ctx
)
3841 struct client_s
*cl
= assuan_get_pointer(ctx
);
3848 * This is called before every Assuan command.
3850 gint
command_startup(assuan_context_t ctx
, const gchar
*name
)
3852 struct client_s
*cl
= assuan_get_pointer(ctx
);
3855 log_write1("%s", name
);
3857 if (!g_ascii_strcasecmp(name
, "ISCACHED") ||
3858 !g_ascii_strcasecmp(name
, "CLEARCACHE") ||
3859 !g_ascii_strcasecmp(name
, "CACHETIMEOUT") ||
3860 !g_ascii_strcasecmp(name
, "GETCONFIG") ||
3861 !g_ascii_strcasecmp(name
, "GETPID") ||
3862 !g_ascii_strcasecmp(name
, "VERSION") ||
3863 !g_ascii_strcasecmp(name
, "SET") ||
3864 !g_ascii_strcasecmp(name
, "BYE") ||
3865 !g_ascii_strcasecmp(name
, "NOP") ||
3866 !g_ascii_strcasecmp(name
, "CANCEL") ||
3867 !g_ascii_strcasecmp(name
, "RESET") ||
3868 !g_ascii_strcasecmp(name
, "END") ||
3869 !g_ascii_strcasecmp(name
, "HELP") ||
3870 !g_ascii_strcasecmp(name
, "OPTION") ||
3871 !g_ascii_strcasecmp(name
, "INPUT") ||
3872 !g_ascii_strcasecmp(name
, "OUTPUT") ||
3873 !g_ascii_strcasecmp(name
, "LS") ||
3874 !g_ascii_strcasecmp(name
, "UNSET"))
3877 cl
->last_rc
= rc
= file_modified(cl
);
3880 if ((rc
== EPWMD_NO_FILE
|| rc
== EPWMD_FILE_MODIFIED
) &&
3881 !g_ascii_strcasecmp(name
, "OPEN"))
3889 * This is called after every Assuan command.
3891 void command_finalize(assuan_context_t ctx
, gint rc
)
3893 struct client_s
*client
= assuan_get_pointer(ctx
);
3895 if (!client
->is_lock_cmd
)
3896 unlock_file_mutex(client
);
3898 log_write1(N_("command completed (rc=%u)"), client
->last_rc
);
3901 gpg_error_t
register_commands(assuan_context_t ctx
)
3905 gint (*handler
)(assuan_context_t
, gchar
*line
);
3907 { "OPEN", open_command
},
3908 { "SAVE", save_command
},
3909 { "LIST", list_command
},
3910 { "REALPATH", realpath_command
},
3911 { "STORE", store_command
},
3912 { "DELETE", delete_command
},
3913 { "GET", get_command
},
3914 { "ATTR", attr_command
},
3915 { "ISCACHED", iscached_command
},
3916 { "CLEARCACHE", clearcache_command
},
3917 { "CACHETIMEOUT", cachetimeout_command
},
3918 { "GETCONFIG", getconfig_command
},
3919 { "DUMP", dump_command
},
3920 { "XPATH", xpath_command
},
3921 { "XPATHATTR", xpathattr_command
},
3922 { "IMPORT", import_command
},
3923 { "LOCK", lock_command
},
3924 { "UNLOCK", unlock_command
},
3925 { "GETPID", getpid_command
},
3926 { "VERSION", version_command
},
3927 { "SET", set_command
},
3928 { "UNSET", unset_command
},
3929 { "RENAME", rename_command
},
3930 { "COPY", copy_command
},
3931 { "LS", ls_command
},
3932 { "MOVE", move_command
},
3939 for (i
=0; table
[i
].name
; i
++) {
3940 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
3946 rc
= assuan_register_bye_notify(ctx
, bye_notify
);
3951 rc
= assuan_register_reset_notify(ctx
, reset_notify
);
3956 rc
= assuan_register_pre_cmd_notify(ctx
, command_startup
);
3961 return assuan_register_post_cmd_notify(ctx
, command_finalize
);
3964 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
,
3965 struct crypto_s
*crypto
, gpointer
*dst
, goffset
*dst_len
)
3967 goffset insize
, len
;
3968 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
3969 guint64 iter
= 0ULL, n_iter
= 0ULL, iter_progress
= 0ULL;
3973 gsize fh_size
= crypto
->fh
->v1
? sizeof(crypto
->fh
->ver
.fh1
) : sizeof(crypto
->fh
->ver
.fh2
);
3974 guint64 fh_iter
= crypto
->fh
->v1
? crypto
->fh
->ver
.fh1
.iter
: crypto
->fh
->ver
.fh2
.iter
;
3975 gsize hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
3977 lseek(crypto
->fh
->fd
, fh_size
, SEEK_SET
);
3978 insize
= crypto
->fh
->st
.st_size
- fh_size
;
3979 crypto
->iv
= gcry_malloc(crypto
->blocksize
);
3982 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3983 return gpg_error_from_errno(ENOMEM
);
3987 memcpy(crypto
->iv
, crypto
->fh
->ver
.fh1
.iv
, crypto
->blocksize
);
3989 memcpy(crypto
->iv
, crypto
->fh
->ver
.fh2
.iv
, crypto
->blocksize
);
3991 crypto
->inbuf
= gcry_malloc(insize
);
3993 if (!crypto
->inbuf
) {
3994 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3995 return gpg_error_from_errno(ENOMEM
);
3998 crypto
->insize
= insize
;
3999 len
= pth_read(crypto
->fh
->fd
, crypto
->inbuf
, crypto
->insize
);
4001 if (len
!= crypto
->insize
)
4002 return GPG_ERR_INV_LENGTH
;
4004 /* No encryption iterations. This is a plain (gzipped) file. */
4005 if ((crypto
->fh
->v1
&& (long)fh_iter
< 0L) ||
4006 (!crypto
->fh
->v1
&& fh_iter
<= 0L)) {
4008 * cache_file_count() needs both .used == TRUE and a valid key in
4009 * order for it to count as a used cache entry. Fixes CACHE status
4012 memset(crypto
->key
, '!', hashlen
);
4016 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->iv
, crypto
->blocksize
))) {
4017 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4021 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->key
, crypto
->keysize
))) {
4022 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4026 iter_progress
= (guint64
)get_key_file_double(client
&& client
->filename
?
4027 client
->filename
: "global", "iteration_progress");
4029 if (iter_progress
> 0ULL && fh_iter
>= iter_progress
) {
4030 rc
= send_status(ctx
, STATUS_DECRYPT
, "0 %llu", fh_iter
);
4036 rc
= iterate_crypto_once(client
, crypto
, STATUS_DECRYPT
);
4041 crypto
->tkey
= gcry_malloc(hashlen
);
4043 if (!crypto
->tkey
) {
4044 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
4045 return gpg_error_from_errno(ENOMEM
);
4048 memcpy(crypto
->tkey
, crypto
->key
, hashlen
);
4049 guchar
*tkey
= crypto
->tkey
;
4052 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->tkey
, crypto
->keysize
))) {
4053 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4057 while (iter
< (crypto
->fh
->v1
? fh_iter
: fh_iter
-1)) {
4058 if (iter_progress
> 0ULL && iter
>= iter_progress
) {
4059 if (!(iter
% iter_progress
)) {
4060 rc
= send_status(ctx
, STATUS_DECRYPT
, "%llu %llu",
4061 ++n_iter
* iter_progress
, fh_iter
);
4068 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->iv
, crypto
->blocksize
))) {
4069 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4073 rc
= iterate_crypto_once(client
, crypto
, STATUS_DECRYPT
);
4076 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
4083 if (iter_progress
&& fh_iter
>= iter_progress
) {
4084 rc
= send_status(ctx
, STATUS_DECRYPT
, "%llu %llu", fh_iter
, fh_iter
);
4091 if (do_decompress(ctx
, crypto
->inbuf
, crypto
->insize
,
4092 (gpointer
*)&crypto
->outbuf
, &outsize
, &zrc
) == FALSE
) {
4093 if (zrc
== Z_MEM_ERROR
)
4094 return gpg_error_from_errno(ENOMEM
);
4096 return GPG_ERR_INV_PASSPHRASE
; // Not a valid gzip header. Must be a bad key.
4099 if (g_strncasecmp(crypto
->outbuf
, "<?xml ", 6) != 0) {
4100 gcry_free(crypto
->outbuf
);
4101 crypto
->outbuf
= NULL
;
4102 return GPG_ERR_INV_PASSPHRASE
;
4106 client
->xml
= crypto
->outbuf
;
4107 client
->len
= outsize
;
4108 crypto
->outbuf
= NULL
;
4111 *dst
= crypto
->outbuf
;
4113 crypto
->outbuf
= NULL
;
4116 /* The calling function should free the crypto struct. */