1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2009 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
48 #include "pwmd_error.h"
61 static void *z_alloc(void *data
, unsigned items
, unsigned size
)
63 return gcry_calloc(items
, size
);
66 static void z_free(void *data
, void *p
)
71 static gpg_error_t
file_modified(struct client_s
*client
)
76 if (client
->state
!= STATE_OPEN
)
79 rc
= lock_file_mutex(client
);
84 if (lstat(client
->filename
, &st
) == 0 && client
->mtime
) {
85 if (client
->mtime
!= st
.st_mtime
)
86 return EPWMD_FILE_MODIFIED
;
93 static gpg_error_t
parse_xml(assuan_context_t ctx
)
95 struct client_s
*client
= assuan_get_pointer(ctx
);
97 client
->doc
= xmlReadMemory(client
->xml
, client
->len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
100 return EPWMD_LIBXML_ERROR
;
105 void unlock_file_mutex(struct client_s
*client
)
110 if (client
->has_lock
== FALSE
|| client
->pinentry
->status
!= PINENTRY_NONE
)
112 if (client
->has_lock
== FALSE
)
116 CACHE_LOCK(client
->ctx
);
118 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
125 client
->has_lock
= client
->is_lock_cmd
= FALSE
;
128 gpg_error_t
lock_file_mutex(struct client_s
*client
)
133 if (client
->has_lock
== TRUE
)
136 CACHE_LOCK(client
->ctx
);
138 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
144 MUTEX_TRYLOCK(client
->ctx
, m
, rc
);
147 client
->has_lock
= TRUE
;
152 void free_client(struct client_s
*client
)
155 xmlFreeDoc(client
->doc
);
158 gcry_free(client
->xml
);
160 if (client
->filename
)
161 g_free(client
->filename
);
164 cleanup_crypto(&client
->crypto
);
166 if (client
->xml_error
)
167 xmlResetError(client
->xml_error
);
170 void cleanup_client(struct client_s
*client
)
172 assuan_context_t ctx
= client
->ctx
;
173 struct client_thread_s
*thd
= client
->thd
;
174 gboolean has_lock
= client
->has_lock
;
176 struct pinentry_s
*pin
= client
->pinentry
;
179 unlock_file_mutex(client
);
180 CACHE_LOCK(client
->ctx
);
181 cache_decr_refcount(client
->md5file
);
184 * This may be a new file so don't use a cache slot. save_command() will
185 * set this to FALSE on success.
187 if (client
->new == TRUE
)
188 cache_clear(client
->md5file
, 1);
192 memset(client
, 0, sizeof(struct client_s
));
193 client
->state
= STATE_CONNECTED
;
196 client
->freed
= TRUE
;
198 client
->pinentry
= pin
;
200 client
->has_lock
= has_lock
;
203 static void gz_cleanup(void *arg
)
205 struct gz_s
**gz
= (struct gz_s
**)arg
;
210 if (!(*gz
)->done
&& (*gz
)->out
)
211 gcry_free((*gz
)->out
);
213 if ((*gz
)->which
== STATUS_COMPRESS
) {
215 deflateEnd(&(*gz
)->z
);
219 inflateEnd(&(*gz
)->z
);
226 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gulong insize
,
227 gpointer
*out
, gulong
*outsize
, gint
*rc
)
233 gz
= g_malloc0(sizeof(struct gz_s
));
236 *rc
= gpg_error_from_errno(ENOMEM
);
240 pth_cleanup_push(gz_cleanup
, &gz
);
241 gz
->which
= STATUS_DECOMPRESS
;
242 gz
->z
.zalloc
= z_alloc
;
243 gz
->z
.zfree
= z_free
;
245 gz
->z
.avail_in
= (uInt
)insize
;
246 gz
->z
.avail_out
= zlib_bufsize
;
247 gz
->z
.next_out
= gz
->out
= gcry_malloc(zlib_bufsize
);
250 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
256 *rc
= inflateInit2(&gz
->z
, 47);
259 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
264 memset(&h
, 0, sizeof(gz_header
));
265 h
.comment
= (guchar
*)buf
;
266 h
.comm_max
= sizeof(buf
);
267 *rc
= inflateGetHeader(&gz
->z
, &h
);
270 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
275 *rc
= inflate(&gz
->z
, Z_BLOCK
);
278 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
284 insize
= (gulong
)strtol((gchar
*)h
.comment
, NULL
, 10);
289 *rc
= inflate(&gz
->z
, Z_FINISH
);
295 if (!gz
->z
.avail_out
) {
296 p
= gcry_realloc(gz
->out
, gz
->z
.total_out
+ zlib_bufsize
);
299 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
305 gz
->z
.next_out
= (guchar
*)gz
->out
+ gz
->z
.total_out
;
306 gz
->z
.avail_out
= zlib_bufsize
;
307 *rc
= send_status(ctx
, STATUS_DECOMPRESS
, "%li %li",
308 gz
->z
.total_out
, insize
);
320 } while (*rc
!= Z_STREAM_END
);
322 *rc
= send_status(ctx
, STATUS_DECOMPRESS
, "%li %li", gz
->z
.total_out
,
329 *outsize
= gz
->z
.total_out
;
336 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
341 file_header_internal_t
*read_file_header(const gchar
*filename
, gboolean v1
,
346 file_header_internal_t
*fh
= g_malloc0(sizeof(file_header_internal_t
));
353 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
354 *rc
= gpg_error_from_errno(ENOMEM
);
358 pth_cleanup_push(g_free
, fh
);
359 fh_size
= v1
? sizeof(fh
->ver
.fh1
) : sizeof(fh
->ver
.fh2
);
361 if (lstat(filename
, &fh
->st
) == -1) {
362 *rc
= gpg_error_from_syserror();
367 if (!S_ISREG(fh
->st
.st_mode
)) {
368 *rc
= GPG_ERR_ENOANO
;
373 fd
= open(filename
, O_RDONLY
);
376 *rc
= gpg_error_from_errno(errno
);
381 pth_cleanup_push(cleanup_fd_cb
, &fd
);
382 p
= v1
? (void *)&fh
->ver
.fh1
: (void *)&fh
->ver
.fh2
;
383 len
= pth_read(fd
, p
, fh_size
);
385 if (len
!= fh_size
) {
389 *rc
= gpg_error_from_errno(n
);
400 static gpg_error_t
open_command_finalize(assuan_context_t ctx
, guchar
*key
,
403 struct client_s
*client
= assuan_get_pointer(ctx
);
408 if (!client
->crypto
->fh
) {
415 rc
= init_client_crypto2(client
->filename
, client
->crypto
);
418 cleanup_client(client
);
419 return send_error(ctx
, rc
);
422 rc
= try_xml_decrypt(ctx
, key
, client
->crypto
, NULL
, NULL
);
425 cleanup_client(client
);
426 return send_error(ctx
, rc
);
430 CACHE_LOCK(client
->ctx
);
432 if (cached
== FALSE
) {
433 if (cache_update_key(client
->md5file
, key
) == FALSE
) {
434 cleanup_client(client
);
436 return send_syserror(ctx
, ENOMEM
);
439 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
440 cache_reset_timeout(client
->md5file
, timeout
);
443 cache_set_timeout(client
->md5file
, -2);
451 gcry_free(client
->xml
);
456 if (client
->new == FALSE
)
457 send_status_all(STATUS_CACHE
);
459 client
->state
= STATE_OPEN
;
462 if (!rc
&& client
->new == FALSE
&&
463 client
->crypto
->fh
->ver
.fh2
.iter
!= (guint64
)get_key_file_double(client
->filename
, "iterations")) {
464 MUTEX_LOCK(&rcfile_mutex
);
465 g_key_file_set_double(keyfileh
, client
->filename
, "iterations",
466 client
->crypto
->fh
->ver
.fh2
.iter
);
467 MUTEX_UNLOCK(&rcfile_mutex
);
468 send_status_all(STATUS_CONFIG
);
471 cleanup_crypto(&client
->crypto
);
472 return send_error(ctx
, rc
);
475 static void req_cleanup(void *arg
)
480 g_strfreev((gchar
**)arg
);
483 static gint
open_command(assuan_context_t ctx
, gchar
*line
)
485 gboolean cached
= FALSE
;
487 struct client_s
*client
= assuan_get_pointer(ctx
);
489 gchar
*filename
= NULL
;
490 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
492 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
495 pth_cleanup_push(req_cleanup
, req
);
497 if (!filename
|| !*filename
) {
499 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
502 log_write2("ARGS=\"%s\" %s", filename
, req
[1] ? "<passphrase>" : "");
504 if (valid_filename(filename
) == FALSE
) {
506 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
509 if (client
->state
== STATE_OPEN
)
510 cleanup_client(client
);
512 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
513 CACHE_LOCK(client
->ctx
);
515 if (cache_has_file(client
->md5file
) == FALSE
) {
516 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
519 return send_syserror(ctx
, ENOMEM
);
523 cache_incr_refcount(client
->md5file
);
525 rc
= lock_file_mutex(client
);
529 return send_error(ctx
, rc
);
532 client
->freed
= FALSE
;
533 client
->crypto
= init_client_crypto();
535 if (!client
->crypto
) {
537 cleanup_client(client
);
538 return send_syserror(ctx
, ENOMEM
);
541 client
->crypto
->key
= gcry_malloc(hashlen
);
543 if (!client
->crypto
->key
) {
545 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
,
546 gpg_error_from_errno(ENOMEM
));
547 cleanup_client(client
);
548 return send_syserror(ctx
, ENOMEM
);
551 memset(client
->crypto
->key
, 0, hashlen
);
552 client
->crypto
->fh
= read_file_header(filename
, FALSE
, &rc
);
554 if (!client
->crypto
->fh
) {
555 if (gpg_err_code_to_errno(rc
) != ENOENT
) {
556 log_write("%s: %s", filename
, pwmd_strerror(rc
));
558 cleanup_client(client
);
559 return send_error(ctx
, rc
);
563 * New files don't need a key.
565 if ((client
->xml
= new_document()) == NULL
) {
566 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
568 cleanup_client(client
);
569 return send_syserror(ctx
, ENOMEM
);
572 client
->len
= xmlStrlen(client
->xml
);
574 client
->filename
= g_strdup(filename
);
576 if (!client
->filename
) {
578 cleanup_client(client
);
579 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
580 return send_syserror(ctx
, ENOMEM
);
583 if (req
[1] && *req
[1])
584 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, req
[1],
589 client
->pinentry
->filename
= g_strdup(client
->filename
);
591 if (!client
->pinentry
->filename
) {
592 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
593 cleanup_client(client
);
594 return send_syserror(ctx
, ENOMEM
);
597 return open_command_finalize(ctx
, client
->crypto
->key
, cached
);
600 client
->mtime
= client
->crypto
->fh
->st
.st_mtime
;
602 client
->filename
= g_strdup(filename
);
604 if (!client
->filename
) {
605 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
607 cleanup_client(client
);
608 return send_syserror(ctx
, ENOMEM
);
612 client
->pinentry
->filename
= g_strdup(client
->filename
);
614 if (!client
->pinentry
->filename
) {
615 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
617 cleanup_client(client
);
618 return send_syserror(ctx
, ENOMEM
);
622 if (client
->crypto
->fh
->ver
.fh2
.iter
<= 0ULL)
625 CACHE_LOCK(client
->ctx
);
626 cached
= cache_get_key(client
->md5file
, client
->crypto
->key
);
629 if (cached
== FALSE
) {
630 gchar
*tmp
= get_key_file_string(filename
, "key_file");
635 cleanup_client(client
);
636 return send_error(ctx
, GPG_ERR_WRONG_KEY_USAGE
);
640 * No key specified and no matching filename found in the cache. Use
641 * pinentry to retrieve the key. Cannot return assuan_process_done()
642 * here otherwise the command will be interrupted. The event loop in
643 * client_thread() will poll the file descriptor waiting for it to
644 * become ready to read a pinentry_key_s which will contain the
645 * entered key or an error code. It will then call
646 * open_command_finalize() to to finish the command.
648 if (!req
[1] || !*req
[1]) {
650 gboolean b
= get_key_file_boolean(filename
, "enable_pinentry");
652 /* From set_pinentry_defaults(). */
653 if (client
->pinentry
->enable
== FALSE
||
654 (client
->pinentry
->enable
== -1 && b
== FALSE
)) {
655 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
660 rc
= lock_pin_mutex(client
);
663 unlock_pin_mutex(client
->pinentry
);
664 cleanup_client(client
);
665 return send_error(ctx
, rc
);
668 client
->pinentry
->which
= PINENTRY_OPEN
;
669 rc
= pinentry_fork(ctx
);
672 unlock_pin_mutex(client
->pinentry
);
673 cleanup_client(client
);
674 return send_error(ctx
, rc
);
677 // Called from pinentry iterate.
678 client
->pinentry
->cb
= open_command_finalize
;
679 client
->pinentry
->status
= PINENTRY_INIT
;
682 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
687 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, req
[1],
690 else if (req
&& req
[1] && *req
[1]) {
691 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, req
[1],
692 strlen(req
[1]) ? strlen(req
[1]) : 1);
697 return open_command_finalize(ctx
, client
->crypto
->key
, cached
);
700 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
701 gulong size
, gpointer
*out
, gulong
*outsize
, gint
*rc
)
706 gint cmd
= Z_NO_FLUSH
;
708 gz
= g_malloc0(sizeof(struct gz_s
));
711 *rc
= gpg_error_from_errno(ENOMEM
);
715 pth_cleanup_push(gz_cleanup
, &gz
);
716 gz
->which
= STATUS_COMPRESS
;
717 gz
->z
.zalloc
= z_alloc
;
718 gz
->z
.zfree
= z_free
;
719 gz
->z
.next_in
= data
;
720 gz
->z
.avail_in
= size
< zlib_bufsize
? (uInt
)size
: (uInt
)zlib_bufsize
;
721 gz
->z
.avail_out
= (uInt
)zlib_bufsize
;
722 gz
->z
.next_out
= gz
->out
= gcry_malloc(zlib_bufsize
);
725 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
731 *rc
= deflateInit2(&gz
->z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
734 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
739 /* Rather than store the size of the uncompressed data in the file header,
740 * store it in the comment field of the gzip header. Don't give anyone too
741 * much information. Not sure why really, but it seems the right way. :)
743 memset(&h
, 0, sizeof(gz_header
));
744 g_snprintf(buf
, sizeof(buf
), "%li", size
);
745 h
.comment
= (guchar
*)buf
;
746 *rc
= deflateSetHeader(&gz
->z
, &h
);
749 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
757 *rc
= deflate(&gz
->z
, cmd
);
763 if (!gz
->z
.avail_out
) {
764 p
= gcry_realloc(gz
->out
, gz
->z
.total_out
+ zlib_bufsize
);
767 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
773 gz
->z
.next_out
= (guchar
*)gz
->out
+ gz
->z
.total_out
;
774 gz
->z
.avail_out
= zlib_bufsize
;
777 if (!gz
->z
.avail_in
&& gz
->z
.total_in
< size
) {
778 if (gz
->z
.total_in
+ zlib_bufsize
> size
)
779 gz
->z
.avail_in
= size
- gz
->z
.total_in
;
781 gz
->z
.avail_in
= zlib_bufsize
;
783 *rc
= send_status(ctx
, STATUS_COMPRESS
, "%li %li",
784 gz
->z
.total_in
, size
);
790 if (gz
->z
.total_in
>= size
)
799 } while (*rc
!= Z_STREAM_END
);
801 *rc
= send_status(ctx
, STATUS_COMPRESS
, "%li %li", gz
->z
.total_in
, size
);
807 *outsize
= gz
->z
.total_out
;
814 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gz
->z
.msg
);
819 #define CRYPTO_BLOCKSIZE(c) (c->blocksize * 1024)
821 static gpg_error_t
iterate_crypto_once(struct client_s
*client
,
822 struct client_crypto_s
*crypto
, status_msg_t which
)
825 goffset len
= CRYPTO_BLOCKSIZE(crypto
);
826 gpointer p
= gcry_malloc(len
);
831 return gpg_err_code_from_errno(ENOMEM
);
833 if (crypto
->insize
< CRYPTO_BLOCKSIZE(crypto
))
834 len
= crypto
->insize
;
836 pth_cleanup_push(gcry_free
, p
);
839 inbuf
= (guchar
*)crypto
->inbuf
+ total
;
842 if (len
+ total
> crypto
->insize
)
843 len
= crypto
->blocksize
;
845 if (which
== STATUS_ENCRYPT
)
846 rc
= gcry_cipher_encrypt(crypto
->gh
, p
, len
, inbuf
, len
);
848 rc
= gcry_cipher_decrypt(crypto
->gh
, p
, len
, inbuf
, len
);
853 tmp
= (guchar
*)crypto
->inbuf
+ total
;
854 memmove(tmp
, p
, len
);
857 if (total
>= crypto
->insize
)
868 /* The crypto struct must be setup for iterations and .key. */
869 gpg_error_t
do_xml_encrypt(struct client_s
*client
,
870 struct client_crypto_s
*crypto
, const gchar
*filename
)
872 goffset len
= crypto
->insize
;
876 guint64 iter_progress
= 0ULL, n_iter
= 0ULL, xiter
= 0ULL;
877 gchar tmp
[FILENAME_MAX
];
880 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
882 if (!crypto
->fh
->ver
.fh2
.iter
) {
884 * cache_file_count() needs both .used == TRUE and a valid key in
885 * order for it to count as a used cache entry. Fixes CACHE status
888 memset(crypto
->key
, '!', hashlen
);
893 * Resize the existing xml buffer to the block size required by gcrypt
894 * rather than duplicating it and wasting memory.
896 len
= (crypto
->insize
/ crypto
->blocksize
) * crypto
->blocksize
;
898 if (crypto
->insize
% crypto
->blocksize
)
899 len
+= crypto
->blocksize
;
901 inbuf
= gcry_realloc(crypto
->inbuf
, len
);
904 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
905 return gpg_error_from_errno(ENOMEM
);
908 crypto
->inbuf
= inbuf
;
909 crypto
->insize
= len
;
910 gcry_create_nonce(crypto
->fh
->ver
.fh2
.iv
, crypto
->blocksize
);
911 crypto
->tkey
= gcry_malloc(hashlen
);
914 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
915 return gpg_error_from_errno(ENOMEM
);
918 memcpy(crypto
->tkey
, crypto
->key
, hashlen
);
919 guchar
*tkey
= crypto
->tkey
;
922 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->tkey
, crypto
->keysize
))) {
923 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
927 iter_progress
= (guint64
)get_key_file_double(
928 client
? client
->filename
: "global", "iteration_progress");
930 if (iter_progress
&& crypto
->fh
->ver
.fh2
.iter
>= iter_progress
) {
931 rc
= send_status(client
? client
->ctx
: NULL
, STATUS_ENCRYPT
,
932 "0 %llu", crypto
->fh
->ver
.fh2
.iter
);
938 while (xiter
< crypto
->fh
->ver
.fh2
.iter
-1) {
939 if (iter_progress
> 0ULL && xiter
>= iter_progress
) {
940 if (!(xiter
% iter_progress
)) {
941 rc
= send_status(client
? client
->ctx
: NULL
, STATUS_ENCRYPT
,
942 "%llu %llu", ++n_iter
* iter_progress
,
943 crypto
->fh
->ver
.fh2
.iter
);
950 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->fh
->ver
.fh2
.iv
,
951 crypto
->blocksize
))) {
952 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
956 rc
= iterate_crypto_once(client
, crypto
, STATUS_ENCRYPT
);
959 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
966 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->fh
->ver
.fh2
.iv
,
967 crypto
->blocksize
))) {
968 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
972 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->key
, crypto
->keysize
))) {
973 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
977 rc
= iterate_crypto_once(client
, crypto
, STATUS_ENCRYPT
);
982 if (iter_progress
&& crypto
->fh
->ver
.fh2
.iter
>= iter_progress
) {
983 rc
= send_status(client
? client
->ctx
: NULL
, STATUS_ENCRYPT
,
984 "%llu %llu", crypto
->fh
->ver
.fh2
.iter
, crypto
->fh
->ver
.fh2
.iter
);
994 if (!client
&& !g_ascii_strcasecmp(filename
, "-")) {
995 crypto
->fh
->fd
= STDOUT_FILENO
;
999 if (lstat(filename
, &st
) == 0) {
1000 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
1002 if (!(mode
& S_IWUSR
))
1003 return gpg_error_from_errno(EACCES
);
1005 else if (errno
!= ENOENT
)
1006 return gpg_error_from_errno(errno
);
1008 g_snprintf(tmp
, sizeof(tmp
), "%s.XXXXXX", filename
);
1009 crypto
->fh
->fd
= mkstemp(tmp
);
1011 if (crypto
->fh
->fd
== -1) {
1013 p
= strrchr(tmp
, '/');
1015 log_write("%s: %s", p
, strerror(rc
));
1016 return gpg_error_from_errno(rc
);
1019 pth_cleanup_push(cleanup_unlink_cb
, tmp
);
1023 * xml_import() or convert_file() from command line.
1025 crypto
->fh
->fd
= STDOUT_FILENO
;
1028 crypto
->fh
->ver
.fh2
.magic
[0] = '\177';
1029 crypto
->fh
->ver
.fh2
.magic
[1] = 'P';
1030 crypto
->fh
->ver
.fh2
.magic
[2] = 'W';
1031 crypto
->fh
->ver
.fh2
.magic
[3] = 'M';
1032 crypto
->fh
->ver
.fh2
.magic
[4] = 'D';
1033 crypto
->fh
->ver
.fh2
.version
= VERSION_HEX
;
1034 len
= pth_write(crypto
->fh
->fd
, &crypto
->fh
->ver
.fh2
, sizeof(crypto
->fh
->ver
.fh2
));
1036 if (len
!= sizeof(crypto
->fh
->ver
.fh2
)) {
1042 return gpg_error_from_errno(len
);
1045 len
= pth_write(crypto
->fh
->fd
, crypto
->inbuf
, crypto
->insize
);
1047 if (len
!= crypto
->insize
) {
1053 return gpg_error_from_errno(len
);
1056 if (fsync(crypto
->fh
->fd
) == -1) {
1062 return gpg_error_from_errno(len
);
1069 if (mode
&& get_key_file_boolean(filename
, "backup") == TRUE
) {
1070 gchar tmp2
[FILENAME_MAX
];
1072 g_snprintf(tmp2
, sizeof(tmp2
), "%s.backup", filename
);
1074 acl
= acl_get_file(filename
, ACL_TYPE_ACCESS
);
1077 log_write("ACL: %s: %s", filename
, strerror(errno
));
1080 if (rename(filename
, tmp2
) == -1) {
1087 return gpg_error_from_errno(len
);
1092 acl
= acl_get_file(".", ACL_TYPE_DEFAULT
);
1095 log_write("ACL: %s: %s", filename
, strerror(errno
));
1099 if (rename(tmp
, filename
) == -1) {
1106 return gpg_error_from_errno(len
);
1112 chmod(filename
, mode
);
1115 if (acl
&& acl_set_file(filename
, ACL_TYPE_ACCESS
, acl
))
1116 log_write("ACL: %s: %s", filename
, strerror(errno
));
1123 if (client
&& lstat(filename
, &st
) == 0)
1124 client
->mtime
= st
.st_mtime
;
1129 gpg_error_t
update_save_flags(const gchar
*filename
,
1130 struct client_crypto_s
*crypto
)
1137 crypto
->fh
= g_malloc0(sizeof(file_header_internal_t
));
1140 return GPG_ERR_ENOMEM
;
1143 rc
= init_client_crypto2(filename
, crypto
);
1148 iter
= (guint64
)get_key_file_double(filename
, "iterations");
1149 crypto
->fh
->ver
.fh2
.iter
= iter
< 0ULL ? 0ULL : iter
;
1153 static gpg_error_t
save_command_finalize(assuan_context_t ctx
, guchar
*key
,
1156 struct client_s
*client
= assuan_get_pointer(ctx
);
1158 gulong len
, outsize
= 0;
1165 if (client
->crypto
->key
&& client
->crypto
->key
!= key
)
1166 gcry_free(client
->crypto
->key
);
1168 client
->crypto
->key
= key
;
1169 rc
= update_timestamp(client
->doc
);
1172 return send_error(ctx
, rc
);
1174 xmlDocDumpFormatMemory(client
->doc
, (xmlChar
**)&xmlbuf
, (gint
*)&len
, 0);
1175 pth_cleanup_push(xmlFree
, xmlbuf
);
1176 iter
= (guint
)get_key_file_integer(client
->filename
, "compression_level");
1181 if (do_compress(ctx
, (gint
)iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zrc
)
1184 cleanup_crypto(&client
->crypto
);
1186 if (zrc
== Z_MEM_ERROR
)
1187 return send_syserror(ctx
, ENOMEM
);
1189 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
1197 client
->crypto
->inbuf
= xmlbuf
;
1198 client
->crypto
->insize
= len
;
1199 rc
= update_save_flags(client
->filename
, client
->crypto
);
1202 cleanup_crypto(&client
->crypto
);
1203 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(rc
));
1204 return send_error(ctx
, rc
);
1207 rc
= do_xml_encrypt(client
, client
->crypto
, client
->filename
);
1210 cleanup_crypto(&client
->crypto
);
1211 return send_error(ctx
, rc
);
1214 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
1215 CACHE_LOCK(client
->ctx
);
1218 cache_reset_timeout(client
->md5file
, timeout
);
1221 if (client
->new == TRUE
)
1222 send_status_all(STATUS_CACHE
);
1224 client
->new = FALSE
;
1225 cleanup_crypto(&client
->crypto
);
1226 return send_error(ctx
, 0);
1229 if (cache_update_key(client
->md5file
, client
->crypto
->key
) == FALSE
) {
1231 cleanup_crypto(&client
->crypto
);
1232 return send_syserror(ctx
, ENOMEM
);
1235 client
->new = FALSE
;
1236 cache_reset_timeout(client
->md5file
, timeout
);
1238 send_status_all(STATUS_CACHE
);
1239 cleanup_crypto(&client
->crypto
);
1240 return send_error(ctx
, 0);
1243 static gint
save_command(assuan_context_t ctx
, gchar
*line
)
1245 gboolean cached
= FALSE
;
1247 struct client_s
*client
= assuan_get_pointer(ctx
);
1248 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
1251 log_write2("ARGS=%s", "<passphrase>");
1253 if (lstat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1254 return send_syserror(ctx
, errno
);
1256 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
)) {
1257 log_write("%s: %s", client
->filename
, pwmd_strerror(GPG_ERR_ENOANO
));
1258 return send_error(ctx
, GPG_ERR_ENOANO
);
1262 cached
= cache_iscached(client
->md5file
);
1266 * If a cache entry doesn't exist for this file and the file has a
1267 * "key_file" or "key" parameter, then it's an error. The reason is that
1268 * cache expiration would be useless.
1270 if (cached
== FALSE
) {
1271 gchar
*tmp
= get_key_file_string(client
->filename
, "key_file");
1275 return send_error(ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1282 if (!client
->crypto
) {
1283 client
->crypto
= init_client_crypto();
1285 if (!client
->crypto
) {
1286 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1287 return send_syserror(ctx
, ENOMEM
);
1291 client
->crypto
->key
= gcry_malloc(hashlen
);
1293 if (!client
->crypto
->key
) {
1294 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1295 cleanup_crypto(&client
->crypto
);
1296 return send_syserror(ctx
, ENOMEM
);
1299 memset(client
->crypto
->key
, '!', hashlen
);
1301 if (get_key_file_double(client
->filename
, "iterations") <= 0ULL)
1304 if (!line
|| !*line
) {
1305 client
->crypto
->tkey
= gcry_malloc(hashlen
);
1307 if (!client
->crypto
->tkey
) {
1308 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1309 cleanup_crypto(&client
->crypto
);
1310 return send_syserror(ctx
, ENOMEM
);
1313 memset(client
->crypto
->tkey
, '!', hashlen
);
1316 if (cache_get_key(client
->md5file
, client
->crypto
->key
) == FALSE
||
1317 !memcmp(client
->crypto
->key
, client
->crypto
->tkey
, hashlen
)) {
1320 #ifdef WITH_PINENTRY
1323 if (client
->pinentry
->enable
== FALSE
||
1324 get_key_file_boolean(client
->filename
, "enable_pinentry") == FALSE
) {
1325 /* Empty keys are allowed. */
1326 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
1330 lock_pin_mutex(client
);
1331 client
->pinentry
->which
= PINENTRY_SAVE
;
1332 rc
= pinentry_fork(ctx
);
1335 unlock_pin_mutex(client
->pinentry
);
1336 return send_error(ctx
, rc
);
1339 client
->pinentry
->cb
= save_command_finalize
;
1340 client
->pinentry
->status
= PINENTRY_INIT
;
1343 /* Empty keys are allowed. */
1344 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, "", 1);
1354 gcry_md_hash_buffer(GCRY_MD_SHA256
, client
->crypto
->key
, line
,
1358 return save_command_finalize(ctx
, client
->crypto
->key
, cached
);
1361 static gint
delete_command(assuan_context_t ctx
, gchar
*line
)
1363 struct client_s
*client
= assuan_get_pointer(ctx
);
1368 log_write2("ARGS=\"%s\"", line
);
1370 if (strchr(line
, '\t'))
1371 req
= split_input_line(line
, "\t", -1);
1373 req
= split_input_line(line
, " ", -1);
1376 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1378 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1382 return send_error(ctx
, rc
);
1386 * No sub-node defined. Remove the entire node (account).
1395 return send_error(ctx
, 0);
1398 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1402 return send_error(ctx
, rc
);
1409 return send_error(ctx
, 0);
1413 * Don't return with assuan_process_done() here. This has been called from
1414 * assuan_process_next() and the command should be finished in
1417 static gint
store_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
1420 assuan_context_t ctx
= data
;
1421 struct client_s
*client
= assuan_get_pointer(ctx
);
1424 gpg_error_t rc
= file_modified(client
);
1426 if (assuan_rc
|| rc
) {
1429 return assuan_rc
? assuan_rc
: rc
;
1432 req
= split_input_line((gchar
*)line
, "\t", 0);
1436 return EPWMD_COMMAND_SYNTAX
;
1439 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1441 if (rc
&& rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1442 rc
= new_account(client
->doc
, *req
);
1459 create_elements_cb(n
, req
+1, &rc
, NULL
);
1461 find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1462 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1466 client
->inquire_status
= INQUIRE_DONE
;
1470 static gint
store_command(assuan_context_t ctx
, gchar
*line
)
1472 struct client_s
*client
= assuan_get_pointer(ctx
);
1475 rc
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1478 return send_error(ctx
, rc
);
1480 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1481 client
->inquire_status
= INQUIRE_BUSY
;
1485 static void *send_data_cb(void *arg
)
1487 struct assuan_cmd_s
*data
= arg
;
1491 pth_cancel_state(PTH_CANCEL_ENABLE
|PTH_CANCEL_ASYNCHRONOUS
, &old
);
1492 rc
= assuan_send_data(data
->ctx
, data
->line
, data
->line_len
);
1493 pth_cancel_state(old
, NULL
);
1494 pth_exit((void *)rc
);
1498 /* For every assuan command that needs to be sent to the client, a timeout is
1499 * needed to determine if the client lost the connection. The timeout is the
1500 * same as the "keepalive" configuration parameter or a default if unset.
1502 gpg_error_t
do_assuan_command(assuan_context_t ctx
,
1503 void *(*cb
)(void *data
), void *data
)
1505 pth_attr_t attr
= pth_attr_new();
1508 gint to
= get_key_file_integer("global", "keepalive");
1509 pth_event_t ev
, tev
;
1513 pth_attr_init(attr
);
1514 pth_attr_set(attr
, PTH_ATTR_JOINABLE
, TRUE
);
1515 tid
= pth_spawn(attr
, cb
, data
);
1517 pth_attr_destroy(attr
);
1520 log_write("%s(%i): pth_spawn(): %s", __FILE__
, __LINE__
,
1521 _gpg_strerror(gpg_error_from_errno(n
)));
1522 return gpg_error_from_errno(n
);
1525 pth_cleanup_push(cleanup_cancel_cb
, tid
);
1526 to
= to
<= 0 ? DEFAULT_KEEPALIVE_TO
: to
;
1527 ev
= pth_event(PTH_EVENT_TID
|PTH_UNTIL_TID_DEAD
, tid
);
1528 tev
= pth_event(PTH_EVENT_TIME
, pth_timeout(to
, 0));
1529 ev
= pth_event_concat(ev
, tev
, NULL
);
1530 pth_cleanup_push(cleanup_ev_cb
, ev
);
1533 st
= pth_event_status(ev
);
1535 if (st
== PTH_STATUS_FAILED
) {
1537 rc
= GPG_ERR_ASS_WRITE_ERROR
;
1539 else if (st
== PTH_STATUS_OCCURRED
)
1540 pth_join(tid
, (void **)&rc
);
1542 st
= pth_event_status(tev
);
1544 if (st
== PTH_STATUS_OCCURRED
) {
1546 rc
= GPG_ERR_ASS_WRITE_ERROR
;
1555 static gpg_error_t
xfer_data(assuan_context_t ctx
, const gchar
*line
,
1561 struct assuan_cmd_s data
;
1562 gint progress
= get_key_file_integer("global", "xfer_progress");
1565 progress
= progress
>0 ? (progress
/ASSUAN_LINELENGTH
)*ASSUAN_LINELENGTH
: 0;
1566 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1568 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1575 if (sent
+ to_send
> total
)
1576 to_send
= total
- sent
;
1578 data
.line
= flush
? NULL
: (gchar
*)line
+sent
;
1579 data
.line_len
= flush
? 0 : to_send
;
1580 rc
= do_assuan_command(ctx
, send_data_cb
, &data
);
1583 sent
+= flush
? 0 : to_send
;
1585 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1586 (sent
== total
&& flush
))
1587 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1589 if (!flush
&& !rc
&& sent
== total
) {
1594 } while (!rc
&& sent
< total
);
1599 static gint
get_command(assuan_context_t ctx
, gchar
*line
)
1601 struct client_s
*client
= assuan_get_pointer(ctx
);
1606 log_write2("ARGS=\"%s\"", line
);
1607 req
= split_input_line(line
, "\t", -1);
1609 if (!req
|| !*req
) {
1611 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1614 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1618 return send_error(ctx
, rc
);
1622 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1627 return send_error(ctx
, rc
);
1629 if (!n
|| !n
->children
)
1630 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1632 n
= find_text_node(n
->children
);
1634 if (!n
|| !n
->content
|| !*n
->content
)
1635 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1637 rc
= xfer_data(ctx
, (gchar
*)n
->content
, xmlStrlen(n
->content
));
1638 return send_error(ctx
, rc
);
1641 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**target
,
1642 gpg_error_t
*rc
, gchar
**req_orig
, void *data
)
1644 gchar
*path
= *(gchar
**)data
;
1645 gchar
*tmp
= NULL
, *result
;
1649 *(gchar
**)data
= NULL
;
1652 path
= g_strjoinv("\t", target
);
1655 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1656 *rc
= gpg_error_from_errno(ENOMEM
);
1661 tmp
= g_strjoinv("\t", req_orig
);
1665 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1666 *rc
= gpg_error_from_errno(ENOMEM
);
1672 result
= g_strdup_printf("%s\t%s", path
, tmp
);
1674 result
= g_strdup(path
);
1677 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1678 *rc
= gpg_error_from_errno(ENOMEM
);
1686 *(gchar
**)data
= result
;
1690 static void list_command_cleanup1(void *arg
);
1691 static gint
realpath_command(assuan_context_t ctx
, gchar
*line
)
1694 struct client_s
*client
= assuan_get_pointer(ctx
);
1702 log_write2("ARGS=\"%s\"", line
);
1704 if (strchr(line
, '\t') != NULL
) {
1705 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1706 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1709 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1710 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1713 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1717 return send_error(ctx
, rc
);
1720 rp
= g_strjoinv("\t", req
);
1724 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1725 return send_syserror(ctx
, ENOMEM
);
1729 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1730 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, &rp
);
1735 return send_error(ctx
, rc
);
1739 string
= g_string_new(rp
);
1744 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1745 return send_syserror(ctx
, ENOMEM
);
1749 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++) {
1750 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+1) && *(t
+1) != '!')) {
1751 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1756 pth_cleanup_push(list_command_cleanup1
, string
);
1757 rc
= xfer_data(ctx
, string
->str
, string
->len
);
1759 return send_error(ctx
, rc
);
1762 static void list_command_cleanup1(void *arg
)
1764 g_string_free((GString
*)arg
, TRUE
);
1767 static void list_command_cleanup2(void *arg
)
1769 struct element_list_s
*elements
= arg
;
1772 gint total
= g_slist_length(elements
->list
);
1775 for (i
= 0; i
< total
; i
++) {
1776 gchar
*tmp
= g_slist_nth_data(elements
->list
, i
);
1780 g_slist_free(elements
->list
);
1782 if (elements
->prefix
)
1783 g_free(elements
->prefix
);
1789 static gint
list_command(assuan_context_t ctx
, gchar
*line
)
1791 struct client_s
*client
= assuan_get_pointer(ctx
);
1793 struct element_list_s
*elements
= NULL
;
1796 if (disable_list_and_dump
== TRUE
)
1797 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1799 log_write2("ARGS=\"%s\"", line
);
1804 rc
= list_accounts(client
->doc
, &str
);
1807 return send_error(ctx
, rc
);
1809 pth_cleanup_push(list_command_cleanup1
, str
);
1810 rc
= xfer_data(ctx
, str
->str
, str
->len
);
1812 return send_error(ctx
, rc
);
1815 elements
= g_malloc0(sizeof(struct element_list_s
));
1818 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1819 rc
= gpg_err_code_from_errno(ENOMEM
);
1823 pth_cleanup_push(list_command_cleanup2
, elements
);
1824 rc
= create_path_list(client
->doc
, elements
, line
);
1830 gint total
= g_slist_length(elements
->list
);
1835 rc
= EPWMD_EMPTY_ELEMENT
;
1839 str
= g_string_new(NULL
);
1842 rc
= gpg_err_code_from_errno(ENOMEM
);
1843 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1847 for (i
= 0; i
< total
; i
++) {
1848 tmp
= g_slist_nth_data(elements
->list
, i
);
1849 g_string_append_printf(str
, "%s%s", tmp
, i
+1 == total
? "" : "\n");
1852 pth_cleanup_push(list_command_cleanup1
, str
);
1853 rc
= xfer_data(ctx
, str
->str
, str
->len
);
1857 rc
= EPWMD_EMPTY_ELEMENT
;
1861 return send_error(ctx
, rc
);
1865 * req[0] - element path
1867 static gint
attribute_list(assuan_context_t ctx
, gchar
**req
)
1869 struct client_s
*client
= assuan_get_pointer(ctx
);
1870 gchar
**attrlist
= NULL
;
1872 gchar
**path
= NULL
;
1878 if (!req
|| !req
[0])
1879 return EPWMD_COMMAND_SYNTAX
;
1881 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1883 * The first argument may be only an account.
1885 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1886 return EPWMD_COMMAND_SYNTAX
;
1889 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1897 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1898 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1908 for (a
= n
->properties
; a
; a
= a
->next
) {
1911 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1913 g_strfreev(attrlist
);
1915 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1916 return gpg_error_from_errno(ENOMEM
);
1921 attrlist
[i
] = g_strdup_printf("%s %s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1924 g_strfreev(attrlist
);
1925 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1926 return gpg_error_from_errno(ENOMEM
);
1929 attrlist
[++i
] = NULL
;
1933 return EPWMD_EMPTY_ELEMENT
;
1935 line
= g_strjoinv("\n", attrlist
);
1938 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1939 g_strfreev(attrlist
);
1940 return gpg_error_from_errno(ENOMEM
);
1943 pth_cleanup_push(g_free
, line
);
1944 pth_cleanup_push(req_cleanup
, attrlist
);
1945 rc
= xfer_data(ctx
, line
, strlen(line
));
1952 * req[0] - attribute
1953 * req[1] - element path
1955 static gint
attribute_delete(struct client_s
*client
, gchar
**req
)
1959 gchar
**path
= NULL
;
1962 if (!req
|| !req
[0] || !req
[1])
1963 return EPWMD_COMMAND_SYNTAX
;
1965 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1967 * The first argument may be only an account.
1969 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1970 return EPWMD_COMMAND_SYNTAX
;
1974 * Don't remove the "name" attribute for the account element. To remove an
1975 * account use DELETE <account>.
1977 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1978 rc
= EPWMD_ATTR_SYNTAX
;
1982 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1988 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1989 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1997 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1998 return EPWMD_ATTR_NOT_FOUND
;
2000 if (xmlRemoveProp(a
) == -1)
2001 return EPWMD_LIBXML_ERROR
;
2010 static xmlNodePtr
create_element_path(struct client_s
*client
, gchar
***path
,
2013 gchar
**src
= *path
;
2014 gchar
**src_orig
= g_strdupv(src
);
2015 xmlNodePtr n
= NULL
;
2020 *rc
= gpg_error_from_errno(ENOMEM
);
2021 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2026 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
2029 if (*rc
== EPWMD_ELEMENT_NOT_FOUND
) {
2030 *rc
= new_account(client
->doc
, src
[0]);
2043 n
= create_target_elements_cb(n
, src
+1, rc
, NULL
);
2045 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
2046 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
2052 * Reset the position of the element tree now that the elements
2053 * have been created.
2058 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
2063 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
2064 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2072 g_strfreev(src_orig
);
2079 * Creates a "target" attribute. When other commands encounter an element with
2080 * this attribute, the element path is modified to the target value. If the
2081 * source element path doesn't exist when using 'ATTR SET target', it is
2082 * created, but the destination element path must exist.
2084 * req[0] - source element path
2085 * req[1] - destination element path
2087 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
2089 gchar
**src
, **dst
, *line
= NULL
;
2093 if (!req
|| !req
[0] || !req
[1])
2094 return EPWMD_COMMAND_SYNTAX
;
2096 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2098 * The first argument may be only an account.
2100 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
2101 return EPWMD_COMMAND_SYNTAX
;
2104 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2106 * The first argument may be only an account.
2108 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
2109 rc
= EPWMD_COMMAND_SYNTAX
;
2114 n
= find_account(client
->doc
, &dst
, &rc
, NULL
, 0);
2117 * Make sure the destination element path exists.
2123 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
2124 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2130 n
= create_element_path(client
, &src
, &rc
);
2135 line
= g_strjoinv("\t", dst
);
2138 rc
= gpg_error_from_errno(ENOMEM
);
2139 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2143 rc
= add_attribute(n
, "target", line
);
2153 * req[0] - account name
2156 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
2162 tmp
= g_strdupv(req
);
2165 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2166 return gpg_error_from_errno(ENOMEM
);
2169 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
2175 if (g_utf8_collate(req
[0], req
[1]) == 0)
2179 * Will not overwrite an existing account.
2181 tmp
= g_strdupv(req
+1);
2184 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2185 return gpg_error_from_errno(ENOMEM
);
2188 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
2191 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
)
2195 return EPWMD_ACCOUNT_EXISTS
;
2198 * Whitespace not allowed in account names.
2200 if (contains_whitespace(req
[1]) == TRUE
)
2201 return EPWMD_ATTR_SYNTAX
;
2203 tmp
= g_strdupv(req
);
2206 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2207 return gpg_error_from_errno(ENOMEM
);
2210 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
2214 return EPWMD_ELEMENT_NOT_FOUND
;
2216 return add_attribute(n
, "name", req
[1]);
2220 * req[0] - attribute
2221 * req[1] - element path
2223 static gint
attribute_get(assuan_context_t ctx
, gchar
**req
)
2225 struct client_s
*client
= assuan_get_pointer(ctx
);
2231 if (!req
|| !req
[0] || !req
[1])
2232 return EPWMD_COMMAND_SYNTAX
;
2234 if (strchr(req
[1], '\t')) {
2235 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2236 return EPWMD_COMMAND_SYNTAX
;
2239 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2240 return EPWMD_COMMAND_SYNTAX
;
2243 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2249 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2250 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2258 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2259 return EPWMD_ATTR_NOT_FOUND
;
2261 pth_cleanup_push(xmlFree
, a
);
2262 rc
= xfer_data(ctx
, (gchar
*)a
, xmlStrlen(a
));
2272 * req[0] - attribute
2273 * req[1] - element path
2276 static gint
attribute_set(struct client_s
*client
, gchar
**req
)
2278 gchar
**path
= NULL
;
2282 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2283 return EPWMD_COMMAND_SYNTAX
;
2286 * Reserved attribute names.
2288 if (g_utf8_collate(req
[0], "name") == 0) {
2290 * Only reserved for the account element. Not the rest of the
2293 if (strchr(req
[1], '\t') == NULL
)
2294 return name_attribute(client
, req
+ 1);
2296 else if (g_utf8_collate(req
[0], "target") == 0)
2297 return target_attribute(client
, req
+ 1);
2299 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2301 * The first argument may be only an account.
2303 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2304 return EPWMD_COMMAND_SYNTAX
;
2307 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2313 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2314 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2321 return add_attribute(n
, req
[0], req
[2]);
2330 * req[1] - attribute name or element path if command is LIST
2331 * req[2] - element path
2332 * req[2] - element path or value
2334 static gint
attr_command(assuan_context_t ctx
, gchar
*line
)
2336 struct client_s
*client
= assuan_get_pointer(ctx
);
2340 log_write2("ARGS=\"%s\"", line
);
2341 req
= split_input_line(line
, " ", 4);
2343 if (!req
|| !req
[0] || !req
[1]) {
2345 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2348 pth_cleanup_push(req_cleanup
, req
);
2350 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2351 rc
= attribute_set(client
, req
+1);
2352 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2353 rc
= attribute_get(ctx
, req
+1);
2354 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2355 rc
= attribute_delete(client
, req
+1);
2356 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2357 rc
= attribute_list(ctx
, req
+1);
2359 rc
= EPWMD_COMMAND_SYNTAX
;
2362 return send_error(ctx
, rc
);
2365 static gint
iscached_command(assuan_context_t ctx
, gchar
*line
)
2367 gchar
**req
= split_input_line(line
, " ", 0);
2371 if (!req
|| !*req
) {
2373 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2376 log_write2("ARGS=\"%s\"", line
);
2378 if (!valid_filename(req
[0])) {
2380 return EPWMD_INVALID_FILENAME
;
2383 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2386 if (cache_iscached(md5file
)) {
2389 return send_error(ctx
, 0);
2393 tmp
= get_key_file_string("global", "data_directory");
2397 return gpg_error_from_errno(ENOMEM
);
2400 path
= expand_homedir(tmp
);
2405 return gpg_error_from_errno(ENOMEM
);
2410 path
= g_strdup_printf("%s/%s", tmp
, req
[0]);
2415 return gpg_error_from_errno(ENOMEM
);
2418 if (access(path
, R_OK
) == -1) {
2419 gpg_error_t rc
= gpg_error_from_syserror();
2423 return send_error(ctx
, rc
);
2427 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2430 static gint
clearcache_command(assuan_context_t ctx
, gchar
*line
)
2432 struct client_s
*client
= assuan_get_pointer(ctx
);
2433 gchar
**req
= split_input_line(line
, " ", 0);
2436 log_write2("ARGS=\"%s\"", line
);
2439 if (!req
|| !*req
) {
2441 cache_clear(client
->md5file
, 2);
2443 return send_error(ctx
, 0);
2446 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2449 if (cache_clear(md5file
, 1) == FALSE
) {
2451 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2455 return send_error(ctx
, 0);
2458 static gint
cachetimeout_command(assuan_context_t ctx
, gchar
*line
)
2462 gchar
**req
= split_input_line(line
, " ", 0);
2465 if (!req
|| !*req
|| !req
[1]) {
2467 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2471 timeout
= strtol(req
[1], &p
, 10);
2473 if (errno
!= 0 || *p
!= 0 || timeout
< -1) {
2475 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2478 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2479 CACHE_LOCK(client
->ctx
);
2481 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2483 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2487 return send_error(ctx
, 0);
2490 static gint
dump_command(assuan_context_t ctx
, gchar
*line
)
2494 struct client_s
*client
= assuan_get_pointer(ctx
);
2497 if (disable_list_and_dump
== TRUE
)
2498 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2500 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2503 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2504 return send_syserror(ctx
, ENOMEM
);
2507 pth_cleanup_push(xmlFree
, xml
);
2508 rc
= xfer_data(ctx
, (gchar
*)xml
, len
);
2510 return send_error(ctx
, rc
);
2513 static gint
getconfig_command(assuan_context_t ctx
, gchar
*line
)
2515 struct client_s
*client
= assuan_get_pointer(ctx
);
2517 gchar filename
[255]={0}, param
[747]={0};
2518 gchar
*p
, *tmp
, *fp
= client
->filename
, *paramp
= line
;
2520 log_write2("ARGS=\"%s\"", line
);
2522 if (strchr(line
, ' ')) {
2523 sscanf(line
, " %254[^ ] %746c", filename
, param
);
2528 if (fp
&& !valid_filename(fp
))
2529 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
2531 paramp
= g_ascii_strdown(paramp
, -1);
2534 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2535 return send_syserror(ctx
, ENOMEM
);
2538 if (fp
&& !g_ascii_strcasecmp(paramp
, "iterations")) {
2539 if (!(client
->opts
& OPT_ITERATIONS
) || fp
!= client
->filename
) {
2540 file_header_internal_t
*fh
= read_file_header(fp
, FALSE
, &rc
);
2542 if (!fh
&& rc
!= GPG_ERR_ENOENT
)
2543 return send_error(ctx
, rc
);
2547 p
= g_strdup_printf("%llu", fh
->ver
.fh2
.iter
);
2548 close_file_header(fh
);
2551 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2553 return send_syserror(ctx
, ENOMEM
);
2560 else if (!g_ascii_strcasecmp(paramp
, "enable_pinentry")) {
2561 #ifdef WITH_PINENTRY
2564 if (fp
== client
->filename
&& (client
->opts
& OPT_PINENTRY
))
2565 n
= client
->pinentry
->enable
;
2567 n
= get_key_file_boolean(fp
, "enable_pinentry");
2569 p
= g_strdup_printf("%s", n
? "true" : "false");
2572 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2574 return send_syserror(ctx
, ENOMEM
);
2579 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2582 else if (!g_ascii_strcasecmp(paramp
, "pinentry_timeout")) {
2583 #ifdef WITH_PINENTRY
2584 if (fp
== client
->filename
&& (client
->opts
& OPT_PINENTRY_TO
))
2585 p
= g_strdup_printf("%i", client
->pinentry
->timeout
);
2587 p
= g_strdup_printf("%i",
2588 get_key_file_integer(fp
, "pinentry_timeout"));
2591 log_write("%s(%i): %s", __FILE__
, __LINE__
,
2593 return send_syserror(ctx
, ENOMEM
);
2598 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2602 p
= get_key_file_string(fp
? fp
: "global", paramp
);
2606 return send_error(ctx
, GPG_ERR_NO_VALUE
);
2608 tmp
= expand_homedir(p
);
2612 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2613 return send_syserror(ctx
, ENOMEM
);
2618 pth_cleanup_push(g_free
, p
);
2619 rc
= xfer_data(ctx
, p
, strlen(p
));
2621 return send_error(ctx
, rc
);
2625 xmlXPathContextPtr xp
;
2626 xmlXPathObjectPtr result
;
2631 static void xpath_command_cleanup(void *arg
)
2633 struct xpath_s
*xpath
= arg
;
2635 req_cleanup(xpath
->req
);
2638 xmlBufferFree(xpath
->buf
);
2641 xmlXPathFreeObject(xpath
->result
);
2644 xmlXPathFreeContext(xpath
->xp
);
2647 static gint
xpath_command(assuan_context_t ctx
, gchar
*line
)
2649 struct client_s
*client
= assuan_get_pointer(ctx
);
2651 struct xpath_s xpath
;
2653 log_write2("ARGS=\"%s\"", line
);
2655 if (disable_list_and_dump
== TRUE
)
2656 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2658 if (!line
|| !*line
)
2659 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2661 memset(&xpath
, 0, sizeof(struct xpath_s
));
2663 if ((xpath
.req
= split_input_line(line
, "\t", 2)) == NULL
) {
2664 if (strv_printf(&xpath
.req
, "%s", line
) == FALSE
)
2665 return send_syserror(ctx
, ENOMEM
);
2668 xpath
.xp
= xmlXPathNewContext(client
->doc
);
2671 xpath_command_cleanup(&xpath
);
2672 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2675 xpath
.result
= xmlXPathEvalExpression((xmlChar
*)xpath
.req
[0], xpath
.xp
);
2677 if (!xpath
.result
) {
2678 xpath_command_cleanup(&xpath
);
2679 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2682 if (xmlXPathNodeSetIsEmpty(xpath
.result
->nodesetval
)) {
2683 rc
= EPWMD_EMPTY_ELEMENT
;
2687 rc
= recurse_xpath_nodeset(client
->doc
, xpath
.result
->nodesetval
,
2688 (xmlChar
*)xpath
.req
[1], &xpath
.buf
);
2692 else if (!xpath
.req
[1] && !xmlBufferLength(xpath
.buf
)) {
2693 rc
= EPWMD_EMPTY_ELEMENT
;
2696 else if (xpath
.req
[1])
2699 pth_cleanup_push(xpath_command_cleanup
, &xpath
);
2700 rc
= xfer_data(ctx
, (gchar
*)xmlBufferContent(xpath
.buf
),
2701 xmlBufferLength(xpath
.buf
));
2705 xpath_command_cleanup(&xpath
);
2706 return send_error(ctx
, rc
);
2709 static gint
import_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
2712 struct client_s
*client
= assuan_get_pointer((assuan_context_t
)data
);
2713 gpg_error_t rc
= file_modified(client
);
2714 gchar
**req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2716 xmlNodePtr n
, root
, copy
;
2718 if (assuan_rc
|| rc
) {
2721 return assuan_rc
? assuan_rc
: rc
;
2724 req
= split_input_line((gchar
*)line
, " ", 2);
2728 return EPWMD_COMMAND_SYNTAX
;
2730 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2731 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
2732 return EPWMD_COMMAND_SYNTAX
;
2737 if (!content
|| !*content
) {
2738 rc
= EPWMD_COMMAND_SYNTAX
;
2742 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2745 rc
= EPWMD_LIBXML_ERROR
;
2749 root
= xmlDocGetRootElement(doc
);
2750 path_orig
= g_strdupv(path
);
2754 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
2755 rc
= gpg_error_from_errno(ENOMEM
);
2759 if (strv_printf(&path
, "%s", (gchar
*)root
->name
) == FALSE
) {
2760 g_strfreev(path_orig
);
2762 rc
= gpg_error_from_errno(ENOMEM
);
2766 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2768 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2769 g_strfreev(path_orig
);
2774 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2776 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2777 g_strfreev(path_orig
);
2782 xmlNodePtr parent
= n
->parent
;
2793 if (rc
== EPWMD_ELEMENT_NOT_FOUND
) {
2794 n
= create_element_path(client
, &path
, &rc
);
2802 copy
= xmlCopyNode(root
, 1);
2803 n
= xmlAddChild(n
, copy
);
2807 rc
= EPWMD_LIBXML_ERROR
;
2812 client
->inquire_status
= INQUIRE_DONE
;
2816 static gint
import_command(assuan_context_t ctx
, gchar
*line
)
2819 struct client_s
*client
= assuan_get_pointer(ctx
);
2821 rc
= assuan_inquire_ext(ctx
, "IMPORT", 0, import_command_finalize
, ctx
);
2824 return send_error(ctx
, rc
);
2826 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
2827 client
->inquire_status
= INQUIRE_BUSY
;
2831 static gint
lock_command(assuan_context_t ctx
, gchar
*line
)
2834 struct client_s
*client
= assuan_get_pointer(ctx
);
2836 rc
= lock_file_mutex(client
);
2839 client
->is_lock_cmd
= TRUE
;
2841 return send_error(ctx
, rc
);
2844 static gint
unlock_command(assuan_context_t ctx
, gchar
*line
)
2846 struct client_s
*client
= assuan_get_pointer(ctx
);
2848 unlock_file_mutex(client
);
2849 return send_error(ctx
, 0);
2852 static gint
getpid_command(assuan_context_t ctx
, gchar
*line
)
2856 pid_t pid
= getpid();
2858 print_fmt(buf
, sizeof(buf
), "%i", pid
);
2859 rc
= xfer_data(ctx
, buf
, strlen(buf
));
2860 return send_error(ctx
, rc
);
2863 static gint
version_command(assuan_context_t ctx
, gchar
*line
)
2868 print_fmt(buf
, sizeof(buf
), "0x%X", VERSION_HEX
);
2869 rc
= xfer_data(ctx
, buf
, strlen(buf
));
2870 return send_error(ctx
, rc
);
2873 #ifdef WITH_PINENTRY
2874 static void set_option_value(gchar
**opt
, const gchar
*value
)
2882 *opt
= g_strdup(value
);
2886 static gint
set_unset_common(assuan_context_t ctx
, const gchar
*name
,
2889 struct client_s
*client
= assuan_get_pointer(ctx
);
2891 if (g_ascii_strcasecmp(name
, (gchar
*)"log_level") == 0) {
2895 l
= strtol(value
, NULL
, 10);
2898 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2901 log_write1("log_level=%li", l
);
2902 MUTEX_LOCK(&rcfile_mutex
);
2903 g_key_file_set_integer(keyfileh
, "global", "log_level", (gint
)l
);
2904 MUTEX_UNLOCK(&rcfile_mutex
);
2907 else if (g_ascii_strcasecmp(name
, (gchar
*)"cipher") == 0) {
2909 const gchar
*p
= value
;
2911 if (!client
->filename
)
2912 return EPWMD_NO_FILE
;
2915 flags
= pwmd_cipher_str_to_cipher(value
);
2918 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2921 p
= get_key_file_string("global", "cipher");
2923 MUTEX_LOCK(&rcfile_mutex
);
2924 g_key_file_set_string(keyfileh
, client
->filename
, "cipher", p
);
2925 MUTEX_UNLOCK(&rcfile_mutex
);
2926 log_write1("cipher=%s", p
);
2933 else if (g_ascii_strcasecmp(name
, (gchar
*)"iterations") == 0) {
2937 if (!client
->filename
)
2938 return EPWMD_NO_FILE
;
2941 MUTEX_LOCK(&rcfile_mutex
);
2942 g_key_file_set_double(keyfileh
, client
->filename
, "iterations",
2943 get_key_file_double("global", "iterations"));
2944 MUTEX_UNLOCK(&rcfile_mutex
);
2945 log_write1("iterations=%llu",
2946 get_key_file_double(client
->filename
, "iterations"));
2951 n
= strtoll(value
, &p
, 10);
2953 if (errno
|| (p
&& *p
) || n
< 0ULL)
2954 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2956 MUTEX_LOCK(&rcfile_mutex
);
2957 g_key_file_set_double(keyfileh
,
2958 client
->filename
? client
->filename
: "global", "iterations", n
);
2959 MUTEX_UNLOCK(&rcfile_mutex
);
2961 if (client
->filename
)
2962 client
->opts
|= OPT_ITERATIONS
;
2964 log_write1("iterations=%llu", n
);
2967 else if (g_ascii_strcasecmp(name
, (gchar
*)"NAME") == 0) {
2968 pth_attr_t attr
= pth_attr_of(pth_self());
2972 pth_attr_destroy(attr
);
2976 print_fmt(buf
, sizeof(buf
), "%s", value
);
2977 pth_attr_set(attr
, PTH_ATTR_NAME
, buf
);
2978 pth_attr_destroy(attr
);
2979 log_write1("name=%s", buf
);
2980 #ifdef WITH_PINENTRY
2981 if (client
->pinentry
->name
)
2982 g_free(client
->pinentry
->name
);
2984 client
->pinentry
->name
= g_strdup(buf
);
2986 if (!client
->pinentry
->name
)
2987 return gpg_error_from_errno(ENOMEM
);
2992 #ifdef WITH_PINENTRY
2993 else if (g_ascii_strcasecmp(name
, (gchar
*)"lc_messages") == 0)
2994 set_option_value(&client
->pinentry
->lcmessages
, value
);
2995 else if (g_ascii_strcasecmp(name
, (gchar
*)"lc_ctype") == 0)
2996 set_option_value(&client
->pinentry
->lcctype
, value
);
2997 else if (g_ascii_strcasecmp(name
, (gchar
*)"ttyname") == 0)
2998 set_option_value(&client
->pinentry
->ttyname
, value
);
2999 else if (g_ascii_strcasecmp(name
, (gchar
*)"ttytype") == 0)
3000 set_option_value(&client
->pinentry
->ttytype
, value
);
3001 else if (g_ascii_strcasecmp(name
, (gchar
*)"display") == 0)
3002 set_option_value(&client
->pinentry
->display
, value
);
3003 else if (g_ascii_strcasecmp(name
, (gchar
*)"pinentry_path") == 0)
3004 set_option_value(&client
->pinentry
->path
, value
);
3005 else if (g_ascii_strcasecmp(name
, (gchar
*)"title") == 0)
3006 set_option_value(&client
->pinentry
->title
, value
);
3007 else if (g_ascii_strcasecmp(name
, (gchar
*)"prompt") == 0)
3008 set_option_value(&client
->pinentry
->prompt
, value
);
3009 else if (g_ascii_strcasecmp(name
, (gchar
*)"desc") == 0)
3010 set_option_value(&client
->pinentry
->desc
, value
);
3011 else if (g_ascii_strcasecmp(name
, "pinentry_timeout") == 0) {
3016 client
->pinentry
->timeout
=
3017 get_key_file_integer(client
->filename
, "pinentry_timeout");
3018 client
->opts
&= ~(OPT_PINENTRY_TO
);
3019 log_write1("pinentry_timeout=%i",
3020 get_key_file_integer(client
->filename
, "pinentry_timeout"));
3024 n
= strtol(value
, &p
, 10);
3027 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3029 client
->pinentry
->timeout
= n
;
3030 client
->opts
|= OPT_PINENTRY_TO
;
3031 log_write1("pinentry_timeout=%i", n
);
3034 else if (g_ascii_strcasecmp(name
, "enable_pinentry") == 0) {
3039 client
->pinentry
->enable
= -1;
3040 client
->opts
&= ~(OPT_PINENTRY
);
3044 n
= strtol(value
, &p
, 10);
3046 if (*p
|| n
< 0 || n
> 1)
3047 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
3049 client
->pinentry
->enable
= n
== 0 ? FALSE
: TRUE
;
3050 client
->opts
|= OPT_PINENTRY
;
3051 log_write1("enable_pinentry=%i", n
);
3056 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
3058 log_write1("%s=%s", name
, value
? value
: "");
3064 static gint
unset_command(assuan_context_t ctx
, gchar
*line
)
3066 log_write2("ARGS=\"%s\"", line
);
3067 return send_error(ctx
, set_unset_common(ctx
, line
, NULL
));
3070 static gint
set_command(assuan_context_t ctx
, gchar
*line
)
3072 gchar name
[64] = {0}, value
[256] = {0};
3074 log_write2("ARGS=\"%s\"", line
);
3076 if (sscanf(line
, " %63[_a-zA-Z] = %255c", name
, value
) != 2)
3077 return send_error(ctx
, gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_SYNTAX
));
3079 return send_error(ctx
, set_unset_common(ctx
, name
, value
));
3082 static gint
rename_command(assuan_context_t ctx
, gchar
*line
)
3084 struct client_s
*client
= assuan_get_pointer(ctx
);
3086 gchar
**req
, **src
, *dst
;
3089 log_write2("ARGS=\"%s\"", line
);
3090 req
= split_input_line(line
, " ", -1);
3092 if (!req
|| !req
[0] || !req
[1]) {
3094 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
3099 if (!valid_xml_element((xmlChar
*)dst
)) {
3101 return EPWMD_INVALID_ELEMENT
;
3104 if (strchr(req
[0], '\t'))
3105 src
= split_input_line(req
[0], "\t", -1);
3107 src
= split_input_line(req
[0], " ", -1);
3109 if (!src
|| !*src
) {
3110 rc
= EPWMD_COMMAND_SYNTAX
;
3114 n
= find_account(client
->doc
, &src
, &rc
, NULL
, 0);
3117 n
= find_elements(client
->doc
, n
->children
, src
+1, &rc
, NULL
, NULL
,
3118 NULL
, FALSE
, 0, NULL
);
3124 xmlNodeSetName(n
, (xmlChar
*)dst
);
3126 rc
= add_attribute(n
, "name", dst
);
3131 return send_error(ctx
, rc
);
3134 static gint
copy_command(assuan_context_t ctx
, gchar
*line
)
3136 struct client_s
*client
= assuan_get_pointer(ctx
);
3138 gchar
**req
, **src
= NULL
, **dst
= NULL
;
3139 xmlNodePtr nsrc
, ndst
, new, n
;
3141 log_write2("ARGS=\"%s\"", line
);
3142 req
= split_input_line(line
, " ", -1);
3144 if (!req
|| !req
[0] || !req
[1]) {
3146 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
3149 if (strchr(req
[0], '\t'))
3150 src
= split_input_line(req
[0], "\t", -1);
3152 src
= split_input_line(req
[0], " ", -1);
3154 if (!src
|| !*src
) {
3155 rc
= EPWMD_COMMAND_SYNTAX
;
3159 if (strchr(req
[1], '\t'))
3160 dst
= split_input_line(req
[1], "\t", -1);
3162 dst
= split_input_line(req
[1], " ", -1);
3164 if (!dst
|| !*dst
) {
3165 rc
= EPWMD_COMMAND_SYNTAX
;
3169 nsrc
= find_account(client
->doc
, &src
, &rc
, NULL
, 0);
3172 nsrc
= find_elements(client
->doc
, nsrc
->children
, src
+1, &rc
, NULL
,
3173 NULL
, NULL
, FALSE
, 0, NULL
);
3178 ndst
= find_account(client
->doc
, &dst
, &rc
, NULL
, 0);
3181 ndst
= find_elements(client
->doc
, ndst
->children
, dst
+1, &rc
, NULL
,
3182 NULL
, NULL
, FALSE
, 0, NULL
);
3184 if (!ndst
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
)
3187 new = xmlCopyNode(nsrc
->children
, 1);
3190 rc
= GPG_ERR_ENOMEM
;
3195 ndst
= create_element_path(client
, &dst
, &rc
);
3202 xmlUnlinkNode(ndst
->children
);
3204 xmlAddChild(ndst
, new);
3216 return send_error(ctx
, rc
);
3219 static void bye_notify(assuan_context_t ctx
)
3221 struct client_s
*cl
= assuan_get_pointer(ctx
);
3223 /* This will let assuan_process_next() return. */
3224 fcntl(cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3227 static void reset_notify(assuan_context_t ctx
)
3229 struct client_s
*cl
= assuan_get_pointer(ctx
);
3236 * This is called before every Assuan command.
3238 gint
command_startup(assuan_context_t ctx
, const gchar
*name
)
3240 struct client_s
*cl
= assuan_get_pointer(ctx
);
3243 log_write1("%s", name
);
3245 if (!g_ascii_strcasecmp(name
, "ISCACHED") ||
3246 !g_ascii_strcasecmp(name
, "CLEARCACHE") ||
3247 !g_ascii_strcasecmp(name
, "CACHETIMEOUT") ||
3248 !g_ascii_strcasecmp(name
, "GETCONFIG") ||
3249 !g_ascii_strcasecmp(name
, "GETPID") ||
3250 !g_ascii_strcasecmp(name
, "VERSION") ||
3251 !g_ascii_strcasecmp(name
, "SET") ||
3252 !g_ascii_strcasecmp(name
, "BYE") ||
3253 !g_ascii_strcasecmp(name
, "NOP") ||
3254 !g_ascii_strcasecmp(name
, "CANCEL") ||
3255 !g_ascii_strcasecmp(name
, "RESET") ||
3256 !g_ascii_strcasecmp(name
, "END") ||
3257 !g_ascii_strcasecmp(name
, "HELP") ||
3258 !g_ascii_strcasecmp(name
, "OPTION") ||
3259 !g_ascii_strcasecmp(name
, "INPUT") ||
3260 !g_ascii_strcasecmp(name
, "OUTPUT") ||
3261 !g_ascii_strcasecmp(name
, "UNSET"))
3264 cl
->last_rc
= rc
= file_modified(cl
);
3267 if ((rc
== EPWMD_NO_FILE
|| rc
== EPWMD_FILE_MODIFIED
) &&
3268 !g_ascii_strcasecmp(name
, "OPEN"))
3276 * This is called after every Assuan command.
3278 void command_finalize(assuan_context_t ctx
, gint rc
)
3280 struct client_s
*client
= assuan_get_pointer(ctx
);
3282 if (!client
->is_lock_cmd
)
3283 unlock_file_mutex(client
);
3285 log_write1(N_("command completed (rc=%u)"), client
->last_rc
);
3288 gpg_error_t
register_commands(assuan_context_t ctx
)
3292 gint (*handler
)(assuan_context_t
, gchar
*line
);
3294 { "OPEN", open_command
},
3295 { "SAVE", save_command
},
3296 { "LIST", list_command
},
3297 { "REALPATH", realpath_command
},
3298 { "STORE", store_command
},
3299 { "DELETE", delete_command
},
3300 { "GET", get_command
},
3301 { "ATTR", attr_command
},
3302 { "ISCACHED", iscached_command
},
3303 { "CLEARCACHE", clearcache_command
},
3304 { "CACHETIMEOUT", cachetimeout_command
},
3305 { "GETCONFIG", getconfig_command
},
3306 { "DUMP", dump_command
},
3307 { "XPATH", xpath_command
},
3308 { "IMPORT", import_command
},
3309 { "LOCK", lock_command
},
3310 { "UNLOCK", unlock_command
},
3311 { "GETPID", getpid_command
},
3312 { "VERSION", version_command
},
3313 { "SET", set_command
},
3314 { "UNSET", unset_command
},
3315 { "RENAME", rename_command
},
3316 { "COPY", copy_command
},
3323 for (i
=0; table
[i
].name
; i
++) {
3324 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
3330 rc
= assuan_register_bye_notify(ctx
, bye_notify
);
3335 rc
= assuan_register_reset_notify(ctx
, reset_notify
);
3340 rc
= assuan_register_pre_cmd_notify(ctx
, command_startup
);
3345 return assuan_register_post_cmd_notify(ctx
, command_finalize
);
3348 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, guchar
*key
,
3349 struct client_crypto_s
*crypto
, gpointer
*dst
, goffset
*dst_len
)
3351 goffset insize
, len
;
3352 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
3353 guint64 iter
= 0ULL, n_iter
= 0ULL, iter_progress
= 0ULL;
3357 gsize fh_size
= crypto
->fh
->v1
? sizeof(crypto
->fh
->ver
.fh1
) : sizeof(crypto
->fh
->ver
.fh2
);
3358 guint64 fh_iter
= crypto
->fh
->v1
? crypto
->fh
->ver
.fh1
.iter
: crypto
->fh
->ver
.fh2
.iter
;
3359 guint hashlen
= gcry_md_get_algo_dlen(GCRY_MD_SHA256
);
3361 lseek(crypto
->fh
->fd
, fh_size
, SEEK_SET
);
3362 insize
= crypto
->fh
->st
.st_size
- fh_size
;
3363 crypto
->iv
= gcry_malloc(crypto
->blocksize
);
3366 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3367 return gpg_error_from_errno(ENOMEM
);
3371 memcpy(crypto
->iv
, crypto
->fh
->ver
.fh1
.iv
, crypto
->blocksize
);
3373 memcpy(crypto
->iv
, crypto
->fh
->ver
.fh2
.iv
, crypto
->blocksize
);
3375 crypto
->inbuf
= gcry_malloc(insize
);
3377 if (!crypto
->inbuf
) {
3378 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
3379 return gpg_error_from_errno(ENOMEM
);
3382 crypto
->insize
= insize
;
3383 len
= pth_read(crypto
->fh
->fd
, crypto
->inbuf
, crypto
->insize
);
3385 if (len
!= crypto
->insize
)
3386 return GPG_ERR_INV_LENGTH
;
3388 /* No encryption iterations. This is a plain (gzipped) file. */
3389 if ((crypto
->fh
->v1
&& (long)fh_iter
< 0L) ||
3390 (!crypto
->fh
->v1
&& fh_iter
<= 0ULL)) {
3392 * cache_file_count() needs both .used == TRUE and a valid key in
3393 * order for it to count as a used cache entry. Fixes CACHE status
3396 memset(key
, '!', hashlen
);
3400 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->iv
, crypto
->blocksize
))) {
3401 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
3405 if ((rc
= gcry_cipher_setkey(crypto
->gh
, key
, crypto
->keysize
))) {
3406 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
3410 iter_progress
= (guint64
)get_key_file_double(client
&& client
->filename
?
3411 client
->filename
: "global", "iteration_progress");
3413 if (iter_progress
> 0ULL && fh_iter
>= iter_progress
) {
3414 rc
= send_status(ctx
, STATUS_DECRYPT
, "0 %llu", fh_iter
);
3420 rc
= iterate_crypto_once(client
, crypto
, STATUS_DECRYPT
);
3425 crypto
->tkey
= gcry_malloc(hashlen
);
3427 if (!crypto
->tkey
) {
3428 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
3429 return gpg_error_from_errno(ENOMEM
);
3432 memcpy(crypto
->tkey
, key
, hashlen
);
3433 guchar
*tkey
= crypto
->tkey
;
3436 if ((rc
= gcry_cipher_setkey(crypto
->gh
, crypto
->tkey
, crypto
->keysize
))) {
3437 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
3441 while (iter
< (crypto
->fh
->v1
? fh_iter
: fh_iter
-1)) {
3442 if (iter_progress
> 0ULL && iter
>= iter_progress
) {
3443 if (!(iter
% iter_progress
)) {
3444 rc
= send_status(ctx
, STATUS_DECRYPT
, "%llu %llu",
3445 ++n_iter
* iter_progress
, fh_iter
);
3452 if ((rc
= gcry_cipher_setiv(crypto
->gh
, crypto
->iv
, crypto
->blocksize
))) {
3453 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
3457 rc
= iterate_crypto_once(client
, crypto
, STATUS_DECRYPT
);
3460 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, _gpg_strerror(rc
));
3467 if (iter_progress
&& fh_iter
>= iter_progress
) {
3468 rc
= send_status(ctx
, STATUS_DECRYPT
, "%llu %llu", fh_iter
, fh_iter
);
3475 if (do_decompress(ctx
, crypto
->inbuf
, crypto
->insize
,
3476 (gpointer
*)&crypto
->outbuf
, &outsize
, &zrc
) == FALSE
) {
3477 if (zrc
== Z_MEM_ERROR
)
3478 return gpg_error_from_errno(ENOMEM
);
3480 return EPWMD_BADKEY
; // Not a valid gzip header. Must be a bad key.
3483 if (g_strncasecmp(crypto
->outbuf
, "<?xml ", 6) != 0) {
3484 gcry_free(crypto
->outbuf
);
3485 crypto
->outbuf
= NULL
;
3486 return EPWMD_BADKEY
;
3490 client
->xml
= crypto
->outbuf
;
3491 client
->len
= outsize
;
3492 crypto
->outbuf
= NULL
;
3495 *dst
= crypto
->outbuf
;
3497 crypto
->outbuf
= NULL
;
3500 /* The calling function should free the crypto struct. */