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
;
448 if (!rc
&& iter
!= get_key_file_integer(client
->filename
, "iterations")) {
449 g_key_file_set_integer(keyfileh
, client
->filename
, "iterations", iter
);
450 send_status_all(STATUS_CONFIG
);
453 return send_error(ctx
, rc
);
456 static int open_command(assuan_context_t ctx
, char *line
)
459 guchar shakey
[gcrykeysize
];
460 gboolean cached
= FALSE
;
462 struct client_s
*client
= assuan_get_pointer(ctx
);
464 gchar
*filename
= NULL
;
465 file_header_t file_header
;
467 memset(shakey
, 0, sizeof(shakey
));
469 if ((req
= split_input_line(line
, " ", 2)) != NULL
)
472 if (!filename
|| !*filename
) {
474 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
477 if (valid_filename(filename
) == FALSE
) {
479 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
482 if (client
->state
== STATE_OPEN
)
483 cleanup_client(client
);
485 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
486 CACHE_LOCK(client
->ctx
);
488 if (cache_has_file(client
->md5file
) == FALSE
) {
489 if (cache_add_file(client
->md5file
, NULL
) == FALSE
) {
492 return send_error(ctx
, EPWMD_MAX_SLOTS
);
496 cache_incr_refcount(client
->md5file
);
498 rc
= lock_file_mutex(client
);
502 return send_error(ctx
, rc
);
505 client
->freed
= FALSE
;
507 if ((rc
= gcry_cipher_open(&client
->gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0))) {
509 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
510 cleanup_client(client
);
511 return send_error(ctx
, rc
);
514 if (stat(filename
, &st
) == 0) {
515 if (!S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
516 log_write("%s: %s", filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
518 cleanup_client(client
);
519 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
522 client
->mtime
= st
.st_mtime
;
525 client
->filename
= g_strdup(filename
);
527 if (!client
->filename
) {
528 memset(shakey
, 0, sizeof(shakey
));
529 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
530 cleanup_client(client
);
532 return send_syserror(ctx
, ENOMEM
);
536 client
->pinentry
->filename
= g_strdup(client
->filename
);
538 if (!client
->pinentry
->filename
) {
539 memset(shakey
, 0, sizeof(shakey
));
540 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
541 cleanup_client(client
);
543 return send_syserror(ctx
, ENOMEM
);
548 * New files don't need a key.
550 if (access(filename
, R_OK
) != 0) {
551 if (errno
!= ENOENT
) {
553 log_write("%s: %s", filename
, strerror(errno
));
555 cleanup_client(client
);
556 return send_syserror(ctx
, rc
);
559 if ((client
->xml
= new_document()) == NULL
) {
560 log_write("%s", strerror(ENOMEM
));
562 cleanup_client(client
);
563 return send_syserror(ctx
, ENOMEM
);
566 client
->len
= xmlStrlen(client
->xml
);
568 client
->filename
= g_strdup(filename
);
570 if (!client
->filename
) {
572 cleanup_client(client
);
573 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
574 return send_syserror(ctx
, ENOMEM
);
577 memset(shakey
, 0, sizeof(shakey
));
579 if (req
[1] && *req
[1])
580 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
583 return open_command_finalize(ctx
, shakey
, cached
);
586 rc
= read_file_header(filename
, &file_header
);
590 cleanup_client(client
);
591 return send_error(ctx
, rc
);
594 if (file_header
.iter
== -1)
597 CACHE_LOCK(client
->ctx
);
598 cached
= cache_get_key(client
->md5file
, shakey
);
601 if (cached
== FALSE
) {
603 * No key specified and no matching filename found in the cache. Use
604 * pinentry to retrieve the key. Cannot return assuan_process_done()
605 * here otherwise the command will be interrupted. The event loop in
606 * client_thread() will poll the file descriptor waiting for it to
607 * become ready to read a pinentry_key_s which will contain the
608 * entered key or rc. It will then call open_command_finalize() to
609 * to finish the command.
611 if (!req
[1] || !*req
[1]) {
613 gboolean b
= get_key_file_boolean(filename
, "enable_pinentry");
615 /* From set_pinentry_defaults(). */
616 if (client
->pinentry
->enable
== FALSE
||
617 (client
->pinentry
->enable
== -1 && b
== FALSE
)) {
618 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
623 lock_pin_mutex(client
);
624 client
->pinentry
->which
= PINENTRY_OPEN
;
625 rc
= pinentry_fork(ctx
);
628 unlock_pin_mutex(client
->pinentry
);
629 cleanup_client(client
);
630 return send_error(ctx
, rc
);
633 client
->pinentry
->cb
= open_command_finalize
;
634 client
->pinentry
->status
= PINENTRY_INIT
;
637 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
642 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, req
[1], strlen(req
[1]));
647 return open_command_finalize(ctx
, shakey
, cached
);
650 gboolean
do_compress(assuan_context_t ctx
, gint level
, gpointer data
,
651 gint size
, gpointer
*out
, glong
*outsize
, gint
*rc
)
657 gint cmd
= Z_NO_FLUSH
;
658 gchar str
[ASSUAN_LINELENGTH
];
662 z
.next_in
= pin
= data
;
663 z
.avail_in
= size
< zlib_bufsize
? size
: zlib_bufsize
;
664 z
.avail_out
= zlib_bufsize
;
665 z
.next_out
= pout
= g_malloc(zlib_bufsize
);
668 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, strerror(ENOMEM
));
673 *rc
= deflateInit2(&z
, level
, Z_DEFLATED
, 31, 8, Z_DEFAULT_STRATEGY
);
676 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
681 memset(&h
, 0, sizeof(gz_header
));
682 g_snprintf(buf
, sizeof(buf
), "%i", size
);
683 h
.comment
= (guchar
*)buf
;
684 *rc
= deflateSetHeader(&z
, &h
);
687 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
696 *rc
= deflate(&z
, cmd
);
703 p
= g_realloc(pout
, z
.total_out
+ zlib_bufsize
);
711 z
.next_out
= pout
+ z
.total_out
;
712 z
.avail_out
= zlib_bufsize
;
715 if (!z
.avail_in
&& z
.total_in
< size
) {
716 if (z
.total_in
+ zlib_bufsize
> size
)
717 z
.avail_in
= size
- z
.total_in
;
719 z
.avail_in
= zlib_bufsize
;
722 *rc
= assuan_write_status(ctx
, "COMPRESS",
723 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
730 if (z
.total_in
>= size
)
741 } while (*rc
!= Z_STREAM_END
);
744 *rc
= assuan_write_status(ctx
, "COMPRESS",
745 print_fmt(str
, sizeof(str
), "%i %i", z
.total_in
, size
));
752 *outsize
= z
.total_out
;
758 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, z
.msg
);
764 gpg_error_t
do_xml_encrypt(struct client_s
*client
, gcry_cipher_hd_t gh
,
765 const gchar
*filename
, gpointer data
, size_t insize
, guchar
*shakey
,
771 guchar tkey
[gcrykeysize
];
774 guint iter_progress
= 0, n_iter
= 0, xiter
= 0;
775 gchar tmp
[FILENAME_MAX
];
778 file_header_t file_header
;
779 gchar str
[ASSUAN_LINELENGTH
];
783 * cache_file_count() needs both .used == TRUE and a valid key in
784 * order for it to count as a used cache entry. Fixes CACHE status
787 memset(shakey
, '!', gcrykeysize
);
789 file_header
.iter
= iter
;
793 if (insize
/ gcryblocksize
) {
794 len
= (insize
/ gcryblocksize
) * gcryblocksize
;
796 if (insize
% gcryblocksize
)
797 len
+= gcryblocksize
;
801 * Resize the existing xml buffer to the block size required by gcrypt
802 * rather than duplicating it and wasting memory.
804 inbuf
= gcry_realloc(data
, len
);
807 return gpg_error_from_errno(ENOMEM
);
810 gcry_create_nonce(file_header
.iv
, sizeof(file_header
.iv
));
811 memcpy(tkey
, shakey
, sizeof(tkey
));
814 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
815 memset(tkey
, 0, sizeof(tkey
));
817 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
821 memset(tkey
, 0, sizeof(tkey
));
822 file_header
.iter
= iter
;
825 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
827 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
828 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
829 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
837 while (xiter
< file_header
.iter
) {
838 if (client
&& iter_progress
> 0 && xiter
>= iter_progress
) {
839 if (!(xiter
% iter_progress
)) {
840 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
841 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
850 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
851 sizeof(file_header
.iv
)))) {
853 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
857 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0)
860 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
868 if ((rc
= gcry_cipher_setiv(gh
, file_header
.iv
,
869 sizeof(file_header
.iv
)))) {
871 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
875 if ((rc
= gcry_cipher_setkey(gh
, shakey
, gcrykeysize
))) {
877 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
881 if (encrypt_xml(gh
, inbuf
, insize
, NULL
, 0) == FALSE
) {
883 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
887 if (client
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
888 rc
= assuan_write_status(client
->ctx
, "ENCRYPT",
889 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
899 if (stat(filename
, &st
) == 0) {
900 mode
= st
.st_mode
& (S_IRWXU
|S_IRWXG
|S_IRWXO
);
903 * FIXME What if the file has an ACL?
905 if (!(mode
& S_IWUSR
)) {
907 return gpg_error_from_errno(EACCES
);
911 if (errno
!= ENOENT
) {
914 return gpg_error_from_errno(rc
);
918 g_snprintf(tmp
, sizeof(tmp
), ".%s.tmp", filename
);
920 if ((fd
= open(tmp
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0600)) == -1) {
923 p
= strrchr(tmp
, '/');
925 log_write("%s: %s", p
, strerror(rc
));
926 return gpg_error_from_errno(rc
);
931 * xml_import() from command line.
935 len
= pth_write(fd
, &file_header
, sizeof(file_header_t
));
937 if (len
!= sizeof(file_header
)) {
946 return gpg_error_from_errno(len
);
949 len
= pth_write(fd
, inbuf
, insize
);
960 return gpg_error_from_errno(len
);
963 if (fsync(fd
) == -1) {
972 return gpg_error_from_errno(len
);
978 if (mode
&& get_key_file_boolean(filename
, "backup") == TRUE
) {
979 gchar tmp2
[FILENAME_MAX
];
981 g_snprintf(tmp2
, sizeof(tmp2
), "%s.backup", filename
);
983 if (rename(filename
, tmp2
) == -1) {
987 return gpg_error_from_errno(len
);
991 if (rename(tmp
, filename
) == -1) {
995 return gpg_error_from_errno(len
);
999 chmod(filename
, mode
);
1006 static gpg_error_t
save_command_finalize(assuan_context_t ctx
,
1007 guchar shakey
[], gboolean cached
)
1009 struct client_s
*client
= assuan_get_pointer(ctx
);
1021 xmlDocDumpFormatMemory(client
->doc
, &p
, &len
, 0);
1024 iter
= get_key_file_integer(client
->filename
, "compression_level");
1029 if (do_compress(ctx
, iter
, xmlbuf
, len
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
1030 memset(shakey
, 0, sizeof(shakey
));
1033 if (zrc
== Z_MEM_ERROR
) {
1034 return send_syserror(ctx
, ENOMEM
);
1037 return send_error(ctx
, GPG_ERR_COMPR_ALGO
);
1045 iter
= get_key_file_integer(client
->filename
, "iterations");
1046 rc
= do_xml_encrypt(client
, client
->gh
, client
->filename
, xmlbuf
, len
, shakey
, iter
);
1049 memset(shakey
, 0, sizeof(shakey
));
1050 return send_error(ctx
, rc
);
1053 stat(client
->filename
, &st
);
1054 client
->mtime
= st
.st_mtime
;
1055 timeout
= get_key_file_integer(client
->filename
, "cache_timeout");
1056 CACHE_LOCK(client
->ctx
);
1059 memset(shakey
, 0, sizeof(shakey
));
1060 cache_reset_timeout(client
->md5file
, timeout
);
1063 if (client
->new == TRUE
)
1064 send_status_all(STATUS_CACHE
);
1066 client
->new = FALSE
;
1067 return send_error(ctx
, 0);
1070 if (cache_update_key(client
->md5file
, shakey
) == FALSE
) {
1071 memset(shakey
, 0, sizeof(shakey
));
1072 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(EPWMD_MAX_SLOTS
));
1074 return send_error(ctx
, EPWMD_MAX_SLOTS
);
1077 client
->new = FALSE
;
1078 memset(shakey
, 0, sizeof(shakey
));
1079 cache_reset_timeout(client
->md5file
, timeout
);
1081 send_status_all(STATUS_CACHE
);
1082 return send_error(ctx
, 0);
1085 static int save_command(assuan_context_t ctx
, char *line
)
1087 gboolean cached
= FALSE
;
1088 guchar shakey
[gcrykeysize
];
1090 struct client_s
*client
= assuan_get_pointer(ctx
);
1093 memset(shakey
, 0, sizeof(shakey
));
1094 rc
= file_modified(client
);
1097 log_write("%s: %s", client
->filename
? client
->filename
: "",
1099 return send_error(ctx
, rc
);
1102 rc
= lock_file_mutex(client
);
1105 return send_error(ctx
, rc
);
1107 if (stat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1108 return send_syserror(ctx
, errno
);
1110 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
) && !S_ISLNK(st
.st_mode
)) {
1111 log_write("%s: %s", client
->filename
, pwmd_strerror(EPWMD_INVALID_FILENAME
));
1112 return send_error(ctx
, EPWMD_INVALID_FILENAME
);
1115 if (get_key_file_integer(client
->filename
, "iterations") == -1)
1118 if (!line
|| !*line
) {
1119 guchar tmp
[sizeof(shakey
)];
1122 memset(tmp
, '!', sizeof(tmp
));
1124 if (cache_get_key(client
->md5file
, shakey
) == FALSE
||
1125 memcmp(shakey
, tmp
, sizeof(shakey
)) == 0) {
1127 #ifdef WITH_PINENTRY
1128 if (get_key_file_boolean(client
->filename
, "enable_pinentry") == FALSE
) {
1129 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1133 lock_pin_mutex(client
);
1134 client
->pinentry
->which
= PINENTRY_SAVE
;
1135 rc
= pinentry_fork(ctx
);
1138 unlock_pin_mutex(client
->pinentry
);
1139 return send_error(ctx
, rc
);
1142 client
->pinentry
->cb
= save_command_finalize
;
1143 client
->pinentry
->status
= PINENTRY_INIT
;
1146 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, "", 1);
1156 gcry_md_hash_buffer(GCRY_MD_SHA256
, shakey
, line
, strlen(line
));
1157 memset(line
, 0, strlen(line
));
1161 return save_command_finalize(ctx
, shakey
, cached
);
1164 static int delete_command(assuan_context_t ctx
, char *line
)
1166 struct client_s
*client
= assuan_get_pointer(ctx
);
1171 rc
= file_modified(client
);
1174 log_write("%s: %s", client
->filename
? client
->filename
: "",
1176 return send_error(ctx
, rc
);
1179 if (strchr(line
, '\t'))
1180 req
= split_input_line(line
, "\t", -1);
1182 req
= split_input_line(line
, " ", -1);
1185 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1187 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1191 return send_error(ctx
, rc
);
1195 * No sub-node defined. Remove the entire node (account).
1204 return send_error(ctx
, 0);
1207 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1211 return send_error(ctx
, rc
);
1218 return send_error(ctx
, 0);
1222 * Don't return with assuan_process_done() here. This has been called from
1223 * assuan_process_next() and the command should be finished in
1226 static int store_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
1229 assuan_context_t ctx
= data
;
1230 struct client_s
*client
= assuan_get_pointer(ctx
);
1233 gpg_error_t rc
= file_modified(client
);
1235 if (assuan_rc
|| rc
) {
1242 return assuan_rc
? assuan_rc
: rc
;
1245 req
= split_input_line((gchar
*)line
, "\t", 0);
1253 return EPWMD_COMMAND_SYNTAX
;
1255 if (valid_xml_element((xmlChar
*)*req
) == FALSE
) {
1257 return EPWMD_INVALID_ELEMENT
;
1260 if (valid_element_path(req
+1, TRUE
) == FALSE
) {
1262 return EPWMD_INVALID_ELEMENT
;
1266 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1268 if (rc
&& rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1269 rc
= new_account(client
->doc
, *req
);
1286 create_elements_cb(n
, req
+1, &rc
, NULL
);
1288 find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1289 NULL
, NULL
, create_elements_cb
, FALSE
, 0, NULL
);
1293 client
->inquire_status
= INQUIRE_DONE
;
1297 static int store_command(assuan_context_t ctx
, char *line
)
1299 struct client_s
*client
= assuan_get_pointer(ctx
);
1300 gpg_error_t rc
= file_modified(client
);
1303 log_write("%s: %s", client
->filename
? client
->filename
: "",
1305 return send_error(ctx
, rc
);
1308 rc
= assuan_inquire_ext(ctx
, "STORE", 0, store_command_finalize
, ctx
);
1311 return send_error(ctx
, rc
);
1313 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
1314 client
->inquire_status
= INQUIRE_BUSY
;
1318 static int get_command(assuan_context_t ctx
, char *line
)
1320 struct client_s
*client
= assuan_get_pointer(ctx
);
1325 rc
= file_modified(client
);
1328 log_write("%s: %s", client
->filename
? client
->filename
: "",
1330 return send_error(ctx
, rc
);
1333 req
= split_input_line(line
, "\t", -1);
1335 if (!req
|| !*req
) {
1337 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1340 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1344 return send_error(ctx
, rc
);
1348 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1353 return send_error(ctx
, rc
);
1355 if (!n
|| !n
->children
)
1356 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1358 n
= find_text_node(n
->children
);
1360 if (!n
|| !n
->content
|| !*n
->content
)
1361 return send_error(ctx
, EPWMD_EMPTY_ELEMENT
);
1363 rc
= assuan_send_data(ctx
, n
->content
, xmlStrlen(n
->content
));
1364 return send_error(ctx
, rc
);
1367 static xmlNodePtr
realpath_elements_cb(xmlNodePtr node
, gchar
**target
,
1368 gpg_error_t
*rc
, gchar
**req_orig
, void *data
)
1370 gchar
*path
= *(gchar
**)data
;
1371 gchar
*tmp
= NULL
, *result
;
1375 *(gchar
**)data
= NULL
;
1378 path
= g_strjoinv("\t", target
);
1381 *rc
= gpg_error_from_errno(ENOMEM
);
1386 tmp
= g_strjoinv("\t", req_orig
);
1390 *rc
= gpg_error_from_errno(ENOMEM
);
1396 result
= g_strdup_printf("%s\t%s", path
, tmp
);
1398 result
= g_strdup(path
);
1401 *rc
= gpg_error_from_errno(ENOMEM
);
1409 *(gchar
**)data
= result
;
1413 static int realpath_command(assuan_context_t ctx
, char *line
)
1416 struct client_s
*client
= assuan_get_pointer(ctx
);
1424 rc
= file_modified(client
);
1427 log_write("%s: %s", client
->filename
? client
->filename
: "",
1429 return send_error(ctx
, rc
);
1432 if (strchr(line
, '\t') != NULL
) {
1433 if ((req
= split_input_line(line
, "\t", 0)) == NULL
)
1434 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1437 if ((req
= split_input_line(line
, " ", 0)) == NULL
)
1438 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
1441 n
= find_account(client
->doc
, &req
, &rc
, NULL
, 0);
1445 return send_error(ctx
, rc
);
1448 rp
= g_strjoinv("\t", req
);
1452 return send_syserror(ctx
, ENOMEM
);
1456 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
1457 NULL
, realpath_elements_cb
, NULL
, FALSE
, 0, &rp
);
1462 return send_error(ctx
, rc
);
1466 string
= g_string_new(rp
);
1471 return send_syserror(ctx
, ENOMEM
);
1474 for (i
= 0, t
= string
->str
+ i
; *t
; t
++, i
++) {
1475 if ((!i
&& *t
!= '!') || (*t
== '\t' && *(t
+1) && *(t
+1) != '!')) {
1476 string
= g_string_insert_c(string
, !i
? i
++ : ++i
, '!');
1481 rc
= assuan_send_data(ctx
, string
->str
, string
->len
);
1482 g_string_free(string
, TRUE
);
1483 return send_error(ctx
, rc
);
1486 static int list_command(assuan_context_t ctx
, char *line
)
1488 struct client_s
*client
= assuan_get_pointer(ctx
);
1490 struct element_list_s
*elements
= NULL
;
1493 if (disable_list_and_dump
== TRUE
)
1494 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1496 rc
= file_modified(client
);
1499 log_write("%s: %s", client
->filename
, pwmd_strerror(rc
));
1500 return send_error(ctx
, rc
);
1506 rc
= list_accounts(client
->doc
, &str
);
1509 return send_error(ctx
, rc
);
1511 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1512 g_string_free(str
, TRUE
);
1513 return send_error(ctx
, rc
);
1516 elements
= g_malloc0(sizeof(struct element_list_s
));
1519 rc
= gpg_err_code_from_errno(ENOMEM
);
1523 rc
= create_path_list(client
->doc
, elements
, line
);
1529 gint total
= g_slist_length(elements
->list
);
1534 rc
= EPWMD_EMPTY_ELEMENT
;
1538 str
= g_string_new(NULL
);
1541 rc
= gpg_err_code_from_errno(ENOMEM
);
1545 for (i
= 0; i
< total
; i
++) {
1546 tmp
= g_slist_nth_data(elements
->list
, i
);
1547 g_string_append_printf(str
, "%s%s", tmp
, i
+1 == total
? "" : "\n");
1550 rc
= assuan_send_data(ctx
, str
->str
, str
->len
);
1551 g_string_free(str
, TRUE
);
1554 rc
= EPWMD_EMPTY_ELEMENT
;
1558 gint total
= g_slist_length(elements
->list
);
1561 for (i
= 0; i
< total
; i
++) {
1562 tmp
= g_slist_nth_data(elements
->list
, i
);
1566 g_slist_free(elements
->list
);
1568 if (elements
->prefix
)
1569 g_free(elements
->prefix
);
1574 return send_error(ctx
, rc
);
1577 static gpg_error_t
add_attribute(xmlNodePtr node
, const gchar
*name
,
1582 if ((a
= xmlHasProp(node
, (xmlChar
*)name
)) == NULL
) {
1583 a
= xmlNewProp(node
, (xmlChar
*)name
, (xmlChar
*)value
);
1586 return EPWMD_LIBXML_ERROR
;
1589 xmlNodeSetContent(a
->children
, (xmlChar
*)value
);
1595 * req[0] - element path
1597 static int attribute_list(assuan_context_t ctx
, gchar
**req
)
1599 struct client_s
*client
= assuan_get_pointer(ctx
);
1600 gchar
**attrlist
= NULL
;
1602 gchar
**path
= NULL
;
1608 if (!req
|| !req
[0])
1609 return EPWMD_COMMAND_SYNTAX
;
1611 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1613 * The first argument may be only an account.
1615 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
1616 return EPWMD_COMMAND_SYNTAX
;
1619 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1627 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1628 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1638 for (a
= n
->properties
; a
; a
= a
->next
) {
1641 if ((pa
= g_realloc(attrlist
, (i
+ 2) * sizeof(gchar
*))) == NULL
) {
1643 g_strfreev(attrlist
);
1645 log_write("%s(%i): %s", __FILE__
, __LINE__
, strerror(ENOMEM
));
1646 return gpg_error_from_errno(ENOMEM
);
1651 attrlist
[i
] = g_strdup_printf("%s\t%s", (gchar
*)a
->name
, (gchar
*)an
->content
);
1654 g_strfreev(attrlist
);
1655 return gpg_error_from_errno(ENOMEM
);
1658 attrlist
[++i
] = NULL
;
1662 return EPWMD_EMPTY_ELEMENT
;
1664 line
= g_strjoinv("\n", attrlist
);
1667 g_strfreev(attrlist
);
1668 return gpg_error_from_errno(ENOMEM
);
1671 rc
= assuan_send_data(ctx
, line
, strlen(line
));
1673 g_strfreev(attrlist
);
1678 * req[0] - attribute
1679 * req[1] - element path
1681 static int attribute_delete(struct client_s
*client
, gchar
**req
)
1685 gchar
**path
= NULL
;
1688 if (!req
|| !req
[0] || !req
[1])
1689 return EPWMD_COMMAND_SYNTAX
;
1691 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1693 * The first argument may be only an account.
1695 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1696 return EPWMD_COMMAND_SYNTAX
;
1700 * Don't remove the "name" attribute for the account element. To remove an
1701 * account use DELETE <account>.
1703 if (!path
[1] && xmlStrEqual((xmlChar
*)req
[0], (xmlChar
*)"name")) {
1704 rc
= EPWMD_ATTR_SYNTAX
;
1708 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1714 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1715 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1723 if ((a
= xmlHasProp(n
, (xmlChar
*)req
[0])) == NULL
)
1724 return EPWMD_ATTR_NOT_FOUND
;
1726 if (xmlRemoveProp(a
) == -1)
1727 return EPWMD_LIBXML_ERROR
;
1736 static xmlNodePtr
create_element_path(struct client_s
*client
, gchar
***path
,
1739 gchar
**src
= *path
;
1740 gchar
**src_orig
= g_strdupv(src
);
1741 xmlNodePtr n
= NULL
;
1746 *rc
= gpg_error_from_errno(ENOMEM
);
1751 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1754 if (*rc
== EPWMD_ELEMENT_NOT_FOUND
) {
1755 *rc
= new_account(client
->doc
, src
[0]);
1768 n
= create_target_elements_cb(n
, src
+1, rc
, NULL
);
1770 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1771 NULL
, NULL
, create_target_elements_cb
, FALSE
, 0, NULL
);
1777 * Reset the position of the element tree now that the elements
1778 * have been created.
1783 n
= find_account(client
->doc
, &src
, rc
, NULL
, 0);
1788 n
= find_elements(client
->doc
, n
->children
, src
+1, rc
,
1789 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1797 g_strfreev(src_orig
);
1804 * Creates a "target" attribute. When other commands encounter an element with
1805 * this attribute, the element path is modified to the target value. If the
1806 * source element path doesn't exist when using 'ATTR SET target', it is
1807 * created, but the destination element path must exist.
1809 * req[0] - source element path
1810 * req[1] - destination element path
1812 static gpg_error_t
target_attribute(struct client_s
*client
, gchar
**req
)
1814 gchar
**src
, **dst
, *line
= NULL
;
1818 if (!req
|| !req
[0] || !req
[1])
1819 return EPWMD_COMMAND_SYNTAX
;
1821 if ((src
= split_input_line(req
[0], "\t", 0)) == NULL
) {
1823 * The first argument may be only an account.
1825 if ((src
= split_input_line(req
[0], " ", 0)) == NULL
)
1826 return EPWMD_COMMAND_SYNTAX
;
1829 if (valid_element_path(src
, FALSE
) == FALSE
) {
1831 return EPWMD_INVALID_ELEMENT
;
1834 if ((dst
= split_input_line(req
[1], "\t", 0)) == NULL
) {
1836 * The first argument may be only an account.
1838 if ((dst
= split_input_line(req
[1], " ", 0)) == NULL
) {
1839 rc
= EPWMD_COMMAND_SYNTAX
;
1844 n
= find_account(client
->doc
, &dst
, &rc
, NULL
, 0);
1847 * Make sure the destination element path exists.
1853 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
1854 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1860 n
= create_element_path(client
, &src
, &rc
);
1865 line
= g_strjoinv("\t", dst
);
1868 rc
= gpg_error_from_errno(ENOMEM
);
1872 rc
= add_attribute(n
, "target", line
);
1882 * req[0] - account name
1885 static gpg_error_t
name_attribute(struct client_s
*client
, gchar
**req
)
1891 tmp
= g_strdupv(req
);
1894 return gpg_error_from_errno(ENOMEM
);
1896 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1902 if (g_utf8_collate(req
[0], req
[1]) == 0)
1906 * Will not overwrite an existing account.
1908 tmp
= g_strdupv(req
+1);
1911 return gpg_error_from_errno(ENOMEM
);
1913 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1916 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
)
1920 return EPWMD_ACCOUNT_EXISTS
;
1923 * Whitespace not allowed in account names.
1925 if (contains_whitespace(req
[1]) == TRUE
)
1926 return EPWMD_ATTR_SYNTAX
;
1928 tmp
= g_strdupv(req
);
1931 return gpg_error_from_errno(ENOMEM
);
1933 n
= find_account(client
->doc
, &tmp
, &rc
, NULL
, 0);
1937 return EPWMD_ELEMENT_NOT_FOUND
;
1939 return add_attribute(n
, "name", req
[1]);
1943 * req[0] - attribute
1944 * req[1] - element path
1946 static int attribute_get(assuan_context_t ctx
, gchar
**req
)
1948 struct client_s
*client
= assuan_get_pointer(ctx
);
1954 if (!req
|| !req
[0] || !req
[1])
1955 return EPWMD_COMMAND_SYNTAX
;
1957 if (strchr(req
[1], '\t')) {
1958 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
)
1959 return EPWMD_COMMAND_SYNTAX
;
1962 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
1963 return EPWMD_COMMAND_SYNTAX
;
1966 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
1972 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1973 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
1981 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
1982 return EPWMD_ATTR_NOT_FOUND
;
1984 rc
= assuan_send_data(ctx
, a
, xmlStrlen(a
));
1994 * req[0] - attribute
1995 * req[1] - element path
1998 static int attribute_set(struct client_s
*client
, gchar
**req
)
2000 gchar
**path
= NULL
;
2004 if (!req
|| !req
[0] || !req
[1] || !req
[2])
2005 return EPWMD_COMMAND_SYNTAX
;
2008 * Reserved attribute names.
2010 if (g_utf8_collate(req
[0], "name") == 0) {
2012 * Only reserved for the account element. Not the rest of the
2015 if (strchr(req
[1], '\t') == NULL
)
2016 return name_attribute(client
, req
+ 1);
2018 else if (g_utf8_collate(req
[0], "target") == 0)
2019 return target_attribute(client
, req
+ 1);
2021 if ((path
= split_input_line(req
[1], "\t", 0)) == NULL
) {
2023 * The first argument may be only an account.
2025 if ((path
= split_input_line(req
[1], " ", 0)) == NULL
)
2026 return EPWMD_COMMAND_SYNTAX
;
2029 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2035 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
2036 NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2043 return add_attribute(n
, req
[0], req
[2]);
2052 * req[1] - attribute name or element path if command is LIST
2053 * req[2] - element path
2054 * req[2] - element path or value
2056 static int attr_command(assuan_context_t ctx
, char *line
)
2058 struct client_s
*client
= assuan_get_pointer(ctx
);
2062 rc
= file_modified(client
);
2065 log_write("%s: %s", client
->filename
? client
->filename
: "",
2067 return send_error(ctx
, rc
);
2070 req
= split_input_line(line
, " ", 4);
2072 if (!req
|| !req
[0] || !req
[1]) {
2074 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2077 if (g_ascii_strcasecmp(req
[0], "SET") == 0)
2078 rc
= attribute_set(client
, req
+1);
2079 else if (g_ascii_strcasecmp(req
[0], "GET") == 0)
2080 rc
= attribute_get(ctx
, req
+1);
2081 else if (g_ascii_strcasecmp(req
[0], "DELETE") == 0)
2082 rc
= attribute_delete(client
, req
+1);
2083 else if (g_ascii_strcasecmp(req
[0], "LIST") == 0)
2084 rc
= attribute_list(ctx
, req
+1);
2086 rc
= EPWMD_COMMAND_SYNTAX
;
2089 return send_error(ctx
, rc
);
2092 static int iscached_command(assuan_context_t ctx
, char *line
)
2094 gchar
**req
= split_input_line(line
, " ", 0);
2097 if (!req
|| !*req
) {
2099 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2102 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2106 if (cache_iscached(md5file
) == FALSE
) {
2108 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2112 return send_error(ctx
, 0);
2115 static int clearcache_command(assuan_context_t ctx
, char *line
)
2117 struct client_s
*client
= assuan_get_pointer(ctx
);
2118 gchar
**req
= split_input_line(line
, " ", 0);
2123 if (!req
|| !*req
) {
2125 cache_clear(client
->md5file
, 2);
2127 return send_error(ctx
, 0);
2130 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2133 if (cache_clear(md5file
, 1) == FALSE
) {
2135 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2139 return send_error(ctx
, 0);
2142 static int cachetimeout_command(assuan_context_t ctx
, char *line
)
2146 gchar
**req
= split_input_line(line
, " ", 0);
2148 struct client_s
*client
= assuan_get_pointer(ctx
);
2150 if (!req
|| !*req
|| !req
[1]) {
2152 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2156 timeout
= strtol(req
[0], &p
, 10);
2158 if (errno
!= 0 || *p
!= 0) {
2160 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2163 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[1], strlen(req
[1]));
2165 CACHE_LOCK(client
->ctx
);
2167 if (cache_set_timeout(md5file
, timeout
) == FALSE
) {
2169 return send_error(ctx
, EPWMD_CACHE_NOT_FOUND
);
2173 return send_error(ctx
, 0);
2176 static int dump_command(assuan_context_t ctx
, char *line
)
2180 struct client_s
*client
= assuan_get_pointer(ctx
);
2183 if (disable_list_and_dump
== TRUE
)
2184 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2186 rc
= file_modified(client
);
2189 log_write("%s: %s", client
->filename
? client
->filename
: "",
2191 return send_error(ctx
, rc
);
2194 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2197 return send_syserror(ctx
, ENOMEM
);
2199 rc
= assuan_send_data(ctx
, xml
, len
);
2201 return send_error(ctx
, rc
);
2204 static int getconfig_command(assuan_context_t ctx
, gchar
*line
)
2206 struct client_s
*client
= assuan_get_pointer(ctx
);
2208 gchar filename
[255]={0}, param
[747]={0};
2209 gchar
*p
, *tmp
, *fp
= client
->filename
, *paramp
= line
;
2211 if (strchr(line
, ' ')) {
2212 sscanf(line
, " %254[a-zA-Z] %746c", filename
, param
);
2217 if (strcmp(line
, "key") == 0 || strcmp(line
, "key_file") == 0)
2218 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2220 p
= get_key_file_string(fp
? fp
: "global", paramp
);
2223 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2225 tmp
= expand_homedir(p
);
2229 return send_syserror(ctx
, ENOMEM
);
2234 rc
= assuan_send_data(ctx
, p
, strlen(p
));
2236 return send_error(ctx
, rc
);
2239 static int xpath_command(assuan_context_t ctx
, gchar
*line
)
2241 struct client_s
*client
= assuan_get_pointer(ctx
);
2243 xmlXPathContextPtr xp
;
2244 xmlXPathObjectPtr result
;
2245 xmlBufferPtr buf
= NULL
;
2248 rc
= file_modified(client
);
2251 log_write("%s: %s", client
->filename
? client
->filename
: "",
2253 return send_error(ctx
, rc
);
2256 if (!line
|| !*line
)
2257 return send_error(ctx
, EPWMD_COMMAND_SYNTAX
);
2259 if ((req
= split_input_line(line
, "\t", 2)) == NULL
) {
2260 if (strv_printf(&req
, "%s", line
) == FALSE
)
2261 return send_syserror(ctx
, ENOMEM
);
2264 xp
= xmlXPathNewContext(client
->doc
);
2267 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2269 result
= xmlXPathEvalExpression((xmlChar
*)req
[0], xp
);
2272 xmlXPathFreeContext(xp
);
2273 return send_error(ctx
, EPWMD_LIBXML_ERROR
);
2276 if (xmlXPathNodeSetIsEmpty(result
->nodesetval
)) {
2277 rc
= EPWMD_EMPTY_ELEMENT
;
2281 rc
= recurse_xpath_nodeset(client
->doc
, result
->nodesetval
,
2282 (xmlChar
*)req
[1], &buf
);
2286 else if (!req
[1] && !xmlBufferLength(buf
)) {
2287 rc
= EPWMD_EMPTY_ELEMENT
;
2293 rc
= assuan_send_data(ctx
, xmlBufferContent(buf
), xmlBufferLength(buf
));
2302 xmlXPathFreeObject(result
);
2305 xmlXPathFreeContext(xp
);
2307 return send_error(ctx
, rc
);
2310 static int import_command_finalize(gpointer data
, gint assuan_rc
, guchar
*line
,
2313 struct client_s
*client
= assuan_get_pointer((assuan_context_t
)data
);
2314 gpg_error_t rc
= file_modified(client
);
2315 gchar
**req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2317 xmlNodePtr n
, root
, copy
;
2319 if (assuan_rc
|| rc
) {
2326 return assuan_rc
? assuan_rc
: rc
;
2329 req
= split_input_line((gchar
*)line
, " ", 2);
2337 return EPWMD_COMMAND_SYNTAX
;
2339 if ((path
= split_input_line(req
[0], "\t", 0)) == NULL
) {
2340 if ((path
= split_input_line(req
[0], " ", 0)) == NULL
)
2341 return EPWMD_COMMAND_SYNTAX
;
2346 if (!content
|| !*content
) {
2347 rc
= EPWMD_COMMAND_SYNTAX
;
2351 if (valid_xml_element((xmlChar
*)*path
) == FALSE
) {
2352 rc
= EPWMD_INVALID_ELEMENT
;
2356 if (valid_element_path(path
+1, FALSE
) == FALSE
) {
2357 rc
= EPWMD_INVALID_ELEMENT
;
2361 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2364 rc
= EPWMD_LIBXML_ERROR
;
2368 root
= xmlDocGetRootElement(doc
);
2369 path_orig
= g_strdupv(path
);
2373 rc
= gpg_error_from_errno(ENOMEM
);
2377 if (strv_printf(&path
, "%s", (gchar
*)root
->name
) == FALSE
) {
2378 g_strfreev(path_orig
);
2380 rc
= gpg_error_from_errno(ENOMEM
);
2384 n
= find_account(client
->doc
, &path
, &rc
, NULL
, 0);
2386 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2387 g_strfreev(path_orig
);
2392 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, FALSE
, 0, NULL
);
2394 if (rc
&& rc
!= EPWMD_ELEMENT_NOT_FOUND
) {
2395 g_strfreev(path_orig
);
2400 xmlNodePtr parent
= n
->parent
;
2411 if (rc
== EPWMD_ELEMENT_NOT_FOUND
) {
2412 n
= create_element_path(client
, &path
, &rc
);
2420 copy
= xmlCopyNode(root
, 1);
2421 n
= xmlAddChild(n
, copy
);
2425 rc
= EPWMD_LIBXML_ERROR
;
2430 client
->inquire_status
= INQUIRE_DONE
;
2434 static int import_command(assuan_context_t ctx
, gchar
*line
)
2437 struct client_s
*client
= assuan_get_pointer(ctx
);
2439 rc
= file_modified(client
);
2442 log_write("%s: %s", client
->filename
? client
->filename
: "",
2444 return send_error(ctx
, rc
);
2447 rc
= assuan_inquire_ext(ctx
, "IMPORT", 0, import_command_finalize
, ctx
);
2450 return send_error(ctx
, rc
);
2452 /* Don't return with assuan_process_done() here. This is an INQUIRE. */
2453 client
->inquire_status
= INQUIRE_BUSY
;
2457 static int lock_command(assuan_context_t ctx
, gchar
*line
)
2460 struct client_s
*client
= assuan_get_pointer(ctx
);
2462 rc
= file_modified(client
);
2465 log_write("%s: %s", client
->filename
? client
->filename
: "",
2467 return send_error(ctx
, rc
);
2470 rc
= lock_file_mutex(client
);
2473 client
->is_lock_cmd
= TRUE
;
2475 return send_error(ctx
, rc
);
2478 static int unlock_command(assuan_context_t ctx
, gchar
*line
)
2481 struct client_s
*client
= assuan_get_pointer(ctx
);
2483 rc
= file_modified(client
);
2486 log_write("%s: %s", client
->filename
? client
->filename
: "",
2488 return send_error(ctx
, rc
);
2491 unlock_file_mutex(client
);
2492 return send_error(ctx
, 0);
2495 static int getpid_command(assuan_context_t ctx
, gchar
*line
)
2499 pid_t pid
= getpid();
2501 print_fmt(buf
, sizeof(buf
), "%i", pid
);
2502 rc
= assuan_send_data(ctx
, buf
, strlen(buf
));
2503 return send_error(ctx
, rc
);
2506 void cleanup_assuan(assuan_context_t ctx
)
2508 struct client_s
*cl
= assuan_get_pointer(ctx
);
2514 static void reset_notify(assuan_context_t ctx
)
2516 struct client_s
*cl
= assuan_get_pointer(ctx
);
2522 static gpg_error_t
parse_client_option(assuan_context_t ctx
, const gchar
*line
)
2524 gchar name
[32] = {0}, value
[256] = {0};
2526 if (sscanf(line
, " %31[a-zA-Z] = %255c", name
, value
) != 2)
2527 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_SYNTAX
);
2529 if (g_strcasecmp(name
, (gchar
*)"NAME") == 0) {
2530 pth_attr_t attr
= pth_attr_of(pth_self());
2532 log_write("OPTION CLIENT %s", line
);
2533 pth_attr_set(attr
, PTH_ATTR_NAME
, value
);
2534 pth_attr_destroy(attr
);
2537 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2542 static int option_handler(assuan_context_t ctx
, const gchar
*name
,
2545 #ifdef WITH_PINENTRY
2546 struct client_s
*client
= assuan_get_pointer(ctx
);
2549 if (!value
|| !*value
)
2550 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2552 if (g_strcasecmp(name
, (gchar
*)"client") == 0)
2553 return parse_client_option(ctx
, value
);
2555 if (g_strcasecmp(name
, (gchar
*)"iterations") == 0) {
2560 n
= strtol(value
, &p
, 10);
2562 if (errno
|| (p
&& *p
))
2563 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2565 g_key_file_set_integer(keyfileh
, client
->filename
? client
->filename
: "global", "iterations", n
);
2566 send_status_all(STATUS_CONFIG
);
2568 #ifdef WITH_PINENTRY
2569 else if (g_strcasecmp(name
, (gchar
*)"ttyname") == 0) {
2570 g_free(client
->pinentry
->ttyname
);
2571 client
->pinentry
->ttyname
= g_strdup(value
);
2573 else if (g_strcasecmp(name
, (gchar
*)"ttytype") == 0) {
2574 g_free(client
->pinentry
->ttytype
);
2575 client
->pinentry
->ttytype
= g_strdup(value
);
2577 else if (g_strcasecmp(name
, (gchar
*)"display") == 0) {
2578 g_free(client
->pinentry
->display
);
2579 client
->pinentry
->display
= g_strdup(value
);
2581 else if (g_strcasecmp(name
, (gchar
*)"path") == 0) {
2582 g_free(client
->pinentry
->path
);
2583 client
->pinentry
->path
= g_strdup(value
);
2585 else if (g_strcasecmp(name
, (gchar
*)"title") == 0) {
2586 g_free(client
->pinentry
->title
);
2587 client
->pinentry
->title
= g_strdup(value
);
2589 else if (g_strcasecmp(name
, (gchar
*)"prompt") == 0) {
2590 g_free(client
->pinentry
->prompt
);
2591 client
->pinentry
->prompt
= g_strdup(value
);
2593 else if (g_strcasecmp(name
, (gchar
*)"desc") == 0) {
2594 g_free(client
->pinentry
->desc
);
2595 client
->pinentry
->desc
= g_strdup(value
);
2598 /* Need to wait for pinentry to support a --timeout option so it can
2599 * terminate itself. Cannot do it here because assuan_pipe_connect() calls
2600 * execv() which replaces the pid of the fork()ed thread from
2601 * pinentry_fork(). So pinentry will become a real process after the
2602 * thread terminates and won't be able to be kill()ed from pwmd.
2604 else if (g_strcasecmp(name
, (gchar
*)"timeout") == 0) {
2606 gint n
= strtol(value
, &p
, 10);
2609 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2611 client
->pinentry
->timeout
= n
;
2614 else if (g_strcasecmp(name
, (gchar
*)"pinentry") == 0) {
2616 gint n
= strtol(value
, &p
, 10);
2618 if (*p
|| n
< 0 || n
> 1)
2619 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_INV_VALUE
);
2621 client
->pinentry
->enable
= n
== 0 ? FALSE
: TRUE
;
2625 return gpg_err_make(PWMD_ERR_SOURCE
, GPG_ERR_UNKNOWN_OPTION
);
2628 log_write("OPTION %s=%s", name
, value
);
2632 gpg_error_t
register_commands(assuan_context_t ctx
)
2636 gint (*handler
)(assuan_context_t
, gchar
*line
);
2638 { "OPEN", open_command
},
2639 { "SAVE", save_command
},
2640 { "LIST", list_command
},
2641 { "REALPATH", realpath_command
},
2642 { "STORE", store_command
},
2643 { "DELETE", delete_command
},
2644 { "GET", get_command
},
2645 { "ATTR", attr_command
},
2646 { "ISCACHED", iscached_command
},
2647 { "CLEARCACHE", clearcache_command
},
2648 { "CACHETIMEOUT", cachetimeout_command
},
2649 { "GETCONFIG", getconfig_command
},
2650 { "DUMP", dump_command
},
2651 { "XPATH", xpath_command
},
2652 { "IMPORT", import_command
},
2653 { "LOCK", lock_command
},
2654 { "UNLOCK", unlock_command
},
2655 { "GETPID", getpid_command
},
2662 for (i
=0; table
[i
].name
; i
++) {
2663 rc
= assuan_register_command (ctx
, table
[i
].name
, table
[i
].handler
);
2669 rc
= assuan_register_bye_notify(ctx
, cleanup_assuan
);
2674 rc
= assuan_register_option_handler(ctx
, option_handler
);
2679 return assuan_register_reset_notify(ctx
, reset_notify
);
2682 gpg_error_t
try_xml_decrypt(assuan_context_t ctx
, gint fd
, struct stat st
,
2683 guchar
*key
, gint
*dst_iter
)
2688 guchar tkey
[gcrykeysize
];
2689 struct client_s
*client
= ctx
? assuan_get_pointer(ctx
) : NULL
;
2690 gcry_cipher_hd_t gh
;
2691 guint iter
= 0, n_iter
= 0;
2693 void *outbuf
= NULL
;
2697 file_header_t file_header
;
2698 gchar str
[ASSUAN_LINELENGTH
];
2701 rc
= gcry_cipher_open(&gh
, GCRY_CIPHER_AES256
, GCRY_CIPHER_MODE_CBC
, 0);
2709 lseek(fd
, 0, SEEK_SET
);
2710 insize
= st
.st_size
- sizeof(file_header_t
);
2711 iv
= gcry_malloc(gcryblocksize
);
2715 gcry_cipher_close(gh
);
2717 return gpg_error_from_errno(ENOMEM
);
2720 len
= pth_read(fd
, &file_header
, sizeof(file_header_t
));
2722 if (len
!= sizeof(file_header_t
)) {
2726 gcry_cipher_close(gh
);
2730 return gpg_error_from_errno(errno
);
2733 *dst_iter
= file_header
.iter
;
2735 /* No encryption iterations. This is a plain (gzipped) file. */
2736 if (file_header
.iter
== -1) {
2738 * cache_file_count() needs both .used == TRUE and a valid key in
2739 * order for it to count as a used cache entry. Fixes CACHE status
2742 memset(key
, '!', gcrykeysize
);
2743 insize
= st
.st_size
- sizeof(file_header_t
);
2746 memcpy(iv
, &file_header
.iv
, sizeof(file_header
.iv
));
2747 inbuf
= gcry_malloc(insize
);
2751 gcry_cipher_close(gh
);
2754 return gpg_error_from_errno(ENOMEM
);
2757 len
= pth_read(fd
, inbuf
, insize
);
2759 if (len
!= insize
) {
2763 gcry_cipher_close(gh
);
2767 return gpg_error_from_errno(errno
);
2770 if (file_header
.iter
== -1)
2773 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2775 gcry_cipher_close(gh
);
2776 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2779 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2786 if ((rc
= gcry_cipher_setkey(gh
, key
, gcrykeysize
))) {
2788 gcry_cipher_close(gh
);
2789 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2792 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2798 gcry_cipher_close(gh
);
2803 iter_progress
= get_key_file_integer(client
->filename
, "iteration_progress");
2805 if (ctx
&& iter_progress
> 0 && file_header
.iter
>= iter_progress
) {
2806 rc
= assuan_write_status(client
->ctx
, "DECRYPT",
2807 print_fmt(str
, sizeof(str
), "%i %i", 0, file_header
.iter
));
2816 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2824 memcpy(tkey
, key
, sizeof(tkey
));
2827 if ((rc
= gcry_cipher_setkey(gh
, tkey
, gcrykeysize
))) {
2829 gcry_cipher_close(gh
);
2830 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2833 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2835 memset(tkey
, 0, sizeof(tkey
));
2841 memset(tkey
, 0, sizeof(tkey
));
2843 while (iter
< file_header
.iter
) {
2844 if (ctx
&& iter_progress
> 0 && iter
>= iter_progress
) {
2845 if (!(iter
% iter_progress
)) {
2846 rc
= assuan_write_status(ctx
, "DECRYPT",
2847 print_fmt(str
, sizeof(str
), "%i %i", ++n_iter
* iter_progress
, file_header
.iter
));
2857 if ((rc
= gcry_cipher_setiv(gh
, iv
, gcryblocksize
))) {
2859 gcry_cipher_close(gh
);
2860 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2863 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2870 rc
= decrypt_xml(gh
, inbuf
, insize
, NULL
, 0);
2874 gcry_cipher_close(gh
);
2875 warnx("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2878 log_write("%s(%i): %s", __FUNCTION__
, __LINE__
, gcry_strerror(rc
));
2889 if (ctx
&& iter_progress
&& file_header
.iter
>= iter_progress
) {
2890 rc
= assuan_write_status(ctx
, "DECRYPT",
2891 print_fmt(str
, sizeof(str
), "%i %i", file_header
.iter
, file_header
.iter
));
2903 if (do_decompress(ctx
, inbuf
, insize
, &outbuf
, &outsize
, &zrc
) == FALSE
) {
2905 * Z_DATA_ERROR may be returned if this file hasn't been compressed yet.
2907 if (zrc
== Z_MEM_ERROR
) {
2909 return gpg_error_from_errno(ENOMEM
);
2911 else if (zrc
!= Z_DATA_ERROR
) {
2915 gcry_cipher_close(gh
);
2917 return EPWMD_BADKEY
;
2926 if (g_strncasecmp(inbuf
, "<?xml version=\"1.0\"?>", 21) != 0) {
2930 gcry_cipher_close(gh
);
2932 return EPWMD_BADKEY
;
2936 client
->xml
= inbuf
;
2937 client
->len
= insize
;
2940 gcry_cipher_close(gh
);
2948 * This is called after every Assuan command.
2950 void command_finalize(assuan_context_t ctx
, gint rc
)
2952 struct client_s
*client
= assuan_get_pointer(ctx
);
2954 if (!client
->is_lock_cmd
)
2955 unlock_file_mutex(client
);