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 error
= pinentry_fork(ctx
);
583 cleanup_client(client
);
587 client
->pinentry
->cb
= open_command_finalize
;
588 client
->pinentry
->status
= PINENTRY_INIT
;
591 cleanup_client(client
);
592 return send_error(ctx
, EPWMD_KEY
);
596 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
600 return open_command_finalize(ctx
, shakey
, cached
);
603 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
604 gint size
, gpointer
*out
, glong
*outsize
, gint
*error
)
611 gint cmd
= Z_NO_FLUSH
;
616 z
.next_in
= pin
= data
;
617 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
618 z
.avail_out
= zlib_bufsize
;
619 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
622 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
623 *error
= Z_MEM_ERROR
;
627 ret
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
630 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
636 memset(&h
, 0, sizeof(gz_header
));
637 snprintf(buf
, sizeof(buf
), "%i", size
);
638 h
.comment
= (guchar
*)buf
;
639 ret
= deflateSetHeader(&z
, &h
);
642 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
652 ret
= deflate(&z
, cmd
);
659 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
667 z
.next_out
= pout
+ z
.total_out
;
668 z
.avail_out
= zlib_bufsize
;
671 if (!z
.avail_in
&& z
.total_in
< size
) {
672 if (z
.total_in
+ zlib_bufsize
> size
)
673 z
.avail_in
= size
- z
.total_in
;
675 z
.avail_in
= zlib_bufsize
;
678 rc
= assuan_write_status(ctx
, "COMPRESS",
679 print_fmt("%i %i", z
.total_in
, size
));
688 if (z
.total_in
>= size
)
699 } while (ret
!= Z_STREAM_END
);
702 rc
= assuan_write_status(ctx
, "COMPRESS",
703 print_fmt("%i %i", z
.total_in
, size
));
712 *outsize
= z
.total_out
;
717 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
724 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
725 const gchar
*filename
, gpointer data
, size_t insize
,
726 const guchar
*shakey
, guint iter
)
731 guchar tkey
[gcrykeysize
];
735 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
736 gchar tmp
[FILENAME_MAX
];
737 struct file_header_s
{
739 guchar iv
[gcryblocksize
];
744 file_header
.iter
= iter
;
748 if (insize
/ gcryblocksize
) {
749 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
751 if (insize
% gcryblocksize
)
752 len
+= gcryblocksize
;
756 * Resize the existing xml buffer to the block size required by gcrypt
757 * rather than duplicating it and wasting memory.
759 inbuf
= gcry_realloc(data
, len
);
762 return gpg_error_from_errno(ENOMEM
);
765 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
766 memcpy(tkey
, shakey
, sizeof(tkey
));
769 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
771 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
775 file_header
.iter
= iter
;
778 iter_progress
= get_key_file_integer("default", "iteration_progress");
780 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
781 error
= assuan_write_status(client
->ctx
, "ENCRYPT", "0");
789 while (xiter
< file_header
.iter
) {
790 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
791 if (!(xiter
% iter_progress
)) {
792 error
= assuan_write_status(client
->ctx
, "ENCRYPT", print_fmt("%i",
793 ++n_iter
* iter_progress
));
802 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
803 sizeof(file_header
.iv
)))) {
805 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
809 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
812 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
820 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
821 sizeof(file_header
.iv
)))) {
823 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
827 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
829 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
833 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
835 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
839 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
840 error
= assuan_write_status(client
->ctx
, "ENCRYPT",
841 print_fmt("%i", file_header
.iter
));
851 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
853 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
856 p
= strrchr(tmp
, '/');
858 log_write("%s: %s", p
, strerror(errno
));
859 return gpg_error_from_errno(error
);
864 * xml_import() from command line.
868 len
= pth_write(fd
, &file_header
, sizeof(struct file_header_s
));
870 if (len
!= sizeof(file_header
)) {
877 return gpg_error_from_errno(len
);
880 len
= pth_write(fd
, inbuf
, insize
);
889 return gpg_error_from_errno(len
);
892 if (fsync(fd
) == -1) {
896 return gpg_error_from_errno(len
);
905 if (stat(filename
, &st
) == 0)
906 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
908 if (rename(tmp
, filename
) == -1) {
911 return gpg_error_from_errno(len
);
915 chmod(filename
, mode
);
922 static gpg_error_t
save_command_finalize(assuan_context_t ctx
,
923 guchar shakey
[], gboolean cached
)
925 struct client_s
*client
= assuan_get_pointer(ctx
);
937 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
940 iter
= get_key_file_integer(client
->filename
, "compression_level");
945 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
946 memset(shakey
, 0, sizeof(shakey
));
949 if (zerror
== Z_MEM_ERROR
) {
950 return send_syserror(ctx
, ENOMEM
);
953 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
961 iter
= get_key_file_integer(client
->filename
, "iterations");
962 error
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
965 memset(shakey
, 0, sizeof(shakey
));
966 return send_error(ctx
, error
);
969 stat(client
->filename
, &st
);
970 client
->mtime
= st
.st_mtime
;
971 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
972 CACHE_LOCK(client
->ctx
);
975 memset(shakey
, 0, sizeof(shakey
));
976 cache_reset_timeout(client
->md5file
, timeout
);
979 if (client
->new == TRUE
)
980 send_cache_status_all();
983 return send_error(ctx
, 0);
986 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
987 memset(shakey
, 0, sizeof(shakey
));
988 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
990 return send_error(ctx
, EPWMD_MAX_SLOTS
);
994 memset(shakey
, 0, sizeof(shakey
));
995 cache_reset_timeout(client
->md5file
, timeout
);
997 send_cache_status_all();
998 return send_error(ctx
, 0);
1001 static int save_command(assuan_context_t ctx
, char *line
)
1003 gboolean cached
= FALSE
;
1004 guchar shakey
[gcrykeysize
];
1006 struct client_s
*client
= assuan_get_pointer(ctx
);
1009 error
= file_modified(client
);
1012 log_write("%s: %s", client
->filename
? client
->filename
: "",
1013 pwmd_strerror(error
));
1014 return send_error(ctx
, error
);
1017 error
= lock_file_mutex(client
);
1020 return send_error(ctx
, error
);
1022 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1023 return send_syserror(ctx
, errno
);
1025 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
1026 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
1027 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
1030 if (!line
|| !*line
) {
1033 if (cache_get_key(client
->md5file
, shakey
) == FALSE
) {
1035 #ifdef WITH_PINENTRY
1036 if (client
->pinentry
->use
== FALSE
)
1037 return send_error(ctx
, EPWMD_KEY
);
1039 error
= pinentry_fork(ctx
);
1042 return send_error(ctx
, error
);
1044 client
->pinentry
->cb
= save_command_finalize
;
1045 client
->pinentry
->status
= PINENTRY_INIT
;
1048 return send_error(ctx
, EPWMD_KEY
);
1056 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
1057 memset(line
, 0, strlen(line
));
1060 return save_command_finalize(ctx
, shakey
, cached
);
1063 static gboolean
contains_whitespace(const gchar
*str
)
1065 const gchar
*p
= str
;
1069 len
= g_utf8_strlen(p
++, -1) -1;
1072 c
= g_utf8_get_char(p
++);
1074 if (g_unichar_isspace(c
))
1081 static int delete_command(assuan_context_t ctx
, char *line
)
1083 struct client_s
*client
= assuan_get_pointer(ctx
);
1088 error
= file_modified(client
);
1091 log_write("%s: %s", client
->filename
? client
->filename
: "",
1092 pwmd_strerror(error
));
1093 return send_error(ctx
, error
);
1096 if (strchr(line
, '\t'))
1097 req
= split_input_line(line
, "\t", -1);
1099 req
= split_input_line(line
, " ", -1);
1102 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1104 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1108 return send_error(ctx
, error
);
1112 * No sub-node defined. Remove the entire node (account).
1121 return send_error(ctx
, 0);
1124 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1128 return send_error(ctx
, error
);
1135 return send_error(ctx
, 0);
1139 * Don't return with assuan_process_done() here. This has been called from
1140 * assuan_process_next() and the command should be finished in
1143 static int store_command_finalize(gpointer data
, gint rc
, guchar
*line
,
1146 assuan_context_t ctx
= data
;
1147 struct client_s
*client
= assuan_get_pointer(ctx
);
1149 guchar
*result
= line
;
1151 gpg_error_t error
= file_modified(client
);
1163 req
= split_input_line((gchar
*)result
, "\t", 0);
1178 return EPWMD_COMMAND_SYNTAX
;
1180 if (valid_xml_element((xmlChar
*)*req
) == FALSE
) {
1182 return EPWMD_INVALID_ELEMENT
;
1185 if (valid_element_path(req
+1, TRUE
) == FALSE
) {
1187 return EPWMD_INVALID_ELEMENT
;
1191 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1193 if (error
&& error
== EPWMD_ELEMENT_NOT_FOUND
) {
1194 error
= new_account(client
->doc
, *req
);
1211 create_elements_cb(n
, req
+1, &error
, NULL
);
1213 find_elements(client
->doc
, n
->children
, req
+1, &error
,
1214 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1218 client
->inquire_status
= INQUIRE_DONE
;
1222 static int store_command(assuan_context_t ctx
, char *line
)
1224 struct client_s
*client
= assuan_get_pointer(ctx
);
1225 gpg_error_t error
= file_modified(client
);
1228 log_write("%s: %s", client
->filename
? client
->filename
: "",
1229 pwmd_strerror(error
));
1230 return send_error(ctx
, error
);
1233 error
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1236 return send_error(ctx
, error
);
1238 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1239 client
->inquire_status
= INQUIRE_BUSY
;
1243 static int get_command(assuan_context_t ctx
, char *line
)
1245 struct client_s
*client
= assuan_get_pointer(ctx
);
1250 error
= file_modified(client
);
1253 log_write("%s: %s", client
->filename
? client
->filename
: "",
1254 pwmd_strerror(error
));
1255 return send_error(ctx
, error
);
1258 req
= split_input_line(line
, "\t", -1);
1260 if (!req
|| !*req
) {
1262 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1265 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1269 return send_error(ctx
, error
);
1273 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1278 return send_error(ctx
, error
);
1280 if (!n
|| !n
->children
)
1281 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1285 if (!n
|| !n
->content
|| !*n
->content
)
1286 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1288 error
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1289 return send_error(ctx
, error
);
1292 static gchar
*element_path_to_req(const gchar
*account
, xmlChar
*path
)
1301 for (n
= 0; *p
&& n
< 3; p
++) {
1306 if (strstr((gchar
*)p
, "text()") != NULL
)
1307 p
[xmlStrlen(p
) - 7] = 0;
1309 for (n
= 0; p
[n
]; n
++) {
1314 buf
= g_strdup_printf("%s\t%s", account
, p
);
1315 p
= (xmlChar
*)buf
+ strlen(buf
) - 1;
1323 gboolean
strv_printf(gchar
***array
, const gchar
*fmt
, ...)
1328 gint len
= *array
? g_strv_length(*array
) : 0;
1334 if ((a
= g_realloc(*array
, (len
+ 2) * sizeof(gchar
*))) == NULL
)
1338 ret
= g_vasprintf(&buf
, fmt
, ap
);
1354 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**req
,
1355 gpg_error_t
*error
, void *data
)
1357 struct realpath_s
*rp
= data
;
1360 g_free(rp
->account
);
1362 rp
->account
= g_strdup(req
[0]);
1365 *error
= gpg_error_from_errno(ENOMEM
);
1372 static int realpath_command(assuan_context_t ctx
, char *line
)
1375 struct client_s
*client
= assuan_get_pointer(ctx
);
1381 struct realpath_s
*rp
;
1384 error
= file_modified(client
);
1387 log_write("%s: %s", client
->filename
? client
->filename
: "",
1388 pwmd_strerror(error
));
1389 return send_error(ctx
, error
);
1392 if (strchr(line
, '\t') != NULL
) {
1393 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1394 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1397 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1398 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1401 n
= find_account(client
->doc
, &req
, &error
, NULL
, 0);
1405 return send_error(ctx
, error
);
1408 rp
= g_malloc(sizeof(struct realpath_s
));
1412 return send_syserror(ctx
, ENOMEM
);
1415 rp
->account
= g_strdup(req
[0]);
1419 return send_syserror(ctx
, ENOMEM
);
1423 n
= find_elements(client
->doc
, n
->children
, req
+1, &error
,
1424 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, rp
);
1427 g_free(rp
->account
);
1430 return send_error(ctx
, error
);
1434 p
= xmlGetNodePath(n
);
1435 result
= element_path_to_req(rp
->account
, p
);
1439 g_free(rp
->account
);
1443 return send_syserror(ctx
, ENOMEM
);
1446 string
= g_string_new(result
);
1448 g_free(rp
->account
);
1455 for (t
= string
->str
+ i
; *t
; t
++, i
++) {
1456 if ((!i
&& *t
!= '!') || *t
== '\t') {
1457 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1462 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1463 g_string_free(string
, TRUE
);
1464 return send_error(ctx
, error
);
1467 struct list_element_s
{
1472 static gboolean
append_to_element_list(struct list_element_s
*elements
)
1479 if (!elements
|| !elements
->elements
)
1482 tmp
= g_strjoinv("\t", elements
->elements
);
1487 g_strfreev(elements
->elements
);
1488 elements
->elements
= NULL
;
1489 total
= g_slist_length(elements
->list
);
1490 a
= g_utf8_collate_key(tmp
, -1);
1498 * Removes duplicate element paths from the list. This is needed when
1499 * appending an element tree from list_command(). The glib docs recommend
1500 * using g_utf8_collate_key() for a large number of strings.
1502 for (i
= 0; i
< total
; i
++) {
1503 gchar
*p
= g_slist_nth_data(elements
->list
, i
);
1504 gchar
*b
= g_utf8_collate_key(p
, -1);
1512 if (strcmp(a
, b
) == 0) {
1523 list
= g_slist_append(elements
->list
, tmp
);
1528 elements
->list
= list
;
1532 static gpg_error_t
do_list_recurse(xmlDocPtr doc
, xmlNodePtr node
,
1533 struct list_element_s
*elements
, gchar
*prefix
)
1538 if (append_to_element_list(elements
) == FALSE
)
1539 return gpg_error_from_errno(ENOMEM
);
1541 for (n
= node
; n
; n
= n
->next
) {
1542 if (n
->type
== XML_ELEMENT_NODE
) {
1543 xmlChar
*content
= node_has_attribute(n
, (xmlChar
*)"target");
1547 if (strv_printf(&elements
->elements
, "%s\t%s", prefix
, n
->name
) == FALSE
)
1548 return gpg_error_from_errno(ENOMEM
);
1550 if (append_to_element_list(elements
) == FALSE
)
1551 return gpg_error_from_errno(ENOMEM
);
1554 tmp
= g_strdup_printf("%s\t!%s", prefix
, n
->name
);
1557 return gpg_error_from_errno(ENOMEM
);
1559 if (strv_printf(&elements
->elements
, "%s", tmp
) == FALSE
) {
1561 return gpg_error_from_errno(ENOMEM
);
1565 error
= do_list_recurse(doc
, n
->children
, elements
, tmp
);
1568 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1574 if (append_to_element_list(elements
) == FALSE
)
1575 return gpg_error_from_errno(ENOMEM
);
1582 static gpg_error_t
do_list_command(assuan_context_t ctx
, xmlDocPtr doc
,
1583 struct list_element_s
*elements
, char *line
)
1585 gchar
*prefix
= NULL
, *account
;
1586 gchar
**req
= NULL
, **oreq
= NULL
, *tmp
;
1588 gboolean account_is_literal
, account_has_target
= FALSE
;
1593 if ((req
= split_input_line(line
, "\t", 0)) == NULL
) {
1594 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1595 return EPWMD_COMMAND_SYNTAX
;
1598 prefix
= g_strdup(*req
);
1602 return gpg_error_from_errno(ENOMEM
);
1605 account
= g_strdup(*req
);
1610 return gpg_error_from_errno(ENOMEM
);
1613 oreq
= g_strdupv(req
);
1619 return gpg_error_from_errno(ENOMEM
);
1624 account_has_target
= FALSE
;
1625 account_is_literal
= is_literal_element_str(prefix
);
1626 n
= find_account(doc
, &p
, &error
, &account_has_target
, 0);
1636 if (!which
&& account_is_literal
== FALSE
&& account_has_target
== FALSE
) {
1637 tmp
= g_strdup_printf("!%s", prefix
);
1640 error
= gpg_error_from_errno(ENOMEM
);
1651 n
= find_elements(doc
, n
->children
, p
+1, &error
, NULL
, NULL
, NULL
,
1657 tmp
= g_strjoinv("\t", p
+1);
1659 error
= gpg_error_from_errno(ENOMEM
);
1663 t
= g_strdup_printf("%s\t%s", prefix
, tmp
);
1665 error
= gpg_error_from_errno(ENOMEM
);
1674 if (strv_printf(&elements
->elements
, "%s", prefix
) == FALSE
) {
1675 error
= gpg_error_from_errno(ENOMEM
);
1679 if (node_has_child_element(n
->children
) == FALSE
) {
1680 if (append_to_element_list(elements
) == FALSE
) {
1681 error
= gpg_error_from_errno(ENOMEM
);
1686 error
= do_list_recurse(doc
, n
->children
, elements
, prefix
);
1691 if (!which
++ && !*(p
+1) && account_is_literal
== FALSE
&& account_has_target
== TRUE
) {
1693 *oreq
= g_strdup_printf("!%s", account
);
1696 error
= gpg_error_from_errno(ENOMEM
);
1702 prefix
= g_strdup(*oreq
);
1705 error
= gpg_error_from_errno(ENOMEM
);
1724 * This could be faster especially when finding "target" attributes.
1726 static int list_command(assuan_context_t ctx
, char *line
)
1728 struct client_s
*client
= assuan_get_pointer(ctx
);
1730 struct list_element_s
*elements
= NULL
;
1735 if (disable_list_and_dump
== TRUE
)
1736 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1738 error
= file_modified(client
);
1741 log_write("%s: %s", client
->filename
, pwmd_strerror(error
));
1742 return send_error(ctx
, error
);
1748 error
= list_accounts(client
->doc
, &str
);
1751 return send_error(ctx
, error
);
1753 error
= assuan_send_data(ctx
, str
->str
, str
->len
);
1754 g_string_free(str
, TRUE
);
1755 return send_error(ctx
, error
);
1758 elements
= g_malloc0(sizeof(struct list_element_s
));
1761 error
= gpg_error_from_errno(ENOMEM
);
1765 error
= do_list_command(ctx
, client
->doc
, elements
, line
);
1771 total
= g_slist_length(elements
->list
);
1774 error
= EPWMD_EMPTY_ELEMENT
;
1779 * Find element paths with a target and append those element trees to
1782 for (i
= 0; i
< total
; i
++) {
1785 tmp
= g_slist_nth_data(elements
->list
, i
);
1786 req
= split_input_line(tmp
, "\t", 0);
1789 if (g_str_has_prefix(tmp
, "!") == TRUE
) {
1797 for (p
= req
; *p
; p
++) {
1798 if (g_str_has_prefix(*p
, "!") == FALSE
)
1809 error
= do_list_command(ctx
, client
->doc
, elements
, tmp
);
1811 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
1814 total
= g_slist_length(elements
->list
);
1818 string
= g_string_new(NULL
);
1820 for (i
= 0; i
< total
; i
++) {
1821 tmp
= g_slist_nth_data(elements
->list
, i
);
1822 g_string_append_printf(string
, "%s\n", tmp
);
1826 string
= g_string_truncate(string
, string
->len
- 1);
1827 error
= assuan_send_data(ctx
, string
->str
, string
->len
);
1828 g_string_free(string
, TRUE
);
1833 g_slist_free(elements
->list
);
1835 if (elements
->elements
)
1836 g_strfreev(elements
->elements
);
1841 return send_error(ctx
, error
);
1844 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1849 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1850 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1853 return EPWMD_LIBXML_ERROR
;
1856 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1862 * req[0] - element path
1864 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1866 struct client_s
*client
= assuan_get_pointer(ctx
);
1867 gchar
**attrlist
= NULL
;
1869 gchar
**path
= NULL
;
1875 if (!req
|| !req
[0])
1876 return EPWMD_COMMAND_SYNTAX
;
1878 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1880 * The first argument may be only an account.
1882 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1883 return EPWMD_COMMAND_SYNTAX
;
1886 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1894 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1895 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1905 for (a
= n
->properties
; a
; a
= a
->next
) {
1908 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1910 g_strfreev(attrlist
);
1913 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(errno
));
1914 return gpg_error_from_errno(error
);
1919 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1922 g_strfreev(attrlist
);
1923 return gpg_error_from_errno(ENOMEM
);
1926 attrlist
[++i
] = NULL
;
1930 return EPWMD_EMPTY_ELEMENT
;
1932 line
= g_strjoinv("\n", attrlist
);
1935 g_strfreev(attrlist
);
1936 return gpg_error_from_errno(ENOMEM
);
1939 error
= assuan_send_data(ctx
, line
, strlen(line
));
1941 g_strfreev(attrlist
);
1946 * req[0] - attribute
1947 * req[1] - element path
1949 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1953 gchar
**path
= NULL
;
1956 if (!req
|| !req
[0] || !req
[1])
1957 return EPWMD_COMMAND_SYNTAX
;
1959 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1961 * The first argument may be only an account.
1963 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1964 return EPWMD_COMMAND_SYNTAX
;
1968 * Don't remove the "name" attribute for the account element. To remove an
1969 * account use DELETE <account>.
1971 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1972 error
= EPWMD_ATTR_SYNTAX
;
1976 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
1982 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
1983 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1991 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1992 return EPWMD_ATTR_NOT_FOUND
;
1994 if (xmlRemoveProp(a
) == -1)
1995 return EPWMD_LIBXML_ERROR
;
2005 * Creates a "target" attribute. When other commands encounter an element with
2006 * this attribute, the element path is modified to the target value. If the
2007 * source element path doesn't exist when using 'ATTR SET target', it is
2008 * created, but the destination element path must exist.
2010 * req[0] - source element path
2011 * req[1] - destination element path
2013 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
2015 gchar
**src
, **dst
, *line
= NULL
;
2019 if (!req
|| !req
[0] || !req
[1])
2020 return EPWMD_COMMAND_SYNTAX
;
2022 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2024 * The first argument may be only an account.
2026 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
2027 return EPWMD_COMMAND_SYNTAX
;
2030 if (valid_element_path(src
, FALSE
) == FALSE
) {
2032 return EPWMD_INVALID_ELEMENT
;
2035 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2037 * The first argument may be only an account.
2039 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
2040 error
= EPWMD_COMMAND_SYNTAX
;
2045 n
= find_account(client
->doc
, &dst
, &error
, NULL
, 0);
2048 * Make sure the destination element path exists.
2054 n
= find_elements(client
->doc
, n
->children
, dst
+1, &error
,
2055 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2062 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
2065 if (error
== EPWMD_ELEMENT_NOT_FOUND
) {
2066 error
= new_account(client
->doc
, src
[0]);
2079 n
= create_target_elements_cb(n
, src
+1, &error
, NULL
);
2081 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
2082 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
2088 * Reset the position of the element tree now that the elements
2089 * have been created.
2091 n
= find_account(client
->doc
, &src
, &error
, NULL
, 0);
2096 n
= find_elements(client
->doc
, n
->children
, src
+1, &error
,
2097 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2103 line
= g_strjoinv("\t", dst
);
2104 error
= add_attribute(n
, "target", line
);
2114 * req[0] - account name
2117 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
2123 tmp
= g_strdupv(req
);
2126 return gpg_error_from_errno(ENOMEM
);
2128 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2134 if (g_utf8_collate(req
[0], req
[1]) == 0)
2138 * Will not overwrite an existing account.
2140 tmp
= g_strdupv(req
+1);
2143 return gpg_error_from_errno(ENOMEM
);
2145 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2148 if (error
&& error
!= EPWMD_ELEMENT_NOT_FOUND
)
2152 return EPWMD_ACCOUNT_EXISTS
;
2155 * Whitespace not allowed in account names.
2157 if (contains_whitespace(req
[1]) == TRUE
)
2158 return EPWMD_ATTR_SYNTAX
;
2160 tmp
= g_strdupv(req
);
2163 return gpg_error_from_errno(ENOMEM
);
2165 n
= find_account(client
->doc
, &tmp
, &error
, NULL
, 0);
2169 return EPWMD_ELEMENT_NOT_FOUND
;
2171 return add_attribute(n
, "name", req
[1]);
2175 * req[0] - attribute
2176 * req[1] - element path
2178 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
2180 struct client_s
*client
= assuan_get_pointer(ctx
);
2186 if (!req
|| !req
[0] || !req
[1])
2187 return EPWMD_COMMAND_SYNTAX
;
2189 if (strchr(req
[1], '\t')) {
2190 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
2191 return EPWMD_COMMAND_SYNTAX
;
2194 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2195 return EPWMD_COMMAND_SYNTAX
;
2198 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2204 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2205 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2213 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
2214 return EPWMD_ATTR_NOT_FOUND
;
2216 error
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
2226 * req[0] - attribute
2227 * req[1] - element path
2230 static int attribute_set(struct client_s
*client
, gchar
**req
)
2232 gchar
**path
= NULL
;
2236 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2237 return EPWMD_COMMAND_SYNTAX
;
2240 * Reserved attribute names.
2242 if (g_utf8_collate(req
[0], "name") == 0) {
2244 * Only reserved for the account element. Not the rest of the
2247 if (strchr(req
[1], '\t') == NULL
)
2248 return name_attribute(client
, req
+ 1);
2250 else if (g_utf8_collate(req
[0], "target") == 0)
2251 return target_attribute(client
, req
+ 1);
2253 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2255 * The first argument may be only an account.
2257 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2258 return EPWMD_COMMAND_SYNTAX
;
2261 n
= find_account(client
->doc
, &path
, &error
, NULL
, 0);
2267 n
= find_elements(client
->doc
, n
->children
, path
+1, &error
,
2268 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2275 return add_attribute(n
, req
[0], req
[2]);
2284 * req[1] - attribute name or element path if command is LIST
2285 * req[2] - element path
2286 * req[2] - element path or value
2288 static int attr_command(assuan_context_t ctx
, char *line
)
2290 struct client_s
*client
= assuan_get_pointer(ctx
);
2291 gchar
**req
= split_input_line(line
, " ", 4);
2292 gpg_error_t error
= 0;
2294 error
= file_modified(client
);
2297 log_write("%s: %s", client
->filename
? client
->filename
: "",
2298 pwmd_strerror(error
));
2300 return send_error(ctx
, error
);
2303 if (!req
|| !req
[0] || !req
[1]) {
2305 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2308 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2309 error
= attribute_set(client
, req
+1);
2310 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2311 error
= attribute_get(ctx
, req
+1);
2312 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2313 error
= attribute_delete(client
, req
+1);
2314 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2315 error
= attribute_list(ctx
, req
+1);
2317 error
= EPWMD_COMMAND_SYNTAX
;
2320 return send_error(ctx
, error
);
2323 static int iscached_command(assuan_context_t ctx
, char *line
)
2325 gchar
**req
= split_input_line(line
, " ", 0);
2328 if (!req
|| !*req
) {
2330 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2333 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2337 if (cache_iscached(md5file
) == FALSE
) {
2339 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2343 return send_error(ctx
, 0);
2346 gpg_error_t
send_cache_status(assuan_context_t ctx
)
2349 struct client_s
*client
= assuan_get_pointer(ctx
);
2351 CACHE_LOCK(client
->ctx
);
2352 tmp
= print_fmt("%i %i",
2354 (cache_size
/ sizeof(file_cache_t
)) - cache_file_count());
2357 return assuan_write_status(ctx
, "CACHE", tmp
);
2360 static int clearcache_command(assuan_context_t ctx
, char *line
)
2362 struct client_s
*client
= assuan_get_pointer(ctx
);
2363 gchar
**req
= split_input_line(line
, " ", 0);
2368 if (!req
|| !*req
) {
2370 cache_clear(client
->md5file
, 2);
2372 return send_error(ctx
, 0);
2375 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2378 if (cache_clear(md5file
, 1) == FALSE
) {
2380 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2384 return send_error(ctx
, 0);
2387 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2391 gchar
**req
= split_input_line(line
, " ", 0);
2393 struct client_s
*client
= assuan_get_pointer(ctx
);
2395 if (!req
|| !*req
|| !req
[1]) {
2397 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2401 timeout
= strtol(req
[0], &p
, 10);
2403 if (errno
!= 0 || *p
!= 0) {
2405 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2408 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2410 CACHE_LOCK(client
->ctx
);
2412 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2414 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2418 send_cache_status_all();
2419 return send_error(ctx
, 0);
2422 static int dump_command(assuan_context_t ctx
, char *line
)
2426 struct client_s
*client
= assuan_get_pointer(ctx
);
2429 if (disable_list_and_dump
== TRUE
)
2430 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2432 error
= file_modified(client
);
2435 log_write("%s: %s", client
->filename
? client
->filename
: "",
2436 pwmd_strerror(error
));
2437 return send_error(ctx
, error
);
2440 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2441 error
= assuan_send_data(ctx
, xml
, len
);
2443 return send_error(ctx
, error
);
2446 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2448 struct client_s
*client
= assuan_get_pointer(ctx
);
2449 gpg_error_t error
= 0;
2452 if (strcmp(line
, "key") == 0 || strcmp(line
, "key_file") == 0)
2453 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2455 p
= get_key_file_string(client
->filename
? client
->filename
: "default", line
);
2458 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2460 tmp
= expand_homedir(p
);
2463 error
= assuan_send_data(ctx
, p
, strlen(p
));
2465 return send_error(ctx
, error
);
2468 void cleanup_assuan(assuan_context_t ctx
)
2470 struct client_s
*cl
= assuan_get_pointer(ctx
);
2475 static void reset_notify(assuan_context_t ctx
)
2477 struct client_s
*cl
= assuan_get_pointer(ctx
);
2482 static int option_handler(assuan_context_t ctx
, const gchar
*name
,
2485 struct client_s
*client
= assuan_get_pointer(ctx
);
2487 if (!value
|| !*value
)
2488 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_INV_VALUE
);
2490 if (g_strcasecmp(name
, (gchar
*)"ttyname") == 0) {
2491 if (client
->pinentry
->ttyname
)
2492 xfree(client
->pinentry
->ttyname
);
2493 client
->pinentry
->ttyname
= xstrdup(value
);
2495 else if (g_strcasecmp(name
, (gchar
*)"ttytype") == 0) {
2496 if (client
->pinentry
->ttytype
)
2497 xfree(client
->pinentry
->ttytype
);
2498 client
->pinentry
->ttytype
= xstrdup(value
);
2500 else if (g_strcasecmp(name
, (gchar
*)"display") == 0) {
2501 if (client
->pinentry
->display
)
2502 xfree(client
->pinentry
->display
);
2503 client
->pinentry
->display
= xstrdup(value
);
2505 else if (g_strcasecmp(name
, (gchar
*)"path") == 0) {
2506 if (client
->pinentry
->path
)
2507 xfree(client
->pinentry
->path
);
2508 client
->pinentry
->path
= xstrdup(value
);
2510 else if (g_strcasecmp(name
, (gchar
*)"title") == 0) {
2511 if (client
->pinentry
->title
)
2512 xfree(client
->pinentry
->title
);
2513 client
->pinentry
->title
= xstrdup(value
);
2515 else if (g_strcasecmp(name
, (gchar
*)"prompt") == 0) {
2516 if (client
->pinentry
->prompt
)
2517 xfree(client
->pinentry
->prompt
);
2518 client
->pinentry
->prompt
= xstrdup(value
);
2520 else if (g_strcasecmp(name
, (gchar
*)"desc") == 0) {
2521 if (client
->pinentry
->desc
)
2522 xfree(client
->pinentry
->desc
);
2523 client
->pinentry
->desc
= xstrdup(value
);
2525 else if (g_strcasecmp(name
, (gchar
*)"timeout") == 0) {
2527 gint n
= strtol(value
, &p
, 10);
2530 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_INV_VALUE
);
2532 client
->pinentry
->timeout
= n
;
2534 else if (g_strcasecmp(name
, (gchar
*)"pinentry") == 0) {
2536 gint n
= strtol(value
, &p
, 10);
2538 if (*p
|| n
< 0 || n
> 1)
2539 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_INV_VALUE
);
2541 client
->pinentry
->use
= n
== 0 ? FALSE
: TRUE
;
2544 return gpg_err_make(GPG_ERR_SOURCE_USER_1
, GPG_ERR_UNKNOWN_OPTION
);
2549 gpg_error_t
register_commands(assuan_context_t ctx
)
2553 int (*handler
)(assuan_context_t
, char *line
);
2555 { "OPEN", open_command
},
2556 { "SAVE", save_command
},
2557 { "LIST", list_command
},
2558 { "REALPATH", realpath_command
},
2559 { "STORE", store_command
},
2560 { "DELETE", delete_command
},
2561 { "GET", get_command
},
2562 { "ATTR", attr_command
},
2563 { "ISCACHED", iscached_command
},
2564 { "CLEARCACHE", clearcache_command
},
2565 { "CACHETIMEOUT", cachetimeout_command
},
2566 { "GETCONFIG", getconfig_command
},
2567 { "DUMP", dump_command
},
2574 for (i
=0; table
[i
].name
; i
++) {
2575 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2581 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2585 rc
= assuan_register_option_handler(ctx
, option_handler
);
2589 return assuan_register_reset_notify(ctx
, reset_notify
);
2592 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2598 guchar tkey
[gcrykeysize
];
2599 struct file_header_s
{
2601 guchar iv
[gcryblocksize
];
2603 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2604 gcry_cipher_hd_t gh
;
2605 guint iter
= 0, n_iter
= 0;
2607 void *outbuf
= NULL
;
2613 error
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2621 lseek(fd
, 0, SEEK_SET
);
2622 insize
= st
.st_size
- sizeof(struct file_header_s
);
2623 iv
= gcry_malloc(gcryblocksize
);
2627 gcry_cipher_close(gh
);
2629 return gpg_error_from_errno(ENOMEM
);
2632 len
= pth_read(fd
, &file_header
, sizeof(struct file_header_s
));
2634 if (len
!= sizeof(file_header
)) {
2638 gcry_cipher_close(gh
);
2642 return gpg_error_from_errno(errno
);
2645 /* No encryption iterations. This is a plain (gzipped) file. */
2646 if (file_header
.iter
== -1)
2647 insize
= st
.st_size
- sizeof(struct file_header_s
);
2649 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2650 inbuf
= gcry_malloc(insize
);
2654 gcry_cipher_close(gh
);
2657 return gpg_error_from_errno(ENOMEM
);
2660 len
= pth_read(fd
, inbuf
, insize
);
2662 if (len
!= insize
) {
2666 gcry_cipher_close(gh
);
2670 return gpg_error_from_errno(errno
);
2673 if (file_header
.iter
== -1)
2676 memcpy(tkey
, key
, sizeof(tkey
));
2679 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2681 gcry_cipher_close(gh
);
2682 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2685 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2692 if ((error
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2694 gcry_cipher_close(gh
);
2695 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2698 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2704 gcry_cipher_close(gh
);
2709 iter_progress
= get_key_file_integer("default", "iteration_progress");
2711 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2712 error
= assuan_write_status(client
->ctx
, "DECRYPT", "0");
2721 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2729 if ((error
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2731 gcry_cipher_close(gh
);
2732 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2735 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2742 while (iter
< file_header
.iter
) {
2743 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2744 if (!(iter
% iter_progress
)) {
2745 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i",
2746 ++n_iter
* iter_progress
));
2756 if ((error
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2758 gcry_cipher_close(gh
);
2759 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2762 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2769 error
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2773 gcry_cipher_close(gh
);
2774 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2777 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(error
));
2788 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2789 error
= assuan_write_status(ctx
, "DECRYPT", print_fmt("%i", file_header
.iter
));
2801 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zerror
) == FALSE
) {
2803 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2805 if (zerror
== Z_MEM_ERROR
) {
2807 return gpg_error_from_errno(ENOMEM
);
2809 else if (zerror
!= Z_DATA_ERROR
) {
2813 gcry_cipher_close(gh
);
2815 return EPWMD_BADKEY
;
2824 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2828 gcry_cipher_close(gh
);
2830 return EPWMD_BADKEY
;
2834 client
->xml
= inbuf
;
2835 client
->len
= insize
;
2838 gcry_cipher_close(gh
);
2846 * This is called after every Assuan command.
2848 void command_finalize(assuan_context_t ctx
, gint error
)
2850 struct client_s
*client
= assuan_get_pointer(ctx
);
2852 unlock_file_mutex(client
);