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 02111-1307 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
;
219 struct pinentry_s
*pin
= client
->pinentry
;
222 unlock_file_mutex(client
);
223 CACHE_LOCK(client
->ctx
);
224 cache_decr_refcount(client
->md5file
);
227 * This may be a new file so don't use a cache slot. save_command() will
228 * set this to FALSE on success.
230 if (client
->new == TRUE
)
231 cache_clear(client
->md5file
, 1);
234 memset(client
, 0, sizeof(struct client_s
));
235 client
->state
= STATE_CONNECTED
;
237 client
->freed
= TRUE
;
239 client
->pinentry
= pin
;
244 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gint insize
,
245 gpointer
*out
, glong
*outsize
, gint
*rc
)
251 gchar str
[ASSUAN_LINELENGTH
];
257 z
.avail_out
= zlib_bufsize
;
258 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
261 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
266 *rc
= inflateInit2(&z
, 47);
269 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
274 memset(&h
, 0, sizeof(gz_header
));
275 h
.comment
= (guchar
*)buf
;
276 h
.comm_max
= sizeof(buf
);
277 *rc
= inflateGetHeader(&z
, &h
);
280 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
286 *rc
= inflate(&z
, Z_BLOCK
);
289 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
296 insize
= atoi((gchar
*)h
.comment
);
301 *rc
= inflate(&z
, Z_FINISH
);
308 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
316 z
.next_out
= pout
+ z
.total_out
;
317 z
.avail_out
= zlib_bufsize
;
320 *rc
= assuan_write_status(ctx
, "DECOMPRESS",
321 print_fmt(str
, sizeof(str
), "%i %i", z
.total_out
, insize
));
336 } while (*rc
!= Z_STREAM_END
);
339 *rc
= assuan_write_status(ctx
, "DECOMPRESS",
340 print_fmt(str
, sizeof(str
), "%i %i", z
.total_out
, insize
));
347 *outsize
= z
.total_out
;
353 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
359 gpg_error_t
read_file_header(const gchar
*filename
, file_header_t
*fh
)
364 fd
= open(filename
, O_RDONLY
);
367 return gpg_error_from_errno(errno
);
369 len
= pth_read(fd
, fh
, sizeof(file_header_t
));
372 if (len
!= sizeof(file_header_t
))
373 return gpg_error_from_errno(errno
);
378 static gpg_error_t
open_command_finalize(assuan_context_t ctx
, guchar shakey
[],
381 struct client_s
*client
= assuan_get_pointer(ctx
);
388 if ((fd
= open_file(client
->filename
, &st
)) == -1) {
390 if (errno
== ENOENT
) {
398 log_write("%s: %s", client
->filename
, strerror(errno
));
399 cleanup_client(client
);
400 memset(shakey
, 0, sizeof(shakey
));
401 return send_syserror(ctx
, rc
);
404 rc
= try_xml_decrypt(ctx
, fd
, st
, shakey
, &iter
);
408 memset(shakey
, 0, sizeof(shakey
));
409 cleanup_client(client
);
410 return send_error(ctx
, rc
);
414 CACHE_LOCK(client
->ctx
);
416 if (cached
== FALSE
) {
417 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
418 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
419 cleanup_client(client
);
421 return send_error(ctx
, EPWMD_MAX_SLOTS
);
424 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
425 cache_reset_timeout(client
->md5file
, timeout
);
428 cache_set_timeout(client
->md5file
, -2);
433 memset(shakey
, 0, sizeof(shakey
));
437 gcry_free(client
->xml
);
442 if (client
->new == FALSE
)
443 send_status_all(STATUS_CACHE
);
445 client
->state
= STATE_OPEN
;
448 if (!rc
&& client
->new == FALSE
&&
449 iter
!= get_key_file_integer(client
->filename
, "iterations")) {
450 g_key_file_set_integer(keyfileh
, client
->filename
, "iterations", iter
);
451 send_status_all(STATUS_CONFIG
);
454 return send_error(ctx
, rc
);
457 static int open_command(assuan_context_t ctx
, char *line
)
460 guchar shakey
[gcrykeysize
];
461 gboolean cached
= FALSE
;
463 struct client_s
*client
= assuan_get_pointer(ctx
);
465 gchar
*filename
= NULL
;
466 file_header_t file_header
;
468 memset(shakey
, 0, sizeof(shakey
));
470 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
473 if (!filename
|| !*filename
) {
475 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
478 if (valid_filename(filename
) == FALSE
) {
480 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
483 if (client
->state
== STATE_OPEN
)
484 cleanup_client(client
);
486 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
487 CACHE_LOCK(client
->ctx
);
489 if (cache_has_file(client
->md5file
) == FALSE
) {
490 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
493 return send_error(ctx
, EPWMD_MAX_SLOTS
);
497 cache_incr_refcount(client
->md5file
);
499 rc
= lock_file_mutex(client
);
503 return send_error(ctx
, rc
);
506 client
->freed
= FALSE
;
508 if ((rc
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
510 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
511 cleanup_client(client
);
512 return send_error(ctx
, rc
);
515 if (stat(filename
, &st
) == 0) {
516 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
517 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
519 cleanup_client(client
);
520 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
523 client
->mtime
= st
.st_mtime
;
526 client
->filename
= g_strdup(filename
);
528 if (!client
->filename
) {
529 memset(shakey
, 0, sizeof(shakey
));
530 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
531 cleanup_client(client
);
533 return send_syserror(ctx
, ENOMEM
);
537 client
->pinentry
->filename
= g_strdup(client
->filename
);
539 if (!client
->pinentry
->filename
) {
540 memset(shakey
, 0, sizeof(shakey
));
541 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
542 cleanup_client(client
);
544 return send_syserror(ctx
, ENOMEM
);
549 * New files don't need a key.
551 if (access(filename
, R_OK
) != 0) {
552 if (errno
!= ENOENT
) {
554 log_write("%s: %s", filename
, strerror(errno
));
556 cleanup_client(client
);
557 return send_syserror(ctx
, rc
);
560 if ((client
->xml
= new_document()) == NULL
) {
561 log_write("%s", strerror(ENOMEM
));
563 cleanup_client(client
);
564 return send_syserror(ctx
, ENOMEM
);
567 client
->len
= xmlStrlen(client
->xml
);
569 client
->filename
= g_strdup(filename
);
571 if (!client
->filename
) {
573 cleanup_client(client
);
574 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
575 return send_syserror(ctx
, ENOMEM
);
578 memset(shakey
, 0, sizeof(shakey
));
580 if (req
[1] && *req
[1])
581 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
584 return open_command_finalize(ctx
, shakey
, cached
);
587 rc
= read_file_header(filename
, &file_header
);
591 cleanup_client(client
);
592 return send_error(ctx
, rc
);
595 if (file_header
.iter
== -1)
598 CACHE_LOCK(client
->ctx
);
599 cached
= cache_get_key(client
->md5file
, shakey
);
602 if (cached
== FALSE
) {
604 * No key specified and no matching filename found in the cache. Use
605 * pinentry to retrieve the key. Cannot return assuan_process_done()
606 * here otherwise the command will be interrupted. The event loop in
607 * client_thread() will poll the file descriptor waiting for it to
608 * become ready to read a pinentry_key_s which will contain the
609 * entered key or rc. It will then call open_command_finalize() to
610 * to finish the command.
612 if (!req
[1] || !*req
[1]) {
614 gboolean b
= get_key_file_boolean(filename
, "enable_pinentry");
616 /* From set_pinentry_defaults(). */
617 if (client
->pinentry
->enable
== FALSE
||
618 (client
->pinentry
->enable
== -1 && b
== FALSE
)) {
619 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
624 rc
= lock_pin_mutex(client
);
627 unlock_pin_mutex(client
->pinentry
);
628 cleanup_client(client
);
629 return send_error(ctx
, rc
);
632 client
->pinentry
->which
= PINENTRY_OPEN
;
633 rc
= pinentry_fork(ctx
);
636 unlock_pin_mutex(client
->pinentry
);
637 cleanup_client(client
);
638 return send_error(ctx
, rc
);
641 client
->pinentry
->cb
= open_command_finalize
;
642 client
->pinentry
->status
= PINENTRY_INIT
;
645 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
650 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
655 return open_command_finalize(ctx
, shakey
, cached
);
658 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
659 gint size
, gpointer
*out
, glong
*outsize
, gint
*rc
)
665 gint cmd
= Z_NO_FLUSH
;
666 gchar str
[ASSUAN_LINELENGTH
];
670 z
.next_in
= pin
= data
;
671 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
672 z
.avail_out
= zlib_bufsize
;
673 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
676 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
681 *rc
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
684 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
689 memset(&h
, 0, sizeof(gz_header
));
690 g_snprintf(buf
, sizeof(buf
), "%i", size
);
691 h
.comment
= (guchar
*)buf
;
692 *rc
= deflateSetHeader(&z
, &h
);
695 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
704 *rc
= deflate(&z
, cmd
);
711 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
719 z
.next_out
= pout
+ z
.total_out
;
720 z
.avail_out
= zlib_bufsize
;
723 if (!z
.avail_in
&& z
.total_in
< size
) {
724 if (z
.total_in
+ zlib_bufsize
> size
)
725 z
.avail_in
= size
- z
.total_in
;
727 z
.avail_in
= zlib_bufsize
;
730 *rc
= assuan_write_status(ctx
, "COMPRESS",
731 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
738 if (z
.total_in
>= size
)
749 } while (*rc
!= Z_STREAM_END
);
752 *rc
= assuan_write_status(ctx
, "COMPRESS",
753 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
760 *outsize
= z
.total_out
;
766 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
772 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
773 const gchar
*filename
, gpointer data
, size_t insize
, guchar
*shakey
,
779 guchar tkey
[gcrykeysize
];
782 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
783 gchar tmp
[FILENAME_MAX
];
786 file_header_t file_header
;
787 gchar str
[ASSUAN_LINELENGTH
];
791 * cache_file_count() needs both .used == TRUE and a valid key in
792 * order for it to count as a used cache entry. Fixes CACHE status
795 memset(shakey
, '!', gcrykeysize
);
797 file_header
.iter
= iter
;
801 if (insize
/ gcryblocksize
) {
802 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
804 if (insize
% gcryblocksize
)
805 len
+= gcryblocksize
;
809 * Resize the existing xml buffer to the block size required by gcrypt
810 * rather than duplicating it and wasting memory.
812 inbuf
= gcry_realloc(data
, len
);
815 return gpg_error_from_errno(ENOMEM
);
818 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
819 memcpy(tkey
, shakey
, sizeof(tkey
));
822 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
823 memset(tkey
, 0, sizeof(tkey
));
825 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
829 memset(tkey
, 0, sizeof(tkey
));
830 file_header
.iter
= iter
;
833 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
835 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
836 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
837 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
845 while (xiter
< file_header
.iter
) {
846 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
847 if (!(xiter
% iter_progress
)) {
848 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
849 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
858 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
859 sizeof(file_header
.iv
)))) {
861 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
865 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
868 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
876 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
877 sizeof(file_header
.iv
)))) {
879 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
883 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
885 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
889 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
891 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
895 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
896 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
897 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
907 if (stat(filename
, &st
) == 0) {
908 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
911 * FIXME What if the file has an ACL?
913 if (!(mode
& S_IWUSR
)) {
915 return gpg_error_from_errno(EACCES
);
919 if (errno
!= ENOENT
) {
922 return gpg_error_from_errno(rc
);
926 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
928 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
931 p
= strrchr(tmp
, '/');
933 log_write("%s: %s", p
, strerror(rc
));
934 return gpg_error_from_errno(rc
);
939 * xml_import() from command line.
943 len
= pth_write(fd
, &file_header
, sizeof(file_header_t
));
945 if (len
!= sizeof(file_header
)) {
954 return gpg_error_from_errno(len
);
957 len
= pth_write(fd
, inbuf
, insize
);
968 return gpg_error_from_errno(len
);
971 if (fsync(fd
) == -1) {
980 return gpg_error_from_errno(len
);
986 if (mode
&& get_key_file_boolean(filename
, "backup") == TRUE
) {
987 gchar tmp2
[FILENAME_MAX
];
989 g_snprintf(tmp2
, sizeof(tmp2
), "%s.backup", filename
);
991 if (rename(filename
, tmp2
) == -1) {
995 return gpg_error_from_errno(len
);
999 if (rename(tmp
, filename
) == -1) {
1003 return gpg_error_from_errno(len
);
1007 chmod(filename
, mode
);
1014 static gpg_error_t
save_command_finalize(assuan_context_t ctx
,
1015 guchar shakey
[], gboolean cached
)
1017 struct client_s
*client
= assuan_get_pointer(ctx
);
1029 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
1032 iter
= get_key_file_integer(client
->filename
, "compression_level");
1037 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
1038 memset(shakey
, 0, sizeof(shakey
));
1041 if (zrc
== Z_MEM_ERROR
) {
1042 return send_syserror(ctx
, ENOMEM
);
1045 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
1053 iter
= get_key_file_integer(client
->filename
, "iterations");
1054 rc
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
1057 memset(shakey
, 0, sizeof(shakey
));
1058 return send_error(ctx
, rc
);
1061 stat(client
->filename
, &st
);
1062 client
->mtime
= st
.st_mtime
;
1063 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
1064 CACHE_LOCK(client
->ctx
);
1067 memset(shakey
, 0, sizeof(shakey
));
1068 cache_reset_timeout(client
->md5file
, timeout
);
1071 if (client
->new == TRUE
)
1072 send_status_all(STATUS_CACHE
);
1074 client
->new = FALSE
;
1075 return send_error(ctx
, 0);
1078 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
1079 memset(shakey
, 0, sizeof(shakey
));
1080 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
1082 return send_error(ctx
, EPWMD_MAX_SLOTS
);
1085 client
->new = FALSE
;
1086 memset(shakey
, 0, sizeof(shakey
));
1087 cache_reset_timeout(client
->md5file
, timeout
);
1089 send_status_all(STATUS_CACHE
);
1090 return send_error(ctx
, 0);
1093 static int save_command(assuan_context_t ctx
, char *line
)
1095 gboolean cached
= FALSE
;
1096 guchar shakey
[gcrykeysize
];
1098 struct client_s
*client
= assuan_get_pointer(ctx
);
1101 memset(shakey
, 0, sizeof(shakey
));
1102 rc
= file_modified(client
);
1105 log_write("%s: %s", client
->filename
? client
->filename
: "",
1107 return send_error(ctx
, rc
);
1110 rc
= lock_file_mutex(client
);
1113 return send_error(ctx
, rc
);
1115 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1116 return send_syserror(ctx
, errno
);
1118 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
1119 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
1120 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
1123 if (get_key_file_integer(client
->filename
, "iterations") == -1)
1126 if (!line
|| !*line
) {
1127 guchar tmp
[sizeof(shakey
)];
1130 memset(tmp
, '!', sizeof(tmp
));
1132 if (cache_get_key(client
->md5file
, shakey
) == FALSE
||
1133 memcmp(shakey
, tmp
, sizeof(shakey
)) == 0) {
1135 #ifdef WITH_PINENTRY
1136 if (get_key_file_boolean(client
->filename
, "enable_pinentry") == FALSE
) {
1137 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1141 lock_pin_mutex(client
);
1142 client
->pinentry
->which
= PINENTRY_SAVE
;
1143 rc
= pinentry_fork(ctx
);
1146 unlock_pin_mutex(client
->pinentry
);
1147 return send_error(ctx
, rc
);
1150 client
->pinentry
->cb
= save_command_finalize
;
1151 client
->pinentry
->status
= PINENTRY_INIT
;
1154 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1164 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
1165 memset(line
, 0, strlen(line
));
1169 return save_command_finalize(ctx
, shakey
, cached
);
1172 static int delete_command(assuan_context_t ctx
, char *line
)
1174 struct client_s
*client
= assuan_get_pointer(ctx
);
1179 rc
= file_modified(client
);
1182 log_write("%s: %s", client
->filename
? client
->filename
: "",
1184 return send_error(ctx
, rc
);
1187 if (strchr(line
, '\t'))
1188 req
= split_input_line(line
, "\t", -1);
1190 req
= split_input_line(line
, " ", -1);
1193 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1195 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1199 return send_error(ctx
, rc
);
1203 * No sub-node defined. Remove the entire node (account).
1212 return send_error(ctx
, 0);
1215 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1219 return send_error(ctx
, rc
);
1226 return send_error(ctx
, 0);
1230 * Don't return with assuan_process_done() here. This has been called from
1231 * assuan_process_next() and the command should be finished in
1234 static int store_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
1237 assuan_context_t ctx
= data
;
1238 struct client_s
*client
= assuan_get_pointer(ctx
);
1241 gpg_error_t rc
= file_modified(client
);
1243 if (assuan_rc
|| rc
) {
1250 return assuan_rc
? assuan_rc
: rc
;
1253 req
= split_input_line((gchar
*)line
, "\t", 0);
1261 return EPWMD_COMMAND_SYNTAX
;
1263 if (valid_xml_element((xmlChar
*)*req
) == FALSE
) {
1265 return EPWMD_INVALID_ELEMENT
;
1268 if (valid_element_path(req
+1, TRUE
) == FALSE
) {
1270 return EPWMD_INVALID_ELEMENT
;
1274 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1276 if (rc
&& rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1277 rc
= new_account(client
->doc
, *req
);
1294 create_elements_cb(n
, req
+1, &rc
, NULL
);
1296 find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1297 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1301 client
->inquire_status
= INQUIRE_DONE
;
1305 static int store_command(assuan_context_t ctx
, char *line
)
1307 struct client_s
*client
= assuan_get_pointer(ctx
);
1308 gpg_error_t rc
= file_modified(client
);
1311 log_write("%s: %s", client
->filename
? client
->filename
: "",
1313 return send_error(ctx
, rc
);
1316 rc
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1319 return send_error(ctx
, rc
);
1321 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1322 client
->inquire_status
= INQUIRE_BUSY
;
1326 static int get_command(assuan_context_t ctx
, char *line
)
1328 struct client_s
*client
= assuan_get_pointer(ctx
);
1333 rc
= file_modified(client
);
1336 log_write("%s: %s", client
->filename
? client
->filename
: "",
1338 return send_error(ctx
, rc
);
1341 req
= split_input_line(line
, "\t", -1);
1343 if (!req
|| !*req
) {
1345 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1348 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1352 return send_error(ctx
, rc
);
1356 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1361 return send_error(ctx
, rc
);
1363 if (!n
|| !n
->children
)
1364 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1366 n
= find_text_node(n
->children
);
1368 if (!n
|| !n
->content
|| !*n
->content
)
1369 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1371 rc
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1372 return send_error(ctx
, rc
);
1375 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**target
,
1376 gpg_error_t
*rc
, gchar
**req_orig
, void *data
)
1378 gchar
*path
= *(gchar
**)data
;
1379 gchar
*tmp
= NULL
, *result
;
1383 *(gchar
**)data
= NULL
;
1386 path
= g_strjoinv("\t", target
);
1389 *rc
= gpg_error_from_errno(ENOMEM
);
1394 tmp
= g_strjoinv("\t", req_orig
);
1398 *rc
= gpg_error_from_errno(ENOMEM
);
1404 result
= g_strdup_printf("%s\t%s", path
, tmp
);
1406 result
= g_strdup(path
);
1409 *rc
= gpg_error_from_errno(ENOMEM
);
1417 *(gchar
**)data
= result
;
1421 static int realpath_command(assuan_context_t ctx
, char *line
)
1424 struct client_s
*client
= assuan_get_pointer(ctx
);
1432 rc
= file_modified(client
);
1435 log_write("%s: %s", client
->filename
? client
->filename
: "",
1437 return send_error(ctx
, rc
);
1440 if (strchr(line
, '\t') != NULL
) {
1441 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1442 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1445 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1446 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1449 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1453 return send_error(ctx
, rc
);
1456 rp
= g_strjoinv("\t", req
);
1460 return send_syserror(ctx
, ENOMEM
);
1464 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1465 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, &rp
);
1470 return send_error(ctx
, rc
);
1474 string
= g_string_new(rp
);
1479 return send_syserror(ctx
, ENOMEM
);
1482 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++) {
1483 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+1) && *(t
+1) != '!')) {
1484 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1489 rc
= assuan_send_data(ctx
, string
->str
, string
->len
);
1490 g_string_free(string
, TRUE
);
1491 return send_error(ctx
, rc
);
1494 static int list_command(assuan_context_t ctx
, char *line
)
1496 struct client_s
*client
= assuan_get_pointer(ctx
);
1498 struct element_list_s
*elements
= NULL
;
1501 if (disable_list_and_dump
== TRUE
)
1502 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1504 rc
= file_modified(client
);
1507 log_write("%s: %s", client
->filename
, pwmd_strerror(rc
));
1508 return send_error(ctx
, rc
);
1514 rc
= list_accounts(client
->doc
, &str
);
1517 return send_error(ctx
, rc
);
1519 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1520 g_string_free(str
, TRUE
);
1521 return send_error(ctx
, rc
);
1524 elements
= g_malloc0(sizeof(struct element_list_s
));
1527 rc
= gpg_err_code_from_errno(ENOMEM
);
1531 rc
= create_path_list(client
->doc
, elements
, line
);
1537 gint total
= g_slist_length(elements
->list
);
1542 rc
= EPWMD_EMPTY_ELEMENT
;
1546 str
= g_string_new(NULL
);
1549 rc
= gpg_err_code_from_errno(ENOMEM
);
1553 for (i
= 0; i
< total
; i
++) {
1554 tmp
= g_slist_nth_data(elements
->list
, i
);
1555 g_string_append_printf(str
, "%s%s", tmp
, i
+1 == total
? "" : "\n");
1558 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1559 g_string_free(str
, TRUE
);
1562 rc
= EPWMD_EMPTY_ELEMENT
;
1566 gint total
= g_slist_length(elements
->list
);
1569 for (i
= 0; i
< total
; i
++) {
1570 tmp
= g_slist_nth_data(elements
->list
, i
);
1574 g_slist_free(elements
->list
);
1576 if (elements
->prefix
)
1577 g_free(elements
->prefix
);
1582 return send_error(ctx
, rc
);
1585 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1590 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1591 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1594 return EPWMD_LIBXML_ERROR
;
1597 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1603 * req[0] - element path
1605 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1607 struct client_s
*client
= assuan_get_pointer(ctx
);
1608 gchar
**attrlist
= NULL
;
1610 gchar
**path
= NULL
;
1616 if (!req
|| !req
[0])
1617 return EPWMD_COMMAND_SYNTAX
;
1619 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1621 * The first argument may be only an account.
1623 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1624 return EPWMD_COMMAND_SYNTAX
;
1627 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1635 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1636 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1646 for (a
= n
->properties
; a
; a
= a
->next
) {
1649 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1651 g_strfreev(attrlist
);
1653 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1654 return gpg_error_from_errno(ENOMEM
);
1659 attrlist
[i
] = g_strdup_printf("%s %s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1662 g_strfreev(attrlist
);
1663 return gpg_error_from_errno(ENOMEM
);
1666 attrlist
[++i
] = NULL
;
1670 return EPWMD_EMPTY_ELEMENT
;
1672 line
= g_strjoinv("\n", attrlist
);
1675 g_strfreev(attrlist
);
1676 return gpg_error_from_errno(ENOMEM
);
1679 rc
= assuan_send_data(ctx
, line
, strlen(line
));
1681 g_strfreev(attrlist
);
1686 * req[0] - attribute
1687 * req[1] - element path
1689 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1693 gchar
**path
= NULL
;
1696 if (!req
|| !req
[0] || !req
[1])
1697 return EPWMD_COMMAND_SYNTAX
;
1699 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1701 * The first argument may be only an account.
1703 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1704 return EPWMD_COMMAND_SYNTAX
;
1708 * Don't remove the "name" attribute for the account element. To remove an
1709 * account use DELETE <account>.
1711 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1712 rc
= EPWMD_ATTR_SYNTAX
;
1716 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1722 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1723 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1731 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1732 return EPWMD_ATTR_NOT_FOUND
;
1734 if (xmlRemoveProp(a
) == -1)
1735 return EPWMD_LIBXML_ERROR
;
1744 static xmlNodePtr
create_element_path(struct client_s
*client
, gchar
***path
,
1747 gchar
**src
= *path
;
1748 gchar
**src_orig
= g_strdupv(src
);
1749 xmlNodePtr n
= NULL
;
1754 *rc
= gpg_error_from_errno(ENOMEM
);
1759 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1762 if (*rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1763 *rc
= new_account(client
->doc
, src
[0]);
1776 n
= create_target_elements_cb(n
, src
+1, rc
, NULL
);
1778 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1779 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
1785 * Reset the position of the element tree now that the elements
1786 * have been created.
1791 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1796 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1797 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1805 g_strfreev(src_orig
);
1812 * Creates a "target" attribute. When other commands encounter an element with
1813 * this attribute, the element path is modified to the target value. If the
1814 * source element path doesn't exist when using 'ATTR SET target', it is
1815 * created, but the destination element path must exist.
1817 * req[0] - source element path
1818 * req[1] - destination element path
1820 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1822 gchar
**src
, **dst
, *line
= NULL
;
1826 if (!req
|| !req
[0] || !req
[1])
1827 return EPWMD_COMMAND_SYNTAX
;
1829 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1831 * The first argument may be only an account.
1833 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1834 return EPWMD_COMMAND_SYNTAX
;
1837 if (valid_element_path(src
, FALSE
) == FALSE
) {
1839 return EPWMD_INVALID_ELEMENT
;
1842 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1844 * The first argument may be only an account.
1846 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1847 rc
= EPWMD_COMMAND_SYNTAX
;
1852 n
= find_account(client
->doc
, &dst
, &rc
, NULL
, 0);
1855 * Make sure the destination element path exists.
1861 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
1862 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1868 n
= create_element_path(client
, &src
, &rc
);
1873 line
= g_strjoinv("\t", dst
);
1876 rc
= gpg_error_from_errno(ENOMEM
);
1880 rc
= add_attribute(n
, "target", line
);
1890 * req[0] - account name
1893 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
1899 tmp
= g_strdupv(req
);
1902 return gpg_error_from_errno(ENOMEM
);
1904 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1910 if (g_utf8_collate(req
[0], req
[1]) == 0)
1914 * Will not overwrite an existing account.
1916 tmp
= g_strdupv(req
+1);
1919 return gpg_error_from_errno(ENOMEM
);
1921 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1924 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
)
1928 return EPWMD_ACCOUNT_EXISTS
;
1931 * Whitespace not allowed in account names.
1933 if (contains_whitespace(req
[1]) == TRUE
)
1934 return EPWMD_ATTR_SYNTAX
;
1936 tmp
= g_strdupv(req
);
1939 return gpg_error_from_errno(ENOMEM
);
1941 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1945 return EPWMD_ELEMENT_NOT_FOUND
;
1947 return add_attribute(n
, "name", req
[1]);
1951 * req[0] - attribute
1952 * req[1] - element path
1954 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
1956 struct client_s
*client
= assuan_get_pointer(ctx
);
1962 if (!req
|| !req
[0] || !req
[1])
1963 return EPWMD_COMMAND_SYNTAX
;
1965 if (strchr(req
[1], '\t')) {
1966 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
1967 return EPWMD_COMMAND_SYNTAX
;
1970 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1971 return EPWMD_COMMAND_SYNTAX
;
1974 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1980 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1981 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1989 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
1990 return EPWMD_ATTR_NOT_FOUND
;
1992 rc
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
2002 * req[0] - attribute
2003 * req[1] - element path
2006 static int attribute_set(struct client_s
*client
, gchar
**req
)
2008 gchar
**path
= NULL
;
2012 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2013 return EPWMD_COMMAND_SYNTAX
;
2016 * Reserved attribute names.
2018 if (g_utf8_collate(req
[0], "name") == 0) {
2020 * Only reserved for the account element. Not the rest of the
2023 if (strchr(req
[1], '\t') == NULL
)
2024 return name_attribute(client
, req
+ 1);
2026 else if (g_utf8_collate(req
[0], "target") == 0)
2027 return target_attribute(client
, req
+ 1);
2029 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2031 * The first argument may be only an account.
2033 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2034 return EPWMD_COMMAND_SYNTAX
;
2037 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2043 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2044 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2051 return add_attribute(n
, req
[0], req
[2]);
2060 * req[1] - attribute name or element path if command is LIST
2061 * req[2] - element path
2062 * req[2] - element path or value
2064 static int attr_command(assuan_context_t ctx
, char *line
)
2066 struct client_s
*client
= assuan_get_pointer(ctx
);
2070 rc
= file_modified(client
);
2073 log_write("%s: %s", client
->filename
? client
->filename
: "",
2075 return send_error(ctx
, rc
);
2078 req
= split_input_line(line
, " ", 4);
2080 if (!req
|| !req
[0] || !req
[1]) {
2082 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2085 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2086 rc
= attribute_set(client
, req
+1);
2087 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2088 rc
= attribute_get(ctx
, req
+1);
2089 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2090 rc
= attribute_delete(client
, req
+1);
2091 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2092 rc
= attribute_list(ctx
, req
+1);
2094 rc
= EPWMD_COMMAND_SYNTAX
;
2097 return send_error(ctx
, rc
);
2100 static int iscached_command(assuan_context_t ctx
, char *line
)
2102 gchar
**req
= split_input_line(line
, " ", 0);
2105 if (!req
|| !*req
) {
2107 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2110 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2114 if (cache_iscached(md5file
) == FALSE
) {
2116 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2120 return send_error(ctx
, 0);
2123 static int clearcache_command(assuan_context_t ctx
, char *line
)
2125 struct client_s
*client
= assuan_get_pointer(ctx
);
2126 gchar
**req
= split_input_line(line
, " ", 0);
2131 if (!req
|| !*req
) {
2133 cache_clear(client
->md5file
, 2);
2135 return send_error(ctx
, 0);
2138 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2141 if (cache_clear(md5file
, 1) == FALSE
) {
2143 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2147 return send_error(ctx
, 0);
2150 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2154 gchar
**req
= split_input_line(line
, " ", 0);
2156 struct client_s
*client
= assuan_get_pointer(ctx
);
2158 if (!req
|| !*req
|| !req
[1]) {
2160 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2164 timeout
= strtol(req
[0], &p
, 10);
2166 if (errno
!= 0 || *p
!= 0) {
2168 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2171 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2173 CACHE_LOCK(client
->ctx
);
2175 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2177 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2181 return send_error(ctx
, 0);
2184 static int dump_command(assuan_context_t ctx
, char *line
)
2188 struct client_s
*client
= assuan_get_pointer(ctx
);
2191 if (disable_list_and_dump
== TRUE
)
2192 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2194 rc
= file_modified(client
);
2197 log_write("%s: %s", client
->filename
? client
->filename
: "",
2199 return send_error(ctx
, rc
);
2202 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2205 return send_syserror(ctx
, ENOMEM
);
2207 rc
= assuan_send_data(ctx
, xml
, len
);
2209 return send_error(ctx
, rc
);
2212 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2214 struct client_s
*client
= assuan_get_pointer(ctx
);
2216 gchar filename
[255]={0}, param
[747]={0};
2217 gchar
*p
, *tmp
, *fp
= client
->filename
, *paramp
= line
;
2219 if (strchr(line
, ' ')) {
2220 sscanf(line
, " %254[a-zA-Z] %746c", filename
, param
);
2225 paramp
= g_ascii_strdown(paramp
, -1);
2228 return send_syserror(ctx
, ENOMEM
);
2230 if (strcmp(paramp
, "key") == 0 || strcmp(paramp
, "key_file") == 0) {
2232 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2235 p
= get_key_file_string(fp
? fp
: "global", paramp
);
2239 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2241 tmp
= expand_homedir(p
);
2245 return send_syserror(ctx
, ENOMEM
);
2250 rc
= assuan_send_data(ctx
, p
, strlen(p
));
2252 return send_error(ctx
, rc
);
2255 static int xpath_command(assuan_context_t ctx
, gchar
*line
)
2257 struct client_s
*client
= assuan_get_pointer(ctx
);
2259 xmlXPathContextPtr xp
;
2260 xmlXPathObjectPtr result
;
2261 xmlBufferPtr buf
= NULL
;
2264 rc
= file_modified(client
);
2267 log_write("%s: %s", client
->filename
? client
->filename
: "",
2269 return send_error(ctx
, rc
);
2272 if (!line
|| !*line
)
2273 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2275 if ((req
= split_input_line(line
, "\t", 2)) == NULL
) {
2276 if (strv_printf(&req
, "%s", line
) == FALSE
)
2277 return send_syserror(ctx
, ENOMEM
);
2280 xp
= xmlXPathNewContext(client
->doc
);
2283 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2285 result
= xmlXPathEvalExpression((xmlChar
*)req
[0], xp
);
2288 xmlXPathFreeContext(xp
);
2289 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2292 if (xmlXPathNodeSetIsEmpty(result
->nodesetval
)) {
2293 rc
= EPWMD_EMPTY_ELEMENT
;
2297 rc
= recurse_xpath_nodeset(client
->doc
, result
->nodesetval
,
2298 (xmlChar
*)req
[1], &buf
);
2302 else if (!req
[1] && !xmlBufferLength(buf
)) {
2303 rc
= EPWMD_EMPTY_ELEMENT
;
2309 rc
= assuan_send_data(ctx
, xmlBufferContent(buf
), xmlBufferLength(buf
));
2318 xmlXPathFreeObject(result
);
2321 xmlXPathFreeContext(xp
);
2323 return send_error(ctx
, rc
);
2326 static int import_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
2329 struct client_s
*client
= assuan_get_pointer((assuan_context_t
)data
);
2330 gpg_error_t rc
= file_modified(client
);
2331 gchar
**req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2333 xmlNodePtr n
, root
, copy
;
2335 if (assuan_rc
|| rc
) {
2342 return assuan_rc
? assuan_rc
: rc
;
2345 req
= split_input_line((gchar
*)line
, " ", 2);
2353 return EPWMD_COMMAND_SYNTAX
;
2355 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2356 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
2357 return EPWMD_COMMAND_SYNTAX
;
2362 if (!content
|| !*content
) {
2363 rc
= EPWMD_COMMAND_SYNTAX
;
2367 if (valid_xml_element((xmlChar
*)*path
) == FALSE
) {
2368 rc
= EPWMD_INVALID_ELEMENT
;
2372 if (valid_element_path(path
+1, FALSE
) == FALSE
) {
2373 rc
= EPWMD_INVALID_ELEMENT
;
2377 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2380 rc
= EPWMD_LIBXML_ERROR
;
2384 root
= xmlDocGetRootElement(doc
);
2385 path_orig
= g_strdupv(path
);
2389 rc
= gpg_error_from_errno(ENOMEM
);
2393 if (strv_printf(&path
, "%s", (gchar
*)root
->name
) == FALSE
) {
2394 g_strfreev(path_orig
);
2396 rc
= gpg_error_from_errno(ENOMEM
);
2400 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2402 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2403 g_strfreev(path_orig
);
2408 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2410 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2411 g_strfreev(path_orig
);
2416 xmlNodePtr parent
= n
->parent
;
2427 if (rc
== EPWMD_ELEMENT_NOT_FOUND
) {
2428 n
= create_element_path(client
, &path
, &rc
);
2436 copy
= xmlCopyNode(root
, 1);
2437 n
= xmlAddChild(n
, copy
);
2441 rc
= EPWMD_LIBXML_ERROR
;
2446 client
->inquire_status
= INQUIRE_DONE
;
2450 static int import_command(assuan_context_t ctx
, gchar
*line
)
2453 struct client_s
*client
= assuan_get_pointer(ctx
);
2455 rc
= file_modified(client
);
2458 log_write("%s: %s", client
->filename
? client
->filename
: "",
2460 return send_error(ctx
, rc
);
2463 rc
= assuan_inquire_ext(ctx
, "IMPORT", 0, import_command_finalize
, ctx
);
2466 return send_error(ctx
, rc
);
2468 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
2469 client
->inquire_status
= INQUIRE_BUSY
;
2473 static int lock_command(assuan_context_t ctx
, gchar
*line
)
2476 struct client_s
*client
= assuan_get_pointer(ctx
);
2478 rc
= file_modified(client
);
2481 log_write("%s: %s", client
->filename
? client
->filename
: "",
2483 return send_error(ctx
, rc
);
2486 rc
= lock_file_mutex(client
);
2489 client
->is_lock_cmd
= TRUE
;
2491 return send_error(ctx
, rc
);
2494 static int unlock_command(assuan_context_t ctx
, gchar
*line
)
2497 struct client_s
*client
= assuan_get_pointer(ctx
);
2499 rc
= file_modified(client
);
2502 log_write("%s: %s", client
->filename
? client
->filename
: "",
2504 return send_error(ctx
, rc
);
2507 unlock_file_mutex(client
);
2508 return send_error(ctx
, 0);
2511 static int getpid_command(assuan_context_t ctx
, gchar
*line
)
2515 pid_t pid
= getpid();
2517 print_fmt(buf
, sizeof(buf
), "%i", pid
);
2518 rc
= assuan_send_data(ctx
, buf
, strlen(buf
));
2519 return send_error(ctx
, rc
);
2522 void cleanup_assuan(assuan_context_t ctx
)
2524 struct client_s
*cl
= assuan_get_pointer(ctx
);
2530 static void reset_notify(assuan_context_t ctx
)
2532 struct client_s
*cl
= assuan_get_pointer(ctx
);
2538 static gpg_error_t
parse_client_option(assuan_context_t ctx
, const gchar
*line
)
2540 gchar name
[32] = {0}, value
[256] = {0};
2542 if (sscanf(line
, " %31[a-zA-Z] = %255c", name
, value
) != 2)
2543 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_SYNTAX
);
2545 if (g_strcasecmp(name
, (gchar
*)"NAME") == 0) {
2546 pth_attr_t attr
= pth_attr_of(pth_self());
2548 log_write("OPTION CLIENT %s", line
);
2549 pth_attr_set(attr
, PTH_ATTR_NAME
, value
);
2550 pth_attr_destroy(attr
);
2553 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2558 static int option_handler(assuan_context_t ctx
, const gchar
*name
,
2561 #ifdef WITH_PINENTRY
2562 struct client_s
*client
= assuan_get_pointer(ctx
);
2565 if (!value
|| !*value
)
2566 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2568 if (g_strcasecmp(name
, (gchar
*)"client") == 0)
2569 return parse_client_option(ctx
, value
);
2571 if (g_strcasecmp(name
, (gchar
*)"iterations") == 0) {
2576 n
= strtol(value
, &p
, 10);
2578 if (errno
|| (p
&& *p
) || n
< -1)
2579 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2581 g_key_file_set_integer(keyfileh
, client
->filename
? client
->filename
: "global", "iterations", n
);
2582 send_status_all(STATUS_CONFIG
);
2584 #ifdef WITH_PINENTRY
2585 else if (g_strcasecmp(name
, (gchar
*)"ttyname") == 0) {
2586 g_free(client
->pinentry
->ttyname
);
2587 client
->pinentry
->ttyname
= g_strdup(value
);
2589 else if (g_strcasecmp(name
, (gchar
*)"ttytype") == 0) {
2590 g_free(client
->pinentry
->ttytype
);
2591 client
->pinentry
->ttytype
= g_strdup(value
);
2593 else if (g_strcasecmp(name
, (gchar
*)"display") == 0) {
2594 g_free(client
->pinentry
->display
);
2595 client
->pinentry
->display
= g_strdup(value
);
2597 else if (g_strcasecmp(name
, (gchar
*)"path") == 0) {
2598 g_free(client
->pinentry
->path
);
2599 client
->pinentry
->path
= g_strdup(value
);
2601 else if (g_strcasecmp(name
, (gchar
*)"title") == 0) {
2602 g_free(client
->pinentry
->title
);
2603 client
->pinentry
->title
= g_strdup(value
);
2605 else if (g_strcasecmp(name
, (gchar
*)"prompt") == 0) {
2606 g_free(client
->pinentry
->prompt
);
2607 client
->pinentry
->prompt
= g_strdup(value
);
2609 else if (g_strcasecmp(name
, (gchar
*)"desc") == 0) {
2610 g_free(client
->pinentry
->desc
);
2611 client
->pinentry
->desc
= g_strdup(value
);
2614 * Look at client_thread() to see how this works.
2616 else if (g_strcasecmp(name
, (gchar
*)"timeout") == 0) {
2618 gint n
= strtol(value
, &p
, 10);
2621 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2623 client
->pinentry
->timeout
= n
;
2625 else if (g_strcasecmp(name
, (gchar
*)"pinentry") == 0) {
2627 gint n
= strtol(value
, &p
, 10);
2629 if (*p
|| n
< 0 || n
> 1)
2630 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2632 client
->pinentry
->enable
= n
== 0 ? FALSE
: TRUE
;
2636 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2638 log_write("OPTION %s=%s", name
, value
);
2642 gpg_error_t
register_commands(assuan_context_t ctx
)
2646 gint (*handler
)(assuan_context_t
, gchar
*line
);
2648 { "OPEN", open_command
},
2649 { "SAVE", save_command
},
2650 { "LIST", list_command
},
2651 { "REALPATH", realpath_command
},
2652 { "STORE", store_command
},
2653 { "DELETE", delete_command
},
2654 { "GET", get_command
},
2655 { "ATTR", attr_command
},
2656 { "ISCACHED", iscached_command
},
2657 { "CLEARCACHE", clearcache_command
},
2658 { "CACHETIMEOUT", cachetimeout_command
},
2659 { "GETCONFIG", getconfig_command
},
2660 { "DUMP", dump_command
},
2661 { "XPATH", xpath_command
},
2662 { "IMPORT", import_command
},
2663 { "LOCK", lock_command
},
2664 { "UNLOCK", unlock_command
},
2665 { "GETPID", getpid_command
},
2672 for (i
=0; table
[i
].name
; i
++) {
2673 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2679 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2684 rc
= assuan_register_option_handler(ctx
, option_handler
);
2689 rc
= assuan_register_reset_notify(ctx
, reset_notify
);
2694 return assuan_register_post_cmd_notify(ctx
, command_finalize
);
2697 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2698 guchar
*key
, gint
*dst_iter
)
2703 guchar tkey
[gcrykeysize
];
2704 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2705 gcry_cipher_hd_t gh
;
2706 guint iter
= 0, n_iter
= 0;
2708 void *outbuf
= NULL
;
2712 file_header_t file_header
;
2713 gchar str
[ASSUAN_LINELENGTH
];
2716 rc
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2724 lseek(fd
, 0, SEEK_SET
);
2725 insize
= st
.st_size
- sizeof(file_header_t
);
2726 iv
= gcry_malloc(gcryblocksize
);
2730 gcry_cipher_close(gh
);
2732 return gpg_error_from_errno(ENOMEM
);
2735 len
= pth_read(fd
, &file_header
, sizeof(file_header_t
));
2737 if (len
!= sizeof(file_header_t
)) {
2741 gcry_cipher_close(gh
);
2745 return gpg_error_from_errno(errno
);
2748 *dst_iter
= file_header
.iter
;
2750 /* No encryption iterations. This is a plain (gzipped) file. */
2751 if (file_header
.iter
== -1) {
2753 * cache_file_count() needs both .used == TRUE and a valid key in
2754 * order for it to count as a used cache entry. Fixes CACHE status
2757 memset(key
, '!', gcrykeysize
);
2758 insize
= st
.st_size
- sizeof(file_header_t
);
2761 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2762 inbuf
= gcry_malloc(insize
);
2766 gcry_cipher_close(gh
);
2769 return gpg_error_from_errno(ENOMEM
);
2772 len
= pth_read(fd
, inbuf
, insize
);
2774 if (len
!= insize
) {
2778 gcry_cipher_close(gh
);
2782 return gpg_error_from_errno(errno
);
2785 if (file_header
.iter
== -1)
2788 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2790 gcry_cipher_close(gh
);
2791 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2794 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2801 if ((rc
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2803 gcry_cipher_close(gh
);
2804 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2807 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2813 gcry_cipher_close(gh
);
2818 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
2820 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2821 rc
= assuan_write_status(client
->ctx
, "DECRYPT",
2822 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
2831 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2839 memcpy(tkey
, key
, sizeof(tkey
));
2842 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2844 gcry_cipher_close(gh
);
2845 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2848 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2850 memset(tkey
, 0, sizeof(tkey
));
2856 memset(tkey
, 0, sizeof(tkey
));
2858 while (iter
< file_header
.iter
) {
2859 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2860 if (!(iter
% iter_progress
)) {
2861 rc
= assuan_write_status(ctx
, "DECRYPT",
2862 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
2872 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2874 gcry_cipher_close(gh
);
2875 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2878 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2885 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2889 gcry_cipher_close(gh
);
2890 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2893 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2904 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2905 rc
= assuan_write_status(ctx
, "DECRYPT",
2906 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
2918 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
2920 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2922 if (zrc
== Z_MEM_ERROR
) {
2924 return gpg_error_from_errno(ENOMEM
);
2926 else if (zrc
!= Z_DATA_ERROR
) {
2930 gcry_cipher_close(gh
);
2932 return EPWMD_BADKEY
;
2941 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2945 gcry_cipher_close(gh
);
2947 return EPWMD_BADKEY
;
2951 client
->xml
= inbuf
;
2952 client
->len
= insize
;
2955 gcry_cipher_close(gh
);
2963 * This is called after every Assuan command.
2965 void command_finalize(assuan_context_t ctx
, gint rc
)
2967 struct client_s
*client
= assuan_get_pointer(ctx
);
2969 if (!client
->is_lock_cmd
)
2970 unlock_file_mutex(client
);