2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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"
46 #include "util-misc.h"
55 /* Flags needed to be retained for a client across commands. */
56 #define FLAG_NEW 0x0001
57 #define FLAG_HAS_LOCK 0x0002
58 #define FLAG_LOCK_CMD 0x0004
59 #define FLAG_NO_PINENTRY 0x0008
60 #define FLAG_OPEN 0x0010
61 #define FLAG_KEEP_LOCK 0x0020
63 /* These are command option flags. */
64 #define OPT_INQUIRE 0x0001
65 #define OPT_NO_PASSPHRASE 0x0002
66 #define OPT_RESET 0x0004
67 #define OPT_LIST_RECURSE 0x0008
68 #define OPT_LIST_VERBOSE 0x0010
69 #define OPT_LOCK 0x0020
70 #define OPT_LOCK_ON_OPEN 0x0040
71 #define OPT_SIGN 0x0080
72 #define OPT_LIST_ALL 0x0100
73 #define OPT_LIST_WITH_TARGET 0x0200
74 #define OPT_DATA 0x0400
75 #define OPT_NO_AGENT 0x0800
78 /* The GETCONFIG command, for example, may fail because pwmd lost the
79 * gpg-agent connection. Update the recovered agent ctx. */
80 #define UPDATE_AGENT_CTX(client, crypto) do { \
81 if (crypto && crypto->agent && crypto->agent->did_restart \
82 && client->crypto && client->crypto->agent \
83 && client->crypto->agent->ctx && \
84 crypto->agent->ctx != client->crypto->agent->ctx) \
86 client->crypto->agent->ctx = crypto->agent->ctx; \
87 crypto->agent->ctx = NULL; \
91 #define UPDATE_AGENT_CTX(client, crypto)
94 struct command_table_s
97 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
100 int unlock
; // unlock the file mutex after validating the checksum
103 static struct command_table_s
**command_table
;
105 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
106 static gpg_error_t
validate_checksum (struct client_s
*,
107 struct cache_data_s
*);
108 static gpg_error_t
update_checksum (struct client_s
*client
);
111 unlock_file_mutex (struct client_s
*client
, int remove
)
115 // OPEN: keep the lock for the same file being reopened.
116 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
119 if (!(client
->flags
& FLAG_HAS_LOCK
))
120 return GPG_ERR_NOT_LOCKED
;
122 rc
= cache_unlock_mutex (client
->md5file
, remove
);
124 rc
= GPG_ERR_INV_STATE
;
126 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
132 lock_file_mutex (struct client_s
*client
, int add
)
135 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
137 if (client
->flags
& FLAG_HAS_LOCK
)
140 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
141 client
->lock_timeout
, add
, timeout
);
143 client
->flags
|= FLAG_HAS_LOCK
;
149 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
153 if (!(client
->flags
& FLAG_OPEN
))
154 return GPG_ERR_INV_STATE
;
156 rc
= lock_file_mutex (client
, 0);
157 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
159 rc
= validate_checksum (client
, NULL
);
160 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
163 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
166 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
167 unlock_file_mutex (client
, 0);
173 parse_xml (assuan_context_t ctx
, int new)
175 struct client_s
*client
= assuan_get_pointer (ctx
);
176 int cached
= client
->doc
!= NULL
;
181 client
->doc
= new_document ();
187 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
188 client
->crypto
->plaintext
= result
;
189 client
->crypto
->plaintext_len
= len
;
190 if (!client
->crypto
->plaintext
)
192 xmlFreeDoc (client
->doc
);
198 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
199 client
->crypto
->plaintext_len
, (xmlDocPtr
*)&client
->doc
);
205 free_client (struct client_s
*client
)
208 xmlFreeDoc (client
->doc
);
211 xfree (client
->filename
);
212 xfree (client
->last_error
);
216 cleanup_crypto_stage2 (client
->crypto
);
217 if (client
->crypto
->pkey_sexp
)
218 gcry_sexp_release (client
->crypto
->pkey_sexp
);
220 client
->crypto
->pkey_sexp
= NULL
;
221 memset (client
->crypto
->sign_grip
, 0,
222 sizeof (client
->crypto
->sign_grip
));
223 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
228 cleanup_client (struct client_s
*client
)
230 assuan_context_t ctx
= client
->ctx
;
231 struct client_thread_s
*thd
= client
->thd
;
232 struct crypto_s
*crypto
= client
->crypto
;
233 long lock_timeout
= client
->lock_timeout
;
234 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
235 struct pinentry_option_s pin_opts
;
236 xmlErrorPtr xml_error
= client
->xml_error
;
238 struct pinentry_option_s agent_pin_opts
;
240 if (crypto
&& crypto
->agent
)
241 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
242 sizeof(struct pinentry_option_s
));
245 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
246 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
247 free_client (client
);
248 memset (client
, 0, sizeof (struct client_s
));
249 client
->xml_error
= xml_error
;
250 client
->crypto
= crypto
;
253 client
->lock_timeout
= lock_timeout
;
254 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
255 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
257 if (crypto
&& crypto
->agent
)
258 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
259 sizeof(struct pinentry_option_s
));
264 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
266 struct client_s
*client
= assuan_get_pointer (ctx
);
268 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
269 int cached
= 0, keyarg
= key
? 1 : 0;
271 unsigned char *salted_key
= NULL
;
275 char *pin_title
= client
->pinentry_opts
.title
;
277 client
->crypto
->filename
= str_dup (client
->filename
);
278 if (cdata
|| client
->flags
& FLAG_NEW
)
280 if (cdata
&& !(client
->flags
& FLAG_NEW
))
282 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
287 cached
= cdata
!= NULL
;
291 if (!key
&& !IS_PKI (client
->crypto
)
292 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
294 if (client
->flags
& FLAG_NO_PINENTRY
)
296 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
303 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
306 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
313 if (!IS_PKI (client
->crypto
))
315 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
318 key
= gcry_malloc (keylen
);
319 memset (key
, 0, keylen
);
322 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
323 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
324 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
325 (void **)&salted_key
, &keysize
);
331 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
332 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
333 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
335 gcry_free (salted_key
);
339 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
344 if (client
->pinentry_opts
.title
!= pin_title
)
345 xfree (client
->pinentry_opts
.title
);
347 client
->pinentry_opts
.title
= pin_title
;
350 gcry_free (salted_key
);
354 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
357 gcry_free (salted_key
);
358 return GPG_ERR_ENOMEM
;
361 cdata
->key
= salted_key
;
362 cdata
->keylen
= keysize
;
365 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
370 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
372 free_cache_data_once (cdata
);
376 int timeout
= config_get_integer (client
->filename
,
382 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
384 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
385 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
386 client
->crypto
->plaintext_len
, &cdata
->doc
,
387 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
389 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
391 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
392 "%S", client
->crypto
->pkey_sexp
);
397 gcry_sexp_release (cdata
->sigkey
);
399 cdata
->sigkey
= NULL
;
400 if (!rc
&& IS_PKI (client
->crypto
))
401 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
402 "%S", client
->crypto
->sigpkey_sexp
);
404 if (!rc
&& !cache_add_file (client
->md5file
,
405 (client
->flags
& FLAG_NEW
)
407 : client
->crypto
->grip
, cdata
, timeout
))
411 free_cache_data_once (cdata
);
412 xmlFreeDoc (client
->doc
);
419 client
->flags
|= FLAG_OPEN
;
424 update_checksum (client
);
426 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
427 rc
= do_lock (client
, 0);
433 req_cleanup (void *arg
)
438 strv_free ((char **) arg
);
442 parse_open_opt_lock (void *data
, void *value
)
444 struct client_s
*client
= data
;
446 client
->opts
|= OPT_LOCK_ON_OPEN
;
451 parse_opt_inquire (void *data
, void *value
)
453 struct client_s
*client
= data
;
456 client
->opts
|= OPT_INQUIRE
;
461 update_checksum (struct client_s
*client
)
465 struct cache_data_s
*cdata
;
466 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
473 cdata
= cache_get_data (client
->md5file
);
477 cdata
->crc
= xmalloc (len
);
478 memcpy (cdata
->crc
, crc
, len
);
485 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
492 if (cdata
&& !cdata
->crc
)
493 return GPG_ERR_CHECKSUM
;
495 rc
= get_checksum (client
->filename
, &crc
, &len
);
500 n
= memcmp (cdata
->crc
, crc
, len
);
501 else if (client
->crc
)
502 n
= memcmp (client
->crc
, crc
, len
);
505 return n
? GPG_ERR_CHECKSUM
: 0;
509 do_open (assuan_context_t ctx
, const char *filename
, const char *password
)
511 struct client_s
*client
= assuan_get_pointer (ctx
);
512 struct cache_data_s
*cdata
;
516 if (!valid_filename (filename
))
517 return GPG_ERR_INV_VALUE
;
519 gcry_md_hash_buffer (GCRY_MD_MD5
, client
->md5file
, filename
,
521 client
->filename
= str_dup (filename
);
522 if (!client
->filename
)
523 return GPG_ERR_ENOMEM
;
526 cdata
= cache_get_data (client
->md5file
);
527 if (cdata
&& cdata
->doc
)
531 /* This will check that the key is cached in the agent which needs to
532 * be determined for files that share a keygrip. */
535 rc
= cache_iscached (client
->filename
, &defer
);
536 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
537 rc
= GPG_ERR_KEY_EXPIRED
;
540 if (!rc
&& !(client
->flags
& FLAG_NEW
))
541 rc
= validate_checksum (client
, cdata
);
544 if (!rc
&& client
->thd
->remote
545 && config_get_boolean (client
->filename
, "tcp_require_key"))
546 rc
= GPG_ERR_KEY_EXPIRED
;
549 if (!rc
&& !password
)
551 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
555 if (IS_PKI (client
->crypto
))
557 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
559 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
560 client
->crypto
->grip
);
561 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
563 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
564 client
->crypto
->sign_grip
);
569 rc
= open_finalize (ctx
, NULL
, 0);
575 /* There was an error accessing the file so clear the cache entry. The
576 * real error will be returned from read_data_file() since the file
577 * may have only disappeared. */
580 log_write ("%s: %s", filename
, pwmd_strerror (rc
));
581 rc
= cache_clear (client
->md5file
);
582 send_status_all (STATUS_CACHE
, NULL
);
589 rc
= read_data_file (client
->filename
, client
->crypto
);
592 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
593 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
595 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
599 client
->flags
|= FLAG_NEW
;
600 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
601 memset (client
->crypto
->sign_grip
, 0,
602 sizeof (client
->crypto
->sign_grip
));
603 rc
= open_finalize (ctx
, NULL
, 0);
607 if (password
&& IS_PKI (client
->crypto
))
610 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
616 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
621 open_command (assuan_context_t ctx
, char *line
)
624 struct client_s
*client
= assuan_get_pointer (ctx
);
625 char **req
, *filename
;
626 unsigned char md5file
[16];
628 assuan_peercred_t peer
;
629 struct argv_s
*args
[] = {
630 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
634 rc
= parse_options (&line
, args
, client
);
636 return send_error (ctx
, rc
);
638 req
= str_split (line
, " ", 2);
640 return send_error (ctx
, GPG_ERR_SYNTAX
);
642 rc
= do_validate_peer (ctx
, req
[0], &peer
);
646 return send_error (ctx
, rc
);
649 pthread_cleanup_push (req_cleanup
, req
);
651 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
652 /* This client may have locked a different file with ISCACHED --lock than
653 * the current filename. This will remove that lock. */
654 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
655 if (client
->flags
& FLAG_OPEN
||
656 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
658 typeof (client
->opts
) opts
= client
->opts
;
659 typeof (client
->flags
) flags
= client
->flags
;
662 client
->flags
|= FLAG_KEEP_LOCK
;
664 cleanup_client (client
);
666 client
->flags
|= flags
;
667 client
->flags
&= ~(FLAG_LOCK_CMD
);
669 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
672 /* Need to lock the mutex here because file_modified() cannot without
673 * knowing the filename. */
674 memcpy (client
->md5file
, md5file
, 16);
675 rc
= lock_file_mutex (client
, 1);
678 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
681 if (IS_PKI (client
->crypto
))
682 rc
= set_pinentry_mode (client
->crypto
->agent
,
683 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
688 rc
= do_open (ctx
, filename
, password
);
690 cleanup_client (client
);
692 cleanup_crypto_stage1 (client
->crypto
);
696 pthread_cleanup_pop (1);
698 if (!rc
&& client
->flags
& FLAG_NEW
)
699 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
702 (void) kill_scd (client
->crypto
->agent
);
704 return send_error (ctx
, rc
);
708 parse_opt_no_passphrase (void *data
, void *value
)
710 struct client_s
*client
= data
;
713 client
->opts
|= OPT_NO_PASSPHRASE
;
718 parse_save_opt_no_agent (void *data
, void *value
)
720 struct client_s
*client
= data
;
722 client
->opts
|= OPT_NO_AGENT
;
727 parse_save_opt_cipher (void *data
, void *value
)
729 struct client_s
*client
= data
;
730 int algo
= cipher_string_to_gcrypt ((char *) value
);
731 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
734 return GPG_ERR_INV_VALUE
;
736 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
741 parse_save_opt_keygrip (void *data
, void *value
)
743 struct client_s
*client
= data
;
745 if (!IS_PKI (client
->crypto
))
746 return GPG_ERR_INV_ARG
;
749 if (client
->crypto
->save
.pkey
)
750 gcry_sexp_release (client
->crypto
->save
.pkey
);
752 client
->crypto
->save
.pkey
= NULL
;
753 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
755 return GPG_ERR_INV_ARG
;
760 parse_save_opt_sign_keygrip (void *data
, void *value
)
762 struct client_s
*client
= data
;
764 if (!IS_PKI (client
->crypto
))
765 return GPG_ERR_INV_ARG
;
768 if (client
->crypto
->save
.sigpkey
)
769 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
771 client
->crypto
->save
.sigpkey
= NULL
;
772 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
774 return GPG_ERR_INV_ARG
;
779 parse_opt_s2k_count (void *data
, void *value
)
781 struct client_s
*client
= data
;
785 return GPG_ERR_INV_VALUE
;
787 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
792 parse_save_opt_iterations (void *data
, void *value
)
794 struct client_s
*client
= data
;
799 return GPG_ERR_INV_VALUE
;
802 n
= strtoull (v
, &p
, 10);
803 if (n
== UINT64_MAX
&& errno
)
804 return gpg_error_from_errno (errno
);
806 return GPG_ERR_INV_VALUE
;
808 client
->crypto
->save
.hdr
.iterations
= n
;
813 save_finalize (assuan_context_t ctx
)
815 struct client_s
*client
= assuan_get_pointer (ctx
);
817 xmlChar
*xmlbuf
= NULL
;
822 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
824 return GPG_ERR_ENOMEM
;
826 pthread_cleanup_push (xmlFree
, xmlbuf
);
828 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
829 && (client
->opts
& OPT_NO_AGENT
))
830 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
832 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
833 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
834 NULL
, &key
, &keylen
, 1, 1,
835 (client
->opts
& OPT_NO_PASSPHRASE
));
840 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
841 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
844 pubkey
= client
->crypto
->pkey_sexp
;
848 sigkey
= client
->crypto
->sigpkey_sexp
;
851 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
852 sigkey
= client
->crypto
->save
.sigpkey
;
856 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
857 client
->filename
, xmlbuf
, xmlbuflen
);
858 if (pubkey
== client
->crypto
->save
.pkey
)
862 gcry_sexp_release (client
->crypto
->pkey_sexp
);
863 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
864 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
865 client
->crypto
->grip
);
868 gcry_sexp_release (pubkey
);
870 client
->crypto
->save
.pkey
= NULL
;
874 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
876 if (sigkey
== client
->crypto
->save
.sigpkey
)
880 if (client
->crypto
->sigpkey_sexp
)
881 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
883 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
884 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
885 client
->crypto
->sign_grip
);
888 gcry_sexp_release (sigkey
);
890 client
->crypto
->save
.sigpkey
= NULL
;
899 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
900 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
904 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
905 send_status_all (STATUS_CACHE
, NULL
);
909 rc
= update_checksum (client
);
910 client
->flags
&= ~(FLAG_NEW
);
914 pthread_cleanup_pop (1); // xmlFree
919 parse_opt_reset (void *data
, void *value
)
921 struct client_s
*client
= data
;
924 client
->opts
|= OPT_RESET
;
929 save_command (assuan_context_t ctx
, char *line
)
931 struct client_s
*client
= assuan_get_pointer (ctx
);
934 struct argv_s
*args
[] = {
935 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
936 parse_opt_no_passphrase
},
937 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
938 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
940 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
941 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
942 parse_save_opt_sign_keygrip
},
943 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
944 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
945 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
946 parse_save_opt_iterations
},
947 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
948 parse_save_opt_no_agent
},
952 cleanup_save (&client
->crypto
->save
);
953 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
954 sizeof (file_header_t
));
955 client
->crypto
->save
.s2k_count
=
956 config_get_ulong (client
->filename
, "s2k_count");
958 if (client
->flags
& FLAG_NEW
)
959 client
->crypto
->save
.hdr
.iterations
=
960 config_get_ulonglong (client
->filename
, "cipher_iterations");
962 rc
= parse_options (&line
, args
, client
);
964 return send_error (ctx
, rc
);
966 if (!(client
->flags
& FLAG_NEW
))
967 client
->opts
&= ~OPT_NO_AGENT
;
968 else if (client
->opts
& OPT_NO_AGENT
)
970 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
971 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
974 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
975 && !(client
->opts
& OPT_INQUIRE
))
976 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
978 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
979 return send_error (ctx
, gpg_error_from_errno (errno
));
981 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
983 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
984 return send_error (ctx
, GPG_ERR_ENOANO
);
988 rc
= cache_iscached (client
->filename
, &defer
);
989 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
991 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
994 if ((!rc
&& defer
) || config_get_boolean ("global", "require_save_key"))
995 client
->opts
|= OPT_RESET
;
997 if (!rc
&& (client
->opts
& OPT_RESET
))
999 rc
= cache_clear (client
->md5file
);
1001 return send_error (ctx
, rc
);
1003 log_write ("%s: %s", client
->filename
,
1004 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1005 send_status_all (STATUS_CACHE
, NULL
);
1009 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
));
1012 return send_error (ctx
, rc
);
1015 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1016 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1017 && !(client
->opts
& OPT_NO_AGENT
))
1019 rc
= set_pinentry_mode (client
->crypto
->agent
,
1020 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1023 if (!(client
->flags
& FLAG_NEW
))
1025 struct crypto_s
*crypto
;
1029 /* Wanting to generate a new key. Require the key to open the
1030 current file before proceeding reguardless of the
1031 require_save_key configuration parameter. */
1032 rc
= cache_clear (client
->md5file
);
1035 rc
= init_client_crypto (&crypto
);
1037 crypto
->client_ctx
= client
->ctx
;
1041 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1042 client
->filename
, &key
, &keylen
);
1045 cleanup_crypto (&crypto
);
1049 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1053 struct inquire_data_s idata
= { 0 };
1054 char *params
= client
->opts
& OPT_INQUIRE
1055 ? NULL
: default_key_params (client
->crypto
);
1057 pthread_cleanup_push (xfree
, params
);
1058 idata
.crypto
= client
->crypto
;
1062 idata
.line
= params
;
1063 idata
.len
= strlen (params
);
1068 client
->crypto
->agent
->inquire_data
= &idata
;
1069 client
->crypto
->agent
->inquire_cb
= NULL
;
1070 rc
= generate_key (client
->crypto
, params
,
1071 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1072 pthread_cleanup_pop (1);
1078 rc
= save_finalize (ctx
);
1080 cleanup_crypto_stage1 (client
->crypto
);
1082 (void) kill_scd (client
->crypto
->agent
);
1084 return send_error (ctx
, rc
);
1088 do_delete (assuan_context_t ctx
, char *line
)
1090 struct client_s
*client
= assuan_get_pointer (ctx
);
1095 if (strchr (line
, '\t'))
1096 req
= str_split (line
, "\t", 0);
1098 req
= str_split (line
, " ", 0);
1101 return GPG_ERR_SYNTAX
;
1103 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1111 * No sub-node defined. Remove the entire node (root element).
1117 rc
= unlink_node (n
);
1126 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1134 rc
= unlink_node (n
);
1142 delete_command (assuan_context_t ctx
, char *line
)
1144 struct client_s
*client
= assuan_get_pointer (ctx
);
1146 struct argv_s
*args
[] = {
1147 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1151 rc
= parse_options (&line
, args
, client
);
1153 return send_error (ctx
, rc
);
1155 if (client
->opts
& OPT_INQUIRE
)
1157 unsigned char *result
;
1160 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1162 return send_error (ctx
, rc
);
1164 line
= (char *) result
;
1167 rc
= do_delete (ctx
, line
);
1169 if (client
->opts
& OPT_INQUIRE
)
1172 return send_error (ctx
, rc
);
1176 store_command (assuan_context_t ctx
, char *line
)
1178 struct client_s
*client
= assuan_get_pointer (ctx
);
1181 unsigned char *result
;
1183 xmlNodePtr n
, parent
;
1185 char *content
= NULL
;
1187 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1189 return send_error (ctx
, rc
);
1191 req
= str_split ((char *) result
, "\t", 0);
1195 return send_error (ctx
, GPG_ERR_SYNTAX
);
1197 len
= strv_length (req
);
1198 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1199 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1202 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1205 if (has_content
|| !*req
[len
- 1])
1208 content
= req
[len
- 1];
1209 req
[len
- 1] = NULL
;
1213 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1214 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1216 rc
= new_root_element (client
->doc
, *req
);
1220 return send_error (ctx
, GPG_ERR_SYNTAX
);
1229 return send_error (ctx
, rc
);
1234 if (req
[1] && *req
[1])
1237 parent
= create_elements_cb (n
, req
+ 1, &rc
, NULL
);
1239 parent
= find_elements (client
->doc
, n
->children
, req
+ 1, &rc
,
1240 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1246 n
= find_text_node (parent
->children
);
1248 xmlNodeSetContent (n
, (xmlChar
*) content
);
1250 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1252 update_element_mtime (parent
);
1257 return send_error (ctx
, rc
);
1261 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1266 int progress
= config_get_integer ("global", "xfer_progress");
1270 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1271 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1272 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1280 if (sent
+ to_send
> total
)
1281 to_send
= total
- sent
;
1283 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1284 flush
? 0 : to_send
);
1287 sent
+= flush
? 0 : to_send
;
1289 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1290 (sent
== total
&& flush
))
1291 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1293 if (!flush
&& !rc
&& sent
== total
)
1300 while (!rc
&& sent
< total
);
1306 do_get (assuan_context_t ctx
, char *line
)
1308 struct client_s
*client
= assuan_get_pointer (ctx
);
1313 req
= str_split (line
, "\t", 0);
1318 return GPG_ERR_SYNTAX
;
1321 n
= find_root_element (client
->doc
, &req
, &rc
, NULL
, 0, 0);
1330 find_elements (client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1337 if (!n
|| !n
->children
)
1338 return GPG_ERR_NO_DATA
;
1340 n
= find_text_node (n
->children
);
1341 if (!n
|| !n
->content
|| !*n
->content
)
1342 return GPG_ERR_NO_DATA
;
1344 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1349 get_command (assuan_context_t ctx
, char *line
)
1351 struct client_s
*client
= assuan_get_pointer (ctx
);
1353 struct argv_s
*args
[] = {
1354 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1358 rc
= parse_options (&line
, args
, client
);
1360 return send_error (ctx
, rc
);
1362 if (client
->opts
& OPT_INQUIRE
)
1364 unsigned char *result
;
1367 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1369 return send_error (ctx
, rc
);
1371 line
= (char *) result
;
1374 rc
= do_get (ctx
, line
);
1376 if (client
->opts
& OPT_INQUIRE
)
1379 return send_error (ctx
, rc
);
1382 static void list_command_cleanup1 (void *arg
);
1384 realpath_command (assuan_context_t ctx
, char *line
)
1386 struct string_s
*string
= NULL
;
1388 struct client_s
*client
= assuan_get_pointer (ctx
);
1389 struct argv_s
*args
[] = {
1390 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1394 rc
= parse_options (&line
, args
, client
);
1396 return send_error (ctx
, rc
);
1398 if (client
->opts
& OPT_INQUIRE
)
1400 unsigned char *result
;
1403 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1405 return send_error (ctx
, rc
);
1407 line
= (char *) result
;
1410 rc
= build_realpath (client
->doc
, line
, &string
);
1413 pthread_cleanup_push (list_command_cleanup1
, string
);
1414 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1415 pthread_cleanup_pop (1);
1418 if (client
->opts
& OPT_INQUIRE
)
1421 return send_error (ctx
, rc
);
1425 list_command_cleanup1 (void *arg
)
1428 string_free ((struct string_s
*) arg
, 1);
1432 list_command_cleanup2 (void *arg
)
1434 struct element_list_s
*elements
= arg
;
1440 int total
= slist_length (elements
->list
);
1443 for (i
= 0; i
< total
; i
++)
1445 char *tmp
= slist_nth_data (elements
->list
, i
);
1449 slist_free (elements
->list
);
1452 if (elements
->prefix
)
1453 xfree (elements
->prefix
);
1456 strv_free (elements
->req
);
1463 parse_list_opt_norecurse (void *data
, void *value
)
1465 struct client_s
*client
= data
;
1467 client
->opts
&= ~(OPT_LIST_RECURSE
);
1472 parse_list_opt_verbose (void *data
, void *value
)
1474 struct client_s
*client
= data
;
1476 client
->opts
|= OPT_LIST_VERBOSE
;
1481 parse_list_opt_target (void *data
, void *value
)
1483 struct client_s
*client
= data
;
1485 client
->opts
|= OPT_LIST_WITH_TARGET
;
1490 parse_list_opt_all (void *data
, void *value
)
1492 struct client_s
*client
= data
;
1494 client
->opts
|= OPT_LIST_ALL
;
1499 list_path_once (struct client_s
*client
, char *line
,
1500 struct element_list_s
*elements
, struct string_s
*result
)
1504 elements
->req
= str_split (line
, " ", 0);
1506 strv_printf (&elements
->req
, "%s", line
);
1508 rc
= create_path_list (client
->doc
, elements
, *elements
->req
);
1509 if (rc
== GPG_ERR_ELOOP
&& elements
->verbose
)
1514 int total
= slist_length (elements
->list
);
1517 rc
= GPG_ERR_NO_DATA
;
1525 for (i
= 0; i
< total
; i
++)
1527 char *tmp
= slist_nth_data (elements
->list
, i
);
1529 string_append_printf (result
, "%s%s", tmp
,
1530 i
+ 1 == total
? "" : "\n");
1535 rc
= GPG_ERR_NO_DATA
;
1542 has_list_flag (char *path
, char *flags
)
1546 while (*p
&& *++p
!= ' ');
1555 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1566 do_list (assuan_context_t ctx
, char *line
)
1568 struct client_s
*client
= assuan_get_pointer (ctx
);
1570 struct element_list_s
*elements
= NULL
;
1572 elements
= xcalloc (1, sizeof (struct element_list_s
));
1574 return GPG_ERR_ENOMEM
;
1576 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1578 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1579 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1581 if (!line
|| !*line
)
1583 struct string_s
*str
= NULL
;
1585 pthread_cleanup_push (list_command_cleanup2
, elements
);
1586 rc
= list_root_elements (client
->doc
, &str
, elements
->verbose
,
1587 elements
->with_target
);
1588 pthread_cleanup_pop (1);
1589 pthread_cleanup_push (list_command_cleanup1
, str
);
1593 if (client
->opts
& OPT_LIST_ALL
)
1595 char **roots
= str_split (str
->str
, "\n", 0);
1598 pthread_cleanup_push (req_cleanup
, roots
);
1599 string_truncate (str
, 0);
1601 for (p
= roots
; *p
; p
++)
1603 if (strchr (*p
, ' '))
1605 if (has_list_flag (*p
, "EO"))
1607 string_append_printf (str
, "%s%s", *p
,
1608 *(p
+ 1) ? "\n" : "");
1613 elements
= xcalloc (1, sizeof (struct element_list_s
));
1616 rc
= GPG_ERR_ENOMEM
;
1620 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1622 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1623 OPT_LIST_WITH_TARGET
);
1624 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1625 pthread_cleanup_push (list_command_cleanup2
, elements
);
1626 rc
= list_path_once (client
, *p
, elements
, str
);
1627 pthread_cleanup_pop (1);
1632 string_append (str
, "\n");
1635 pthread_cleanup_pop (1);
1639 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1642 pthread_cleanup_pop (1);
1646 pthread_cleanup_push (list_command_cleanup2
, elements
);
1647 struct string_s
*str
= string_new (NULL
);
1648 pthread_cleanup_push (list_command_cleanup1
, str
);
1649 rc
= list_path_once (client
, line
, elements
, str
);
1651 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1653 pthread_cleanup_pop (1);
1654 pthread_cleanup_pop (1);
1659 list_command (assuan_context_t ctx
, char *line
)
1661 struct client_s
*client
= assuan_get_pointer (ctx
);
1663 struct argv_s
*args
[] = {
1664 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1665 parse_list_opt_norecurse
},
1666 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1667 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1668 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1669 parse_list_opt_target
},
1670 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1674 if (disable_list_and_dump
== 1)
1675 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1677 client
->opts
|= OPT_LIST_RECURSE
;
1678 rc
= parse_options (&line
, args
, client
);
1680 return send_error (ctx
, rc
);
1682 if (client
->opts
& OPT_LIST_ALL
)
1683 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1685 if (client
->opts
& OPT_INQUIRE
)
1687 unsigned char *result
;
1690 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1692 return send_error (ctx
, rc
);
1694 line
= (char *) result
;
1697 rc
= do_list (ctx
, line
);
1699 if (client
->opts
& OPT_INQUIRE
)
1702 return send_error (ctx
, rc
);
1706 * req[0] - element path
1709 attribute_list (assuan_context_t ctx
, char **req
)
1711 struct client_s
*client
= assuan_get_pointer (ctx
);
1712 char **attrlist
= NULL
;
1720 if (!req
|| !req
[0])
1721 return GPG_ERR_SYNTAX
;
1723 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1726 * The first argument may be only a root element.
1728 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1729 return GPG_ERR_SYNTAX
;
1732 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1742 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1743 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1754 for (a
= n
->properties
; a
; a
= a
->next
)
1758 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1761 strv_free (attrlist
);
1763 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1764 pwmd_strerror (GPG_ERR_ENOMEM
));
1765 return GPG_ERR_ENOMEM
;
1770 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1772 && an
->content
? (char *) an
->content
: "");
1776 strv_free (attrlist
);
1777 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1778 pwmd_strerror (GPG_ERR_ENOMEM
));
1779 return GPG_ERR_ENOMEM
;
1782 attrlist
[++i
] = NULL
;
1786 return GPG_ERR_NO_DATA
;
1788 line
= strv_join ("\n", attrlist
);
1792 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1793 pwmd_strerror (GPG_ERR_ENOMEM
));
1794 strv_free (attrlist
);
1795 return GPG_ERR_ENOMEM
;
1798 pthread_cleanup_push (xfree
, line
);
1799 pthread_cleanup_push (req_cleanup
, attrlist
);
1800 rc
= xfer_data (ctx
, line
, strlen (line
));
1801 pthread_cleanup_pop (1);
1802 pthread_cleanup_pop (1);
1807 * req[0] - attribute
1808 * req[1] - element path
1811 attribute_delete (struct client_s
*client
, char **req
)
1817 if (!req
|| !req
[0] || !req
[1])
1818 return GPG_ERR_SYNTAX
;
1820 if (!strcmp (req
[0], "_name"))
1821 return GPG_ERR_INV_ATTR
;
1823 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1826 * The first argument may be only a root element.
1828 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1829 return GPG_ERR_SYNTAX
;
1832 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
1838 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
1839 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1844 rc
= delete_attribute (n
, (xmlChar
*) req
[0]);
1852 create_element_path (struct client_s
*client
,
1853 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1855 char **req
= *elements
;
1856 char **req_orig
= strv_dup (req
);
1857 xmlNodePtr n
= NULL
;
1863 *rc
= GPG_ERR_ENOMEM
;
1864 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1865 pwmd_strerror (GPG_ERR_ENOMEM
));
1870 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1873 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1876 *rc
= new_root_element (client
->doc
, req
[0]);
1882 else if (n
== parent
)
1884 *rc
= GPG_ERR_CONFLICT
;
1891 n
= create_target_elements_cb (n
, req
+ 1, rc
, NULL
);
1893 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
, NULL
, NULL
,
1894 create_target_elements_cb
, 0, 0, parent
, 0);
1900 * Reset the position of the element tree now that the elements
1901 * have been created.
1906 n
= find_root_element (client
->doc
, &req
, rc
, NULL
, 0, 0);
1910 n
= find_elements (client
->doc
, n
->children
, req
+ 1, rc
,
1911 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1918 strv_free (req_orig
);
1925 * Creates a "target" attribute. When other commands encounter an element with
1926 * this attribute, the element path is modified to the target value. If the
1927 * source element path doesn't exist when using 'ATTR SET target', it is
1928 * created, but the destination element path must exist.
1930 * req[0] - source element path
1931 * req[1] - destination element path
1934 target_attribute (struct client_s
*client
, char **req
)
1936 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1940 if (!req
|| !req
[0] || !req
[1])
1941 return GPG_ERR_SYNTAX
;
1943 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
1946 * The first argument may be only a root element.
1948 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
1949 return GPG_ERR_SYNTAX
;
1952 if (!valid_element_path (src
, 0))
1953 return GPG_ERR_INV_VALUE
;
1955 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
1958 * The first argument may be only a root element.
1960 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
1962 rc
= GPG_ERR_SYNTAX
;
1967 odst
= strv_dup (dst
);
1970 rc
= GPG_ERR_ENOMEM
;
1974 n
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1976 * Make sure the destination element path exists.
1983 n
= find_elements (client
->doc
, n
->children
, dst
+ 1, &rc
,
1984 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1989 rc
= validate_target_attribute (client
->doc
, req
[0], n
);
1990 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1993 n
= create_element_path (client
, &src
, &rc
, NULL
);
1997 line
= strv_join ("\t", odst
);
2000 rc
= GPG_ERR_ENOMEM
;
2004 rc
= add_attribute (n
, "target", line
);
2015 * req[0] - attribute
2016 * req[1] - element path
2019 attribute_get (assuan_context_t ctx
, char **req
)
2021 struct client_s
*client
= assuan_get_pointer (ctx
);
2027 if (!req
|| !req
[0] || !req
[1])
2028 return GPG_ERR_SYNTAX
;
2030 if (strchr (req
[1], '\t'))
2032 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2033 return GPG_ERR_SYNTAX
;
2037 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2038 return GPG_ERR_SYNTAX
;
2041 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2048 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
2049 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2057 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2058 return GPG_ERR_NOT_FOUND
;
2060 pthread_cleanup_push (xmlFree
, a
);
2063 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2065 rc
= GPG_ERR_NO_DATA
;
2067 pthread_cleanup_pop (1);
2076 * req[0] - attribute
2077 * req[1] - element path
2081 attribute_set (struct client_s
*client
, char **req
)
2087 if (!req
|| !req
[0] || !req
[1])
2088 return GPG_ERR_SYNTAX
;
2091 * Reserved attribute names.
2093 if (!strcmp (req
[0], "_name"))
2094 return GPG_ERR_INV_ATTR
;
2095 else if (!strcmp (req
[0], "target"))
2096 return target_attribute (client
, req
+ 1);
2097 else if (!valid_xml_attribute (req
[0]))
2098 return GPG_ERR_INV_VALUE
;
2100 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2103 * The first argument may be only a root element.
2105 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2106 return GPG_ERR_SYNTAX
;
2109 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2116 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
2117 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2123 rc
= add_attribute (n
, req
[0], req
[2]);
2132 * req[1] - attribute name or element path if command is LIST
2133 * req[2] - element path
2134 * req[2] - element path or value
2138 do_attr (assuan_context_t ctx
, char *line
)
2140 struct client_s
*client
= assuan_get_pointer (ctx
);
2144 req
= str_split (line
, " ", 4);
2145 if (!req
|| !req
[0] || !req
[1])
2148 return GPG_ERR_SYNTAX
;
2151 pthread_cleanup_push (req_cleanup
, req
);
2153 if (strcasecmp (req
[0], "SET") == 0)
2154 rc
= attribute_set (client
, req
+ 1);
2155 else if (strcasecmp (req
[0], "GET") == 0)
2156 rc
= attribute_get (ctx
, req
+ 1);
2157 else if (strcasecmp (req
[0], "DELETE") == 0)
2158 rc
= attribute_delete (client
, req
+ 1);
2159 else if (strcasecmp (req
[0], "LIST") == 0)
2160 rc
= attribute_list (ctx
, req
+ 1);
2162 rc
= GPG_ERR_SYNTAX
;
2164 pthread_cleanup_pop (1);
2169 attr_command (assuan_context_t ctx
, char *line
)
2171 struct client_s
*client
= assuan_get_pointer (ctx
);
2173 struct argv_s
*args
[] = {
2174 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2178 rc
= parse_options (&line
, args
, client
);
2180 return send_error (ctx
, rc
);
2182 if (client
->opts
& OPT_INQUIRE
)
2184 unsigned char *result
;
2187 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2189 return send_error (ctx
, rc
);
2191 line
= (char *) result
;
2194 rc
= do_attr (ctx
, line
);
2196 if (client
->opts
& OPT_INQUIRE
)
2199 return send_error (ctx
, rc
);
2203 parse_iscached_opt_lock (void *data
, void *value
)
2205 struct client_s
*client
= data
;
2208 client
->opts
|= OPT_LOCK
;
2213 iscached_command (assuan_context_t ctx
, char *line
)
2215 struct client_s
*client
= assuan_get_pointer (ctx
);
2217 struct argv_s
*args
[] = {
2218 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2222 if (!line
|| !*line
)
2223 return send_error (ctx
, GPG_ERR_SYNTAX
);
2225 rc
= parse_options (&line
, args
, client
);
2227 return send_error (ctx
, rc
);
2228 else if (!valid_filename (line
))
2229 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2231 rc
= cache_iscached (line
, NULL
);
2232 if (client
->opts
& OPT_LOCK
2233 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2235 unsigned char md5file
[16];
2236 gpg_error_t trc
= rc
;
2238 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2239 if (memcmp (md5file
, client
->md5file
, 16))
2240 cleanup_client (client
);
2242 memcpy (client
->md5file
, md5file
, 16);
2243 rc
= do_lock (client
, 1);
2248 return send_error (ctx
, rc
);
2252 clearcache_command (assuan_context_t ctx
, char *line
)
2254 gpg_error_t rc
= 0, all_rc
= 0;
2255 unsigned char md5file
[16];
2259 struct client_thread_s
*once
= NULL
;
2262 MUTEX_LOCK (&cn_mutex
);
2263 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2265 if (!line
|| !*line
)
2268 t
= slist_length (cn_thread_list
);
2270 for (i
= 0; i
< t
; i
++)
2272 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2273 assuan_peercred_t peer
;
2278 /* Lock each connected clients' file mutex to prevent any other client
2279 * from accessing the cache entry (the file mutex is locked upon
2280 * command startup). The cache for the entry is not cleared if the
2281 * file mutex is locked by another client to prevent this function
2286 if (thd
->cl
->filename
)
2288 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2289 all_rc
= !all_rc
? rc
: all_rc
;
2297 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2298 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2300 if (pthread_equal (pthread_self (), thd
->tid
))
2304 if (!thd
->cl
->filename
||
2305 cache_iscached (thd
->cl
->filename
,
2306 NULL
) == GPG_ERR_NO_DATA
)
2312 cache_defer_clear (thd
->cl
->md5file
);
2315 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2323 rc
= cache_clear (thd
->cl
->md5file
);
2324 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2332 /* A single data filename was specified. Lock only this data file
2333 * mutex and free the cache entry. */
2336 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2337 rc
= do_validate_peer (ctx
, line
, &peer
);
2339 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2341 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2343 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2345 if (pthread_equal (pthread_self (), thd
->tid
))
2352 rc
= cache_clear (thd
->cl
->md5file
);
2353 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2357 cache_defer_clear (thd
->cl
->md5file
);
2365 /* Only connected clients' cache entries have been cleared. Now clear any
2366 * remaining cache entries without clients but only if there wasn't an
2367 * error from above since this would defeat the locking check of the
2368 * remaining entries. */
2374 /* No clients are using the specified file. */
2375 else if (!all_rc
&& !rc
&& !once
)
2377 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2378 rc
= cache_clear (md5file
);
2381 /* Release the connection mutex. */
2382 pthread_cleanup_pop (1);
2386 send_status_all (STATUS_CACHE
, NULL
);
2388 /* One or more files were locked while clearing all cache entries. */
2392 return send_error (ctx
, rc
);
2396 cachetimeout_command (assuan_context_t ctx
, char *line
)
2399 char **req
= str_split (line
, " ", 0);
2402 assuan_peercred_t peer
;
2404 if (!req
|| !*req
|| !req
[1])
2407 return send_error (ctx
, GPG_ERR_SYNTAX
);
2411 timeout
= (int) strtol (req
[1], &p
, 10);
2412 if (errno
!= 0 || *p
|| timeout
< -1)
2415 return send_error (ctx
, GPG_ERR_SYNTAX
);
2418 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2421 unsigned char md5file
[16];
2423 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2424 rc
= cache_set_timeout (md5file
, timeout
);
2425 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2428 MUTEX_LOCK (&rcfile_mutex
);
2429 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2431 MUTEX_UNLOCK (&rcfile_mutex
);
2436 return send_error (ctx
, rc
);
2440 dump_command (assuan_context_t ctx
, char *line
)
2444 struct client_s
*client
= assuan_get_pointer (ctx
);
2447 if (disable_list_and_dump
== 1)
2448 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2450 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2454 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2455 pwmd_strerror (GPG_ERR_ENOMEM
));
2456 return send_error (ctx
, GPG_ERR_ENOMEM
);
2459 pthread_cleanup_push (xmlFree
, xml
);
2460 rc
= xfer_data (ctx
, (char *) xml
, len
);
2461 pthread_cleanup_pop (1);
2462 return send_error (ctx
, rc
);
2466 getconfig_command (assuan_context_t ctx
, char *line
)
2468 struct client_s
*client
= assuan_get_pointer (ctx
);
2470 char filename
[255] = { 0 }, param
[747] = { 0 };
2471 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2473 if (!line
|| !*line
)
2474 return send_error (ctx
, GPG_ERR_SYNTAX
);
2476 if (strchr (line
, ' '))
2478 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2483 if (fp
&& !valid_filename (fp
))
2484 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2486 paramp
= str_down (paramp
);
2487 if (!strcmp (paramp
, "cipher") && fp
)
2489 struct crypto_s
*crypto
= NULL
;
2491 rc
= init_client_crypto (&crypto
);
2494 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2498 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2508 UPDATE_AGENT_CTX (client
, crypto
);
2509 cleanup_crypto (&crypto
);
2510 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2511 return send_error (ctx
, rc
);
2516 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2518 struct crypto_s
*crypto
= NULL
;
2520 rc
= init_client_crypto (&crypto
);
2523 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2526 tmp
= str_asprintf ("%llu",
2527 (unsigned long long) crypto
->hdr
.
2530 rc
= GPG_ERR_ENOMEM
;
2534 UPDATE_AGENT_CTX (client
, crypto
);
2535 cleanup_crypto (&crypto
);
2536 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2537 return send_error (ctx
, rc
);
2542 else if (!strcmp (paramp
, "passphrase"))
2543 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2545 p
= config_get_value (fp
? fp
: "global", paramp
);
2547 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2549 tmp
= expand_homedir (p
);
2553 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2554 pwmd_strerror (GPG_ERR_ENOMEM
));
2555 return send_error (ctx
, GPG_ERR_ENOMEM
);
2560 pthread_cleanup_push (xfree
, p
);
2561 rc
= xfer_data (ctx
, p
, strlen (p
));
2562 pthread_cleanup_pop (1);
2563 return send_error (ctx
, rc
);
2568 xmlXPathContextPtr xp
;
2569 xmlXPathObjectPtr result
;
2575 xpath_command_cleanup (void *arg
)
2577 struct xpath_s
*xpath
= arg
;
2582 req_cleanup (xpath
->req
);
2585 xmlBufferFree (xpath
->buf
);
2588 xmlXPathFreeObject (xpath
->result
);
2591 xmlXPathFreeContext (xpath
->xp
);
2595 do_xpath (assuan_context_t ctx
, char *line
)
2598 struct client_s
*client
= assuan_get_pointer (ctx
);
2599 struct xpath_s _x
= { 0 };
2600 struct xpath_s
*xpath
= &_x
;
2602 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2604 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2605 return GPG_ERR_ENOMEM
;
2608 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2611 rc
= GPG_ERR_BAD_DATA
;
2616 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2619 rc
= GPG_ERR_BAD_DATA
;
2623 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2625 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2629 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2630 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2634 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2636 rc
= GPG_ERR_NO_DATA
;
2639 else if (xpath
->req
[1])
2645 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2646 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2647 xmlBufferLength (xpath
->buf
));
2648 pthread_cleanup_pop (0);
2650 xpath_command_cleanup (xpath
);
2655 xpath_command (assuan_context_t ctx
, char *line
)
2657 struct client_s
*client
= assuan_get_pointer (ctx
);
2659 struct argv_s
*args
[] = {
2660 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2664 if (disable_list_and_dump
== 1)
2665 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2667 rc
= parse_options (&line
, args
, client
);
2669 return send_error (ctx
, rc
);
2671 if (client
->opts
& OPT_INQUIRE
)
2673 unsigned char *result
;
2676 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2678 return send_error (ctx
, rc
);
2680 line
= (char *) result
;
2683 if (!line
|| !*line
)
2684 rc
= GPG_ERR_SYNTAX
;
2687 rc
= do_xpath (ctx
, line
);
2689 if (client
->opts
& OPT_INQUIRE
)
2692 return send_error (ctx
, rc
);
2696 do_xpathattr (assuan_context_t ctx
, char *line
)
2698 struct client_s
*client
= assuan_get_pointer (ctx
);
2702 struct xpath_s _x
= { 0 };
2703 struct xpath_s
*xpath
= &_x
;
2705 if ((req
= str_split (line
, " ", 3)) == NULL
)
2706 return GPG_ERR_ENOMEM
;
2710 rc
= GPG_ERR_SYNTAX
;
2714 if (!strcasecmp (req
[0], "SET"))
2716 else if (!strcasecmp (req
[0], "DELETE"))
2720 rc
= GPG_ERR_SYNTAX
;
2724 if (!req
[1] || !req
[2])
2726 rc
= GPG_ERR_SYNTAX
;
2730 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2732 rc
= GPG_ERR_ENOMEM
;
2736 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2738 rc
= GPG_ERR_SYNTAX
;
2742 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2745 rc
= GPG_ERR_BAD_DATA
;
2749 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2752 rc
= GPG_ERR_BAD_DATA
;
2756 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2758 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2762 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2763 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2764 (xmlChar
*) req
[1]);
2767 xpath_command_cleanup (xpath
);
2772 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2774 xpathattr_command (assuan_context_t ctx
, char *line
)
2776 struct client_s
*client
= assuan_get_pointer (ctx
);
2778 struct argv_s
*args
[] = {
2779 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2783 if (disable_list_and_dump
== 1)
2784 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2786 rc
= parse_options (&line
, args
, client
);
2788 return send_error (ctx
, rc
);
2790 if (client
->opts
& OPT_INQUIRE
)
2792 unsigned char *result
;
2795 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2797 return send_error (ctx
, rc
);
2799 line
= (char *) result
;
2802 if (!line
|| !*line
)
2803 rc
= GPG_ERR_SYNTAX
;
2806 rc
= do_xpathattr (ctx
, line
);
2808 if (client
->opts
& OPT_INQUIRE
)
2811 return send_error (ctx
, rc
);
2815 do_import (struct client_s
*client
, const char *root_element
,
2816 unsigned char *content
)
2818 char **dst_path
= NULL
;
2819 xmlDocPtr doc
= NULL
;
2820 xmlNodePtr n
, root
, copy
;
2823 if (!content
|| !*content
)
2826 return GPG_ERR_SYNTAX
;
2830 dst_path
= str_split (root_element
, "\t", 0);
2832 if (dst_path
&& !valid_element_path (dst_path
, 0))
2835 strv_free (dst_path
);
2837 return GPG_ERR_INV_VALUE
;
2840 struct string_s
*str
= string_new_content ((char *)content
);
2841 str
= string_prepend (str
, "<pwmd>");
2842 str
= string_append (str
, "</pwmd>");
2843 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2844 string_free (str
, 1);
2847 rc
= GPG_ERR_BAD_DATA
;
2851 root
= xmlDocGetRootElement (doc
);
2852 xmlNodePtr root_orig
= root
->children
;
2853 root
= root
->children
;
2854 rc
= validate_import (root
);
2863 char **path
= strv_dup (dst_path
);
2866 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2867 pwmd_strerror (GPG_ERR_ENOMEM
));
2868 rc
= GPG_ERR_ENOMEM
;
2872 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2876 rc
= GPG_ERR_ENOMEM
;
2880 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2883 rc
= GPG_ERR_ENOMEM
;
2888 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 0);
2889 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2897 n
= find_elements (client
->doc
, n
->children
, path
+ 1, &rc
,
2898 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
2899 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2914 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2916 n
= create_element_path (client
, &path
, &rc
, NULL
);
2923 copy
= xmlCopyNodeList (root
->children
);
2924 n
= xmlAddChildList (n
, copy
);
2926 rc
= GPG_ERR_ENOMEM
;
2935 /* Check if the content root element can create a DTD root element. */
2936 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2938 rc
= GPG_ERR_SYNTAX
;
2944 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2946 rc
= GPG_ERR_SYNTAX
;
2950 char *tmp
= str_dup ((char *) a
);
2952 int literal
= is_literal_element (&tmp
);
2954 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2957 rc
= GPG_ERR_INV_VALUE
;
2961 if (strv_printf (&path
, "%s", tmp
) == 0)
2964 rc
= GPG_ERR_ENOMEM
;
2969 n
= find_root_element (client
->doc
, &path
, &rc
, NULL
, 0, 1);
2970 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2972 rc
= GPG_ERR_BAD_DATA
;
2976 /* Overwriting the existing tree. */
2980 xmlFreeNodeList (n
);
2984 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2985 n
= xmlCopyNode (root
, 1);
2986 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
2991 rc
= update_element_mtime (n
->parent
);
2993 for (root
= root_orig
->next
; root
; root
= root
->next
)
2995 if (root
->type
== XML_ELEMENT_NODE
)
3008 strv_free (dst_path
);
3014 parse_import_opt_root (void *data
, void *value
)
3016 struct client_s
*client
= data
;
3018 client
->import_root
= str_dup (value
);
3019 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3023 import_command (assuan_context_t ctx
, char *line
)
3026 struct client_s
*client
= assuan_get_pointer (ctx
);
3027 unsigned char *result
;
3029 struct argv_s
*args
[] = {
3030 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3034 xfree (client
->import_root
);
3035 client
->import_root
= NULL
;
3036 rc
= parse_options (&line
, args
, client
);
3038 return send_error (ctx
, rc
);
3040 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3042 return send_error (ctx
, rc
);
3044 rc
= do_import (client
, client
->import_root
, result
);
3045 xfree (client
->import_root
);
3046 client
->import_root
= NULL
;
3047 return send_error (ctx
, rc
);
3051 do_lock (struct client_s
*client
, int add
)
3053 gpg_error_t rc
= lock_file_mutex (client
, add
);
3056 client
->flags
|= FLAG_LOCK_CMD
;
3062 lock_command (assuan_context_t ctx
, char *line
)
3064 struct client_s
*client
= assuan_get_pointer (ctx
);
3065 gpg_error_t rc
= do_lock (client
, 0);
3067 return send_error (ctx
, rc
);
3071 unlock_command (assuan_context_t ctx
, char *line
)
3073 struct client_s
*client
= assuan_get_pointer (ctx
);
3076 rc
= unlock_file_mutex (client
, 0);
3077 return send_error (ctx
, rc
);
3081 option_command (assuan_context_t ctx
, char *line
)
3083 struct client_s
*client
= assuan_get_pointer (ctx
);
3085 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3087 struct agent_s
*agent
= client
->crypto
->agent
;
3089 char namebuf
[255] = { 0 };
3090 char *name
= namebuf
;
3091 char *value
= NULL
, *p
;
3093 p
= strchr (line
, '=');
3095 strncpy (namebuf
, line
, sizeof(namebuf
));
3098 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3102 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3104 if (strcasecmp (name
, (char *) "log_level") == 0)
3110 l
= strtol (value
, NULL
, 10);
3113 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3116 MUTEX_LOCK (&rcfile_mutex
);
3117 config_set_int_param (&global_config
, "global", "log_level", value
);
3118 MUTEX_UNLOCK (&rcfile_mutex
);
3121 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3128 n
= strtol (value
, &p
, 10);
3130 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3133 client
->lock_timeout
= n
;
3136 else if (strcasecmp (name
, (char *) "NAME") == 0)
3138 char *tmp
= pthread_getspecific (thread_name_key
);
3143 if (!value
|| !*value
)
3145 pthread_setspecific (thread_name_key
, str_dup (""));
3149 pthread_setspecific (thread_name_key
, str_dup (value
));
3152 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3154 xfree (pin_opts
->lc_messages
);
3155 pin_opts
->lc_messages
= NULL
;
3156 if (value
&& *value
)
3157 pin_opts
->lc_messages
= str_dup (value
);
3160 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3163 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3165 xfree (pin_opts
->lc_ctype
);
3166 pin_opts
->lc_ctype
= NULL
;
3167 if (value
&& *value
)
3168 pin_opts
->lc_ctype
= str_dup (value
);
3171 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3174 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3176 xfree (pin_opts
->ttyname
);
3177 pin_opts
->ttyname
= NULL
;
3178 if (value
&& *value
)
3179 pin_opts
->ttyname
= str_dup (value
);
3182 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3185 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3187 xfree (pin_opts
->ttytype
);
3188 pin_opts
->ttytype
= NULL
;
3189 if (value
&& *value
)
3190 pin_opts
->ttytype
= str_dup (value
);
3193 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3196 else if (strcasecmp (name
, (char *) "display") == 0)
3198 xfree (pin_opts
->display
);
3199 pin_opts
->display
= NULL
;
3200 if (value
&& *value
)
3201 pin_opts
->display
= str_dup (value
);
3204 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3207 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3209 xfree (pin_opts
->desc
);
3210 pin_opts
->desc
= NULL
;
3211 if (value
&& *value
)
3212 pin_opts
->desc
= str_dup (value
);
3214 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3216 xfree (pin_opts
->title
);
3217 pin_opts
->title
= NULL
;
3218 if (value
&& *value
)
3219 pin_opts
->title
= str_dup (value
);
3221 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3223 xfree (pin_opts
->prompt
);
3224 pin_opts
->prompt
= NULL
;
3225 if (value
&& *value
)
3226 pin_opts
->prompt
= str_dup (value
);
3229 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3237 n
= (int) strtol (value
, &p
, 10);
3240 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3242 pin_opts
->timeout
= n
;
3243 MUTEX_LOCK (&rcfile_mutex
);
3244 config_set_int_param (&global_config
,
3245 client
->filename
? client
->filename
: "global",
3246 "pinentry_timeout", value
);
3247 MUTEX_UNLOCK (&rcfile_mutex
);
3250 else if (strcasecmp (name
, "disable-pinentry") == 0)
3255 if (value
&& *value
)
3257 n
= (int) strtol (value
, &p
, 10);
3258 if (*p
|| n
< 0 || n
> 1)
3259 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3263 client
->flags
|= FLAG_NO_PINENTRY
;
3265 client
->flags
&= ~FLAG_NO_PINENTRY
;
3270 if (client
->flags
& FLAG_NO_PINENTRY
)
3271 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3274 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3278 return send_error (ctx
, rc
);
3283 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3287 if (!rc
&& use_agent
&& agent
)
3289 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3294 return send_error (ctx
, rc
);
3298 do_rename (assuan_context_t ctx
, char *line
)
3300 struct client_s
*client
= assuan_get_pointer (ctx
);
3302 char **req
, **src
, *dst
;
3305 req
= str_split (line
, " ", 0);
3307 if (!req
|| !req
[0] || !req
[1])
3310 return GPG_ERR_SYNTAX
;
3314 is_literal_element (&dst
);
3316 if (!valid_xml_element ((xmlChar
*) dst
))
3319 return GPG_ERR_INV_VALUE
;
3322 if (strchr (req
[0], '\t'))
3323 src
= str_split (req
[0], "\t", 0);
3325 src
= str_split (req
[0], " ", 0);
3329 rc
= GPG_ERR_SYNTAX
;
3333 n
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3335 n
= find_elements (client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3336 NULL
, 0, 0, NULL
, 0);
3341 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3344 rc
= GPG_ERR_ENOMEM
;
3348 /* To prevent unwanted effects:
3350 * <root name="a"><b/></root>
3354 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3357 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3367 for (p
= src
; *p
; p
++)
3371 strv_printf (&tmp
, "%s", *p
);
3375 strv_printf (&tmp
, "!%s", dst
);
3376 ndst
= find_root_element (client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3377 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3384 ndst
= find_elements (client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3385 NULL
, NULL
, 0, 0, NULL
, 0);
3388 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3393 /* Target may exist:
3396 * <root name="b" target="a"/>
3405 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3412 xmlFreeNodeList (ndst
);
3415 rc
= add_attribute (n
, "_name", dst
);
3424 rename_command (assuan_context_t ctx
, char *line
)
3426 struct client_s
*client
= assuan_get_pointer (ctx
);
3428 struct argv_s
*args
[] = {
3429 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3433 rc
= parse_options (&line
, args
, client
);
3435 return send_error (ctx
, rc
);
3437 if (client
->opts
& OPT_INQUIRE
)
3439 unsigned char *result
;
3442 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3444 return send_error (ctx
, rc
);
3446 line
= (char *) result
;
3449 rc
= do_rename (ctx
, line
);
3451 if (client
->opts
& OPT_INQUIRE
)
3454 return send_error (ctx
, rc
);
3458 do_copy (assuan_context_t ctx
, char *line
)
3460 struct client_s
*client
= assuan_get_pointer (ctx
);
3462 char **req
, **src
= NULL
, **dst
= NULL
;
3463 xmlNodePtr nsrc
, ndst
, new = NULL
;
3465 req
= str_split (line
, " ", 0);
3466 if (!req
|| !req
[0] || !req
[1])
3469 return GPG_ERR_SYNTAX
;
3472 if (strchr (req
[0], '\t'))
3473 src
= str_split (req
[0], "\t", 0);
3475 src
= str_split (req
[0], " ", 0);
3479 rc
= GPG_ERR_SYNTAX
;
3483 if (strchr (req
[1], '\t'))
3484 dst
= str_split (req
[1], "\t", 0);
3486 dst
= str_split (req
[1], " ", 0);
3490 rc
= GPG_ERR_SYNTAX
;
3494 if (!valid_element_path (dst
, 0))
3496 rc
= GPG_ERR_INV_VALUE
;
3500 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3502 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3503 NULL
, NULL
, 0, 0, NULL
, 0);
3508 new = xmlCopyNodeList (nsrc
);
3511 rc
= GPG_ERR_ENOMEM
;
3516 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3520 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3521 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3528 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3530 else if (!ndst
|| create
)
3532 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3537 /* Merge any attributes from the src node to the initial dst node. */
3538 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3540 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3543 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3547 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3548 xmlNewProp (ndst
, attr
->name
, tmp
);
3550 rc
= add_attribute (ndst
, NULL
, NULL
);
3553 xmlNodePtr n
= ndst
->children
;
3555 xmlFreeNodeList (n
);
3556 ndst
->children
= NULL
;
3560 n
= xmlCopyNodeList (new->children
);
3563 rc
= GPG_ERR_ENOMEM
;
3567 n
= xmlAddChildList (ndst
, n
);
3570 rc
= GPG_ERR_ENOMEM
;
3574 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
) ==
3575 ndst
->parent
? ndst
: ndst
->parent
);
3581 xmlUnlinkNode (new);
3582 xmlFreeNodeList (new);
3598 copy_command (assuan_context_t ctx
, char *line
)
3600 struct client_s
*client
= assuan_get_pointer (ctx
);
3602 struct argv_s
*args
[] = {
3603 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3607 rc
= parse_options (&line
, args
, client
);
3609 return send_error (ctx
, rc
);
3611 if (client
->opts
& OPT_INQUIRE
)
3613 unsigned char *result
;
3616 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3618 return send_error (ctx
, rc
);
3620 line
= (char *) result
;
3623 rc
= do_copy (ctx
, line
);
3625 if (client
->opts
& OPT_INQUIRE
)
3628 return send_error (ctx
, rc
);
3632 do_move (assuan_context_t ctx
, char *line
)
3634 struct client_s
*client
= assuan_get_pointer (ctx
);
3636 char **req
, **src
= NULL
, **dst
= NULL
;
3637 xmlNodePtr nsrc
, ndst
= NULL
;
3639 req
= str_split (line
, " ", 0);
3641 if (!req
|| !req
[0] || !req
[1])
3644 return GPG_ERR_SYNTAX
;
3647 if (strchr (req
[0], '\t'))
3648 src
= str_split (req
[0], "\t", 0);
3650 src
= str_split (req
[0], " ", 0);
3654 rc
= GPG_ERR_SYNTAX
;
3658 if (strchr (req
[1], '\t'))
3659 dst
= str_split (req
[1], "\t", 0);
3661 dst
= str_split (req
[1], " ", 0);
3663 nsrc
= find_root_element (client
->doc
, &src
, &rc
, NULL
, 0, 0);
3665 nsrc
= find_elements (client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3666 NULL
, NULL
, 0, 0, NULL
, 0);
3673 if (!valid_element_path (dst
, 0))
3675 rc
= GPG_ERR_INV_VALUE
;
3679 ndst
= find_root_element (client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3681 ndst
= find_elements (client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3682 NULL
, NULL
, 0, 0, NULL
, 0);
3685 ndst
= xmlDocGetRootElement (client
->doc
);
3687 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3691 rc
= GPG_ERR_CONFLICT
;
3696 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3703 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3704 xmlNodePtr dup
= find_element (ndst
->children
, (char *) a
, NULL
);
3712 if (ndst
== xmlDocGetRootElement (client
->doc
))
3714 xmlNodePtr n
= nsrc
;
3717 while (n
->parent
&& n
->parent
!= ndst
)
3720 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3721 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3723 if (xmlStrEqual (a
, b
))
3726 xmlUnlinkNode (nsrc
);
3728 xmlFreeNodeList (n
);
3736 xmlUnlinkNode (dup
);
3737 xmlFreeNodeList (dup
);
3741 xmlUnlinkNode (dup
);
3747 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3749 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3750 && !strcmp ((char *) name
, *dst
))
3753 rc
= GPG_ERR_CONFLICT
;
3758 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3764 update_element_mtime (nsrc
->parent
);
3765 xmlUnlinkNode (nsrc
);
3766 ndst
= xmlAddChildList (ndst
, nsrc
);
3769 rc
= GPG_ERR_ENOMEM
;
3771 update_element_mtime (ndst
->parent
);
3787 move_command (assuan_context_t ctx
, char *line
)
3789 struct client_s
*client
= assuan_get_pointer (ctx
);
3791 struct argv_s
*args
[] = {
3792 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3796 rc
= parse_options (&line
, args
, client
);
3798 return send_error (ctx
, rc
);
3800 if (client
->opts
& OPT_INQUIRE
)
3802 unsigned char *result
;
3805 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3807 return send_error (ctx
, rc
);
3809 line
= (char *) result
;
3812 rc
= do_move (ctx
, line
);
3814 if (client
->opts
& OPT_INQUIRE
)
3817 return send_error (ctx
, rc
);
3821 ls_command (assuan_context_t ctx
, char *line
)
3824 char *tmp
= str_asprintf ("%s/data", homedir
);
3825 char *dir
= expand_homedir (tmp
);
3826 DIR *d
= opendir (dir
);
3828 rc
= gpg_error_from_errno (errno
);
3834 return send_error (ctx
, rc
);
3838 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3839 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3845 while (!readdir_r (d
, p
, &cur
) && cur
)
3847 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3849 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3850 && cur
->d_name
[2] == '\0')
3853 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3860 rc
= GPG_ERR_ENOMEM
;
3872 return send_error (ctx
, rc
);
3875 return send_error (ctx
, GPG_ERR_NO_DATA
);
3877 list
[strlen (list
) - 1] = 0;
3878 rc
= xfer_data (ctx
, list
, strlen (list
));
3880 return send_error (ctx
, rc
);
3884 bye_notify (assuan_context_t ctx
, char *line
)
3886 struct client_s
*cl
= assuan_get_pointer (ctx
);
3889 if (cl
->thd
->remote
)
3895 struct timeval tv
= { 0, 50000 };
3897 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3898 if (rc
== GNUTLS_E_AGAIN
)
3899 select (0, NULL
, NULL
, NULL
, &tv
);
3901 while (rc
== GNUTLS_E_AGAIN
);
3905 /* This will let assuan_process_next() return. */
3906 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3907 cl
->last_rc
= 0; // BYE command result
3912 reset_notify (assuan_context_t ctx
, char *line
)
3914 struct client_s
*client
= assuan_get_pointer (ctx
);
3917 cleanup_client (client
);
3923 * This is called before every Assuan command.
3926 command_startup (assuan_context_t ctx
, const char *name
)
3928 struct client_s
*client
= assuan_get_pointer (ctx
);
3930 struct command_table_s
*cmd
= NULL
;
3932 log_write1 ("command='%s'", name
);
3933 client
->last_rc
= client
->opts
= 0;
3935 for (int i
= 0; command_table
[i
]; i
++)
3937 if (!strcasecmp (name
, command_table
[i
]->name
))
3939 if (command_table
[i
]->ignore_startup
)
3941 cmd
= command_table
[i
];
3946 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3951 * This is called after every Assuan command.
3954 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3956 struct client_s
*client
= assuan_get_pointer (ctx
);
3958 if (!(client
->flags
& FLAG_LOCK_CMD
))
3959 unlock_file_mutex (client
, 0);
3961 log_write1 (_("command completed: rc=%u"), client
->last_rc
);
3962 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3964 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3969 help_command (assuan_context_t ctx
, char *line
)
3974 if (!line
|| !*line
)
3977 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3978 "For commands that take an element path as an argument, each element is "
3979 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3982 for (i
= 0; command_table
[i
]; i
++)
3984 if (!command_table
[i
]->help
)
3987 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
3992 tmp
= strip_texi_and_wrap (help
);
3994 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3996 return send_error (ctx
, rc
);
3999 for (i
= 0; command_table
[i
]; i
++)
4001 if (!strcasecmp (line
, command_table
[i
]->name
))
4005 if (!command_table
[i
]->help
)
4008 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4009 tmp
= str_asprintf (_("Usage: %s"), help
);
4011 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4013 return send_error (ctx
, rc
);
4017 return send_error (ctx
, GPG_ERR_INV_NAME
);
4021 new_command (const char *name
, int ignore
, int unlock
,
4022 gpg_error_t (*handler
) (assuan_context_t
, char *),
4028 for (i
= 0; command_table
[i
]; i
++);
4031 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4032 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4033 command_table
[i
]->name
= name
;
4034 command_table
[i
]->handler
= handler
;
4035 command_table
[i
]->ignore_startup
= ignore
;
4036 command_table
[i
]->unlock
= unlock
;
4037 command_table
[i
++]->help
= help
;
4038 command_table
[i
] = NULL
;
4046 for (i
= 0; command_table
[i
]; i
++)
4047 xfree (command_table
[i
]);
4049 xfree (command_table
);
4053 sort_commands (const void *arg1
, const void *arg2
)
4055 struct command_table_s
*const *a
= arg1
;
4056 struct command_table_s
*const *b
= arg2
;
4065 return strcmp ((*a
)->name
, (*b
)->name
);
4069 passwd_command (assuan_context_t ctx
, char *line
)
4071 struct client_s
*client
= assuan_get_pointer (ctx
);
4073 struct argv_s
*args
[] = {
4074 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4075 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4076 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4080 if (client
->flags
& FLAG_NEW
)
4081 return send_error (ctx
, GPG_ERR_INV_STATE
);
4083 client
->crypto
->save
.s2k_count
=
4084 config_get_ulong (client
->filename
, "s2k_count");
4085 rc
= parse_options (&line
, args
, client
);
4087 return send_error (ctx
, rc
);
4089 if (!rc
&& client
->opts
& OPT_RESET
)
4091 rc
= cache_clear (client
->md5file
);
4093 send_status_all (STATUS_CACHE
, NULL
);
4098 if (!IS_PKI (client
->crypto
))
4100 struct crypto_s
*crypto
;
4102 xfree (client
->crypto
->filename
);
4103 client
->crypto
->filename
= str_dup (client
->filename
);
4104 rc
= change_passwd (ctx
, client
->filename
,
4105 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4106 (client
->opts
& OPT_NO_PASSPHRASE
));
4109 cleanup_crypto (&client
->crypto
);
4110 client
->crypto
= crypto
;
4111 update_checksum (client
);
4112 cleanup_crypto_stage1 (client
->crypto
);
4118 if (client
->crypto
->save
.s2k_count
)
4119 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4120 "OPTION s2k-count=%lu",
4121 client
->crypto
->save
.s2k_count
);
4124 rc
= agent_passwd (client
->crypto
);
4129 return send_error (ctx
, rc
);
4133 parse_keygrip_opt_sign (void *data
, void *value
)
4135 struct client_s
*client
= data
;
4138 client
->opts
|= OPT_SIGN
;
4143 keygrip_command (assuan_context_t ctx
, char *line
)
4145 struct client_s
*client
= assuan_get_pointer (ctx
);
4147 struct crypto_s
*crypto
= NULL
;
4148 struct argv_s
*args
[] = {
4149 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4153 if (!line
|| !*line
)
4154 return send_error (ctx
, GPG_ERR_SYNTAX
);
4156 rc
= parse_options (&line
, args
, client
);
4158 return send_error (ctx
, rc
);
4160 if (!valid_filename (line
))
4161 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4163 rc
= init_client_crypto (&crypto
);
4165 return send_error (ctx
, rc
);
4167 rc
= read_data_file (line
, crypto
);
4170 char *hexgrip
= NULL
;
4172 if (!IS_PKI (crypto
))
4174 cleanup_crypto (&crypto
);
4175 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4178 if (client
->opts
& OPT_SIGN
)
4180 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4181 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4185 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4188 rc
= GPG_ERR_ENOMEM
;
4190 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4195 UPDATE_AGENT_CTX (client
, crypto
);
4196 cleanup_crypto (&crypto
);
4197 return send_error (ctx
, rc
);
4201 parse_opt_data (void *data
, void *value
)
4203 struct client_s
*client
= data
;
4206 client
->opts
|= OPT_DATA
;
4211 getinfo_command (assuan_context_t ctx
, char *line
)
4213 struct client_s
*client
= assuan_get_pointer (ctx
);
4215 char buf
[ASSUAN_LINELENGTH
];
4216 struct argv_s
*args
[] = {
4217 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4221 rc
= parse_options (&line
, args
, client
);
4223 return send_error (ctx
, rc
);
4225 if (!strcasecmp (line
, "clients"))
4227 if (client
->opts
& OPT_DATA
)
4229 MUTEX_LOCK (&cn_mutex
);
4230 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4231 MUTEX_UNLOCK (&cn_mutex
);
4232 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4235 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4237 else if (!strcasecmp (line
, "cache"))
4239 if (client
->opts
& OPT_DATA
)
4241 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4242 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4245 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4247 else if (!strcasecmp (line
, "pid"))
4250 pid_t pid
= getpid ();
4252 snprintf (buf
, sizeof (buf
), "%i", pid
);
4253 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4255 else if (!strcasecmp (line
, "version"))
4257 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4267 "", use_agent
? "AGENT" : "");
4268 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4271 else if (!strcasecmp (line
, "last_error"))
4273 if (client
->last_error
)
4274 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4276 rc
= GPG_ERR_NO_DATA
;
4279 rc
= gpg_error (GPG_ERR_SYNTAX
);
4281 return send_error (ctx
, rc
);
4286 send_data_cb (void *user
, const void *buf
, size_t len
)
4288 assuan_context_t ctx
= user
;
4290 return assuan_send_data (ctx
, buf
, len
);
4294 send_status_cb (void *user
, const char *line
)
4296 assuan_context_t ctx
= user
;
4297 char keyword
[200], *k
;
4300 for (p
= line
, k
= keyword
; *p
; p
++)
4312 while (isspace (*p
))
4315 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4320 agent_command (assuan_context_t ctx
, char *line
)
4325 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4328 struct client_s
*client
= assuan_get_pointer (ctx
);
4330 if (!line
|| !*line
)
4331 return send_error (ctx
, GPG_ERR_SYNTAX
);
4333 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4334 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4335 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4336 send_status_cb
, client
->ctx
);
4337 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4342 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4345 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4347 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4351 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4353 return send_error (ctx
, rc
);
4359 /* !BEGIN-HELP-TEXT!
4361 * This comment is used as a marker to generate the offline documentation
4362 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4363 * script to determine where commands begin and end.
4365 new_command("HELP", 1, 1, help_command
, _(
4366 "HELP [<COMMAND>]\n"
4367 "Show available commands or command specific help text."
4370 new_command("AGENT", 1, 1, agent_command
, _(
4372 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4373 "@command{gpg-agent}."
4376 new_command("GETINFO", 1, 1, getinfo_command
, _(
4377 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4378 "Get server and other information: @var{cache} returns the number of cached "
4379 "documents via a status message. @var{clients} returns the number of "
4380 "connected clients via a status message. @var{pid} returns the process ID "
4381 "number of the server via a data response. @var{VERSION} returns the server "
4382 "version number and compile-time features with a data response with each "
4383 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4384 "the last failed command when available. @xref{Status Messages}. "
4386 "When the @option{--data} option is specified then the result will be sent "
4387 "via a data response rather than a status message."
4390 new_command("PASSWD", 0, 0, passwd_command
, _(
4391 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4392 "Changes the passphrase of the secret key required to open the current "
4393 "file or the passphrase of a symmetrically encrypted data file. When the "
4394 "@option{--reset} option is passed then the cache entry for the current "
4395 "file will be reset and the passphrase, if any, will be required during the "
4396 "next @code{OPEN}. @xref{OPEN}."
4398 "The @option{--s2k-count} option sets number of hash iterations for a "
4399 "passphrase and must be either @code{0} to use the calibrated count of the "
4400 "machine (the default), or a value greater than or equal to @code{65536}. "
4401 "@xref{SAVE}. This option has no effect for symmetrically encrypted data "
4404 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4405 "the data file, although a passphrase may be required when changing it."
4408 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4409 "KEYGRIP [--sign] <filename>\n"
4410 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4413 "When the @option{--sign} option is specified then the key used for signing "
4414 "of the specified @var{filename} will be returned."
4416 "For symmetrically encrypted data files this command returns the error "
4417 "GPG_ERR_NOT_SUPPORTED."
4420 new_command("OPEN", 1, 1, open_command
, _(
4421 "OPEN [--lock] <filename> [<passphrase>]\n"
4422 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4423 "found on the file-system then a new document will be created. If the file "
4424 "is found, it is looked for in the file cache. If cached and no "
4425 "@var{passphrase} was specified then the cached document is opened. When not "
4426 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4427 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4430 "When the @option{--lock} option is passed then the file mutex will be "
4431 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4432 "file has been opened."
4435 new_command("SAVE", 0, 0, save_command
, _(
4436 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4437 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4438 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4439 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4440 "keypair will be generated and a pinentry will be used to prompt for the "
4441 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4442 "passed in which case the data file will not be passphrase protected. "
4444 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4445 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4446 "use is enabled. The datafile will be symmetrically encrypted and will not "
4447 "use or generate any keypair."
4449 "The @option{--reset} option will clear the cache entry for the current file "
4450 "and require a passphrase, if needed, before saving."
4452 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4453 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4454 "(@pxref{Configuration}) for available ciphers."
4456 "The @option{--cipher-iterations} option specifies the number of times to "
4457 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4459 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4460 "the client to obtain the key paramaters to use when generating a new "
4461 "keypair. The inquired data is expected to be an S-expression. If not "
4462 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4463 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4464 "that when this option is specified a new keypair will be generated "
4465 "reguardless if the file is a new one and that if the data file is protected "
4466 "the passphrase to open it will be required before generating the new keypair."
4468 "You can encrypt the data file to a public key other than the one that it "
4469 "was originally encrypted with by passing the @option{--keygrip} option with "
4470 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4471 "be of any key that @command{gpg-agent} knows about. The "
4472 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4473 "secret key. This option may be needed when using a smartcard. This option "
4474 "has no effect with symmetrically encrypted data files."
4476 "The @option{--s2k-count} option sets number of hash iterations for a "
4477 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4478 "value and is the default. This setting only affects new files. To change "
4479 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4480 "has no effect with symmetrically encrypted data files."
4483 new_command("ISCACHED", 1, 0, iscached_command
, _(
4484 "ISCACHED [--lock] <filename>\n"
4485 "An @emph{OK} response is returned if the specified @var{filename} is found "
4486 "in the file cache. If not found in the cache but exists on the filesystem "
4487 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4490 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4491 "file exists; it does not need to be opened nor cached."
4494 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4495 "CLEARCACHE [<filename>]\n"
4496 "Clears a file cache entry for all or the specified @var{filename}."
4499 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4500 "CACHETIMEOUT <filename> <seconds>\n"
4501 "The time in @var{seconds} until @var{filename} will be removed from the "
4502 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4503 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4504 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4508 new_command("LIST", 0, 1, list_command
, _(
4509 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4510 "If no element path is given then a newline separated list of root elements "
4511 "is returned with a data response. If given, then all reachable elements "
4512 "of the specified element path are returned unless the @option{--no-recurse} "
4513 "option is specified. If specified, only the child elements of the element "
4514 "path are returned without recursing into grandchildren. Each resulting "
4515 "element is prefixed with the literal @code{!} character when the element "
4516 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4518 "When the @option{--verbose} option is passed then each element path "
4519 "returned will have zero or more flags appened to it. These flags are "
4520 "delimited from the element path by a single space character. A flag itself "
4521 "is a single character. Flag @code{+} indicates that there are child nodes of "
4522 "the current element path. Flag @code{E} indicates that an element of an "
4523 "element path contained in a @var{target} attribute could not be found. Flag "
4524 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4525 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4526 "of the @var{target} attribute contained in the current element (see below)."
4528 "The @option{--with-target} option implies @option{--verbose} and will append "
4529 "an additional flag @code{T} followed by a single space then an element path. "
4530 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4531 "current element when it contains a @var{target} attribute. When no "
4532 "@var{target} attribute is found then no flag will be appended."
4534 "The @option{--no-recurse} option limits the amount of data returned to only "
4535 "the listing of children of the specified element path and not any "
4538 "The @option{--all} option lists the entire element tree for each root "
4539 "element. This option also implies option @option{--verbose}."
4541 "When the @option{--inquire} option is passed then all remaining non-option "
4542 "arguments are retrieved via a server @emph{INQUIRE}."
4545 new_command("REALPATH", 0, 1, realpath_command
, _(
4546 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4547 "Resolves all @code{target} attributes of the specified element path and "
4548 "returns the result with a data response. @xref{Target Attribute}, for details."
4550 "When the @option{--inquire} option is passed then all remaining non-option "
4551 "arguments are retrieved via a server @emph{INQUIRE}."
4554 new_command("STORE", 0, 1, store_command
, _(
4555 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4556 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4558 "Creates a new element path or modifies the @var{content} of an existing "
4559 "element. If only a single element is specified then a new root element is "
4560 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4561 "set to the final @key{TAB} delimited element. If no @var{content} is "
4562 "specified after the final @key{TAB}, then the content of the element will "
4563 "be removed, or empty when creating a new element."
4565 "The only restriction of an element name is that it not contain whitespace "
4566 "or begin with the literal element character @code{!} unless specifying a "
4567 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4568 "the @key{TAB} delimited elements. It is recommended that the content of an "
4569 "element be base64 encoded when it contains control or @key{TAB} characters "
4570 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4573 new_command("RENAME", 0, 1, rename_command
, _(
4574 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4575 "Renames the specified @var{element} to the new @var{value}. If an element of "
4576 "the same name as the @var{value} already exists it will be overwritten."
4578 "When the @option{--inquire} option is passed then all remaining non-option "
4579 "arguments are retrieved via a server @emph{INQUIRE}."
4582 new_command("COPY", 0, 1, copy_command
, _(
4583 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4584 "Copies the entire element tree starting from the child node of the source "
4585 "element, to the destination element path. If the destination element path "
4586 "does not exist then it will be created; otherwise it is overwritten."
4588 "Note that attributes from the source element are merged into the "
4589 "destination element when the destination element path exists. When an "
4590 "attribute of the same name exists in both the source and destination "
4591 "elements then the destination attribute will be updated to the source "
4594 "When the @option{--inquire} option is passed then all remaining non-option "
4595 "arguments are retrieved via a server @emph{INQUIRE}."
4598 new_command("MOVE", 0, 1, move_command
, _(
4599 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4600 "Moves the source element path to the destination element path. If the "
4601 "destination is not specified then it will be moved to the root node of the "
4602 "document. If the destination is specified and exists then it will be "
4603 "overwritten; otherwise non-existing elements of the destination element "
4604 "path will be created."
4606 "When the @option{--inquire} option is passed then all remaining non-option "
4607 "arguments are retrieved via a server @emph{INQUIRE}."
4610 new_command("DELETE", 0, 1, delete_command
, _(
4611 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4612 "Removes the specified element path and all of its children. This may break "
4613 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4614 "refers to this element or any of its children."
4616 "When the @option{--inquire} option is passed then all remaining non-option "
4617 "arguments are retrieved via a server @emph{INQUIRE}."
4620 new_command("GET", 0, 1, get_command
, _(
4621 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4622 "Retrieves the content of the specified element. The content is returned "
4623 "with a data response."
4625 "When the @option{--inquire} option is passed then all remaining non-option "
4626 "arguments are retrieved via a server @emph{INQUIRE}."
4629 new_command("ATTR", 0, 1, attr_command
, _(
4630 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4632 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4634 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4635 "element. When no @var{value} is specified any existing value will be removed."
4637 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4639 " Removes an @var{attribute} from an element."
4641 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4643 " Retrieves a newline separated list of attributes names and values "
4644 "from the specified element. Each attribute name and value is space delimited."
4646 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4648 " Retrieves the value of an @var{attribute} from an element."
4651 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4652 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4655 "The @code{_mtime} attribute is updated each time an element is modified by "
4656 "either storing content, editing attributes or by deleting a child element. "
4657 "The @code{_ctime} attribute is created for each new element in an element "
4660 "When the @option{--inquire} option is passed then all remaining non-option "
4661 "arguments are retrieved via a server @emph{INQUIRE}."
4663 "@xref{Target Attribute}, for details about this special attribute."
4666 new_command("XPATH", 0, 1, xpath_command
, _(
4667 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4668 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4669 "specified it is assumed the expression is a request to return a result. "
4670 "Otherwise, the result is set to the @var{value} argument and the document is "
4671 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4672 "is assumed to be empty and the document is updated. For example:"
4675 "XPATH //element[@@_name='password']@key{TAB}\n"
4678 "would clear the content of all @code{password} elements in the data file "
4679 "while leaving off the trailing @key{TAB} would return all @code{password} "
4680 "elements in @abbr{XML} format."
4682 "When the @option{--inquire} option is passed then all remaining non-option "
4683 "arguments are retrieved via a server @emph{INQUIRE}."
4685 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4686 "expression syntax."
4689 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4690 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4691 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4692 "attributes and does not return a result. For the @var{SET} operation the "
4693 "@var{value} is optional but the field is required. If not specified then "
4694 "the attribute value will be empty. For example:"
4697 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4700 "would create an @code{password} attribute for each @code{password} element "
4701 "found in the document. The attribute value will be empty but still exist."
4703 "When the @option{--inquire} option is passed then all remaining non-option "
4704 "arguments are retrieved via a server @emph{INQUIRE}."
4706 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4707 "expression syntax."
4710 new_command("IMPORT", 0, 1, import_command
, _(
4711 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4712 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4714 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4715 "argument is raw @abbr{XML} data. The content is created as a child of "
4716 "the element path specified with the @option{--root} option or at the "
4717 "document root when not specified. Existing elements of the same name will "
4720 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4724 new_command("DUMP", 0, 1, dump_command
, _(
4726 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4727 "dumping a specific node."
4730 new_command("LOCK", 0, 0, lock_command
, _(
4732 "Locks the mutex associated with the opened file. This prevents other clients "
4733 "from sending commands to the same opened file until the client "
4734 "that sent this command either disconnects or sends the @code{UNLOCK} "
4735 "command. @xref{UNLOCK}."
4738 new_command("UNLOCK", 1, 0, unlock_command
, _(
4740 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4741 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4742 "@pxref{ISCACHED})."
4745 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4746 "GETCONFIG [filename] <parameter>\n"
4747 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4748 "data response. If no file has been opened then the value for @var{filename} "
4749 "or the default from the @samp{global} section will be returned. If a file "
4750 "has been opened and no @var{filename} is specified, a value previously "
4751 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4754 new_command("OPTION", 1, 1, option_command
, _(
4755 "OPTION <NAME>=<VALUE>\n"
4756 "Sets a client option @var{name} to @var{value}. The value for an option is "
4757 "kept for the duration of the connection."
4760 "@item DISABLE-PINENTRY\n"
4761 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4762 "server inquire is sent to the client to obtain the passphrase. This option "
4763 "may be set as needed before the @pxref{OPEN}, @pxref{PASSWD}, and "
4764 "@pxref{SAVE} commands."
4766 "@item PINENTRY-TIMEOUT\n"
4767 "Sets the number of seconds before a pinentry prompt will return an error "
4768 "while waiting for user input."
4771 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4774 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4777 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4779 "@item PINENTRY-DESC\n"
4780 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4782 "@item PINENTRY-TITLE\n"
4783 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4785 "@item PINENTRY-PROMPT\n"
4786 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4789 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4791 "@item LC-MESSAGES\n"
4792 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4795 "Associates the thread ID of the connection with the specified textual "
4796 "representation. Useful for debugging log messages."
4798 "@item LOCK-TIMEOUT\n"
4799 "When not @code{0}, the duration in tenths of a second to wait for the file "
4800 "mutex which has been locked by another thread to be released before returning "
4801 "an error. When @code{-1}, then an error will be returned immediately."
4804 "An integer specifiying the logging level."
4808 new_command("LS", 1, 1, ls_command
, _(
4810 "Lists the available data files stored in the data directory "
4811 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4814 new_command("RESET", 1, 1, NULL
, _(
4816 "Closes the currently opened file but keeps any previously set client options."
4819 new_command("NOP", 1, 1, NULL
, _(
4821 "Does nothing. Always returns successfully."
4824 /* !END-HELP-TEXT! */
4825 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4826 new_command ("END", 1, 1, NULL
, NULL
);
4827 new_command ("BYE", 1, 1, NULL
, NULL
);
4830 for (i
= 0; command_table
[i
]; i
++);
4831 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4836 register_commands (assuan_context_t ctx
)
4840 for (; command_table
[i
]; i
++)
4842 if (!command_table
[i
]->handler
)
4845 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4846 command_table
[i
]->handler
,
4847 command_table
[i
]->help
);
4852 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4856 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4860 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4864 return assuan_register_post_cmd_notify (ctx
, command_finalize
);