2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
41 #include "pwmd-error.h"
49 #include "util-misc.h"
53 /* Flags needed to be retained for a client across commands. */
54 #define FLAG_NEW 0x0001
55 #define FLAG_HAS_LOCK 0x0002
56 #define FLAG_LOCK_CMD 0x0004
57 #define FLAG_NO_PINENTRY 0x0008
58 #define FLAG_OPEN 0x0010
59 #define FLAG_KEEP_LOCK 0x0020
61 /* These are command option flags. */
62 #define OPT_INQUIRE 0x0001
63 #define OPT_NO_PASSPHRASE 0x0002
64 #define OPT_RESET 0x0004
65 #define OPT_LIST_RECURSE 0x0008
66 #define OPT_LIST_VERBOSE 0x0010
67 #define OPT_LOCK 0x0020
68 #define OPT_LOCK_ON_OPEN 0x0040
69 #define OPT_SIGN 0x0080
70 #define OPT_LIST_ALL 0x0100
71 #define OPT_LIST_WITH_TARGET 0x0200
72 #define OPT_DATA 0x0400
74 struct command_table_s
{
76 gpg_error_t (*handler
)(assuan_context_t
, char *line
);
79 int unlock
; // unlock the file mutex after validating the checksum
82 static struct command_table_s
**command_table
;
84 static gpg_error_t
do_lock(struct client_s
*client
, int add
);
85 static gpg_error_t
validate_checksum(struct client_s
*, struct cache_data_s
*);
86 static gpg_error_t
update_checksum(struct client_s
*client
);
88 static gpg_error_t
unlock_file_mutex(struct client_s
*client
, int remove
)
92 // OPEN: keep the lock for the same file being reopened.
93 if (client
->flags
&FLAG_KEEP_LOCK
&& client
->flags
&FLAG_HAS_LOCK
)
96 if (!(client
->flags
&FLAG_HAS_LOCK
))
97 return GPG_ERR_NOT_LOCKED
;
99 rc
= cache_unlock_mutex(client
->md5file
, remove
);
101 rc
= GPG_ERR_INV_STATE
;
103 client
->flags
&= ~(FLAG_HAS_LOCK
|FLAG_LOCK_CMD
);
108 static gpg_error_t
lock_file_mutex(struct client_s
*client
, int add
)
111 int timeout
= config_get_integer(client
->filename
, "cache_timeout");
113 if (client
->flags
&FLAG_HAS_LOCK
)
116 rc
= cache_lock_mutex(client
->ctx
, client
->md5file
,
117 client
->lock_timeout
, add
, timeout
);
119 client
->flags
|= FLAG_HAS_LOCK
;
124 static gpg_error_t
file_modified(struct client_s
*client
,
125 struct command_table_s
*cmd
)
129 if (!(client
->flags
&FLAG_OPEN
))
130 return GPG_ERR_INV_STATE
;
132 rc
= lock_file_mutex(client
, 0);
133 if (!rc
|| rc
== GPG_ERR_NO_DATA
) {
134 rc
= validate_checksum(client
, NULL
);
135 if (gpg_err_code(rc
) == GPG_ERR_ENOENT
)
139 if ((cmd
->unlock
&& !(client
->flags
&FLAG_HAS_LOCK
)) || rc
)
140 unlock_file_mutex(client
, 0);
145 static gpg_error_t
parse_xml(assuan_context_t ctx
, int new)
147 struct client_s
*client
= assuan_get_pointer(ctx
);
148 int cached
= client
->doc
!= NULL
;
151 client
->doc
= new_document();
153 xmlDocDumpFormatMemory(client
->doc
,
154 (xmlChar
**)&client
->crypto
->plaintext
,
155 (int *)&client
->crypto
->plaintext_len
, 0);
156 if (!client
->crypto
->plaintext
) {
157 xmlFreeDoc(client
->doc
);
163 client
->doc
= parse_doc((char *)client
->crypto
->plaintext
,
164 client
->crypto
->plaintext_len
);
166 return !client
->doc
? GPG_ERR_ENOMEM
: 0;
169 static void free_client(struct client_s
*client
)
172 xmlFreeDoc(client
->doc
);
175 xfree(client
->filename
);
176 xfree(client
->last_error
);
178 if (client
->crypto
) {
179 cleanup_crypto_stage2(client
->crypto
);
180 if (client
->crypto
->pkey_sexp
)
181 gcry_sexp_release(client
->crypto
->pkey_sexp
);
183 client
->crypto
->pkey_sexp
= NULL
;
184 memset(client
->crypto
->sign_grip
, 0, sizeof(client
->crypto
->sign_grip
));
187 if (client
->xml_error
)
188 xmlResetError(client
->xml_error
);
191 void cleanup_client(struct client_s
*client
)
193 assuan_context_t ctx
= client
->ctx
;
194 struct client_thread_s
*thd
= client
->thd
;
195 struct crypto_s
*crypto
= client
->crypto
;
196 long lock_timeout
= client
->lock_timeout
;
198 unlock_file_mutex(client
, client
->flags
&FLAG_NEW
);
200 memset(client
, 0, sizeof(struct client_s
));
201 client
->crypto
= crypto
;
204 client
->lock_timeout
= lock_timeout
;
207 static gpg_error_t
open_finalize(assuan_context_t ctx
)
209 struct client_s
*client
= assuan_get_pointer(ctx
);
211 struct cache_data_s
*cdata
= cache_get_data(client
->md5file
);
214 client
->crypto
->filename
= str_dup(client
->filename
);
215 if (cdata
|| client
->flags
&FLAG_NEW
) {
216 if (!(client
->flags
&FLAG_NEW
)) {
217 rc
= decrypt_xml(client
->crypto
, cdata
->doc
, cdata
->doclen
);
222 cached
= cdata
!= NULL
;
226 rc
= decrypt_data(client
->ctx
, client
->crypto
);
230 rc
= parse_xml(ctx
, client
->flags
&FLAG_NEW
);
232 int timeout
= config_get_integer(client
->filename
,
236 cdata
= xcalloc(1, sizeof(struct cache_data_s
));
237 rc
= encrypt_xml(NULL
, cache_key
, cache_keysize
,
238 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
239 client
->crypto
->plaintext_len
, &cdata
->doc
,
240 &cdata
->doclen
, &cache_iv
, &cache_blocksize
, 0);
241 if (!rc
&& !(client
->flags
&FLAG_NEW
)) {
242 rc
= gcry_sexp_build((gcry_sexp_t
*)&cdata
->pubkey
, NULL
,
243 "%S", client
->crypto
->pkey_sexp
);
248 gcry_sexp_release(cdata
->sigkey
);
251 rc
= gcry_sexp_build((gcry_sexp_t
*)&cdata
->sigkey
, NULL
,
252 "%S", client
->crypto
->sigpkey_sexp
);
254 if (!rc
&& !cache_add_file(client
->md5file
, (client
->flags
&FLAG_NEW
)
255 ? NULL
: client
->crypto
->grip
, cdata
, timeout
)) {
257 free_cache_data_once(cdata
);
258 xmlFreeDoc(client
->doc
);
265 client
->flags
|= FLAG_OPEN
;
270 update_checksum(client
);
272 if (!rc
&& (client
->opts
&OPT_LOCK_ON_OPEN
))
273 rc
= do_lock(client
, 0);
278 static void req_cleanup(void *arg
)
283 strv_free((char **)arg
);
286 static gpg_error_t
parse_open_opt_lock(void * data
, void * value
)
288 struct client_s
*client
= data
;
290 client
->opts
|= OPT_LOCK_ON_OPEN
;
294 static gpg_error_t
parse_opt_pinentry(void * data
, void * value
)
296 struct client_s
*client
= data
;
298 client
->flags
|= FLAG_NO_PINENTRY
;
302 static gpg_error_t
parse_opt_inquire(void * data
, void * value
)
304 struct client_s
*client
= data
;
307 client
->opts
|= OPT_INQUIRE
;
311 static gpg_error_t
update_checksum(struct client_s
*client
)
315 struct cache_data_s
*cdata
;
316 gpg_error_t rc
= get_checksum(client
->filename
, &crc
, &len
);
323 cdata
= cache_get_data(client
->md5file
);
326 cdata
->crc
= xmalloc(len
);
327 memcpy(cdata
->crc
, crc
, len
);
333 static gpg_error_t
validate_checksum(struct client_s
*client
,
334 struct cache_data_s
*cdata
)
341 if (cdata
&& !cdata
->crc
)
342 return GPG_ERR_CHECKSUM
;
344 rc
= get_checksum(client
->filename
, &crc
, &len
);
349 n
= memcmp(cdata
->crc
, crc
, len
);
350 else if (client
->crc
)
351 n
= memcmp(client
->crc
, crc
, len
);
354 return n
? GPG_ERR_CHECKSUM
: 0;
357 static gpg_error_t
do_open(assuan_context_t ctx
, const char *filename
,
358 const char *password
)
360 struct client_s
*client
= assuan_get_pointer(ctx
);
361 struct cache_data_s
*cdata
;
365 if (!valid_filename(filename
))
366 return GPG_ERR_INV_VALUE
;
368 gcry_md_hash_buffer(GCRY_MD_MD5
, client
->md5file
, filename
, strlen(filename
));
369 client
->filename
= str_dup(filename
);
370 if (!client
->filename
)
371 return GPG_ERR_ENOMEM
;
374 cdata
= cache_get_data(client
->md5file
);
375 if (cdata
&& cdata
->doc
) {
378 rc
= validate_checksum(client
, cdata
);
379 /* This will check that the key is cached in the agent which needs to
380 * be determined for files that share a keygrip. */
382 rc
= cache_iscached(client
->filename
, &defer
);
384 rc
= GPG_ERR_KEY_EXPIRED
;
388 if (!rc
&& client
->thd
->remote
&&
389 config_get_boolean(NULL
, "tcp_require_key"))
390 rc
= GPG_ERR_KEY_EXPIRED
;
393 if (!rc
&& !password
) {
394 rc
= read_data_header(client
->filename
, &client
->crypto
->hdr
,
397 gcry_sexp_build(&client
->crypto
->pkey_sexp
, NULL
, "%S",
399 gcry_pk_get_keygrip(client
->crypto
->pkey_sexp
, client
->crypto
->grip
);
400 gcry_sexp_build(&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
402 gcry_pk_get_keygrip(client
->crypto
->sigpkey_sexp
, client
->crypto
->sign_grip
);
403 rc
= open_finalize(ctx
);
408 /* There was an error accessing the file so clear the cache entry. The
409 * real error will be returned from read_data_file() since the file
410 * may have only disappeared. */
412 log_write("%s: %s", filename
, pwmd_strerror(rc
));
413 rc
= cache_clear(client
->md5file
);
414 send_status_all(STATUS_CACHE
, NULL
);
421 rc
= read_data_file(client
->filename
, client
->crypto
);
423 if (gpg_err_source(rc
) != GPG_ERR_SOURCE_DEFAULT
||
424 gpg_err_code(rc
) != GPG_ERR_ENOENT
) {
425 log_write("%s: %s", client
->filename
, pwmd_strerror(rc
));
429 client
->flags
|= FLAG_NEW
;
430 memset(client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
431 memset(client
->crypto
->sign_grip
, 0, sizeof(client
->crypto
->sign_grip
));
432 rc
= open_finalize(ctx
);
437 rc
= set_agent_passphrase(client
->crypto
, password
, strlen(password
));
442 rc
= open_finalize(ctx
);
446 static gpg_error_t
open_command(assuan_context_t ctx
, char *line
)
449 struct client_s
*client
= assuan_get_pointer(ctx
);
450 char **req
, *password
= NULL
, *filename
;
451 unsigned char md5file
[16];
453 struct argv_s
*args
[] = {
454 &(struct argv_s
) { "lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
455 &(struct argv_s
) { "no-pinentry", OPTION_TYPE_NOARG
,
456 parse_opt_pinentry
},
460 client
->flags
&= ~(FLAG_NO_PINENTRY
);
461 rc
= parse_options(&line
, args
, client
);
463 return send_error(ctx
, rc
);
465 req
= str_split(line
, " ", 2);
467 return send_error(ctx
, GPG_ERR_SYNTAX
);
470 if (client
->thd
->remote
) {
471 rc
= tls_validate_access(client
, req
[0]);
473 if (rc
== GPG_ERR_INV_USER_ID
)
474 log_write(_("client validation failed for file '%s'"), req
[0]);
476 return send_error(ctx
, rc
);
479 /* Remote pinentries are not supported. */
480 client
->flags
|= FLAG_NO_PINENTRY
;
484 pthread_cleanup_push(req_cleanup
, req
);
486 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, filename
, strlen(filename
));
487 /* This client may have locked a different file with ISCACHED --lock than
488 * the current filename. This will remove that lock. */
489 same_file
= !memcmp(md5file
, client
->md5file
, 16) ? 1 : 0;
490 if (client
->flags
&FLAG_OPEN
||
491 (client
->flags
&FLAG_HAS_LOCK
&& !same_file
)) {
492 typeof(client
->opts
) opts
= client
->opts
;
493 typeof(client
->flags
) flags
= client
->flags
;
496 client
->flags
|= FLAG_KEEP_LOCK
;
498 cleanup_client(client
);
500 client
->flags
|= flags
;
501 client
->flags
&= ~(FLAG_LOCK_CMD
|FLAG_NEW
);
503 client
->flags
&= ~(FLAG_HAS_LOCK
);
506 /* Need to lock the mutex here because file_modified() cannot without
507 * knowing the filename. */
508 memcpy(client
->md5file
, md5file
, 16);
509 rc
= lock_file_mutex(client
, 1);
511 password
= req
[1] && *req
[1] ? req
[1] : NULL
;
512 rc
= set_pinentry_mode(client
->crypto
->agent
,
513 (client
->flags
&FLAG_NO_PINENTRY
) ? "loopback" : "ask");
515 rc
= do_open(ctx
, filename
, password
);
517 cleanup_client(client
);
519 cleanup_crypto_stage1(client
->crypto
);
523 pthread_cleanup_pop(1);
525 if (!rc
&& client
->flags
& FLAG_NEW
)
526 rc
= send_status(ctx
, STATUS_NEWFILE
, NULL
);
528 (void)kill_scd(client
->crypto
->agent
);
529 return send_error(ctx
, rc
);
532 static gpg_error_t
parse_save_opt_no_passphrase(void * data
, void * value
)
534 struct client_s
*client
= data
;
537 client
->opts
|= OPT_NO_PASSPHRASE
;
541 static gpg_error_t
parse_save_opt_cipher(void * data
, void * value
)
543 struct client_s
*client
= data
;
544 int algo
= cipher_string_to_gcrypt((char *)value
);
545 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
548 return GPG_ERR_INV_VALUE
;
550 hdr
->flags
= set_cipher_flag(hdr
->flags
, algo
);
554 static gpg_error_t
parse_save_opt_keygrip(void * data
, void * value
)
556 struct client_s
*client
= data
;
558 if (client
->crypto
->save
.pkey
)
559 gcry_sexp_release(client
->crypto
->save
.pkey
);
561 client
->crypto
->save
.pkey
= NULL
;
562 return get_pubkey(client
->crypto
, value
, &client
->crypto
->save
.pkey
);
565 static gpg_error_t
parse_save_opt_sign_keygrip(void * data
, void * value
)
567 struct client_s
*client
= data
;
569 if (client
->crypto
->save
.sigpkey
)
570 gcry_sexp_release(client
->crypto
->save
.sigpkey
);
572 client
->crypto
->save
.sigpkey
= NULL
;
573 return get_pubkey(client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
576 static gpg_error_t
parse_opt_s2k_count(void * data
, void * value
)
578 struct client_s
*client
= data
;
582 return GPG_ERR_INV_VALUE
;
584 client
->crypto
->save
.s2k_count
= strtoul(v
, NULL
, 10);
588 static gpg_error_t
parse_save_opt_iterations(void * data
, void * value
)
590 struct client_s
*client
= data
;
595 return GPG_ERR_INV_VALUE
;
598 n
= strtoull(v
, &p
, 10);
599 if (n
== UINT64_MAX
&& errno
)
600 return gpg_error_from_syserror();
602 return GPG_ERR_INV_VALUE
;
604 client
->crypto
->save
.hdr
.iterations
= n
;
608 static gpg_error_t
save_finalize(assuan_context_t ctx
)
610 struct client_s
*client
= assuan_get_pointer(ctx
);
612 xmlChar
*xmlbuf
= NULL
;
614 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
615 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
616 struct cache_data_s
*cdata
;
618 xmlDocDumpFormatMemory(client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
620 return GPG_ERR_ENOMEM
;
623 pubkey
= client
->crypto
->pkey_sexp
;
626 sigkey
= client
->crypto
->sigpkey_sexp
;
628 gcry_sexp_build(&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
629 sigkey
= client
->crypto
->save
.sigpkey
;
633 pthread_cleanup_push(xmlFree
, xmlbuf
);
634 rc
= encrypt_data_file(client
->ctx
, client
->crypto
, pubkey
, sigkey
,
635 client
->filename
, xmlbuf
, xmlbuflen
);
636 if (pubkey
== client
->crypto
->save
.pkey
) {
638 gcry_sexp_release(client
->crypto
->pkey_sexp
);
639 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
640 gcry_pk_get_keygrip(client
->crypto
->pkey_sexp
, client
->crypto
->grip
);
643 gcry_sexp_release(pubkey
);
645 client
->crypto
->save
.pkey
= NULL
;
649 gcry_pk_get_keygrip(sigkey
, client
->crypto
->sign_grip
);
651 if (sigkey
== client
->crypto
->save
.sigpkey
) {
653 if (client
->crypto
->sigpkey_sexp
)
654 gcry_sexp_release(client
->crypto
->sigpkey_sexp
);
656 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
657 gcry_pk_get_keygrip(client
->crypto
->sigpkey_sexp
, client
->crypto
->sign_grip
);
660 gcry_sexp_release(sigkey
);
662 client
->crypto
->save
.sigpkey
= NULL
;
666 /* This is safe since it is a fast operation. */
668 pthread_cleanup_push(cleanup_cache_mutex
, NULL
);
669 cdata
= cache_get_data(client
->md5file
);
670 int cached
= cdata
!= NULL
;
673 cdata
= xcalloc(1, sizeof(struct cache_data_s
));
675 /* Update in case of any --keygrip argument */
677 gcry_sexp_release(cdata
->pubkey
);
679 gcry_sexp_build((gcry_sexp_t
*)&cdata
->pubkey
, NULL
, "%S",
680 client
->crypto
->pkey_sexp
);
683 gcry_sexp_release(cdata
->sigkey
);
685 gcry_sexp_build((gcry_sexp_t
*)&cdata
->sigkey
, NULL
, "%S",
686 client
->crypto
->sigpkey_sexp
);
688 gcry_free(cdata
->doc
);
690 rc
= encrypt_xml(NULL
, cache_key
, cache_keysize
, GCRY_CIPHER_AES
,
691 xmlbuf
, xmlbuflen
, &cdata
->doc
, &cdata
->doclen
, &cache_iv
,
692 &cache_blocksize
, 0);
694 rc
= cache_set_data(client
->md5file
, cdata
, client
->crypto
->grip
);
695 if (!rc
&& (!cached
|| (client
->flags
&FLAG_NEW
)))
696 send_status_all(STATUS_CACHE
, NULL
);
699 update_checksum(client
);
700 client
->flags
&= ~(FLAG_NEW
);
705 pthread_cleanup_pop(1); // mutex unlock
708 pthread_cleanup_pop(1); // xmlFree
712 static gpg_error_t
parse_opt_reset(void * data
, void * value
)
714 struct client_s
*client
= data
;
717 client
->opts
|= OPT_RESET
;
721 static gpg_error_t
save_command(assuan_context_t ctx
, char *line
)
723 struct client_s
*client
= assuan_get_pointer(ctx
);
726 struct argv_s
*args
[] = {
727 &(struct argv_s
) { "no-passphrase", OPTION_TYPE_NOARG
,
728 parse_save_opt_no_passphrase
},
729 &(struct argv_s
) { "cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
730 &(struct argv_s
) { "inquire-keyparam", OPTION_TYPE_NOARG
, parse_opt_inquire
},
731 &(struct argv_s
) { "keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
732 &(struct argv_s
) { "sign-keygrip", OPTION_TYPE_ARG
,
733 parse_save_opt_sign_keygrip
},
734 &(struct argv_s
) { "s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
735 &(struct argv_s
) { "reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
736 &(struct argv_s
) { "cipher-iterations", OPTION_TYPE_ARG
, parse_save_opt_iterations
},
740 cleanup_save(&client
->crypto
->save
);
741 memcpy(&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
, sizeof(file_header_t
));
742 client
->crypto
->save
.s2k_count
= config_get_ulong(client
->filename
, "s2k_count");
744 if (client
->flags
&FLAG_NEW
)
745 client
->crypto
->save
.hdr
.iterations
=
746 config_get_ulonglong(client
->filename
, "cipher_iterations");
748 rc
= parse_options(&line
, args
, client
);
750 return send_error(ctx
, rc
);
752 if ((client
->opts
&OPT_NO_PASSPHRASE
) && !(client
->flags
&FLAG_NEW
)
753 && !(client
->opts
&OPT_INQUIRE
))
754 return send_error(ctx
, GPG_ERR_WRONG_KEY_USAGE
);
756 if (lstat(client
->filename
, &st
) == -1 && errno
!= ENOENT
)
757 return send_error(ctx
, gpg_error_from_syserror());
759 if (errno
!= ENOENT
&& !S_ISREG(st
.st_mode
)) {
760 log_write("%s: %s", client
->filename
, pwmd_strerror(GPG_ERR_ENOANO
));
761 return send_error(ctx
, GPG_ERR_ENOANO
);
765 rc
= cache_iscached(client
->filename
, &defer
);
767 log_write("%s: %s", client
->filename
, pwmd_strerror(GPG_ERR_KEY_EXPIRED
));
768 client
->opts
|= OPT_RESET
;
771 if (client
->opts
&OPT_RESET
) {
772 rc
= cache_clear(client
->md5file
);
774 return send_error(ctx
, rc
);
776 send_status_all(STATUS_CACHE
, NULL
);
779 rc
= update_element_mtime(xmlDocGetRootElement(client
->doc
));
781 return send_error(ctx
, rc
);
783 if (!rc
&& !client
->crypto
->save
.pkey
&&
784 (client
->flags
&FLAG_NEW
|| client
->opts
&OPT_INQUIRE
)) {
785 rc
= send_status(client
->ctx
, STATUS_GENKEY
, NULL
);
787 struct inquire_data_s idata
= {0};
788 char *params
= client
->opts
&OPT_INQUIRE
? NULL
789 : default_key_params(client
->crypto
);
791 pthread_cleanup_push(xfree
, params
);
792 idata
.crypto
= client
->crypto
;
796 idata
.len
= strlen(params
);
801 client
->crypto
->agent
->inquire_data
= &idata
;
802 client
->crypto
->agent
->inquire_cb
= NULL
;
803 rc
= generate_key(client
->crypto
, params
,
804 (client
->opts
&OPT_NO_PASSPHRASE
), 1);
805 pthread_cleanup_pop(1);
810 rc
= save_finalize(ctx
);
812 cleanup_crypto_stage1(client
->crypto
);
813 (void)kill_scd(client
->crypto
->agent
);
814 return send_error(ctx
, rc
);
817 static gpg_error_t
do_delete(assuan_context_t ctx
, char *line
)
819 struct client_s
*client
= assuan_get_pointer(ctx
);
824 if (strchr(line
, '\t'))
825 req
= str_split(line
, "\t", 0);
827 req
= str_split(line
, " ", 0);
830 return GPG_ERR_SYNTAX
;
832 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, 0);
839 * No sub-node defined. Remove the entire node (root element).
851 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
864 static gpg_error_t
delete_command(assuan_context_t ctx
, char *line
)
866 struct client_s
*client
= assuan_get_pointer(ctx
);
868 struct argv_s
*args
[] = {
869 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
873 rc
= parse_options(&line
, args
, client
);
875 return send_error(ctx
, rc
);
877 if (client
->opts
&OPT_INQUIRE
) {
878 unsigned char *result
;
881 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
883 return send_error(ctx
, rc
);
885 line
= (char *)result
;
888 rc
= do_delete(ctx
, line
);
890 if (client
->opts
&OPT_INQUIRE
)
893 return send_error(ctx
, rc
);
896 static gpg_error_t
store_command(assuan_context_t ctx
, char *line
)
898 struct client_s
*client
= assuan_get_pointer(ctx
);
901 unsigned char *result
;
903 xmlNodePtr n
, parent
;
905 char *content
= NULL
;
907 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
909 return send_error(ctx
, rc
);
911 req
= str_split((char *)result
, "\t", 0);
915 return send_error(ctx
, GPG_ERR_SYNTAX
);
917 len
= strv_length(req
);
918 has_content
= line
[strlen(line
)-1] != '\t' && len
> 1;
919 if (*(req
+1) && !valid_element_path(req
, has_content
)) {
921 return send_error(ctx
, GPG_ERR_INV_VALUE
);
924 if (has_content
|| !*req
[len
-1]) {
926 content
= req
[len
-1];
931 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, 0);
932 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
933 rc
= new_root_element(client
->doc
, *req
);
936 return send_error(ctx
, GPG_ERR_SYNTAX
);
944 return send_error(ctx
, rc
);
949 if (req
[1] && *req
[1]) {
951 parent
= create_elements_cb(n
, req
+1, &rc
, NULL
);
953 parent
= find_elements(client
->doc
, n
->children
, req
+1, &rc
,
954 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
, 0);
957 if (!rc
&& len
> 1) {
958 n
= find_text_node(parent
->children
);
960 xmlNodeSetContent(n
, (xmlChar
*)content
);
962 xmlNodeAddContent(parent
, (xmlChar
*)content
);
964 update_element_mtime(parent
);
969 return send_error(ctx
, rc
);
972 static gpg_error_t
xfer_data(assuan_context_t ctx
, const char *line
,
978 int progress
= config_get_integer("global", "xfer_progress");
981 progress
= progress
>0 ? (progress
/ASSUAN_LINELENGTH
)*ASSUAN_LINELENGTH
: 0;
982 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
983 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
990 if (sent
+ to_send
> total
)
991 to_send
= total
- sent
;
993 rc
= assuan_send_data(ctx
, flush
? NULL
: (char *)line
+sent
,
994 flush
? 0 : to_send
);
996 sent
+= flush
? 0 : to_send
;
998 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
999 (sent
== total
&& flush
))
1000 rc
= send_status(ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1002 if (!flush
&& !rc
&& sent
== total
) {
1007 } while (!rc
&& sent
< total
);
1012 static gpg_error_t
do_get(assuan_context_t ctx
, char *line
)
1014 struct client_s
*client
= assuan_get_pointer(ctx
);
1019 req
= str_split(line
, "\t", 0);
1021 if (!req
|| !*req
) {
1023 return GPG_ERR_SYNTAX
;
1026 n
= find_root_element(client
->doc
, &req
, &rc
, NULL
, 0, 0);
1033 n
= find_elements(client
->doc
, n
->children
, req
+1, &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1039 if (!n
|| !n
->children
)
1040 return GPG_ERR_NO_DATA
;
1042 n
= find_text_node(n
->children
);
1043 if (!n
|| !n
->content
|| !*n
->content
)
1044 return GPG_ERR_NO_DATA
;
1046 rc
= xfer_data(ctx
, (char *)n
->content
, xmlStrlen(n
->content
));
1050 static gpg_error_t
get_command(assuan_context_t ctx
, char *line
)
1052 struct client_s
*client
= assuan_get_pointer(ctx
);
1054 struct argv_s
*args
[] = {
1055 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1059 rc
= parse_options(&line
, args
, client
);
1061 return send_error(ctx
, rc
);
1063 if (client
->opts
&OPT_INQUIRE
) {
1064 unsigned char *result
;
1067 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
1069 return send_error(ctx
, rc
);
1071 line
= (char *)result
;
1074 rc
= do_get(ctx
, line
);
1076 if (client
->opts
&OPT_INQUIRE
)
1079 return send_error(ctx
, rc
);
1082 static void list_command_cleanup1(void *arg
);
1083 static gpg_error_t
realpath_command(assuan_context_t ctx
, char *line
)
1085 struct string_s
*string
= NULL
;
1087 struct client_s
*client
= assuan_get_pointer(ctx
);
1088 struct argv_s
*args
[] = {
1089 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1093 rc
= parse_options(&line
, args
, client
);
1095 return send_error(ctx
, rc
);
1097 if (client
->opts
&OPT_INQUIRE
) {
1098 unsigned char *result
;
1101 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
1103 return send_error(ctx
, rc
);
1105 line
= (char *)result
;
1108 rc
= build_realpath(client
->doc
, line
, &string
);
1110 pthread_cleanup_push(list_command_cleanup1
, string
);
1111 rc
= xfer_data(ctx
, string
->str
, string
->len
);
1112 pthread_cleanup_pop(1);
1115 if (client
->opts
&OPT_INQUIRE
)
1118 return send_error(ctx
, rc
);
1121 static void list_command_cleanup1(void *arg
)
1124 string_free((struct string_s
*)arg
, 1);
1127 static void list_command_cleanup2(void *arg
)
1129 struct element_list_s
*elements
= arg
;
1132 if (elements
->list
) {
1133 int total
= slist_length(elements
->list
);
1136 for (i
= 0; i
< total
; i
++) {
1137 char *tmp
= slist_nth_data(elements
->list
, i
);
1141 slist_free(elements
->list
);
1144 if (elements
->prefix
)
1145 xfree(elements
->prefix
);
1148 strv_free(elements
->req
);
1154 static gpg_error_t
parse_list_opt_norecurse(void * data
, void * value
)
1156 struct client_s
*client
= data
;
1158 client
->opts
&= ~(OPT_LIST_RECURSE
);
1162 static gpg_error_t
parse_list_opt_verbose(void * data
, void * value
)
1164 struct client_s
*client
= data
;
1166 client
->opts
|= OPT_LIST_VERBOSE
;
1170 static gpg_error_t
parse_list_opt_target(void * data
, void * value
)
1172 struct client_s
*client
= data
;
1174 client
->opts
|= OPT_LIST_WITH_TARGET
;
1178 static gpg_error_t
parse_list_opt_all(void * data
, void * value
)
1180 struct client_s
*client
= data
;
1182 client
->opts
|= OPT_LIST_ALL
;
1186 static gpg_error_t
list_path_once(struct client_s
*client
, char *line
,
1187 struct element_list_s
*elements
, struct string_s
*result
)
1191 elements
->req
= str_split(line
, " ", 0);
1193 strv_printf(&elements
->req
, "%s", line
);
1195 rc
= create_path_list(client
->doc
, elements
, *elements
->req
);
1196 if (rc
== GPG_ERR_ELOOP
&& elements
->verbose
)
1201 int total
= slist_length(elements
->list
);
1205 rc
= GPG_ERR_NO_DATA
;
1209 for (i
= 0; i
< total
; i
++) {
1210 char *tmp
= slist_nth_data(elements
->list
, i
);
1212 string_append_printf(result
, "%s%s", tmp
,
1213 i
+1 == total
? "" : "\n");
1219 rc
= GPG_ERR_NO_DATA
;
1225 static int has_list_flag(char *path
, char *flags
)
1229 while (*p
&& *++p
!= ' ');
1237 for (f
= flags
; *f
&& *f
!= ' '; f
++) {
1246 static gpg_error_t
do_list(assuan_context_t ctx
, char *line
)
1248 struct client_s
*client
= assuan_get_pointer(ctx
);
1250 struct element_list_s
*elements
= NULL
;
1252 elements
= xcalloc(1, sizeof(struct element_list_s
));
1254 return GPG_ERR_ENOMEM
;
1256 elements
->recurse
= client
->opts
&OPT_LIST_RECURSE
;
1257 elements
->verbose
= (client
->opts
&OPT_LIST_VERBOSE
)|(client
->opts
&OPT_LIST_WITH_TARGET
);
1258 elements
->with_target
= client
->opts
&OPT_LIST_WITH_TARGET
;
1260 if (!line
|| !*line
) {
1261 struct string_s
*str
= NULL
;
1263 pthread_cleanup_push(list_command_cleanup2
, elements
);
1264 rc
= list_root_elements(client
->doc
, &str
, elements
->verbose
,
1265 elements
->with_target
);
1266 pthread_cleanup_pop(1);
1267 pthread_cleanup_push(list_command_cleanup1
, str
);
1270 if (client
->opts
&OPT_LIST_ALL
) {
1271 char **roots
= str_split(str
->str
, "\n", 0);
1274 pthread_cleanup_push(req_cleanup
, roots
);
1275 string_truncate(str
, 0);
1277 for (p
= roots
; *p
; p
++) {
1278 if (strchr(*p
, ' ')) {
1279 if (has_list_flag(*p
, "EO")) {
1280 string_append_printf(str
, "%s%s", *p
,
1281 *(p
+1) ? "\n" : "");
1286 elements
= xcalloc(1, sizeof(struct element_list_s
));
1288 rc
= GPG_ERR_ENOMEM
;
1292 elements
->recurse
= client
->opts
&OPT_LIST_RECURSE
;
1293 elements
->verbose
= (client
->opts
&OPT_LIST_VERBOSE
)|(client
->opts
&OPT_LIST_WITH_TARGET
);
1294 elements
->with_target
= client
->opts
&OPT_LIST_WITH_TARGET
;
1295 pthread_cleanup_push(list_command_cleanup2
, elements
);
1296 rc
= list_path_once(client
, *p
, elements
, str
);
1297 pthread_cleanup_pop(1);
1302 string_append(str
, "\n");
1305 pthread_cleanup_pop(1);
1309 rc
= xfer_data(ctx
, str
->str
, str
->len
);
1312 pthread_cleanup_pop(1);
1316 pthread_cleanup_push(list_command_cleanup2
, elements
);
1317 struct string_s
*str
= string_new(NULL
);
1318 pthread_cleanup_push(list_command_cleanup1
, str
);
1319 rc
= list_path_once(client
, line
, elements
, str
);
1321 rc
= xfer_data(ctx
, str
->str
, str
->len
);
1323 pthread_cleanup_pop(1);
1324 pthread_cleanup_pop(1);
1328 static gpg_error_t
list_command(assuan_context_t ctx
, char *line
)
1330 struct client_s
*client
= assuan_get_pointer(ctx
);
1332 struct argv_s
*args
[] = {
1333 &(struct argv_s
) { "no-recurse", OPTION_TYPE_NOARG
, parse_list_opt_norecurse
},
1334 &(struct argv_s
) { "verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1335 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1336 &(struct argv_s
) { "with-target", OPTION_TYPE_NOARG
, parse_list_opt_target
},
1337 &(struct argv_s
) { "all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1341 if (disable_list_and_dump
== 1)
1342 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1344 client
->opts
|= OPT_LIST_RECURSE
;
1345 rc
= parse_options(&line
, args
, client
);
1347 return send_error(ctx
, rc
);
1349 if (client
->opts
&OPT_LIST_ALL
)
1350 client
->opts
|= OPT_LIST_RECURSE
|OPT_LIST_VERBOSE
;
1352 if (client
->opts
&OPT_INQUIRE
) {
1353 unsigned char *result
;
1356 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
1358 return send_error(ctx
, rc
);
1360 line
= (char *)result
;
1363 rc
= do_list(ctx
, line
);
1365 if (client
->opts
&OPT_INQUIRE
)
1368 return send_error(ctx
, rc
);
1372 * req[0] - element path
1374 static gpg_error_t
attribute_list(assuan_context_t ctx
, char **req
)
1376 struct client_s
*client
= assuan_get_pointer(ctx
);
1377 char **attrlist
= NULL
;
1385 if (!req
|| !req
[0])
1386 return GPG_ERR_SYNTAX
;
1388 if ((path
= str_split(req
[0], "\t", 0)) == NULL
) {
1390 * The first argument may be only a root element.
1392 if ((path
= str_split(req
[0], " ", 0)) == NULL
)
1393 return GPG_ERR_SYNTAX
;
1396 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, 0);
1404 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1405 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1415 for (a
= n
->properties
; a
; a
= a
->next
) {
1418 if ((pa
= xrealloc(attrlist
, (i
+ 2) * sizeof(char *))) == NULL
) {
1420 strv_free(attrlist
);
1422 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
1423 return GPG_ERR_ENOMEM
;
1428 attrlist
[i
] = str_asprintf("%s %s", (char *)a
->name
,
1429 an
&& an
->content
? (char *)an
->content
: "");
1432 strv_free(attrlist
);
1433 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
1434 return GPG_ERR_ENOMEM
;
1437 attrlist
[++i
] = NULL
;
1441 return GPG_ERR_NO_DATA
;
1443 line
= strv_join("\n", attrlist
);
1446 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
1447 strv_free(attrlist
);
1448 return GPG_ERR_ENOMEM
;
1451 pthread_cleanup_push(xfree
, line
);
1452 pthread_cleanup_push(req_cleanup
, attrlist
);
1453 rc
= xfer_data(ctx
, line
, strlen(line
));
1454 pthread_cleanup_pop(1);
1455 pthread_cleanup_pop(1);
1460 * req[0] - attribute
1461 * req[1] - element path
1463 static gpg_error_t
attribute_delete(struct client_s
*client
, char **req
)
1469 if (!req
|| !req
[0] || !req
[1])
1470 return GPG_ERR_SYNTAX
;
1472 if (!strcmp(req
[0], "_name"))
1473 return GPG_ERR_INV_ATTR
;
1475 if ((path
= str_split(req
[1], "\t", 0)) == NULL
) {
1477 * The first argument may be only a root element.
1479 if ((path
= str_split(req
[1], " ", 0)) == NULL
)
1480 return GPG_ERR_SYNTAX
;
1483 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, 0);
1488 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1489 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1494 rc
= delete_attribute(n
, (xmlChar
*)req
[0]);
1501 static xmlNodePtr
create_element_path(struct client_s
*client
,
1502 char ***elements
, gpg_error_t
*rc
, xmlNodePtr parent
)
1504 char **req
= *elements
;
1505 char **req_orig
= strv_dup(req
);
1506 xmlNodePtr n
= NULL
;
1511 *rc
= GPG_ERR_ENOMEM
;
1512 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
1517 n
= find_root_element(client
->doc
, &req
, rc
, NULL
, 0, 0);
1519 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1522 *rc
= new_root_element(client
->doc
, req
[0]);
1528 else if (n
== parent
) {
1529 *rc
= GPG_ERR_CONFLICT
;
1535 n
= create_target_elements_cb(n
, req
+1, rc
, NULL
);
1537 n
= find_elements(client
->doc
, n
->children
, req
+1, rc
, NULL
, NULL
,
1538 create_target_elements_cb
, 0, 0, parent
, 0);
1544 * Reset the position of the element tree now that the elements
1545 * have been created.
1550 n
= find_root_element(client
->doc
, &req
, rc
, NULL
, 0, 0);
1554 n
= find_elements(client
->doc
, n
->children
, req
+1, rc
,
1555 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1562 strv_free(req_orig
);
1569 * Creates a "target" attribute. When other commands encounter an element with
1570 * this attribute, the element path is modified to the target value. If the
1571 * source element path doesn't exist when using 'ATTR SET target', it is
1572 * created, but the destination element path must exist.
1574 * req[0] - source element path
1575 * req[1] - destination element path
1577 static gpg_error_t
target_attribute(struct client_s
*client
, char **req
)
1579 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1583 if (!req
|| !req
[0] || !req
[1])
1584 return GPG_ERR_SYNTAX
;
1586 if ((src
= str_split(req
[0], "\t", 0)) == NULL
) {
1588 * The first argument may be only a root element.
1590 if ((src
= str_split(req
[0], " ", 0)) == NULL
)
1591 return GPG_ERR_SYNTAX
;
1594 if (!valid_element_path(src
, 0))
1595 return GPG_ERR_INV_VALUE
;
1597 if ((dst
= str_split(req
[1], "\t", 0)) == NULL
) {
1599 * The first argument may be only a root element.
1601 if ((dst
= str_split(req
[1], " ", 0)) == NULL
) {
1602 rc
= GPG_ERR_SYNTAX
;
1607 odst
= strv_dup(dst
);
1609 rc
= GPG_ERR_ENOMEM
;
1613 n
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1615 * Make sure the destination element path exists.
1621 n
= find_elements(client
->doc
, n
->children
, dst
+1, &rc
,
1622 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1627 rc
= validate_target_attribute(client
->doc
, req
[0], n
);
1628 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1631 n
= create_element_path(client
, &src
, &rc
, NULL
);
1635 line
= strv_join("\t", odst
);
1637 rc
= GPG_ERR_ENOMEM
;
1641 rc
= add_attribute(n
, "target", line
);
1652 * req[0] - attribute
1653 * req[1] - element path
1655 static gpg_error_t
attribute_get(assuan_context_t ctx
, char **req
)
1657 struct client_s
*client
= assuan_get_pointer(ctx
);
1663 if (!req
|| !req
[0] || !req
[1])
1664 return GPG_ERR_SYNTAX
;
1666 if (strchr(req
[1], '\t')) {
1667 if ((path
= str_split(req
[1], "\t", 0)) == NULL
)
1668 return GPG_ERR_SYNTAX
;
1671 if ((path
= str_split(req
[1], " ", 0)) == NULL
)
1672 return GPG_ERR_SYNTAX
;
1675 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, 0);
1681 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1682 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1690 if ((a
= xmlGetProp(n
, (xmlChar
*)req
[0])) == NULL
)
1691 return GPG_ERR_NOT_FOUND
;
1693 pthread_cleanup_push(xmlFree
, a
);
1696 rc
= xfer_data(ctx
, (char *)a
, xmlStrlen(a
));
1698 rc
= GPG_ERR_NO_DATA
;
1700 pthread_cleanup_pop(1);
1709 * req[0] - attribute
1710 * req[1] - element path
1713 static gpg_error_t
attribute_set(struct client_s
*client
, char **req
)
1719 if (!req
|| !req
[0] || !req
[1])
1720 return GPG_ERR_SYNTAX
;
1723 * Reserved attribute names.
1725 if (!strcmp(req
[0], "_name"))
1726 return GPG_ERR_INV_ATTR
;
1727 else if (!strcmp(req
[0], "target"))
1728 return target_attribute(client
, req
+ 1);
1730 if ((path
= str_split(req
[1], "\t", 0)) == NULL
) {
1732 * The first argument may be only a root element.
1734 if ((path
= str_split(req
[1], " ", 0)) == NULL
)
1735 return GPG_ERR_SYNTAX
;
1738 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, 0);
1744 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
,
1745 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1751 rc
= add_attribute(n
, req
[0], req
[2]);
1760 * req[1] - attribute name or element path if command is LIST
1761 * req[2] - element path
1762 * req[2] - element path or value
1765 static gpg_error_t
do_attr(assuan_context_t ctx
, char *line
)
1767 struct client_s
*client
= assuan_get_pointer(ctx
);
1771 req
= str_split(line
, " ", 4);
1772 if (!req
|| !req
[0] || !req
[1]) {
1774 return GPG_ERR_SYNTAX
;
1777 pthread_cleanup_push(req_cleanup
, req
);
1779 if (strcasecmp(req
[0], "SET") == 0)
1780 rc
= attribute_set(client
, req
+1);
1781 else if (strcasecmp(req
[0], "GET") == 0)
1782 rc
= attribute_get(ctx
, req
+1);
1783 else if (strcasecmp(req
[0], "DELETE") == 0)
1784 rc
= attribute_delete(client
, req
+1);
1785 else if (strcasecmp(req
[0], "LIST") == 0)
1786 rc
= attribute_list(ctx
, req
+1);
1788 rc
= GPG_ERR_SYNTAX
;
1790 pthread_cleanup_pop(1);
1794 static gpg_error_t
attr_command(assuan_context_t ctx
, char *line
)
1796 struct client_s
*client
= assuan_get_pointer(ctx
);
1798 struct argv_s
*args
[] = {
1799 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1803 rc
= parse_options(&line
, args
, client
);
1805 return send_error(ctx
, rc
);
1807 if (client
->opts
&OPT_INQUIRE
) {
1808 unsigned char *result
;
1811 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
1813 return send_error(ctx
, rc
);
1815 line
= (char *)result
;
1818 rc
= do_attr(ctx
, line
);
1820 if (client
->opts
&OPT_INQUIRE
)
1823 return send_error(ctx
, rc
);
1826 static gpg_error_t
parse_iscached_opt_lock(void * data
, void * value
)
1828 struct client_s
*client
= data
;
1831 client
->opts
|= OPT_LOCK
;
1835 static gpg_error_t
iscached_command(assuan_context_t ctx
, char *line
)
1837 struct client_s
*client
= assuan_get_pointer(ctx
);
1839 unsigned char md5file
[16];
1840 struct argv_s
*args
[] = {
1841 &(struct argv_s
) { "lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
1845 if (!line
|| !*line
)
1846 return send_error(ctx
, GPG_ERR_SYNTAX
);
1848 rc
= parse_options(&line
, args
, client
);
1850 return send_error(ctx
, rc
);
1851 else if (!valid_filename(line
))
1852 return send_error(ctx
, GPG_ERR_INV_VALUE
);
1854 rc
= cache_iscached(line
, NULL
);
1855 if (client
->opts
&OPT_LOCK
&& (!rc
|| gpg_err_code(rc
) == GPG_ERR_NO_DATA
)) {
1856 gpg_error_t trc
= rc
;
1857 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, line
, strlen(line
));
1858 if (memcmp(md5file
, client
->md5file
, 16))
1859 cleanup_client(client
);
1861 memcpy(client
->md5file
, md5file
, 16);
1862 rc
= do_lock(client
, 1);
1867 return send_error(ctx
, rc
);
1870 static gpg_error_t
clearcache_command(assuan_context_t ctx
, char *line
)
1872 gpg_error_t rc
= 0, all_rc
= 0;
1873 unsigned char md5file
[16];
1877 struct client_thread_s
*once
= NULL
;
1880 MUTEX_LOCK(&cn_mutex
);
1881 pthread_cleanup_push(cleanup_mutex_cb
, &cn_mutex
);
1883 if (!line
|| !*line
)
1886 t
= slist_length(cn_thread_list
);
1888 for (i
= 0; i
< t
; i
++) {
1889 struct client_thread_s
*thd
= slist_nth_data(cn_thread_list
, i
);
1894 /* Lock each connected clients' file mutex to prevent any other client
1895 * from accessing the cache entry (the file mutex is locked upon
1896 * command startup). The cache for the entry is not cleared if the
1897 * file mutex is locked by another client to prevent this function
1901 rc
= cache_lock_mutex(thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
1902 if (gpg_err_code(rc
) == GPG_ERR_LOCKED
) {
1903 if (pthread_equal(pthread_self(), thd
->tid
))
1906 if (!thd
->cl
->filename
||
1907 cache_iscached(thd
->cl
->filename
, NULL
) == GPG_ERR_NO_DATA
) {
1912 cache_defer_clear(thd
->cl
->md5file
);
1915 else if (gpg_err_code(rc
) == GPG_ERR_NO_DATA
) {
1921 rc
= cache_clear(thd
->cl
->md5file
);
1925 cache_unlock_mutex(thd
->cl
->md5file
, 0);
1932 /* A single data filename was specified. Lock only this data file
1933 * mutex and free the cache entry. */
1935 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, line
, strlen(line
));
1937 if (!memcmp(thd
->cl
->md5file
, md5file
, sizeof(md5file
))) {
1938 rc
= cache_lock_mutex(thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
1939 if (gpg_err_code(rc
) == GPG_ERR_LOCKED
) {
1940 if (pthread_equal(pthread_self(), thd
->tid
))
1946 rc
= cache_clear(thd
->cl
->md5file
);
1947 cache_unlock_mutex(thd
->cl
->md5file
, 0);
1950 cache_defer_clear(thd
->cl
->md5file
);
1957 /* Only connected clients' cache entries have been cleared. Now clear any
1958 * remaining cache entries without clients but only if there wasn't an
1959 * error from above since this would defeat the locking check of the
1960 * remaining entries. */
1963 /* No clients are using the specified file. */
1964 else if (!all_rc
&& !rc
&& !once
) {
1965 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, line
, strlen(line
));
1966 rc
= cache_clear(md5file
);
1969 /* Release the connection mutex. */
1970 pthread_cleanup_pop(1);
1974 send_status_all(STATUS_CACHE
, NULL
);
1976 /* One or more files were locked while clearing all cache entries. */
1980 return send_error(ctx
, rc
);
1983 static gpg_error_t
cachetimeout_command(assuan_context_t ctx
, char *line
)
1985 unsigned char md5file
[16];
1987 char **req
= str_split(line
, " ", 0);
1991 if (!req
|| !*req
|| !req
[1]) {
1993 return send_error(ctx
, GPG_ERR_SYNTAX
);
1997 timeout
= (int)strtol(req
[1], &p
, 10);
1998 if (errno
!= 0 || *p
!= 0 || timeout
< -1) {
2000 return send_error(ctx
, GPG_ERR_SYNTAX
);
2003 gcry_md_hash_buffer(GCRY_MD_MD5
, md5file
, req
[0], strlen(req
[0]));
2005 rc
= cache_set_timeout(md5file
, timeout
);
2007 MUTEX_LOCK(&rcfile_mutex
);
2008 config_set_int_param(&global_config
, req
[0], "cache_timeout", req
[1]);
2009 MUTEX_UNLOCK(&rcfile_mutex
);
2013 return send_error(ctx
, rc
);
2016 static gpg_error_t
dump_command(assuan_context_t ctx
, char *line
)
2020 struct client_s
*client
= assuan_get_pointer(ctx
);
2023 if (disable_list_and_dump
== 1)
2024 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2026 xmlDocDumpFormatMemory(client
->doc
, &xml
, &len
, 1);
2029 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
2030 return send_error(ctx
, GPG_ERR_ENOMEM
);
2033 pthread_cleanup_push(xmlFree
, xml
);
2034 rc
= xfer_data(ctx
, (char *)xml
, len
);
2035 pthread_cleanup_pop(1);
2036 return send_error(ctx
, rc
);
2039 static gpg_error_t
getconfig_command(assuan_context_t ctx
, char *line
)
2041 struct client_s
*client
= assuan_get_pointer(ctx
);
2043 char filename
[255]={0}, param
[747]={0};
2044 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2046 if (!line
|| !*line
)
2047 return send_error(ctx
, GPG_ERR_SYNTAX
);
2049 if (strchr(line
, ' ')) {
2050 sscanf(line
, " %254[^ ] %746c", filename
, param
);
2055 if (fp
&& !valid_filename(fp
))
2056 return send_error(ctx
, GPG_ERR_INV_VALUE
);
2058 paramp
= str_down(paramp
);
2059 if (!strcmp(paramp
, "cipher") && fp
) {
2060 struct crypto_s
*crypto
;
2062 rc
= init_client_crypto(&crypto
);
2064 rc
= read_data_header(fp
, &crypto
->hdr
, NULL
, NULL
);
2066 const char *t
= gcry_cipher_algo_name(cipher_to_gcrypt(crypto
->hdr
.flags
));
2075 cleanup_crypto(&crypto
);
2076 if (rc
&& gpg_err_code(rc
) != GPG_ERR_ENOENT
)
2077 return send_error(ctx
, rc
);
2082 else if (!strcmp(paramp
, "cipher_iterations") && fp
) {
2083 struct crypto_s
*crypto
;
2085 rc
= init_client_crypto(&crypto
);
2087 rc
= read_data_header(fp
, &crypto
->hdr
, NULL
, NULL
);
2089 tmp
= str_asprintf("%llu",
2090 (unsigned long long)crypto
->hdr
.iterations
);
2092 rc
= GPG_ERR_ENOMEM
;
2096 cleanup_crypto(&crypto
);
2097 if (rc
&& gpg_err_code(rc
) != GPG_ERR_ENOENT
)
2098 return send_error(ctx
, rc
);
2103 else if (!strcmp(paramp
, "passphrase"))
2104 return send_error(ctx
, GPG_ERR_UNKNOWN_OPTION
);
2106 p
= config_get_value(fp
? fp
: "global", paramp
);
2108 return send_error(ctx
, GPG_ERR_UNKNOWN_OPTION
);
2110 tmp
= expand_homedir(p
);
2113 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
2114 return send_error(ctx
, GPG_ERR_ENOMEM
);
2119 pthread_cleanup_push(xfree
, p
);
2120 rc
= xfer_data(ctx
, p
, strlen(p
));
2121 pthread_cleanup_pop(1);
2122 return send_error(ctx
, rc
);
2126 xmlXPathContextPtr xp
;
2127 xmlXPathObjectPtr result
;
2132 static void xpath_command_cleanup(void *arg
)
2134 struct xpath_s
*xpath
= arg
;
2139 req_cleanup(xpath
->req
);
2142 xmlBufferFree(xpath
->buf
);
2145 xmlXPathFreeObject(xpath
->result
);
2148 xmlXPathFreeContext(xpath
->xp
);
2151 static gpg_error_t
do_xpath(assuan_context_t ctx
, char *line
)
2154 struct client_s
*client
= assuan_get_pointer(ctx
);
2155 struct xpath_s _x
= {0};
2156 struct xpath_s
*xpath
= &_x
;
2158 if ((xpath
->req
= str_split(line
, "\t", 2)) == NULL
) {
2159 if (strv_printf(&xpath
->req
, "%s", line
) == 0)
2160 return GPG_ERR_ENOMEM
;
2163 xpath
->xp
= xmlXPathNewContext(client
->doc
);
2165 rc
= GPG_ERR_BAD_DATA
;
2169 xpath
->result
= xmlXPathEvalExpression((xmlChar
*)xpath
->req
[0], xpath
->xp
);
2170 if (!xpath
->result
) {
2171 rc
= GPG_ERR_BAD_DATA
;
2175 if (xmlXPathNodeSetIsEmpty(xpath
->result
->nodesetval
)) {
2176 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2180 rc
= recurse_xpath_nodeset(client
->doc
, xpath
->result
->nodesetval
,
2181 (xmlChar
*)xpath
->req
[1], &xpath
->buf
, 0, NULL
);
2184 else if (!xpath
->req
[1] && !xmlBufferLength(xpath
->buf
)) {
2185 rc
= GPG_ERR_NO_DATA
;
2188 else if (xpath
->req
[1]) {
2193 pthread_cleanup_push(xpath_command_cleanup
, &xpath
);
2194 rc
= xfer_data(ctx
, (char *)xmlBufferContent(xpath
->buf
),
2195 xmlBufferLength(xpath
->buf
));
2196 pthread_cleanup_pop(0);
2198 xpath_command_cleanup(xpath
);
2202 static gpg_error_t
xpath_command(assuan_context_t ctx
, char *line
)
2204 struct client_s
*client
= assuan_get_pointer(ctx
);
2206 struct argv_s
*args
[] = {
2207 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2211 if (disable_list_and_dump
== 1)
2212 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2214 rc
= parse_options(&line
, args
, client
);
2216 return send_error(ctx
, rc
);
2218 if (client
->opts
&OPT_INQUIRE
) {
2219 unsigned char *result
;
2222 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
2224 return send_error(ctx
, rc
);
2226 line
= (char *)result
;
2229 if (!line
|| !*line
)
2230 rc
= GPG_ERR_SYNTAX
;
2233 rc
= do_xpath(ctx
, line
);
2235 if (client
->opts
&OPT_INQUIRE
)
2238 return send_error(ctx
, rc
);
2241 static gpg_error_t
do_xpathattr(assuan_context_t ctx
, char *line
)
2243 struct client_s
*client
= assuan_get_pointer(ctx
);
2247 struct xpath_s _x
= {0};
2248 struct xpath_s
*xpath
= &_x
;
2250 if ((req
= str_split(line
, " ", 3)) == NULL
)
2251 return GPG_ERR_ENOMEM
;
2254 rc
= GPG_ERR_SYNTAX
;
2258 if (!strcasecmp(req
[0], "SET"))
2260 else if (!strcasecmp(req
[0], "DELETE"))
2263 rc
= GPG_ERR_SYNTAX
;
2267 if (!req
[1] || !req
[2]) {
2268 rc
= GPG_ERR_SYNTAX
;
2272 if ((xpath
->req
= str_split(req
[2], "\t", 3)) == NULL
) {
2273 rc
= GPG_ERR_ENOMEM
;
2277 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
)) {
2278 rc
= GPG_ERR_SYNTAX
;
2282 xpath
->xp
= xmlXPathNewContext(client
->doc
);
2284 rc
= GPG_ERR_BAD_DATA
;
2288 xpath
->result
= xmlXPathEvalExpression((xmlChar
*)xpath
->req
[0], xpath
->xp
);
2289 if (!xpath
->result
) {
2290 rc
= GPG_ERR_BAD_DATA
;
2294 if (xmlXPathNodeSetIsEmpty(xpath
->result
->nodesetval
)) {
2295 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2299 rc
= recurse_xpath_nodeset(client
->doc
, xpath
->result
->nodesetval
,
2300 (xmlChar
*)xpath
->req
[1], &xpath
->buf
, cmd
, (xmlChar
*)req
[1]);
2303 xpath_command_cleanup(xpath
);
2308 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2309 static gpg_error_t
xpathattr_command(assuan_context_t ctx
, char *line
)
2311 struct client_s
*client
= assuan_get_pointer(ctx
);
2313 struct argv_s
*args
[] = {
2314 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2318 if (disable_list_and_dump
== 1)
2319 return send_error(ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2321 rc
= parse_options(&line
, args
, client
);
2323 return send_error(ctx
, rc
);
2325 if (client
->opts
&OPT_INQUIRE
) {
2326 unsigned char *result
;
2329 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
2331 return send_error(ctx
, rc
);
2333 line
= (char *)result
;
2336 if (!line
|| !*line
)
2337 rc
= GPG_ERR_SYNTAX
;
2340 rc
= do_xpathattr(ctx
, line
);
2342 if (client
->opts
&OPT_INQUIRE
)
2345 return send_error(ctx
, rc
);
2348 static gpg_error_t
do_import(struct client_s
*client
, unsigned char *line
)
2350 char **req
, **path
= NULL
, **path_orig
= NULL
, *content
;
2351 xmlDocPtr doc
= NULL
;
2352 xmlNodePtr n
, root
, copy
;
2355 req
= str_split((char *)line
, "\t", 2);
2358 return GPG_ERR_SYNTAX
;
2361 path
= str_split(req
[1], "\t", 0);
2362 if (!content
|| !*content
) {
2363 rc
= GPG_ERR_SYNTAX
;
2367 if (path
&& !valid_element_path(path
, 0)) {
2368 rc
= GPG_ERR_INV_VALUE
;
2372 doc
= xmlReadDoc((xmlChar
*)content
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2374 rc
= GPG_ERR_BAD_DATA
;
2378 root
= xmlDocGetRootElement(doc
);
2379 rc
= validate_import(root
);
2384 path_orig
= strv_dup(path
);
2386 log_write("%s(%i): %s", __FILE__
, __LINE__
, pwmd_strerror(GPG_ERR_ENOMEM
));
2387 rc
= GPG_ERR_ENOMEM
;
2391 xmlChar
*a
= xmlGetProp(root
, (xmlChar
*)"_name");
2393 strv_free(path_orig
);
2394 rc
= GPG_ERR_ENOMEM
;
2398 if (strv_printf(&path
, "%s", (char *)a
) == 0) {
2400 strv_free(path_orig
);
2401 rc
= GPG_ERR_ENOMEM
;
2406 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, 0);
2408 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
2409 strv_free(path_orig
);
2414 n
= find_elements(client
->doc
, n
->children
, path
+1, &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
2416 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
2417 strv_free(path_orig
);
2421 xmlNodePtr parent
= n
->parent
;
2432 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
) {
2433 n
= create_element_path(client
, &path
, &rc
, NULL
);
2439 copy
= xmlCopyNodeList(root
);
2440 n
= xmlAddChildList(n
, copy
);
2442 rc
= GPG_ERR_BAD_DATA
;
2445 /* Check if the content root element can create a DTD root element. */
2446 if (!xmlStrEqual((xmlChar
*)"element", root
->name
)) {
2447 rc
= GPG_ERR_SYNTAX
;
2453 if ((a
= xmlGetProp(root
, (xmlChar
*)"_name")) == NULL
) {
2454 rc
= GPG_ERR_SYNTAX
;
2458 char *tmp
= str_dup((char *)a
);
2460 int literal
= is_literal_element(&tmp
);
2462 if (!valid_xml_element((xmlChar
*)tmp
) || literal
) {
2464 rc
= GPG_ERR_INV_VALUE
;
2468 if (strv_printf(&path
, "%s", tmp
) == 0) {
2470 rc
= GPG_ERR_ENOMEM
;
2475 n
= find_root_element(client
->doc
, &path
, &rc
, NULL
, 0, 1);
2477 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
2478 rc
= GPG_ERR_BAD_DATA
;
2482 /* Overwriting the existing tree. */
2489 xmlSetProp(root
, (xmlChar
*)"_name", (xmlChar
*)path
[0]);
2490 n
= xmlCopyNode(root
, 1);
2491 n
= xmlAddChildList(xmlDocGetRootElement(client
->doc
), n
);
2495 rc
= update_element_mtime(n
->parent
);
2508 static gpg_error_t
import_command(assuan_context_t ctx
, char *line
)
2511 struct client_s
*client
= assuan_get_pointer(ctx
);
2512 unsigned char *result
;
2515 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
2517 return send_error(ctx
, rc
);
2519 rc
= do_import(client
, result
);
2520 return send_error(ctx
, rc
);
2523 static gpg_error_t
do_lock(struct client_s
*client
, int add
)
2525 gpg_error_t rc
= lock_file_mutex(client
, add
);
2528 client
->flags
|= FLAG_LOCK_CMD
;
2533 static gpg_error_t
lock_command(assuan_context_t ctx
, char *line
)
2535 struct client_s
*client
= assuan_get_pointer(ctx
);
2536 gpg_error_t rc
= do_lock(client
, 0);
2538 return send_error(ctx
, rc
);
2541 static gpg_error_t
unlock_command(assuan_context_t ctx
, char *line
)
2543 struct client_s
*client
= assuan_get_pointer(ctx
);
2546 rc
= unlock_file_mutex(client
, 0);
2547 return send_error(ctx
, rc
);
2550 static gpg_error_t
option_command(assuan_context_t ctx
, const char *name
,
2553 struct client_s
*client
= assuan_get_pointer(ctx
);
2554 struct agent_s
*agent
= client
->crypto
->agent
;
2557 log_write1("OPTION name='%s' value='%s'", name
, value
);
2559 if (strcasecmp(name
, (char *)"log_level") == 0) {
2563 l
= strtol(value
, NULL
, 10);
2566 return gpg_error(GPG_ERR_INV_VALUE
);
2569 MUTEX_LOCK(&rcfile_mutex
);
2570 config_set_int_param(&global_config
, "global", "log_level", value
);
2571 MUTEX_UNLOCK(&rcfile_mutex
);
2574 else if (strcasecmp(name
, (char *)"lock-timeout") == 0) {
2579 n
= strtol(value
, &p
, 10);
2581 return gpg_error(GPG_ERR_INV_VALUE
);
2584 client
->lock_timeout
= n
;
2587 else if (strcasecmp(name
, (char *)"NAME") == 0) {
2588 char *tmp
= pthread_getspecific(thread_name_key
);
2593 if (!value
|| !*value
) {
2594 pthread_setspecific(thread_name_key
, str_dup(""));
2598 pthread_setspecific(thread_name_key
, str_dup(value
));
2601 else if (strcasecmp(name
, (char *)"lc-messages") == 0) {
2602 rc
= set_agent_option(client
->crypto
->agent
, "lc-messages", value
);
2604 xfree(agent
->lc_messages
);
2605 agent
->lc_messages
= str_dup(value
);
2608 else if (strcasecmp(name
, (char *)"lc-ctype") == 0) {
2609 rc
= set_agent_option(client
->crypto
->agent
, "lc-ctype", value
);
2611 xfree(agent
->lc_ctype
);
2612 agent
->lc_ctype
= str_dup(value
);
2615 else if (strcasecmp(name
, (char *)"ttyname") == 0) {
2616 rc
= set_agent_option(client
->crypto
->agent
, "ttyname", value
);
2618 xfree(agent
->ttyname
);
2619 agent
->ttyname
= str_dup(value
);
2622 else if (strcasecmp(name
, (char *)"ttytype") == 0) {
2623 rc
= set_agent_option(client
->crypto
->agent
, "ttytype", value
);
2625 xfree(agent
->ttytype
);
2626 agent
->ttytype
= str_dup(value
);
2629 else if (strcasecmp(name
, (char *)"display") == 0) {
2630 rc
= set_agent_option(client
->crypto
->agent
, "display", value
);
2632 xfree(agent
->display
);
2633 agent
->display
= str_dup(value
);
2636 else if (strcasecmp(name
, (char *)"desc") == 0) {
2637 if (client
->crypto
->agent
->desc
)
2638 xfree(client
->crypto
->agent
->desc
);
2640 client
->crypto
->agent
->desc
= str_dup(value
);
2641 if (!client
->crypto
->agent
->desc
)
2642 rc
= GPG_ERR_ENOMEM
;
2645 else if (strcasecmp(name
, "pinentry_timeout") == 0) {
2652 n
= (int)strtol(value
, &p
, 10);
2655 return gpg_error(GPG_ERR_INV_VALUE
);
2657 MUTEX_LOCK(&rcfile_mutex
);
2658 config_set_int_param(global_config
, client
->filename
? client
->filename
:
2659 "global", "pinentry_timeout", value
);
2660 MUTEX_UNLOCK(&rcfile_mutex
);
2665 return gpg_error(GPG_ERR_UNKNOWN_OPTION
);
2671 static gpg_error_t
do_rename(assuan_context_t ctx
, char *line
)
2673 struct client_s
*client
= assuan_get_pointer(ctx
);
2675 char **req
, **src
, *dst
;
2678 req
= str_split(line
, " ", 0);
2680 if (!req
|| !req
[0] || !req
[1]) {
2682 return GPG_ERR_SYNTAX
;
2686 is_literal_element(&dst
);
2688 if (!valid_xml_element((xmlChar
*)dst
)) {
2690 return GPG_ERR_INV_VALUE
;
2693 if (strchr(req
[0], '\t'))
2694 src
= str_split(req
[0], "\t", 0);
2696 src
= str_split(req
[0], " ", 0);
2698 if (!src
|| !*src
) {
2699 rc
= GPG_ERR_SYNTAX
;
2703 n
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, 0);
2705 n
= find_elements(client
->doc
, n
->children
, src
+1, &rc
, NULL
, NULL
,
2706 NULL
, 0, 0, NULL
, 0);
2711 xmlChar
*a
= xmlGetProp(n
, (xmlChar
*)"_name");
2713 rc
= GPG_ERR_ENOMEM
;
2717 /* To prevent unwanted effects:
2719 * <root name="a"><b/></root>
2723 if (xmlStrEqual(a
, (xmlChar
*)dst
)) {
2725 rc
= GPG_ERR_AMBIGUOUS_NAME
;
2734 for (p
= src
; *p
; p
++) {
2737 strv_printf(&tmp
, "%s", *p
);
2741 strv_printf(&tmp
, "!%s", dst
);
2742 ndst
= find_root_element(client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
2743 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
) {
2749 ndst
= find_elements(client
->doc
, ndst
->children
, tmp
+1, &rc
, NULL
,
2750 NULL
, NULL
, 0, 0, NULL
, 0);
2753 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2758 /* Target may exist:
2761 * <root name="b" target="a"/>
2769 rc
= GPG_ERR_AMBIGUOUS_NAME
;
2775 xmlFreeNodeList(ndst
);
2778 rc
= add_attribute(n
, "_name", dst
);
2786 static gpg_error_t
rename_command(assuan_context_t ctx
, char *line
)
2788 struct client_s
*client
= assuan_get_pointer(ctx
);
2790 struct argv_s
*args
[] = {
2791 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2795 rc
= parse_options(&line
, args
, client
);
2797 return send_error(ctx
, rc
);
2799 if (client
->opts
&OPT_INQUIRE
) {
2800 unsigned char *result
;
2803 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
2805 return send_error(ctx
, rc
);
2807 line
= (char *)result
;
2810 rc
= do_rename(ctx
, line
);
2812 if (client
->opts
&OPT_INQUIRE
)
2815 return send_error(ctx
, rc
);
2818 static gpg_error_t
do_copy(assuan_context_t ctx
, char *line
)
2820 struct client_s
*client
= assuan_get_pointer(ctx
);
2822 char **req
, **src
= NULL
, **dst
= NULL
;
2823 xmlNodePtr nsrc
, ndst
, new = NULL
;
2825 req
= str_split(line
, " ", 0);
2826 if (!req
|| !req
[0] || !req
[1]) {
2828 return GPG_ERR_SYNTAX
;
2831 if (strchr(req
[0], '\t'))
2832 src
= str_split(req
[0], "\t", 0);
2834 src
= str_split(req
[0], " ", 0);
2836 if (!src
|| !*src
) {
2837 rc
= GPG_ERR_SYNTAX
;
2841 if (strchr(req
[1], '\t'))
2842 dst
= str_split(req
[1], "\t", 0);
2844 dst
= str_split(req
[1], " ", 0);
2846 if (!dst
|| !*dst
) {
2847 rc
= GPG_ERR_SYNTAX
;
2851 if (!valid_element_path(dst
, 0)) {
2852 rc
= GPG_ERR_INV_VALUE
;
2856 nsrc
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, 0);
2858 nsrc
= find_elements(client
->doc
, nsrc
->children
, src
+1, &rc
, NULL
,
2859 NULL
, NULL
, 0, 0, NULL
, 0);
2864 new = xmlCopyNodeList(nsrc
);
2866 rc
= GPG_ERR_ENOMEM
;
2871 ndst
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2872 if (ndst
&& dst
[1]) {
2874 ndst
= find_elements(client
->doc
, ndst
->children
, dst
+1, &rc
, NULL
,
2875 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
2882 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2885 ndst
= create_element_path(client
, &dst
, &rc
, NULL
);
2890 /* Merge any attributes from the src node to the initial dst node. */
2891 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
) {
2892 if (xmlStrEqual(attr
->name
, (xmlChar
*)"_name"))
2895 xmlAttrPtr a
= xmlHasProp(ndst
, attr
->name
);
2899 xmlChar
*tmp
= xmlNodeGetContent(attr
->children
);
2900 xmlNewProp(ndst
, attr
->name
, tmp
);
2902 rc
= add_attribute(ndst
, NULL
, NULL
);
2905 xmlNodePtr n
= ndst
->children
;
2908 ndst
->children
= NULL
;
2910 if (new->children
) {
2911 n
= xmlCopyNodeList(new->children
);
2913 rc
= GPG_ERR_ENOMEM
;
2917 n
= xmlAddChildList(ndst
, n
);
2919 rc
= GPG_ERR_ENOMEM
;
2923 rc
= update_element_mtime(xmlDocGetRootElement(client
->doc
) ==
2924 ndst
->parent
? ndst
: ndst
->parent
);
2930 xmlFreeNodeList(new);
2945 static gpg_error_t
copy_command(assuan_context_t ctx
, char *line
)
2947 struct client_s
*client
= assuan_get_pointer(ctx
);
2949 struct argv_s
*args
[] = {
2950 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2954 rc
= parse_options(&line
, args
, client
);
2956 return send_error(ctx
, rc
);
2958 if (client
->opts
&OPT_INQUIRE
) {
2959 unsigned char *result
;
2962 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
2964 return send_error(ctx
, rc
);
2966 line
= (char *)result
;
2969 rc
= do_copy(ctx
, line
);
2971 if (client
->opts
&OPT_INQUIRE
)
2974 return send_error(ctx
, rc
);
2977 static gpg_error_t
do_move(assuan_context_t ctx
, char *line
)
2979 struct client_s
*client
= assuan_get_pointer(ctx
);
2981 char **req
, **src
= NULL
, **dst
= NULL
;
2982 xmlNodePtr nsrc
, ndst
= NULL
;
2984 req
= str_split(line
, " ", 0);
2986 if (!req
|| !req
[0] || !req
[1]) {
2988 return GPG_ERR_SYNTAX
;
2991 if (strchr(req
[0], '\t'))
2992 src
= str_split(req
[0], "\t", 0);
2994 src
= str_split(req
[0], " ", 0);
2996 if (!src
|| !*src
) {
2997 rc
= GPG_ERR_SYNTAX
;
3001 if (strchr(req
[1], '\t'))
3002 dst
= str_split(req
[1], "\t", 0);
3004 dst
= str_split(req
[1], " ", 0);
3006 nsrc
= find_root_element(client
->doc
, &src
, &rc
, NULL
, 0, 0);
3008 nsrc
= find_elements(client
->doc
, nsrc
->children
, src
+1, &rc
, NULL
,
3009 NULL
, NULL
, 0, 0, NULL
, 0);
3015 if (!valid_element_path(dst
, 0)) {
3016 rc
= GPG_ERR_INV_VALUE
;
3020 ndst
= find_root_element(client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3022 ndst
= find_elements(client
->doc
, ndst
->children
, dst
+1, &rc
, NULL
,
3023 NULL
, NULL
, 0, 0, NULL
, 0);
3026 ndst
= xmlDocGetRootElement(client
->doc
);
3028 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
) {
3030 rc
= GPG_ERR_CONFLICT
;
3035 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3041 xmlChar
*a
= node_has_attribute(nsrc
, (xmlChar
*)"_name");
3042 xmlNodePtr dup
= find_element(ndst
->children
, (char *)a
, NULL
);
3049 if (ndst
== xmlDocGetRootElement(client
->doc
)) {
3050 xmlNodePtr n
= nsrc
;
3053 while (n
->parent
&& n
->parent
!= ndst
)
3056 xmlChar
*a
= node_has_attribute(n
, (xmlChar
*)"_name");
3057 xmlChar
*b
= node_has_attribute(nsrc
, (xmlChar
*)"_name");
3059 if (xmlStrEqual(a
, b
)) {
3061 xmlUnlinkNode(nsrc
);
3071 xmlFreeNodeList(dup
);
3080 xmlChar
*name
= node_has_attribute(nsrc
, (xmlChar
*)"_name");
3082 if (nsrc
->parent
== xmlDocGetRootElement(client
->doc
)
3083 && !strcmp((char *)name
, *dst
)) {
3085 rc
= GPG_ERR_CONFLICT
;
3090 ndst
= create_element_path(client
, &dst
, &rc
, nsrc
);
3096 update_element_mtime(nsrc
->parent
);
3097 xmlUnlinkNode(nsrc
);
3098 ndst
= xmlAddChildList(ndst
, nsrc
);
3101 rc
= GPG_ERR_ENOMEM
;
3103 update_element_mtime(ndst
->parent
);
3118 static gpg_error_t
move_command(assuan_context_t ctx
, char *line
)
3120 struct client_s
*client
= assuan_get_pointer(ctx
);
3122 struct argv_s
*args
[] = {
3123 &(struct argv_s
) { "inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3127 rc
= parse_options(&line
, args
, client
);
3129 return send_error(ctx
, rc
);
3131 if (client
->opts
&OPT_INQUIRE
) {
3132 unsigned char *result
;
3135 rc
= assuan_inquire(ctx
, "DATA", &result
, &len
, 0);
3137 return send_error(ctx
, rc
);
3139 line
= (char *)result
;
3142 rc
= do_move(ctx
, line
);
3144 if (client
->opts
&OPT_INQUIRE
)
3147 return send_error(ctx
, rc
);
3150 static gpg_error_t
ls_command(assuan_context_t ctx
, char *line
)
3153 char *tmp
= str_asprintf("%s/data", homedir
);
3154 char *dir
= expand_homedir(tmp
);
3155 DIR *d
= opendir(dir
);
3157 rc
= gpg_error_from_syserror();
3162 return send_error(ctx
, rc
);
3165 size_t len
= offsetof(struct dirent
, d_name
)+pathconf(dir
, _PC_NAME_MAX
)+1;
3166 struct dirent
*p
= xmalloc(len
), *cur
= NULL
;
3172 while (!readdir_r(d
, p
, &cur
) && cur
) {
3173 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3175 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.' && cur
->d_name
[2] == '\0')
3178 tmp
= str_asprintf("%s%s\n", list
? list
: "", cur
->d_name
);
3184 rc
= GPG_ERR_ENOMEM
;
3196 return send_error(ctx
, rc
);
3199 return send_error(ctx
, GPG_ERR_NO_DATA
);
3201 list
[strlen(list
)-1] = 0;
3202 rc
= xfer_data(ctx
, list
, strlen(list
));
3204 return send_error(ctx
, rc
);
3207 static gpg_error_t
bye_notify(assuan_context_t ctx
, char *line
)
3209 struct client_s
*cl
= assuan_get_pointer(ctx
);
3212 if (cl
->thd
->remote
) {
3216 rc
= gnutls_bye(cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3217 if (rc
== GNUTLS_E_AGAIN
)
3219 } while (rc
== GNUTLS_E_AGAIN
);
3223 /* This will let assuan_process_next() return. */
3224 fcntl(cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3225 cl
->last_rc
= 0; // BYE command result
3229 static gpg_error_t
reset_notify(assuan_context_t ctx
, char *line
)
3231 struct client_s
*client
= assuan_get_pointer(ctx
);
3234 cleanup_client(client
);
3240 * This is called before every Assuan command.
3242 static gpg_error_t
command_startup(assuan_context_t ctx
, const char *name
)
3244 struct client_s
*client
= assuan_get_pointer(ctx
);
3246 struct command_table_s
*cmd
= NULL
;
3248 log_write1("command='%s'", name
);
3249 client
->last_rc
= client
->opts
= 0;
3251 for (int i
= 0; command_table
[i
]; i
++) {
3252 if (!strcasecmp(name
, command_table
[i
]->name
)) {
3253 if (command_table
[i
]->ignore_startup
)
3255 cmd
= command_table
[i
];
3260 client
->last_rc
= rc
= gpg_error(file_modified(client
, cmd
));
3265 * This is called after every Assuan command.
3267 static void command_finalize(assuan_context_t ctx
, gpg_error_t rc
)
3269 struct client_s
*client
= assuan_get_pointer(ctx
);
3271 if (!(client
->flags
&FLAG_LOCK_CMD
))
3272 unlock_file_mutex(client
, 0);
3274 log_write1(_("command completed: rc=%u"), client
->last_rc
);
3275 client
->last_rc
= gpg_error(GPG_ERR_UNKNOWN_COMMAND
);
3278 static gpg_error_t
help_command(assuan_context_t ctx
, char *line
)
3283 if (!line
|| !*line
) {
3285 char *help
= str_dup(_(
3286 "Usage: HELP [<COMMAND>]\n"
3287 "For commands that take an element path as an argument, each element is "
3288 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3292 for (i
= 0; command_table
[i
]; i
++) {
3293 if (!command_table
[i
]->help
)
3296 tmp
= str_asprintf("%s %s", help
, command_table
[i
]->name
);
3301 tmp
= strip_texi_and_wrap(help
);
3303 rc
= xfer_data(ctx
, tmp
, strlen(tmp
));
3305 return send_error(ctx
, rc
);
3308 for (i
= 0; command_table
[i
]; i
++) {
3309 if (!strcasecmp(line
, command_table
[i
]->name
)) {
3312 if (!command_table
[i
]->help
)
3315 help
= strip_texi_and_wrap(command_table
[i
]->help
);
3316 tmp
= str_asprintf(_("Usage: %s"), help
);
3318 rc
= xfer_data(ctx
, tmp
, strlen(tmp
));
3320 return send_error(ctx
, rc
);
3324 return send_error(ctx
, GPG_ERR_INV_NAME
);
3327 static void new_command(const char *name
, int ignore
, int unlock
,
3328 gpg_error_t (*handler
)(assuan_context_t
, char *), const char *help
)
3333 for (i
= 0; command_table
[i
]; i
++);
3335 command_table
= xrealloc(command_table
, (i
+2)*sizeof(struct command_table_s
*));
3336 command_table
[i
] = xcalloc(1, sizeof(struct command_table_s
));
3337 command_table
[i
]->name
= name
;
3338 command_table
[i
]->handler
= handler
;
3339 command_table
[i
]->ignore_startup
= ignore
;
3340 command_table
[i
]->unlock
= unlock
;
3341 command_table
[i
++]->help
= help
;
3342 command_table
[i
] = NULL
;
3345 void deinit_commands()
3349 for (i
= 0; command_table
[i
]; i
++)
3350 xfree(command_table
[i
]);
3352 xfree(command_table
);
3355 static int sort_commands(const void *arg1
, const void *arg2
)
3357 struct command_table_s
* const *a
= arg1
;
3358 struct command_table_s
* const *b
= arg2
;
3367 return strcmp((*a
)->name
, (*b
)->name
);
3370 static gpg_error_t
passwd_command(assuan_context_t ctx
, char *line
)
3372 struct client_s
*client
= assuan_get_pointer(ctx
);
3374 struct argv_s
*args
[] = {
3375 &(struct argv_s
) { "reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
3376 &(struct argv_s
) { "s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
3380 if (client
->flags
&FLAG_NEW
)
3381 return send_error(ctx
, GPG_ERR_INV_STATE
);
3383 client
->crypto
->save
.s2k_count
= config_get_ulong(client
->filename
, "s2k_count");
3384 rc
= parse_options(&line
, args
, client
);
3386 return send_error(ctx
, rc
);
3388 if (!rc
&& client
->opts
&OPT_RESET
) {
3389 rc
= cache_clear(client
->md5file
);
3391 send_status_all(STATUS_CACHE
, NULL
);
3395 if (client
->crypto
->save
.s2k_count
)
3396 rc
= send_to_agent(client
->crypto
->agent
, NULL
, NULL
,
3397 "OPTION s2k-count=%lu", client
->crypto
->save
.s2k_count
);
3400 rc
= agent_passwd(client
->crypto
);
3403 return send_error(ctx
, rc
);
3406 static gpg_error_t
parse_keygrip_opt_sign(void * data
, void * value
)
3408 struct client_s
*client
= data
;
3411 client
->opts
|= OPT_SIGN
;
3415 static gpg_error_t
keygrip_command(assuan_context_t ctx
, char *line
)
3417 struct client_s
*client
= assuan_get_pointer(ctx
);
3419 struct crypto_s
*crypto
;
3420 struct argv_s
*args
[] = {
3421 &(struct argv_s
) { "sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
3425 if (!line
|| !*line
)
3426 return send_error(ctx
, GPG_ERR_SYNTAX
);
3428 rc
= parse_options(&line
, args
, client
);
3430 return send_error(ctx
, rc
);
3432 if (!valid_filename(line
))
3433 return send_error(ctx
, GPG_ERR_INV_VALUE
);
3435 rc
= init_client_crypto(&crypto
);
3437 return send_error(ctx
, rc
);
3439 rc
= read_data_file(line
, crypto
);
3441 char *hexgrip
= NULL
;
3443 if (client
->opts
&OPT_SIGN
) {
3444 if (valid_keygrip(crypto
->sign_grip
, sizeof(crypto
->sign_grip
)))
3445 hexgrip
= bin2hex(crypto
->sign_grip
, sizeof(crypto
->sign_grip
));
3449 hexgrip
= bin2hex(crypto
->grip
, sizeof(crypto
->grip
));
3452 rc
= GPG_ERR_ENOMEM
;
3454 rc
= xfer_data(ctx
, hexgrip
, strlen(hexgrip
));
3459 cleanup_crypto(&crypto
);
3460 return send_error(ctx
, rc
);
3463 static gpg_error_t
parse_opt_data(void * data
, void * value
)
3465 struct client_s
*client
= data
;
3468 client
->opts
|= OPT_DATA
;
3472 static gpg_error_t
getinfo_command(assuan_context_t ctx
, char *line
)
3474 struct client_s
*client
= assuan_get_pointer(ctx
);
3476 char buf
[ASSUAN_LINELENGTH
];
3477 struct argv_s
*args
[] = {
3478 &(struct argv_s
) { "data", OPTION_TYPE_NOARG
, parse_opt_data
},
3482 rc
= parse_options(&line
, args
, client
);
3484 return send_error(ctx
, rc
);
3486 if (!strcasecmp(line
, "clients")) {
3487 if (client
->opts
&OPT_DATA
) {
3488 MUTEX_LOCK(&cn_mutex
);
3489 snprintf(buf
, sizeof(buf
), "%i", slist_length(cn_thread_list
));
3490 MUTEX_UNLOCK(&cn_mutex
);
3491 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3494 rc
= send_status(ctx
, STATUS_CLIENTS
, NULL
);
3496 else if (!strcasecmp(line
, "cache")) {
3497 if (client
->opts
&OPT_DATA
) {
3498 snprintf(buf
, sizeof(buf
), "%u", cache_file_count());
3499 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3502 rc
= send_status(ctx
, STATUS_CACHE
, NULL
);
3504 else if (!strcasecmp(line
, "pid")) {
3506 pid_t pid
= getpid();
3508 snprintf(buf
, sizeof(buf
), "%i", pid
);
3509 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3511 else if (!strcasecmp(line
, "version")) {
3512 char *buf
= str_asprintf("0x%06x %s", VERSION_HEX
,
3520 rc
= xfer_data(ctx
, buf
, strlen(buf
));
3523 else if (!strcasecmp(line
, "last_error")) {
3524 if (client
->last_error
)
3525 rc
= xfer_data(ctx
, client
->last_error
, strlen(client
->last_error
));
3527 rc
= GPG_ERR_NO_DATA
;
3530 rc
= gpg_error(GPG_ERR_SYNTAX
);
3532 return send_error(ctx
, rc
);
3535 static gpg_error_t
send_data_cb(void * user
, const void *buf
, size_t len
)
3537 assuan_context_t ctx
= user
;
3539 return assuan_send_data(ctx
, buf
, len
);
3542 static gpg_error_t
send_status_cb(void * user
, const char *line
)
3544 assuan_context_t ctx
= user
;
3545 char keyword
[200], *k
;
3548 for (p
= line
, k
= keyword
; *p
; p
++) {
3562 return assuan_write_status(ctx
, keyword
, *p
? p
: NULL
);
3565 static gpg_error_t
agent_command(assuan_context_t ctx
, char *line
)
3568 struct client_s
*client
= assuan_get_pointer(ctx
);
3570 if (!line
|| !*line
)
3571 return send_error(ctx
, GPG_ERR_SYNTAX
);
3573 assuan_set_flag(client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
3574 rc
= assuan_transact(client
->crypto
->agent
->ctx
, line
, send_data_cb
,
3575 client
->ctx
, agent_loopback_cb
, client
->crypto
, send_status_cb
,
3577 if (gpg_err_code(rc
) == GPG_ERR_ASS_CANCELED
) {
3581 rc
= assuan_write_line(client
->crypto
->agent
->ctx
, "CAN");
3583 rc
= assuan_read_line(client
->crypto
->agent
->ctx
, &line
, &len
);
3585 rc
= gpg_error(GPG_ERR_ASS_CANCELED
);
3589 assuan_set_flag(client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
3590 return send_error(ctx
, rc
);
3593 void init_commands()
3595 /* !BEGIN-HELP-TEXT!
3597 * This comment is used as a marker to generate the offline documentation
3598 * found in doc/pwmd.info. The indentation needs to be kept for the awk
3599 * script to determine where commands begin and end.
3601 new_command("HELP", 1, 1, help_command
, _(
3602 "HELP [<COMMAND>]\n"
3603 "Show available commands or command specific help text."
3606 new_command("AGENT", 1, 1, agent_command
, _(
3608 "Send a @command{gpg-agent} protocol @var{command} directly to the "
3609 "@command{gpg-agent}."
3612 new_command("GETINFO", 1, 1, getinfo_command
, _(
3613 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
3614 "Get server and other information: @var{cache} returns the number of cached "
3615 "documents via a status message. @var{clients} returns the number of "
3616 "connected clients via a status message. @var{pid} returns the process ID "
3617 "number of the server via a data response. @var{VERSION} returns the server "
3618 "version number and compile-time features with a data response with each "
3619 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
3620 "the last failed command when available. @xref{Status Messages}. "
3622 "When the @option{--data} option is specified then the result will be send "
3623 "via a data response rather than a status message."
3626 new_command("PASSWD", 0, 0, passwd_command
, _(
3627 "PASSWD [--reset] [--s2k-count=N]\n"
3628 "Changes the passphrase of the secret key required to open the current "
3629 "file. When the @option{--reset} option is passed then the cache entry for "
3630 "the current file will be reset and the passphrase, if any, will be required "
3631 "during the next @code{OPEN}. @xref{OPEN}."
3633 "The @option{--s2k-count} option sets number of hash iterations for a "
3634 "passphrase and must be either @code{0} to use the calibrated count of the "
3635 "machine (the default), or a value greater than or equal to @code{65536}. "
3639 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
3640 "KEYGRIP [--sign] <filename>\n"
3641 "Returns the hex encoded keygrip of the specified @var{filename} with a "
3644 "When the @option{--sign} option is specified then the key used for signing "
3645 "of the specified @var{filename} will be returned."
3648 new_command("OPEN", 1, 1, open_command
, _(
3649 "OPEN [--lock] [--no-pinentry] <filename> [<passphrase>]\n"
3650 "Opens @var{filename} using @var{passphrase}. When the filename is not "
3651 "found on the file-system then a new document will be created. If the file "
3652 "is found, it is looked for in the file cache. If cached and no "
3653 "@var{passphrase} was specified then the cached document is opened. When not "
3654 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
3655 "for decryption unless @option{--no-pinentry} was specified (see below)."
3657 "When the @option{--lock} option is passed then the file mutex will be "
3658 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
3659 "file has been opened."
3661 "By default, a pinentry is used to retrieve a passphrase when required. "
3662 "Passing @option{--no-pinentry} will disable pinentry use for the rest of "
3663 "the session. When pinentry use is disabled but required for some operation "
3664 "then a server @emph{INQUIRE} will be send to the client to retrieve the "
3665 "passphrase. See the @code{OPTION} command (@pxref{OPTION}), for pinentry "
3669 new_command("SAVE", 0, 0, save_command
, _(
3670 "SAVE [--no-passphrase] [--reset] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring [--sign-keygrip=hexstring]]\n"
3671 "Writes the @abbr{XML} document to disk. The file written to is the file that "
3672 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
3673 "new one or the option @option{--inquire-keyparam} was passed, then a new "
3674 "keypair will be generated and a pinentry will be used to prompt for the "
3675 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
3676 "passed, in which case the data file will not be passphrase protected."
3678 "The @option{--reset} option will clear the cache entry for the current file "
3681 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
3682 "an alternate cipher. The default is @code{aes256}. See the Configuration "
3683 "(@pxref{Configuration}) for available ciphers."
3685 "The @option{--cipher-iterations} option specifies the number of times to "
3686 "encrypt the XML data. The default is 0 although 1 iteration is still done."
3688 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
3689 "the client to obtain the key paramaters to use when generating a new "
3690 "keypair. The inquired data is expected to be an S-expression. If not "
3691 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
3692 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
3693 "that when this option is specified a new keypair will be generated "
3694 "reguardless if the file is a new one or not."
3696 "You can encrypt the data file to a public key other than the one that it "
3697 "was originally encrypted with by passing the @option{--keygrip} option with "
3698 "the hex encoded keygrip of the public key as its argument. The keygrip may "
3699 "be of any key that @command{gpg-agent} knows about. The "
3700 "@option{--sign-keygrip} option may also be used to sign with an alternate "
3701 "secret key. This option may be needed when using a smartcard."
3703 "The @option{--s2k-count} option sets number of hash iterations for a "
3704 "passphrase. A value less-than @code{65536} will use the machine calibrated "
3705 "value which is the default. This setting only affects new files. To change "
3706 "the setting, use the @code{PASSWD} command (@pxref{PASSWD})."
3709 new_command("ISCACHED", 1, 0, iscached_command
, _(
3710 "ISCACHED [--lock] <filename>\n"
3711 "An @emph{OK} response is returned if the specified @var{filename} is found "
3712 "in the file cache. If not found in the cache but exists on the filesystem "
3713 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
3716 "The @option{lock} option will lock the file mutex of @var{filename} when the "
3717 "file exists; it does not need to be opened nor cached."
3720 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
3721 "CLEARCACHE [<filename>]\n"
3722 "Clears a file cache entry for all or the specified @var{filename}."
3725 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
3726 "CACHETIMEOUT <filename> <seconds>\n"
3727 "The time in @var{seconds} until @var{filename} will be removed from the "
3728 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
3729 "the passphrase for each @code{OPEN} command (@pxref{OPEN}). "
3730 "@xref{Configuration}, and the @code{cache_timeout} parameter."
3733 new_command("LIST", 0, 1, list_command
, _(
3734 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
3735 "If no element path is given then a newline separated list of root elements "
3736 "is returned with a data response. If given, then all reachable elements "
3737 "of the specified element path are returned unless the @option{--no-recurse} "
3738 "option is specified. If specified, only the child elements of the element "
3739 "path are returned without recursing into grandchildren. Each resulting "
3740 "element is prefixed with the literal @code{!} character when the element "
3741 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
3743 "When the @option{--verbose} option is passed then each element path "
3744 "returned will have zero or more flags appened to it. These flags are "
3745 "delimited from the element path by a single space character. A flag itself "
3746 "is a single character. Flag @code{+} indicates that there are child nodes of "
3747 "the current element path. Flag @code{E} indicates that an element of an "
3748 "element path contained in a @var{target} attribute could not be found. Flag "
3749 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
3750 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
3751 "of a @var{target} attribute (see below)."
3753 "The @option{--with-target} option implies @option{--verbose} and will append "
3754 "an additional flag @code{T} followed by a single space then an element path. "
3755 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
3756 "current element when it contains a @var{target} attribute. When no "
3757 "@var{target} attribute is found then no flag will be appended."
3759 "The @option{--no-recurse} option limits the amount of data returned to only "
3760 "the listing of children of the specified element path and not any "
3763 "The @option{--all} option lists the entire element tree for each root "
3764 "element. This option also implies option @option{--verbose}."
3766 "When the @option{--inquire} option is passed then all remaining non-option "
3767 "arguments are retrieved via a server @emph{INQUIRE}."
3770 new_command("REALPATH", 0, 1, realpath_command
, _(
3771 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
3772 "Resolves all @code{target} attributes of the specified element path and "
3773 "returns the result with a data response. @xref{Target Attribute}, for details."
3775 "When the @option{--inquire} option is passed then all remaining non-option "
3776 "arguments are retrieved via a server @emph{INQUIRE}."
3779 new_command("STORE", 0, 1, store_command
, _(
3780 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
3781 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
3783 "Creates a new element path or modifies the @var{content} of an existing "
3784 "element. If only a single element is specified then a new root element is "
3785 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
3786 "set to the final @key{TAB} delimited element. If no @var{content} is "
3787 "specified after the final @key{TAB}, then the content of the element will "
3788 "be removed, or empty when creating a new element."
3790 "The only restriction of an element name is that it not contain whitespace "
3791 "or begin with the literal element character @code{!} unless specifying a "
3792 "literal element (@pxref{Target Attribute}). There is no whitespace between "
3793 "the @key{TAB} delimited elements. It is recommended that the content of an "
3794 "element be base64 encoded when it contains control or @key{TAB} characters "
3795 "to prevent @abbr{XML} and @command{pwmd} parsing errors."
3798 new_command("RENAME", 0, 1, rename_command
, _(
3799 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
3800 "Renames the specified @var{element} to the new @var{value}. If an element of "
3801 "the same name as the @var{value} already exists it will be overwritten."
3803 "When the @option{--inquire} option is passed then all remaining non-option "
3804 "arguments are retrieved via a server @emph{INQUIRE}."
3807 new_command("COPY", 0, 1, copy_command
, _(
3808 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
3809 "Copies the entire element tree starting from the child node of the source "
3810 "element, to the destination element path. If the destination element path "
3811 "does not exist then it will be created; otherwise it is overwritten."
3813 "Note that attributes from the source element are merged into the "
3814 "destination element when the destination element path exists. When an "
3815 "attribute of the same name exists in both the source and destination "
3816 "elements then the destination attribute will be updated to the source "
3819 "When the @option{--inquire} option is passed then all remaining non-option "
3820 "arguments are retrieved via a server @emph{INQUIRE}."
3823 new_command("MOVE", 0, 1, move_command
, _(
3824 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
3825 "Moves the source element path to the destination element path. If the "
3826 "destination is not specified then it will be moved to the root node of the "
3827 "document. If the destination is specified and exists then it will be "
3828 "overwritten; otherwise it will be created."
3830 "When the @option{--inquire} option is passed then all remaining non-option "
3831 "arguments are retrieved via a server @emph{INQUIRE}."
3834 new_command("DELETE", 0, 1, delete_command
, _(
3835 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
3836 "Removes the specified element path and all of its children. This may break "
3837 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
3838 "refers to this element or any of its children."
3840 "When the @option{--inquire} option is passed then all remaining non-option "
3841 "arguments are retrieved via a server @emph{INQUIRE}."
3844 new_command("GET", 0, 1, get_command
, _(
3845 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
3846 "Retrieves the content of the specified element. The content is returned "
3847 "with a data response."
3849 "When the @option{--inquire} option is passed then all remaining non-option "
3850 "arguments are retrieved via a server @emph{INQUIRE}."
3853 new_command("ATTR", 0, 1, attr_command
, _(
3854 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
3856 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
3858 " Stores or updates an @var{attribute} name and optional @var{value} of an "
3859 "element. When no @var{value} is specified any existing value will be removed."
3861 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
3863 " Removes an @var{attribute} from an element."
3865 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
3867 " Retrieves a newline separated list of attributes names and values "
3868 "from the specified element. Each attribute name and value is space delimited."
3870 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
3872 " Retrieves the value of an @var{attribute} from an element."
3875 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
3876 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
3879 "The @code{_mtime} attribute is updated each time an element is modified by "
3880 "either storing content, editing attributes or by deleting a child element. "
3881 "The @code{_ctime} attribute is created for each new element in an element "
3884 "When the @option{--inquire} option is passed then all remaining non-option "
3885 "arguments are retrieved via a server @emph{INQUIRE}."
3887 "@xref{Target Attribute}, for details about this special attribute."
3890 new_command("XPATH", 0, 1, xpath_command
, _(
3891 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
3892 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
3893 "specified, it is assumed the expression is a request to return a result. "
3894 "Otherwise, the result is set to the @var{value} argument and the document is "
3895 "updated. If there is no @var{value} after the @key{TAB} character, the value "
3896 "is assumed to be empty and the document is updated. For example:"
3899 "XPATH //element[@@_name='password']@key{TAB}\n"
3902 "would clear the content of all @code{password} elements in the data file "
3903 "while leaving off the trailing @key{TAB} would return all @code{password} "
3904 "elements in @abbr{XML} format."
3906 "When the @option{--inquire} option is passed then all remaining non-option "
3907 "arguments are retrieved via a server @emph{INQUIRE}."
3909 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
3910 "expression syntax."
3913 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
3914 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
3915 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
3916 "attributes and does not return a result. For the @var{SET} operation the "
3917 "@var{value} is optional but the field is required. If not specified then "
3918 "the attribute value will be empty. For example:"
3921 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
3924 "would create an @code{password} attribute for each @code{password} element "
3925 "found in the document. The attribute value will be empty but still exist."
3927 "When the @option{--inquire} option is passed then all remaining non-option "
3928 "arguments are retrieved via a server @emph{INQUIRE}."
3930 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
3931 "expression syntax."
3934 new_command("IMPORT", 0, 1, import_command
, _(
3935 "IMPORT <content>[<TAB>[!]element[<TAB>[!]child[..]]]\n"
3936 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
3938 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
3939 "argument is raw @abbr{XML} data. The content is created as a child of the "
3940 "specified element path and will overwrite an existing element of the same "
3941 "name. If an element of the element path does not exist then it will be "
3944 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
3948 new_command("DUMP", 0, 1, dump_command
, _(
3950 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
3951 "dumping a specific node."
3954 new_command("LOCK", 0, 0, lock_command
, _(
3956 "Locks the mutex associated with the opened file. This prevents other clients "
3957 "from sending commands to the same opened file until the client "
3958 "that sent this command either disconnects or sends the @code{UNLOCK} "
3959 "command. @xref{UNLOCK}."
3962 new_command("UNLOCK", 1, 0, unlock_command
, _(
3964 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
3965 "a commands' @option{lock} option. @xref{LOCK}."
3968 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
3969 "GETCONFIG [filename] <parameter>\n"
3970 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
3971 "data response. If no file has been opened then the value for @var{filename} "
3972 "or the default from the @samp{global} section will be returned. If a file "
3973 "has been opened and no @var{filename} is specified, a value previously "
3974 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
3977 new_command("OPTION", 1, 1, NULL
, _(
3978 "OPTION <NAME>=<VALUE>\n"
3979 "Sets a client option @var{name} to @var{value}. The value for an option is "
3980 "kept for the duration of the connection."
3984 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3987 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3990 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3993 " Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
3996 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
3998 "@item LC-MESSAGES\n"
3999 " Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4002 " Associates the thread ID of the connection with the specified textual "
4003 "representation. Useful for debugging log messages."
4005 "@item LOCK-TIMEOUT\n"
4006 " When not @code{0}, the duration in tenths of a second to wait for the file "
4007 "mutex which has been locked by another thread to be released before returning "
4008 "an error. When @code{-1}, then an error will be returned immediately."
4012 new_command("LS", 1, 1, ls_command
, _(
4014 "Lists the available data files stored in the data directory "
4015 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4018 new_command("RESET", 1, 1, NULL
, _(
4020 "Closes the currently opened file but keeps any previously set client options."
4023 new_command("NOP", 1, 1, NULL
, _(
4025 "Does nothing. Always returns successfully."
4028 /* !END-HELP-TEXT! */
4029 new_command("CANCEL", 1, 1, NULL
, NULL
);
4030 new_command("END", 1, 1, NULL
, NULL
);
4031 new_command("BYE", 1, 1, NULL
, NULL
);
4034 for (i
= 0; command_table
[i
]; i
++);
4035 qsort(command_table
, i
-1, sizeof(struct command_table_s
*), sort_commands
);
4038 gpg_error_t
register_commands(assuan_context_t ctx
)
4042 for (; command_table
[i
]; i
++) {
4043 if (!command_table
[i
]->handler
)
4046 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4047 command_table
[i
]->handler
, command_table
[i
]->help
);
4052 rc
= assuan_register_option_handler(ctx
, option_command
);
4056 rc
= assuan_register_bye_notify(ctx
, bye_notify
);
4060 rc
= assuan_register_reset_notify(ctx
, reset_notify
);
4064 rc
= assuan_register_pre_cmd_notify(ctx
, command_startup
);
4068 return assuan_register_post_cmd_notify(ctx
, command_finalize
);