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
)
86 if ((gcryerrno
= gcry_cipher_encrypt(gh
, outbuf
, outsize
, inbuf
, insize
))) {
87 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
94 gboolean
decrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
95 void *inbuf
, gsize insize
)
97 if ((gcryerrno
= gcry_cipher_decrypt(gh
, outbuf
, outsize
, inbuf
, insize
))) {
98 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
105 static gpg_error_t
parse_xml(assuan_context_t ctx
)
107 struct client_s
*client
= assuan_get_pointer(ctx
);
109 client
->doc
= xmlReadMemory(client
->xml
, client
->len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
112 return EPWMD_LIBXML_ERROR
;
117 gboolean
valid_filename(const gchar
*filename
)
121 if (!filename
|| !*filename
)
124 for (p
= filename
; *p
; p
++) {
125 if (g_ascii_isalnum(*p
) == FALSE
&& *p
!= '-' && *p
!= '_' && *p
!= '.')
132 gint
open_file(const gchar
*filename
, struct stat
*st
)
136 if ((fd
= open(filename
, O_RDONLY
)) == -1)
139 if (stat(filename
, st
) == -1) {
147 static void cleanup_client(struct client_s
*client
)
149 assuan_context_t ctx
= client
->ctx
;
152 * This may be a new file so don't use a cache slot. save_command() will
153 * set this to FALSE on success.
155 if (client
->new == TRUE
)
156 cache_clear(client
->md5file
, 1);
159 xmlFreeDoc(client
->doc
);
162 gcry_free(client
->xml
);
164 if (client
->filename
)
165 g_free(client
->filename
);
167 gcry_cipher_close(client
->gh
);
168 memset(client
, 0, sizeof(struct client_s
));
169 client
->state
= STATE_CONNECTED
;
171 client
->freed
= TRUE
;
174 static gchar
*print_fmt(const char *fmt
, ...)
177 static gchar buf
[ASSUAN_LINELENGTH
] = {0};
180 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
185 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gint insize
,
186 gpointer
*out
, glong
*outsize
, gint
*error
)
198 z
.avail_out
= zlib_bufsize
;
199 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
202 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
203 *error
= Z_MEM_ERROR
;
207 ret
= inflateInit2(&z
, 47);
210 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
215 memset(&h
, 0, sizeof(gz_header
));
216 h
.comment
= (guchar
*)buf
;
217 h
.comm_max
= sizeof(buf
);
218 ret
= inflateGetHeader(&z
, &h
);
221 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
227 ret
= inflate(&z
, Z_BLOCK
);
230 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
237 insize
= atoi((gchar
*)h
.comment
);
242 ret
= inflate(&z
, Z_FINISH
);
249 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
257 z
.next_out
= pout
+ z
.total_out
;
258 z
.avail_out
= zlib_bufsize
;
261 assuan_write_status(ctx
, "DECOMPRESS",
262 print_fmt("%i %i", z
.total_out
, insize
));
271 } while (ret
!= Z_STREAM_END
);
274 assuan_write_status(ctx
, "DECOMPRESS",
275 print_fmt("%i %i", z
.total_out
, insize
));
278 *outsize
= z
.total_out
;
283 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
290 static int open_command(assuan_context_t ctx
, char *line
)
294 guchar shakey
[gcrykeysize
];
298 struct client_s
*client
= assuan_get_pointer(ctx
);
300 gchar
*filename
= NULL
;
302 #ifdef WORKING_PTHREADS
306 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
309 if (!filename
|| !*filename
) {
311 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
314 if (valid_filename(filename
) == FALSE
) {
316 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
319 if (client
->state
== STATE_OPEN
)
320 cleanup_client(client
);
322 client
->freed
= FALSE
;
324 if ((gcryerrno
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
326 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
327 cleanup_client(client
);
328 return send_error(ctx
, gcryerrno
);
331 if (stat(filename
, &st
) == 0) {
332 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
333 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
335 cleanup_client(client
);
336 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
339 client
->mtime
= st
.st_mtime
;
342 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
345 * New files don't need a key.
347 if (access(filename
, R_OK
|W_OK
) != 0) {
348 if (errno
!= ENOENT
) {
350 log_write("%s: %s", filename
, strerror(errno
));
352 cleanup_client(client
);
353 return send_syserror(ctx
, error
);
356 if ((client
->xml
= new_document()) == NULL
) {
357 log_write("%s", strerror(ENOMEM
));
359 cleanup_client(client
);
360 return send_syserror(ctx
, ENOMEM
);
363 client
->len
= xmlStrlen(client
->xml
);
365 if (cache_has_file(client
->md5file
) == TRUE
)
366 cache_clear(client
->md5file
, 1);
368 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
369 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
371 cleanup_client(client
);
372 return send_error(ctx
, EPWMD_MAX_SLOTS
);
376 client
->filename
= g_strdup(filename
);
378 if (!client
->filename
) {
380 cleanup_client(client
);
381 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
382 return send_syserror(ctx
, ENOMEM
);
385 if (req
[1] && *req
[1]) {
386 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
387 memset(req
[1], 0, strlen(req
[1]));
394 if ((fd
= open_file(filename
, &st
)) == -1) {
396 log_write("%s: %s", filename
, strerror(errno
));
398 cleanup_client(client
);
399 return send_syserror(ctx
, error
);
405 if (cache_get_key(client
->md5file
, shakey
) == TRUE
)
409 * No key specified and no matching filename found in the cache.
411 if (!req
[1] || !*req
[1]) {
414 cleanup_client(client
);
415 return send_error(ctx
, EPWMD_KEY
);
420 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
421 memset(req
[1], 0, strlen(req
[1]));
424 error
= try_xml_decrypt(ctx
, fd
, st
, shakey
);
428 memset(shakey
, 0, sizeof(shakey
));
430 cleanup_client(client
);
431 return send_error(ctx
, error
);
435 client
->filename
= g_strdup(filename
);
437 if (!client
->filename
) {
438 memset(shakey
, 0, sizeof(shakey
));
439 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
441 cleanup_client(client
);
442 return send_syserror(ctx
, ENOMEM
);
447 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
448 memset(shakey
, 0, sizeof(shakey
));
449 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
451 cleanup_client(client
);
452 return send_error(ctx
, EPWMD_MAX_SLOTS
);
455 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
456 cache_reset_timeout(client
->md5file
, timeout
);
459 cache_set_timeout(client
->md5file
, -2);
461 memset(shakey
, 0, sizeof(shakey
));
465 error
= parse_xml(ctx
);
468 gcry_free(client
->xml
);
473 client
->state
= STATE_OPEN
;
474 send_cache_status(ctx
);
477 return send_error(ctx
, error
);
480 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
481 gint size
, gpointer
*out
, glong
*outsize
, gint
*error
)
488 gint cmd
= Z_NO_FLUSH
;
492 z
.next_in
= pin
= data
;
493 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
494 z
.avail_out
= zlib_bufsize
;
495 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
498 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
499 *error
= Z_MEM_ERROR
;
503 ret
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
506 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
512 memset(&h
, 0, sizeof(gz_header
));
513 snprintf(buf
, sizeof(buf
), "%i", size
);
514 h
.comment
= (guchar
*)buf
;
515 ret
= deflateSetHeader(&z
, &h
);
518 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
528 ret
= deflate(&z
, cmd
);
535 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
543 z
.next_out
= pout
+ z
.total_out
;
544 z
.avail_out
= zlib_bufsize
;
547 if (!z
.avail_in
&& z
.total_in
< size
) {
548 if (z
.total_in
+ zlib_bufsize
> size
)
549 z
.avail_in
= size
- z
.total_in
;
551 z
.avail_in
= zlib_bufsize
;
554 assuan_write_status(ctx
, "COMPRESS",
555 print_fmt("%i %i", z
.total_in
, size
));
558 if (z
.total_in
>= size
)
567 } while (ret
!= Z_STREAM_END
);
570 assuan_write_status(ctx
, "COMPRESS",
571 print_fmt("%i %i", z
.total_in
, size
));
574 *outsize
= z
.total_out
;
579 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
586 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
587 const gchar
*filename
, gpointer data
, size_t insize
,
588 const guchar
*shakey
, guint iter
)
593 guchar tkey
[gcrykeysize
];
596 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
597 gchar tmp
[FILENAME_MAX
];
598 struct file_header_s
{
600 guchar iv
[gcryblocksize
];
603 if (insize
/ gcryblocksize
) {
604 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
606 if (insize
% gcryblocksize
)
607 len
+= gcryblocksize
;
611 * Resize the existing xml buffer to the block size required by gcrypt
612 * rather than duplicating it and wasting memory.
614 inbuf
= gcry_realloc(data
, len
);
617 return gpg_error_from_errno(ENOMEM
);
620 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
621 memcpy(tkey
, shakey
, sizeof(tkey
));
624 if ((gcryerrno
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
626 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
630 file_header
.iter
= iter
;
633 iter_progress
= get_key_file_integer("default", "iteration_progress");
635 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
)
636 assuan_write_status(client
->ctx
, "ENCRYPT", "0");
638 while (xiter
< file_header
.iter
) {
639 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
640 if (!(xiter
% iter_progress
))
641 assuan_write_status(client
->ctx
, "ENCRYPT", print_fmt("%i",
642 ++n_iter
* iter_progress
));
645 if ((gcryerrno
= gcry_cipher_setiv(gh
, file_header
.iv
,
646 sizeof(file_header
.iv
)))) {
648 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
652 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
655 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
662 if ((gcryerrno
= gcry_cipher_setiv(gh
, file_header
.iv
,
663 sizeof(file_header
.iv
)))) {
665 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
669 if ((gcryerrno
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
671 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
675 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
677 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
681 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
)
682 assuan_write_status(client
->ctx
, "ENCRYPT",
683 print_fmt("%i", file_header
.iter
));
686 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
688 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
691 p
= strrchr(tmp
, '/');
693 log_write("%s: %s", p
, strerror(errno
));
694 return gpg_error_from_errno(error
);
699 * xml_import() from command line.
703 len
= write(fd
, &file_header
, sizeof(struct file_header_s
));
705 if (len
!= sizeof(file_header
)) {
712 return gpg_error_from_errno(len
);
715 len
= write(fd
, inbuf
, insize
);
724 return gpg_error_from_errno(len
);
727 if (fsync(fd
) == -1) {
731 return gpg_error_from_errno(len
);
740 if (stat(filename
, &st
) == 0)
741 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
743 if (rename(tmp
, filename
) == -1) {
746 return gpg_error_from_errno(len
);
750 chmod(filename
, mode
);
757 static int save_command(assuan_context_t ctx
, char *line
)
763 guchar shakey
[gcrykeysize
];
766 struct client_s
*client
= assuan_get_pointer(ctx
);
773 #ifdef WORKING_PTHREADS
776 error
= file_modified(client
);
779 log_write("%s: %s", client
->filename
? client
->filename
: "",
780 pwmd_strerror(error
));
781 return send_error(ctx
, error
);
784 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
785 return send_syserror(ctx
, errno
);
787 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
788 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
789 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
792 if (!line
|| !*line
) {
793 if (cache_get_key(client
->md5file
, shakey
) == FALSE
)
794 return send_error(ctx
, EPWMD_KEY
);
799 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
800 memset(line
, 0, strlen(line
));
803 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
806 iter
= get_key_file_integer(client
->filename
, "compression_level");
811 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
812 memset(shakey
, 0, sizeof(shakey
));
815 if (zerror
== Z_MEM_ERROR
) {
816 return send_syserror(ctx
, ENOMEM
);
819 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
827 if ((iter
= get_key_file_integer(client
->filename
, "iterations")) == -1)
830 error
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
833 memset(shakey
, 0, sizeof(shakey
));
834 return send_error(ctx
, error
);
837 stat(client
->filename
, &st
);
838 client
->mtime
= st
.st_mtime
;
839 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
842 memset(shakey
, 0, sizeof(shakey
));
843 cache_reset_timeout(client
->md5file
, timeout
);
848 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
849 memset(shakey
, 0, sizeof(shakey
));
850 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
851 return send_error(ctx
, EPWMD_MAX_SLOTS
);
855 memset(shakey
, 0, sizeof(shakey
));
856 cache_reset_timeout(client
->md5file
, timeout
);
860 static gboolean
contains_whitespace(const gchar
*str
)
862 const gchar
*p
= str
;
866 len
= g_utf8_strlen(p
++, -1) -1;
869 c
= g_utf8_get_char(p
++);
871 if (g_unichar_isspace(c
))
878 static int delete_command(assuan_context_t ctx
, char *line
)
880 struct client_s
*client
= assuan_get_pointer(ctx
);
885 error
= file_modified(client
);
888 log_write("%s: %s", client
->filename
? client
->filename
: "",
889 pwmd_strerror(error
));
890 return send_error(ctx
, error
);
893 if (strchr(line
, '\t'))
894 req
= split_input_line(line
, "\t", -1);
896 req
= split_input_line(line
, " ", -1);
899 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
901 n
= find_account(client
->doc
, &req
, &error
, NULL
);
905 return send_error(ctx
, error
);
909 * No sub-node defined. Remove the entire node (account).
921 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, NULL
);
925 return send_error(ctx
, error
);
935 static int store_command(assuan_context_t ctx
, char *line
)
937 struct client_s
*client
= assuan_get_pointer(ctx
);
944 error
= file_modified(client
);
947 log_write("%s: %s", client
->filename
? client
->filename
: "",
948 pwmd_strerror(error
));
949 return send_error(ctx
, error
);
952 error
= assuan_inquire(ctx
, "STORE", &result
, &len
, 0);
955 return send_error(ctx
, error
);
957 req
= split_input_line((gchar
*)result
, "\t", 0);
965 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
968 n
= find_account(client
->doc
, &req
, &error
, NULL
);
970 if (error
&& error
== EPWMD_ELEMENT_NOT_FOUND
) {
971 if (contains_whitespace(*req
) == TRUE
) {
973 return send_error(ctx
, EPWMD_INVALID_ELEMENT
);
976 error
= new_account(client
->doc
, *req
);
980 return send_error(ctx
, error
);
988 return send_error(ctx
, error
);
993 create_elements_cb(n
, req
+1, &error
, NULL
);
995 find_elements(client
->doc
, n
->children
, req
+1, &error
,
996 NULL
, NULL
, create_elements_cb
, NULL
);
1000 return send_error(ctx
, error
);
1003 static int get_command(assuan_context_t ctx
, char *line
)
1005 struct client_s
*client
= assuan_get_pointer(ctx
);
1010 error
= file_modified(client
);
1013 log_write("%s: %s", client
->filename
? client
->filename
: "",
1014 pwmd_strerror(error
));
1015 return send_error(ctx
, error
);
1018 req
= split_input_line(line
, "\t", -1);
1020 if (!req
|| !*req
) {
1022 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1025 n
= find_account(client
->doc
, &req
, &error
, NULL
);
1029 return send_error(ctx
, error
);
1033 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, NULL
);
1038 return send_error(ctx
, error
);
1040 if (!n
|| !n
->children
)
1041 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1045 if (!n
|| !n
->content
|| !*n
->content
)
1046 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1048 error
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1049 return send_error(ctx
, error
);
1052 static gchar
*element_path_to_req(const gchar
*account
, xmlChar
*path
)
1061 for (n
= 0; *p
&& n
< 3; p
++) {
1066 if (strstr((gchar
*)p
, "text()") != NULL
)
1067 p
[xmlStrlen(p
) - 7] = 0;
1069 for (n
= 0; p
[n
]; n
++) {
1074 buf
= g_strdup_printf("%s\t%s", account
, p
);
1078 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...)
1083 gint len
= *array
? g_strv_length(*array
) : 0;
1089 if ((a
= g_realloc(*array
, (len
+ 2) * sizeof(gchar
*))) == NULL
)
1093 ret
= g_vasprintf(&buf
, fmt
, ap
);
1109 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**req
,
1110 gpg_error_t
*error
, void *data
)
1112 struct realpath_s
*rp
= data
;
1115 g_free(rp
->account
);
1117 rp
->account
= g_strdup(req
[0]);
1120 *error
= gpg_error_from_errno(ENOMEM
);
1127 static int realpath_command(assuan_context_t ctx
, char *line
)
1130 struct client_s
*client
= assuan_get_pointer(ctx
);
1136 struct realpath_s
*rp
;
1139 error
= file_modified(client
);
1142 log_write("%s: %s", client
->filename
? client
->filename
: "",
1143 pwmd_strerror(error
));
1144 return send_error(ctx
, error
);
1147 if (strchr(line
, '\t') != NULL
) {
1148 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1149 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1152 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1153 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1156 n
= find_account(client
->doc
, &req
, &error
, NULL
);
1160 return send_error(ctx
, error
);
1163 rp
= g_malloc(sizeof(struct realpath_s
));
1167 return send_syserror(ctx
, ENOMEM
);
1170 rp
->account
= g_strdup(req
[0]);
1174 return send_syserror(ctx
, ENOMEM
);
1178 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
,
1179 NULL
, realpath_elements_cb
, NULL
, rp
);
1182 g_free(rp
->account
);
1185 return send_error(ctx
, error
);
1189 p
= xmlGetNodePath(n
);
1190 result
= element_path_to_req(rp
->account
, p
);
1194 g_free(rp
->account
);
1198 return send_syserror(ctx
, ENOMEM
);
1201 string
= g_string_new(result
);
1203 g_free(rp
->account
);
1210 for (t
= string
->str
+ i
; *t
; t
++, i
++) {
1211 if (!i
|| *t
== '\t') {
1212 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1217 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1218 g_string_free(string
, TRUE
);
1219 return send_error(ctx
, error
);
1222 struct list_element_s
{
1227 static gboolean
append_to_element_list(struct list_element_s
*elements
)
1234 if (!elements
|| !elements
->elements
)
1237 tmp
= g_strjoinv("\t", elements
->elements
);
1242 g_strfreev(elements
->elements
);
1243 elements
->elements
= NULL
;
1244 total
= g_slist_length(elements
->list
);
1245 a
= g_utf8_collate_key(tmp
, -1);
1253 * Removes duplicate element paths from the list. This is needed when
1254 * appending an element tree from list_command(). The glib docs recommend
1255 * using g_utf8_collate_key() for a large number of strings.
1257 for (i
= 0; i
< total
; i
++) {
1258 gchar
*p
= g_slist_nth_data(elements
->list
, i
);
1259 gchar
*b
= g_utf8_collate_key(p
, -1);
1267 if (strcmp(a
, b
) == 0) {
1278 list
= g_slist_append(elements
->list
, tmp
);
1283 elements
->list
= list
;
1287 static gpg_error_t
do_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
1288 struct list_element_s
*elements
, gchar
*prefix
)
1293 if (append_to_element_list(elements
) == FALSE
)
1294 return gpg_error_from_errno(ENOMEM
);
1296 for (n
= node
; n
; n
= n
->next
) {
1297 if (n
->type
== XML_ELEMENT_NODE
) {
1298 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
1302 if (strv_printf(&elements
->elements
, "%s\t%s", prefix
, n
->name
) == FALSE
)
1303 return gpg_error_from_errno(ENOMEM
);
1305 if (append_to_element_list(elements
) == FALSE
)
1306 return gpg_error_from_errno(ENOMEM
);
1309 tmp
= g_strdup_printf("%s\t!%s", prefix
, n
->name
);
1312 return gpg_error_from_errno(ENOMEM
);
1314 if (strv_printf(&elements
->elements
, "%s", tmp
) == FALSE
) {
1316 return gpg_error_from_errno(ENOMEM
);
1320 error
= do_list_recurse(doc
, n
->children
, elements
, tmp
);
1323 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1329 if (append_to_element_list(elements
) == FALSE
)
1330 return gpg_error_from_errno(ENOMEM
);
1337 static gpg_error_t
do_list_command(assuan_context_t ctx
, xmlDocPtr doc
,
1338 struct list_element_s
*elements
, char *line
)
1340 gchar
*prefix
= NULL
, *account
;
1341 gchar
**req
= NULL
, **oreq
= NULL
, *tmp
;
1343 gboolean account_is_literal
, account_has_target
= FALSE
;
1348 if ((req
= split_input_line(line
, "\t", 0)) == NULL
) {
1349 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1350 return EPWMD_COMMAND_SYNTAX
;
1353 prefix
= g_strdup(*req
);
1357 return gpg_error_from_errno(ENOMEM
);
1360 account
= g_strdup(*req
);
1365 return gpg_error_from_errno(ENOMEM
);
1368 oreq
= g_strdupv(req
);
1374 return gpg_error_from_errno(ENOMEM
);
1379 account_has_target
= FALSE
;
1380 account_is_literal
= is_literal_element_str(prefix
);
1381 n
= find_account(doc
, &p
, &error
, &account_has_target
);
1391 if (!which
&& account_is_literal
== FALSE
&& account_has_target
== FALSE
) {
1392 tmp
= g_strdup_printf("!%s", prefix
);
1395 error
= gpg_error_from_errno(ENOMEM
);
1406 n
= find_elements(doc
, n
->children
, p
+1, &error
,
1407 NULL
, NULL
, NULL
, NULL
);
1412 tmp
= g_strjoinv("\t", p
+1);
1414 error
= gpg_error_from_errno(ENOMEM
);
1418 t
= g_strdup_printf("%s\t%s", prefix
, tmp
);
1420 error
= gpg_error_from_errno(ENOMEM
);
1429 if (strv_printf(&elements
->elements
, "%s", prefix
) == FALSE
) {
1430 error
= gpg_error_from_errno(ENOMEM
);
1434 if (node_has_child_element(n
->children
) == FALSE
) {
1435 if (append_to_element_list(elements
) == FALSE
) {
1436 error
= gpg_error_from_errno(ENOMEM
);
1441 error
= do_list_recurse(doc
, n
->children
, elements
, prefix
);
1446 if (!which
++ && !*(p
+1) && account_is_literal
== FALSE
&& account_has_target
== TRUE
) {
1448 *oreq
= g_strdup_printf("!%s", account
);
1451 error
= gpg_error_from_errno(ENOMEM
);
1457 prefix
= g_strdup(*oreq
);
1460 error
= gpg_error_from_errno(ENOMEM
);
1479 * This could be faster especially when finding "target" attributes.
1481 static int list_command(assuan_context_t ctx
, char *line
)
1483 struct client_s
*client
= assuan_get_pointer(ctx
);
1485 struct list_element_s
*elements
= NULL
;
1490 if (disable_list_and_dump
== TRUE
)
1491 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1493 error
= file_modified(client
);
1496 log_write("%s: %s", client
->filename
, pwmd_strerror(error
));
1497 return send_error(ctx
, error
);
1503 error
= list_accounts(client
->doc
, &str
);
1506 return send_error(ctx
, error
);
1508 error
= assuan_send_data(ctx
, str
->str
, str
->len
);
1509 g_string_free(str
, TRUE
);
1510 return send_error(ctx
, error
);
1513 elements
= g_malloc0(sizeof(struct list_element_s
));
1516 error
= gpg_error_from_errno(ENOMEM
);
1520 is_list_command
= TRUE
;
1521 error
= do_list_command(ctx
, client
->doc
, elements
, line
);
1527 total
= g_slist_length(elements
->list
);
1530 error
= EPWMD_EMPTY_ELEMENT
;
1535 * Find element paths with a target and append those element trees to
1538 for (i
= 0; i
< total
; i
++) {
1541 tmp
= g_slist_nth_data(elements
->list
, i
);
1542 req
= split_input_line(tmp
, "\t", 0);
1545 if (g_str_has_prefix(tmp
, "!") == TRUE
) {
1553 for (p
= req
; *p
; p
++) {
1554 if (g_str_has_prefix(*p
, "!") == FALSE
)
1565 error
= do_list_command(ctx
, client
->doc
, elements
, tmp
);
1567 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1570 total
= g_slist_length(elements
->list
);
1574 string
= g_string_new(NULL
);
1576 for (i
= 0; i
< total
; i
++) {
1577 tmp
= g_slist_nth_data(elements
->list
, i
);
1578 g_string_append_printf(string
, "%s\n", tmp
);
1582 string
= g_string_truncate(string
, string
->len
- 1);
1583 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1584 g_string_free(string
, TRUE
);
1587 is_list_command
= FALSE
;
1591 g_slist_free(elements
->list
);
1593 if (elements
->elements
)
1594 g_strfreev(elements
->elements
);
1599 return send_error(ctx
, error
);
1602 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1607 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1608 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1611 return EPWMD_LIBXML_ERROR
;
1614 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1620 * req[0] - element path
1622 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1624 struct client_s
*client
= assuan_get_pointer(ctx
);
1625 gchar
**attrlist
= NULL
;
1627 gchar
**path
= NULL
;
1633 if (!req
|| !req
[0])
1634 return EPWMD_COMMAND_SYNTAX
;
1636 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1638 * The first argument may be only an account.
1640 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1641 return EPWMD_COMMAND_SYNTAX
;
1644 n
= find_account(client
->doc
, &path
, &error
, NULL
);
1652 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1653 NULL
, NULL
, NULL
, NULL
);
1663 for (a
= n
->properties
; a
; a
= a
->next
) {
1666 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1668 g_strfreev(attrlist
);
1671 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
1672 return gpg_error_from_errno(error
);
1677 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1680 g_strfreev(attrlist
);
1681 return gpg_error_from_errno(ENOMEM
);
1684 attrlist
[++i
] = NULL
;
1688 return EPWMD_EMPTY_ELEMENT
;
1690 line
= g_strjoinv("\n", attrlist
);
1693 g_strfreev(attrlist
);
1694 return gpg_error_from_errno(ENOMEM
);
1697 error
= assuan_send_data(ctx
, line
, g_utf8_strlen(line
, -1));
1699 g_strfreev(attrlist
);
1704 * req[0] - attribute
1705 * req[1] - element path
1707 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1711 gchar
**path
= NULL
;
1714 if (!req
|| !req
[0] || !req
[1])
1715 return EPWMD_COMMAND_SYNTAX
;
1717 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1719 * The first argument may be only an account.
1721 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1722 return EPWMD_COMMAND_SYNTAX
;
1726 * Don't remove the "name" attribute for the account element. To remove an
1727 * account use DELETE <account>.
1729 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1730 error
= EPWMD_ATTR_SYNTAX
;
1734 n
= find_account(client
->doc
, &path
, &error
, NULL
);
1740 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1741 NULL
, NULL
, NULL
, NULL
);
1749 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1750 return EPWMD_ATTR_NOT_FOUND
;
1752 if (xmlRemoveProp(a
) == -1)
1753 return EPWMD_LIBXML_ERROR
;
1763 * Creates a "target" attribute. When other commands encounter an element with
1764 * this attribute, the element path is modified to the target value. If the
1765 * source element path doesn't exist when using 'ATTR SET target', it is
1766 * created, but the destination element path must exist.
1768 * req[0] - source element path
1769 * req[1] - destination element path
1771 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1773 gchar
**src
, **dst
, *line
;
1777 if (!req
|| !req
[0] || !req
[1])
1778 return EPWMD_COMMAND_SYNTAX
;
1780 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1782 * The first argument may be only an account.
1784 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1785 return EPWMD_COMMAND_SYNTAX
;
1788 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1790 * The first argument may be only an account.
1792 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1793 error
= EPWMD_COMMAND_SYNTAX
;
1798 n
= find_account(client
->doc
, &dst
, &error
, NULL
);
1801 * Make sure the destination element path exists.
1807 n
= find_elements(client
->doc
, n
->children
, dst
+1, &error
,
1808 NULL
, NULL
, NULL
, NULL
);
1815 n
= find_account(client
->doc
, &src
, &error
, NULL
);
1818 if (error
== EPWMD_ELEMENT_NOT_FOUND
) {
1819 error
= new_account(client
->doc
, src
[0]);
1832 n
= create_target_elements_cb(n
, src
+1, &error
, NULL
);
1834 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
1835 NULL
, NULL
, create_target_elements_cb
, NULL
);
1841 * Reset the position of the element tree now that the elements
1842 * have been created.
1844 n
= find_account(client
->doc
, &src
, &error
, NULL
);
1849 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
1850 NULL
, NULL
, NULL
, NULL
);
1856 line
= g_strjoinv("\t", dst
);
1857 error
= add_attribute(n
, "target", line
);
1876 * req[0] - account name
1879 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
1885 tmp
= g_strdupv(req
);
1888 return gpg_error_from_errno(ENOMEM
);
1890 n
= find_account(client
->doc
, &tmp
, &error
, NULL
);
1896 if (g_utf8_collate(req
[0], req
[1]) == 0)
1900 * Will not overwrite an existing account.
1902 tmp
= g_strdupv(req
+1);
1905 return gpg_error_from_errno(ENOMEM
);
1907 n
= find_account(client
->doc
, &tmp
, &error
, NULL
);
1910 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1914 return EPWMD_ACCOUNT_EXISTS
;
1917 * Whitespace not allowed in account names.
1919 if (contains_whitespace(req
[1]) == TRUE
)
1920 return EPWMD_ATTR_SYNTAX
;
1922 tmp
= g_strdupv(req
);
1925 return gpg_error_from_errno(ENOMEM
);
1927 n
= find_account(client
->doc
, &tmp
, &error
, NULL
);
1931 return EPWMD_ELEMENT_NOT_FOUND
;
1933 return add_attribute(n
, "name", req
[1]);
1937 * req[0] - attribute
1938 * req[1] - element path
1940 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
1942 struct client_s
*client
= assuan_get_pointer(ctx
);
1948 if (!req
|| !req
[0] || !req
[1])
1949 return EPWMD_COMMAND_SYNTAX
;
1951 if (strchr(req
[1], '\t')) {
1952 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
1953 return EPWMD_COMMAND_SYNTAX
;
1956 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1957 return EPWMD_COMMAND_SYNTAX
;
1960 n
= find_account(client
->doc
, &path
, &error
, NULL
);
1966 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1967 NULL
, NULL
, NULL
, NULL
);
1975 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
1976 return EPWMD_ATTR_NOT_FOUND
;
1978 error
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
1988 * req[0] - attribute
1989 * req[1] - element path
1992 static int attribute_set(struct client_s
*client
, gchar
**req
)
1994 gchar
**path
= NULL
;
1998 if (!req
|| !req
[0] || !req
[1] || !req
[2])
1999 return EPWMD_COMMAND_SYNTAX
;
2002 * Reserved attribute names.
2004 if (g_utf8_collate(req
[0], "name") == 0) {
2006 * Only reserved for the account element. Not the rest of the
2009 if (strchr(req
[1], '\t') == NULL
)
2010 return name_attribute(client
, req
+ 1);
2012 else if (g_utf8_collate(req
[0], "target") == 0)
2013 return target_attribute(client
, req
+ 1);
2015 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2017 * The first argument may be only an account.
2019 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2020 return EPWMD_COMMAND_SYNTAX
;
2023 n
= find_account(client
->doc
, &path
, &error
, NULL
);
2029 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2030 NULL
, NULL
, NULL
, NULL
);
2037 return add_attribute(n
, req
[0], req
[2]);
2046 * req[1] - attribute name or element path if command is LIST
2047 * req[2] - element path
2048 * req[2] - element path or value
2050 static int attr_command(assuan_context_t ctx
, char *line
)
2052 struct client_s
*client
= assuan_get_pointer(ctx
);
2053 gchar
**req
= split_input_line(line
, " ", 4);
2054 gpg_error_t error
= 0;
2056 error
= file_modified(client
);
2059 log_write("%s: %s", client
->filename
? client
->filename
: "",
2060 pwmd_strerror(error
));
2062 return send_error(ctx
, error
);
2065 if (!req
|| !req
[0] || !req
[1]) {
2067 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2070 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2071 error
= attribute_set(client
, req
+1);
2072 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2073 error
= attribute_get(ctx
, req
+1);
2074 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2075 error
= attribute_delete(client
, req
+1);
2076 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2077 error
= attribute_list(ctx
, req
+1);
2079 error
= EPWMD_COMMAND_SYNTAX
;
2082 return send_error(ctx
, error
);
2085 static int iscached_command(assuan_context_t ctx
, char *line
)
2087 gchar
**req
= split_input_line(line
, " ", 0);
2090 #ifdef WORKING_PTHREADS
2094 if (!req
|| !*req
) {
2096 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2099 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2102 if (cache_iscached(md5file
) == FALSE
)
2103 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2108 void send_cache_status(assuan_context_t ctx
)
2111 gint i
= MUTEX_OFFSET
;
2113 tmp
= print_fmt("%i %i",
2115 (i
/ sizeof(file_cache_t
)) - cache_file_count());
2118 assuan_write_status(ctx
, "CACHE", tmp
);
2121 static int clearcache_command(assuan_context_t ctx
, char *line
)
2123 struct client_s
*client
= assuan_get_pointer(ctx
);
2124 gchar
**req
= split_input_line(line
, " ", 0);
2127 #ifdef WORKING_PTHREADS
2131 if (!req
|| !*req
) {
2133 cache_clear(client
->md5file
, 2);
2134 send_cache_status(ctx
);
2138 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2140 cache_clear(md5file
, 1);
2141 send_cache_status(ctx
);
2145 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2149 gchar
**req
= split_input_line(line
, " ", 0);
2152 #ifdef WORKING_PTHREADS
2156 if (!req
|| !*req
|| !req
[1]) {
2158 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2162 timeout
= strtol(req
[0], &p
, 10);
2164 if (errno
!= 0 || *p
!= 0) {
2166 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2169 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2172 if (cache_set_timeout(md5file
, timeout
) == FALSE
)
2173 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2175 send_cache_status(ctx
);
2179 static int dump_command(assuan_context_t ctx
, char *line
)
2183 struct client_s
*client
= assuan_get_pointer(ctx
);
2186 if (disable_list_and_dump
== TRUE
)
2187 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2189 error
= file_modified(client
);
2192 log_write("%s: %s", client
->filename
? client
->filename
: "",
2193 pwmd_strerror(error
));
2194 return send_error(ctx
, error
);
2197 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2198 error
= assuan_send_data(ctx
, xml
, len
);
2203 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2205 struct client_s
*client
= assuan_get_pointer(ctx
);
2206 gpg_error_t error
= 0;
2209 if (g_utf8_collate(line
, "key") == 0 || g_utf8_collate(line
, "key_file") == 0)
2210 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2212 p
= get_key_file_string(client
->filename
? client
->filename
: "default", line
);
2215 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2217 tmp
= expand_homedir(p
);
2220 error
= assuan_send_data(ctx
, p
, g_utf8_strlen(p
, -1));
2222 return send_error(ctx
, error
);
2225 void cleanup_assuan(assuan_context_t ctx
)
2227 struct client_s
*cl
= assuan_get_pointer(ctx
);
2230 #ifdef WORKING_PTHREADS
2235 gpg_error_t
register_commands(assuan_context_t ctx
)
2239 int (*handler
)(assuan_context_t
, char *line
);
2241 { "OPEN", open_command
},
2242 { "SAVE", save_command
},
2243 { "LIST", list_command
},
2244 { "REALPATH", realpath_command
},
2245 { "STORE", store_command
},
2246 { "DELETE", delete_command
},
2247 { "GET", get_command
},
2248 { "ATTR", attr_command
},
2249 { "ISCACHED", iscached_command
},
2250 { "CLEARCACHE", clearcache_command
},
2251 { "CACHETIMEOUT", cachetimeout_command
},
2252 { "GETCONFIG", getconfig_command
},
2253 { "DUMP", dump_command
},
2260 for (i
=0; table
[i
].name
; i
++) {
2261 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2267 return assuan_register_bye_notify(ctx
, cleanup_assuan
);
2270 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2276 guchar tkey
[gcrykeysize
];
2277 struct file_header_s
{
2279 guchar iv
[gcryblocksize
];
2281 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2282 gcry_cipher_hd_t gh
;
2283 guint iter
= 0, n_iter
= 0;
2285 void *outbuf
= NULL
;
2290 gcryerrno
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2298 lseek(fd
, 0, SEEK_SET
);
2299 insize
= st
.st_size
- sizeof(struct file_header_s
);
2300 iv
= gcry_malloc(gcryblocksize
);
2304 gcry_cipher_close(gh
);
2306 return gpg_error_from_errno(ENOMEM
);
2309 len
= read(fd
, &file_header
, sizeof(struct file_header_s
));
2311 if (len
!= sizeof(file_header
)) {
2315 gcry_cipher_close(gh
);
2319 return gpg_error_from_errno(errno
);
2322 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2323 inbuf
= gcry_malloc(insize
);
2327 gcry_cipher_close(gh
);
2330 return gpg_error_from_errno(ENOMEM
);
2333 len
= read(fd
, inbuf
, insize
);
2335 if (len
!= insize
) {
2339 gcry_cipher_close(gh
);
2343 return gpg_error_from_errno(errno
);
2346 memcpy(tkey
, key
, sizeof(tkey
));
2349 if ((gcryerrno
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2351 gcry_cipher_close(gh
);
2352 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2355 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2362 if ((gcryerrno
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2364 gcry_cipher_close(gh
);
2365 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2368 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2374 gcry_cipher_close(gh
);
2379 iter_progress
= get_key_file_integer("default", "iteration_progress");
2381 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
)
2382 assuan_write_status(client
->ctx
, "DECRYPT", "0");
2384 if (decrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
2386 gcry_cipher_close(gh
);
2387 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2390 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2397 if ((gcryerrno
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2399 gcry_cipher_close(gh
);
2400 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2403 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2410 while (iter
< file_header
.iter
) {
2411 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2412 if (!(iter
% iter_progress
))
2413 assuan_write_status(ctx
, "DECRYPT", print_fmt("%i",
2414 ++n_iter
* iter_progress
));
2417 if ((gcryerrno
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2419 gcry_cipher_close(gh
);
2420 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2423 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2430 if (decrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
2432 gcry_cipher_close(gh
);
2433 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2436 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2446 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
)
2447 assuan_write_status(ctx
, "DECRYPT", print_fmt("%i", file_header
.iter
));
2451 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
2453 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2455 if (zerror
== Z_MEM_ERROR
) {
2457 return gpg_error_from_errno(ENOMEM
);
2459 else if (zerror
!= Z_DATA_ERROR
) {
2463 gcry_cipher_close(gh
);
2465 return EPWMD_BADKEY
;
2474 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2478 gcry_cipher_close(gh
);
2480 return EPWMD_BADKEY
;
2484 client
->xml
= inbuf
;
2485 client
->len
= insize
;
2488 gcry_cipher_close(gh
);