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>
44 #include "pwmd_error.h"
48 static void *z_alloc(void *data
, unsigned items
, unsigned size
)
51 return gcry_calloc(items
, size
);
53 return calloc(items
, size
);
57 static void z_free(void *data
, void *p
)
66 static gpg_error_t
file_modified(struct client_s
*client
)
70 if (client
->state
!= STATE_OPEN
)
73 if (stat(client
->filename
, &st
) == 0 && client
->mtime
) {
74 if (client
->mtime
!= st
.st_mtime
)
75 return EPWMD_FILE_MODIFIED
;
81 static gboolean
encrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
82 void *inbuf
, gsize insize
)
86 if ((rc
= gcry_cipher_encrypt(gh
, outbuf
, outsize
, inbuf
, insize
))) {
87 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
94 gpg_error_t
decrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
95 void *inbuf
, gsize insize
)
99 if ((rc
= gcry_cipher_decrypt(gh
, outbuf
, outsize
, inbuf
, insize
)))
100 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
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 void unlock_file_mutex(struct client_s
*client
)
151 if (client
->has_lock
== FALSE
|| client
->pinentry
->status
== PINENTRY_INIT
)
154 CACHE_LOCK(client
->ctx
);
156 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
162 pth_mutex_release(m
);
163 client
->has_lock
= FALSE
;
166 gpg_error_t
lock_file_mutex(struct client_s
*client
)
170 if (client
->has_lock
== TRUE
)
173 CACHE_LOCK(client
->ctx
);
175 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
182 if (pth_mutex_acquire(m
, TRUE
, NULL
) == FALSE
) {
183 if (errno
== EBUSY
) {
185 assuan_write_status(client
->ctx
, "LOCKED", N_("Waiting for lock"));
187 pth_mutex_acquire(m
, FALSE
, NULL
);
191 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(errno
));
192 return gpg_error_from_errno(e
);
196 client
->has_lock
= TRUE
;
200 void free_client(struct client_s
*client
)
203 xmlFreeDoc(client
->doc
);
206 gcry_free(client
->xml
);
208 if (client
->filename
)
209 g_free(client
->filename
);
212 gcry_cipher_close(client
->gh
);
215 static void cleanup_client(struct client_s
*client
)
217 assuan_context_t ctx
= client
->ctx
;
218 struct pinentry_s
*pin
= client
->pinentry
;
220 unlock_file_mutex(client
);
221 CACHE_LOCK(client
->ctx
);
222 cache_decr_refcount(client
->md5file
);
225 * This may be a new file so don't use a cache slot. save_command() will
226 * set this to FALSE on success.
228 if (client
->new == TRUE
)
229 cache_clear(client
->md5file
, 1);
232 memset(client
, 0, sizeof(struct client_s
));
233 client
->state
= STATE_CONNECTED
;
235 client
->freed
= TRUE
;
236 client
->pinentry
= pin
;
240 static gchar
*print_fmt(const char *fmt
, ...)
243 static gchar buf
[ASSUAN_LINELENGTH
] = {0};
246 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
251 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gint insize
,
252 gpointer
*out
, glong
*outsize
, gint
*error
)
265 z
.avail_out
= zlib_bufsize
;
266 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
269 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
270 *error
= Z_MEM_ERROR
;
274 ret
= inflateInit2(&z
, 47);
277 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
282 memset(&h
, 0, sizeof(gz_header
));
283 h
.comment
= (guchar
*)buf
;
284 h
.comm_max
= sizeof(buf
);
285 ret
= inflateGetHeader(&z
, &h
);
288 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
294 ret
= inflate(&z
, Z_BLOCK
);
297 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
304 insize
= atoi((gchar
*)h
.comment
);
309 ret
= inflate(&z
, Z_FINISH
);
316 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
324 z
.next_out
= pout
+ z
.total_out
;
325 z
.avail_out
= zlib_bufsize
;
328 rc
= assuan_write_status(ctx
, "DECOMPRESS",
329 print_fmt("%i %i", z
.total_out
, insize
));
346 } while (ret
!= Z_STREAM_END
);
349 rc
= assuan_write_status(ctx
, "DECOMPRESS",
350 print_fmt("%i %i", z
.total_out
, insize
));
359 *outsize
= z
.total_out
;
364 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
371 static gpg_error_t
open_command_finalize(assuan_context_t ctx
, guchar shakey
[],
374 struct client_s
*client
= assuan_get_pointer(ctx
);
380 if ((fd
= open_file(client
->filename
, &st
)) == -1) {
382 if (errno
== ENOENT
) {
390 log_write("%s: %s", client
->filename
, strerror(errno
));
391 cleanup_client(client
);
392 memset(shakey
, 0, sizeof(shakey
));
393 return send_syserror(ctx
, error
);
396 error
= try_xml_decrypt(ctx
, fd
, st
, shakey
);
400 memset(shakey
, 0, sizeof(shakey
));
401 cleanup_client(client
);
402 return send_error(ctx
, error
);
406 CACHE_LOCK(client
->ctx
);
408 if (cached
== FALSE
&& shakey
[0]) {
409 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
410 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
411 cleanup_client(client
);
413 return send_error(ctx
, EPWMD_MAX_SLOTS
);
416 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
417 cache_reset_timeout(client
->md5file
, timeout
);
420 cache_set_timeout(client
->md5file
, -2);
425 memset(shakey
, 0, sizeof(shakey
));
426 error
= parse_xml(ctx
);
429 gcry_free(client
->xml
);
434 if (client
->new == FALSE
)
435 send_cache_status_all();
437 client
->state
= STATE_OPEN
;
440 return send_error(ctx
, error
);
443 static int open_command(assuan_context_t ctx
, char *line
)
446 guchar shakey
[gcrykeysize
];
447 gboolean cached
= FALSE
;
449 struct client_s
*client
= assuan_get_pointer(ctx
);
451 gchar
*filename
= NULL
;
453 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
456 if (!filename
|| !*filename
) {
458 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
461 if (valid_filename(filename
) == FALSE
) {
463 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
466 if (client
->state
== STATE_OPEN
)
467 cleanup_client(client
);
469 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
470 CACHE_LOCK(client
->ctx
);
472 if (cache_has_file(client
->md5file
) == FALSE
) {
473 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
476 return send_error(ctx
, EPWMD_MAX_SLOTS
);
480 cache_incr_refcount(client
->md5file
);
482 error
= lock_file_mutex(client
);
486 return send_error(ctx
, error
);
489 client
->freed
= FALSE
;
491 if ((error
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
493 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
494 cleanup_client(client
);
495 return send_error(ctx
, error
);
498 if (stat(filename
, &st
) == 0) {
499 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
500 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
502 cleanup_client(client
);
503 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
506 client
->mtime
= st
.st_mtime
;
509 client
->filename
= g_strdup(filename
);
511 if (!client
->filename
) {
512 memset(shakey
, 0, sizeof(shakey
));
513 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
514 cleanup_client(client
);
516 return send_syserror(ctx
, ENOMEM
);
520 * New files don't need a key.
522 if (access(filename
, R_OK
|W_OK
) != 0) {
523 if (errno
!= ENOENT
) {
525 log_write("%s: %s", filename
, strerror(errno
));
527 cleanup_client(client
);
528 return send_syserror(ctx
, error
);
531 if ((client
->xml
= new_document()) == NULL
) {
532 log_write("%s", strerror(ENOMEM
));
534 cleanup_client(client
);
535 return send_syserror(ctx
, ENOMEM
);
538 client
->len
= xmlStrlen(client
->xml
);
540 client
->filename
= g_strdup(filename
);
542 if (!client
->filename
) {
544 cleanup_client(client
);
545 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
546 return send_syserror(ctx
, ENOMEM
);
549 memset(shakey
, 0, sizeof(shakey
));
551 if (req
[1] && *req
[1])
552 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
555 return open_command_finalize(ctx
, shakey
, cached
);
558 CACHE_LOCK(client
->ctx
);
559 cached
= cache_get_key(client
->md5file
, shakey
);
562 if (cached
== FALSE
) {
564 * No key specified and no matching filename found in the cache. Use
565 * pinentry to retrieve the key. Cannot return assuan_process_done()
566 * here otherwise the command will be interrupted. The event loop in
567 * client_thread() will poll the file descriptor waiting for it to
568 * become ready to read a pinentry_key_s which will contain the
569 * entered key or error. It will then call open_command_finalize() to
570 * to finish the command.
572 if (!req
[1] || !*req
[1]) {
575 if (client
->pinentry
->use
== FALSE
) {
576 cleanup_client(client
);
577 return send_error(ctx
, EPWMD_KEY
);
580 client
->pinentry
->filename
= client
->filename
;
581 client
->pinentry
->which
= PINENTRY_OPEN
;
582 error
= pinentry_fork(ctx
);
585 cleanup_client(client
);
586 return send_error(ctx
, error
);
589 client
->pinentry
->cb
= open_command_finalize
;
590 client
->pinentry
->status
= PINENTRY_INIT
;
593 cleanup_client(client
);
594 return send_error(ctx
, EPWMD_KEY
);
598 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
602 return open_command_finalize(ctx
, shakey
, cached
);
605 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
606 gint size
, gpointer
*out
, glong
*outsize
, gint
*error
)
613 gint cmd
= Z_NO_FLUSH
;
618 z
.next_in
= pin
= data
;
619 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
620 z
.avail_out
= zlib_bufsize
;
621 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
624 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
625 *error
= Z_MEM_ERROR
;
629 ret
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
632 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
638 memset(&h
, 0, sizeof(gz_header
));
639 snprintf(buf
, sizeof(buf
), "%i", size
);
640 h
.comment
= (guchar
*)buf
;
641 ret
= deflateSetHeader(&z
, &h
);
644 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
654 ret
= deflate(&z
, cmd
);
661 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
669 z
.next_out
= pout
+ z
.total_out
;
670 z
.avail_out
= zlib_bufsize
;
673 if (!z
.avail_in
&& z
.total_in
< size
) {
674 if (z
.total_in
+ zlib_bufsize
> size
)
675 z
.avail_in
= size
- z
.total_in
;
677 z
.avail_in
= zlib_bufsize
;
680 rc
= assuan_write_status(ctx
, "COMPRESS",
681 print_fmt("%i %i", z
.total_in
, size
));
690 if (z
.total_in
>= size
)
701 } while (ret
!= Z_STREAM_END
);
704 rc
= assuan_write_status(ctx
, "COMPRESS",
705 print_fmt("%i %i", z
.total_in
, size
));
714 *outsize
= z
.total_out
;
719 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
726 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
727 const gchar
*filename
, gpointer data
, size_t insize
,
728 const guchar
*shakey
, guint iter
)
733 guchar tkey
[gcrykeysize
];
737 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
738 gchar tmp
[FILENAME_MAX
];
739 struct file_header_s
{
741 guchar iv
[gcryblocksize
];
746 file_header
.iter
= iter
;
750 if (insize
/ gcryblocksize
) {
751 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
753 if (insize
% gcryblocksize
)
754 len
+= gcryblocksize
;
758 * Resize the existing xml buffer to the block size required by gcrypt
759 * rather than duplicating it and wasting memory.
761 inbuf
= gcry_realloc(data
, len
);
764 return gpg_error_from_errno(ENOMEM
);
767 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
768 memcpy(tkey
, shakey
, sizeof(tkey
));
771 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
773 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
777 file_header
.iter
= iter
;
780 iter_progress
= get_key_file_integer("default", "iteration_progress");
782 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
783 error
= assuan_write_status(client
->ctx
, "ENCRYPT", "0");
791 while (xiter
< file_header
.iter
) {
792 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
793 if (!(xiter
% iter_progress
)) {
794 error
= assuan_write_status(client
->ctx
, "ENCRYPT", print_fmt("%i",
795 ++n_iter
* iter_progress
));
804 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
805 sizeof(file_header
.iv
)))) {
807 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
811 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
814 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
822 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
823 sizeof(file_header
.iv
)))) {
825 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
829 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
831 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
835 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
837 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
841 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
842 error
= assuan_write_status(client
->ctx
, "ENCRYPT",
843 print_fmt("%i", file_header
.iter
));
853 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
855 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
858 p
= strrchr(tmp
, '/');
860 log_write("%s: %s", p
, strerror(errno
));
861 return gpg_error_from_errno(error
);
866 * xml_import() from command line.
870 len
= pth_write(fd
, &file_header
, sizeof(struct file_header_s
));
872 if (len
!= sizeof(file_header
)) {
879 return gpg_error_from_errno(len
);
882 len
= pth_write(fd
, inbuf
, insize
);
891 return gpg_error_from_errno(len
);
894 if (fsync(fd
) == -1) {
898 return gpg_error_from_errno(len
);
907 if (stat(filename
, &st
) == 0)
908 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
910 if (rename(tmp
, filename
) == -1) {
913 return gpg_error_from_errno(len
);
917 chmod(filename
, mode
);
924 static gpg_error_t
save_command_finalize(assuan_context_t ctx
,
925 guchar shakey
[], gboolean cached
)
927 struct client_s
*client
= assuan_get_pointer(ctx
);
939 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
942 iter
= get_key_file_integer(client
->filename
, "compression_level");
947 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
948 memset(shakey
, 0, sizeof(shakey
));
951 if (zerror
== Z_MEM_ERROR
) {
952 return send_syserror(ctx
, ENOMEM
);
955 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
963 iter
= get_key_file_integer(client
->filename
, "iterations");
964 error
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
967 memset(shakey
, 0, sizeof(shakey
));
968 return send_error(ctx
, error
);
971 stat(client
->filename
, &st
);
972 client
->mtime
= st
.st_mtime
;
973 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
974 CACHE_LOCK(client
->ctx
);
977 memset(shakey
, 0, sizeof(shakey
));
978 cache_reset_timeout(client
->md5file
, timeout
);
981 if (client
->new == TRUE
)
982 send_cache_status_all();
985 return send_error(ctx
, 0);
988 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
989 memset(shakey
, 0, sizeof(shakey
));
990 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
992 return send_error(ctx
, EPWMD_MAX_SLOTS
);
996 memset(shakey
, 0, sizeof(shakey
));
997 cache_reset_timeout(client
->md5file
, timeout
);
999 send_cache_status_all();
1000 return send_error(ctx
, 0);
1003 static int save_command(assuan_context_t ctx
, char *line
)
1005 gboolean cached
= FALSE
;
1006 guchar shakey
[gcrykeysize
];
1008 struct client_s
*client
= assuan_get_pointer(ctx
);
1011 error
= file_modified(client
);
1014 log_write("%s: %s", client
->filename
? client
->filename
: "",
1015 pwmd_strerror(error
));
1016 return send_error(ctx
, error
);
1019 error
= lock_file_mutex(client
);
1022 return send_error(ctx
, error
);
1024 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1025 return send_syserror(ctx
, errno
);
1027 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
1028 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
1029 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
1032 if (!line
|| !*line
) {
1035 if (cache_get_key(client
->md5file
, shakey
) == FALSE
) {
1037 #ifdef WITH_PINENTRY
1038 if (client
->pinentry
->use
== FALSE
)
1039 return send_error(ctx
, EPWMD_KEY
);
1041 client
->pinentry
->which
= PINENTRY_SAVE
;
1042 error
= pinentry_fork(ctx
);
1045 return send_error(ctx
, error
);
1047 client
->pinentry
->cb
= save_command_finalize
;
1048 client
->pinentry
->status
= PINENTRY_INIT
;
1051 return send_error(ctx
, EPWMD_KEY
);
1059 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
1060 memset(line
, 0, strlen(line
));
1063 return save_command_finalize(ctx
, shakey
, cached
);
1066 static gboolean
contains_whitespace(const gchar
*str
)
1068 const gchar
*p
= str
;
1072 len
= g_utf8_strlen(p
++, -1) -1;
1075 c
= g_utf8_get_char(p
++);
1077 if (g_unichar_isspace(c
))
1084 static int delete_command(assuan_context_t ctx
, char *line
)
1086 struct client_s
*client
= assuan_get_pointer(ctx
);
1091 error
= file_modified(client
);
1094 log_write("%s: %s", client
->filename
? client
->filename
: "",
1095 pwmd_strerror(error
));
1096 return send_error(ctx
, error
);
1099 if (strchr(line
, '\t'))
1100 req
= split_input_line(line
, "\t", -1);
1102 req
= split_input_line(line
, " ", -1);
1105 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1107 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1111 return send_error(ctx
, error
);
1115 * No sub-node defined. Remove the entire node (account).
1124 return send_error(ctx
, 0);
1127 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1131 return send_error(ctx
, error
);
1138 return send_error(ctx
, 0);
1142 * Don't return with assuan_process_done() here. This has been called from
1143 * assuan_process_next() and the command should be finished in
1146 static int store_command_finalize(gpointer data
, gint rc
, guchar
*line
,
1149 assuan_context_t ctx
= data
;
1150 struct client_s
*client
= assuan_get_pointer(ctx
);
1152 guchar
*result
= line
;
1154 gpg_error_t error
= file_modified(client
);
1166 req
= split_input_line((gchar
*)result
, "\t", 0);
1181 return EPWMD_COMMAND_SYNTAX
;
1183 if (valid_xml_element((xmlChar
*)*req
) == FALSE
) {
1185 return EPWMD_INVALID_ELEMENT
;
1188 if (valid_element_path(req
+1, TRUE
) == FALSE
) {
1190 return EPWMD_INVALID_ELEMENT
;
1194 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1196 if (error
&& error
== EPWMD_ELEMENT_NOT_FOUND
) {
1197 error
= new_account(client
->doc
, *req
);
1214 create_elements_cb(n
, req
+1, &error
, NULL
);
1216 find_elements(client
->doc
, n
->children
, req
+1, &error
,
1217 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1221 client
->inquire_status
= INQUIRE_DONE
;
1225 static int store_command(assuan_context_t ctx
, char *line
)
1227 struct client_s
*client
= assuan_get_pointer(ctx
);
1228 gpg_error_t error
= file_modified(client
);
1231 log_write("%s: %s", client
->filename
? client
->filename
: "",
1232 pwmd_strerror(error
));
1233 return send_error(ctx
, error
);
1236 error
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1239 return send_error(ctx
, error
);
1241 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1242 client
->inquire_status
= INQUIRE_BUSY
;
1246 static int get_command(assuan_context_t ctx
, char *line
)
1248 struct client_s
*client
= assuan_get_pointer(ctx
);
1253 error
= file_modified(client
);
1256 log_write("%s: %s", client
->filename
? client
->filename
: "",
1257 pwmd_strerror(error
));
1258 return send_error(ctx
, error
);
1261 req
= split_input_line(line
, "\t", -1);
1263 if (!req
|| !*req
) {
1265 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1268 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1272 return send_error(ctx
, error
);
1276 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1281 return send_error(ctx
, error
);
1283 if (!n
|| !n
->children
)
1284 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1288 if (!n
|| !n
->content
|| !*n
->content
)
1289 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1291 error
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1292 return send_error(ctx
, error
);
1295 static gchar
*element_path_to_req(const gchar
*account
, xmlChar
*path
)
1304 for (n
= 0; *p
&& n
< 3; p
++) {
1309 if (strstr((gchar
*)p
, "text()") != NULL
)
1310 p
[xmlStrlen(p
) - 7] = 0;
1312 for (n
= 0; p
[n
]; n
++) {
1317 buf
= g_strdup_printf("%s\t%s", account
, p
);
1318 p
= (xmlChar
*)buf
+ strlen(buf
) - 1;
1326 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...)
1331 gint len
= *array
? g_strv_length(*array
) : 0;
1337 if ((a
= g_realloc(*array
, (len
+ 2) * sizeof(gchar
*))) == NULL
)
1341 ret
= g_vasprintf(&buf
, fmt
, ap
);
1357 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**req
,
1358 gpg_error_t
*error
, void *data
)
1360 struct realpath_s
*rp
= data
;
1363 g_free(rp
->account
);
1365 rp
->account
= g_strdup(req
[0]);
1368 *error
= gpg_error_from_errno(ENOMEM
);
1375 static int realpath_command(assuan_context_t ctx
, char *line
)
1378 struct client_s
*client
= assuan_get_pointer(ctx
);
1384 struct realpath_s
*rp
;
1387 error
= file_modified(client
);
1390 log_write("%s: %s", client
->filename
? client
->filename
: "",
1391 pwmd_strerror(error
));
1392 return send_error(ctx
, error
);
1395 if (strchr(line
, '\t') != NULL
) {
1396 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1397 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1400 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1401 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1404 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1408 return send_error(ctx
, error
);
1411 rp
= g_malloc(sizeof(struct realpath_s
));
1415 return send_syserror(ctx
, ENOMEM
);
1418 rp
->account
= g_strdup(req
[0]);
1422 return send_syserror(ctx
, ENOMEM
);
1426 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
,
1427 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, rp
);
1430 g_free(rp
->account
);
1433 return send_error(ctx
, error
);
1437 p
= xmlGetNodePath(n
);
1438 result
= element_path_to_req(rp
->account
, p
);
1442 g_free(rp
->account
);
1446 return send_syserror(ctx
, ENOMEM
);
1449 string
= g_string_new(result
);
1451 g_free(rp
->account
);
1458 for (t
= string
->str
+ i
; *t
; t
++, i
++) {
1459 if ((!i
&& *t
!= '!') || *t
== '\t') {
1460 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1465 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1466 g_string_free(string
, TRUE
);
1467 return send_error(ctx
, error
);
1470 struct list_element_s
{
1475 static gboolean
append_to_element_list(struct list_element_s
*elements
)
1482 if (!elements
|| !elements
->elements
)
1485 tmp
= g_strjoinv("\t", elements
->elements
);
1490 g_strfreev(elements
->elements
);
1491 elements
->elements
= NULL
;
1492 total
= g_slist_length(elements
->list
);
1493 a
= g_utf8_collate_key(tmp
, -1);
1501 * Removes duplicate element paths from the list. This is needed when
1502 * appending an element tree from list_command(). The glib docs recommend
1503 * using g_utf8_collate_key() for a large number of strings.
1505 for (i
= 0; i
< total
; i
++) {
1506 gchar
*p
= g_slist_nth_data(elements
->list
, i
);
1507 gchar
*b
= g_utf8_collate_key(p
, -1);
1515 if (strcmp(a
, b
) == 0) {
1526 list
= g_slist_append(elements
->list
, tmp
);
1531 elements
->list
= list
;
1535 static gpg_error_t
do_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
1536 struct list_element_s
*elements
, gchar
*prefix
)
1541 if (append_to_element_list(elements
) == FALSE
)
1542 return gpg_error_from_errno(ENOMEM
);
1544 for (n
= node
; n
; n
= n
->next
) {
1545 if (n
->type
== XML_ELEMENT_NODE
) {
1546 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
1550 if (strv_printf(&elements
->elements
, "%s\t%s", prefix
, n
->name
) == FALSE
)
1551 return gpg_error_from_errno(ENOMEM
);
1553 if (append_to_element_list(elements
) == FALSE
)
1554 return gpg_error_from_errno(ENOMEM
);
1557 tmp
= g_strdup_printf("%s\t!%s", prefix
, n
->name
);
1560 return gpg_error_from_errno(ENOMEM
);
1562 if (strv_printf(&elements
->elements
, "%s", tmp
) == FALSE
) {
1564 return gpg_error_from_errno(ENOMEM
);
1568 error
= do_list_recurse(doc
, n
->children
, elements
, tmp
);
1571 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1577 if (append_to_element_list(elements
) == FALSE
)
1578 return gpg_error_from_errno(ENOMEM
);
1585 static gpg_error_t
do_list_command(assuan_context_t ctx
, xmlDocPtr doc
,
1586 struct list_element_s
*elements
, char *line
)
1588 gchar
*prefix
= NULL
, *account
;
1589 gchar
**req
= NULL
, **oreq
= NULL
, *tmp
;
1591 gboolean account_is_literal
, account_has_target
= FALSE
;
1596 if ((req
= split_input_line(line
, "\t", 0)) == NULL
) {
1597 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1598 return EPWMD_COMMAND_SYNTAX
;
1601 prefix
= g_strdup(*req
);
1605 return gpg_error_from_errno(ENOMEM
);
1608 account
= g_strdup(*req
);
1613 return gpg_error_from_errno(ENOMEM
);
1616 oreq
= g_strdupv(req
);
1622 return gpg_error_from_errno(ENOMEM
);
1627 account_has_target
= FALSE
;
1628 account_is_literal
= is_literal_element_str(prefix
);
1629 n
= find_account(doc
, &p
, &error
, &account_has_target
, 0);
1639 if (!which
&& account_is_literal
== FALSE
&& account_has_target
== FALSE
) {
1640 tmp
= g_strdup_printf("!%s", prefix
);
1643 error
= gpg_error_from_errno(ENOMEM
);
1654 n
= find_elements(doc
, n
->children
, p
+1, &error
, NULL
, NULL
, NULL
,
1660 tmp
= g_strjoinv("\t", p
+1);
1662 error
= gpg_error_from_errno(ENOMEM
);
1666 t
= g_strdup_printf("%s\t%s", prefix
, tmp
);
1668 error
= gpg_error_from_errno(ENOMEM
);
1677 if (strv_printf(&elements
->elements
, "%s", prefix
) == FALSE
) {
1678 error
= gpg_error_from_errno(ENOMEM
);
1682 if (node_has_child_element(n
->children
) == FALSE
) {
1683 if (append_to_element_list(elements
) == FALSE
) {
1684 error
= gpg_error_from_errno(ENOMEM
);
1689 error
= do_list_recurse(doc
, n
->children
, elements
, prefix
);
1694 if (!which
++ && !*(p
+1) && account_is_literal
== FALSE
&& account_has_target
== TRUE
) {
1696 *oreq
= g_strdup_printf("!%s", account
);
1699 error
= gpg_error_from_errno(ENOMEM
);
1705 prefix
= g_strdup(*oreq
);
1708 error
= gpg_error_from_errno(ENOMEM
);
1727 * This could be faster especially when finding "target" attributes.
1729 static int list_command(assuan_context_t ctx
, char *line
)
1731 struct client_s
*client
= assuan_get_pointer(ctx
);
1733 struct list_element_s
*elements
= NULL
;
1738 if (disable_list_and_dump
== TRUE
)
1739 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1741 error
= file_modified(client
);
1744 log_write("%s: %s", client
->filename
, pwmd_strerror(error
));
1745 return send_error(ctx
, error
);
1751 error
= list_accounts(client
->doc
, &str
);
1754 return send_error(ctx
, error
);
1756 error
= assuan_send_data(ctx
, str
->str
, str
->len
);
1757 g_string_free(str
, TRUE
);
1758 return send_error(ctx
, error
);
1761 elements
= g_malloc0(sizeof(struct list_element_s
));
1764 error
= gpg_error_from_errno(ENOMEM
);
1768 error
= do_list_command(ctx
, client
->doc
, elements
, line
);
1774 total
= g_slist_length(elements
->list
);
1777 error
= EPWMD_EMPTY_ELEMENT
;
1782 * Find element paths with a target and append those element trees to
1785 for (i
= 0; i
< total
; i
++) {
1788 tmp
= g_slist_nth_data(elements
->list
, i
);
1789 req
= split_input_line(tmp
, "\t", 0);
1792 if (g_str_has_prefix(tmp
, "!") == TRUE
) {
1800 for (p
= req
; *p
; p
++) {
1801 if (g_str_has_prefix(*p
, "!") == FALSE
)
1812 error
= do_list_command(ctx
, client
->doc
, elements
, tmp
);
1814 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1817 total
= g_slist_length(elements
->list
);
1821 string
= g_string_new(NULL
);
1823 for (i
= 0; i
< total
; i
++) {
1824 tmp
= g_slist_nth_data(elements
->list
, i
);
1825 g_string_append_printf(string
, "%s\n", tmp
);
1829 string
= g_string_truncate(string
, string
->len
- 1);
1830 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1831 g_string_free(string
, TRUE
);
1836 g_slist_free(elements
->list
);
1838 if (elements
->elements
)
1839 g_strfreev(elements
->elements
);
1844 return send_error(ctx
, error
);
1847 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1852 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1853 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1856 return EPWMD_LIBXML_ERROR
;
1859 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1865 * req[0] - element path
1867 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1869 struct client_s
*client
= assuan_get_pointer(ctx
);
1870 gchar
**attrlist
= NULL
;
1872 gchar
**path
= NULL
;
1878 if (!req
|| !req
[0])
1879 return EPWMD_COMMAND_SYNTAX
;
1881 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1883 * The first argument may be only an account.
1885 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1886 return EPWMD_COMMAND_SYNTAX
;
1889 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1897 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1898 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1908 for (a
= n
->properties
; a
; a
= a
->next
) {
1911 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1913 g_strfreev(attrlist
);
1916 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
1917 return gpg_error_from_errno(error
);
1922 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1925 g_strfreev(attrlist
);
1926 return gpg_error_from_errno(ENOMEM
);
1929 attrlist
[++i
] = NULL
;
1933 return EPWMD_EMPTY_ELEMENT
;
1935 line
= g_strjoinv("\n", attrlist
);
1938 g_strfreev(attrlist
);
1939 return gpg_error_from_errno(ENOMEM
);
1942 error
= assuan_send_data(ctx
, line
, strlen(line
));
1944 g_strfreev(attrlist
);
1949 * req[0] - attribute
1950 * req[1] - element path
1952 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1956 gchar
**path
= NULL
;
1959 if (!req
|| !req
[0] || !req
[1])
1960 return EPWMD_COMMAND_SYNTAX
;
1962 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1964 * The first argument may be only an account.
1966 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1967 return EPWMD_COMMAND_SYNTAX
;
1971 * Don't remove the "name" attribute for the account element. To remove an
1972 * account use DELETE <account>.
1974 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1975 error
= EPWMD_ATTR_SYNTAX
;
1979 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1985 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1986 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1994 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1995 return EPWMD_ATTR_NOT_FOUND
;
1997 if (xmlRemoveProp(a
) == -1)
1998 return EPWMD_LIBXML_ERROR
;
2008 * Creates a "target" attribute. When other commands encounter an element with
2009 * this attribute, the element path is modified to the target value. If the
2010 * source element path doesn't exist when using 'ATTR SET target', it is
2011 * created, but the destination element path must exist.
2013 * req[0] - source element path
2014 * req[1] - destination element path
2016 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
2018 gchar
**src
, **dst
, *line
= NULL
;
2022 if (!req
|| !req
[0] || !req
[1])
2023 return EPWMD_COMMAND_SYNTAX
;
2025 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2027 * The first argument may be only an account.
2029 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
2030 return EPWMD_COMMAND_SYNTAX
;
2033 if (valid_element_path(src
, FALSE
) == FALSE
) {
2035 return EPWMD_INVALID_ELEMENT
;
2038 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2040 * The first argument may be only an account.
2042 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
2043 error
= EPWMD_COMMAND_SYNTAX
;
2048 n
= find_account(client
->doc
, &dst
, &error
, NULL
, 0);
2051 * Make sure the destination element path exists.
2057 n
= find_elements(client
->doc
, n
->children
, dst
+1, &error
,
2058 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2065 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
2068 if (error
== EPWMD_ELEMENT_NOT_FOUND
) {
2069 error
= new_account(client
->doc
, src
[0]);
2082 n
= create_target_elements_cb(n
, src
+1, &error
, NULL
);
2084 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
2085 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
2091 * Reset the position of the element tree now that the elements
2092 * have been created.
2094 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
2099 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
2100 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2106 line
= g_strjoinv("\t", dst
);
2107 error
= add_attribute(n
, "target", line
);
2117 * req[0] - account name
2120 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
2126 tmp
= g_strdupv(req
);
2129 return gpg_error_from_errno(ENOMEM
);
2131 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2137 if (g_utf8_collate(req
[0], req
[1]) == 0)
2141 * Will not overwrite an existing account.
2143 tmp
= g_strdupv(req
+1);
2146 return gpg_error_from_errno(ENOMEM
);
2148 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2151 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
2155 return EPWMD_ACCOUNT_EXISTS
;
2158 * Whitespace not allowed in account names.
2160 if (contains_whitespace(req
[1]) == TRUE
)
2161 return EPWMD_ATTR_SYNTAX
;
2163 tmp
= g_strdupv(req
);
2166 return gpg_error_from_errno(ENOMEM
);
2168 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2172 return EPWMD_ELEMENT_NOT_FOUND
;
2174 return add_attribute(n
, "name", req
[1]);
2178 * req[0] - attribute
2179 * req[1] - element path
2181 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
2183 struct client_s
*client
= assuan_get_pointer(ctx
);
2189 if (!req
|| !req
[0] || !req
[1])
2190 return EPWMD_COMMAND_SYNTAX
;
2192 if (strchr(req
[1], '\t')) {
2193 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2194 return EPWMD_COMMAND_SYNTAX
;
2197 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2198 return EPWMD_COMMAND_SYNTAX
;
2201 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2207 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2208 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2216 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2217 return EPWMD_ATTR_NOT_FOUND
;
2219 error
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
2229 * req[0] - attribute
2230 * req[1] - element path
2233 static int attribute_set(struct client_s
*client
, gchar
**req
)
2235 gchar
**path
= NULL
;
2239 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2240 return EPWMD_COMMAND_SYNTAX
;
2243 * Reserved attribute names.
2245 if (g_utf8_collate(req
[0], "name") == 0) {
2247 * Only reserved for the account element. Not the rest of the
2250 if (strchr(req
[1], '\t') == NULL
)
2251 return name_attribute(client
, req
+ 1);
2253 else if (g_utf8_collate(req
[0], "target") == 0)
2254 return target_attribute(client
, req
+ 1);
2256 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2258 * The first argument may be only an account.
2260 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2261 return EPWMD_COMMAND_SYNTAX
;
2264 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2270 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2271 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2278 return add_attribute(n
, req
[0], req
[2]);
2287 * req[1] - attribute name or element path if command is LIST
2288 * req[2] - element path
2289 * req[2] - element path or value
2291 static int attr_command(assuan_context_t ctx
, char *line
)
2293 struct client_s
*client
= assuan_get_pointer(ctx
);
2294 gchar
**req
= split_input_line(line
, " ", 4);
2295 gpg_error_t error
= 0;
2297 error
= file_modified(client
);
2300 log_write("%s: %s", client
->filename
? client
->filename
: "",
2301 pwmd_strerror(error
));
2303 return send_error(ctx
, error
);
2306 if (!req
|| !req
[0] || !req
[1]) {
2308 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2311 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2312 error
= attribute_set(client
, req
+1);
2313 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2314 error
= attribute_get(ctx
, req
+1);
2315 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2316 error
= attribute_delete(client
, req
+1);
2317 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2318 error
= attribute_list(ctx
, req
+1);
2320 error
= EPWMD_COMMAND_SYNTAX
;
2323 return send_error(ctx
, error
);
2326 static int iscached_command(assuan_context_t ctx
, char *line
)
2328 gchar
**req
= split_input_line(line
, " ", 0);
2331 if (!req
|| !*req
) {
2333 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2336 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2340 if (cache_iscached(md5file
) == FALSE
) {
2342 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2346 return send_error(ctx
, 0);
2349 gpg_error_t
send_cache_status(assuan_context_t ctx
)
2352 struct client_s
*client
= assuan_get_pointer(ctx
);
2354 CACHE_LOCK(client
->ctx
);
2355 tmp
= print_fmt("%i %i",
2357 (cache_size
/ sizeof(file_cache_t
)) - cache_file_count());
2360 return assuan_write_status(ctx
, "CACHE", tmp
);
2363 static int clearcache_command(assuan_context_t ctx
, char *line
)
2365 struct client_s
*client
= assuan_get_pointer(ctx
);
2366 gchar
**req
= split_input_line(line
, " ", 0);
2371 if (!req
|| !*req
) {
2373 cache_clear(client
->md5file
, 2);
2375 return send_error(ctx
, 0);
2378 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2381 if (cache_clear(md5file
, 1) == FALSE
) {
2383 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2387 return send_error(ctx
, 0);
2390 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2394 gchar
**req
= split_input_line(line
, " ", 0);
2396 struct client_s
*client
= assuan_get_pointer(ctx
);
2398 if (!req
|| !*req
|| !req
[1]) {
2400 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2404 timeout
= strtol(req
[0], &p
, 10);
2406 if (errno
!= 0 || *p
!= 0) {
2408 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2411 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2413 CACHE_LOCK(client
->ctx
);
2415 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2417 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2421 send_cache_status_all();
2422 return send_error(ctx
, 0);
2425 static int dump_command(assuan_context_t ctx
, char *line
)
2429 struct client_s
*client
= assuan_get_pointer(ctx
);
2432 if (disable_list_and_dump
== TRUE
)
2433 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2435 error
= file_modified(client
);
2438 log_write("%s: %s", client
->filename
? client
->filename
: "",
2439 pwmd_strerror(error
));
2440 return send_error(ctx
, error
);
2443 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2444 error
= assuan_send_data(ctx
, xml
, len
);
2446 return send_error(ctx
, error
);
2449 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2451 struct client_s
*client
= assuan_get_pointer(ctx
);
2452 gpg_error_t error
= 0;
2455 if (strcmp(line
, "key") == 0 || strcmp(line
, "key_file") == 0)
2456 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2458 p
= get_key_file_string(client
->filename
? client
->filename
: "default", line
);
2461 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2463 tmp
= expand_homedir(p
);
2466 error
= assuan_send_data(ctx
, p
, strlen(p
));
2468 return send_error(ctx
, error
);
2471 void cleanup_assuan(assuan_context_t ctx
)
2473 struct client_s
*cl
= assuan_get_pointer(ctx
);
2478 static void reset_notify(assuan_context_t ctx
)
2480 struct client_s
*cl
= assuan_get_pointer(ctx
);
2485 static gpg_error_t
parse_client_option(assuan_context_t ctx
, const gchar
*line
)
2487 gchar name
[32] = {0}, value
[256] = {0};
2488 pth_attr_t attr
= pth_attr_of(pth_self());
2490 if (sscanf(line
, " %31[a-zA-Z] = %255c", name
, value
) != 2)
2491 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_SYNTAX
);
2493 if (g_strcasecmp(name
, (gchar
*)"NAME") == 0) {
2494 log_write("OPTION CLIENT %s", line
);
2495 pth_attr_set(attr
, PTH_ATTR_NAME
, value
);
2498 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_UNKNOWN_OPTION
);
2503 static int option_handler(assuan_context_t ctx
, const gchar
*name
,
2506 struct client_s
*client
= assuan_get_pointer(ctx
);
2508 if (!value
|| !*value
)
2509 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_INV_VALUE
);
2511 if (g_strcasecmp(name
, (gchar
*)"ttyname") == 0) {
2512 g_free(client
->pinentry
->ttyname
);
2513 client
->pinentry
->ttyname
= g_strdup(value
);
2515 else if (g_strcasecmp(name
, (gchar
*)"ttytype") == 0) {
2516 g_free(client
->pinentry
->ttytype
);
2517 client
->pinentry
->ttytype
= g_strdup(value
);
2519 else if (g_strcasecmp(name
, (gchar
*)"display") == 0) {
2520 g_free(client
->pinentry
->display
);
2521 client
->pinentry
->display
= g_strdup(value
);
2523 else if (g_strcasecmp(name
, (gchar
*)"path") == 0) {
2524 g_free(client
->pinentry
->path
);
2525 client
->pinentry
->path
= g_strdup(value
);
2527 else if (g_strcasecmp(name
, (gchar
*)"title") == 0) {
2528 g_free(client
->pinentry
->title
);
2529 client
->pinentry
->title
= g_strdup(value
);
2531 else if (g_strcasecmp(name
, (gchar
*)"prompt") == 0) {
2532 g_free(client
->pinentry
->prompt
);
2533 client
->pinentry
->prompt
= g_strdup(value
);
2535 else if (g_strcasecmp(name
, (gchar
*)"desc") == 0) {
2536 g_free(client
->pinentry
->desc
);
2537 client
->pinentry
->desc
= g_strdup(value
);
2539 else if (g_strcasecmp(name
, (gchar
*)"timeout") == 0) {
2541 gint n
= strtol(value
, &p
, 10);
2544 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_INV_VALUE
);
2546 client
->pinentry
->timeout
= n
;
2548 else if (g_strcasecmp(name
, (gchar
*)"pinentry") == 0) {
2550 gint n
= strtol(value
, &p
, 10);
2552 if (*p
|| n
< 0 || n
> 1)
2553 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_INV_VALUE
);
2555 client
->pinentry
->use
= n
== 0 ? FALSE
: TRUE
;
2557 else if (g_strcasecmp(name
, (gchar
*)"client") == 0)
2558 return parse_client_option(ctx
, value
);
2560 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_UNKNOWN_OPTION
);
2562 log_write("OPTION %s=%s", name
, value
);
2566 gpg_error_t
register_commands(assuan_context_t ctx
)
2570 int (*handler
)(assuan_context_t
, char *line
);
2572 { "OPEN", open_command
},
2573 { "SAVE", save_command
},
2574 { "LIST", list_command
},
2575 { "REALPATH", realpath_command
},
2576 { "STORE", store_command
},
2577 { "DELETE", delete_command
},
2578 { "GET", get_command
},
2579 { "ATTR", attr_command
},
2580 { "ISCACHED", iscached_command
},
2581 { "CLEARCACHE", clearcache_command
},
2582 { "CACHETIMEOUT", cachetimeout_command
},
2583 { "GETCONFIG", getconfig_command
},
2584 { "DUMP", dump_command
},
2591 for (i
=0; table
[i
].name
; i
++) {
2592 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2598 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2602 rc
= assuan_register_option_handler(ctx
, option_handler
);
2606 return assuan_register_reset_notify(ctx
, reset_notify
);
2609 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2615 guchar tkey
[gcrykeysize
];
2616 struct file_header_s
{
2618 guchar iv
[gcryblocksize
];
2620 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2621 gcry_cipher_hd_t gh
;
2622 guint iter
= 0, n_iter
= 0;
2624 void *outbuf
= NULL
;
2630 error
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2638 lseek(fd
, 0, SEEK_SET
);
2639 insize
= st
.st_size
- sizeof(struct file_header_s
);
2640 iv
= gcry_malloc(gcryblocksize
);
2644 gcry_cipher_close(gh
);
2646 return gpg_error_from_errno(ENOMEM
);
2649 len
= pth_read(fd
, &file_header
, sizeof(struct file_header_s
));
2651 if (len
!= sizeof(file_header
)) {
2655 gcry_cipher_close(gh
);
2659 return gpg_error_from_errno(errno
);
2662 /* No encryption iterations. This is a plain (gzipped) file. */
2663 if (file_header
.iter
== -1)
2664 insize
= st
.st_size
- sizeof(struct file_header_s
);
2666 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2667 inbuf
= gcry_malloc(insize
);
2671 gcry_cipher_close(gh
);
2674 return gpg_error_from_errno(ENOMEM
);
2677 len
= pth_read(fd
, inbuf
, insize
);
2679 if (len
!= insize
) {
2683 gcry_cipher_close(gh
);
2687 return gpg_error_from_errno(errno
);
2690 if (file_header
.iter
== -1)
2693 memcpy(tkey
, key
, sizeof(tkey
));
2696 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2698 gcry_cipher_close(gh
);
2699 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2702 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2709 if ((error
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2711 gcry_cipher_close(gh
);
2712 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2715 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2721 gcry_cipher_close(gh
);
2726 iter_progress
= get_key_file_integer("default", "iteration_progress");
2728 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2729 error
= assuan_write_status(client
->ctx
, "DECRYPT", "0");
2738 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2746 if ((error
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2748 gcry_cipher_close(gh
);
2749 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2752 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2759 while (iter
< file_header
.iter
) {
2760 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2761 if (!(iter
% iter_progress
)) {
2762 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i",
2763 ++n_iter
* iter_progress
));
2773 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2775 gcry_cipher_close(gh
);
2776 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2779 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2786 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2790 gcry_cipher_close(gh
);
2791 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2794 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2805 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2806 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i", file_header
.iter
));
2818 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
2820 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2822 if (zerror
== Z_MEM_ERROR
) {
2824 return gpg_error_from_errno(ENOMEM
);
2826 else if (zerror
!= Z_DATA_ERROR
) {
2830 gcry_cipher_close(gh
);
2832 return EPWMD_BADKEY
;
2841 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2845 gcry_cipher_close(gh
);
2847 return EPWMD_BADKEY
;
2851 client
->xml
= inbuf
;
2852 client
->len
= insize
;
2855 gcry_cipher_close(gh
);
2863 * This is called after every Assuan command.
2865 void command_finalize(assuan_context_t ctx
, gint error
)
2867 struct client_s
*client
= assuan_get_pointer(ctx
);
2869 unlock_file_mutex(client
);