2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
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>
37 #include "pwmd-error.h"
42 #include "util-misc.h"
51 /* Flags needed to be retained for a client across commands. */
52 #define FLAG_NEW 0x0001
53 #define FLAG_HAS_LOCK 0x0002
54 #define FLAG_LOCK_CMD 0x0004
55 #define FLAG_NO_PINENTRY 0x0008
56 #define FLAG_OPEN 0x0010
57 #define FLAG_KEEP_LOCK 0x0020
59 /* These are command option flags. */
60 #define OPT_INQUIRE 0x0001
61 #define OPT_NO_PASSPHRASE 0x0002
62 #define OPT_RESET 0x0004
63 #define OPT_LIST_RECURSE 0x0008
64 #define OPT_LIST_VERBOSE 0x0010
65 #define OPT_LOCK 0x0020
66 #define OPT_LOCK_ON_OPEN 0x0040
67 #define OPT_SIGN 0x0080
68 #define OPT_LIST_ALL 0x0100
69 #define OPT_LIST_WITH_TARGET 0x0200
70 #define OPT_DATA 0x0400
71 #define OPT_NO_AGENT 0x0800
74 /* The GETCONFIG command, for example, may fail because pwmd lost the
75 * gpg-agent connection. Update the recovered agent ctx. */
76 #define UPDATE_AGENT_CTX(client, crypto) do { \
77 if (crypto && crypto->agent && crypto->agent->did_restart \
78 && client->crypto && client->crypto->agent \
79 && client->crypto->agent->ctx && \
80 crypto->agent->ctx != client->crypto->agent->ctx) \
82 client->crypto->agent->ctx = crypto->agent->ctx; \
83 crypto->agent->ctx = NULL; \
87 #define UPDATE_AGENT_CTX(client, crypto)
90 struct command_table_s
93 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
96 int unlock
; // unlock the file mutex after validating the checksum
99 static struct command_table_s
**command_table
;
101 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
102 static gpg_error_t
validate_checksum (struct client_s
*,
103 struct cache_data_s
*);
104 static gpg_error_t
update_checksum (struct client_s
*client
);
107 unlock_file_mutex (struct client_s
*client
, int remove
)
111 // OPEN: keep the lock for the same file being reopened.
112 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
115 if (!(client
->flags
& FLAG_HAS_LOCK
))
116 return GPG_ERR_NOT_LOCKED
;
118 rc
= cache_unlock_mutex (client
->md5file
, remove
);
120 rc
= GPG_ERR_INV_STATE
;
122 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
128 lock_file_mutex (struct client_s
*client
, int add
)
131 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
133 if (client
->flags
& FLAG_HAS_LOCK
)
136 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
137 client
->lock_timeout
, add
, timeout
);
139 client
->flags
|= FLAG_HAS_LOCK
;
145 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
149 if (!(client
->flags
& FLAG_OPEN
))
150 return GPG_ERR_INV_STATE
;
152 rc
= lock_file_mutex (client
, 0);
153 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
155 rc
= validate_checksum (client
, NULL
);
156 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
159 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
162 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
163 unlock_file_mutex (client
, 0);
169 parse_xml (assuan_context_t ctx
, int new)
171 struct client_s
*client
= assuan_get_pointer (ctx
);
172 int cached
= client
->doc
!= NULL
;
177 client
->doc
= new_document ();
183 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
184 client
->crypto
->plaintext
= result
;
185 client
->crypto
->plaintext_len
= len
;
186 if (!client
->crypto
->plaintext
)
188 xmlFreeDoc (client
->doc
);
194 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
195 client
->crypto
->plaintext_len
, (xmlDocPtr
*)&client
->doc
);
201 free_client (struct client_s
*client
)
204 xmlFreeDoc (client
->doc
);
207 xfree (client
->filename
);
208 xfree (client
->last_error
);
212 cleanup_crypto_stage2 (client
->crypto
);
213 if (client
->crypto
->pkey_sexp
)
214 gcry_sexp_release (client
->crypto
->pkey_sexp
);
216 client
->crypto
->pkey_sexp
= NULL
;
217 memset (client
->crypto
->sign_grip
, 0,
218 sizeof (client
->crypto
->sign_grip
));
219 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
224 cleanup_client (struct client_s
*client
)
226 assuan_context_t ctx
= client
->ctx
;
227 struct client_thread_s
*thd
= client
->thd
;
228 struct crypto_s
*crypto
= client
->crypto
;
229 long lock_timeout
= client
->lock_timeout
;
230 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
231 struct pinentry_option_s pin_opts
;
232 xmlErrorPtr xml_error
= client
->xml_error
;
234 struct pinentry_option_s agent_pin_opts
;
236 if (crypto
&& crypto
->agent
)
237 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
238 sizeof(struct pinentry_option_s
));
241 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
242 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
243 free_client (client
);
244 memset (client
, 0, sizeof (struct client_s
));
245 client
->xml_error
= xml_error
;
246 client
->crypto
= crypto
;
249 client
->lock_timeout
= lock_timeout
;
250 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
251 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
253 if (crypto
&& crypto
->agent
)
254 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
255 sizeof(struct pinentry_option_s
));
260 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
262 struct client_s
*client
= assuan_get_pointer (ctx
);
264 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
265 int cached
= 0, keyarg
= key
? 1 : 0;
267 unsigned char *salted_key
= NULL
;
271 char *pin_title
= client
->pinentry_opts
.title
;
273 client
->crypto
->filename
= str_dup (client
->filename
);
274 if (cdata
|| client
->flags
& FLAG_NEW
)
276 if (cdata
&& !(client
->flags
& FLAG_NEW
))
278 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
283 cached
= cdata
!= NULL
;
287 if (!key
&& !IS_PKI (client
->crypto
)
288 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
290 if (client
->flags
& FLAG_NO_PINENTRY
)
292 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
299 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
302 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
309 if (!IS_PKI (client
->crypto
))
311 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
314 key
= gcry_malloc (keylen
);
315 memset (key
, 0, keylen
);
318 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
319 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
320 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
321 (void **)&salted_key
, &keysize
);
327 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
328 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
329 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
331 gcry_free (salted_key
);
335 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
340 if (client
->pinentry_opts
.title
!= pin_title
)
341 xfree (client
->pinentry_opts
.title
);
343 client
->pinentry_opts
.title
= pin_title
;
346 gcry_free (salted_key
);
350 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
353 gcry_free (salted_key
);
354 return GPG_ERR_ENOMEM
;
357 cdata
->key
= salted_key
;
358 cdata
->keylen
= keysize
;
361 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
366 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
368 free_cache_data_once (cdata
);
372 int timeout
= config_get_integer (client
->filename
,
378 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
380 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
381 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
382 client
->crypto
->plaintext_len
, &cdata
->doc
,
383 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
385 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
387 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
388 "%S", client
->crypto
->pkey_sexp
);
393 gcry_sexp_release (cdata
->sigkey
);
395 cdata
->sigkey
= NULL
;
396 if (!rc
&& IS_PKI (client
->crypto
))
397 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
398 "%S", client
->crypto
->sigpkey_sexp
);
400 if (!rc
&& !cache_add_file (client
->md5file
,
401 (client
->flags
& FLAG_NEW
)
403 : client
->crypto
->grip
, cdata
, timeout
))
407 free_cache_data_once (cdata
);
408 xmlFreeDoc (client
->doc
);
415 client
->flags
|= FLAG_OPEN
;
420 update_checksum (client
);
422 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
423 rc
= do_lock (client
, 0);
429 req_cleanup (void *arg
)
434 strv_free ((char **) arg
);
438 parse_open_opt_lock (void *data
, void *value
)
440 struct client_s
*client
= data
;
442 client
->opts
|= OPT_LOCK_ON_OPEN
;
447 parse_opt_inquire (void *data
, void *value
)
449 struct client_s
*client
= data
;
452 client
->opts
|= OPT_INQUIRE
;
457 update_checksum (struct client_s
*client
)
461 struct cache_data_s
*cdata
;
462 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
469 cdata
= cache_get_data (client
->md5file
);
473 cdata
->crc
= xmalloc (len
);
474 memcpy (cdata
->crc
, crc
, len
);
481 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
488 if (cdata
&& !cdata
->crc
)
489 return GPG_ERR_CHECKSUM
;
491 rc
= get_checksum (client
->filename
, &crc
, &len
);
496 n
= memcmp (cdata
->crc
, crc
, len
);
497 else if (client
->crc
)
498 n
= memcmp (client
->crc
, crc
, len
);
501 return n
? GPG_ERR_CHECKSUM
: 0;
505 do_open (assuan_context_t ctx
, const char *filename
, const char *password
)
507 struct client_s
*client
= assuan_get_pointer (ctx
);
508 struct cache_data_s
*cdata
;
512 if (!valid_filename (filename
))
513 return GPG_ERR_INV_VALUE
;
515 gcry_md_hash_buffer (GCRY_MD_MD5
, client
->md5file
, filename
,
517 client
->filename
= str_dup (filename
);
518 if (!client
->filename
)
519 return GPG_ERR_ENOMEM
;
522 cdata
= cache_get_data (client
->md5file
);
523 if (cdata
&& cdata
->doc
)
527 /* This will check that the key is cached in the agent which needs to
528 * be determined for files that share a keygrip. */
531 rc
= cache_iscached (client
->filename
, &defer
);
532 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
533 rc
= GPG_ERR_KEY_EXPIRED
;
536 if (!rc
&& !(client
->flags
& FLAG_NEW
))
537 rc
= validate_checksum (client
, cdata
);
540 if (!rc
&& client
->thd
->remote
541 && config_get_boolean (client
->filename
, "tcp_require_key"))
542 rc
= GPG_ERR_KEY_EXPIRED
;
545 if (!rc
&& !password
)
547 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
551 if (IS_PKI (client
->crypto
))
553 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
555 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
556 client
->crypto
->grip
);
557 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
559 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
560 client
->crypto
->sign_grip
);
565 rc
= open_finalize (ctx
, NULL
, 0);
571 /* There was an error accessing the file so clear the cache entry. The
572 * real error will be returned from read_data_file() since the file
573 * may have only disappeared. */
576 log_write ("%s: %s", filename
, pwmd_strerror (rc
));
577 rc
= cache_clear (client
->md5file
);
578 send_status_all (STATUS_CACHE
, NULL
);
585 rc
= read_data_file (client
->filename
, client
->crypto
);
588 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
589 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
591 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
595 client
->flags
|= FLAG_NEW
;
596 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
597 memset (client
->crypto
->sign_grip
, 0,
598 sizeof (client
->crypto
->sign_grip
));
599 rc
= open_finalize (ctx
, NULL
, 0);
603 if (password
&& IS_PKI (client
->crypto
))
606 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
612 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
617 open_command (assuan_context_t ctx
, char *line
)
620 struct client_s
*client
= assuan_get_pointer (ctx
);
621 char **req
, *filename
;
622 unsigned char md5file
[16];
624 assuan_peercred_t peer
;
625 struct argv_s
*args
[] = {
626 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
630 rc
= parse_options (&line
, args
, client
);
632 return send_error (ctx
, rc
);
634 req
= str_split (line
, " ", 2);
636 return send_error (ctx
, GPG_ERR_SYNTAX
);
638 rc
= do_validate_peer (ctx
, req
[0], &peer
);
642 return send_error (ctx
, rc
);
645 pthread_cleanup_push (req_cleanup
, req
);
647 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
648 /* This client may have locked a different file with ISCACHED --lock than
649 * the current filename. This will remove that lock. */
650 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
651 if (client
->flags
& FLAG_OPEN
||
652 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
654 typeof (client
->opts
) opts
= client
->opts
;
655 typeof (client
->flags
) flags
= client
->flags
;
658 client
->flags
|= FLAG_KEEP_LOCK
;
660 cleanup_client (client
);
662 client
->flags
|= flags
;
663 client
->flags
&= ~(FLAG_LOCK_CMD
);
665 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
668 /* Need to lock the mutex here because file_modified() cannot without
669 * knowing the filename. */
670 memcpy (client
->md5file
, md5file
, 16);
671 rc
= lock_file_mutex (client
, 1);
674 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
677 if (IS_PKI (client
->crypto
))
678 rc
= set_pinentry_mode (client
->crypto
->agent
,
679 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
684 rc
= do_open (ctx
, filename
, password
);
686 cleanup_client (client
);
688 cleanup_crypto_stage1 (client
->crypto
);
692 pthread_cleanup_pop (1);
694 if (!rc
&& client
->flags
& FLAG_NEW
)
695 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
698 (void) kill_scd (client
->crypto
->agent
);
700 return send_error (ctx
, rc
);
704 parse_opt_no_passphrase (void *data
, void *value
)
706 struct client_s
*client
= data
;
709 client
->opts
|= OPT_NO_PASSPHRASE
;
714 parse_save_opt_no_agent (void *data
, void *value
)
716 struct client_s
*client
= data
;
718 client
->opts
|= OPT_NO_AGENT
;
723 parse_save_opt_cipher (void *data
, void *value
)
725 struct client_s
*client
= data
;
726 int algo
= cipher_string_to_gcrypt ((char *) value
);
727 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
730 return GPG_ERR_INV_VALUE
;
732 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
737 parse_save_opt_keygrip (void *data
, void *value
)
739 struct client_s
*client
= data
;
741 if (!IS_PKI (client
->crypto
))
742 return GPG_ERR_INV_ARG
;
745 if (client
->crypto
->save
.pkey
)
746 gcry_sexp_release (client
->crypto
->save
.pkey
);
748 client
->crypto
->save
.pkey
= NULL
;
749 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
751 return GPG_ERR_INV_ARG
;
756 parse_save_opt_sign_keygrip (void *data
, void *value
)
758 struct client_s
*client
= data
;
760 if (!IS_PKI (client
->crypto
))
761 return GPG_ERR_INV_ARG
;
764 if (client
->crypto
->save
.sigpkey
)
765 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
767 client
->crypto
->save
.sigpkey
= NULL
;
768 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
770 return GPG_ERR_INV_ARG
;
775 parse_opt_s2k_count (void *data
, void *value
)
777 struct client_s
*client
= data
;
781 return GPG_ERR_INV_VALUE
;
783 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
788 parse_save_opt_iterations (void *data
, void *value
)
790 struct client_s
*client
= data
;
795 return GPG_ERR_INV_VALUE
;
798 n
= strtoull (v
, &p
, 10);
799 if (n
== UINT64_MAX
&& errno
)
800 return gpg_error_from_errno (errno
);
802 return GPG_ERR_INV_VALUE
;
804 client
->crypto
->save
.hdr
.iterations
= n
;
809 save_finalize (assuan_context_t ctx
)
811 struct client_s
*client
= assuan_get_pointer (ctx
);
813 xmlChar
*xmlbuf
= NULL
;
818 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
820 return GPG_ERR_ENOMEM
;
822 pthread_cleanup_push (xmlFree
, xmlbuf
);
824 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
825 && (client
->opts
& OPT_NO_AGENT
))
826 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
828 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
829 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
830 NULL
, &key
, &keylen
, 1, 1,
831 (client
->opts
& OPT_NO_PASSPHRASE
));
836 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
837 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
840 pubkey
= client
->crypto
->pkey_sexp
;
844 sigkey
= client
->crypto
->sigpkey_sexp
;
847 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
848 sigkey
= client
->crypto
->save
.sigpkey
;
852 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
853 client
->filename
, xmlbuf
, xmlbuflen
);
854 if (pubkey
== client
->crypto
->save
.pkey
)
858 gcry_sexp_release (client
->crypto
->pkey_sexp
);
859 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
860 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
861 client
->crypto
->grip
);
864 gcry_sexp_release (pubkey
);
866 client
->crypto
->save
.pkey
= NULL
;
870 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
872 if (sigkey
== client
->crypto
->save
.sigpkey
)
876 if (client
->crypto
->sigpkey_sexp
)
877 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
879 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
880 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
881 client
->crypto
->sign_grip
);
884 gcry_sexp_release (sigkey
);
886 client
->crypto
->save
.sigpkey
= NULL
;
895 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
896 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
900 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
901 send_status_all (STATUS_CACHE
, NULL
);
905 rc
= update_checksum (client
);
906 client
->flags
&= ~(FLAG_NEW
);
910 pthread_cleanup_pop (1); // xmlFree
915 parse_opt_reset (void *data
, void *value
)
917 struct client_s
*client
= data
;
920 client
->opts
|= OPT_RESET
;
925 save_command (assuan_context_t ctx
, char *line
)
927 struct client_s
*client
= assuan_get_pointer (ctx
);
930 struct argv_s
*args
[] = {
931 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
932 parse_opt_no_passphrase
},
933 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
934 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
936 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
937 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
938 parse_save_opt_sign_keygrip
},
939 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
940 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
941 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
942 parse_save_opt_iterations
},
943 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
944 parse_save_opt_no_agent
},
948 cleanup_save (&client
->crypto
->save
);
949 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
950 sizeof (file_header_t
));
951 client
->crypto
->save
.s2k_count
=
952 config_get_ulong (client
->filename
, "s2k_count");
954 if (client
->flags
& FLAG_NEW
)
955 client
->crypto
->save
.hdr
.iterations
=
956 config_get_ulonglong (client
->filename
, "cipher_iterations");
958 rc
= parse_options (&line
, args
, client
);
960 return send_error (ctx
, rc
);
962 if (!(client
->flags
& FLAG_NEW
))
963 client
->opts
&= ~OPT_NO_AGENT
;
964 else if (client
->opts
& OPT_NO_AGENT
)
966 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
967 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
970 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
971 && !(client
->opts
& OPT_INQUIRE
))
972 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
974 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
975 return send_error (ctx
, gpg_error_from_errno (errno
));
977 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
979 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
980 return send_error (ctx
, GPG_ERR_ENOANO
);
984 rc
= cache_iscached (client
->filename
, &defer
);
985 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
987 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
990 if ((!rc
&& defer
) || config_get_boolean ("global", "require_save_key"))
991 client
->opts
|= OPT_RESET
;
993 if (!rc
&& (client
->opts
& OPT_RESET
))
995 rc
= cache_clear (client
->md5file
);
997 return send_error (ctx
, rc
);
999 log_write ("%s: %s", client
->filename
,
1000 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1001 send_status_all (STATUS_CACHE
, NULL
);
1005 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
));
1008 return send_error (ctx
, rc
);
1011 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1012 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1013 && !(client
->opts
& OPT_NO_AGENT
))
1015 rc
= set_pinentry_mode (client
->crypto
->agent
,
1016 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1019 if (!(client
->flags
& FLAG_NEW
))
1021 struct crypto_s
*crypto
;
1025 /* Wanting to generate a new key. Require the key to open the
1026 current file before proceeding reguardless of the
1027 require_save_key configuration parameter. */
1028 rc
= cache_clear (client
->md5file
);
1031 rc
= init_client_crypto (&crypto
);
1033 crypto
->client_ctx
= client
->ctx
;
1037 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1038 client
->filename
, &key
, &keylen
);
1041 cleanup_crypto (&crypto
);
1045 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1049 struct inquire_data_s idata
= { 0 };
1050 char *params
= client
->opts
& OPT_INQUIRE
1051 ? NULL
: default_key_params (client
->crypto
);
1053 pthread_cleanup_push (xfree
, params
);
1054 idata
.crypto
= client
->crypto
;
1058 idata
.line
= params
;
1059 idata
.len
= strlen (params
);
1064 client
->crypto
->agent
->inquire_data
= &idata
;
1065 client
->crypto
->agent
->inquire_cb
= NULL
;
1066 rc
= generate_key (client
->crypto
, params
,
1067 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1068 pthread_cleanup_pop (1);
1074 rc
= save_finalize (ctx
);
1076 cleanup_crypto_stage1 (client
->crypto
);
1078 (void) kill_scd (client
->crypto
->agent
);
1080 return send_error (ctx
, rc
);
1084 do_delete (assuan_context_t ctx
, char *line
)
1086 struct client_s
*client
= assuan_get_pointer (ctx
);
1091 if (strchr (line
, '\t'))
1092 req
= str_split (line
, "\t", 0);
1094 req
= str_split (line
, " ", 0);
1097 return GPG_ERR_SYNTAX
;
1099 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1107 * No sub-node defined. Remove the entire node (root element).
1113 rc
= unlink_node (n
);
1122 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1130 rc
= unlink_node (n
);
1138 delete_command (assuan_context_t ctx
, char *line
)
1140 struct client_s
*client
= assuan_get_pointer (ctx
);
1142 struct argv_s
*args
[] = {
1143 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1147 rc
= parse_options (&line
, args
, client
);
1149 return send_error (ctx
, rc
);
1151 if (client
->opts
& OPT_INQUIRE
)
1153 unsigned char *result
;
1156 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1158 return send_error (ctx
, rc
);
1160 line
= (char *) result
;
1163 rc
= do_delete (ctx
, line
);
1165 if (client
->opts
& OPT_INQUIRE
)
1168 return send_error (ctx
, rc
);
1172 store_command (assuan_context_t ctx
, char *line
)
1174 struct client_s
*client
= assuan_get_pointer (ctx
);
1177 unsigned char *result
;
1179 xmlNodePtr n
, parent
;
1181 char *content
= NULL
;
1183 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1185 return send_error (ctx
, rc
);
1187 req
= str_split ((char *) result
, "\t", 0);
1191 return send_error (ctx
, GPG_ERR_SYNTAX
);
1193 len
= strv_length (req
);
1194 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1195 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1198 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1201 if (has_content
|| !*req
[len
- 1])
1204 content
= req
[len
- 1];
1205 req
[len
- 1] = NULL
;
1209 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1210 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1212 rc
= new_root_element (client
, client
->doc
, *req
);
1216 return send_error (ctx
, rc
);
1225 return send_error (ctx
, rc
);
1230 if (req
[1] && *req
[1])
1233 parent
= create_elements_cb (client
, n
, req
+ 1, &rc
, NULL
);
1235 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1236 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1242 n
= find_text_node (parent
->children
);
1244 xmlNodeSetContent (n
, (xmlChar
*) content
);
1246 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1248 update_element_mtime (parent
);
1253 return send_error (ctx
, rc
);
1257 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1262 int progress
= config_get_integer ("global", "xfer_progress");
1266 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1267 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1268 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1276 if (sent
+ to_send
> total
)
1277 to_send
= total
- sent
;
1279 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1280 flush
? 0 : to_send
);
1283 sent
+= flush
? 0 : to_send
;
1285 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1286 (sent
== total
&& flush
))
1287 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1289 if (!flush
&& !rc
&& sent
== total
)
1296 while (!rc
&& sent
< total
);
1302 do_get (assuan_context_t ctx
, char *line
)
1304 struct client_s
*client
= assuan_get_pointer (ctx
);
1309 req
= str_split (line
, "\t", 0);
1314 return GPG_ERR_SYNTAX
;
1317 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1326 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1333 if (!n
|| !n
->children
)
1334 return GPG_ERR_NO_DATA
;
1336 n
= find_text_node (n
->children
);
1337 if (!n
|| !n
->content
|| !*n
->content
)
1338 return GPG_ERR_NO_DATA
;
1340 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1345 get_command (assuan_context_t ctx
, char *line
)
1347 struct client_s
*client
= assuan_get_pointer (ctx
);
1349 struct argv_s
*args
[] = {
1350 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1354 rc
= parse_options (&line
, args
, client
);
1356 return send_error (ctx
, rc
);
1358 if (client
->opts
& OPT_INQUIRE
)
1360 unsigned char *result
;
1363 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1365 return send_error (ctx
, rc
);
1367 line
= (char *) result
;
1370 rc
= do_get (ctx
, line
);
1372 if (client
->opts
& OPT_INQUIRE
)
1375 return send_error (ctx
, rc
);
1378 static void list_command_cleanup1 (void *arg
);
1380 realpath_command (assuan_context_t ctx
, char *line
)
1382 struct string_s
*string
= NULL
;
1384 struct client_s
*client
= assuan_get_pointer (ctx
);
1385 struct argv_s
*args
[] = {
1386 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1390 rc
= parse_options (&line
, args
, client
);
1392 return send_error (ctx
, rc
);
1394 if (client
->opts
& OPT_INQUIRE
)
1396 unsigned char *result
;
1399 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1401 return send_error (ctx
, rc
);
1403 line
= (char *) result
;
1406 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1409 pthread_cleanup_push (list_command_cleanup1
, string
);
1410 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1411 pthread_cleanup_pop (1);
1414 if (client
->opts
& OPT_INQUIRE
)
1417 return send_error (ctx
, rc
);
1421 list_command_cleanup1 (void *arg
)
1424 string_free ((struct string_s
*) arg
, 1);
1428 list_command_cleanup2 (void *arg
)
1430 struct element_list_s
*elements
= arg
;
1436 int total
= slist_length (elements
->list
);
1439 for (i
= 0; i
< total
; i
++)
1441 char *tmp
= slist_nth_data (elements
->list
, i
);
1445 slist_free (elements
->list
);
1448 if (elements
->prefix
)
1449 xfree (elements
->prefix
);
1452 strv_free (elements
->req
);
1459 parse_list_opt_norecurse (void *data
, void *value
)
1461 struct client_s
*client
= data
;
1463 client
->opts
&= ~(OPT_LIST_RECURSE
);
1468 parse_list_opt_verbose (void *data
, void *value
)
1470 struct client_s
*client
= data
;
1472 client
->opts
|= OPT_LIST_VERBOSE
;
1477 parse_list_opt_target (void *data
, void *value
)
1479 struct client_s
*client
= data
;
1481 client
->opts
|= OPT_LIST_WITH_TARGET
;
1486 parse_list_opt_all (void *data
, void *value
)
1488 struct client_s
*client
= data
;
1490 client
->opts
|= OPT_LIST_ALL
;
1495 list_path_once (struct client_s
*client
, char *line
,
1496 struct element_list_s
*elements
, struct string_s
*result
)
1500 elements
->req
= str_split (line
, " ", 0);
1502 strv_printf (&elements
->req
, "%s", line
);
1504 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1505 if (rc
== GPG_ERR_ELOOP
&& elements
->verbose
)
1510 int total
= slist_length (elements
->list
);
1513 rc
= GPG_ERR_NO_DATA
;
1521 for (i
= 0; i
< total
; i
++)
1523 char *tmp
= slist_nth_data (elements
->list
, i
);
1525 string_append_printf (result
, "%s%s", tmp
,
1526 i
+ 1 == total
? "" : "\n");
1531 rc
= GPG_ERR_NO_DATA
;
1538 has_list_flag (char *path
, char *flags
)
1542 while (*p
&& *++p
!= ' ');
1551 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1562 do_list (assuan_context_t ctx
, char *line
)
1564 struct client_s
*client
= assuan_get_pointer (ctx
);
1566 struct element_list_s
*elements
= NULL
;
1568 elements
= xcalloc (1, sizeof (struct element_list_s
));
1570 return GPG_ERR_ENOMEM
;
1572 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1574 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1575 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1577 if (!line
|| !*line
)
1579 struct string_s
*str
= NULL
;
1581 pthread_cleanup_push (list_command_cleanup2
, elements
);
1582 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1583 elements
->with_target
);
1584 pthread_cleanup_pop (1);
1585 pthread_cleanup_push (list_command_cleanup1
, str
);
1589 if (client
->opts
& OPT_LIST_ALL
)
1591 char **roots
= str_split (str
->str
, "\n", 0);
1594 pthread_cleanup_push (req_cleanup
, roots
);
1595 string_truncate (str
, 0);
1597 for (p
= roots
; *p
; p
++)
1599 if (strchr (*p
, ' '))
1601 if (has_list_flag (*p
, "EO"))
1603 string_append_printf (str
, "%s%s", *p
,
1604 *(p
+ 1) ? "\n" : "");
1609 elements
= xcalloc (1, sizeof (struct element_list_s
));
1612 rc
= GPG_ERR_ENOMEM
;
1616 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1618 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1619 OPT_LIST_WITH_TARGET
);
1620 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1621 pthread_cleanup_push (list_command_cleanup2
, elements
);
1622 rc
= list_path_once (client
, *p
, elements
, str
);
1623 pthread_cleanup_pop (1);
1628 string_append (str
, "\n");
1631 pthread_cleanup_pop (1);
1635 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1638 pthread_cleanup_pop (1);
1642 pthread_cleanup_push (list_command_cleanup2
, elements
);
1643 struct string_s
*str
= string_new (NULL
);
1644 pthread_cleanup_push (list_command_cleanup1
, str
);
1645 rc
= list_path_once (client
, line
, elements
, str
);
1647 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1649 pthread_cleanup_pop (1);
1650 pthread_cleanup_pop (1);
1655 list_command (assuan_context_t ctx
, char *line
)
1657 struct client_s
*client
= assuan_get_pointer (ctx
);
1659 struct argv_s
*args
[] = {
1660 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1661 parse_list_opt_norecurse
},
1662 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1663 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1664 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1665 parse_list_opt_target
},
1666 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1670 if (disable_list_and_dump
== 1)
1671 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1673 client
->opts
|= OPT_LIST_RECURSE
;
1674 rc
= parse_options (&line
, args
, client
);
1676 return send_error (ctx
, rc
);
1678 if (client
->opts
& OPT_LIST_ALL
)
1679 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1681 if (client
->opts
& OPT_INQUIRE
)
1683 unsigned char *result
;
1686 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1688 return send_error (ctx
, rc
);
1690 line
= (char *) result
;
1693 rc
= do_list (ctx
, line
);
1695 if (client
->opts
& OPT_INQUIRE
)
1698 return send_error (ctx
, rc
);
1702 * req[0] - element path
1705 attribute_list (assuan_context_t ctx
, char **req
)
1707 struct client_s
*client
= assuan_get_pointer (ctx
);
1708 char **attrlist
= NULL
;
1716 if (!req
|| !req
[0])
1717 return GPG_ERR_SYNTAX
;
1719 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1722 * The first argument may be only a root element.
1724 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1725 return GPG_ERR_SYNTAX
;
1728 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1738 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1739 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1750 for (a
= n
->properties
; a
; a
= a
->next
)
1754 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1757 strv_free (attrlist
);
1759 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1760 pwmd_strerror (GPG_ERR_ENOMEM
));
1761 return GPG_ERR_ENOMEM
;
1766 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1768 && an
->content
? (char *) an
->content
: "");
1772 strv_free (attrlist
);
1773 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1774 pwmd_strerror (GPG_ERR_ENOMEM
));
1775 return GPG_ERR_ENOMEM
;
1778 attrlist
[++i
] = NULL
;
1782 return GPG_ERR_NO_DATA
;
1784 line
= strv_join ("\n", attrlist
);
1788 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1789 pwmd_strerror (GPG_ERR_ENOMEM
));
1790 strv_free (attrlist
);
1791 return GPG_ERR_ENOMEM
;
1794 pthread_cleanup_push (xfree
, line
);
1795 pthread_cleanup_push (req_cleanup
, attrlist
);
1796 rc
= xfer_data (ctx
, line
, strlen (line
));
1797 pthread_cleanup_pop (1);
1798 pthread_cleanup_pop (1);
1803 * req[0] - attribute
1804 * req[1] - element path
1807 attribute_delete (struct client_s
*client
, char **req
)
1813 if (!req
|| !req
[0] || !req
[1])
1814 return GPG_ERR_SYNTAX
;
1816 if (!strcmp (req
[0], "_name"))
1817 return GPG_ERR_INV_ATTR
;
1819 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1822 * The first argument may be only a root element.
1824 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1825 return GPG_ERR_SYNTAX
;
1828 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1834 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1835 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1840 rc
= delete_attribute (n
, (xmlChar
*) req
[0]);
1848 create_element_path (struct client_s
*client
,
1849 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1851 char **req
= *elements
;
1852 char **req_orig
= strv_dup (req
);
1853 xmlNodePtr n
= NULL
;
1859 *rc
= GPG_ERR_ENOMEM
;
1860 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1861 pwmd_strerror (GPG_ERR_ENOMEM
));
1866 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
1869 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1872 *rc
= new_root_element (client
, client
->doc
, req
[0]);
1878 else if (n
== parent
)
1880 *rc
= GPG_ERR_CONFLICT
;
1887 n
= create_target_elements_cb (client
, n
, req
+ 1, rc
, NULL
);
1889 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
1890 NULL
, NULL
, create_target_elements_cb
, 0, 0,
1897 * Reset the position of the element tree now that the elements
1898 * have been created.
1903 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
1907 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
1908 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1915 strv_free (req_orig
);
1922 * Creates a "target" attribute. When other commands encounter an element with
1923 * this attribute, the element path is modified to the target value. If the
1924 * source element path doesn't exist when using 'ATTR SET target', it is
1925 * created, but the destination element path must exist.
1927 * req[0] - source element path
1928 * req[1] - destination element path
1931 target_attribute (struct client_s
*client
, char **req
)
1933 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
1937 if (!req
|| !req
[0] || !req
[1])
1938 return GPG_ERR_SYNTAX
;
1940 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
1943 * The first argument may be only a root element.
1945 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
1946 return GPG_ERR_SYNTAX
;
1949 if (!valid_element_path (src
, 0))
1950 return GPG_ERR_INV_VALUE
;
1952 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
1955 * The first argument may be only a root element.
1957 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
1959 rc
= GPG_ERR_SYNTAX
;
1964 odst
= strv_dup (dst
);
1967 rc
= GPG_ERR_ENOMEM
;
1971 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
1973 * Make sure the destination element path exists.
1980 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
1981 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1986 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
1987 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1990 n
= create_element_path (client
, &src
, &rc
, NULL
);
1994 line
= strv_join ("\t", odst
);
1997 rc
= GPG_ERR_ENOMEM
;
2001 rc
= add_attribute (n
, "target", line
);
2012 * req[0] - attribute
2013 * req[1] - element path
2016 attribute_get (assuan_context_t ctx
, char **req
)
2018 struct client_s
*client
= assuan_get_pointer (ctx
);
2024 if (!req
|| !req
[0] || !req
[1])
2025 return GPG_ERR_SYNTAX
;
2027 if (strchr (req
[1], '\t'))
2029 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2030 return GPG_ERR_SYNTAX
;
2034 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2035 return GPG_ERR_SYNTAX
;
2038 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2045 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2046 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2054 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2055 return GPG_ERR_NOT_FOUND
;
2057 pthread_cleanup_push (xmlFree
, a
);
2060 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2062 rc
= GPG_ERR_NO_DATA
;
2064 pthread_cleanup_pop (1);
2073 * req[0] - attribute
2074 * req[1] - element path
2078 attribute_set (struct client_s
*client
, char **req
)
2084 if (!req
|| !req
[0] || !req
[1])
2085 return GPG_ERR_SYNTAX
;
2088 * Reserved attribute names.
2090 if (!strcmp (req
[0], "_name"))
2091 return GPG_ERR_INV_ATTR
;
2092 else if (!strcmp (req
[0], "target"))
2093 return target_attribute (client
, req
+ 1);
2094 else if (!valid_xml_attribute (req
[0]))
2095 return GPG_ERR_INV_VALUE
;
2097 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2100 * The first argument may be only a root element.
2102 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2103 return GPG_ERR_SYNTAX
;
2106 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2113 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2114 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2120 rc
= add_attribute (n
, req
[0], req
[2]);
2129 * req[1] - attribute name or element path if command is LIST
2130 * req[2] - element path
2131 * req[2] - element path or value
2135 do_attr (assuan_context_t ctx
, char *line
)
2137 struct client_s
*client
= assuan_get_pointer (ctx
);
2141 req
= str_split (line
, " ", 4);
2142 if (!req
|| !req
[0] || !req
[1])
2145 return GPG_ERR_SYNTAX
;
2148 pthread_cleanup_push (req_cleanup
, req
);
2150 if (strcasecmp (req
[0], "SET") == 0)
2151 rc
= attribute_set (client
, req
+ 1);
2152 else if (strcasecmp (req
[0], "GET") == 0)
2153 rc
= attribute_get (ctx
, req
+ 1);
2154 else if (strcasecmp (req
[0], "DELETE") == 0)
2155 rc
= attribute_delete (client
, req
+ 1);
2156 else if (strcasecmp (req
[0], "LIST") == 0)
2157 rc
= attribute_list (ctx
, req
+ 1);
2159 rc
= GPG_ERR_SYNTAX
;
2161 pthread_cleanup_pop (1);
2166 attr_command (assuan_context_t ctx
, char *line
)
2168 struct client_s
*client
= assuan_get_pointer (ctx
);
2170 struct argv_s
*args
[] = {
2171 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2175 rc
= parse_options (&line
, args
, client
);
2177 return send_error (ctx
, rc
);
2179 if (client
->opts
& OPT_INQUIRE
)
2181 unsigned char *result
;
2184 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2186 return send_error (ctx
, rc
);
2188 line
= (char *) result
;
2191 rc
= do_attr (ctx
, line
);
2193 if (client
->opts
& OPT_INQUIRE
)
2196 return send_error (ctx
, rc
);
2200 parse_iscached_opt_lock (void *data
, void *value
)
2202 struct client_s
*client
= data
;
2205 client
->opts
|= OPT_LOCK
;
2210 iscached_command (assuan_context_t ctx
, char *line
)
2212 struct client_s
*client
= assuan_get_pointer (ctx
);
2214 struct argv_s
*args
[] = {
2215 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2219 if (!line
|| !*line
)
2220 return send_error (ctx
, GPG_ERR_SYNTAX
);
2222 rc
= parse_options (&line
, args
, client
);
2224 return send_error (ctx
, rc
);
2225 else if (!valid_filename (line
))
2226 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2228 rc
= cache_iscached (line
, NULL
);
2229 if (client
->opts
& OPT_LOCK
2230 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2232 unsigned char md5file
[16];
2233 gpg_error_t trc
= rc
;
2235 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2236 if (memcmp (md5file
, client
->md5file
, 16))
2237 cleanup_client (client
);
2239 memcpy (client
->md5file
, md5file
, 16);
2240 rc
= do_lock (client
, 1);
2245 return send_error (ctx
, rc
);
2249 clearcache_command (assuan_context_t ctx
, char *line
)
2251 gpg_error_t rc
= 0, all_rc
= 0;
2252 unsigned char md5file
[16];
2256 struct client_thread_s
*once
= NULL
;
2259 MUTEX_LOCK (&cn_mutex
);
2260 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2262 if (!line
|| !*line
)
2265 t
= slist_length (cn_thread_list
);
2267 for (i
= 0; i
< t
; i
++)
2269 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2270 assuan_peercred_t peer
;
2275 /* Lock each connected clients' file mutex to prevent any other client
2276 * from accessing the cache entry (the file mutex is locked upon
2277 * command startup). The cache for the entry is not cleared if the
2278 * file mutex is locked by another client to prevent this function
2283 if (thd
->cl
->filename
)
2285 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2286 all_rc
= !all_rc
? rc
: all_rc
;
2294 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2295 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2297 if (pthread_equal (pthread_self (), thd
->tid
))
2301 if (!thd
->cl
->filename
||
2302 cache_iscached (thd
->cl
->filename
,
2303 NULL
) == GPG_ERR_NO_DATA
)
2309 cache_defer_clear (thd
->cl
->md5file
);
2312 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2320 rc
= cache_clear (thd
->cl
->md5file
);
2321 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2329 /* A single data filename was specified. Lock only this data file
2330 * mutex and free the cache entry. */
2333 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2334 rc
= do_validate_peer (ctx
, line
, &peer
);
2336 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2338 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2340 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2342 if (pthread_equal (pthread_self (), thd
->tid
))
2349 rc
= cache_clear (thd
->cl
->md5file
);
2350 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2354 cache_defer_clear (thd
->cl
->md5file
);
2362 /* Only connected clients' cache entries have been cleared. Now clear any
2363 * remaining cache entries without clients but only if there wasn't an
2364 * error from above since this would defeat the locking check of the
2365 * remaining entries. */
2371 /* No clients are using the specified file. */
2372 else if (!all_rc
&& !rc
&& !once
)
2374 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2375 rc
= cache_clear (md5file
);
2378 /* Release the connection mutex. */
2379 pthread_cleanup_pop (1);
2383 send_status_all (STATUS_CACHE
, NULL
);
2385 /* One or more files were locked while clearing all cache entries. */
2389 return send_error (ctx
, rc
);
2393 cachetimeout_command (assuan_context_t ctx
, char *line
)
2396 char **req
= str_split (line
, " ", 0);
2399 assuan_peercred_t peer
;
2401 if (!req
|| !*req
|| !req
[1])
2404 return send_error (ctx
, GPG_ERR_SYNTAX
);
2408 timeout
= (int) strtol (req
[1], &p
, 10);
2409 if (errno
!= 0 || *p
|| timeout
< -1)
2412 return send_error (ctx
, GPG_ERR_SYNTAX
);
2415 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2418 unsigned char md5file
[16];
2420 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2421 rc
= cache_set_timeout (md5file
, timeout
);
2422 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2425 MUTEX_LOCK (&rcfile_mutex
);
2426 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2428 MUTEX_UNLOCK (&rcfile_mutex
);
2433 return send_error (ctx
, rc
);
2437 dump_command (assuan_context_t ctx
, char *line
)
2441 struct client_s
*client
= assuan_get_pointer (ctx
);
2444 if (disable_list_and_dump
== 1)
2445 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2447 rc
= peer_is_self(client
);
2449 return send_error (ctx
, rc
);
2451 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2455 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2456 pwmd_strerror (GPG_ERR_ENOMEM
));
2457 return send_error (ctx
, GPG_ERR_ENOMEM
);
2460 pthread_cleanup_push (xmlFree
, xml
);
2461 rc
= xfer_data (ctx
, (char *) xml
, len
);
2462 pthread_cleanup_pop (1);
2463 return send_error (ctx
, rc
);
2467 getconfig_command (assuan_context_t ctx
, char *line
)
2469 struct client_s
*client
= assuan_get_pointer (ctx
);
2471 char filename
[255] = { 0 }, param
[747] = { 0 };
2472 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2474 if (!line
|| !*line
)
2475 return send_error (ctx
, GPG_ERR_SYNTAX
);
2477 if (strchr (line
, ' '))
2479 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2484 if (fp
&& !valid_filename (fp
))
2485 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2487 paramp
= str_down (paramp
);
2488 if (!strcmp (paramp
, "cipher") && fp
)
2490 struct crypto_s
*crypto
= NULL
;
2492 rc
= init_client_crypto (&crypto
);
2495 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2499 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2509 UPDATE_AGENT_CTX (client
, crypto
);
2510 cleanup_crypto (&crypto
);
2511 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2512 return send_error (ctx
, rc
);
2517 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2519 struct crypto_s
*crypto
= NULL
;
2521 rc
= init_client_crypto (&crypto
);
2524 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2527 tmp
= str_asprintf ("%llu",
2528 (unsigned long long) crypto
->hdr
.
2531 rc
= GPG_ERR_ENOMEM
;
2535 UPDATE_AGENT_CTX (client
, crypto
);
2536 cleanup_crypto (&crypto
);
2537 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2538 return send_error (ctx
, rc
);
2543 else if (!strcmp (paramp
, "passphrase"))
2544 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2546 p
= config_get_value (fp
? fp
: "global", paramp
);
2548 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2550 tmp
= expand_homedir (p
);
2554 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2555 pwmd_strerror (GPG_ERR_ENOMEM
));
2556 return send_error (ctx
, GPG_ERR_ENOMEM
);
2561 pthread_cleanup_push (xfree
, p
);
2562 rc
= xfer_data (ctx
, p
, strlen (p
));
2563 pthread_cleanup_pop (1);
2564 return send_error (ctx
, rc
);
2569 xmlXPathContextPtr xp
;
2570 xmlXPathObjectPtr result
;
2576 xpath_command_cleanup (void *arg
)
2578 struct xpath_s
*xpath
= arg
;
2583 req_cleanup (xpath
->req
);
2586 xmlBufferFree (xpath
->buf
);
2589 xmlXPathFreeObject (xpath
->result
);
2592 xmlXPathFreeContext (xpath
->xp
);
2596 do_xpath (assuan_context_t ctx
, char *line
)
2599 struct client_s
*client
= assuan_get_pointer (ctx
);
2600 struct xpath_s _x
= { 0 };
2601 struct xpath_s
*xpath
= &_x
;
2603 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2605 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2606 return GPG_ERR_ENOMEM
;
2609 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2612 rc
= GPG_ERR_BAD_DATA
;
2617 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2620 rc
= GPG_ERR_BAD_DATA
;
2624 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2626 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2630 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2631 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2635 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2637 rc
= GPG_ERR_NO_DATA
;
2640 else if (xpath
->req
[1])
2646 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2647 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2648 xmlBufferLength (xpath
->buf
));
2649 pthread_cleanup_pop (0);
2651 xpath_command_cleanup (xpath
);
2656 xpath_command (assuan_context_t ctx
, char *line
)
2658 struct client_s
*client
= assuan_get_pointer (ctx
);
2660 struct argv_s
*args
[] = {
2661 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2665 if (disable_list_and_dump
== 1)
2666 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2668 rc
= peer_is_self(client
);
2670 return send_error (ctx
, rc
);
2672 rc
= parse_options (&line
, args
, client
);
2674 return send_error (ctx
, rc
);
2676 if (client
->opts
& OPT_INQUIRE
)
2678 unsigned char *result
;
2681 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2683 return send_error (ctx
, rc
);
2685 line
= (char *) result
;
2688 if (!line
|| !*line
)
2689 rc
= GPG_ERR_SYNTAX
;
2692 rc
= do_xpath (ctx
, line
);
2694 if (client
->opts
& OPT_INQUIRE
)
2697 return send_error (ctx
, rc
);
2701 do_xpathattr (assuan_context_t ctx
, char *line
)
2703 struct client_s
*client
= assuan_get_pointer (ctx
);
2707 struct xpath_s _x
= { 0 };
2708 struct xpath_s
*xpath
= &_x
;
2710 if ((req
= str_split (line
, " ", 3)) == NULL
)
2711 return GPG_ERR_ENOMEM
;
2715 rc
= GPG_ERR_SYNTAX
;
2719 if (!strcasecmp (req
[0], "SET"))
2721 else if (!strcasecmp (req
[0], "DELETE"))
2725 rc
= GPG_ERR_SYNTAX
;
2729 if (!req
[1] || !req
[2])
2731 rc
= GPG_ERR_SYNTAX
;
2735 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2737 rc
= GPG_ERR_ENOMEM
;
2741 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2743 rc
= GPG_ERR_SYNTAX
;
2747 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2750 rc
= GPG_ERR_BAD_DATA
;
2754 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2757 rc
= GPG_ERR_BAD_DATA
;
2761 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2763 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2767 rc
= recurse_xpath_nodeset (client
->doc
, xpath
->result
->nodesetval
,
2768 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2769 (xmlChar
*) req
[1]);
2772 xpath_command_cleanup (xpath
);
2777 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2779 xpathattr_command (assuan_context_t ctx
, char *line
)
2781 struct client_s
*client
= assuan_get_pointer (ctx
);
2783 struct argv_s
*args
[] = {
2784 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2788 if (disable_list_and_dump
== 1)
2789 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2791 rc
= peer_is_self(client
);
2793 return send_error (ctx
, rc
);
2795 rc
= parse_options (&line
, args
, client
);
2797 return send_error (ctx
, rc
);
2799 if (client
->opts
& OPT_INQUIRE
)
2801 unsigned char *result
;
2804 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2806 return send_error (ctx
, rc
);
2808 line
= (char *) result
;
2811 if (!line
|| !*line
)
2812 rc
= GPG_ERR_SYNTAX
;
2815 rc
= do_xpathattr (ctx
, line
);
2817 if (client
->opts
& OPT_INQUIRE
)
2820 return send_error (ctx
, rc
);
2824 do_import (struct client_s
*client
, const char *root_element
,
2825 unsigned char *content
)
2827 char **dst_path
= NULL
;
2828 xmlDocPtr doc
= NULL
;
2829 xmlNodePtr n
, root
, copy
;
2832 if (!content
|| !*content
)
2835 return GPG_ERR_SYNTAX
;
2839 dst_path
= str_split (root_element
, "\t", 0);
2841 if (dst_path
&& !valid_element_path (dst_path
, 0))
2844 strv_free (dst_path
);
2846 return GPG_ERR_INV_VALUE
;
2849 struct string_s
*str
= string_new_content ((char *)content
);
2850 str
= string_prepend (str
, "<pwmd>");
2851 str
= string_append (str
, "</pwmd>");
2852 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2853 string_free (str
, 1);
2856 rc
= GPG_ERR_BAD_DATA
;
2860 root
= xmlDocGetRootElement (doc
);
2861 xmlNodePtr root_orig
= root
->children
;
2862 root
= root
->children
;
2863 rc
= validate_import (root
);
2872 char **path
= strv_dup (dst_path
);
2875 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2876 pwmd_strerror (GPG_ERR_ENOMEM
));
2877 rc
= GPG_ERR_ENOMEM
;
2881 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2885 rc
= GPG_ERR_INV_VALUE
;
2889 if (strv_printf (&path
, "%s", (char *) a
) == 0)
2892 rc
= GPG_ERR_ENOMEM
;
2897 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2898 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2906 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2907 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
2908 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2923 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
2925 n
= create_element_path (client
, &path
, &rc
, NULL
);
2932 copy
= xmlCopyNodeList (root
->children
);
2933 n
= xmlAddChildList (n
, copy
);
2935 rc
= GPG_ERR_ENOMEM
;
2944 /* Check if the content root element can create a DTD root element. */
2945 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
2947 rc
= GPG_ERR_SYNTAX
;
2953 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
2955 rc
= GPG_ERR_SYNTAX
;
2959 char *tmp
= str_dup ((char *) a
);
2961 int literal
= is_literal_element (&tmp
);
2963 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
2966 rc
= GPG_ERR_INV_VALUE
;
2970 if (strv_printf (&path
, "%s", tmp
) == 0)
2973 rc
= GPG_ERR_ENOMEM
;
2978 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
2979 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2981 rc
= GPG_ERR_BAD_DATA
;
2985 /* Overwriting the existing tree. */
2989 xmlFreeNodeList (n
);
2993 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
2994 n
= xmlCopyNode (root
, 1);
2995 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3000 rc
= update_element_mtime (n
->parent
);
3002 for (root
= root_orig
->next
; root
; root
= root
->next
)
3004 if (root
->type
== XML_ELEMENT_NODE
)
3017 strv_free (dst_path
);
3023 parse_import_opt_root (void *data
, void *value
)
3025 struct client_s
*client
= data
;
3027 client
->import_root
= str_dup (value
);
3028 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3032 import_command (assuan_context_t ctx
, char *line
)
3035 struct client_s
*client
= assuan_get_pointer (ctx
);
3036 unsigned char *result
;
3038 struct argv_s
*args
[] = {
3039 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3043 xfree (client
->import_root
);
3044 client
->import_root
= NULL
;
3045 rc
= parse_options (&line
, args
, client
);
3047 return send_error (ctx
, rc
);
3049 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3052 xfree (client
->import_root
);
3053 client
->import_root
= NULL
;
3054 return send_error (ctx
, rc
);
3057 rc
= do_import (client
, client
->import_root
, result
);
3058 xfree (client
->import_root
);
3059 client
->import_root
= NULL
;
3060 return send_error (ctx
, rc
);
3064 do_lock (struct client_s
*client
, int add
)
3066 gpg_error_t rc
= lock_file_mutex (client
, add
);
3069 client
->flags
|= FLAG_LOCK_CMD
;
3075 lock_command (assuan_context_t ctx
, char *line
)
3077 struct client_s
*client
= assuan_get_pointer (ctx
);
3078 gpg_error_t rc
= do_lock (client
, 0);
3080 return send_error (ctx
, rc
);
3084 unlock_command (assuan_context_t ctx
, char *line
)
3086 struct client_s
*client
= assuan_get_pointer (ctx
);
3089 rc
= unlock_file_mutex (client
, 0);
3090 return send_error (ctx
, rc
);
3094 option_command (assuan_context_t ctx
, char *line
)
3096 struct client_s
*client
= assuan_get_pointer (ctx
);
3098 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3100 struct agent_s
*agent
= client
->crypto
->agent
;
3102 char namebuf
[255] = { 0 };
3103 char *name
= namebuf
;
3104 char *value
= NULL
, *p
;
3106 p
= strchr (line
, '=');
3108 strncpy (namebuf
, line
, sizeof(namebuf
));
3111 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3115 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3117 if (strcasecmp (name
, (char *) "log_level") == 0)
3123 l
= strtol (value
, NULL
, 10);
3126 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3129 MUTEX_LOCK (&rcfile_mutex
);
3130 config_set_int_param (&global_config
, "global", "log_level", value
);
3131 MUTEX_UNLOCK (&rcfile_mutex
);
3134 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3141 n
= strtol (value
, &p
, 10);
3143 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3146 client
->lock_timeout
= n
;
3149 else if (strcasecmp (name
, (char *) "NAME") == 0)
3151 char *tmp
= pthread_getspecific (thread_name_key
);
3156 if (!value
|| !*value
)
3158 pthread_setspecific (thread_name_key
, str_dup (""));
3162 pthread_setspecific (thread_name_key
, str_dup (value
));
3165 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3167 xfree (pin_opts
->lc_messages
);
3168 pin_opts
->lc_messages
= NULL
;
3169 if (value
&& *value
)
3170 pin_opts
->lc_messages
= str_dup (value
);
3173 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3176 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3178 xfree (pin_opts
->lc_ctype
);
3179 pin_opts
->lc_ctype
= NULL
;
3180 if (value
&& *value
)
3181 pin_opts
->lc_ctype
= str_dup (value
);
3184 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3187 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3189 xfree (pin_opts
->ttyname
);
3190 pin_opts
->ttyname
= NULL
;
3191 if (value
&& *value
)
3192 pin_opts
->ttyname
= str_dup (value
);
3195 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3198 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3200 xfree (pin_opts
->ttytype
);
3201 pin_opts
->ttytype
= NULL
;
3202 if (value
&& *value
)
3203 pin_opts
->ttytype
= str_dup (value
);
3206 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3209 else if (strcasecmp (name
, (char *) "display") == 0)
3211 xfree (pin_opts
->display
);
3212 pin_opts
->display
= NULL
;
3213 if (value
&& *value
)
3214 pin_opts
->display
= str_dup (value
);
3217 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3220 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3222 xfree (pin_opts
->desc
);
3223 pin_opts
->desc
= NULL
;
3224 if (value
&& *value
)
3225 pin_opts
->desc
= str_dup (value
);
3227 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3229 xfree (pin_opts
->title
);
3230 pin_opts
->title
= NULL
;
3231 if (value
&& *value
)
3232 pin_opts
->title
= str_dup (value
);
3234 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3236 xfree (pin_opts
->prompt
);
3237 pin_opts
->prompt
= NULL
;
3238 if (value
&& *value
)
3239 pin_opts
->prompt
= str_dup (value
);
3242 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3250 n
= (int) strtol (value
, &p
, 10);
3253 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3255 pin_opts
->timeout
= n
;
3256 MUTEX_LOCK (&rcfile_mutex
);
3257 config_set_int_param (&global_config
,
3258 client
->filename
? client
->filename
: "global",
3259 "pinentry_timeout", value
);
3260 MUTEX_UNLOCK (&rcfile_mutex
);
3263 else if (strcasecmp (name
, "disable-pinentry") == 0)
3268 if (value
&& *value
)
3270 n
= (int) strtol (value
, &p
, 10);
3271 if (*p
|| n
< 0 || n
> 1)
3272 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3276 client
->flags
|= FLAG_NO_PINENTRY
;
3278 client
->flags
&= ~FLAG_NO_PINENTRY
;
3283 if (client
->flags
& FLAG_NO_PINENTRY
)
3284 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3287 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3291 return send_error (ctx
, rc
);
3296 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3300 if (!rc
&& use_agent
&& agent
)
3302 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3307 return send_error (ctx
, rc
);
3311 do_rename (assuan_context_t ctx
, char *line
)
3313 struct client_s
*client
= assuan_get_pointer (ctx
);
3315 char **req
, **src
, *dst
;
3318 req
= str_split (line
, " ", 0);
3320 if (!req
|| !req
[0] || !req
[1])
3323 return GPG_ERR_SYNTAX
;
3327 is_literal_element (&dst
);
3329 if (!valid_xml_element ((xmlChar
*) dst
))
3332 return GPG_ERR_INV_VALUE
;
3335 if (strchr (req
[0], '\t'))
3336 src
= str_split (req
[0], "\t", 0);
3338 src
= str_split (req
[0], " ", 0);
3342 rc
= GPG_ERR_SYNTAX
;
3346 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3348 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3349 NULL
, 0, 0, NULL
, 0);
3354 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3357 rc
= GPG_ERR_ENOMEM
;
3361 /* To prevent unwanted effects:
3363 * <root name="a"><b/></root>
3367 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3370 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3380 for (p
= src
; *p
; p
++)
3384 strv_printf (&tmp
, "%s", *p
);
3388 strv_printf (&tmp
, "!%s", dst
);
3389 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3390 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3397 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3398 NULL
, NULL
, 0, 0, NULL
, 0);
3401 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3406 /* Target may exist:
3409 * <root name="b" target="a"/>
3418 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3425 xmlFreeNodeList (ndst
);
3428 rc
= add_attribute (n
, "_name", dst
);
3437 rename_command (assuan_context_t ctx
, char *line
)
3439 struct client_s
*client
= assuan_get_pointer (ctx
);
3441 struct argv_s
*args
[] = {
3442 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3446 rc
= parse_options (&line
, args
, client
);
3448 return send_error (ctx
, rc
);
3450 if (client
->opts
& OPT_INQUIRE
)
3452 unsigned char *result
;
3455 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3457 return send_error (ctx
, rc
);
3459 line
= (char *) result
;
3462 rc
= do_rename (ctx
, line
);
3464 if (client
->opts
& OPT_INQUIRE
)
3467 return send_error (ctx
, rc
);
3471 do_copy (assuan_context_t ctx
, char *line
)
3473 struct client_s
*client
= assuan_get_pointer (ctx
);
3475 char **req
, **src
= NULL
, **dst
= NULL
;
3476 xmlNodePtr nsrc
, ndst
, new = NULL
;
3478 req
= str_split (line
, " ", 0);
3479 if (!req
|| !req
[0] || !req
[1])
3482 return GPG_ERR_SYNTAX
;
3485 if (strchr (req
[0], '\t'))
3486 src
= str_split (req
[0], "\t", 0);
3488 src
= str_split (req
[0], " ", 0);
3492 rc
= GPG_ERR_SYNTAX
;
3496 if (strchr (req
[1], '\t'))
3497 dst
= str_split (req
[1], "\t", 0);
3499 dst
= str_split (req
[1], " ", 0);
3503 rc
= GPG_ERR_SYNTAX
;
3507 if (!valid_element_path (dst
, 0))
3509 rc
= GPG_ERR_INV_VALUE
;
3513 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3515 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3516 NULL
, NULL
, 0, 0, NULL
, 0);
3521 new = xmlCopyNodeList (nsrc
);
3524 rc
= GPG_ERR_ENOMEM
;
3529 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3533 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3534 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3541 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3543 else if (!ndst
|| create
)
3545 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3550 /* Merge any attributes from the src node to the initial dst node. */
3551 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3553 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3556 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3560 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3561 xmlNewProp (ndst
, attr
->name
, tmp
);
3563 rc
= add_attribute (ndst
, NULL
, NULL
);
3566 xmlNodePtr n
= ndst
->children
;
3568 xmlFreeNodeList (n
);
3569 ndst
->children
= NULL
;
3573 n
= xmlCopyNodeList (new->children
);
3576 rc
= GPG_ERR_ENOMEM
;
3580 n
= xmlAddChildList (ndst
, n
);
3583 rc
= GPG_ERR_ENOMEM
;
3587 rc
= update_element_mtime (xmlDocGetRootElement (client
->doc
) ==
3588 ndst
->parent
? ndst
: ndst
->parent
);
3594 xmlUnlinkNode (new);
3595 xmlFreeNodeList (new);
3611 copy_command (assuan_context_t ctx
, char *line
)
3613 struct client_s
*client
= assuan_get_pointer (ctx
);
3615 struct argv_s
*args
[] = {
3616 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3620 rc
= parse_options (&line
, args
, client
);
3622 return send_error (ctx
, rc
);
3624 if (client
->opts
& OPT_INQUIRE
)
3626 unsigned char *result
;
3629 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3631 return send_error (ctx
, rc
);
3633 line
= (char *) result
;
3636 rc
= do_copy (ctx
, line
);
3638 if (client
->opts
& OPT_INQUIRE
)
3641 return send_error (ctx
, rc
);
3645 do_move (assuan_context_t ctx
, char *line
)
3647 struct client_s
*client
= assuan_get_pointer (ctx
);
3649 char **req
, **src
= NULL
, **dst
= NULL
;
3650 xmlNodePtr nsrc
, ndst
= NULL
;
3652 req
= str_split (line
, " ", 0);
3654 if (!req
|| !req
[0] || !req
[1])
3657 return GPG_ERR_SYNTAX
;
3660 if (strchr (req
[0], '\t'))
3661 src
= str_split (req
[0], "\t", 0);
3663 src
= str_split (req
[0], " ", 0);
3667 rc
= GPG_ERR_SYNTAX
;
3671 if (strchr (req
[1], '\t'))
3672 dst
= str_split (req
[1], "\t", 0);
3674 dst
= str_split (req
[1], " ", 0);
3676 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3678 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3679 NULL
, NULL
, 0, 0, NULL
, 0);
3686 if (!valid_element_path (dst
, 0))
3688 rc
= GPG_ERR_INV_VALUE
;
3692 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3694 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3695 NULL
, NULL
, 0, 0, NULL
, 0);
3698 ndst
= xmlDocGetRootElement (client
->doc
);
3700 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3704 rc
= GPG_ERR_CONFLICT
;
3709 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3716 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3718 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
3730 if (ndst
== xmlDocGetRootElement (client
->doc
))
3732 xmlNodePtr n
= nsrc
;
3735 while (n
->parent
&& n
->parent
!= ndst
)
3738 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3739 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3741 if (xmlStrEqual (a
, b
))
3744 xmlUnlinkNode (nsrc
);
3746 xmlFreeNodeList (n
);
3754 xmlUnlinkNode (dup
);
3755 xmlFreeNodeList (dup
);
3759 xmlUnlinkNode (dup
);
3765 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3767 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3768 && !strcmp ((char *) name
, *dst
))
3771 rc
= GPG_ERR_CONFLICT
;
3776 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3782 update_element_mtime (nsrc
->parent
);
3783 xmlUnlinkNode (nsrc
);
3784 ndst
= xmlAddChildList (ndst
, nsrc
);
3787 rc
= GPG_ERR_ENOMEM
;
3789 update_element_mtime (ndst
->parent
);
3805 move_command (assuan_context_t ctx
, char *line
)
3807 struct client_s
*client
= assuan_get_pointer (ctx
);
3809 struct argv_s
*args
[] = {
3810 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3814 rc
= parse_options (&line
, args
, client
);
3816 return send_error (ctx
, rc
);
3818 if (client
->opts
& OPT_INQUIRE
)
3820 unsigned char *result
;
3823 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3825 return send_error (ctx
, rc
);
3827 line
= (char *) result
;
3830 rc
= do_move (ctx
, line
);
3832 if (client
->opts
& OPT_INQUIRE
)
3835 return send_error (ctx
, rc
);
3839 ls_command (assuan_context_t ctx
, char *line
)
3842 char *tmp
= str_asprintf ("%s/data", homedir
);
3843 char *dir
= expand_homedir (tmp
);
3844 DIR *d
= opendir (dir
);
3846 rc
= gpg_error_from_errno (errno
);
3852 return send_error (ctx
, rc
);
3856 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3857 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3863 while (!readdir_r (d
, p
, &cur
) && cur
)
3865 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3867 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
3868 && cur
->d_name
[2] == '\0')
3871 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3878 rc
= GPG_ERR_ENOMEM
;
3890 return send_error (ctx
, rc
);
3893 return send_error (ctx
, GPG_ERR_NO_DATA
);
3895 list
[strlen (list
) - 1] = 0;
3896 rc
= xfer_data (ctx
, list
, strlen (list
));
3898 return send_error (ctx
, rc
);
3902 bye_notify (assuan_context_t ctx
, char *line
)
3904 struct client_s
*cl
= assuan_get_pointer (ctx
);
3907 if (cl
->thd
->remote
)
3913 struct timeval tv
= { 0, 50000 };
3915 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3916 if (rc
== GNUTLS_E_AGAIN
)
3917 select (0, NULL
, NULL
, NULL
, &tv
);
3919 while (rc
== GNUTLS_E_AGAIN
);
3923 /* This will let assuan_process_next() return. */
3924 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
3925 cl
->last_rc
= 0; // BYE command result
3930 reset_notify (assuan_context_t ctx
, char *line
)
3932 struct client_s
*client
= assuan_get_pointer (ctx
);
3935 cleanup_client (client
);
3941 * This is called before every Assuan command.
3944 command_startup (assuan_context_t ctx
, const char *name
)
3946 struct client_s
*client
= assuan_get_pointer (ctx
);
3948 struct command_table_s
*cmd
= NULL
;
3950 log_write1 ("command='%s'", name
);
3951 client
->last_rc
= client
->opts
= 0;
3953 for (int i
= 0; command_table
[i
]; i
++)
3955 if (!strcasecmp (name
, command_table
[i
]->name
))
3957 if (command_table
[i
]->ignore_startup
)
3959 cmd
= command_table
[i
];
3964 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3969 * This is called after every Assuan command.
3972 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3974 struct client_s
*client
= assuan_get_pointer (ctx
);
3976 if (!(client
->flags
& FLAG_LOCK_CMD
))
3977 unlock_file_mutex (client
, 0);
3979 log_write1 (_("command completed: rc=%u"), client
->last_rc
);
3980 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3982 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3987 help_command (assuan_context_t ctx
, char *line
)
3992 if (!line
|| !*line
)
3995 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
3996 "For commands that take an element path as an argument, each element is "
3997 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4000 for (i
= 0; command_table
[i
]; i
++)
4002 if (!command_table
[i
]->help
)
4005 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4010 tmp
= strip_texi_and_wrap (help
);
4012 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4014 return send_error (ctx
, rc
);
4017 for (i
= 0; command_table
[i
]; i
++)
4019 if (!strcasecmp (line
, command_table
[i
]->name
))
4023 if (!command_table
[i
]->help
)
4026 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4027 tmp
= str_asprintf (_("Usage: %s"), help
);
4029 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4031 return send_error (ctx
, rc
);
4035 return send_error (ctx
, GPG_ERR_INV_NAME
);
4039 new_command (const char *name
, int ignore
, int unlock
,
4040 gpg_error_t (*handler
) (assuan_context_t
, char *),
4046 for (i
= 0; command_table
[i
]; i
++);
4049 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4050 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4051 command_table
[i
]->name
= name
;
4052 command_table
[i
]->handler
= handler
;
4053 command_table
[i
]->ignore_startup
= ignore
;
4054 command_table
[i
]->unlock
= unlock
;
4055 command_table
[i
++]->help
= help
;
4056 command_table
[i
] = NULL
;
4064 for (i
= 0; command_table
[i
]; i
++)
4065 xfree (command_table
[i
]);
4067 xfree (command_table
);
4071 sort_commands (const void *arg1
, const void *arg2
)
4073 struct command_table_s
*const *a
= arg1
;
4074 struct command_table_s
*const *b
= arg2
;
4083 return strcmp ((*a
)->name
, (*b
)->name
);
4087 passwd_command (assuan_context_t ctx
, char *line
)
4089 struct client_s
*client
= assuan_get_pointer (ctx
);
4091 struct argv_s
*args
[] = {
4092 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4093 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4094 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4098 if (client
->flags
& FLAG_NEW
)
4099 return send_error (ctx
, GPG_ERR_INV_STATE
);
4101 client
->crypto
->save
.s2k_count
=
4102 config_get_ulong (client
->filename
, "s2k_count");
4103 rc
= parse_options (&line
, args
, client
);
4105 return send_error (ctx
, rc
);
4107 if (!rc
&& client
->opts
& OPT_RESET
)
4109 rc
= cache_clear (client
->md5file
);
4111 send_status_all (STATUS_CACHE
, NULL
);
4116 if (!IS_PKI (client
->crypto
))
4118 struct crypto_s
*crypto
;
4120 xfree (client
->crypto
->filename
);
4121 client
->crypto
->filename
= str_dup (client
->filename
);
4122 rc
= change_passwd (ctx
, client
->filename
,
4123 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4124 (client
->opts
& OPT_NO_PASSPHRASE
));
4127 cleanup_crypto (&client
->crypto
);
4128 client
->crypto
= crypto
;
4129 update_checksum (client
);
4130 cleanup_crypto_stage1 (client
->crypto
);
4136 if (client
->crypto
->save
.s2k_count
)
4137 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4138 "OPTION s2k-count=%lu",
4139 client
->crypto
->save
.s2k_count
);
4142 rc
= agent_passwd (client
->crypto
);
4147 return send_error (ctx
, rc
);
4151 parse_keygrip_opt_sign (void *data
, void *value
)
4153 struct client_s
*client
= data
;
4156 client
->opts
|= OPT_SIGN
;
4161 keygrip_command (assuan_context_t ctx
, char *line
)
4163 struct client_s
*client
= assuan_get_pointer (ctx
);
4165 struct crypto_s
*crypto
= NULL
;
4166 struct argv_s
*args
[] = {
4167 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4171 if (!line
|| !*line
)
4172 return send_error (ctx
, GPG_ERR_SYNTAX
);
4174 rc
= parse_options (&line
, args
, client
);
4176 return send_error (ctx
, rc
);
4178 if (!valid_filename (line
))
4179 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4181 rc
= init_client_crypto (&crypto
);
4183 return send_error (ctx
, rc
);
4185 rc
= read_data_file (line
, crypto
);
4188 char *hexgrip
= NULL
;
4190 if (!IS_PKI (crypto
))
4192 cleanup_crypto (&crypto
);
4193 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4196 if (client
->opts
& OPT_SIGN
)
4198 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4199 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4203 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4206 rc
= GPG_ERR_ENOMEM
;
4208 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4213 UPDATE_AGENT_CTX (client
, crypto
);
4214 cleanup_crypto (&crypto
);
4215 return send_error (ctx
, rc
);
4219 parse_opt_data (void *data
, void *value
)
4221 struct client_s
*client
= data
;
4224 client
->opts
|= OPT_DATA
;
4229 getinfo_command (assuan_context_t ctx
, char *line
)
4231 struct client_s
*client
= assuan_get_pointer (ctx
);
4233 char buf
[ASSUAN_LINELENGTH
];
4234 struct argv_s
*args
[] = {
4235 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4239 rc
= parse_options (&line
, args
, client
);
4241 return send_error (ctx
, rc
);
4243 if (!strcasecmp (line
, "clients"))
4245 if (client
->opts
& OPT_DATA
)
4247 MUTEX_LOCK (&cn_mutex
);
4248 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4249 MUTEX_UNLOCK (&cn_mutex
);
4250 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4253 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4255 else if (!strcasecmp (line
, "cache"))
4257 if (client
->opts
& OPT_DATA
)
4259 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4260 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4263 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4265 else if (!strcasecmp (line
, "pid"))
4268 pid_t pid
= getpid ();
4270 snprintf (buf
, sizeof (buf
), "%i", pid
);
4271 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4273 else if (!strcasecmp (line
, "version"))
4275 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4282 "", use_agent
? "AGENT" : "");
4283 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4286 else if (!strcasecmp (line
, "last_error"))
4288 if (client
->last_error
)
4289 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4291 rc
= GPG_ERR_NO_DATA
;
4294 rc
= gpg_error (GPG_ERR_SYNTAX
);
4296 return send_error (ctx
, rc
);
4301 send_data_cb (void *user
, const void *buf
, size_t len
)
4303 assuan_context_t ctx
= user
;
4305 return assuan_send_data (ctx
, buf
, len
);
4309 send_status_cb (void *user
, const char *line
)
4311 assuan_context_t ctx
= user
;
4312 char keyword
[200], *k
;
4315 for (p
= line
, k
= keyword
; *p
; p
++)
4327 while (isspace (*p
))
4330 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4335 agent_command (assuan_context_t ctx
, char *line
)
4340 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4343 struct client_s
*client
= assuan_get_pointer (ctx
);
4345 if (!line
|| !*line
)
4346 return send_error (ctx
, GPG_ERR_SYNTAX
);
4348 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4349 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4350 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4351 send_status_cb
, client
->ctx
);
4352 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4357 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4360 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4362 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4366 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4368 return send_error (ctx
, rc
);
4374 /* !BEGIN-HELP-TEXT!
4376 * This comment is used as a marker to generate the offline documentation
4377 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4378 * script to determine where commands begin and end.
4380 new_command("HELP", 1, 1, help_command
, _(
4381 "HELP [<COMMAND>]\n"
4382 "Show available commands or command specific help text."
4385 new_command("AGENT", 1, 1, agent_command
, _(
4387 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4388 "@command{gpg-agent}."
4391 new_command("GETINFO", 1, 1, getinfo_command
, _(
4392 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4393 "Get server and other information: @var{cache} returns the number of cached "
4394 "documents via a status message. @var{clients} returns the number of "
4395 "connected clients via a status message. @var{pid} returns the process ID "
4396 "number of the server via a data response. @var{VERSION} returns the server "
4397 "version number and compile-time features with a data response with each "
4398 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4399 "the last failed command when available. @xref{Status Messages}. "
4401 "When the @option{--data} option is specified then the result will be sent "
4402 "via a data response rather than a status message."
4405 new_command("PASSWD", 0, 0, passwd_command
, _(
4406 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4407 "Changes the passphrase of the secret key required to open the current "
4408 "file or the passphrase of a symmetrically encrypted data file. When the "
4409 "@option{--reset} option is passed then the cache entry for the current "
4410 "file will be reset and the passphrase, if any, will be required during the "
4411 "next @code{OPEN} (@pxref{OPEN})."
4413 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4414 "of hash iterations for a passphrase and must be either @code{0} to use "
4415 "the calibrated count of the machine (the default), or a value greater than "
4416 "or equal to @code{65536}. This option has no effect for symmetrically "
4417 "encrypted data files."
4419 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4420 "the data file, although a passphrase may be required when changing it."
4423 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4424 "KEYGRIP [--sign] <filename>\n"
4425 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4428 "When the @option{--sign} option is specified then the key used for signing "
4429 "of the specified @var{filename} will be returned."
4431 "For symmetrically encrypted data files this command returns the error "
4432 "GPG_ERR_NOT_SUPPORTED."
4435 new_command("OPEN", 1, 1, open_command
, _(
4436 "OPEN [--lock] <filename> [<passphrase>]\n"
4437 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4438 "found on the file-system then a new document will be created. If the file "
4439 "is found, it is looked for in the file cache. If cached and no "
4440 "@var{passphrase} was specified then the cached document is opened. When not "
4441 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4442 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4445 "When the @option{--lock} option is passed then the file mutex will be "
4446 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4447 "file has been opened."
4450 new_command("SAVE", 0, 0, save_command
, _(
4451 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4452 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4453 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4454 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4455 "keypair will be generated and a pinentry will be used to prompt for the "
4456 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4457 "passed in which case the data file will not be passphrase protected. "
4459 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4460 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4461 "use is enabled. The datafile will be symmetrically encrypted and will not "
4462 "use or generate any keypair."
4464 "The @option{--reset} option will clear the cache entry for the current file "
4465 "and require a passphrase, if needed, before saving."
4467 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4468 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4469 "(@pxref{Configuration}) for available ciphers."
4471 "The @option{--cipher-iterations} option specifies the number of times to "
4472 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4474 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4475 "the client to obtain the key paramaters to use when generating a new "
4476 "keypair. The inquired data is expected to be an S-expression. If not "
4477 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4478 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4479 "that when this option is specified a new keypair will be generated "
4480 "reguardless if the file is a new one and that if the data file is protected "
4481 "the passphrase to open it will be required before generating the new keypair."
4483 "You can encrypt the data file to a public key other than the one that it "
4484 "was originally encrypted with by passing the @option{--keygrip} option with "
4485 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4486 "be of any key that @command{gpg-agent} knows about. The "
4487 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4488 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4489 "keygrip of an existing data file. This option may be needed when using a "
4490 "smartcard. This option has no effect with symmetrically encrypted data files."
4492 "The @option{--s2k-count} option sets number of hash iterations for a "
4493 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4494 "value and is the default. This setting only affects new files. To change "
4495 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4496 "has no effect with symmetrically encrypted data files."
4499 new_command("ISCACHED", 1, 0, iscached_command
, _(
4500 "ISCACHED [--lock] <filename>\n"
4501 "An @emph{OK} response is returned if the specified @var{filename} is found "
4502 "in the file cache. If not found in the cache but exists on the filesystem "
4503 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4506 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4507 "file exists; it does not need to be opened nor cached. The lock will be "
4508 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4512 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4513 "CLEARCACHE [<filename>]\n"
4514 "Clears a file cache entry for all or the specified @var{filename}."
4517 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4518 "CACHETIMEOUT <filename> <seconds>\n"
4519 "The time in @var{seconds} until @var{filename} will be removed from the "
4520 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4521 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4522 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4526 new_command("LIST", 0, 1, list_command
, _(
4527 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4528 "If no element path is given then a newline separated list of root elements "
4529 "is returned with a data response. If given, then all reachable elements "
4530 "of the specified element path are returned unless the @option{--no-recurse} "
4531 "option is specified. If specified, only the child elements of the element "
4532 "path are returned without recursing into grandchildren. Each resulting "
4533 "element is prefixed with the literal @code{!} character when the element "
4534 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4536 "When the @option{--verbose} option is passed then each element path "
4537 "returned will have zero or more flags appened to it. These flags are "
4538 "delimited from the element path by a single space character. A flag itself "
4539 "is a single character. Flag @code{+} indicates that there are child nodes of "
4540 "the current element path. Flag @code{E} indicates that an element of an "
4541 "element path contained in a @var{target} attribute could not be found. Flag "
4542 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4543 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4544 "of the @var{target} attribute contained in the current element (see below)."
4546 "The @option{--with-target} option implies @option{--verbose} and will append "
4547 "an additional flag @code{T} followed by a single space then an element path. "
4548 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4549 "current element when it contains a @var{target} attribute. When no "
4550 "@var{target} attribute is found then no flag will be appended."
4552 "The @option{--no-recurse} option limits the amount of data returned to only "
4553 "the listing of children of the specified element path and not any "
4556 "The @option{--all} option lists the entire element tree for each root "
4557 "element. This option also implies option @option{--verbose}."
4559 "When the @option{--inquire} option is passed then all remaining non-option "
4560 "arguments are retrieved via a server @emph{INQUIRE}."
4563 new_command("REALPATH", 0, 1, realpath_command
, _(
4564 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4565 "Resolves all @code{target} attributes of the specified element path and "
4566 "returns the result with a data response. @xref{Target Attribute}, for details."
4568 "When the @option{--inquire} option is passed then all remaining non-option "
4569 "arguments are retrieved via a server @emph{INQUIRE}."
4572 new_command("STORE", 0, 1, store_command
, _(
4573 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4574 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4576 "Creates a new element path or modifies the @var{content} of an existing "
4577 "element. If only a single element is specified then a new root element is "
4578 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4579 "set to the final @key{TAB} delimited element. If no @var{content} is "
4580 "specified after the final @key{TAB}, then the content of an existing "
4581 "element will be removed; or empty when creating a new element."
4583 "The only restriction of an element name is that it not contain whitespace "
4584 "or begin with the literal element character @code{!} unless specifying a "
4585 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4586 "the @key{TAB} delimited elements. It is recommended that the content of an "
4587 "element be base64 encoded when it contains control or @key{TAB} characters "
4588 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4591 new_command("RENAME", 0, 1, rename_command
, _(
4592 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4593 "Renames the specified @var{element} to the new @var{value}. If an element of "
4594 "the same name as the @var{value} already exists it will be overwritten."
4596 "When the @option{--inquire} option is passed then all remaining non-option "
4597 "arguments are retrieved via a server @emph{INQUIRE}."
4600 new_command("COPY", 0, 1, copy_command
, _(
4601 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4602 "Copies the entire element tree starting from the child node of the source "
4603 "element, to the destination element path. If the destination element path "
4604 "does not exist then it will be created; otherwise it is overwritten."
4606 "Note that attributes from the source element are merged into the "
4607 "destination element when the destination element path exists. When an "
4608 "attribute of the same name exists in both the source and destination "
4609 "elements then the destination attribute will be updated to the source "
4612 "When the @option{--inquire} option is passed then all remaining non-option "
4613 "arguments are retrieved via a server @emph{INQUIRE}."
4616 new_command("MOVE", 0, 1, move_command
, _(
4617 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4618 "Moves the source element path to the destination element path. If the "
4619 "destination is not specified then it will be moved to the root node of the "
4620 "document. If the destination is specified and exists then it will be "
4621 "overwritten; otherwise non-existing elements of the destination element "
4622 "path will be created."
4624 "When the @option{--inquire} option is passed then all remaining non-option "
4625 "arguments are retrieved via a server @emph{INQUIRE}."
4628 new_command("DELETE", 0, 1, delete_command
, _(
4629 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4630 "Removes the specified element path and all of its children. This may break "
4631 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4632 "refers to this element or any of its children."
4634 "When the @option{--inquire} option is passed then all remaining non-option "
4635 "arguments are retrieved via a server @emph{INQUIRE}."
4638 new_command("GET", 0, 1, get_command
, _(
4639 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4640 "Retrieves the content of the specified element. The content is returned "
4641 "with a data response."
4643 "When the @option{--inquire} option is passed then all remaining non-option "
4644 "arguments are retrieved via a server @emph{INQUIRE}."
4647 new_command("ATTR", 0, 1, attr_command
, _(
4648 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4650 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4652 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4653 "element. When no @var{value} is specified any existing value will be removed."
4655 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4657 " Removes an @var{attribute} from an element."
4659 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4661 " Retrieves a newline separated list of attributes names and values "
4662 "from the specified element. Each attribute name and value is space delimited."
4664 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4666 " Retrieves the value of an @var{attribute} from an element."
4669 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4670 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4673 "The @code{_mtime} attribute is updated each time an element is modified by "
4674 "either storing content, editing attributes or by deleting a child element. "
4675 "The @code{_ctime} attribute is created for each new element in an element "
4678 "When the @option{--inquire} option is passed then all remaining non-option "
4679 "arguments are retrieved via a server @emph{INQUIRE}."
4681 "@xref{Target Attribute}, for details about this special attribute."
4684 new_command("XPATH", 0, 1, xpath_command
, _(
4685 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4686 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4687 "specified it is assumed the expression is a request to return a result. "
4688 "Otherwise, the result is set to the @var{value} argument and the document is "
4689 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4690 "is assumed to be empty and the document is updated. For example:"
4693 "XPATH //element[@@_name='password']@key{TAB}\n"
4696 "would clear the content of all @code{password} elements in the data file "
4697 "while leaving off the trailing @key{TAB} would return all @code{password} "
4698 "elements in @abbr{XML} format."
4700 "When the @option{--inquire} option is passed then all remaining non-option "
4701 "arguments are retrieved via a server @emph{INQUIRE}."
4703 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4704 "expression syntax."
4707 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4708 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4709 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4710 "attributes and does not return a result. For the @var{SET} operation the "
4711 "@var{value} is optional but the field is required. If not specified then "
4712 "the attribute value will be empty. For example:"
4715 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4718 "would create an @code{password} attribute for each @code{password} element "
4719 "found in the document. The attribute value will be empty but still exist."
4721 "When the @option{--inquire} option is passed then all remaining non-option "
4722 "arguments are retrieved via a server @emph{INQUIRE}."
4724 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4725 "expression syntax."
4728 new_command("IMPORT", 0, 1, import_command
, _(
4729 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4730 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4732 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4733 "argument is raw @abbr{XML} data. The content is created as a child of "
4734 "the element path specified with the @option{--root} option or at the "
4735 "document root when not specified. Existing elements of the same name will "
4738 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4742 new_command("DUMP", 0, 1, dump_command
, _(
4744 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4745 "dumping a specific node."
4748 new_command("LOCK", 0, 0, lock_command
, _(
4750 "Locks the mutex associated with the opened file. This prevents other clients "
4751 "from sending commands to the same opened file until the client "
4752 "that sent this command either disconnects or sends the @code{UNLOCK} "
4753 "command. @xref{UNLOCK}."
4756 new_command("UNLOCK", 1, 0, unlock_command
, _(
4758 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4759 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4760 "@pxref{ISCACHED})."
4763 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4764 "GETCONFIG [filename] <parameter>\n"
4765 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4766 "data response. If no file has been opened then the value for @var{filename} "
4767 "or the default from the @samp{global} section will be returned. If a file "
4768 "has been opened and no @var{filename} is specified, a value previously "
4769 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4772 new_command("OPTION", 1, 1, option_command
, _(
4773 "OPTION <NAME>=<VALUE>\n"
4774 "Sets a client option @var{name} to @var{value}. The value for an option is "
4775 "kept for the duration of the connection."
4778 "@item DISABLE-PINENTRY\n"
4779 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4780 "server inquire is sent to the client to obtain the passphrase. This option "
4781 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4782 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4784 "@item PINENTRY-TIMEOUT\n"
4785 "Sets the number of seconds before a pinentry prompt will return an error "
4786 "while waiting for user input."
4789 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4792 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4795 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4797 "@item PINENTRY-DESC\n"
4798 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4800 "@item PINENTRY-TITLE\n"
4801 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4803 "@item PINENTRY-PROMPT\n"
4804 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4807 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4809 "@item LC-MESSAGES\n"
4810 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4813 "Associates the thread ID of the connection with the specified textual "
4814 "representation. Useful for debugging log messages."
4816 "@item LOCK-TIMEOUT\n"
4817 "When not @code{0}, the duration in tenths of a second to wait for the file "
4818 "mutex which has been locked by another thread to be released before returning "
4819 "an error. When @code{-1}, then an error will be returned immediately."
4822 "An integer specifiying the logging level."
4826 new_command("LS", 1, 1, ls_command
, _(
4828 "Lists the available data files stored in the data directory "
4829 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4832 new_command("RESET", 1, 1, NULL
, _(
4834 "Closes the currently opened file but keeps any previously set client options."
4837 new_command("NOP", 1, 1, NULL
, _(
4839 "Does nothing. Always returns successfully."
4842 /* !END-HELP-TEXT! */
4843 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4844 new_command ("END", 1, 1, NULL
, NULL
);
4845 new_command ("BYE", 1, 1, NULL
, NULL
);
4848 for (i
= 0; command_table
[i
]; i
++);
4849 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4854 register_commands (assuan_context_t ctx
)
4858 for (; command_table
[i
]; i
++)
4860 if (!command_table
[i
]->handler
)
4863 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4864 command_table
[i
]->handler
,
4865 command_table
[i
]->help
);
4870 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4874 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4878 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4882 return assuan_register_post_cmd_notify (ctx
, command_finalize
);