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
);
406 cache_incr_refcount(client
->md5file
);
408 error
= lock_file_mutex(client
);
412 return send_error(ctx
, error
);
415 client
->freed
= FALSE
;
417 if ((error
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
419 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
420 cleanup_client(client
);
421 return send_error(ctx
, error
);
424 if (stat(filename
, &st
) == 0) {
425 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
426 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
428 cleanup_client(client
);
429 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
432 client
->mtime
= st
.st_mtime
;
436 * New files don't need a key.
438 if (access(filename
, R_OK
|W_OK
) != 0) {
439 if (errno
!= ENOENT
) {
441 log_write("%s: %s", filename
, strerror(errno
));
443 cleanup_client(client
);
444 return send_syserror(ctx
, error
);
447 if ((client
->xml
= new_document()) == NULL
) {
448 log_write("%s", strerror(ENOMEM
));
450 cleanup_client(client
);
451 return send_syserror(ctx
, ENOMEM
);
454 client
->len
= xmlStrlen(client
->xml
);
456 client
->filename
= g_strdup(filename
);
458 if (!client
->filename
) {
460 cleanup_client(client
);
461 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
462 return send_syserror(ctx
, ENOMEM
);
465 if (req
[1] && *req
[1]) {
466 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
467 memset(req
[1], 0, strlen(req
[1]));
474 if ((fd
= open_file(filename
, &st
)) == -1) {
476 log_write("%s: %s", filename
, strerror(errno
));
478 cleanup_client(client
);
479 return send_syserror(ctx
, error
);
485 CACHE_LOCK(client
->ctx
);
487 if (cache_get_key(client
->md5file
, shakey
) == TRUE
)
491 * No key specified and no matching filename found in the cache.
493 if (!req
[1] || !*req
[1]) {
496 cleanup_client(client
);
498 return send_error(ctx
, EPWMD_KEY
);
505 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
506 memset(req
[1], 0, strlen(req
[1]));
509 error
= try_xml_decrypt(ctx
, fd
, st
, shakey
);
513 memset(shakey
, 0, sizeof(shakey
));
515 cleanup_client(client
);
516 return send_error(ctx
, error
);
520 client
->filename
= g_strdup(filename
);
522 if (!client
->filename
) {
523 memset(shakey
, 0, sizeof(shakey
));
524 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
526 cleanup_client(client
);
527 return send_syserror(ctx
, ENOMEM
);
531 CACHE_LOCK(client
->ctx
);
534 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
535 memset(shakey
, 0, sizeof(shakey
));
536 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
538 cleanup_client(client
);
540 return send_error(ctx
, EPWMD_MAX_SLOTS
);
543 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
544 cache_reset_timeout(client
->md5file
, timeout
);
547 cache_set_timeout(client
->md5file
, -2);
550 memset(shakey
, 0, sizeof(shakey
));
554 error
= parse_xml(ctx
);
557 gcry_free(client
->xml
);
562 if (client
->new == FALSE
)
563 send_cache_status_all();
565 client
->state
= STATE_OPEN
;
568 return send_error(ctx
, error
);
571 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
572 gint size
, gpointer
*out
, glong
*outsize
, gint
*error
)
579 gint cmd
= Z_NO_FLUSH
;
584 z
.next_in
= pin
= data
;
585 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
586 z
.avail_out
= zlib_bufsize
;
587 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
590 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
591 *error
= Z_MEM_ERROR
;
595 ret
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
598 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
604 memset(&h
, 0, sizeof(gz_header
));
605 snprintf(buf
, sizeof(buf
), "%i", size
);
606 h
.comment
= (guchar
*)buf
;
607 ret
= deflateSetHeader(&z
, &h
);
610 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
620 ret
= deflate(&z
, cmd
);
627 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
635 z
.next_out
= pout
+ z
.total_out
;
636 z
.avail_out
= zlib_bufsize
;
639 if (!z
.avail_in
&& z
.total_in
< size
) {
640 if (z
.total_in
+ zlib_bufsize
> size
)
641 z
.avail_in
= size
- z
.total_in
;
643 z
.avail_in
= zlib_bufsize
;
646 rc
= assuan_write_status(ctx
, "COMPRESS",
647 print_fmt("%i %i", z
.total_in
, size
));
656 if (z
.total_in
>= size
)
667 } while (ret
!= Z_STREAM_END
);
670 rc
= assuan_write_status(ctx
, "COMPRESS",
671 print_fmt("%i %i", z
.total_in
, size
));
680 *outsize
= z
.total_out
;
685 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
692 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
693 const gchar
*filename
, gpointer data
, size_t insize
,
694 const guchar
*shakey
, guint iter
)
699 guchar tkey
[gcrykeysize
];
703 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
704 gchar tmp
[FILENAME_MAX
];
705 struct file_header_s
{
707 guchar iv
[gcryblocksize
];
710 if (insize
/ gcryblocksize
) {
711 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
713 if (insize
% gcryblocksize
)
714 len
+= gcryblocksize
;
718 * Resize the existing xml buffer to the block size required by gcrypt
719 * rather than duplicating it and wasting memory.
721 inbuf
= gcry_realloc(data
, len
);
724 return gpg_error_from_errno(ENOMEM
);
727 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
728 memcpy(tkey
, shakey
, sizeof(tkey
));
731 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
733 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
737 file_header
.iter
= iter
;
740 iter_progress
= get_key_file_integer("default", "iteration_progress");
742 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
743 error
= assuan_write_status(client
->ctx
, "ENCRYPT", "0");
751 while (xiter
< file_header
.iter
) {
752 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
753 if (!(xiter
% iter_progress
)) {
754 error
= assuan_write_status(client
->ctx
, "ENCRYPT", print_fmt("%i",
755 ++n_iter
* iter_progress
));
764 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
765 sizeof(file_header
.iv
)))) {
767 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
771 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
774 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
782 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
783 sizeof(file_header
.iv
)))) {
785 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
789 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
791 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
795 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
797 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
801 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
802 error
= assuan_write_status(client
->ctx
, "ENCRYPT",
803 print_fmt("%i", file_header
.iter
));
812 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
814 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
817 p
= strrchr(tmp
, '/');
819 log_write("%s: %s", p
, strerror(errno
));
820 return gpg_error_from_errno(error
);
825 * xml_import() from command line.
829 len
= pth_write(fd
, &file_header
, sizeof(struct file_header_s
));
831 if (len
!= sizeof(file_header
)) {
838 return gpg_error_from_errno(len
);
841 len
= pth_write(fd
, inbuf
, insize
);
850 return gpg_error_from_errno(len
);
853 if (fsync(fd
) == -1) {
857 return gpg_error_from_errno(len
);
866 if (stat(filename
, &st
) == 0)
867 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
869 if (rename(tmp
, filename
) == -1) {
872 return gpg_error_from_errno(len
);
876 chmod(filename
, mode
);
883 static int save_command(assuan_context_t ctx
, char *line
)
889 guchar shakey
[gcrykeysize
];
892 struct client_s
*client
= assuan_get_pointer(ctx
);
899 error
= file_modified(client
);
902 log_write("%s: %s", client
->filename
? client
->filename
: "",
903 pwmd_strerror(error
));
904 return send_error(ctx
, error
);
907 error
= lock_file_mutex(client
);
910 return send_error(ctx
, error
);
912 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
913 return send_syserror(ctx
, errno
);
915 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
916 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
917 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
920 if (!line
|| !*line
) {
923 if (cache_get_key(client
->md5file
, shakey
) == FALSE
) {
925 return send_error(ctx
, EPWMD_KEY
);
932 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
933 memset(line
, 0, strlen(line
));
936 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
939 iter
= get_key_file_integer(client
->filename
, "compression_level");
944 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
945 memset(shakey
, 0, sizeof(shakey
));
948 if (zerror
== Z_MEM_ERROR
) {
949 return send_syserror(ctx
, ENOMEM
);
952 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
960 if ((iter
= get_key_file_integer(client
->filename
, "iterations")) == -1)
963 error
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
966 memset(shakey
, 0, sizeof(shakey
));
967 return send_error(ctx
, error
);
970 stat(client
->filename
, &st
);
971 client
->mtime
= st
.st_mtime
;
972 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
973 CACHE_LOCK(client
->ctx
);
976 memset(shakey
, 0, sizeof(shakey
));
977 cache_reset_timeout(client
->md5file
, timeout
);
980 if (client
->new == TRUE
)
981 send_cache_status_all();
984 return send_error(ctx
, 0);
987 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
988 memset(shakey
, 0, sizeof(shakey
));
989 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
991 return send_error(ctx
, EPWMD_MAX_SLOTS
);
995 memset(shakey
, 0, sizeof(shakey
));
996 cache_reset_timeout(client
->md5file
, timeout
);
998 send_cache_status_all();
999 return send_error(ctx
, 0);
1002 static gboolean
contains_whitespace(const gchar
*str
)
1004 const gchar
*p
= str
;
1008 len
= g_utf8_strlen(p
++, -1) -1;
1011 c
= g_utf8_get_char(p
++);
1013 if (g_unichar_isspace(c
))
1020 static int delete_command(assuan_context_t ctx
, char *line
)
1022 struct client_s
*client
= assuan_get_pointer(ctx
);
1027 error
= file_modified(client
);
1030 log_write("%s: %s", client
->filename
? client
->filename
: "",
1031 pwmd_strerror(error
));
1032 return send_error(ctx
, error
);
1035 if (strchr(line
, '\t'))
1036 req
= split_input_line(line
, "\t", -1);
1038 req
= split_input_line(line
, " ", -1);
1041 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1043 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1047 return send_error(ctx
, error
);
1051 * No sub-node defined. Remove the entire node (account).
1060 return send_error(ctx
, 0);
1063 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1067 return send_error(ctx
, error
);
1074 return send_error(ctx
, 0);
1078 * Don't return with assuan_process_done() here. This has been called from
1079 * assuan_process_next() and the command should be finished in
1082 static int store_command_finalize(gpointer data
, gint rc
, guchar
*line
,
1085 assuan_context_t ctx
= data
;
1086 struct client_s
*client
= assuan_get_pointer(ctx
);
1088 guchar
*result
= line
;
1090 gpg_error_t error
= file_modified(client
);
1102 req
= split_input_line((gchar
*)result
, "\t", 0);
1117 return EPWMD_COMMAND_SYNTAX
;
1120 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1122 if (error
&& error
== EPWMD_ELEMENT_NOT_FOUND
) {
1123 if (contains_whitespace(*req
) == TRUE
) {
1125 return EPWMD_INVALID_ELEMENT
;
1128 error
= new_account(client
->doc
, *req
);
1145 create_elements_cb(n
, req
+1, &error
, NULL
);
1147 find_elements(client
->doc
, n
->children
, req
+1, &error
,
1148 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1152 client
->inquire_status
= INQUIRE_DONE
;
1156 static int store_command(assuan_context_t ctx
, char *line
)
1158 struct client_s
*client
= assuan_get_pointer(ctx
);
1159 gpg_error_t error
= file_modified(client
);
1162 log_write("%s: %s", client
->filename
? client
->filename
: "",
1163 pwmd_strerror(error
));
1164 return send_error(ctx
, error
);
1167 error
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1170 return send_error(ctx
, error
);
1172 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1173 client
->inquire_status
= INQUIRE_BUSY
;
1177 static int get_command(assuan_context_t ctx
, char *line
)
1179 struct client_s
*client
= assuan_get_pointer(ctx
);
1184 error
= file_modified(client
);
1187 log_write("%s: %s", client
->filename
? client
->filename
: "",
1188 pwmd_strerror(error
));
1189 return send_error(ctx
, error
);
1192 req
= split_input_line(line
, "\t", -1);
1194 if (!req
|| !*req
) {
1196 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1199 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1203 return send_error(ctx
, error
);
1207 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1212 return send_error(ctx
, error
);
1214 if (!n
|| !n
->children
)
1215 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1219 if (!n
|| !n
->content
|| !*n
->content
)
1220 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1222 error
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1223 return send_error(ctx
, error
);
1226 static gchar
*element_path_to_req(const gchar
*account
, xmlChar
*path
)
1235 for (n
= 0; *p
&& n
< 3; p
++) {
1240 if (strstr((gchar
*)p
, "text()") != NULL
)
1241 p
[xmlStrlen(p
) - 7] = 0;
1243 for (n
= 0; p
[n
]; n
++) {
1248 buf
= g_strdup_printf("%s\t%s", account
, p
);
1252 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...)
1257 gint len
= *array
? g_strv_length(*array
) : 0;
1263 if ((a
= g_realloc(*array
, (len
+ 2) * sizeof(gchar
*))) == NULL
)
1267 ret
= g_vasprintf(&buf
, fmt
, ap
);
1283 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**req
,
1284 gpg_error_t
*error
, void *data
)
1286 struct realpath_s
*rp
= data
;
1289 g_free(rp
->account
);
1291 rp
->account
= g_strdup(req
[0]);
1294 *error
= gpg_error_from_errno(ENOMEM
);
1301 static int realpath_command(assuan_context_t ctx
, char *line
)
1304 struct client_s
*client
= assuan_get_pointer(ctx
);
1310 struct realpath_s
*rp
;
1313 error
= file_modified(client
);
1316 log_write("%s: %s", client
->filename
? client
->filename
: "",
1317 pwmd_strerror(error
));
1318 return send_error(ctx
, error
);
1321 if (strchr(line
, '\t') != NULL
) {
1322 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1323 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1326 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1327 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1330 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1334 return send_error(ctx
, error
);
1337 rp
= g_malloc(sizeof(struct realpath_s
));
1341 return send_syserror(ctx
, ENOMEM
);
1344 rp
->account
= g_strdup(req
[0]);
1348 return send_syserror(ctx
, ENOMEM
);
1352 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
,
1353 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, rp
);
1356 g_free(rp
->account
);
1359 return send_error(ctx
, error
);
1363 p
= xmlGetNodePath(n
);
1364 result
= element_path_to_req(rp
->account
, p
);
1368 g_free(rp
->account
);
1372 return send_syserror(ctx
, ENOMEM
);
1375 string
= g_string_new(result
);
1377 g_free(rp
->account
);
1384 for (t
= string
->str
+ i
; *t
; t
++, i
++) {
1385 if (!i
|| *t
== '\t') {
1386 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1391 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1392 g_string_free(string
, TRUE
);
1393 return send_error(ctx
, error
);
1396 struct list_element_s
{
1401 static gboolean
append_to_element_list(struct list_element_s
*elements
)
1408 if (!elements
|| !elements
->elements
)
1411 tmp
= g_strjoinv("\t", elements
->elements
);
1416 g_strfreev(elements
->elements
);
1417 elements
->elements
= NULL
;
1418 total
= g_slist_length(elements
->list
);
1419 a
= g_utf8_collate_key(tmp
, -1);
1427 * Removes duplicate element paths from the list. This is needed when
1428 * appending an element tree from list_command(). The glib docs recommend
1429 * using g_utf8_collate_key() for a large number of strings.
1431 for (i
= 0; i
< total
; i
++) {
1432 gchar
*p
= g_slist_nth_data(elements
->list
, i
);
1433 gchar
*b
= g_utf8_collate_key(p
, -1);
1441 if (strcmp(a
, b
) == 0) {
1452 list
= g_slist_append(elements
->list
, tmp
);
1457 elements
->list
= list
;
1461 static gpg_error_t
do_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
1462 struct list_element_s
*elements
, gchar
*prefix
)
1467 if (append_to_element_list(elements
) == FALSE
)
1468 return gpg_error_from_errno(ENOMEM
);
1470 for (n
= node
; n
; n
= n
->next
) {
1471 if (n
->type
== XML_ELEMENT_NODE
) {
1472 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
1476 if (strv_printf(&elements
->elements
, "%s\t%s", prefix
, n
->name
) == FALSE
)
1477 return gpg_error_from_errno(ENOMEM
);
1479 if (append_to_element_list(elements
) == FALSE
)
1480 return gpg_error_from_errno(ENOMEM
);
1483 tmp
= g_strdup_printf("%s\t!%s", prefix
, n
->name
);
1486 return gpg_error_from_errno(ENOMEM
);
1488 if (strv_printf(&elements
->elements
, "%s", tmp
) == FALSE
) {
1490 return gpg_error_from_errno(ENOMEM
);
1494 error
= do_list_recurse(doc
, n
->children
, elements
, tmp
);
1497 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1503 if (append_to_element_list(elements
) == FALSE
)
1504 return gpg_error_from_errno(ENOMEM
);
1511 static gpg_error_t
do_list_command(assuan_context_t ctx
, xmlDocPtr doc
,
1512 struct list_element_s
*elements
, char *line
)
1514 gchar
*prefix
= NULL
, *account
;
1515 gchar
**req
= NULL
, **oreq
= NULL
, *tmp
;
1517 gboolean account_is_literal
, account_has_target
= FALSE
;
1522 if ((req
= split_input_line(line
, "\t", 0)) == NULL
) {
1523 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1524 return EPWMD_COMMAND_SYNTAX
;
1527 prefix
= g_strdup(*req
);
1531 return gpg_error_from_errno(ENOMEM
);
1534 account
= g_strdup(*req
);
1539 return gpg_error_from_errno(ENOMEM
);
1542 oreq
= g_strdupv(req
);
1548 return gpg_error_from_errno(ENOMEM
);
1553 account_has_target
= FALSE
;
1554 account_is_literal
= is_literal_element_str(prefix
);
1555 n
= find_account(doc
, &p
, &error
, &account_has_target
, 0);
1565 if (!which
&& account_is_literal
== FALSE
&& account_has_target
== FALSE
) {
1566 tmp
= g_strdup_printf("!%s", prefix
);
1569 error
= gpg_error_from_errno(ENOMEM
);
1580 n
= find_elements(doc
, n
->children
, p
+1, &error
, NULL
, NULL
, NULL
,
1586 tmp
= g_strjoinv("\t", p
+1);
1588 error
= gpg_error_from_errno(ENOMEM
);
1592 t
= g_strdup_printf("%s\t%s", prefix
, tmp
);
1594 error
= gpg_error_from_errno(ENOMEM
);
1603 if (strv_printf(&elements
->elements
, "%s", prefix
) == FALSE
) {
1604 error
= gpg_error_from_errno(ENOMEM
);
1608 if (node_has_child_element(n
->children
) == FALSE
) {
1609 if (append_to_element_list(elements
) == FALSE
) {
1610 error
= gpg_error_from_errno(ENOMEM
);
1615 error
= do_list_recurse(doc
, n
->children
, elements
, prefix
);
1620 if (!which
++ && !*(p
+1) && account_is_literal
== FALSE
&& account_has_target
== TRUE
) {
1622 *oreq
= g_strdup_printf("!%s", account
);
1625 error
= gpg_error_from_errno(ENOMEM
);
1631 prefix
= g_strdup(*oreq
);
1634 error
= gpg_error_from_errno(ENOMEM
);
1653 * This could be faster especially when finding "target" attributes.
1655 static int list_command(assuan_context_t ctx
, char *line
)
1657 struct client_s
*client
= assuan_get_pointer(ctx
);
1659 struct list_element_s
*elements
= NULL
;
1664 if (disable_list_and_dump
== TRUE
)
1665 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1667 error
= file_modified(client
);
1670 log_write("%s: %s", client
->filename
, pwmd_strerror(error
));
1671 return send_error(ctx
, error
);
1677 error
= list_accounts(client
->doc
, &str
);
1680 return send_error(ctx
, error
);
1682 error
= assuan_send_data(ctx
, str
->str
, str
->len
);
1683 g_string_free(str
, TRUE
);
1684 return send_error(ctx
, error
);
1687 elements
= g_malloc0(sizeof(struct list_element_s
));
1690 error
= gpg_error_from_errno(ENOMEM
);
1694 error
= do_list_command(ctx
, client
->doc
, elements
, line
);
1700 total
= g_slist_length(elements
->list
);
1703 error
= EPWMD_EMPTY_ELEMENT
;
1708 * Find element paths with a target and append those element trees to
1711 for (i
= 0; i
< total
; i
++) {
1714 tmp
= g_slist_nth_data(elements
->list
, i
);
1715 req
= split_input_line(tmp
, "\t", 0);
1718 if (g_str_has_prefix(tmp
, "!") == TRUE
) {
1726 for (p
= req
; *p
; p
++) {
1727 if (g_str_has_prefix(*p
, "!") == FALSE
)
1738 error
= do_list_command(ctx
, client
->doc
, elements
, tmp
);
1740 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1743 total
= g_slist_length(elements
->list
);
1747 string
= g_string_new(NULL
);
1749 for (i
= 0; i
< total
; i
++) {
1750 tmp
= g_slist_nth_data(elements
->list
, i
);
1751 g_string_append_printf(string
, "%s\n", tmp
);
1755 string
= g_string_truncate(string
, string
->len
- 1);
1756 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1757 g_string_free(string
, TRUE
);
1762 g_slist_free(elements
->list
);
1764 if (elements
->elements
)
1765 g_strfreev(elements
->elements
);
1770 return send_error(ctx
, error
);
1773 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1778 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1779 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1782 return EPWMD_LIBXML_ERROR
;
1785 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1791 * req[0] - element path
1793 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1795 struct client_s
*client
= assuan_get_pointer(ctx
);
1796 gchar
**attrlist
= NULL
;
1798 gchar
**path
= NULL
;
1804 if (!req
|| !req
[0])
1805 return EPWMD_COMMAND_SYNTAX
;
1807 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1809 * The first argument may be only an account.
1811 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1812 return EPWMD_COMMAND_SYNTAX
;
1815 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1823 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1824 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1834 for (a
= n
->properties
; a
; a
= a
->next
) {
1837 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1839 g_strfreev(attrlist
);
1842 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
1843 return gpg_error_from_errno(error
);
1848 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1851 g_strfreev(attrlist
);
1852 return gpg_error_from_errno(ENOMEM
);
1855 attrlist
[++i
] = NULL
;
1859 return EPWMD_EMPTY_ELEMENT
;
1861 line
= g_strjoinv("\n", attrlist
);
1864 g_strfreev(attrlist
);
1865 return gpg_error_from_errno(ENOMEM
);
1868 error
= assuan_send_data(ctx
, line
, strlen(line
));
1870 g_strfreev(attrlist
);
1875 * req[0] - attribute
1876 * req[1] - element path
1878 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1882 gchar
**path
= NULL
;
1885 if (!req
|| !req
[0] || !req
[1])
1886 return EPWMD_COMMAND_SYNTAX
;
1888 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1890 * The first argument may be only an account.
1892 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1893 return EPWMD_COMMAND_SYNTAX
;
1897 * Don't remove the "name" attribute for the account element. To remove an
1898 * account use DELETE <account>.
1900 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1901 error
= EPWMD_ATTR_SYNTAX
;
1905 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1911 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1912 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1920 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1921 return EPWMD_ATTR_NOT_FOUND
;
1923 if (xmlRemoveProp(a
) == -1)
1924 return EPWMD_LIBXML_ERROR
;
1934 * Creates a "target" attribute. When other commands encounter an element with
1935 * this attribute, the element path is modified to the target value. If the
1936 * source element path doesn't exist when using 'ATTR SET target', it is
1937 * created, but the destination element path must exist.
1939 * req[0] - source element path
1940 * req[1] - destination element path
1942 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1944 gchar
**src
, **dst
, *line
;
1948 if (!req
|| !req
[0] || !req
[1])
1949 return EPWMD_COMMAND_SYNTAX
;
1951 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1953 * The first argument may be only an account.
1955 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1956 return EPWMD_COMMAND_SYNTAX
;
1959 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1961 * The first argument may be only an account.
1963 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1964 error
= EPWMD_COMMAND_SYNTAX
;
1969 n
= find_account(client
->doc
, &dst
, &error
, NULL
, 0);
1972 * Make sure the destination element path exists.
1978 n
= find_elements(client
->doc
, n
->children
, dst
+1, &error
,
1979 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1986 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
1989 if (error
== EPWMD_ELEMENT_NOT_FOUND
) {
1990 error
= new_account(client
->doc
, src
[0]);
2003 n
= create_target_elements_cb(n
, src
+1, &error
, NULL
);
2005 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
2006 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
2012 * Reset the position of the element tree now that the elements
2013 * have been created.
2015 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
2020 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
2021 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2027 line
= g_strjoinv("\t", dst
);
2028 error
= add_attribute(n
, "target", line
);
2047 * req[0] - account name
2050 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
2056 tmp
= g_strdupv(req
);
2059 return gpg_error_from_errno(ENOMEM
);
2061 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2067 if (g_utf8_collate(req
[0], req
[1]) == 0)
2071 * Will not overwrite an existing account.
2073 tmp
= g_strdupv(req
+1);
2076 return gpg_error_from_errno(ENOMEM
);
2078 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2081 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
2085 return EPWMD_ACCOUNT_EXISTS
;
2088 * Whitespace not allowed in account names.
2090 if (contains_whitespace(req
[1]) == TRUE
)
2091 return EPWMD_ATTR_SYNTAX
;
2093 tmp
= g_strdupv(req
);
2096 return gpg_error_from_errno(ENOMEM
);
2098 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2102 return EPWMD_ELEMENT_NOT_FOUND
;
2104 return add_attribute(n
, "name", req
[1]);
2108 * req[0] - attribute
2109 * req[1] - element path
2111 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
2113 struct client_s
*client
= assuan_get_pointer(ctx
);
2119 if (!req
|| !req
[0] || !req
[1])
2120 return EPWMD_COMMAND_SYNTAX
;
2122 if (strchr(req
[1], '\t')) {
2123 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2124 return EPWMD_COMMAND_SYNTAX
;
2127 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2128 return EPWMD_COMMAND_SYNTAX
;
2131 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2137 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2138 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2146 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2147 return EPWMD_ATTR_NOT_FOUND
;
2149 error
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
2159 * req[0] - attribute
2160 * req[1] - element path
2163 static int attribute_set(struct client_s
*client
, gchar
**req
)
2165 gchar
**path
= NULL
;
2169 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2170 return EPWMD_COMMAND_SYNTAX
;
2173 * Reserved attribute names.
2175 if (g_utf8_collate(req
[0], "name") == 0) {
2177 * Only reserved for the account element. Not the rest of the
2180 if (strchr(req
[1], '\t') == NULL
)
2181 return name_attribute(client
, req
+ 1);
2183 else if (g_utf8_collate(req
[0], "target") == 0)
2184 return target_attribute(client
, req
+ 1);
2186 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2188 * The first argument may be only an account.
2190 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2191 return EPWMD_COMMAND_SYNTAX
;
2194 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2200 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2201 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2208 return add_attribute(n
, req
[0], req
[2]);
2217 * req[1] - attribute name or element path if command is LIST
2218 * req[2] - element path
2219 * req[2] - element path or value
2221 static int attr_command(assuan_context_t ctx
, char *line
)
2223 struct client_s
*client
= assuan_get_pointer(ctx
);
2224 gchar
**req
= split_input_line(line
, " ", 4);
2225 gpg_error_t error
= 0;
2227 error
= file_modified(client
);
2230 log_write("%s: %s", client
->filename
? client
->filename
: "",
2231 pwmd_strerror(error
));
2233 return send_error(ctx
, error
);
2236 if (!req
|| !req
[0] || !req
[1]) {
2238 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2241 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2242 error
= attribute_set(client
, req
+1);
2243 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2244 error
= attribute_get(ctx
, req
+1);
2245 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2246 error
= attribute_delete(client
, req
+1);
2247 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2248 error
= attribute_list(ctx
, req
+1);
2250 error
= EPWMD_COMMAND_SYNTAX
;
2253 return send_error(ctx
, error
);
2256 static int iscached_command(assuan_context_t ctx
, char *line
)
2258 gchar
**req
= split_input_line(line
, " ", 0);
2261 if (!req
|| !*req
) {
2263 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2266 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2270 if (cache_iscached(md5file
) == FALSE
) {
2272 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2276 return send_error(ctx
, 0);
2279 gpg_error_t
send_cache_status(assuan_context_t ctx
)
2282 struct client_s
*client
= assuan_get_pointer(ctx
);
2284 CACHE_LOCK(client
->ctx
);
2285 tmp
= print_fmt("%i %i",
2287 (cache_size
/ sizeof(file_cache_t
)) - cache_file_count());
2290 return assuan_write_status(ctx
, "CACHE", tmp
);
2293 static int clearcache_command(assuan_context_t ctx
, char *line
)
2295 struct client_s
*client
= assuan_get_pointer(ctx
);
2296 gchar
**req
= split_input_line(line
, " ", 0);
2301 if (!req
|| !*req
) {
2303 cache_clear(client
->md5file
, 2);
2305 return send_error(ctx
, 0);
2308 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2311 if (cache_clear(md5file
, 1) == FALSE
) {
2313 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2317 return send_error(ctx
, 0);
2320 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2324 gchar
**req
= split_input_line(line
, " ", 0);
2326 struct client_s
*client
= assuan_get_pointer(ctx
);
2328 if (!req
|| !*req
|| !req
[1]) {
2330 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2334 timeout
= strtol(req
[0], &p
, 10);
2336 if (errno
!= 0 || *p
!= 0) {
2338 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2341 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2343 CACHE_LOCK(client
->ctx
);
2345 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2347 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2351 send_cache_status_all();
2352 return send_error(ctx
, 0);
2355 static int dump_command(assuan_context_t ctx
, char *line
)
2359 struct client_s
*client
= assuan_get_pointer(ctx
);
2362 if (disable_list_and_dump
== TRUE
)
2363 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2365 error
= file_modified(client
);
2368 log_write("%s: %s", client
->filename
? client
->filename
: "",
2369 pwmd_strerror(error
));
2370 return send_error(ctx
, error
);
2373 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2374 error
= assuan_send_data(ctx
, xml
, len
);
2376 return send_error(ctx
, error
);
2379 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2381 struct client_s
*client
= assuan_get_pointer(ctx
);
2382 gpg_error_t error
= 0;
2385 if (strcmp(line
, "key") == 0 || strcmp(line
, "key_file") == 0)
2386 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2388 p
= get_key_file_string(client
->filename
? client
->filename
: "default", line
);
2391 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2393 tmp
= expand_homedir(p
);
2396 error
= assuan_send_data(ctx
, p
, strlen(p
));
2398 return send_error(ctx
, error
);
2401 void cleanup_assuan(assuan_context_t ctx
)
2403 struct client_s
*cl
= assuan_get_pointer(ctx
);
2408 static void reset_notify(assuan_context_t ctx
)
2410 struct client_s
*cl
= assuan_get_pointer(ctx
);
2415 gpg_error_t
register_commands(assuan_context_t ctx
)
2419 int (*handler
)(assuan_context_t
, char *line
);
2421 { "OPEN", open_command
},
2422 { "SAVE", save_command
},
2423 { "LIST", list_command
},
2424 { "REALPATH", realpath_command
},
2425 { "STORE", store_command
},
2426 { "DELETE", delete_command
},
2427 { "GET", get_command
},
2428 { "ATTR", attr_command
},
2429 { "ISCACHED", iscached_command
},
2430 { "CLEARCACHE", clearcache_command
},
2431 { "CACHETIMEOUT", cachetimeout_command
},
2432 { "GETCONFIG", getconfig_command
},
2433 { "DUMP", dump_command
},
2440 for (i
=0; table
[i
].name
; i
++) {
2441 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2447 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2451 return assuan_register_reset_notify(ctx
, reset_notify
);
2454 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2460 guchar tkey
[gcrykeysize
];
2461 struct file_header_s
{
2463 guchar iv
[gcryblocksize
];
2465 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2466 gcry_cipher_hd_t gh
;
2467 guint iter
= 0, n_iter
= 0;
2469 void *outbuf
= NULL
;
2475 error
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2483 lseek(fd
, 0, SEEK_SET
);
2484 insize
= st
.st_size
- sizeof(struct file_header_s
);
2485 iv
= gcry_malloc(gcryblocksize
);
2489 gcry_cipher_close(gh
);
2491 return gpg_error_from_errno(ENOMEM
);
2494 len
= pth_read(fd
, &file_header
, sizeof(struct file_header_s
));
2496 if (len
!= sizeof(file_header
)) {
2500 gcry_cipher_close(gh
);
2504 return gpg_error_from_errno(errno
);
2507 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2508 inbuf
= gcry_malloc(insize
);
2512 gcry_cipher_close(gh
);
2515 return gpg_error_from_errno(ENOMEM
);
2518 len
= pth_read(fd
, inbuf
, insize
);
2520 if (len
!= insize
) {
2524 gcry_cipher_close(gh
);
2528 return gpg_error_from_errno(errno
);
2531 memcpy(tkey
, key
, sizeof(tkey
));
2534 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2536 gcry_cipher_close(gh
);
2537 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2540 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2547 if ((error
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2549 gcry_cipher_close(gh
);
2550 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2553 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2559 gcry_cipher_close(gh
);
2564 iter_progress
= get_key_file_integer("default", "iteration_progress");
2566 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2567 error
= assuan_write_status(client
->ctx
, "DECRYPT", "0");
2576 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2584 if ((error
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2586 gcry_cipher_close(gh
);
2587 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2590 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2597 while (iter
< file_header
.iter
) {
2598 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2599 if (!(iter
% iter_progress
)) {
2600 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i",
2601 ++n_iter
* iter_progress
));
2611 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2613 gcry_cipher_close(gh
);
2614 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2617 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2624 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2628 gcry_cipher_close(gh
);
2629 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2632 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2643 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2644 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i", file_header
.iter
));
2655 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
2657 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2659 if (zerror
== Z_MEM_ERROR
) {
2661 return gpg_error_from_errno(ENOMEM
);
2663 else if (zerror
!= Z_DATA_ERROR
) {
2667 gcry_cipher_close(gh
);
2669 return EPWMD_BADKEY
;
2678 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2682 gcry_cipher_close(gh
);
2684 return EPWMD_BADKEY
;
2688 client
->xml
= inbuf
;
2689 client
->len
= insize
;
2692 gcry_cipher_close(gh
);
2700 * This is called after every Assuan command.
2702 void command_finalize(assuan_context_t ctx
, gint error
)
2704 struct client_s
*client
= assuan_get_pointer(ctx
);
2706 unlock_file_mutex(client
);