1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
3 Copyright (C) 2006-2008 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>
47 #include "pwmd_error.h"
52 static void *z_alloc(void *data
, unsigned items
, unsigned size
)
55 return gcry_calloc(items
, size
);
57 return calloc(items
, size
);
61 static void z_free(void *data
, void *p
)
70 static gpg_error_t
file_modified(struct client_s
*client
)
75 if (client
->state
!= STATE_OPEN
)
78 rc
= lock_file_mutex(client
);
83 if (stat(client
->filename
, &st
) == 0 && client
->mtime
) {
84 if (client
->mtime
!= st
.st_mtime
)
85 return EPWMD_FILE_MODIFIED
;
91 static gboolean
encrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
92 void *inbuf
, gsize insize
)
96 if ((rc
= gcry_cipher_encrypt(gh
, outbuf
, outsize
, inbuf
, insize
))) {
97 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
104 static gpg_error_t
decrypt_xml(gcry_cipher_hd_t gh
, void *outbuf
, gsize outsize
,
105 void *inbuf
, gsize insize
)
109 if ((rc
= gcry_cipher_decrypt(gh
, outbuf
, outsize
, inbuf
, insize
)))
110 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
115 static gpg_error_t
parse_xml(assuan_context_t ctx
)
117 struct client_s
*client
= assuan_get_pointer(ctx
);
119 client
->doc
= xmlReadMemory(client
->xml
, client
->len
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
122 return EPWMD_LIBXML_ERROR
;
127 void unlock_file_mutex(struct client_s
*client
)
132 if (client
->has_lock
== FALSE
|| client
->pinentry
->status
!= PINENTRY_NONE
)
134 if (client
->has_lock
== FALSE
)
138 CACHE_LOCK(client
->ctx
);
140 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
146 pth_mutex_release(m
);
147 client
->has_lock
= client
->is_lock_cmd
= FALSE
;
150 gpg_error_t
lock_file_mutex(struct client_s
*client
)
154 if (client
->has_lock
== TRUE
)
157 CACHE_LOCK(client
->ctx
);
159 if (cache_get_mutex(client
->md5file
, &m
) == FALSE
) {
166 if (pth_mutex_acquire(m
, TRUE
, NULL
) == FALSE
) {
167 if (errno
== EBUSY
) {
170 * If a client disconnects unexpectedly while waiting for a
171 * lock, this lets the thread terminate because send_status()
172 * will return an error. We can't use an PTH_EVENT_FUNC here
173 * because the thread that would call the callback uses it's
174 * own stack space which messes up access to the client data
175 * (if I understand it correctly).
177 while (pth_mutex_acquire(m
, TRUE
, NULL
) == FALSE
) {
178 gpg_error_t rc
= send_status(client
->ctx
, STATUS_LOCKED
);
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 void cleanup_client(struct client_s
*client
)
217 assuan_context_t ctx
= client
->ctx
;
219 struct pinentry_s
*pin
= client
->pinentry
;
222 unlock_file_mutex(client
);
223 CACHE_LOCK(client
->ctx
);
224 cache_decr_refcount(client
->md5file
);
227 * This may be a new file so don't use a cache slot. save_command() will
228 * set this to FALSE on success.
230 if (client
->new == TRUE
)
231 cache_clear(client
->md5file
, 1);
234 memset(client
, 0, sizeof(struct client_s
));
235 client
->state
= STATE_CONNECTED
;
237 client
->freed
= TRUE
;
239 client
->pinentry
= pin
;
244 gboolean
do_decompress(assuan_context_t ctx
, gpointer in
, gint insize
,
245 gpointer
*out
, glong
*outsize
, gint
*rc
)
251 gchar str
[ASSUAN_LINELENGTH
];
257 z
.avail_out
= zlib_bufsize
;
258 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
261 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
266 *rc
= inflateInit2(&z
, 47);
269 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
274 memset(&h
, 0, sizeof(gz_header
));
275 h
.comment
= (guchar
*)buf
;
276 h
.comm_max
= sizeof(buf
);
277 *rc
= inflateGetHeader(&z
, &h
);
280 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
286 *rc
= inflate(&z
, Z_BLOCK
);
289 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
296 insize
= atoi((gchar
*)h
.comment
);
301 *rc
= inflate(&z
, Z_FINISH
);
308 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
316 z
.next_out
= pout
+ z
.total_out
;
317 z
.avail_out
= zlib_bufsize
;
320 *rc
= assuan_write_status(ctx
, "DECOMPRESS",
321 print_fmt(str
, sizeof(str
), "%i %i", z
.total_out
, insize
));
336 } while (*rc
!= Z_STREAM_END
);
339 *rc
= assuan_write_status(ctx
, "DECOMPRESS",
340 print_fmt(str
, sizeof(str
), "%i %i", z
.total_out
, insize
));
347 *outsize
= z
.total_out
;
353 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
359 gpg_error_t
read_file_header(const gchar
*filename
, file_header_t
*fh
)
364 fd
= open(filename
, O_RDONLY
);
367 return gpg_error_from_errno(errno
);
369 len
= pth_read(fd
, fh
, sizeof(file_header_t
));
372 if (len
!= sizeof(file_header_t
))
373 return gpg_error_from_errno(errno
);
378 static gpg_error_t
open_command_finalize(assuan_context_t ctx
, guchar shakey
[],
381 struct client_s
*client
= assuan_get_pointer(ctx
);
388 if ((fd
= open_file(client
->filename
, &st
)) == -1) {
390 if (errno
== ENOENT
) {
398 log_write("%s: %s", client
->filename
, strerror(errno
));
399 cleanup_client(client
);
400 memset(shakey
, 0, sizeof(shakey
));
401 return send_syserror(ctx
, rc
);
404 rc
= try_xml_decrypt(ctx
, fd
, st
, shakey
, &iter
);
408 memset(shakey
, 0, sizeof(shakey
));
409 cleanup_client(client
);
410 return send_error(ctx
, rc
);
414 CACHE_LOCK(client
->ctx
);
416 if (cached
== FALSE
) {
417 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
418 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
419 cleanup_client(client
);
421 return send_error(ctx
, EPWMD_MAX_SLOTS
);
424 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
425 cache_reset_timeout(client
->md5file
, timeout
);
428 cache_set_timeout(client
->md5file
, -2);
433 memset(shakey
, 0, sizeof(shakey
));
437 gcry_free(client
->xml
);
442 if (client
->new == FALSE
)
443 send_status_all(STATUS_CACHE
);
445 client
->state
= STATE_OPEN
;
449 g_key_file_set_integer(keyfileh
, client
->filename
, "iterations", iter
);
451 return send_error(ctx
, rc
);
454 static int open_command(assuan_context_t ctx
, char *line
)
457 guchar shakey
[gcrykeysize
];
458 gboolean cached
= FALSE
;
460 struct client_s
*client
= assuan_get_pointer(ctx
);
462 gchar
*filename
= NULL
;
463 file_header_t file_header
;
465 memset(shakey
, 0, sizeof(shakey
));
467 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
470 if (!filename
|| !*filename
) {
472 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
475 if (valid_filename(filename
) == FALSE
) {
477 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
480 if (client
->state
== STATE_OPEN
)
481 cleanup_client(client
);
483 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
484 CACHE_LOCK(client
->ctx
);
486 if (cache_has_file(client
->md5file
) == FALSE
) {
487 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
490 return send_error(ctx
, EPWMD_MAX_SLOTS
);
494 cache_incr_refcount(client
->md5file
);
496 rc
= lock_file_mutex(client
);
500 return send_error(ctx
, rc
);
503 client
->freed
= FALSE
;
505 if ((rc
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
507 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
508 cleanup_client(client
);
509 return send_error(ctx
, rc
);
512 if (stat(filename
, &st
) == 0) {
513 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
514 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
516 cleanup_client(client
);
517 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
520 client
->mtime
= st
.st_mtime
;
523 client
->filename
= g_strdup(filename
);
525 if (!client
->filename
) {
526 memset(shakey
, 0, sizeof(shakey
));
527 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
528 cleanup_client(client
);
530 return send_syserror(ctx
, ENOMEM
);
534 client
->pinentry
->filename
= g_strdup(client
->filename
);
536 if (!client
->pinentry
->filename
) {
537 memset(shakey
, 0, sizeof(shakey
));
538 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
539 cleanup_client(client
);
541 return send_syserror(ctx
, ENOMEM
);
546 * New files don't need a key.
548 if (access(filename
, R_OK
) != 0) {
549 if (errno
!= ENOENT
) {
551 log_write("%s: %s", filename
, strerror(errno
));
553 cleanup_client(client
);
554 return send_syserror(ctx
, rc
);
557 if ((client
->xml
= new_document()) == NULL
) {
558 log_write("%s", strerror(ENOMEM
));
560 cleanup_client(client
);
561 return send_syserror(ctx
, ENOMEM
);
564 client
->len
= xmlStrlen(client
->xml
);
566 client
->filename
= g_strdup(filename
);
568 if (!client
->filename
) {
570 cleanup_client(client
);
571 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
572 return send_syserror(ctx
, ENOMEM
);
575 memset(shakey
, 0, sizeof(shakey
));
577 if (req
[1] && *req
[1])
578 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
581 return open_command_finalize(ctx
, shakey
, cached
);
584 rc
= read_file_header(filename
, &file_header
);
588 cleanup_client(client
);
589 return send_error(ctx
, rc
);
592 if (file_header
.iter
== -1)
595 CACHE_LOCK(client
->ctx
);
596 cached
= cache_get_key(client
->md5file
, shakey
);
599 if (cached
== FALSE
) {
601 * No key specified and no matching filename found in the cache. Use
602 * pinentry to retrieve the key. Cannot return assuan_process_done()
603 * here otherwise the command will be interrupted. The event loop in
604 * client_thread() will poll the file descriptor waiting for it to
605 * become ready to read a pinentry_key_s which will contain the
606 * entered key or rc. It will then call open_command_finalize() to
607 * to finish the command.
609 if (!req
[1] || !*req
[1]) {
611 gboolean b
= get_key_file_boolean(filename
, "enable_pinentry");
613 /* From set_pinentry_defaults(). */
614 if (client
->pinentry
->enable
== FALSE
||
615 (client
->pinentry
->enable
== -1 && b
== FALSE
)) {
616 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
621 lock_pin_mutex(client
);
622 client
->pinentry
->which
= PINENTRY_OPEN
;
623 rc
= pinentry_fork(ctx
);
626 unlock_pin_mutex(client
->pinentry
);
627 cleanup_client(client
);
628 return send_error(ctx
, rc
);
631 client
->pinentry
->cb
= open_command_finalize
;
632 client
->pinentry
->status
= PINENTRY_INIT
;
635 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
640 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
645 return open_command_finalize(ctx
, shakey
, cached
);
648 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
649 gint size
, gpointer
*out
, glong
*outsize
, gint
*rc
)
655 gint cmd
= Z_NO_FLUSH
;
656 gchar str
[ASSUAN_LINELENGTH
];
660 z
.next_in
= pin
= data
;
661 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
662 z
.avail_out
= zlib_bufsize
;
663 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
666 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
671 *rc
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
674 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
679 memset(&h
, 0, sizeof(gz_header
));
680 g_snprintf(buf
, sizeof(buf
), "%i", size
);
681 h
.comment
= (guchar
*)buf
;
682 *rc
= deflateSetHeader(&z
, &h
);
685 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
694 *rc
= deflate(&z
, cmd
);
701 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
709 z
.next_out
= pout
+ z
.total_out
;
710 z
.avail_out
= zlib_bufsize
;
713 if (!z
.avail_in
&& z
.total_in
< size
) {
714 if (z
.total_in
+ zlib_bufsize
> size
)
715 z
.avail_in
= size
- z
.total_in
;
717 z
.avail_in
= zlib_bufsize
;
720 *rc
= assuan_write_status(ctx
, "COMPRESS",
721 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
728 if (z
.total_in
>= size
)
739 } while (*rc
!= Z_STREAM_END
);
742 *rc
= assuan_write_status(ctx
, "COMPRESS",
743 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
750 *outsize
= z
.total_out
;
756 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
762 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
763 const gchar
*filename
, gpointer data
, size_t insize
, guchar
*shakey
,
769 guchar tkey
[gcrykeysize
];
772 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
773 gchar tmp
[FILENAME_MAX
];
776 file_header_t file_header
;
777 gchar str
[ASSUAN_LINELENGTH
];
781 * cache_file_count() needs both .used == TRUE and a valid key in
782 * order for it to count as a used cache entry. Fixes CACHE status
785 memset(shakey
, '!', gcrykeysize
);
787 file_header
.iter
= iter
;
791 if (insize
/ gcryblocksize
) {
792 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
794 if (insize
% gcryblocksize
)
795 len
+= gcryblocksize
;
799 * Resize the existing xml buffer to the block size required by gcrypt
800 * rather than duplicating it and wasting memory.
802 inbuf
= gcry_realloc(data
, len
);
805 return gpg_error_from_errno(ENOMEM
);
808 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
809 memcpy(tkey
, shakey
, sizeof(tkey
));
812 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
813 memset(tkey
, 0, sizeof(tkey
));
815 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
819 memset(tkey
, 0, sizeof(tkey
));
820 file_header
.iter
= iter
;
823 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
825 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
826 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
827 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
835 while (xiter
< file_header
.iter
) {
836 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
837 if (!(xiter
% iter_progress
)) {
838 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
839 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
848 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
849 sizeof(file_header
.iv
)))) {
851 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
855 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
858 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
866 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
867 sizeof(file_header
.iv
)))) {
869 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
873 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
875 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
879 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
881 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
885 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
886 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
887 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
897 if (stat(filename
, &st
) == 0) {
898 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
901 * FIXME What if the file has an ACL?
903 if (!(mode
& S_IWUSR
)) {
905 return gpg_error_from_errno(EACCES
);
909 if (errno
!= ENOENT
) {
912 return gpg_error_from_errno(rc
);
916 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
918 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
921 p
= strrchr(tmp
, '/');
923 log_write("%s: %s", p
, strerror(rc
));
924 return gpg_error_from_errno(rc
);
929 * xml_import() from command line.
933 len
= pth_write(fd
, &file_header
, sizeof(file_header_t
));
935 if (len
!= sizeof(file_header
)) {
944 return gpg_error_from_errno(len
);
947 len
= pth_write(fd
, inbuf
, insize
);
958 return gpg_error_from_errno(len
);
961 if (fsync(fd
) == -1) {
970 return gpg_error_from_errno(len
);
976 if (mode
&& get_key_file_boolean(filename
, "backup") == TRUE
) {
977 gchar tmp2
[FILENAME_MAX
];
979 g_snprintf(tmp2
, sizeof(tmp2
), "%s.backup", filename
);
981 if (rename(filename
, tmp2
) == -1) {
985 return gpg_error_from_errno(len
);
989 if (rename(tmp
, filename
) == -1) {
993 return gpg_error_from_errno(len
);
997 chmod(filename
, mode
);
1004 static gpg_error_t
save_command_finalize(assuan_context_t ctx
,
1005 guchar shakey
[], gboolean cached
)
1007 struct client_s
*client
= assuan_get_pointer(ctx
);
1019 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
1022 iter
= get_key_file_integer(client
->filename
, "compression_level");
1027 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
1028 memset(shakey
, 0, sizeof(shakey
));
1031 if (zrc
== Z_MEM_ERROR
) {
1032 return send_syserror(ctx
, ENOMEM
);
1035 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
1043 iter
= get_key_file_integer(client
->filename
, "iterations");
1044 rc
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
1047 memset(shakey
, 0, sizeof(shakey
));
1048 return send_error(ctx
, rc
);
1051 stat(client
->filename
, &st
);
1052 client
->mtime
= st
.st_mtime
;
1053 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
1054 CACHE_LOCK(client
->ctx
);
1057 memset(shakey
, 0, sizeof(shakey
));
1058 cache_reset_timeout(client
->md5file
, timeout
);
1061 if (client
->new == TRUE
)
1062 send_status_all(STATUS_CACHE
);
1064 client
->new = FALSE
;
1065 return send_error(ctx
, 0);
1068 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
1069 memset(shakey
, 0, sizeof(shakey
));
1070 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
1072 return send_error(ctx
, EPWMD_MAX_SLOTS
);
1075 client
->new = FALSE
;
1076 memset(shakey
, 0, sizeof(shakey
));
1077 cache_reset_timeout(client
->md5file
, timeout
);
1079 send_status_all(STATUS_CACHE
);
1080 return send_error(ctx
, 0);
1083 static int save_command(assuan_context_t ctx
, char *line
)
1085 gboolean cached
= FALSE
;
1086 guchar shakey
[gcrykeysize
];
1088 struct client_s
*client
= assuan_get_pointer(ctx
);
1091 memset(shakey
, 0, sizeof(shakey
));
1092 rc
= file_modified(client
);
1095 log_write("%s: %s", client
->filename
? client
->filename
: "",
1097 return send_error(ctx
, rc
);
1100 rc
= lock_file_mutex(client
);
1103 return send_error(ctx
, rc
);
1105 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1106 return send_syserror(ctx
, errno
);
1108 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
1109 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
1110 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
1113 if (get_key_file_integer(client
->filename
, "iterations") == -1)
1116 if (!line
|| !*line
) {
1117 guchar tmp
[sizeof(shakey
)];
1120 memset(tmp
, '!', sizeof(tmp
));
1122 if (cache_get_key(client
->md5file
, shakey
) == FALSE
||
1123 memcmp(shakey
, tmp
, sizeof(shakey
)) == 0) {
1125 #ifdef WITH_PINENTRY
1126 if (get_key_file_boolean(client
->filename
, "enable_pinentry") == FALSE
) {
1127 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1131 lock_pin_mutex(client
);
1132 client
->pinentry
->which
= PINENTRY_SAVE
;
1133 rc
= pinentry_fork(ctx
);
1136 unlock_pin_mutex(client
->pinentry
);
1137 return send_error(ctx
, rc
);
1140 client
->pinentry
->cb
= save_command_finalize
;
1141 client
->pinentry
->status
= PINENTRY_INIT
;
1144 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1154 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
1155 memset(line
, 0, strlen(line
));
1159 return save_command_finalize(ctx
, shakey
, cached
);
1162 static int delete_command(assuan_context_t ctx
, char *line
)
1164 struct client_s
*client
= assuan_get_pointer(ctx
);
1169 rc
= file_modified(client
);
1172 log_write("%s: %s", client
->filename
? client
->filename
: "",
1174 return send_error(ctx
, rc
);
1177 if (strchr(line
, '\t'))
1178 req
= split_input_line(line
, "\t", -1);
1180 req
= split_input_line(line
, " ", -1);
1183 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1185 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1189 return send_error(ctx
, rc
);
1193 * No sub-node defined. Remove the entire node (account).
1202 return send_error(ctx
, 0);
1205 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1209 return send_error(ctx
, rc
);
1216 return send_error(ctx
, 0);
1220 * Don't return with assuan_process_done() here. This has been called from
1221 * assuan_process_next() and the command should be finished in
1224 static int store_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
1227 assuan_context_t ctx
= data
;
1228 struct client_s
*client
= assuan_get_pointer(ctx
);
1231 gpg_error_t rc
= file_modified(client
);
1233 if (assuan_rc
|| rc
) {
1240 return assuan_rc
? assuan_rc
: rc
;
1243 req
= split_input_line((gchar
*)line
, "\t", 0);
1251 return EPWMD_COMMAND_SYNTAX
;
1253 if (valid_xml_element((xmlChar
*)*req
) == FALSE
) {
1255 return EPWMD_INVALID_ELEMENT
;
1258 if (valid_element_path(req
+1, TRUE
) == FALSE
) {
1260 return EPWMD_INVALID_ELEMENT
;
1264 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1266 if (rc
&& rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1267 rc
= new_account(client
->doc
, *req
);
1284 create_elements_cb(n
, req
+1, &rc
, NULL
);
1286 find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1287 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1291 client
->inquire_status
= INQUIRE_DONE
;
1295 static int store_command(assuan_context_t ctx
, char *line
)
1297 struct client_s
*client
= assuan_get_pointer(ctx
);
1298 gpg_error_t rc
= file_modified(client
);
1301 log_write("%s: %s", client
->filename
? client
->filename
: "",
1303 return send_error(ctx
, rc
);
1306 rc
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1309 return send_error(ctx
, rc
);
1311 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1312 client
->inquire_status
= INQUIRE_BUSY
;
1316 static int get_command(assuan_context_t ctx
, char *line
)
1318 struct client_s
*client
= assuan_get_pointer(ctx
);
1323 rc
= file_modified(client
);
1326 log_write("%s: %s", client
->filename
? client
->filename
: "",
1328 return send_error(ctx
, rc
);
1331 req
= split_input_line(line
, "\t", -1);
1333 if (!req
|| !*req
) {
1335 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1338 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1342 return send_error(ctx
, rc
);
1346 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1351 return send_error(ctx
, rc
);
1353 if (!n
|| !n
->children
)
1354 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1356 n
= find_text_node(n
->children
);
1358 if (!n
|| !n
->content
|| !*n
->content
)
1359 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1361 rc
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1362 return send_error(ctx
, rc
);
1365 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**target
,
1366 gpg_error_t
*rc
, gchar
**req_orig
, void *data
)
1368 gchar
*path
= *(gchar
**)data
;
1369 gchar
*tmp
= NULL
, *result
;
1373 *(gchar
**)data
= NULL
;
1376 path
= g_strjoinv("\t", target
);
1379 *rc
= gpg_error_from_errno(ENOMEM
);
1384 tmp
= g_strjoinv("\t", req_orig
);
1388 *rc
= gpg_error_from_errno(ENOMEM
);
1394 result
= g_strdup_printf("%s\t%s", path
, tmp
);
1396 result
= g_strdup(path
);
1399 *rc
= gpg_error_from_errno(ENOMEM
);
1407 *(gchar
**)data
= result
;
1411 static int realpath_command(assuan_context_t ctx
, char *line
)
1414 struct client_s
*client
= assuan_get_pointer(ctx
);
1422 rc
= file_modified(client
);
1425 log_write("%s: %s", client
->filename
? client
->filename
: "",
1427 return send_error(ctx
, rc
);
1430 if (strchr(line
, '\t') != NULL
) {
1431 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1432 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1435 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1436 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1439 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1443 return send_error(ctx
, rc
);
1446 rp
= g_strjoinv("\t", req
);
1450 return send_syserror(ctx
, ENOMEM
);
1454 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1455 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, &rp
);
1460 return send_error(ctx
, rc
);
1464 string
= g_string_new(rp
);
1469 return send_syserror(ctx
, ENOMEM
);
1472 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++) {
1473 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+1) && *(t
+1) != '!')) {
1474 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1479 rc
= assuan_send_data(ctx
, string
->str
, string
->len
);
1480 g_string_free(string
, TRUE
);
1481 return send_error(ctx
, rc
);
1484 static int list_command(assuan_context_t ctx
, char *line
)
1486 struct client_s
*client
= assuan_get_pointer(ctx
);
1488 struct element_list_s
*elements
= NULL
;
1491 if (disable_list_and_dump
== TRUE
)
1492 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1494 rc
= file_modified(client
);
1497 log_write("%s: %s", client
->filename
, pwmd_strerror(rc
));
1498 return send_error(ctx
, rc
);
1504 rc
= list_accounts(client
->doc
, &str
);
1507 return send_error(ctx
, rc
);
1509 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1510 g_string_free(str
, TRUE
);
1511 return send_error(ctx
, rc
);
1514 elements
= g_malloc0(sizeof(struct element_list_s
));
1517 rc
= gpg_err_code_from_errno(ENOMEM
);
1521 rc
= create_path_list(client
->doc
, elements
, line
);
1527 gint total
= g_slist_length(elements
->list
);
1532 rc
= EPWMD_EMPTY_ELEMENT
;
1536 str
= g_string_new(NULL
);
1539 rc
= gpg_err_code_from_errno(ENOMEM
);
1543 for (i
= 0; i
< total
; i
++) {
1544 tmp
= g_slist_nth_data(elements
->list
, i
);
1545 g_string_append_printf(str
, "%s%s", tmp
, i
+1 == total
? "" : "\n");
1548 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1549 g_string_free(str
, TRUE
);
1552 rc
= EPWMD_EMPTY_ELEMENT
;
1556 gint total
= g_slist_length(elements
->list
);
1559 for (i
= 0; i
< total
; i
++) {
1560 tmp
= g_slist_nth_data(elements
->list
, i
);
1564 g_slist_free(elements
->list
);
1566 if (elements
->prefix
)
1567 g_free(elements
->prefix
);
1572 return send_error(ctx
, rc
);
1575 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1580 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1581 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1584 return EPWMD_LIBXML_ERROR
;
1587 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1593 * req[0] - element path
1595 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1597 struct client_s
*client
= assuan_get_pointer(ctx
);
1598 gchar
**attrlist
= NULL
;
1600 gchar
**path
= NULL
;
1606 if (!req
|| !req
[0])
1607 return EPWMD_COMMAND_SYNTAX
;
1609 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1611 * The first argument may be only an account.
1613 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1614 return EPWMD_COMMAND_SYNTAX
;
1617 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1625 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1626 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1636 for (a
= n
->properties
; a
; a
= a
->next
) {
1639 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1641 g_strfreev(attrlist
);
1643 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1644 return gpg_error_from_errno(ENOMEM
);
1649 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1652 g_strfreev(attrlist
);
1653 return gpg_error_from_errno(ENOMEM
);
1656 attrlist
[++i
] = NULL
;
1660 return EPWMD_EMPTY_ELEMENT
;
1662 line
= g_strjoinv("\n", attrlist
);
1665 g_strfreev(attrlist
);
1666 return gpg_error_from_errno(ENOMEM
);
1669 rc
= assuan_send_data(ctx
, line
, strlen(line
));
1671 g_strfreev(attrlist
);
1676 * req[0] - attribute
1677 * req[1] - element path
1679 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1683 gchar
**path
= NULL
;
1686 if (!req
|| !req
[0] || !req
[1])
1687 return EPWMD_COMMAND_SYNTAX
;
1689 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1691 * The first argument may be only an account.
1693 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1694 return EPWMD_COMMAND_SYNTAX
;
1698 * Don't remove the "name" attribute for the account element. To remove an
1699 * account use DELETE <account>.
1701 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1702 rc
= EPWMD_ATTR_SYNTAX
;
1706 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1712 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1713 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1721 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1722 return EPWMD_ATTR_NOT_FOUND
;
1724 if (xmlRemoveProp(a
) == -1)
1725 return EPWMD_LIBXML_ERROR
;
1734 static xmlNodePtr
create_element_path(struct client_s
*client
, gchar
***path
,
1737 gchar
**src
= *path
;
1738 gchar
**src_orig
= g_strdupv(src
);
1739 xmlNodePtr n
= NULL
;
1744 *rc
= gpg_error_from_errno(ENOMEM
);
1749 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1752 if (*rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1753 *rc
= new_account(client
->doc
, src
[0]);
1766 n
= create_target_elements_cb(n
, src
+1, rc
, NULL
);
1768 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1769 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
1775 * Reset the position of the element tree now that the elements
1776 * have been created.
1781 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1786 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1787 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1795 g_strfreev(src_orig
);
1802 * Creates a "target" attribute. When other commands encounter an element with
1803 * this attribute, the element path is modified to the target value. If the
1804 * source element path doesn't exist when using 'ATTR SET target', it is
1805 * created, but the destination element path must exist.
1807 * req[0] - source element path
1808 * req[1] - destination element path
1810 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1812 gchar
**src
, **dst
, *line
= NULL
;
1816 if (!req
|| !req
[0] || !req
[1])
1817 return EPWMD_COMMAND_SYNTAX
;
1819 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1821 * The first argument may be only an account.
1823 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1824 return EPWMD_COMMAND_SYNTAX
;
1827 if (valid_element_path(src
, FALSE
) == FALSE
) {
1829 return EPWMD_INVALID_ELEMENT
;
1832 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1834 * The first argument may be only an account.
1836 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1837 rc
= EPWMD_COMMAND_SYNTAX
;
1842 n
= find_account(client
->doc
, &dst
, &rc
, NULL
, 0);
1845 * Make sure the destination element path exists.
1851 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
1852 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1858 n
= create_element_path(client
, &src
, &rc
);
1863 line
= g_strjoinv("\t", dst
);
1866 rc
= gpg_error_from_errno(ENOMEM
);
1870 rc
= add_attribute(n
, "target", line
);
1880 * req[0] - account name
1883 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
1889 tmp
= g_strdupv(req
);
1892 return gpg_error_from_errno(ENOMEM
);
1894 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1900 if (g_utf8_collate(req
[0], req
[1]) == 0)
1904 * Will not overwrite an existing account.
1906 tmp
= g_strdupv(req
+1);
1909 return gpg_error_from_errno(ENOMEM
);
1911 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1914 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
)
1918 return EPWMD_ACCOUNT_EXISTS
;
1921 * Whitespace not allowed in account names.
1923 if (contains_whitespace(req
[1]) == TRUE
)
1924 return EPWMD_ATTR_SYNTAX
;
1926 tmp
= g_strdupv(req
);
1929 return gpg_error_from_errno(ENOMEM
);
1931 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1935 return EPWMD_ELEMENT_NOT_FOUND
;
1937 return add_attribute(n
, "name", req
[1]);
1941 * req[0] - attribute
1942 * req[1] - element path
1944 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
1946 struct client_s
*client
= assuan_get_pointer(ctx
);
1952 if (!req
|| !req
[0] || !req
[1])
1953 return EPWMD_COMMAND_SYNTAX
;
1955 if (strchr(req
[1], '\t')) {
1956 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
1957 return EPWMD_COMMAND_SYNTAX
;
1960 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1961 return EPWMD_COMMAND_SYNTAX
;
1964 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1970 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1971 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1979 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
1980 return EPWMD_ATTR_NOT_FOUND
;
1982 rc
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
1992 * req[0] - attribute
1993 * req[1] - element path
1996 static int attribute_set(struct client_s
*client
, gchar
**req
)
1998 gchar
**path
= NULL
;
2002 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2003 return EPWMD_COMMAND_SYNTAX
;
2006 * Reserved attribute names.
2008 if (g_utf8_collate(req
[0], "name") == 0) {
2010 * Only reserved for the account element. Not the rest of the
2013 if (strchr(req
[1], '\t') == NULL
)
2014 return name_attribute(client
, req
+ 1);
2016 else if (g_utf8_collate(req
[0], "target") == 0)
2017 return target_attribute(client
, req
+ 1);
2019 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2021 * The first argument may be only an account.
2023 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2024 return EPWMD_COMMAND_SYNTAX
;
2027 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2033 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2034 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2041 return add_attribute(n
, req
[0], req
[2]);
2050 * req[1] - attribute name or element path if command is LIST
2051 * req[2] - element path
2052 * req[2] - element path or value
2054 static int attr_command(assuan_context_t ctx
, char *line
)
2056 struct client_s
*client
= assuan_get_pointer(ctx
);
2060 rc
= file_modified(client
);
2063 log_write("%s: %s", client
->filename
? client
->filename
: "",
2065 return send_error(ctx
, rc
);
2068 req
= split_input_line(line
, " ", 4);
2070 if (!req
|| !req
[0] || !req
[1]) {
2072 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2075 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2076 rc
= attribute_set(client
, req
+1);
2077 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2078 rc
= attribute_get(ctx
, req
+1);
2079 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2080 rc
= attribute_delete(client
, req
+1);
2081 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2082 rc
= attribute_list(ctx
, req
+1);
2084 rc
= EPWMD_COMMAND_SYNTAX
;
2087 return send_error(ctx
, rc
);
2090 static int iscached_command(assuan_context_t ctx
, char *line
)
2092 gchar
**req
= split_input_line(line
, " ", 0);
2095 if (!req
|| !*req
) {
2097 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2100 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2104 if (cache_iscached(md5file
) == FALSE
) {
2106 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2110 return send_error(ctx
, 0);
2113 static int clearcache_command(assuan_context_t ctx
, char *line
)
2115 struct client_s
*client
= assuan_get_pointer(ctx
);
2116 gchar
**req
= split_input_line(line
, " ", 0);
2121 if (!req
|| !*req
) {
2123 cache_clear(client
->md5file
, 2);
2125 return send_error(ctx
, 0);
2128 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2131 if (cache_clear(md5file
, 1) == FALSE
) {
2133 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2137 return send_error(ctx
, 0);
2140 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2144 gchar
**req
= split_input_line(line
, " ", 0);
2146 struct client_s
*client
= assuan_get_pointer(ctx
);
2148 if (!req
|| !*req
|| !req
[1]) {
2150 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2154 timeout
= strtol(req
[0], &p
, 10);
2156 if (errno
!= 0 || *p
!= 0) {
2158 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2161 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2163 CACHE_LOCK(client
->ctx
);
2165 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2167 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2171 return send_error(ctx
, 0);
2174 static int dump_command(assuan_context_t ctx
, char *line
)
2178 struct client_s
*client
= assuan_get_pointer(ctx
);
2181 if (disable_list_and_dump
== TRUE
)
2182 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2184 rc
= file_modified(client
);
2187 log_write("%s: %s", client
->filename
? client
->filename
: "",
2189 return send_error(ctx
, rc
);
2192 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2195 return send_syserror(ctx
, ENOMEM
);
2197 rc
= assuan_send_data(ctx
, xml
, len
);
2199 return send_error(ctx
, rc
);
2202 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2204 struct client_s
*client
= assuan_get_pointer(ctx
);
2206 gchar filename
[255]={0}, param
[747]={0};
2207 gchar
*p
, *tmp
, *fp
= client
->filename
, *paramp
= line
;
2209 if (strchr(line
, ' ')) {
2210 sscanf(line
, " %254[a-zA-Z] %746c", filename
, param
);
2215 if (strcmp(line
, "key") == 0 || strcmp(line
, "key_file") == 0)
2216 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2218 p
= get_key_file_string(fp
? fp
: "global", paramp
);
2221 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2223 tmp
= expand_homedir(p
);
2227 return send_syserror(ctx
, ENOMEM
);
2232 rc
= assuan_send_data(ctx
, p
, strlen(p
));
2234 return send_error(ctx
, rc
);
2237 static int xpath_command(assuan_context_t ctx
, gchar
*line
)
2239 struct client_s
*client
= assuan_get_pointer(ctx
);
2241 xmlXPathContextPtr xp
;
2242 xmlXPathObjectPtr result
;
2243 xmlBufferPtr buf
= NULL
;
2246 rc
= file_modified(client
);
2249 log_write("%s: %s", client
->filename
? client
->filename
: "",
2251 return send_error(ctx
, rc
);
2254 if (!line
|| !*line
)
2255 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2257 if ((req
= split_input_line(line
, "\t", 2)) == NULL
) {
2258 if (strv_printf(&req
, "%s", line
) == FALSE
)
2259 return send_syserror(ctx
, ENOMEM
);
2262 xp
= xmlXPathNewContext(client
->doc
);
2265 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2267 result
= xmlXPathEvalExpression((xmlChar
*)req
[0], xp
);
2270 xmlXPathFreeContext(xp
);
2271 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2274 if (xmlXPathNodeSetIsEmpty(result
->nodesetval
)) {
2275 rc
= EPWMD_EMPTY_ELEMENT
;
2279 rc
= recurse_xpath_nodeset(client
->doc
, result
->nodesetval
,
2280 (xmlChar
*)req
[1], &buf
);
2284 else if (!req
[1] && !xmlBufferLength(buf
)) {
2285 rc
= EPWMD_EMPTY_ELEMENT
;
2291 rc
= assuan_send_data(ctx
, xmlBufferContent(buf
), xmlBufferLength(buf
));
2300 xmlXPathFreeObject(result
);
2303 xmlXPathFreeContext(xp
);
2305 return send_error(ctx
, rc
);
2308 static int import_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
2311 struct client_s
*client
= assuan_get_pointer((assuan_context_t
)data
);
2312 gpg_error_t rc
= file_modified(client
);
2313 gchar
**req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2315 xmlNodePtr n
, root
, copy
;
2317 if (assuan_rc
|| rc
) {
2324 return assuan_rc
? assuan_rc
: rc
;
2327 req
= split_input_line((gchar
*)line
, " ", 2);
2335 return EPWMD_COMMAND_SYNTAX
;
2337 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2338 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
2339 return EPWMD_COMMAND_SYNTAX
;
2344 if (!content
|| !*content
) {
2345 rc
= EPWMD_COMMAND_SYNTAX
;
2349 if (valid_xml_element((xmlChar
*)*path
) == FALSE
) {
2350 rc
= EPWMD_INVALID_ELEMENT
;
2354 if (valid_element_path(path
+1, FALSE
) == FALSE
) {
2355 rc
= EPWMD_INVALID_ELEMENT
;
2359 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2362 rc
= EPWMD_LIBXML_ERROR
;
2366 root
= xmlDocGetRootElement(doc
);
2367 path_orig
= g_strdupv(path
);
2371 rc
= gpg_error_from_errno(ENOMEM
);
2375 if (strv_printf(&path
, "%s", (gchar
*)root
->name
) == FALSE
) {
2376 g_strfreev(path_orig
);
2378 rc
= gpg_error_from_errno(ENOMEM
);
2382 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2384 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2385 g_strfreev(path_orig
);
2390 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2392 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2393 g_strfreev(path_orig
);
2398 xmlNodePtr parent
= n
->parent
;
2409 if (rc
== EPWMD_ELEMENT_NOT_FOUND
) {
2410 n
= create_element_path(client
, &path
, &rc
);
2418 copy
= xmlCopyNode(root
, 1);
2419 n
= xmlAddChild(n
, copy
);
2423 rc
= EPWMD_LIBXML_ERROR
;
2428 client
->inquire_status
= INQUIRE_DONE
;
2432 static int import_command(assuan_context_t ctx
, gchar
*line
)
2435 struct client_s
*client
= assuan_get_pointer(ctx
);
2437 rc
= file_modified(client
);
2440 log_write("%s: %s", client
->filename
? client
->filename
: "",
2442 return send_error(ctx
, rc
);
2445 rc
= assuan_inquire_ext(ctx
, "IMPORT", 0, import_command_finalize
, ctx
);
2448 return send_error(ctx
, rc
);
2450 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
2451 client
->inquire_status
= INQUIRE_BUSY
;
2455 static int lock_command(assuan_context_t ctx
, gchar
*line
)
2458 struct client_s
*client
= assuan_get_pointer(ctx
);
2460 rc
= file_modified(client
);
2463 log_write("%s: %s", client
->filename
? client
->filename
: "",
2465 return send_error(ctx
, rc
);
2468 rc
= lock_file_mutex(client
);
2471 client
->is_lock_cmd
= TRUE
;
2473 return send_error(ctx
, rc
);
2476 static int unlock_command(assuan_context_t ctx
, gchar
*line
)
2479 struct client_s
*client
= assuan_get_pointer(ctx
);
2481 rc
= file_modified(client
);
2484 log_write("%s: %s", client
->filename
? client
->filename
: "",
2486 return send_error(ctx
, rc
);
2489 unlock_file_mutex(client
);
2490 return send_error(ctx
, 0);
2493 static int getpid_command(assuan_context_t ctx
, gchar
*line
)
2497 pid_t pid
= getpid();
2499 print_fmt(buf
, sizeof(buf
), "%i", pid
);
2500 rc
= assuan_send_data(ctx
, buf
, strlen(buf
));
2501 return send_error(ctx
, rc
);
2504 void cleanup_assuan(assuan_context_t ctx
)
2506 struct client_s
*cl
= assuan_get_pointer(ctx
);
2512 static void reset_notify(assuan_context_t ctx
)
2514 struct client_s
*cl
= assuan_get_pointer(ctx
);
2520 static gpg_error_t
parse_client_option(assuan_context_t ctx
, const gchar
*line
)
2522 gchar name
[32] = {0}, value
[256] = {0};
2524 if (sscanf(line
, " %31[a-zA-Z] = %255c", name
, value
) != 2)
2525 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_SYNTAX
);
2527 if (g_strcasecmp(name
, (gchar
*)"NAME") == 0) {
2528 pth_attr_t attr
= pth_attr_of(pth_self());
2530 log_write("OPTION CLIENT %s", line
);
2531 pth_attr_set(attr
, PTH_ATTR_NAME
, value
);
2532 pth_attr_destroy(attr
);
2535 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2540 static int option_handler(assuan_context_t ctx
, const gchar
*name
,
2543 #ifdef WITH_PINENTRY
2544 struct client_s
*client
= assuan_get_pointer(ctx
);
2547 if (!value
|| !*value
)
2548 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2550 if (g_strcasecmp(name
, (gchar
*)"client") == 0)
2551 return parse_client_option(ctx
, value
);
2553 if (g_strcasecmp(name
, (gchar
*)"iterations") == 0) {
2558 n
= strtol(value
, &p
, 10);
2560 if (errno
|| (p
&& *p
))
2561 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2563 g_key_file_set_integer(keyfileh
, client
->filename
? client
->filename
: "global", "iterations", n
);
2564 send_status_all(STATUS_CONFIG
);
2566 #ifdef WITH_PINENTRY
2567 else if (g_strcasecmp(name
, (gchar
*)"ttyname") == 0) {
2568 g_free(client
->pinentry
->ttyname
);
2569 client
->pinentry
->ttyname
= g_strdup(value
);
2571 else if (g_strcasecmp(name
, (gchar
*)"ttytype") == 0) {
2572 g_free(client
->pinentry
->ttytype
);
2573 client
->pinentry
->ttytype
= g_strdup(value
);
2575 else if (g_strcasecmp(name
, (gchar
*)"display") == 0) {
2576 g_free(client
->pinentry
->display
);
2577 client
->pinentry
->display
= g_strdup(value
);
2579 else if (g_strcasecmp(name
, (gchar
*)"path") == 0) {
2580 g_free(client
->pinentry
->path
);
2581 client
->pinentry
->path
= g_strdup(value
);
2583 else if (g_strcasecmp(name
, (gchar
*)"title") == 0) {
2584 g_free(client
->pinentry
->title
);
2585 client
->pinentry
->title
= g_strdup(value
);
2587 else if (g_strcasecmp(name
, (gchar
*)"prompt") == 0) {
2588 g_free(client
->pinentry
->prompt
);
2589 client
->pinentry
->prompt
= g_strdup(value
);
2591 else if (g_strcasecmp(name
, (gchar
*)"desc") == 0) {
2592 g_free(client
->pinentry
->desc
);
2593 client
->pinentry
->desc
= g_strdup(value
);
2596 /* Need to wait for pinentry to support a --timeout option so it can
2597 * terminate itself. Cannot do it here because assuan_pipe_connect() calls
2598 * execv() which replaces the pid of the fork()ed thread from
2599 * pinentry_fork(). So pinentry will become a real process after the
2600 * thread terminates and won't be able to be kill()ed from pwmd.
2602 else if (g_strcasecmp(name
, (gchar
*)"timeout") == 0) {
2604 gint n
= strtol(value
, &p
, 10);
2607 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2609 client
->pinentry
->timeout
= n
;
2612 else if (g_strcasecmp(name
, (gchar
*)"pinentry") == 0) {
2614 gint n
= strtol(value
, &p
, 10);
2616 if (*p
|| n
< 0 || n
> 1)
2617 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2619 client
->pinentry
->enable
= n
== 0 ? FALSE
: TRUE
;
2623 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2626 log_write("OPTION %s=%s", name
, value
);
2630 gpg_error_t
register_commands(assuan_context_t ctx
)
2634 gint (*handler
)(assuan_context_t
, gchar
*line
);
2636 { "OPEN", open_command
},
2637 { "SAVE", save_command
},
2638 { "LIST", list_command
},
2639 { "REALPATH", realpath_command
},
2640 { "STORE", store_command
},
2641 { "DELETE", delete_command
},
2642 { "GET", get_command
},
2643 { "ATTR", attr_command
},
2644 { "ISCACHED", iscached_command
},
2645 { "CLEARCACHE", clearcache_command
},
2646 { "CACHETIMEOUT", cachetimeout_command
},
2647 { "GETCONFIG", getconfig_command
},
2648 { "DUMP", dump_command
},
2649 { "XPATH", xpath_command
},
2650 { "IMPORT", import_command
},
2651 { "LOCK", lock_command
},
2652 { "UNLOCK", unlock_command
},
2653 { "GETPID", getpid_command
},
2660 for (i
=0; table
[i
].name
; i
++) {
2661 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2667 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2672 rc
= assuan_register_option_handler(ctx
, option_handler
);
2677 return assuan_register_reset_notify(ctx
, reset_notify
);
2680 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2681 guchar
*key
, gint
*dst_iter
)
2686 guchar tkey
[gcrykeysize
];
2687 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2688 gcry_cipher_hd_t gh
;
2689 guint iter
= 0, n_iter
= 0;
2691 void *outbuf
= NULL
;
2695 file_header_t file_header
;
2696 gchar str
[ASSUAN_LINELENGTH
];
2699 rc
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2707 lseek(fd
, 0, SEEK_SET
);
2708 insize
= st
.st_size
- sizeof(file_header_t
);
2709 iv
= gcry_malloc(gcryblocksize
);
2713 gcry_cipher_close(gh
);
2715 return gpg_error_from_errno(ENOMEM
);
2718 len
= pth_read(fd
, &file_header
, sizeof(file_header_t
));
2720 if (len
!= sizeof(file_header_t
)) {
2724 gcry_cipher_close(gh
);
2728 return gpg_error_from_errno(errno
);
2731 *dst_iter
= file_header
.iter
;
2733 /* No encryption iterations. This is a plain (gzipped) file. */
2734 if (file_header
.iter
== -1) {
2736 * cache_file_count() needs both .used == TRUE and a valid key in
2737 * order for it to count as a used cache entry. Fixes CACHE status
2740 memset(key
, '!', gcrykeysize
);
2741 insize
= st
.st_size
- sizeof(file_header_t
);
2744 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2745 inbuf
= gcry_malloc(insize
);
2749 gcry_cipher_close(gh
);
2752 return gpg_error_from_errno(ENOMEM
);
2755 len
= pth_read(fd
, inbuf
, insize
);
2757 if (len
!= insize
) {
2761 gcry_cipher_close(gh
);
2765 return gpg_error_from_errno(errno
);
2768 if (file_header
.iter
== -1)
2771 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2773 gcry_cipher_close(gh
);
2774 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2777 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2784 if ((rc
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2786 gcry_cipher_close(gh
);
2787 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2790 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2796 gcry_cipher_close(gh
);
2801 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
2803 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2804 rc
= assuan_write_status(client
->ctx
, "DECRYPT",
2805 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
2814 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2822 memcpy(tkey
, key
, sizeof(tkey
));
2825 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2827 gcry_cipher_close(gh
);
2828 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2831 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2833 memset(tkey
, 0, sizeof(tkey
));
2839 memset(tkey
, 0, sizeof(tkey
));
2841 while (iter
< file_header
.iter
) {
2842 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2843 if (!(iter
% iter_progress
)) {
2844 rc
= assuan_write_status(ctx
, "DECRYPT",
2845 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
2855 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2857 gcry_cipher_close(gh
);
2858 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2861 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2868 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2872 gcry_cipher_close(gh
);
2873 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2876 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2887 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2888 rc
= assuan_write_status(ctx
, "DECRYPT",
2889 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
2901 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
2903 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2905 if (zrc
== Z_MEM_ERROR
) {
2907 return gpg_error_from_errno(ENOMEM
);
2909 else if (zrc
!= Z_DATA_ERROR
) {
2913 gcry_cipher_close(gh
);
2915 return EPWMD_BADKEY
;
2924 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2928 gcry_cipher_close(gh
);
2930 return EPWMD_BADKEY
;
2934 client
->xml
= inbuf
;
2935 client
->len
= insize
;
2938 gcry_cipher_close(gh
);
2946 * This is called after every Assuan command.
2948 void command_finalize(assuan_context_t ctx
, gint rc
)
2950 struct client_s
*client
= assuan_get_pointer(ctx
);
2952 if (!client
->is_lock_cmd
)
2953 unlock_file_mutex(client
);