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
)
199 z
.avail_out
= zlib_bufsize
;
200 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
203 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
204 *error
= Z_MEM_ERROR
;
208 ret
= inflateInit2(&z
, 47);
211 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
216 memset(&h
, 0, sizeof(gz_header
));
217 h
.comment
= (guchar
*)buf
;
218 h
.comm_max
= sizeof(buf
);
219 ret
= inflateGetHeader(&z
, &h
);
222 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
228 ret
= inflate(&z
, Z_BLOCK
);
231 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
238 insize
= atoi((gchar
*)h
.comment
);
243 ret
= inflate(&z
, Z_FINISH
);
250 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
258 z
.next_out
= pout
+ z
.total_out
;
259 z
.avail_out
= zlib_bufsize
;
262 rc
= assuan_write_status(ctx
, "DECOMPRESS",
263 print_fmt("%i %i", z
.total_out
, insize
));
280 } while (ret
!= Z_STREAM_END
);
283 rc
= assuan_write_status(ctx
, "DECOMPRESS",
284 print_fmt("%i %i", z
.total_out
, insize
));
293 *outsize
= z
.total_out
;
298 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
305 static int open_command(assuan_context_t ctx
, char *line
)
309 guchar shakey
[gcrykeysize
];
313 struct client_s
*client
= assuan_get_pointer(ctx
);
315 gchar
*filename
= NULL
;
317 CACHE_LOCK(ctx
, NULL
);
319 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
322 if (!filename
|| !*filename
) {
324 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
327 if (valid_filename(filename
) == FALSE
) {
329 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
332 if (client
->state
== STATE_OPEN
)
333 cleanup_client(client
);
335 client
->freed
= FALSE
;
337 if ((gcryerrno
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
339 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
340 cleanup_client(client
);
341 return send_error(ctx
, gcryerrno
);
344 if (stat(filename
, &st
) == 0) {
345 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
346 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
348 cleanup_client(client
);
349 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
352 client
->mtime
= st
.st_mtime
;
355 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
358 * New files don't need a key.
360 if (access(filename
, R_OK
|W_OK
) != 0) {
361 if (errno
!= ENOENT
) {
363 log_write("%s: %s", filename
, strerror(errno
));
365 cleanup_client(client
);
366 return send_syserror(ctx
, error
);
369 if ((client
->xml
= new_document()) == NULL
) {
370 log_write("%s", strerror(ENOMEM
));
372 cleanup_client(client
);
373 return send_syserror(ctx
, ENOMEM
);
376 client
->len
= xmlStrlen(client
->xml
);
378 if (cache_has_file(client
->md5file
) == TRUE
)
379 cache_clear(client
->md5file
, 1);
381 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
382 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
384 cleanup_client(client
);
385 return send_error(ctx
, EPWMD_MAX_SLOTS
);
389 client
->filename
= g_strdup(filename
);
391 if (!client
->filename
) {
393 cleanup_client(client
);
394 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
395 return send_syserror(ctx
, ENOMEM
);
398 if (req
[1] && *req
[1]) {
399 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
400 memset(req
[1], 0, strlen(req
[1]));
407 if ((fd
= open_file(filename
, &st
)) == -1) {
409 log_write("%s: %s", filename
, strerror(errno
));
411 cleanup_client(client
);
412 return send_syserror(ctx
, error
);
418 if (cache_get_key(client
->md5file
, shakey
) == TRUE
)
422 * No key specified and no matching filename found in the cache.
424 if (!req
[1] || !*req
[1]) {
427 cleanup_client(client
);
428 return send_error(ctx
, EPWMD_KEY
);
433 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
434 memset(req
[1], 0, strlen(req
[1]));
437 error
= try_xml_decrypt(ctx
, fd
, st
, shakey
);
441 memset(shakey
, 0, sizeof(shakey
));
443 cleanup_client(client
);
444 return send_error(ctx
, error
);
448 client
->filename
= g_strdup(filename
);
450 if (!client
->filename
) {
451 memset(shakey
, 0, sizeof(shakey
));
452 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
454 cleanup_client(client
);
455 return send_syserror(ctx
, ENOMEM
);
460 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
461 memset(shakey
, 0, sizeof(shakey
));
462 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
464 cleanup_client(client
);
465 return send_error(ctx
, EPWMD_MAX_SLOTS
);
468 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
469 cache_reset_timeout(client
->md5file
, timeout
);
472 cache_set_timeout(client
->md5file
, -2);
474 memset(shakey
, 0, sizeof(shakey
));
478 error
= parse_xml(ctx
);
481 gcry_free(client
->xml
);
486 client
->state
= STATE_OPEN
;
488 return send_error(ctx
, error
);
491 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
492 gint size
, gpointer
*out
, glong
*outsize
, gint
*error
)
499 gint cmd
= Z_NO_FLUSH
;
504 z
.next_in
= pin
= data
;
505 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
506 z
.avail_out
= zlib_bufsize
;
507 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
510 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
511 *error
= Z_MEM_ERROR
;
515 ret
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
518 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
524 memset(&h
, 0, sizeof(gz_header
));
525 snprintf(buf
, sizeof(buf
), "%i", size
);
526 h
.comment
= (guchar
*)buf
;
527 ret
= deflateSetHeader(&z
, &h
);
530 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
540 ret
= deflate(&z
, cmd
);
547 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
555 z
.next_out
= pout
+ z
.total_out
;
556 z
.avail_out
= zlib_bufsize
;
559 if (!z
.avail_in
&& z
.total_in
< size
) {
560 if (z
.total_in
+ zlib_bufsize
> size
)
561 z
.avail_in
= size
- z
.total_in
;
563 z
.avail_in
= zlib_bufsize
;
566 rc
= assuan_write_status(ctx
, "COMPRESS",
567 print_fmt("%i %i", z
.total_in
, size
));
576 if (z
.total_in
>= size
)
587 } while (ret
!= Z_STREAM_END
);
590 rc
= assuan_write_status(ctx
, "COMPRESS",
591 print_fmt("%i %i", z
.total_in
, size
));
600 *outsize
= z
.total_out
;
605 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
612 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
613 const gchar
*filename
, gpointer data
, size_t insize
,
614 const guchar
*shakey
, guint iter
)
619 guchar tkey
[gcrykeysize
];
622 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
623 gchar tmp
[FILENAME_MAX
];
624 struct file_header_s
{
626 guchar iv
[gcryblocksize
];
629 if (insize
/ gcryblocksize
) {
630 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
632 if (insize
% gcryblocksize
)
633 len
+= gcryblocksize
;
637 * Resize the existing xml buffer to the block size required by gcrypt
638 * rather than duplicating it and wasting memory.
640 inbuf
= gcry_realloc(data
, len
);
643 return gpg_error_from_errno(ENOMEM
);
646 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
647 memcpy(tkey
, shakey
, sizeof(tkey
));
650 if ((gcryerrno
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
652 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
656 file_header
.iter
= iter
;
659 iter_progress
= get_key_file_integer("default", "iteration_progress");
661 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
662 error
= assuan_write_status(client
->ctx
, "ENCRYPT", "0");
670 while (xiter
< file_header
.iter
) {
671 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
672 if (!(xiter
% iter_progress
)) {
673 error
= assuan_write_status(client
->ctx
, "ENCRYPT", print_fmt("%i",
674 ++n_iter
* iter_progress
));
683 if ((gcryerrno
= gcry_cipher_setiv(gh
, file_header
.iv
,
684 sizeof(file_header
.iv
)))) {
686 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
690 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
693 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
701 if ((gcryerrno
= gcry_cipher_setiv(gh
, file_header
.iv
,
702 sizeof(file_header
.iv
)))) {
704 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
708 if ((gcryerrno
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
710 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
714 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
716 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
720 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
721 error
= assuan_write_status(client
->ctx
, "ENCRYPT",
722 print_fmt("%i", file_header
.iter
));
731 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
733 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
736 p
= strrchr(tmp
, '/');
738 log_write("%s: %s", p
, strerror(errno
));
739 return gpg_error_from_errno(error
);
744 * xml_import() from command line.
748 len
= pth_write(fd
, &file_header
, sizeof(struct file_header_s
));
750 if (len
!= sizeof(file_header
)) {
757 return gpg_error_from_errno(len
);
760 len
= pth_write(fd
, inbuf
, insize
);
769 return gpg_error_from_errno(len
);
772 if (fsync(fd
) == -1) {
776 return gpg_error_from_errno(len
);
785 if (stat(filename
, &st
) == 0)
786 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
788 if (rename(tmp
, filename
) == -1) {
791 return gpg_error_from_errno(len
);
795 chmod(filename
, mode
);
802 static int save_command(assuan_context_t ctx
, char *line
)
808 guchar shakey
[gcrykeysize
];
811 struct client_s
*client
= assuan_get_pointer(ctx
);
818 CACHE_LOCK(ctx
, NULL
);
819 error
= file_modified(client
);
822 log_write("%s: %s", client
->filename
? client
->filename
: "",
823 pwmd_strerror(error
));
824 return send_error(ctx
, error
);
827 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
828 return send_syserror(ctx
, errno
);
830 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
831 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
832 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
835 if (!line
|| !*line
) {
836 if (cache_get_key(client
->md5file
, shakey
) == FALSE
)
837 return send_error(ctx
, EPWMD_KEY
);
842 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
843 memset(line
, 0, strlen(line
));
846 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
849 iter
= get_key_file_integer(client
->filename
, "compression_level");
854 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
855 memset(shakey
, 0, sizeof(shakey
));
858 if (zerror
== Z_MEM_ERROR
) {
859 return send_syserror(ctx
, ENOMEM
);
862 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
870 if ((iter
= get_key_file_integer(client
->filename
, "iterations")) == -1)
873 error
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
876 memset(shakey
, 0, sizeof(shakey
));
877 return send_error(ctx
, error
);
880 stat(client
->filename
, &st
);
881 client
->mtime
= st
.st_mtime
;
882 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
885 memset(shakey
, 0, sizeof(shakey
));
886 cache_reset_timeout(client
->md5file
, timeout
);
888 return send_error(ctx
, 0);
891 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
892 memset(shakey
, 0, sizeof(shakey
));
893 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
894 return send_error(ctx
, EPWMD_MAX_SLOTS
);
898 memset(shakey
, 0, sizeof(shakey
));
899 cache_reset_timeout(client
->md5file
, timeout
);
900 return send_error(ctx
, 0);
903 static gboolean
contains_whitespace(const gchar
*str
)
905 const gchar
*p
= str
;
909 len
= g_utf8_strlen(p
++, -1) -1;
912 c
= g_utf8_get_char(p
++);
914 if (g_unichar_isspace(c
))
921 static int delete_command(assuan_context_t ctx
, char *line
)
923 struct client_s
*client
= assuan_get_pointer(ctx
);
928 error
= file_modified(client
);
931 log_write("%s: %s", client
->filename
? client
->filename
: "",
932 pwmd_strerror(error
));
933 return send_error(ctx
, error
);
936 if (strchr(line
, '\t'))
937 req
= split_input_line(line
, "\t", -1);
939 req
= split_input_line(line
, " ", -1);
942 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
944 n
= find_account(client
->doc
, &req
, &error
, NULL
);
948 return send_error(ctx
, error
);
952 * No sub-node defined. Remove the entire node (account).
961 return send_error(ctx
, 0);
964 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, NULL
);
968 return send_error(ctx
, error
);
975 return send_error(ctx
, 0);
979 * Don't return with assuan_process_done() here. This has been called from
980 * assuan_process_next() and the command should be finished in
983 #define INQUIRE_RETURN(client, rc) {client->inquire_error = rc; return rc;}
985 static int store_command_finalize(gpointer data
, gint rc
, guchar
*line
,
988 assuan_context_t ctx
= data
;
989 struct client_s
*client
= assuan_get_pointer(ctx
);
991 guchar
*result
= line
;
993 gpg_error_t error
= file_modified(client
);
1005 req
= split_input_line((gchar
*)result
, "\t", 0);
1016 INQUIRE_RETURN(client
, rc
);
1020 INQUIRE_RETURN(client
, EPWMD_COMMAND_SYNTAX
);
1023 n
= find_account(client
->doc
, &req
, &error
, NULL
);
1025 if (error
&& error
== EPWMD_ELEMENT_NOT_FOUND
) {
1026 if (contains_whitespace(*req
) == TRUE
) {
1028 INQUIRE_RETURN(client
, EPWMD_INVALID_ELEMENT
);
1031 error
= new_account(client
->doc
, *req
);
1035 INQUIRE_RETURN(client
, error
);
1043 INQUIRE_RETURN(client
, error
);
1048 create_elements_cb(n
, req
+1, &error
, NULL
);
1050 find_elements(client
->doc
, n
->children
, req
+1, &error
,
1051 NULL
, NULL
, create_elements_cb
, NULL
);
1055 client
->inquire
= INQUIRE_OK
;
1056 INQUIRE_RETURN(client
, error
);
1059 static int store_command(assuan_context_t ctx
, char *line
)
1061 struct client_s
*client
= assuan_get_pointer(ctx
);
1062 gpg_error_t error
= file_modified(client
);
1065 log_write("%s: %s", client
->filename
? client
->filename
: "",
1066 pwmd_strerror(error
));
1067 return send_error(ctx
, error
);
1070 error
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1073 return send_error(ctx
, error
);
1075 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1076 client
->inquire
= INQUIRE_INPROGRESS
;
1080 static int get_command(assuan_context_t ctx
, char *line
)
1082 struct client_s
*client
= assuan_get_pointer(ctx
);
1087 error
= file_modified(client
);
1090 log_write("%s: %s", client
->filename
? client
->filename
: "",
1091 pwmd_strerror(error
));
1092 return send_error(ctx
, error
);
1095 req
= split_input_line(line
, "\t", -1);
1097 if (!req
|| !*req
) {
1099 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1102 n
= find_account(client
->doc
, &req
, &error
, NULL
);
1106 return send_error(ctx
, error
);
1110 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, NULL
);
1115 return send_error(ctx
, error
);
1117 if (!n
|| !n
->children
)
1118 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1122 if (!n
|| !n
->content
|| !*n
->content
)
1123 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1125 error
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1126 return send_error(ctx
, error
);
1129 static gchar
*element_path_to_req(const gchar
*account
, xmlChar
*path
)
1138 for (n
= 0; *p
&& n
< 3; p
++) {
1143 if (strstr((gchar
*)p
, "text()") != NULL
)
1144 p
[xmlStrlen(p
) - 7] = 0;
1146 for (n
= 0; p
[n
]; n
++) {
1151 buf
= g_strdup_printf("%s\t%s", account
, p
);
1155 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...)
1160 gint len
= *array
? g_strv_length(*array
) : 0;
1166 if ((a
= g_realloc(*array
, (len
+ 2) * sizeof(gchar
*))) == NULL
)
1170 ret
= g_vasprintf(&buf
, fmt
, ap
);
1186 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**req
,
1187 gpg_error_t
*error
, void *data
)
1189 struct realpath_s
*rp
= data
;
1192 g_free(rp
->account
);
1194 rp
->account
= g_strdup(req
[0]);
1197 *error
= gpg_error_from_errno(ENOMEM
);
1204 static int realpath_command(assuan_context_t ctx
, char *line
)
1207 struct client_s
*client
= assuan_get_pointer(ctx
);
1213 struct realpath_s
*rp
;
1216 error
= file_modified(client
);
1219 log_write("%s: %s", client
->filename
? client
->filename
: "",
1220 pwmd_strerror(error
));
1221 return send_error(ctx
, error
);
1224 if (strchr(line
, '\t') != NULL
) {
1225 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1226 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1229 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1230 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1233 n
= find_account(client
->doc
, &req
, &error
, NULL
);
1237 return send_error(ctx
, error
);
1240 rp
= g_malloc(sizeof(struct realpath_s
));
1244 return send_syserror(ctx
, ENOMEM
);
1247 rp
->account
= g_strdup(req
[0]);
1251 return send_syserror(ctx
, ENOMEM
);
1255 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
,
1256 NULL
, realpath_elements_cb
, NULL
, rp
);
1259 g_free(rp
->account
);
1262 return send_error(ctx
, error
);
1266 p
= xmlGetNodePath(n
);
1267 result
= element_path_to_req(rp
->account
, p
);
1271 g_free(rp
->account
);
1275 return send_syserror(ctx
, ENOMEM
);
1278 string
= g_string_new(result
);
1280 g_free(rp
->account
);
1287 for (t
= string
->str
+ i
; *t
; t
++, i
++) {
1288 if (!i
|| *t
== '\t') {
1289 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1294 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1295 g_string_free(string
, TRUE
);
1296 return send_error(ctx
, error
);
1299 struct list_element_s
{
1304 static gboolean
append_to_element_list(struct list_element_s
*elements
)
1311 if (!elements
|| !elements
->elements
)
1314 tmp
= g_strjoinv("\t", elements
->elements
);
1319 g_strfreev(elements
->elements
);
1320 elements
->elements
= NULL
;
1321 total
= g_slist_length(elements
->list
);
1322 a
= g_utf8_collate_key(tmp
, -1);
1330 * Removes duplicate element paths from the list. This is needed when
1331 * appending an element tree from list_command(). The glib docs recommend
1332 * using g_utf8_collate_key() for a large number of strings.
1334 for (i
= 0; i
< total
; i
++) {
1335 gchar
*p
= g_slist_nth_data(elements
->list
, i
);
1336 gchar
*b
= g_utf8_collate_key(p
, -1);
1344 if (strcmp(a
, b
) == 0) {
1355 list
= g_slist_append(elements
->list
, tmp
);
1360 elements
->list
= list
;
1364 static gpg_error_t
do_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
1365 struct list_element_s
*elements
, gchar
*prefix
)
1370 if (append_to_element_list(elements
) == FALSE
)
1371 return gpg_error_from_errno(ENOMEM
);
1373 for (n
= node
; n
; n
= n
->next
) {
1374 if (n
->type
== XML_ELEMENT_NODE
) {
1375 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
1379 if (strv_printf(&elements
->elements
, "%s\t%s", prefix
, n
->name
) == FALSE
)
1380 return gpg_error_from_errno(ENOMEM
);
1382 if (append_to_element_list(elements
) == FALSE
)
1383 return gpg_error_from_errno(ENOMEM
);
1386 tmp
= g_strdup_printf("%s\t!%s", prefix
, n
->name
);
1389 return gpg_error_from_errno(ENOMEM
);
1391 if (strv_printf(&elements
->elements
, "%s", tmp
) == FALSE
) {
1393 return gpg_error_from_errno(ENOMEM
);
1397 error
= do_list_recurse(doc
, n
->children
, elements
, tmp
);
1400 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1406 if (append_to_element_list(elements
) == FALSE
)
1407 return gpg_error_from_errno(ENOMEM
);
1414 static gpg_error_t
do_list_command(assuan_context_t ctx
, xmlDocPtr doc
,
1415 struct list_element_s
*elements
, char *line
)
1417 gchar
*prefix
= NULL
, *account
;
1418 gchar
**req
= NULL
, **oreq
= NULL
, *tmp
;
1420 gboolean account_is_literal
, account_has_target
= FALSE
;
1425 if ((req
= split_input_line(line
, "\t", 0)) == NULL
) {
1426 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1427 return EPWMD_COMMAND_SYNTAX
;
1430 prefix
= g_strdup(*req
);
1434 return gpg_error_from_errno(ENOMEM
);
1437 account
= g_strdup(*req
);
1442 return gpg_error_from_errno(ENOMEM
);
1445 oreq
= g_strdupv(req
);
1451 return gpg_error_from_errno(ENOMEM
);
1456 account_has_target
= FALSE
;
1457 account_is_literal
= is_literal_element_str(prefix
);
1458 n
= find_account(doc
, &p
, &error
, &account_has_target
);
1468 if (!which
&& account_is_literal
== FALSE
&& account_has_target
== FALSE
) {
1469 tmp
= g_strdup_printf("!%s", prefix
);
1472 error
= gpg_error_from_errno(ENOMEM
);
1483 n
= find_elements(doc
, n
->children
, p
+1, &error
,
1484 NULL
, NULL
, NULL
, NULL
);
1489 tmp
= g_strjoinv("\t", p
+1);
1491 error
= gpg_error_from_errno(ENOMEM
);
1495 t
= g_strdup_printf("%s\t%s", prefix
, tmp
);
1497 error
= gpg_error_from_errno(ENOMEM
);
1506 if (strv_printf(&elements
->elements
, "%s", prefix
) == FALSE
) {
1507 error
= gpg_error_from_errno(ENOMEM
);
1511 if (node_has_child_element(n
->children
) == FALSE
) {
1512 if (append_to_element_list(elements
) == FALSE
) {
1513 error
= gpg_error_from_errno(ENOMEM
);
1518 error
= do_list_recurse(doc
, n
->children
, elements
, prefix
);
1523 if (!which
++ && !*(p
+1) && account_is_literal
== FALSE
&& account_has_target
== TRUE
) {
1525 *oreq
= g_strdup_printf("!%s", account
);
1528 error
= gpg_error_from_errno(ENOMEM
);
1534 prefix
= g_strdup(*oreq
);
1537 error
= gpg_error_from_errno(ENOMEM
);
1556 * This could be faster especially when finding "target" attributes.
1558 static int list_command(assuan_context_t ctx
, char *line
)
1560 struct client_s
*client
= assuan_get_pointer(ctx
);
1562 struct list_element_s
*elements
= NULL
;
1567 if (disable_list_and_dump
== TRUE
)
1568 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1570 error
= file_modified(client
);
1573 log_write("%s: %s", client
->filename
, pwmd_strerror(error
));
1574 return send_error(ctx
, error
);
1580 error
= list_accounts(client
->doc
, &str
);
1583 return send_error(ctx
, error
);
1585 error
= assuan_send_data(ctx
, str
->str
, str
->len
);
1586 g_string_free(str
, TRUE
);
1587 return send_error(ctx
, error
);
1590 elements
= g_malloc0(sizeof(struct list_element_s
));
1593 error
= gpg_error_from_errno(ENOMEM
);
1597 is_list_command
= TRUE
;
1598 error
= do_list_command(ctx
, client
->doc
, elements
, line
);
1604 total
= g_slist_length(elements
->list
);
1607 error
= EPWMD_EMPTY_ELEMENT
;
1612 * Find element paths with a target and append those element trees to
1615 for (i
= 0; i
< total
; i
++) {
1618 tmp
= g_slist_nth_data(elements
->list
, i
);
1619 req
= split_input_line(tmp
, "\t", 0);
1622 if (g_str_has_prefix(tmp
, "!") == TRUE
) {
1630 for (p
= req
; *p
; p
++) {
1631 if (g_str_has_prefix(*p
, "!") == FALSE
)
1642 error
= do_list_command(ctx
, client
->doc
, elements
, tmp
);
1644 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1647 total
= g_slist_length(elements
->list
);
1651 string
= g_string_new(NULL
);
1653 for (i
= 0; i
< total
; i
++) {
1654 tmp
= g_slist_nth_data(elements
->list
, i
);
1655 g_string_append_printf(string
, "%s\n", tmp
);
1659 string
= g_string_truncate(string
, string
->len
- 1);
1660 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1661 g_string_free(string
, TRUE
);
1664 is_list_command
= FALSE
;
1668 g_slist_free(elements
->list
);
1670 if (elements
->elements
)
1671 g_strfreev(elements
->elements
);
1676 return send_error(ctx
, error
);
1679 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1684 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1685 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1688 return EPWMD_LIBXML_ERROR
;
1691 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1697 * req[0] - element path
1699 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1701 struct client_s
*client
= assuan_get_pointer(ctx
);
1702 gchar
**attrlist
= NULL
;
1704 gchar
**path
= NULL
;
1710 if (!req
|| !req
[0])
1711 return EPWMD_COMMAND_SYNTAX
;
1713 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1715 * The first argument may be only an account.
1717 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1718 return EPWMD_COMMAND_SYNTAX
;
1721 n
= find_account(client
->doc
, &path
, &error
, NULL
);
1729 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1730 NULL
, NULL
, NULL
, NULL
);
1740 for (a
= n
->properties
; a
; a
= a
->next
) {
1743 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1745 g_strfreev(attrlist
);
1748 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
1749 return gpg_error_from_errno(error
);
1754 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1757 g_strfreev(attrlist
);
1758 return gpg_error_from_errno(ENOMEM
);
1761 attrlist
[++i
] = NULL
;
1765 return EPWMD_EMPTY_ELEMENT
;
1767 line
= g_strjoinv("\n", attrlist
);
1770 g_strfreev(attrlist
);
1771 return gpg_error_from_errno(ENOMEM
);
1774 error
= assuan_send_data(ctx
, line
, g_utf8_strlen(line
, -1));
1776 g_strfreev(attrlist
);
1781 * req[0] - attribute
1782 * req[1] - element path
1784 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1788 gchar
**path
= NULL
;
1791 if (!req
|| !req
[0] || !req
[1])
1792 return EPWMD_COMMAND_SYNTAX
;
1794 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1796 * The first argument may be only an account.
1798 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1799 return EPWMD_COMMAND_SYNTAX
;
1803 * Don't remove the "name" attribute for the account element. To remove an
1804 * account use DELETE <account>.
1806 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1807 error
= EPWMD_ATTR_SYNTAX
;
1811 n
= find_account(client
->doc
, &path
, &error
, NULL
);
1817 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1818 NULL
, NULL
, NULL
, NULL
);
1826 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1827 return EPWMD_ATTR_NOT_FOUND
;
1829 if (xmlRemoveProp(a
) == -1)
1830 return EPWMD_LIBXML_ERROR
;
1840 * Creates a "target" attribute. When other commands encounter an element with
1841 * this attribute, the element path is modified to the target value. If the
1842 * source element path doesn't exist when using 'ATTR SET target', it is
1843 * created, but the destination element path must exist.
1845 * req[0] - source element path
1846 * req[1] - destination element path
1848 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1850 gchar
**src
, **dst
, *line
;
1854 if (!req
|| !req
[0] || !req
[1])
1855 return EPWMD_COMMAND_SYNTAX
;
1857 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1859 * The first argument may be only an account.
1861 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1862 return EPWMD_COMMAND_SYNTAX
;
1865 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1867 * The first argument may be only an account.
1869 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1870 error
= EPWMD_COMMAND_SYNTAX
;
1875 n
= find_account(client
->doc
, &dst
, &error
, NULL
);
1878 * Make sure the destination element path exists.
1884 n
= find_elements(client
->doc
, n
->children
, dst
+1, &error
,
1885 NULL
, NULL
, NULL
, NULL
);
1892 n
= find_account(client
->doc
, &src
, &error
, NULL
);
1895 if (error
== EPWMD_ELEMENT_NOT_FOUND
) {
1896 error
= new_account(client
->doc
, src
[0]);
1909 n
= create_target_elements_cb(n
, src
+1, &error
, NULL
);
1911 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
1912 NULL
, NULL
, create_target_elements_cb
, NULL
);
1918 * Reset the position of the element tree now that the elements
1919 * have been created.
1921 n
= find_account(client
->doc
, &src
, &error
, NULL
);
1926 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
1927 NULL
, NULL
, NULL
, NULL
);
1933 line
= g_strjoinv("\t", dst
);
1934 error
= add_attribute(n
, "target", line
);
1953 * req[0] - account name
1956 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
1962 tmp
= g_strdupv(req
);
1965 return gpg_error_from_errno(ENOMEM
);
1967 n
= find_account(client
->doc
, &tmp
, &error
, NULL
);
1973 if (g_utf8_collate(req
[0], req
[1]) == 0)
1977 * Will not overwrite an existing account.
1979 tmp
= g_strdupv(req
+1);
1982 return gpg_error_from_errno(ENOMEM
);
1984 n
= find_account(client
->doc
, &tmp
, &error
, NULL
);
1987 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1991 return EPWMD_ACCOUNT_EXISTS
;
1994 * Whitespace not allowed in account names.
1996 if (contains_whitespace(req
[1]) == TRUE
)
1997 return EPWMD_ATTR_SYNTAX
;
1999 tmp
= g_strdupv(req
);
2002 return gpg_error_from_errno(ENOMEM
);
2004 n
= find_account(client
->doc
, &tmp
, &error
, NULL
);
2008 return EPWMD_ELEMENT_NOT_FOUND
;
2010 return add_attribute(n
, "name", req
[1]);
2014 * req[0] - attribute
2015 * req[1] - element path
2017 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
2019 struct client_s
*client
= assuan_get_pointer(ctx
);
2025 if (!req
|| !req
[0] || !req
[1])
2026 return EPWMD_COMMAND_SYNTAX
;
2028 if (strchr(req
[1], '\t')) {
2029 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2030 return EPWMD_COMMAND_SYNTAX
;
2033 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2034 return EPWMD_COMMAND_SYNTAX
;
2037 n
= find_account(client
->doc
, &path
, &error
, NULL
);
2043 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2044 NULL
, NULL
, NULL
, NULL
);
2052 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2053 return EPWMD_ATTR_NOT_FOUND
;
2055 error
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
2065 * req[0] - attribute
2066 * req[1] - element path
2069 static int attribute_set(struct client_s
*client
, gchar
**req
)
2071 gchar
**path
= NULL
;
2075 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2076 return EPWMD_COMMAND_SYNTAX
;
2079 * Reserved attribute names.
2081 if (g_utf8_collate(req
[0], "name") == 0) {
2083 * Only reserved for the account element. Not the rest of the
2086 if (strchr(req
[1], '\t') == NULL
)
2087 return name_attribute(client
, req
+ 1);
2089 else if (g_utf8_collate(req
[0], "target") == 0)
2090 return target_attribute(client
, req
+ 1);
2092 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2094 * The first argument may be only an account.
2096 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2097 return EPWMD_COMMAND_SYNTAX
;
2100 n
= find_account(client
->doc
, &path
, &error
, NULL
);
2106 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2107 NULL
, NULL
, NULL
, NULL
);
2114 return add_attribute(n
, req
[0], req
[2]);
2123 * req[1] - attribute name or element path if command is LIST
2124 * req[2] - element path
2125 * req[2] - element path or value
2127 static int attr_command(assuan_context_t ctx
, char *line
)
2129 struct client_s
*client
= assuan_get_pointer(ctx
);
2130 gchar
**req
= split_input_line(line
, " ", 4);
2131 gpg_error_t error
= 0;
2133 error
= file_modified(client
);
2136 log_write("%s: %s", client
->filename
? client
->filename
: "",
2137 pwmd_strerror(error
));
2139 return send_error(ctx
, error
);
2142 if (!req
|| !req
[0] || !req
[1]) {
2144 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2147 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2148 error
= attribute_set(client
, req
+1);
2149 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2150 error
= attribute_get(ctx
, req
+1);
2151 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2152 error
= attribute_delete(client
, req
+1);
2153 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2154 error
= attribute_list(ctx
, req
+1);
2156 error
= EPWMD_COMMAND_SYNTAX
;
2159 return send_error(ctx
, error
);
2162 static int iscached_command(assuan_context_t ctx
, char *line
)
2164 gchar
**req
= split_input_line(line
, " ", 0);
2167 CACHE_LOCK(ctx
, NULL
);
2169 if (!req
|| !*req
) {
2171 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2174 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2177 if (cache_iscached(md5file
) == FALSE
)
2178 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2180 return send_error(ctx
, 0);
2183 gpg_error_t
send_cache_status(assuan_context_t ctx
)
2187 tmp
= print_fmt("%i %i",
2189 (cache_size
/ sizeof(file_cache_t
)) - cache_file_count());
2191 return assuan_write_status(ctx
, "CACHE", tmp
);
2194 static int clearcache_command(assuan_context_t ctx
, char *line
)
2196 struct client_s
*client
= assuan_get_pointer(ctx
);
2197 gchar
**req
= split_input_line(line
, " ", 0);
2200 CACHE_LOCK(ctx
, NULL
);
2202 if (!req
|| !*req
) {
2204 cache_clear(client
->md5file
, 2);
2205 return send_error(ctx
, 0);
2208 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2211 if (cache_clear(md5file
, 1) == FALSE
)
2212 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2214 return send_error(ctx
, 0);
2217 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2221 gchar
**req
= split_input_line(line
, " ", 0);
2224 CACHE_LOCK(ctx
, NULL
);
2226 if (!req
|| !*req
|| !req
[1]) {
2228 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2232 timeout
= strtol(req
[0], &p
, 10);
2234 if (errno
!= 0 || *p
!= 0) {
2236 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2239 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2242 if (cache_set_timeout(md5file
, timeout
) == FALSE
)
2243 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2245 send_cache_status_all();
2246 return send_error(ctx
, 0);
2249 static int dump_command(assuan_context_t ctx
, char *line
)
2253 struct client_s
*client
= assuan_get_pointer(ctx
);
2256 if (disable_list_and_dump
== TRUE
)
2257 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2259 error
= file_modified(client
);
2262 log_write("%s: %s", client
->filename
? client
->filename
: "",
2263 pwmd_strerror(error
));
2264 return send_error(ctx
, error
);
2267 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2268 error
= assuan_send_data(ctx
, xml
, len
);
2270 return send_error(ctx
, error
);
2273 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2275 struct client_s
*client
= assuan_get_pointer(ctx
);
2276 gpg_error_t error
= 0;
2279 if (g_utf8_collate(line
, "key") == 0 || g_utf8_collate(line
, "key_file") == 0)
2280 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2282 p
= get_key_file_string(client
->filename
? client
->filename
: "default", line
);
2285 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2287 tmp
= expand_homedir(p
);
2290 error
= assuan_send_data(ctx
, p
, g_utf8_strlen(p
, -1));
2292 return send_error(ctx
, error
);
2295 void cleanup_assuan(assuan_context_t ctx
)
2297 struct client_s
*cl
= assuan_get_pointer(ctx
);
2302 static void reset_notify(assuan_context_t ctx
)
2304 struct client_s
*cl
= assuan_get_pointer(ctx
);
2309 gpg_error_t
register_commands(assuan_context_t ctx
)
2313 int (*handler
)(assuan_context_t
, char *line
);
2315 { "OPEN", open_command
},
2316 { "SAVE", save_command
},
2317 { "LIST", list_command
},
2318 { "REALPATH", realpath_command
},
2319 { "STORE", store_command
},
2320 { "DELETE", delete_command
},
2321 { "GET", get_command
},
2322 { "ATTR", attr_command
},
2323 { "ISCACHED", iscached_command
},
2324 { "CLEARCACHE", clearcache_command
},
2325 { "CACHETIMEOUT", cachetimeout_command
},
2326 { "GETCONFIG", getconfig_command
},
2327 { "DUMP", dump_command
},
2334 for (i
=0; table
[i
].name
; i
++) {
2335 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2341 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2345 return assuan_register_reset_notify(ctx
, reset_notify
);
2348 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2354 guchar tkey
[gcrykeysize
];
2355 struct file_header_s
{
2357 guchar iv
[gcryblocksize
];
2359 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2360 gcry_cipher_hd_t gh
;
2361 guint iter
= 0, n_iter
= 0;
2363 void *outbuf
= NULL
;
2369 gcryerrno
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2377 lseek(fd
, 0, SEEK_SET
);
2378 insize
= st
.st_size
- sizeof(struct file_header_s
);
2379 iv
= gcry_malloc(gcryblocksize
);
2383 gcry_cipher_close(gh
);
2385 return gpg_error_from_errno(ENOMEM
);
2388 len
= pth_read(fd
, &file_header
, sizeof(struct file_header_s
));
2390 if (len
!= sizeof(file_header
)) {
2394 gcry_cipher_close(gh
);
2398 return gpg_error_from_errno(errno
);
2401 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2402 inbuf
= gcry_malloc(insize
);
2406 gcry_cipher_close(gh
);
2409 return gpg_error_from_errno(ENOMEM
);
2412 len
= pth_read(fd
, inbuf
, insize
);
2414 if (len
!= insize
) {
2418 gcry_cipher_close(gh
);
2422 return gpg_error_from_errno(errno
);
2425 memcpy(tkey
, key
, sizeof(tkey
));
2428 if ((gcryerrno
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2430 gcry_cipher_close(gh
);
2431 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2434 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2441 if ((gcryerrno
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2443 gcry_cipher_close(gh
);
2444 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2447 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2453 gcry_cipher_close(gh
);
2458 iter_progress
= get_key_file_integer("default", "iteration_progress");
2460 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2461 error
= assuan_write_status(client
->ctx
, "DECRYPT", "0");
2470 if (decrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
2472 gcry_cipher_close(gh
);
2473 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2476 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2483 if ((gcryerrno
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2485 gcry_cipher_close(gh
);
2486 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2489 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2496 while (iter
< file_header
.iter
) {
2497 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2498 if (!(iter
% iter_progress
)) {
2499 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i",
2500 ++n_iter
* iter_progress
));
2510 if ((gcryerrno
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2512 gcry_cipher_close(gh
);
2513 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2516 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2523 if (decrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
2525 gcry_cipher_close(gh
);
2526 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2529 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(gcryerrno
));
2540 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2541 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i", file_header
.iter
));
2552 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
2554 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2556 if (zerror
== Z_MEM_ERROR
) {
2558 return gpg_error_from_errno(ENOMEM
);
2560 else if (zerror
!= Z_DATA_ERROR
) {
2564 gcry_cipher_close(gh
);
2566 return EPWMD_BADKEY
;
2575 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2579 gcry_cipher_close(gh
);
2581 return EPWMD_BADKEY
;
2585 client
->xml
= inbuf
;
2586 client
->len
= insize
;
2589 gcry_cipher_close(gh
);