1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2007 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>
29 #include <glib/gprintf.h>
46 #include "pwmd_error.h"
50 static void *z_alloc(void *data
, unsigned items
, unsigned size
)
53 return gcry_calloc(items
, size
);
55 return calloc(items
, size
);
59 static void z_free(void *data
, void *p
)
68 static gpg_error_t
file_modified(struct client_s
*client
)
72 if (client
->state
!= STATE_OPEN
)
75 if (stat(client
->filename
, &st
) == 0 && client
->mtime
) {
76 if (client
->mtime
!= st
.st_mtime
)
77 return EPWMD_FILE_MODIFIED
;
83 static gboolean
encrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
84 void *inbuf
, gsize insize
)
88 if ((rc
= gcry_cipher_encrypt(gh
, outbuf
, outsize
, inbuf
, insize
))) {
89 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
96 gpg_error_t
decrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
97 void *inbuf
, gsize insize
)
101 if ((rc
= gcry_cipher_decrypt(gh
, outbuf
, outsize
, inbuf
, insize
)))
102 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
107 static gpg_error_t
parse_xml(assuan_context_t ctx
)
109 struct client_s
*client
= assuan_get_pointer(ctx
);
111 client
->doc
= xmlReadMemory(client
->xml
, client
->len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
114 return EPWMD_LIBXML_ERROR
;
119 gboolean
valid_filename(const gchar
*filename
)
123 if (!filename
|| !*filename
)
126 for (p
= filename
; *p
; p
++) {
127 if (g_ascii_isalnum(*p
) == FALSE
&& *p
!= '-' && *p
!= '_' && *p
!= '.')
134 gint
open_file(const gchar
*filename
, struct stat
*st
)
138 if ((fd
= open(filename
, O_RDONLY
)) == -1)
141 if (stat(filename
, st
) == -1) {
149 void unlock_file_mutex(struct client_s
*client
)
153 if (client
->has_lock
== FALSE
)
156 CACHE_LOCK(client
->ctx
);
158 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
164 pth_mutex_release(m
);
165 client
->has_lock
= FALSE
;
168 gpg_error_t
lock_file_mutex(struct client_s
*client
)
172 if (client
->has_lock
== TRUE
)
175 CACHE_LOCK(client
->ctx
);
177 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
184 if (pth_mutex_acquire(m
, TRUE
, NULL
) == FALSE
) {
185 if (errno
== EBUSY
) {
187 assuan_write_status(client
->ctx
, "LOCKED", N_("Waiting for lock"));
189 pth_mutex_acquire(m
, FALSE
, NULL
);
193 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(errno
));
194 return gpg_error_from_errno(e
);
198 client
->has_lock
= TRUE
;
202 static void cleanup_client(struct client_s
*client
)
204 assuan_context_t ctx
= client
->ctx
;
206 unlock_file_mutex(client
);
207 CACHE_LOCK(client
->ctx
);
208 cache_decr_refcount(client
->md5file
);
211 * This may be a new file so don't use a cache slot. save_command() will
212 * set this to FALSE on success.
214 if (client
->new == TRUE
)
215 cache_clear(client
->md5file
, 1);
218 xmlFreeDoc(client
->doc
);
221 gcry_free(client
->xml
);
223 if (client
->filename
)
224 g_free(client
->filename
);
227 gcry_cipher_close(client
->gh
);
229 memset(client
, 0, sizeof(struct client_s
));
230 client
->state
= STATE_CONNECTED
;
232 client
->freed
= TRUE
;
236 static gchar
*print_fmt(const char *fmt
, ...)
239 static gchar buf
[ASSUAN_LINELENGTH
] = {0};
242 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
247 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gint insize
,
248 gpointer
*out
, glong
*outsize
, gint
*error
)
261 z
.avail_out
= zlib_bufsize
;
262 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
265 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
266 *error
= Z_MEM_ERROR
;
270 ret
= inflateInit2(&z
, 47);
273 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
278 memset(&h
, 0, sizeof(gz_header
));
279 h
.comment
= (guchar
*)buf
;
280 h
.comm_max
= sizeof(buf
);
281 ret
= inflateGetHeader(&z
, &h
);
284 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
290 ret
= inflate(&z
, Z_BLOCK
);
293 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
300 insize
= atoi((gchar
*)h
.comment
);
305 ret
= inflate(&z
, Z_FINISH
);
312 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
320 z
.next_out
= pout
+ z
.total_out
;
321 z
.avail_out
= zlib_bufsize
;
324 rc
= assuan_write_status(ctx
, "DECOMPRESS",
325 print_fmt("%i %i", z
.total_out
, insize
));
342 } while (ret
!= Z_STREAM_END
);
345 rc
= assuan_write_status(ctx
, "DECOMPRESS",
346 print_fmt("%i %i", z
.total_out
, insize
));
355 *outsize
= z
.total_out
;
360 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
367 static int open_command(assuan_context_t ctx
, char *line
)
371 guchar shakey
[gcrykeysize
];
375 struct client_s
*client
= assuan_get_pointer(ctx
);
377 gchar
*filename
= NULL
;
379 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
382 if (!filename
|| !*filename
) {
384 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
387 if (valid_filename(filename
) == FALSE
) {
389 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
392 if (client
->state
== STATE_OPEN
)
393 cleanup_client(client
);
395 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
396 CACHE_LOCK(client
->ctx
);
398 if (cache_has_file(client
->md5file
) == FALSE
) {
399 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
402 return send_error(ctx
, EPWMD_MAX_SLOTS
);
407 error
= lock_file_mutex(client
);
411 return send_error(ctx
, error
);
414 client
->freed
= FALSE
;
416 if ((error
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
418 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
419 cleanup_client(client
);
420 return send_error(ctx
, error
);
423 if (stat(filename
, &st
) == 0) {
424 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
425 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
427 cleanup_client(client
);
428 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
431 client
->mtime
= st
.st_mtime
;
435 * New files don't need a key.
437 if (access(filename
, R_OK
|W_OK
) != 0) {
438 if (errno
!= ENOENT
) {
440 log_write("%s: %s", filename
, strerror(errno
));
442 cleanup_client(client
);
443 return send_syserror(ctx
, error
);
446 if ((client
->xml
= new_document()) == NULL
) {
447 log_write("%s", strerror(ENOMEM
));
449 cleanup_client(client
);
450 return send_syserror(ctx
, ENOMEM
);
453 client
->len
= xmlStrlen(client
->xml
);
455 client
->filename
= g_strdup(filename
);
457 if (!client
->filename
) {
459 cleanup_client(client
);
460 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
461 return send_syserror(ctx
, ENOMEM
);
464 if (req
[1] && *req
[1]) {
465 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
466 memset(req
[1], 0, strlen(req
[1]));
473 if ((fd
= open_file(filename
, &st
)) == -1) {
475 log_write("%s: %s", filename
, strerror(errno
));
477 cleanup_client(client
);
478 return send_syserror(ctx
, error
);
484 CACHE_LOCK(client
->ctx
);
486 if (cache_get_key(client
->md5file
, shakey
) == TRUE
)
490 * No key specified and no matching filename found in the cache.
492 if (!req
[1] || !*req
[1]) {
495 cleanup_client(client
);
497 return send_error(ctx
, EPWMD_KEY
);
504 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
505 memset(req
[1], 0, strlen(req
[1]));
508 error
= try_xml_decrypt(ctx
, fd
, st
, shakey
);
512 memset(shakey
, 0, sizeof(shakey
));
514 cleanup_client(client
);
515 return send_error(ctx
, error
);
519 client
->filename
= g_strdup(filename
);
521 if (!client
->filename
) {
522 memset(shakey
, 0, sizeof(shakey
));
523 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
525 cleanup_client(client
);
526 return send_syserror(ctx
, ENOMEM
);
530 CACHE_LOCK(client
->ctx
);
533 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
534 memset(shakey
, 0, sizeof(shakey
));
535 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
537 cleanup_client(client
);
539 return send_error(ctx
, EPWMD_MAX_SLOTS
);
542 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
543 cache_reset_timeout(client
->md5file
, timeout
);
546 cache_set_timeout(client
->md5file
, -2);
549 memset(shakey
, 0, sizeof(shakey
));
553 error
= parse_xml(ctx
);
556 gcry_free(client
->xml
);
561 client
->state
= STATE_OPEN
;
563 return send_error(ctx
, error
);
566 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
567 gint size
, gpointer
*out
, glong
*outsize
, gint
*error
)
574 gint cmd
= Z_NO_FLUSH
;
579 z
.next_in
= pin
= data
;
580 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
581 z
.avail_out
= zlib_bufsize
;
582 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
585 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
586 *error
= Z_MEM_ERROR
;
590 ret
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
593 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
599 memset(&h
, 0, sizeof(gz_header
));
600 snprintf(buf
, sizeof(buf
), "%i", size
);
601 h
.comment
= (guchar
*)buf
;
602 ret
= deflateSetHeader(&z
, &h
);
605 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
615 ret
= deflate(&z
, cmd
);
622 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
630 z
.next_out
= pout
+ z
.total_out
;
631 z
.avail_out
= zlib_bufsize
;
634 if (!z
.avail_in
&& z
.total_in
< size
) {
635 if (z
.total_in
+ zlib_bufsize
> size
)
636 z
.avail_in
= size
- z
.total_in
;
638 z
.avail_in
= zlib_bufsize
;
641 rc
= assuan_write_status(ctx
, "COMPRESS",
642 print_fmt("%i %i", z
.total_in
, size
));
651 if (z
.total_in
>= size
)
662 } while (ret
!= Z_STREAM_END
);
665 rc
= assuan_write_status(ctx
, "COMPRESS",
666 print_fmt("%i %i", z
.total_in
, size
));
675 *outsize
= z
.total_out
;
680 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
687 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
688 const gchar
*filename
, gpointer data
, size_t insize
,
689 const guchar
*shakey
, guint iter
)
694 guchar tkey
[gcrykeysize
];
698 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
699 gchar tmp
[FILENAME_MAX
];
700 struct file_header_s
{
702 guchar iv
[gcryblocksize
];
705 if (insize
/ gcryblocksize
) {
706 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
708 if (insize
% gcryblocksize
)
709 len
+= gcryblocksize
;
713 * Resize the existing xml buffer to the block size required by gcrypt
714 * rather than duplicating it and wasting memory.
716 inbuf
= gcry_realloc(data
, len
);
719 return gpg_error_from_errno(ENOMEM
);
722 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
723 memcpy(tkey
, shakey
, sizeof(tkey
));
726 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
728 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
732 file_header
.iter
= iter
;
735 iter_progress
= get_key_file_integer("default", "iteration_progress");
737 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
738 error
= assuan_write_status(client
->ctx
, "ENCRYPT", "0");
746 while (xiter
< file_header
.iter
) {
747 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
748 if (!(xiter
% iter_progress
)) {
749 error
= assuan_write_status(client
->ctx
, "ENCRYPT", print_fmt("%i",
750 ++n_iter
* iter_progress
));
759 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
760 sizeof(file_header
.iv
)))) {
762 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
766 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
769 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
777 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
778 sizeof(file_header
.iv
)))) {
780 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
784 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
786 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
790 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
792 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
796 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
797 error
= assuan_write_status(client
->ctx
, "ENCRYPT",
798 print_fmt("%i", file_header
.iter
));
807 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
809 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
812 p
= strrchr(tmp
, '/');
814 log_write("%s: %s", p
, strerror(errno
));
815 return gpg_error_from_errno(error
);
820 * xml_import() from command line.
824 len
= pth_write(fd
, &file_header
, sizeof(struct file_header_s
));
826 if (len
!= sizeof(file_header
)) {
833 return gpg_error_from_errno(len
);
836 len
= pth_write(fd
, inbuf
, insize
);
845 return gpg_error_from_errno(len
);
848 if (fsync(fd
) == -1) {
852 return gpg_error_from_errno(len
);
861 if (stat(filename
, &st
) == 0)
862 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
864 if (rename(tmp
, filename
) == -1) {
867 return gpg_error_from_errno(len
);
871 chmod(filename
, mode
);
878 static int save_command(assuan_context_t ctx
, char *line
)
884 guchar shakey
[gcrykeysize
];
887 struct client_s
*client
= assuan_get_pointer(ctx
);
894 error
= file_modified(client
);
897 log_write("%s: %s", client
->filename
? client
->filename
: "",
898 pwmd_strerror(error
));
899 return send_error(ctx
, error
);
902 error
= lock_file_mutex(client
);
905 return send_error(ctx
, error
);
907 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
908 return send_syserror(ctx
, errno
);
910 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
911 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
912 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
915 if (!line
|| !*line
) {
918 if (cache_get_key(client
->md5file
, shakey
) == FALSE
) {
920 return send_error(ctx
, EPWMD_KEY
);
927 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
928 memset(line
, 0, strlen(line
));
931 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
934 iter
= get_key_file_integer(client
->filename
, "compression_level");
939 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
940 memset(shakey
, 0, sizeof(shakey
));
943 if (zerror
== Z_MEM_ERROR
) {
944 return send_syserror(ctx
, ENOMEM
);
947 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
955 if ((iter
= get_key_file_integer(client
->filename
, "iterations")) == -1)
958 error
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
961 memset(shakey
, 0, sizeof(shakey
));
962 return send_error(ctx
, error
);
965 stat(client
->filename
, &st
);
966 client
->mtime
= st
.st_mtime
;
967 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
968 CACHE_LOCK(client
->ctx
);
971 memset(shakey
, 0, sizeof(shakey
));
972 cache_reset_timeout(client
->md5file
, timeout
);
975 return send_error(ctx
, 0);
978 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
979 memset(shakey
, 0, sizeof(shakey
));
980 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
982 return send_error(ctx
, EPWMD_MAX_SLOTS
);
986 memset(shakey
, 0, sizeof(shakey
));
987 cache_reset_timeout(client
->md5file
, timeout
);
989 return send_error(ctx
, 0);
992 static gboolean
contains_whitespace(const gchar
*str
)
994 const gchar
*p
= str
;
998 len
= g_utf8_strlen(p
++, -1) -1;
1001 c
= g_utf8_get_char(p
++);
1003 if (g_unichar_isspace(c
))
1010 static int delete_command(assuan_context_t ctx
, char *line
)
1012 struct client_s
*client
= assuan_get_pointer(ctx
);
1017 error
= file_modified(client
);
1020 log_write("%s: %s", client
->filename
? client
->filename
: "",
1021 pwmd_strerror(error
));
1022 return send_error(ctx
, error
);
1025 if (strchr(line
, '\t'))
1026 req
= split_input_line(line
, "\t", -1);
1028 req
= split_input_line(line
, " ", -1);
1031 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1033 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1037 return send_error(ctx
, error
);
1041 * No sub-node defined. Remove the entire node (account).
1050 return send_error(ctx
, 0);
1053 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1057 return send_error(ctx
, error
);
1064 return send_error(ctx
, 0);
1068 * Don't return with assuan_process_done() here. This has been called from
1069 * assuan_process_next() and the command should be finished in
1072 static int store_command_finalize(gpointer data
, gint rc
, guchar
*line
,
1075 assuan_context_t ctx
= data
;
1076 struct client_s
*client
= assuan_get_pointer(ctx
);
1078 guchar
*result
= line
;
1080 gpg_error_t error
= file_modified(client
);
1092 req
= split_input_line((gchar
*)result
, "\t", 0);
1107 return EPWMD_COMMAND_SYNTAX
;
1110 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1112 if (error
&& error
== EPWMD_ELEMENT_NOT_FOUND
) {
1113 if (contains_whitespace(*req
) == TRUE
) {
1115 return EPWMD_INVALID_ELEMENT
;
1118 error
= new_account(client
->doc
, *req
);
1135 create_elements_cb(n
, req
+1, &error
, NULL
);
1137 find_elements(client
->doc
, n
->children
, req
+1, &error
,
1138 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1142 return send_error(ctx
, error
);
1145 static int store_command(assuan_context_t ctx
, char *line
)
1147 struct client_s
*client
= assuan_get_pointer(ctx
);
1148 gpg_error_t error
= file_modified(client
);
1151 log_write("%s: %s", client
->filename
? client
->filename
: "",
1152 pwmd_strerror(error
));
1153 return send_error(ctx
, error
);
1156 error
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1159 return send_error(ctx
, error
);
1161 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1165 static int get_command(assuan_context_t ctx
, char *line
)
1167 struct client_s
*client
= assuan_get_pointer(ctx
);
1172 error
= file_modified(client
);
1175 log_write("%s: %s", client
->filename
? client
->filename
: "",
1176 pwmd_strerror(error
));
1177 return send_error(ctx
, error
);
1180 req
= split_input_line(line
, "\t", -1);
1182 if (!req
|| !*req
) {
1184 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1187 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1191 return send_error(ctx
, error
);
1195 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1200 return send_error(ctx
, error
);
1202 if (!n
|| !n
->children
)
1203 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1207 if (!n
|| !n
->content
|| !*n
->content
)
1208 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1210 error
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1211 return send_error(ctx
, error
);
1214 static gchar
*element_path_to_req(const gchar
*account
, xmlChar
*path
)
1223 for (n
= 0; *p
&& n
< 3; p
++) {
1228 if (strstr((gchar
*)p
, "text()") != NULL
)
1229 p
[xmlStrlen(p
) - 7] = 0;
1231 for (n
= 0; p
[n
]; n
++) {
1236 buf
= g_strdup_printf("%s\t%s", account
, p
);
1240 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...)
1245 gint len
= *array
? g_strv_length(*array
) : 0;
1251 if ((a
= g_realloc(*array
, (len
+ 2) * sizeof(gchar
*))) == NULL
)
1255 ret
= g_vasprintf(&buf
, fmt
, ap
);
1271 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**req
,
1272 gpg_error_t
*error
, void *data
)
1274 struct realpath_s
*rp
= data
;
1277 g_free(rp
->account
);
1279 rp
->account
= g_strdup(req
[0]);
1282 *error
= gpg_error_from_errno(ENOMEM
);
1289 static int realpath_command(assuan_context_t ctx
, char *line
)
1292 struct client_s
*client
= assuan_get_pointer(ctx
);
1298 struct realpath_s
*rp
;
1301 error
= file_modified(client
);
1304 log_write("%s: %s", client
->filename
? client
->filename
: "",
1305 pwmd_strerror(error
));
1306 return send_error(ctx
, error
);
1309 if (strchr(line
, '\t') != NULL
) {
1310 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1311 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1314 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1315 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1318 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1322 return send_error(ctx
, error
);
1325 rp
= g_malloc(sizeof(struct realpath_s
));
1329 return send_syserror(ctx
, ENOMEM
);
1332 rp
->account
= g_strdup(req
[0]);
1336 return send_syserror(ctx
, ENOMEM
);
1340 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
,
1341 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, rp
);
1344 g_free(rp
->account
);
1347 return send_error(ctx
, error
);
1351 p
= xmlGetNodePath(n
);
1352 result
= element_path_to_req(rp
->account
, p
);
1356 g_free(rp
->account
);
1360 return send_syserror(ctx
, ENOMEM
);
1363 string
= g_string_new(result
);
1365 g_free(rp
->account
);
1372 for (t
= string
->str
+ i
; *t
; t
++, i
++) {
1373 if (!i
|| *t
== '\t') {
1374 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1379 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1380 g_string_free(string
, TRUE
);
1381 return send_error(ctx
, error
);
1384 struct list_element_s
{
1389 static gboolean
append_to_element_list(struct list_element_s
*elements
)
1396 if (!elements
|| !elements
->elements
)
1399 tmp
= g_strjoinv("\t", elements
->elements
);
1404 g_strfreev(elements
->elements
);
1405 elements
->elements
= NULL
;
1406 total
= g_slist_length(elements
->list
);
1407 a
= g_utf8_collate_key(tmp
, -1);
1415 * Removes duplicate element paths from the list. This is needed when
1416 * appending an element tree from list_command(). The glib docs recommend
1417 * using g_utf8_collate_key() for a large number of strings.
1419 for (i
= 0; i
< total
; i
++) {
1420 gchar
*p
= g_slist_nth_data(elements
->list
, i
);
1421 gchar
*b
= g_utf8_collate_key(p
, -1);
1429 if (strcmp(a
, b
) == 0) {
1440 list
= g_slist_append(elements
->list
, tmp
);
1445 elements
->list
= list
;
1449 static gpg_error_t
do_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
1450 struct list_element_s
*elements
, gchar
*prefix
)
1455 if (append_to_element_list(elements
) == FALSE
)
1456 return gpg_error_from_errno(ENOMEM
);
1458 for (n
= node
; n
; n
= n
->next
) {
1459 if (n
->type
== XML_ELEMENT_NODE
) {
1460 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
1464 if (strv_printf(&elements
->elements
, "%s\t%s", prefix
, n
->name
) == FALSE
)
1465 return gpg_error_from_errno(ENOMEM
);
1467 if (append_to_element_list(elements
) == FALSE
)
1468 return gpg_error_from_errno(ENOMEM
);
1471 tmp
= g_strdup_printf("%s\t!%s", prefix
, n
->name
);
1474 return gpg_error_from_errno(ENOMEM
);
1476 if (strv_printf(&elements
->elements
, "%s", tmp
) == FALSE
) {
1478 return gpg_error_from_errno(ENOMEM
);
1482 error
= do_list_recurse(doc
, n
->children
, elements
, tmp
);
1485 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1491 if (append_to_element_list(elements
) == FALSE
)
1492 return gpg_error_from_errno(ENOMEM
);
1499 static gpg_error_t
do_list_command(assuan_context_t ctx
, xmlDocPtr doc
,
1500 struct list_element_s
*elements
, char *line
)
1502 gchar
*prefix
= NULL
, *account
;
1503 gchar
**req
= NULL
, **oreq
= NULL
, *tmp
;
1505 gboolean account_is_literal
, account_has_target
= FALSE
;
1510 if ((req
= split_input_line(line
, "\t", 0)) == NULL
) {
1511 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1512 return EPWMD_COMMAND_SYNTAX
;
1515 prefix
= g_strdup(*req
);
1519 return gpg_error_from_errno(ENOMEM
);
1522 account
= g_strdup(*req
);
1527 return gpg_error_from_errno(ENOMEM
);
1530 oreq
= g_strdupv(req
);
1536 return gpg_error_from_errno(ENOMEM
);
1541 account_has_target
= FALSE
;
1542 account_is_literal
= is_literal_element_str(prefix
);
1543 n
= find_account(doc
, &p
, &error
, &account_has_target
, 0);
1553 if (!which
&& account_is_literal
== FALSE
&& account_has_target
== FALSE
) {
1554 tmp
= g_strdup_printf("!%s", prefix
);
1557 error
= gpg_error_from_errno(ENOMEM
);
1568 n
= find_elements(doc
, n
->children
, p
+1, &error
, NULL
, NULL
, NULL
,
1574 tmp
= g_strjoinv("\t", p
+1);
1576 error
= gpg_error_from_errno(ENOMEM
);
1580 t
= g_strdup_printf("%s\t%s", prefix
, tmp
);
1582 error
= gpg_error_from_errno(ENOMEM
);
1591 if (strv_printf(&elements
->elements
, "%s", prefix
) == FALSE
) {
1592 error
= gpg_error_from_errno(ENOMEM
);
1596 if (node_has_child_element(n
->children
) == FALSE
) {
1597 if (append_to_element_list(elements
) == FALSE
) {
1598 error
= gpg_error_from_errno(ENOMEM
);
1603 error
= do_list_recurse(doc
, n
->children
, elements
, prefix
);
1608 if (!which
++ && !*(p
+1) && account_is_literal
== FALSE
&& account_has_target
== TRUE
) {
1610 *oreq
= g_strdup_printf("!%s", account
);
1613 error
= gpg_error_from_errno(ENOMEM
);
1619 prefix
= g_strdup(*oreq
);
1622 error
= gpg_error_from_errno(ENOMEM
);
1641 * This could be faster especially when finding "target" attributes.
1643 static int list_command(assuan_context_t ctx
, char *line
)
1645 struct client_s
*client
= assuan_get_pointer(ctx
);
1647 struct list_element_s
*elements
= NULL
;
1652 if (disable_list_and_dump
== TRUE
)
1653 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1655 error
= file_modified(client
);
1658 log_write("%s: %s", client
->filename
, pwmd_strerror(error
));
1659 return send_error(ctx
, error
);
1665 error
= list_accounts(client
->doc
, &str
);
1668 return send_error(ctx
, error
);
1670 error
= assuan_send_data(ctx
, str
->str
, str
->len
);
1671 g_string_free(str
, TRUE
);
1672 return send_error(ctx
, error
);
1675 elements
= g_malloc0(sizeof(struct list_element_s
));
1678 error
= gpg_error_from_errno(ENOMEM
);
1682 error
= do_list_command(ctx
, client
->doc
, elements
, line
);
1688 total
= g_slist_length(elements
->list
);
1691 error
= EPWMD_EMPTY_ELEMENT
;
1696 * Find element paths with a target and append those element trees to
1699 for (i
= 0; i
< total
; i
++) {
1702 tmp
= g_slist_nth_data(elements
->list
, i
);
1703 req
= split_input_line(tmp
, "\t", 0);
1706 if (g_str_has_prefix(tmp
, "!") == TRUE
) {
1714 for (p
= req
; *p
; p
++) {
1715 if (g_str_has_prefix(*p
, "!") == FALSE
)
1726 error
= do_list_command(ctx
, client
->doc
, elements
, tmp
);
1728 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1731 total
= g_slist_length(elements
->list
);
1735 string
= g_string_new(NULL
);
1737 for (i
= 0; i
< total
; i
++) {
1738 tmp
= g_slist_nth_data(elements
->list
, i
);
1739 g_string_append_printf(string
, "%s\n", tmp
);
1743 string
= g_string_truncate(string
, string
->len
- 1);
1744 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1745 g_string_free(string
, TRUE
);
1750 g_slist_free(elements
->list
);
1752 if (elements
->elements
)
1753 g_strfreev(elements
->elements
);
1758 return send_error(ctx
, error
);
1761 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1766 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1767 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1770 return EPWMD_LIBXML_ERROR
;
1773 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1779 * req[0] - element path
1781 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1783 struct client_s
*client
= assuan_get_pointer(ctx
);
1784 gchar
**attrlist
= NULL
;
1786 gchar
**path
= NULL
;
1792 if (!req
|| !req
[0])
1793 return EPWMD_COMMAND_SYNTAX
;
1795 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1797 * The first argument may be only an account.
1799 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1800 return EPWMD_COMMAND_SYNTAX
;
1803 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1811 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1812 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1822 for (a
= n
->properties
; a
; a
= a
->next
) {
1825 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1827 g_strfreev(attrlist
);
1830 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
1831 return gpg_error_from_errno(error
);
1836 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1839 g_strfreev(attrlist
);
1840 return gpg_error_from_errno(ENOMEM
);
1843 attrlist
[++i
] = NULL
;
1847 return EPWMD_EMPTY_ELEMENT
;
1849 line
= g_strjoinv("\n", attrlist
);
1852 g_strfreev(attrlist
);
1853 return gpg_error_from_errno(ENOMEM
);
1856 error
= assuan_send_data(ctx
, line
, strlen(line
));
1858 g_strfreev(attrlist
);
1863 * req[0] - attribute
1864 * req[1] - element path
1866 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1870 gchar
**path
= NULL
;
1873 if (!req
|| !req
[0] || !req
[1])
1874 return EPWMD_COMMAND_SYNTAX
;
1876 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1878 * The first argument may be only an account.
1880 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1881 return EPWMD_COMMAND_SYNTAX
;
1885 * Don't remove the "name" attribute for the account element. To remove an
1886 * account use DELETE <account>.
1888 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1889 error
= EPWMD_ATTR_SYNTAX
;
1893 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1899 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1900 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1908 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1909 return EPWMD_ATTR_NOT_FOUND
;
1911 if (xmlRemoveProp(a
) == -1)
1912 return EPWMD_LIBXML_ERROR
;
1922 * Creates a "target" attribute. When other commands encounter an element with
1923 * this attribute, the element path is modified to the target value. If the
1924 * source element path doesn't exist when using 'ATTR SET target', it is
1925 * created, but the destination element path must exist.
1927 * req[0] - source element path
1928 * req[1] - destination element path
1930 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1932 gchar
**src
, **dst
, *line
;
1936 if (!req
|| !req
[0] || !req
[1])
1937 return EPWMD_COMMAND_SYNTAX
;
1939 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1941 * The first argument may be only an account.
1943 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1944 return EPWMD_COMMAND_SYNTAX
;
1947 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1949 * The first argument may be only an account.
1951 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1952 error
= EPWMD_COMMAND_SYNTAX
;
1957 n
= find_account(client
->doc
, &dst
, &error
, NULL
, 0);
1960 * Make sure the destination element path exists.
1966 n
= find_elements(client
->doc
, n
->children
, dst
+1, &error
,
1967 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1974 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
1977 if (error
== EPWMD_ELEMENT_NOT_FOUND
) {
1978 error
= new_account(client
->doc
, src
[0]);
1991 n
= create_target_elements_cb(n
, src
+1, &error
, NULL
);
1993 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
1994 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
2000 * Reset the position of the element tree now that the elements
2001 * have been created.
2003 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
2008 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
2009 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2015 line
= g_strjoinv("\t", dst
);
2016 error
= add_attribute(n
, "target", line
);
2035 * req[0] - account name
2038 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
2044 tmp
= g_strdupv(req
);
2047 return gpg_error_from_errno(ENOMEM
);
2049 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2055 if (g_utf8_collate(req
[0], req
[1]) == 0)
2059 * Will not overwrite an existing account.
2061 tmp
= g_strdupv(req
+1);
2064 return gpg_error_from_errno(ENOMEM
);
2066 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2069 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
2073 return EPWMD_ACCOUNT_EXISTS
;
2076 * Whitespace not allowed in account names.
2078 if (contains_whitespace(req
[1]) == TRUE
)
2079 return EPWMD_ATTR_SYNTAX
;
2081 tmp
= g_strdupv(req
);
2084 return gpg_error_from_errno(ENOMEM
);
2086 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2090 return EPWMD_ELEMENT_NOT_FOUND
;
2092 return add_attribute(n
, "name", req
[1]);
2096 * req[0] - attribute
2097 * req[1] - element path
2099 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
2101 struct client_s
*client
= assuan_get_pointer(ctx
);
2107 if (!req
|| !req
[0] || !req
[1])
2108 return EPWMD_COMMAND_SYNTAX
;
2110 if (strchr(req
[1], '\t')) {
2111 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2112 return EPWMD_COMMAND_SYNTAX
;
2115 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2116 return EPWMD_COMMAND_SYNTAX
;
2119 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2125 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2126 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2134 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2135 return EPWMD_ATTR_NOT_FOUND
;
2137 error
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
2147 * req[0] - attribute
2148 * req[1] - element path
2151 static int attribute_set(struct client_s
*client
, gchar
**req
)
2153 gchar
**path
= NULL
;
2157 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2158 return EPWMD_COMMAND_SYNTAX
;
2161 * Reserved attribute names.
2163 if (g_utf8_collate(req
[0], "name") == 0) {
2165 * Only reserved for the account element. Not the rest of the
2168 if (strchr(req
[1], '\t') == NULL
)
2169 return name_attribute(client
, req
+ 1);
2171 else if (g_utf8_collate(req
[0], "target") == 0)
2172 return target_attribute(client
, req
+ 1);
2174 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2176 * The first argument may be only an account.
2178 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2179 return EPWMD_COMMAND_SYNTAX
;
2182 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2188 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2189 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2196 return add_attribute(n
, req
[0], req
[2]);
2205 * req[1] - attribute name or element path if command is LIST
2206 * req[2] - element path
2207 * req[2] - element path or value
2209 static int attr_command(assuan_context_t ctx
, char *line
)
2211 struct client_s
*client
= assuan_get_pointer(ctx
);
2212 gchar
**req
= split_input_line(line
, " ", 4);
2213 gpg_error_t error
= 0;
2215 error
= file_modified(client
);
2218 log_write("%s: %s", client
->filename
? client
->filename
: "",
2219 pwmd_strerror(error
));
2221 return send_error(ctx
, error
);
2224 if (!req
|| !req
[0] || !req
[1]) {
2226 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2229 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2230 error
= attribute_set(client
, req
+1);
2231 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2232 error
= attribute_get(ctx
, req
+1);
2233 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2234 error
= attribute_delete(client
, req
+1);
2235 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2236 error
= attribute_list(ctx
, req
+1);
2238 error
= EPWMD_COMMAND_SYNTAX
;
2241 return send_error(ctx
, error
);
2244 static int iscached_command(assuan_context_t ctx
, char *line
)
2246 gchar
**req
= split_input_line(line
, " ", 0);
2249 if (!req
|| !*req
) {
2251 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2254 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2258 if (cache_iscached(md5file
) == FALSE
) {
2260 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2264 return send_error(ctx
, 0);
2267 gpg_error_t
send_cache_status(assuan_context_t ctx
)
2270 struct client_s
*client
= assuan_get_pointer(ctx
);
2272 CACHE_LOCK(client
->ctx
);
2273 tmp
= print_fmt("%i %i",
2275 (cache_size
/ sizeof(file_cache_t
)) - cache_file_count());
2278 return assuan_write_status(ctx
, "CACHE", tmp
);
2281 static int clearcache_command(assuan_context_t ctx
, char *line
)
2283 struct client_s
*client
= assuan_get_pointer(ctx
);
2284 gchar
**req
= split_input_line(line
, " ", 0);
2289 if (!req
|| !*req
) {
2291 cache_clear(client
->md5file
, 2);
2293 return send_error(ctx
, 0);
2296 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2299 if (cache_clear(md5file
, 1) == FALSE
) {
2301 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2305 return send_error(ctx
, 0);
2308 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2312 gchar
**req
= split_input_line(line
, " ", 0);
2314 struct client_s
*client
= assuan_get_pointer(ctx
);
2316 if (!req
|| !*req
|| !req
[1]) {
2318 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2322 timeout
= strtol(req
[0], &p
, 10);
2324 if (errno
!= 0 || *p
!= 0) {
2326 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2329 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2331 CACHE_LOCK(client
->ctx
);
2333 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2335 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2339 send_cache_status_all();
2340 return send_error(ctx
, 0);
2343 static int dump_command(assuan_context_t ctx
, char *line
)
2347 struct client_s
*client
= assuan_get_pointer(ctx
);
2350 if (disable_list_and_dump
== TRUE
)
2351 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2353 error
= file_modified(client
);
2356 log_write("%s: %s", client
->filename
? client
->filename
: "",
2357 pwmd_strerror(error
));
2358 return send_error(ctx
, error
);
2361 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2362 error
= assuan_send_data(ctx
, xml
, len
);
2364 return send_error(ctx
, error
);
2367 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2369 struct client_s
*client
= assuan_get_pointer(ctx
);
2370 gpg_error_t error
= 0;
2373 if (strcmp(line
, "key") == 0 || strcmp(line
, "key_file") == 0)
2374 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2376 p
= get_key_file_string(client
->filename
? client
->filename
: "default", line
);
2379 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2381 tmp
= expand_homedir(p
);
2384 error
= assuan_send_data(ctx
, p
, strlen(p
));
2386 return send_error(ctx
, error
);
2389 void cleanup_assuan(assuan_context_t ctx
)
2391 struct client_s
*cl
= assuan_get_pointer(ctx
);
2396 static void reset_notify(assuan_context_t ctx
)
2398 struct client_s
*cl
= assuan_get_pointer(ctx
);
2403 gpg_error_t
register_commands(assuan_context_t ctx
)
2407 int (*handler
)(assuan_context_t
, char *line
);
2409 { "OPEN", open_command
},
2410 { "SAVE", save_command
},
2411 { "LIST", list_command
},
2412 { "REALPATH", realpath_command
},
2413 { "STORE", store_command
},
2414 { "DELETE", delete_command
},
2415 { "GET", get_command
},
2416 { "ATTR", attr_command
},
2417 { "ISCACHED", iscached_command
},
2418 { "CLEARCACHE", clearcache_command
},
2419 { "CACHETIMEOUT", cachetimeout_command
},
2420 { "GETCONFIG", getconfig_command
},
2421 { "DUMP", dump_command
},
2428 for (i
=0; table
[i
].name
; i
++) {
2429 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2435 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2439 return assuan_register_reset_notify(ctx
, reset_notify
);
2442 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2448 guchar tkey
[gcrykeysize
];
2449 struct file_header_s
{
2451 guchar iv
[gcryblocksize
];
2453 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2454 gcry_cipher_hd_t gh
;
2455 guint iter
= 0, n_iter
= 0;
2457 void *outbuf
= NULL
;
2463 error
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2471 lseek(fd
, 0, SEEK_SET
);
2472 insize
= st
.st_size
- sizeof(struct file_header_s
);
2473 iv
= gcry_malloc(gcryblocksize
);
2477 gcry_cipher_close(gh
);
2479 return gpg_error_from_errno(ENOMEM
);
2482 len
= pth_read(fd
, &file_header
, sizeof(struct file_header_s
));
2484 if (len
!= sizeof(file_header
)) {
2488 gcry_cipher_close(gh
);
2492 return gpg_error_from_errno(errno
);
2495 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2496 inbuf
= gcry_malloc(insize
);
2500 gcry_cipher_close(gh
);
2503 return gpg_error_from_errno(ENOMEM
);
2506 len
= pth_read(fd
, inbuf
, insize
);
2508 if (len
!= insize
) {
2512 gcry_cipher_close(gh
);
2516 return gpg_error_from_errno(errno
);
2519 memcpy(tkey
, key
, sizeof(tkey
));
2522 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2524 gcry_cipher_close(gh
);
2525 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2528 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2535 if ((error
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2537 gcry_cipher_close(gh
);
2538 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2541 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2547 gcry_cipher_close(gh
);
2552 iter_progress
= get_key_file_integer("default", "iteration_progress");
2554 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2555 error
= assuan_write_status(client
->ctx
, "DECRYPT", "0");
2564 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2572 if ((error
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2574 gcry_cipher_close(gh
);
2575 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2578 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2585 while (iter
< file_header
.iter
) {
2586 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2587 if (!(iter
% iter_progress
)) {
2588 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i",
2589 ++n_iter
* iter_progress
));
2599 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2601 gcry_cipher_close(gh
);
2602 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2605 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2612 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2616 gcry_cipher_close(gh
);
2617 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2620 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2631 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2632 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i", file_header
.iter
));
2643 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
2645 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2647 if (zerror
== Z_MEM_ERROR
) {
2649 return gpg_error_from_errno(ENOMEM
);
2651 else if (zerror
!= Z_DATA_ERROR
) {
2655 gcry_cipher_close(gh
);
2657 return EPWMD_BADKEY
;
2666 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2670 gcry_cipher_close(gh
);
2672 return EPWMD_BADKEY
;
2676 client
->xml
= inbuf
;
2677 client
->len
= insize
;
2680 gcry_cipher_close(gh
);
2688 * This is called after every Assuan command.
2690 void command_finalize(assuan_context_t ctx
, gint error
)
2692 struct client_s
*client
= assuan_get_pointer(ctx
);
2694 unlock_file_mutex(client
);