1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2008 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>
47 #include "pwmd_error.h"
52 static void *z_alloc(void *data
, unsigned items
, unsigned size
)
55 return gcry_calloc(items
, size
);
57 return calloc(items
, size
);
61 static void z_free(void *data
, void *p
)
70 static gpg_error_t
file_modified(struct client_s
*client
)
75 if (client
->state
!= STATE_OPEN
)
78 rc
= lock_file_mutex(client
);
83 if (stat(client
->filename
, &st
) == 0 && client
->mtime
) {
84 if (client
->mtime
!= st
.st_mtime
)
85 return EPWMD_FILE_MODIFIED
;
91 static gboolean
encrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
92 void *inbuf
, gsize insize
)
96 if ((rc
= gcry_cipher_encrypt(gh
, outbuf
, outsize
, inbuf
, insize
))) {
97 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
104 static gpg_error_t
decrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
105 void *inbuf
, gsize insize
)
109 if ((rc
= gcry_cipher_decrypt(gh
, outbuf
, outsize
, inbuf
, insize
)))
110 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
115 static gpg_error_t
parse_xml(assuan_context_t ctx
)
117 struct client_s
*client
= assuan_get_pointer(ctx
);
119 client
->doc
= xmlReadMemory(client
->xml
, client
->len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
122 return EPWMD_LIBXML_ERROR
;
127 void unlock_file_mutex(struct client_s
*client
)
132 if (client
->has_lock
== FALSE
|| client
->pinentry
->status
!= PINENTRY_NONE
)
134 if (client
->has_lock
== FALSE
)
138 CACHE_LOCK(client
->ctx
);
140 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
146 pth_mutex_release(m
);
147 client
->has_lock
= client
->is_lock_cmd
= FALSE
;
150 gpg_error_t
lock_file_mutex(struct client_s
*client
)
154 if (client
->has_lock
== TRUE
)
157 CACHE_LOCK(client
->ctx
);
159 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
166 if (pth_mutex_acquire(m
, TRUE
, NULL
) == FALSE
) {
167 if (errno
== EBUSY
) {
170 * If a client disconnects unexpectedly while waiting for a
171 * lock, this lets the thread terminate because send_status()
172 * will return an error. We can't use an PTH_EVENT_FUNC here
173 * because the thread that would call the callback uses it's
174 * own stack space which messes up access to the client data
175 * (if I understand it correctly).
177 while (pth_mutex_acquire(m
, TRUE
, NULL
) == FALSE
) {
178 gpg_error_t rc
= send_status(client
->ctx
, STATUS_LOCKED
);
187 pth_mutex_acquire(m
, FALSE
, NULL
);
191 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(errno
));
192 return gpg_error_from_errno(e
);
196 client
->has_lock
= TRUE
;
200 void free_client(struct client_s
*client
)
203 xmlFreeDoc(client
->doc
);
206 gcry_free(client
->xml
);
208 if (client
->filename
)
209 g_free(client
->filename
);
212 gcry_cipher_close(client
->gh
);
215 void cleanup_client(struct client_s
*client
)
217 assuan_context_t ctx
= client
->ctx
;
218 struct client_thread_s
*thd
= client
->thd
;
220 struct pinentry_s
*pin
= client
->pinentry
;
223 unlock_file_mutex(client
);
224 CACHE_LOCK(client
->ctx
);
225 cache_decr_refcount(client
->md5file
);
228 * This may be a new file so don't use a cache slot. save_command() will
229 * set this to FALSE on success.
231 if (client
->new == TRUE
)
232 cache_clear(client
->md5file
, 1);
235 memset(client
, 0, sizeof(struct client_s
));
236 client
->state
= STATE_CONNECTED
;
239 client
->freed
= TRUE
;
241 client
->pinentry
= pin
;
246 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gint insize
,
247 gpointer
*out
, glong
*outsize
, gint
*rc
)
253 gchar str
[ASSUAN_LINELENGTH
];
259 z
.avail_out
= zlib_bufsize
;
260 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
263 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
268 *rc
= inflateInit2(&z
, 47);
271 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
276 memset(&h
, 0, sizeof(gz_header
));
277 h
.comment
= (guchar
*)buf
;
278 h
.comm_max
= sizeof(buf
);
279 *rc
= inflateGetHeader(&z
, &h
);
282 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
288 *rc
= inflate(&z
, Z_BLOCK
);
291 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
298 insize
= atoi((gchar
*)h
.comment
);
303 *rc
= inflate(&z
, Z_FINISH
);
310 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
318 z
.next_out
= pout
+ z
.total_out
;
319 z
.avail_out
= zlib_bufsize
;
322 *rc
= assuan_write_status(ctx
, "DECOMPRESS",
323 print_fmt(str
, sizeof(str
), "%i %i", z
.total_out
, insize
));
338 } while (*rc
!= Z_STREAM_END
);
341 *rc
= assuan_write_status(ctx
, "DECOMPRESS",
342 print_fmt(str
, sizeof(str
), "%i %i", z
.total_out
, insize
));
349 *outsize
= z
.total_out
;
355 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
361 gpg_error_t
read_file_header(const gchar
*filename
, file_header_t
*fh
)
366 fd
= open(filename
, O_RDONLY
);
369 return gpg_error_from_errno(errno
);
371 len
= pth_read(fd
, fh
, sizeof(file_header_t
));
374 if (len
!= sizeof(file_header_t
))
375 return gpg_error_from_errno(errno
);
380 static gpg_error_t
open_command_finalize(assuan_context_t ctx
, guchar shakey
[],
383 struct client_s
*client
= assuan_get_pointer(ctx
);
390 if ((fd
= open_file(client
->filename
, &st
)) == -1) {
392 if (errno
== ENOENT
) {
400 log_write("%s: %s", client
->filename
, strerror(errno
));
401 cleanup_client(client
);
402 memset(shakey
, 0, sizeof(shakey
));
403 return send_syserror(ctx
, rc
);
406 rc
= try_xml_decrypt(ctx
, fd
, st
, shakey
, &iter
);
410 memset(shakey
, 0, sizeof(shakey
));
411 cleanup_client(client
);
412 return send_error(ctx
, rc
);
416 CACHE_LOCK(client
->ctx
);
418 if (cached
== FALSE
) {
419 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
420 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
421 cleanup_client(client
);
423 return send_error(ctx
, EPWMD_MAX_SLOTS
);
426 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
427 cache_reset_timeout(client
->md5file
, timeout
);
430 cache_set_timeout(client
->md5file
, -2);
435 memset(shakey
, 0, sizeof(shakey
));
439 gcry_free(client
->xml
);
444 if (client
->new == FALSE
)
445 send_status_all(STATUS_CACHE
);
447 client
->state
= STATE_OPEN
;
450 if (!rc
&& client
->new == FALSE
&&
451 iter
!= get_key_file_integer(client
->filename
, "iterations")) {
452 g_key_file_set_integer(keyfileh
, client
->filename
, "iterations", iter
);
453 send_status_all(STATUS_CONFIG
);
456 return send_error(ctx
, rc
);
459 static int open_command(assuan_context_t ctx
, char *line
)
462 guchar shakey
[gcrykeysize
];
463 gboolean cached
= FALSE
;
465 struct client_s
*client
= assuan_get_pointer(ctx
);
467 gchar
*filename
= NULL
;
468 file_header_t file_header
;
470 memset(shakey
, 0, sizeof(shakey
));
472 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
475 if (!filename
|| !*filename
) {
477 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
480 if (valid_filename(filename
) == FALSE
) {
482 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
485 if (client
->state
== STATE_OPEN
)
486 cleanup_client(client
);
488 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
489 CACHE_LOCK(client
->ctx
);
491 if (cache_has_file(client
->md5file
) == FALSE
) {
492 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
495 return send_error(ctx
, EPWMD_MAX_SLOTS
);
499 cache_incr_refcount(client
->md5file
);
501 rc
= lock_file_mutex(client
);
505 return send_error(ctx
, rc
);
508 client
->freed
= FALSE
;
510 if ((rc
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
512 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
513 cleanup_client(client
);
514 return send_error(ctx
, rc
);
517 if (stat(filename
, &st
) == 0) {
518 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
519 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
521 cleanup_client(client
);
522 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
525 client
->mtime
= st
.st_mtime
;
528 client
->filename
= g_strdup(filename
);
530 if (!client
->filename
) {
531 memset(shakey
, 0, sizeof(shakey
));
532 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
533 cleanup_client(client
);
535 return send_syserror(ctx
, ENOMEM
);
539 client
->pinentry
->filename
= g_strdup(client
->filename
);
541 if (!client
->pinentry
->filename
) {
542 memset(shakey
, 0, sizeof(shakey
));
543 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
544 cleanup_client(client
);
546 return send_syserror(ctx
, ENOMEM
);
551 * New files don't need a key.
553 if (access(filename
, R_OK
) != 0) {
554 if (errno
!= ENOENT
) {
556 log_write("%s: %s", filename
, strerror(errno
));
558 cleanup_client(client
);
559 return send_syserror(ctx
, rc
);
562 if ((client
->xml
= new_document()) == NULL
) {
563 log_write("%s", strerror(ENOMEM
));
565 cleanup_client(client
);
566 return send_syserror(ctx
, ENOMEM
);
569 client
->len
= xmlStrlen(client
->xml
);
571 client
->filename
= g_strdup(filename
);
573 if (!client
->filename
) {
575 cleanup_client(client
);
576 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
577 return send_syserror(ctx
, ENOMEM
);
580 memset(shakey
, 0, sizeof(shakey
));
582 if (req
[1] && *req
[1])
583 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
586 return open_command_finalize(ctx
, shakey
, cached
);
589 rc
= read_file_header(filename
, &file_header
);
593 cleanup_client(client
);
594 return send_error(ctx
, rc
);
597 if (file_header
.iter
== -1)
600 CACHE_LOCK(client
->ctx
);
601 cached
= cache_get_key(client
->md5file
, shakey
);
604 if (cached
== FALSE
) {
606 * No key specified and no matching filename found in the cache. Use
607 * pinentry to retrieve the key. Cannot return assuan_process_done()
608 * here otherwise the command will be interrupted. The event loop in
609 * client_thread() will poll the file descriptor waiting for it to
610 * become ready to read a pinentry_key_s which will contain the
611 * entered key or rc. It will then call open_command_finalize() to
612 * to finish the command.
614 if (!req
[1] || !*req
[1]) {
616 gboolean b
= get_key_file_boolean(filename
, "enable_pinentry");
618 /* From set_pinentry_defaults(). */
619 if (client
->pinentry
->enable
== FALSE
||
620 (client
->pinentry
->enable
== -1 && b
== FALSE
)) {
621 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
626 rc
= lock_pin_mutex(client
);
629 unlock_pin_mutex(client
->pinentry
);
630 cleanup_client(client
);
631 return send_error(ctx
, rc
);
634 client
->pinentry
->which
= PINENTRY_OPEN
;
635 rc
= pinentry_fork(ctx
);
638 unlock_pin_mutex(client
->pinentry
);
639 cleanup_client(client
);
640 return send_error(ctx
, rc
);
643 client
->pinentry
->cb
= open_command_finalize
;
644 client
->pinentry
->status
= PINENTRY_INIT
;
647 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
652 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
657 return open_command_finalize(ctx
, shakey
, cached
);
660 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
661 gint size
, gpointer
*out
, glong
*outsize
, gint
*rc
)
667 gint cmd
= Z_NO_FLUSH
;
668 gchar str
[ASSUAN_LINELENGTH
];
672 z
.next_in
= pin
= data
;
673 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
674 z
.avail_out
= zlib_bufsize
;
675 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
678 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
683 *rc
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
686 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
691 memset(&h
, 0, sizeof(gz_header
));
692 g_snprintf(buf
, sizeof(buf
), "%i", size
);
693 h
.comment
= (guchar
*)buf
;
694 *rc
= deflateSetHeader(&z
, &h
);
697 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
706 *rc
= deflate(&z
, cmd
);
713 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
721 z
.next_out
= pout
+ z
.total_out
;
722 z
.avail_out
= zlib_bufsize
;
725 if (!z
.avail_in
&& z
.total_in
< size
) {
726 if (z
.total_in
+ zlib_bufsize
> size
)
727 z
.avail_in
= size
- z
.total_in
;
729 z
.avail_in
= zlib_bufsize
;
732 *rc
= assuan_write_status(ctx
, "COMPRESS",
733 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
740 if (z
.total_in
>= size
)
751 } while (*rc
!= Z_STREAM_END
);
754 *rc
= assuan_write_status(ctx
, "COMPRESS",
755 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
762 *outsize
= z
.total_out
;
768 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
774 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
775 const gchar
*filename
, gpointer data
, size_t insize
, guchar
*shakey
,
781 guchar tkey
[gcrykeysize
];
784 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
785 gchar tmp
[FILENAME_MAX
];
788 file_header_t file_header
;
789 gchar str
[ASSUAN_LINELENGTH
];
793 * cache_file_count() needs both .used == TRUE and a valid key in
794 * order for it to count as a used cache entry. Fixes CACHE status
797 memset(shakey
, '!', gcrykeysize
);
799 file_header
.iter
= iter
;
803 if (insize
/ gcryblocksize
) {
804 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
806 if (insize
% gcryblocksize
)
807 len
+= gcryblocksize
;
811 * Resize the existing xml buffer to the block size required by gcrypt
812 * rather than duplicating it and wasting memory.
814 inbuf
= gcry_realloc(data
, len
);
817 return gpg_error_from_errno(ENOMEM
);
820 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
821 memcpy(tkey
, shakey
, sizeof(tkey
));
824 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
825 memset(tkey
, 0, sizeof(tkey
));
827 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
831 memset(tkey
, 0, sizeof(tkey
));
832 file_header
.iter
= iter
;
835 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
837 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
838 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
839 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
847 while (xiter
< file_header
.iter
) {
848 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
849 if (!(xiter
% iter_progress
)) {
850 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
851 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
860 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
861 sizeof(file_header
.iv
)))) {
863 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
867 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
870 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
878 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
879 sizeof(file_header
.iv
)))) {
881 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
885 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
887 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
891 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
893 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
897 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
898 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
899 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
909 if (stat(filename
, &st
) == 0) {
910 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
913 * FIXME What if the file has an ACL?
915 if (!(mode
& S_IWUSR
)) {
917 return gpg_error_from_errno(EACCES
);
921 if (errno
!= ENOENT
) {
924 return gpg_error_from_errno(rc
);
928 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
930 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
933 p
= strrchr(tmp
, '/');
935 log_write("%s: %s", p
, strerror(rc
));
936 return gpg_error_from_errno(rc
);
941 * xml_import() from command line.
945 len
= pth_write(fd
, &file_header
, sizeof(file_header_t
));
947 if (len
!= sizeof(file_header
)) {
956 return gpg_error_from_errno(len
);
959 len
= pth_write(fd
, inbuf
, insize
);
970 return gpg_error_from_errno(len
);
973 if (fsync(fd
) == -1) {
982 return gpg_error_from_errno(len
);
988 if (mode
&& get_key_file_boolean(filename
, "backup") == TRUE
) {
989 gchar tmp2
[FILENAME_MAX
];
991 g_snprintf(tmp2
, sizeof(tmp2
), "%s.backup", filename
);
993 if (rename(filename
, tmp2
) == -1) {
997 return gpg_error_from_errno(len
);
1001 if (rename(tmp
, filename
) == -1) {
1005 return gpg_error_from_errno(len
);
1009 chmod(filename
, mode
);
1016 static gpg_error_t
save_command_finalize(assuan_context_t ctx
,
1017 guchar shakey
[], gboolean cached
)
1019 struct client_s
*client
= assuan_get_pointer(ctx
);
1031 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
1034 iter
= get_key_file_integer(client
->filename
, "compression_level");
1039 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
1040 memset(shakey
, 0, sizeof(shakey
));
1043 if (zrc
== Z_MEM_ERROR
) {
1044 return send_syserror(ctx
, ENOMEM
);
1047 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
1055 iter
= get_key_file_integer(client
->filename
, "iterations");
1056 rc
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
1059 memset(shakey
, 0, sizeof(shakey
));
1060 return send_error(ctx
, rc
);
1063 stat(client
->filename
, &st
);
1064 client
->mtime
= st
.st_mtime
;
1065 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
1066 CACHE_LOCK(client
->ctx
);
1069 memset(shakey
, 0, sizeof(shakey
));
1070 cache_reset_timeout(client
->md5file
, timeout
);
1073 if (client
->new == TRUE
)
1074 send_status_all(STATUS_CACHE
);
1076 client
->new = FALSE
;
1077 return send_error(ctx
, 0);
1080 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
1081 memset(shakey
, 0, sizeof(shakey
));
1082 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
1084 return send_error(ctx
, EPWMD_MAX_SLOTS
);
1087 client
->new = FALSE
;
1088 memset(shakey
, 0, sizeof(shakey
));
1089 cache_reset_timeout(client
->md5file
, timeout
);
1091 send_status_all(STATUS_CACHE
);
1092 return send_error(ctx
, 0);
1095 static int save_command(assuan_context_t ctx
, char *line
)
1097 gboolean cached
= FALSE
;
1098 guchar shakey
[gcrykeysize
];
1100 struct client_s
*client
= assuan_get_pointer(ctx
);
1103 memset(shakey
, 0, sizeof(shakey
));
1104 rc
= file_modified(client
);
1107 return send_error(ctx
, rc
);
1109 rc
= lock_file_mutex(client
);
1112 return send_error(ctx
, rc
);
1114 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1115 return send_syserror(ctx
, errno
);
1117 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
1118 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
1119 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
1122 if (get_key_file_integer(client
->filename
, "iterations") == -1)
1125 if (!line
|| !*line
) {
1126 guchar tmp
[sizeof(shakey
)];
1129 memset(tmp
, '!', sizeof(tmp
));
1131 if (cache_get_key(client
->md5file
, shakey
) == FALSE
||
1132 memcmp(shakey
, tmp
, sizeof(shakey
)) == 0) {
1134 #ifdef WITH_PINENTRY
1135 if (get_key_file_boolean(client
->filename
, "enable_pinentry") == FALSE
) {
1136 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1140 lock_pin_mutex(client
);
1141 client
->pinentry
->which
= PINENTRY_SAVE
;
1142 rc
= pinentry_fork(ctx
);
1145 unlock_pin_mutex(client
->pinentry
);
1146 return send_error(ctx
, rc
);
1149 client
->pinentry
->cb
= save_command_finalize
;
1150 client
->pinentry
->status
= PINENTRY_INIT
;
1153 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1163 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
1164 memset(line
, 0, strlen(line
));
1168 return save_command_finalize(ctx
, shakey
, cached
);
1171 static int delete_command(assuan_context_t ctx
, char *line
)
1173 struct client_s
*client
= assuan_get_pointer(ctx
);
1178 rc
= file_modified(client
);
1181 return send_error(ctx
, rc
);
1183 if (strchr(line
, '\t'))
1184 req
= split_input_line(line
, "\t", -1);
1186 req
= split_input_line(line
, " ", -1);
1189 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1191 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1195 return send_error(ctx
, rc
);
1199 * No sub-node defined. Remove the entire node (account).
1208 return send_error(ctx
, 0);
1211 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1215 return send_error(ctx
, rc
);
1222 return send_error(ctx
, 0);
1226 * Don't return with assuan_process_done() here. This has been called from
1227 * assuan_process_next() and the command should be finished in
1230 static int store_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
1233 assuan_context_t ctx
= data
;
1234 struct client_s
*client
= assuan_get_pointer(ctx
);
1237 gpg_error_t rc
= file_modified(client
);
1239 if (assuan_rc
|| rc
) {
1246 return assuan_rc
? assuan_rc
: rc
;
1249 req
= split_input_line((gchar
*)line
, "\t", 0);
1257 return EPWMD_COMMAND_SYNTAX
;
1259 if (valid_xml_element((xmlChar
*)*req
) == FALSE
) {
1261 return EPWMD_INVALID_ELEMENT
;
1264 if (valid_element_path(req
+1, TRUE
) == FALSE
) {
1266 return EPWMD_INVALID_ELEMENT
;
1270 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1272 if (rc
&& rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1273 rc
= new_account(client
->doc
, *req
);
1290 create_elements_cb(n
, req
+1, &rc
, NULL
);
1292 find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1293 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1297 client
->inquire_status
= INQUIRE_DONE
;
1301 static int store_command(assuan_context_t ctx
, char *line
)
1303 struct client_s
*client
= assuan_get_pointer(ctx
);
1304 gpg_error_t rc
= file_modified(client
);
1307 return send_error(ctx
, rc
);
1309 rc
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1312 return send_error(ctx
, rc
);
1314 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1315 client
->inquire_status
= INQUIRE_BUSY
;
1319 static int get_command(assuan_context_t ctx
, char *line
)
1321 struct client_s
*client
= assuan_get_pointer(ctx
);
1326 rc
= file_modified(client
);
1329 return send_error(ctx
, rc
);
1331 req
= split_input_line(line
, "\t", -1);
1333 if (!req
|| !*req
) {
1335 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1338 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1342 return send_error(ctx
, rc
);
1346 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1351 return send_error(ctx
, rc
);
1353 if (!n
|| !n
->children
)
1354 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1356 n
= find_text_node(n
->children
);
1358 if (!n
|| !n
->content
|| !*n
->content
)
1359 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1361 rc
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1362 return send_error(ctx
, rc
);
1365 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**target
,
1366 gpg_error_t
*rc
, gchar
**req_orig
, void *data
)
1368 gchar
*path
= *(gchar
**)data
;
1369 gchar
*tmp
= NULL
, *result
;
1373 *(gchar
**)data
= NULL
;
1376 path
= g_strjoinv("\t", target
);
1379 *rc
= gpg_error_from_errno(ENOMEM
);
1384 tmp
= g_strjoinv("\t", req_orig
);
1388 *rc
= gpg_error_from_errno(ENOMEM
);
1394 result
= g_strdup_printf("%s\t%s", path
, tmp
);
1396 result
= g_strdup(path
);
1399 *rc
= gpg_error_from_errno(ENOMEM
);
1407 *(gchar
**)data
= result
;
1411 static int realpath_command(assuan_context_t ctx
, char *line
)
1414 struct client_s
*client
= assuan_get_pointer(ctx
);
1422 rc
= file_modified(client
);
1425 return send_error(ctx
, rc
);
1427 if (strchr(line
, '\t') != NULL
) {
1428 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1429 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1432 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1433 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1436 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1440 return send_error(ctx
, rc
);
1443 rp
= g_strjoinv("\t", req
);
1447 return send_syserror(ctx
, ENOMEM
);
1451 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1452 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, &rp
);
1457 return send_error(ctx
, rc
);
1461 string
= g_string_new(rp
);
1466 return send_syserror(ctx
, ENOMEM
);
1469 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++) {
1470 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+1) && *(t
+1) != '!')) {
1471 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1476 rc
= assuan_send_data(ctx
, string
->str
, string
->len
);
1477 g_string_free(string
, TRUE
);
1478 return send_error(ctx
, rc
);
1481 static int list_command(assuan_context_t ctx
, char *line
)
1483 struct client_s
*client
= assuan_get_pointer(ctx
);
1485 struct element_list_s
*elements
= NULL
;
1488 if (disable_list_and_dump
== TRUE
)
1489 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1491 rc
= file_modified(client
);
1494 return send_error(ctx
, rc
);
1499 rc
= list_accounts(client
->doc
, &str
);
1502 return send_error(ctx
, rc
);
1504 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1505 g_string_free(str
, TRUE
);
1506 return send_error(ctx
, rc
);
1509 elements
= g_malloc0(sizeof(struct element_list_s
));
1512 rc
= gpg_err_code_from_errno(ENOMEM
);
1516 rc
= create_path_list(client
->doc
, elements
, line
);
1522 gint total
= g_slist_length(elements
->list
);
1527 rc
= EPWMD_EMPTY_ELEMENT
;
1531 str
= g_string_new(NULL
);
1534 rc
= gpg_err_code_from_errno(ENOMEM
);
1538 for (i
= 0; i
< total
; i
++) {
1539 tmp
= g_slist_nth_data(elements
->list
, i
);
1540 g_string_append_printf(str
, "%s%s", tmp
, i
+1 == total
? "" : "\n");
1543 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1544 g_string_free(str
, TRUE
);
1547 rc
= EPWMD_EMPTY_ELEMENT
;
1551 gint total
= g_slist_length(elements
->list
);
1554 for (i
= 0; i
< total
; i
++) {
1555 tmp
= g_slist_nth_data(elements
->list
, i
);
1559 g_slist_free(elements
->list
);
1561 if (elements
->prefix
)
1562 g_free(elements
->prefix
);
1567 return send_error(ctx
, rc
);
1570 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1575 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1576 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1579 return EPWMD_LIBXML_ERROR
;
1582 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1588 * req[0] - element path
1590 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1592 struct client_s
*client
= assuan_get_pointer(ctx
);
1593 gchar
**attrlist
= NULL
;
1595 gchar
**path
= NULL
;
1601 if (!req
|| !req
[0])
1602 return EPWMD_COMMAND_SYNTAX
;
1604 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1606 * The first argument may be only an account.
1608 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1609 return EPWMD_COMMAND_SYNTAX
;
1612 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1620 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1621 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1631 for (a
= n
->properties
; a
; a
= a
->next
) {
1634 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1636 g_strfreev(attrlist
);
1638 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1639 return gpg_error_from_errno(ENOMEM
);
1644 attrlist
[i
] = g_strdup_printf("%s %s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1647 g_strfreev(attrlist
);
1648 return gpg_error_from_errno(ENOMEM
);
1651 attrlist
[++i
] = NULL
;
1655 return EPWMD_EMPTY_ELEMENT
;
1657 line
= g_strjoinv("\n", attrlist
);
1660 g_strfreev(attrlist
);
1661 return gpg_error_from_errno(ENOMEM
);
1664 rc
= assuan_send_data(ctx
, line
, strlen(line
));
1666 g_strfreev(attrlist
);
1671 * req[0] - attribute
1672 * req[1] - element path
1674 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1678 gchar
**path
= NULL
;
1681 if (!req
|| !req
[0] || !req
[1])
1682 return EPWMD_COMMAND_SYNTAX
;
1684 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1686 * The first argument may be only an account.
1688 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1689 return EPWMD_COMMAND_SYNTAX
;
1693 * Don't remove the "name" attribute for the account element. To remove an
1694 * account use DELETE <account>.
1696 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1697 rc
= EPWMD_ATTR_SYNTAX
;
1701 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1707 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1708 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1716 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1717 return EPWMD_ATTR_NOT_FOUND
;
1719 if (xmlRemoveProp(a
) == -1)
1720 return EPWMD_LIBXML_ERROR
;
1729 static xmlNodePtr
create_element_path(struct client_s
*client
, gchar
***path
,
1732 gchar
**src
= *path
;
1733 gchar
**src_orig
= g_strdupv(src
);
1734 xmlNodePtr n
= NULL
;
1739 *rc
= gpg_error_from_errno(ENOMEM
);
1744 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1747 if (*rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1748 *rc
= new_account(client
->doc
, src
[0]);
1761 n
= create_target_elements_cb(n
, src
+1, rc
, NULL
);
1763 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1764 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
1770 * Reset the position of the element tree now that the elements
1771 * have been created.
1776 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1781 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1782 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1790 g_strfreev(src_orig
);
1797 * Creates a "target" attribute. When other commands encounter an element with
1798 * this attribute, the element path is modified to the target value. If the
1799 * source element path doesn't exist when using 'ATTR SET target', it is
1800 * created, but the destination element path must exist.
1802 * req[0] - source element path
1803 * req[1] - destination element path
1805 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1807 gchar
**src
, **dst
, *line
= NULL
;
1811 if (!req
|| !req
[0] || !req
[1])
1812 return EPWMD_COMMAND_SYNTAX
;
1814 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1816 * The first argument may be only an account.
1818 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1819 return EPWMD_COMMAND_SYNTAX
;
1822 if (valid_element_path(src
, FALSE
) == FALSE
) {
1824 return EPWMD_INVALID_ELEMENT
;
1827 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1829 * The first argument may be only an account.
1831 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1832 rc
= EPWMD_COMMAND_SYNTAX
;
1837 n
= find_account(client
->doc
, &dst
, &rc
, NULL
, 0);
1840 * Make sure the destination element path exists.
1846 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
1847 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1853 n
= create_element_path(client
, &src
, &rc
);
1858 line
= g_strjoinv("\t", dst
);
1861 rc
= gpg_error_from_errno(ENOMEM
);
1865 rc
= add_attribute(n
, "target", line
);
1875 * req[0] - account name
1878 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
1884 tmp
= g_strdupv(req
);
1887 return gpg_error_from_errno(ENOMEM
);
1889 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1895 if (g_utf8_collate(req
[0], req
[1]) == 0)
1899 * Will not overwrite an existing account.
1901 tmp
= g_strdupv(req
+1);
1904 return gpg_error_from_errno(ENOMEM
);
1906 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1909 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
)
1913 return EPWMD_ACCOUNT_EXISTS
;
1916 * Whitespace not allowed in account names.
1918 if (contains_whitespace(req
[1]) == TRUE
)
1919 return EPWMD_ATTR_SYNTAX
;
1921 tmp
= g_strdupv(req
);
1924 return gpg_error_from_errno(ENOMEM
);
1926 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1930 return EPWMD_ELEMENT_NOT_FOUND
;
1932 return add_attribute(n
, "name", req
[1]);
1936 * req[0] - attribute
1937 * req[1] - element path
1939 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
1941 struct client_s
*client
= assuan_get_pointer(ctx
);
1947 if (!req
|| !req
[0] || !req
[1])
1948 return EPWMD_COMMAND_SYNTAX
;
1950 if (strchr(req
[1], '\t')) {
1951 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
1952 return EPWMD_COMMAND_SYNTAX
;
1955 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1956 return EPWMD_COMMAND_SYNTAX
;
1959 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1965 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1966 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1974 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
1975 return EPWMD_ATTR_NOT_FOUND
;
1977 rc
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
1987 * req[0] - attribute
1988 * req[1] - element path
1991 static int attribute_set(struct client_s
*client
, gchar
**req
)
1993 gchar
**path
= NULL
;
1997 if (!req
|| !req
[0] || !req
[1] || !req
[2])
1998 return EPWMD_COMMAND_SYNTAX
;
2001 * Reserved attribute names.
2003 if (g_utf8_collate(req
[0], "name") == 0) {
2005 * Only reserved for the account element. Not the rest of the
2008 if (strchr(req
[1], '\t') == NULL
)
2009 return name_attribute(client
, req
+ 1);
2011 else if (g_utf8_collate(req
[0], "target") == 0)
2012 return target_attribute(client
, req
+ 1);
2014 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2016 * The first argument may be only an account.
2018 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2019 return EPWMD_COMMAND_SYNTAX
;
2022 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2028 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2029 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2036 return add_attribute(n
, req
[0], req
[2]);
2045 * req[1] - attribute name or element path if command is LIST
2046 * req[2] - element path
2047 * req[2] - element path or value
2049 static int attr_command(assuan_context_t ctx
, char *line
)
2051 struct client_s
*client
= assuan_get_pointer(ctx
);
2055 rc
= file_modified(client
);
2058 return send_error(ctx
, rc
);
2060 req
= split_input_line(line
, " ", 4);
2062 if (!req
|| !req
[0] || !req
[1]) {
2064 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2067 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2068 rc
= attribute_set(client
, req
+1);
2069 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2070 rc
= attribute_get(ctx
, req
+1);
2071 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2072 rc
= attribute_delete(client
, req
+1);
2073 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2074 rc
= attribute_list(ctx
, req
+1);
2076 rc
= EPWMD_COMMAND_SYNTAX
;
2079 return send_error(ctx
, rc
);
2082 static int iscached_command(assuan_context_t ctx
, char *line
)
2084 gchar
**req
= split_input_line(line
, " ", 0);
2087 if (!req
|| !*req
) {
2089 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2092 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2096 if (cache_iscached(md5file
) == FALSE
) {
2098 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2102 return send_error(ctx
, 0);
2105 static int clearcache_command(assuan_context_t ctx
, char *line
)
2107 struct client_s
*client
= assuan_get_pointer(ctx
);
2108 gchar
**req
= split_input_line(line
, " ", 0);
2113 if (!req
|| !*req
) {
2115 cache_clear(client
->md5file
, 2);
2117 return send_error(ctx
, 0);
2120 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2123 if (cache_clear(md5file
, 1) == FALSE
) {
2125 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2129 return send_error(ctx
, 0);
2132 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2136 gchar
**req
= split_input_line(line
, " ", 0);
2138 struct client_s
*client
= assuan_get_pointer(ctx
);
2140 if (!req
|| !*req
|| !req
[1]) {
2142 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2146 timeout
= strtol(req
[0], &p
, 10);
2148 if (errno
!= 0 || *p
!= 0) {
2150 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2153 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2155 CACHE_LOCK(client
->ctx
);
2157 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2159 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2163 return send_error(ctx
, 0);
2166 static int dump_command(assuan_context_t ctx
, char *line
)
2170 struct client_s
*client
= assuan_get_pointer(ctx
);
2173 if (disable_list_and_dump
== TRUE
)
2174 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2176 rc
= file_modified(client
);
2179 return send_error(ctx
, rc
);
2181 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2184 return send_syserror(ctx
, ENOMEM
);
2186 rc
= assuan_send_data(ctx
, xml
, len
);
2188 return send_error(ctx
, rc
);
2191 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2193 struct client_s
*client
= assuan_get_pointer(ctx
);
2195 gchar filename
[255]={0}, param
[747]={0};
2196 gchar
*p
, *tmp
, *fp
= client
->filename
, *paramp
= line
;
2198 if (strchr(line
, ' ')) {
2199 sscanf(line
, " %254[a-zA-Z] %746c", filename
, param
);
2204 paramp
= g_ascii_strdown(paramp
, -1);
2207 return send_syserror(ctx
, ENOMEM
);
2209 if (strcmp(paramp
, "key") == 0 || strcmp(paramp
, "key_file") == 0) {
2211 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2214 p
= get_key_file_string(fp
? fp
: "global", paramp
);
2218 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2220 tmp
= expand_homedir(p
);
2224 return send_syserror(ctx
, ENOMEM
);
2229 rc
= assuan_send_data(ctx
, p
, strlen(p
));
2231 return send_error(ctx
, rc
);
2234 static int xpath_command(assuan_context_t ctx
, gchar
*line
)
2236 struct client_s
*client
= assuan_get_pointer(ctx
);
2238 xmlXPathContextPtr xp
;
2239 xmlXPathObjectPtr result
;
2240 xmlBufferPtr buf
= NULL
;
2243 rc
= file_modified(client
);
2246 return send_error(ctx
, rc
);
2248 if (!line
|| !*line
)
2249 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2251 if ((req
= split_input_line(line
, "\t", 2)) == NULL
) {
2252 if (strv_printf(&req
, "%s", line
) == FALSE
)
2253 return send_syserror(ctx
, ENOMEM
);
2256 xp
= xmlXPathNewContext(client
->doc
);
2259 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2261 result
= xmlXPathEvalExpression((xmlChar
*)req
[0], xp
);
2264 xmlXPathFreeContext(xp
);
2265 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2268 if (xmlXPathNodeSetIsEmpty(result
->nodesetval
)) {
2269 rc
= EPWMD_EMPTY_ELEMENT
;
2273 rc
= recurse_xpath_nodeset(client
->doc
, result
->nodesetval
,
2274 (xmlChar
*)req
[1], &buf
);
2278 else if (!req
[1] && !xmlBufferLength(buf
)) {
2279 rc
= EPWMD_EMPTY_ELEMENT
;
2285 rc
= assuan_send_data(ctx
, xmlBufferContent(buf
), xmlBufferLength(buf
));
2294 xmlXPathFreeObject(result
);
2297 xmlXPathFreeContext(xp
);
2299 return send_error(ctx
, rc
);
2302 static int import_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
2305 struct client_s
*client
= assuan_get_pointer((assuan_context_t
)data
);
2306 gpg_error_t rc
= file_modified(client
);
2307 gchar
**req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2309 xmlNodePtr n
, root
, copy
;
2311 if (assuan_rc
|| rc
) {
2318 return assuan_rc
? assuan_rc
: rc
;
2321 req
= split_input_line((gchar
*)line
, " ", 2);
2329 return EPWMD_COMMAND_SYNTAX
;
2331 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2332 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
2333 return EPWMD_COMMAND_SYNTAX
;
2338 if (!content
|| !*content
) {
2339 rc
= EPWMD_COMMAND_SYNTAX
;
2343 if (valid_xml_element((xmlChar
*)*path
) == FALSE
) {
2344 rc
= EPWMD_INVALID_ELEMENT
;
2348 if (valid_element_path(path
+1, FALSE
) == FALSE
) {
2349 rc
= EPWMD_INVALID_ELEMENT
;
2353 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2356 rc
= EPWMD_LIBXML_ERROR
;
2360 root
= xmlDocGetRootElement(doc
);
2361 path_orig
= g_strdupv(path
);
2365 rc
= gpg_error_from_errno(ENOMEM
);
2369 if (strv_printf(&path
, "%s", (gchar
*)root
->name
) == FALSE
) {
2370 g_strfreev(path_orig
);
2372 rc
= gpg_error_from_errno(ENOMEM
);
2376 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2378 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2379 g_strfreev(path_orig
);
2384 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2386 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2387 g_strfreev(path_orig
);
2392 xmlNodePtr parent
= n
->parent
;
2403 if (rc
== EPWMD_ELEMENT_NOT_FOUND
) {
2404 n
= create_element_path(client
, &path
, &rc
);
2412 copy
= xmlCopyNode(root
, 1);
2413 n
= xmlAddChild(n
, copy
);
2417 rc
= EPWMD_LIBXML_ERROR
;
2422 client
->inquire_status
= INQUIRE_DONE
;
2426 static int import_command(assuan_context_t ctx
, gchar
*line
)
2429 struct client_s
*client
= assuan_get_pointer(ctx
);
2431 rc
= file_modified(client
);
2434 return send_error(ctx
, rc
);
2436 rc
= assuan_inquire_ext(ctx
, "IMPORT", 0, import_command_finalize
, ctx
);
2439 return send_error(ctx
, rc
);
2441 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
2442 client
->inquire_status
= INQUIRE_BUSY
;
2446 static int lock_command(assuan_context_t ctx
, gchar
*line
)
2449 struct client_s
*client
= assuan_get_pointer(ctx
);
2451 rc
= file_modified(client
);
2454 return send_error(ctx
, rc
);
2456 rc
= lock_file_mutex(client
);
2459 client
->is_lock_cmd
= TRUE
;
2461 return send_error(ctx
, rc
);
2464 static int unlock_command(assuan_context_t ctx
, gchar
*line
)
2466 struct client_s
*client
= assuan_get_pointer(ctx
);
2467 gpg_error_t rc
= file_modified(client
);
2470 return send_error(ctx
, rc
);
2472 unlock_file_mutex(client
);
2473 return send_error(ctx
, 0);
2476 static int getpid_command(assuan_context_t ctx
, gchar
*line
)
2480 pid_t pid
= getpid();
2482 print_fmt(buf
, sizeof(buf
), "%i", pid
);
2483 rc
= assuan_send_data(ctx
, buf
, strlen(buf
));
2484 return send_error(ctx
, rc
);
2487 void cleanup_assuan(assuan_context_t ctx
)
2489 struct client_s
*cl
= assuan_get_pointer(ctx
);
2495 static void reset_notify(assuan_context_t ctx
)
2497 struct client_s
*cl
= assuan_get_pointer(ctx
);
2503 static gpg_error_t
parse_client_option(assuan_context_t ctx
, const gchar
*line
)
2505 gchar name
[32] = {0}, value
[256] = {0};
2507 if (sscanf(line
, " %31[a-zA-Z] = %255c", name
, value
) != 2)
2508 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_SYNTAX
);
2510 if (g_strcasecmp(name
, (gchar
*)"NAME") == 0) {
2511 pth_attr_t attr
= pth_attr_of(pth_self());
2513 log_write("OPTION CLIENT %s", line
);
2514 pth_attr_set(attr
, PTH_ATTR_NAME
, value
);
2515 pth_attr_destroy(attr
);
2518 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2523 static int option_handler(assuan_context_t ctx
, const gchar
*name
,
2526 struct client_s
*client
= assuan_get_pointer(ctx
);
2528 if (!value
|| !*value
)
2529 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2531 if (g_strcasecmp(name
, (gchar
*)"client") == 0)
2532 return parse_client_option(ctx
, value
);
2534 if (g_strcasecmp(name
, (gchar
*)"iterations") == 0) {
2539 n
= strtol(value
, &p
, 10);
2541 if (errno
|| (p
&& *p
) || n
< -1)
2542 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2544 g_key_file_set_integer(keyfileh
, client
->filename
? client
->filename
: "global", "iterations", n
);
2545 send_status_all(STATUS_CONFIG
);
2547 #ifdef WITH_PINENTRY
2548 else if (g_strcasecmp(name
, (gchar
*)"ttyname") == 0) {
2549 g_free(client
->pinentry
->ttyname
);
2550 client
->pinentry
->ttyname
= g_strdup(value
);
2552 else if (g_strcasecmp(name
, (gchar
*)"ttytype") == 0) {
2553 g_free(client
->pinentry
->ttytype
);
2554 client
->pinentry
->ttytype
= g_strdup(value
);
2556 else if (g_strcasecmp(name
, (gchar
*)"display") == 0) {
2557 g_free(client
->pinentry
->display
);
2558 client
->pinentry
->display
= g_strdup(value
);
2560 else if (g_strcasecmp(name
, (gchar
*)"path") == 0) {
2561 g_free(client
->pinentry
->path
);
2562 client
->pinentry
->path
= g_strdup(value
);
2564 else if (g_strcasecmp(name
, (gchar
*)"title") == 0) {
2565 g_free(client
->pinentry
->title
);
2566 client
->pinentry
->title
= g_strdup(value
);
2568 else if (g_strcasecmp(name
, (gchar
*)"prompt") == 0) {
2569 g_free(client
->pinentry
->prompt
);
2570 client
->pinentry
->prompt
= g_strdup(value
);
2572 else if (g_strcasecmp(name
, (gchar
*)"desc") == 0) {
2573 g_free(client
->pinentry
->desc
);
2574 client
->pinentry
->desc
= g_strdup(value
);
2577 * Look at client_thread() to see how this works.
2579 else if (g_strcasecmp(name
, (gchar
*)"timeout") == 0) {
2581 gint n
= strtol(value
, &p
, 10);
2584 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2586 client
->pinentry
->timeout
= n
;
2588 else if (g_strcasecmp(name
, (gchar
*)"pinentry") == 0) {
2590 gint n
= strtol(value
, &p
, 10);
2592 if (*p
|| n
< 0 || n
> 1)
2593 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2595 client
->pinentry
->enable
= n
== 0 ? FALSE
: TRUE
;
2599 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2601 log_write("OPTION %s=%s", name
, value
);
2605 gpg_error_t
register_commands(assuan_context_t ctx
)
2609 gint (*handler
)(assuan_context_t
, gchar
*line
);
2611 { "OPEN", open_command
},
2612 { "SAVE", save_command
},
2613 { "LIST", list_command
},
2614 { "REALPATH", realpath_command
},
2615 { "STORE", store_command
},
2616 { "DELETE", delete_command
},
2617 { "GET", get_command
},
2618 { "ATTR", attr_command
},
2619 { "ISCACHED", iscached_command
},
2620 { "CLEARCACHE", clearcache_command
},
2621 { "CACHETIMEOUT", cachetimeout_command
},
2622 { "GETCONFIG", getconfig_command
},
2623 { "DUMP", dump_command
},
2624 { "XPATH", xpath_command
},
2625 { "IMPORT", import_command
},
2626 { "LOCK", lock_command
},
2627 { "UNLOCK", unlock_command
},
2628 { "GETPID", getpid_command
},
2635 for (i
=0; table
[i
].name
; i
++) {
2636 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2642 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2647 rc
= assuan_register_option_handler(ctx
, option_handler
);
2652 rc
= assuan_register_reset_notify(ctx
, reset_notify
);
2657 return assuan_register_post_cmd_notify(ctx
, command_finalize
);
2660 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2661 guchar
*key
, gint
*dst_iter
)
2666 guchar tkey
[gcrykeysize
];
2667 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2668 gcry_cipher_hd_t gh
;
2669 guint iter
= 0, n_iter
= 0;
2670 gint iter_progress
= 0;
2671 void *outbuf
= NULL
;
2675 file_header_t file_header
;
2676 gchar str
[ASSUAN_LINELENGTH
];
2679 rc
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2687 lseek(fd
, 0, SEEK_SET
);
2688 insize
= st
.st_size
- sizeof(file_header_t
);
2689 iv
= gcry_malloc(gcryblocksize
);
2693 gcry_cipher_close(gh
);
2695 return gpg_error_from_errno(ENOMEM
);
2698 len
= pth_read(fd
, &file_header
, sizeof(file_header_t
));
2700 if (len
!= sizeof(file_header_t
)) {
2704 gcry_cipher_close(gh
);
2708 return gpg_error_from_errno(errno
);
2711 *dst_iter
= file_header
.iter
;
2713 /* No encryption iterations. This is a plain (gzipped) file. */
2714 if (file_header
.iter
== -1) {
2716 * cache_file_count() needs both .used == TRUE and a valid key in
2717 * order for it to count as a used cache entry. Fixes CACHE status
2720 memset(key
, '!', gcrykeysize
);
2721 insize
= st
.st_size
- sizeof(file_header_t
);
2724 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2725 inbuf
= gcry_malloc(insize
);
2729 gcry_cipher_close(gh
);
2732 return gpg_error_from_errno(ENOMEM
);
2735 len
= pth_read(fd
, inbuf
, insize
);
2737 if (len
!= insize
) {
2741 gcry_cipher_close(gh
);
2745 return gpg_error_from_errno(errno
);
2748 if (file_header
.iter
== -1)
2751 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2753 gcry_cipher_close(gh
);
2754 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2757 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2764 if ((rc
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2766 gcry_cipher_close(gh
);
2767 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2770 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2776 gcry_cipher_close(gh
);
2781 if (client
&& client
->filename
)
2782 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
2784 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2785 rc
= assuan_write_status(client
->ctx
, "DECRYPT",
2786 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
2795 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2803 memcpy(tkey
, key
, sizeof(tkey
));
2806 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2808 gcry_cipher_close(gh
);
2809 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2812 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2814 memset(tkey
, 0, sizeof(tkey
));
2820 memset(tkey
, 0, sizeof(tkey
));
2822 while (iter
< file_header
.iter
) {
2823 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2824 if (!(iter
% iter_progress
)) {
2825 rc
= assuan_write_status(ctx
, "DECRYPT",
2826 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
2836 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2838 gcry_cipher_close(gh
);
2839 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2842 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2849 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2853 gcry_cipher_close(gh
);
2854 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2857 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2868 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2869 rc
= assuan_write_status(ctx
, "DECRYPT",
2870 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
2882 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
2884 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2886 if (zrc
== Z_MEM_ERROR
) {
2888 return gpg_error_from_errno(ENOMEM
);
2890 else if (zrc
!= Z_DATA_ERROR
) {
2894 gcry_cipher_close(gh
);
2896 return EPWMD_BADKEY
;
2905 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2909 gcry_cipher_close(gh
);
2911 return EPWMD_BADKEY
;
2915 client
->xml
= inbuf
;
2916 client
->len
= insize
;
2919 gcry_cipher_close(gh
);
2927 * This is called after every Assuan command.
2929 void command_finalize(assuan_context_t ctx
, gint rc
)
2931 struct client_s
*client
= assuan_get_pointer(ctx
);
2933 if (!client
->is_lock_cmd
)
2934 unlock_file_mutex(client
);