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_save_opt_inquire (void *data
, void *value
)
449 struct client_s
*client
= data
;
452 if (!(client
->flags
& FLAG_NEW
))
454 rc
= peer_is_invoker (client
);
455 if (rc
== GPG_ERR_EACCES
)
460 client
->opts
|= OPT_INQUIRE
;
465 parse_opt_inquire (void *data
, void *value
)
467 struct client_s
*client
= data
;
470 client
->opts
|= OPT_INQUIRE
;
475 update_checksum (struct client_s
*client
)
479 struct cache_data_s
*cdata
;
480 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
487 cdata
= cache_get_data (client
->md5file
);
491 cdata
->crc
= xmalloc (len
);
492 memcpy (cdata
->crc
, crc
, len
);
499 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
506 if (cdata
&& !cdata
->crc
)
507 return GPG_ERR_CHECKSUM
;
509 rc
= get_checksum (client
->filename
, &crc
, &len
);
514 n
= memcmp (cdata
->crc
, crc
, len
);
515 else if (client
->crc
)
516 n
= memcmp (client
->crc
, crc
, len
);
519 return n
? GPG_ERR_CHECKSUM
: 0;
523 do_open (assuan_context_t ctx
, const char *filename
, const char *password
)
525 struct client_s
*client
= assuan_get_pointer (ctx
);
526 struct cache_data_s
*cdata
;
530 if (!valid_filename (filename
))
531 return GPG_ERR_INV_VALUE
;
533 gcry_md_hash_buffer (GCRY_MD_MD5
, client
->md5file
, filename
,
535 client
->filename
= str_dup (filename
);
536 if (!client
->filename
)
537 return GPG_ERR_ENOMEM
;
540 cdata
= cache_get_data (client
->md5file
);
541 if (cdata
&& cdata
->doc
)
545 /* This will check that the key is cached in the agent which needs to
546 * be determined for files that share a keygrip. */
549 rc
= cache_iscached (client
->filename
, &defer
);
550 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
551 rc
= GPG_ERR_KEY_EXPIRED
;
554 if (!rc
&& !(client
->flags
& FLAG_NEW
))
555 rc
= validate_checksum (client
, cdata
);
558 if (!rc
&& client
->thd
->remote
559 && config_get_boolean (client
->filename
, "tcp_require_key"))
560 rc
= GPG_ERR_KEY_EXPIRED
;
563 if (!rc
&& !password
)
565 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
569 if (IS_PKI (client
->crypto
))
571 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
573 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
574 client
->crypto
->grip
);
575 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
577 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
578 client
->crypto
->sign_grip
);
583 rc
= open_finalize (ctx
, NULL
, 0);
589 /* There was an error accessing the file so clear the cache entry. The
590 * real error will be returned from read_data_file() since the file
591 * may have only disappeared. */
594 log_write ("%s: %s", filename
, pwmd_strerror (rc
));
595 rc
= cache_clear (client
->md5file
);
596 send_status_all (STATUS_CACHE
, NULL
);
603 rc
= read_data_file (client
->filename
, client
->crypto
);
606 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
607 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
609 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
613 client
->flags
|= FLAG_NEW
;
614 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
615 memset (client
->crypto
->sign_grip
, 0,
616 sizeof (client
->crypto
->sign_grip
));
617 rc
= open_finalize (ctx
, NULL
, 0);
621 if (password
&& IS_PKI (client
->crypto
))
624 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
630 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
635 open_command (assuan_context_t ctx
, char *line
)
638 struct client_s
*client
= assuan_get_pointer (ctx
);
639 char **req
, *filename
;
640 unsigned char md5file
[16];
642 assuan_peercred_t peer
;
643 struct argv_s
*args
[] = {
644 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
648 rc
= parse_options (&line
, args
, client
);
650 return send_error (ctx
, rc
);
652 req
= str_split (line
, " ", 2);
654 return send_error (ctx
, GPG_ERR_SYNTAX
);
656 rc
= do_validate_peer (ctx
, req
[0], &peer
);
660 return send_error (ctx
, rc
);
663 pthread_cleanup_push (req_cleanup
, req
);
665 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
666 /* This client may have locked a different file with ISCACHED --lock than
667 * the current filename. This will remove that lock. */
668 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
669 if (client
->flags
& FLAG_OPEN
||
670 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
672 typeof (client
->opts
) opts
= client
->opts
;
673 typeof (client
->flags
) flags
= client
->flags
;
676 client
->flags
|= FLAG_KEEP_LOCK
;
678 cleanup_client (client
);
680 client
->flags
|= flags
;
681 client
->flags
&= ~(FLAG_LOCK_CMD
);
683 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
686 /* Need to lock the mutex here because file_modified() cannot without
687 * knowing the filename. */
688 memcpy (client
->md5file
, md5file
, 16);
689 rc
= lock_file_mutex (client
, 1);
691 client
->flags
&= ~FLAG_OPEN
;
695 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
698 if (IS_PKI (client
->crypto
))
699 rc
= set_pinentry_mode (client
->crypto
->agent
,
700 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
705 rc
= do_open (ctx
, filename
, password
);
707 cleanup_client (client
);
709 cleanup_crypto_stage1 (client
->crypto
);
713 pthread_cleanup_pop (1);
715 if (!rc
&& client
->flags
& FLAG_NEW
)
716 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
719 (void) kill_scd (client
->crypto
->agent
);
722 return send_error (ctx
, rc
);
726 parse_opt_no_passphrase (void *data
, void *value
)
728 struct client_s
*client
= data
;
731 client
->opts
|= OPT_NO_PASSPHRASE
;
736 parse_save_opt_no_agent (void *data
, void *value
)
738 struct client_s
*client
= data
;
740 client
->opts
|= OPT_NO_AGENT
;
745 parse_save_opt_cipher (void *data
, void *value
)
747 struct client_s
*client
= data
;
748 int algo
= cipher_string_to_gcrypt ((char *) value
);
749 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
753 return GPG_ERR_INV_VALUE
;
755 if (!(client
->flags
& FLAG_NEW
))
757 rc
= peer_is_invoker (client
);
758 if (rc
== GPG_ERR_EACCES
)
762 flags
&= ((uint16_t) ((uint32_t) (flags
& 0xFFFFFFFF)));
763 if (!(client
->crypto
->hdr
.flags
& gcrypt_to_cipher (algo
)))
773 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
780 permitted_to_save (struct client_s
*client
, const unsigned char *grip
,
781 size_t size
, const char *value
)
786 if (!(client
->flags
& FLAG_NEW
))
788 hexgrip
= bin2hex (grip
, size
);
789 rc
= !strcmp (hexgrip
, (char *)value
) ? 0 : GPG_ERR_EACCES
;
792 rc
= peer_is_invoker (client
);
800 parse_save_opt_keygrip (void *data
, void *value
)
803 struct client_s
*client
= data
;
804 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->grip
,
805 sizeof (client
->crypto
->grip
),
808 if (!IS_PKI (client
->crypto
))
809 return GPG_ERR_INV_ARG
;
814 if (client
->crypto
->save
.pkey
)
815 gcry_sexp_release (client
->crypto
->save
.pkey
);
817 client
->crypto
->save
.pkey
= NULL
;
818 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
820 return GPG_ERR_INV_ARG
;
825 parse_save_opt_sign_keygrip (void *data
, void *value
)
828 struct client_s
*client
= data
;
829 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->sign_grip
,
830 sizeof (client
->crypto
->sign_grip
),
833 if (!IS_PKI (client
->crypto
))
834 return GPG_ERR_INV_ARG
;
839 if (client
->crypto
->save
.sigpkey
)
840 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
842 client
->crypto
->save
.sigpkey
= NULL
;
843 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
845 return GPG_ERR_INV_ARG
;
850 parse_opt_s2k_count (void *data
, void *value
)
852 struct client_s
*client
= data
;
856 return GPG_ERR_INV_VALUE
;
858 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
863 parse_save_opt_iterations (void *data
, void *value
)
865 struct client_s
*client
= data
;
870 return GPG_ERR_INV_VALUE
;
873 n
= strtoull (v
, &p
, 10);
874 if (n
== UINT64_MAX
&& errno
)
875 return gpg_error_from_errno (errno
);
877 return GPG_ERR_INV_VALUE
;
879 if (!(client
->flags
& FLAG_NEW
))
881 gpg_error_t rc
= peer_is_invoker (client
);
883 if (rc
== GPG_ERR_EACCES
)
885 if (client
->crypto
->hdr
.iterations
!= n
)
894 client
->crypto
->save
.hdr
.iterations
= n
;
899 save_finalize (assuan_context_t ctx
)
901 struct client_s
*client
= assuan_get_pointer (ctx
);
903 xmlChar
*xmlbuf
= NULL
;
908 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
910 return GPG_ERR_ENOMEM
;
912 pthread_cleanup_push (xmlFree
, xmlbuf
);
914 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
915 && (client
->opts
& OPT_NO_AGENT
))
916 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
918 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
919 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
920 NULL
, &key
, &keylen
, 1, 1,
921 (client
->opts
& OPT_NO_PASSPHRASE
));
926 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
927 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
930 pubkey
= client
->crypto
->pkey_sexp
;
934 sigkey
= client
->crypto
->sigpkey_sexp
;
937 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
938 sigkey
= client
->crypto
->save
.sigpkey
;
942 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
943 client
->filename
, xmlbuf
, xmlbuflen
);
944 if (pubkey
== client
->crypto
->save
.pkey
)
948 gcry_sexp_release (client
->crypto
->pkey_sexp
);
949 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
950 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
951 client
->crypto
->grip
);
954 gcry_sexp_release (pubkey
);
956 client
->crypto
->save
.pkey
= NULL
;
960 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
962 if (sigkey
== client
->crypto
->save
.sigpkey
)
966 if (client
->crypto
->sigpkey_sexp
)
967 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
969 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
970 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
971 client
->crypto
->sign_grip
);
974 gcry_sexp_release (sigkey
);
976 client
->crypto
->save
.sigpkey
= NULL
;
985 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
986 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
990 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
991 send_status_all (STATUS_CACHE
, NULL
);
995 rc
= update_checksum (client
);
996 client
->flags
&= ~(FLAG_NEW
);
1000 pthread_cleanup_pop (1); // xmlFree
1005 parse_opt_reset (void *data
, void *value
)
1007 struct client_s
*client
= data
;
1010 client
->opts
|= OPT_RESET
;
1015 save_command (assuan_context_t ctx
, char *line
)
1017 struct client_s
*client
= assuan_get_pointer (ctx
);
1020 struct argv_s
*args
[] = {
1021 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1022 parse_opt_no_passphrase
},
1023 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
1024 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
1025 parse_save_opt_inquire
},
1026 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
1027 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
1028 parse_save_opt_sign_keygrip
},
1029 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
1030 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
1031 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
1032 parse_save_opt_iterations
},
1033 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
1034 parse_save_opt_no_agent
},
1038 cleanup_save (&client
->crypto
->save
);
1039 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
1040 sizeof (file_header_t
));
1041 client
->crypto
->save
.s2k_count
=
1042 config_get_ulong (client
->filename
, "s2k_count");
1044 if (client
->flags
& FLAG_NEW
)
1045 client
->crypto
->save
.hdr
.iterations
=
1046 config_get_ulonglong (client
->filename
, "cipher_iterations");
1048 rc
= parse_options (&line
, args
, client
);
1050 return send_error (ctx
, rc
);
1052 if (!(client
->flags
& FLAG_NEW
))
1053 client
->opts
&= ~OPT_NO_AGENT
;
1054 else if (client
->opts
& OPT_NO_AGENT
)
1056 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
1057 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
1060 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
1061 && !(client
->opts
& OPT_INQUIRE
))
1062 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1064 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1065 return send_error (ctx
, gpg_error_from_errno (errno
));
1067 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
1069 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
1070 return send_error (ctx
, GPG_ERR_ENOANO
);
1074 rc
= cache_iscached (client
->filename
, &defer
);
1075 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
1077 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1080 if ((!rc
&& defer
) || config_get_boolean ("global", "require_save_key"))
1081 client
->opts
|= OPT_RESET
;
1083 if (!rc
&& (client
->opts
& OPT_RESET
))
1085 rc
= cache_clear (client
->md5file
);
1087 return send_error (ctx
, rc
);
1089 log_write ("%s: %s", client
->filename
,
1090 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1091 send_status_all (STATUS_CACHE
, NULL
);
1095 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1098 return send_error (ctx
, rc
);
1101 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1102 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1103 && !(client
->opts
& OPT_NO_AGENT
))
1105 rc
= set_pinentry_mode (client
->crypto
->agent
,
1106 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1109 if (!(client
->flags
& FLAG_NEW
))
1111 struct crypto_s
*crypto
;
1115 /* Wanting to generate a new key. Require the key to open the
1116 current file before proceeding reguardless of the
1117 require_save_key configuration parameter. */
1118 rc
= cache_clear (client
->md5file
);
1121 rc
= init_client_crypto (&crypto
);
1123 crypto
->client_ctx
= client
->ctx
;
1127 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1128 client
->filename
, &key
, &keylen
);
1131 cleanup_crypto (&crypto
);
1135 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1139 struct inquire_data_s idata
= { 0 };
1140 char *params
= client
->opts
& OPT_INQUIRE
1141 ? NULL
: default_key_params (client
->crypto
);
1143 pthread_cleanup_push (xfree
, params
);
1144 idata
.crypto
= client
->crypto
;
1148 idata
.line
= params
;
1149 idata
.len
= strlen (params
);
1154 client
->crypto
->agent
->inquire_data
= &idata
;
1155 client
->crypto
->agent
->inquire_cb
= NULL
;
1156 rc
= generate_key (client
->crypto
, params
,
1157 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1158 pthread_cleanup_pop (1);
1164 rc
= save_finalize (ctx
);
1166 cleanup_crypto_stage1 (client
->crypto
);
1168 (void) kill_scd (client
->crypto
->agent
);
1170 return send_error (ctx
, rc
);
1174 do_delete (assuan_context_t ctx
, char *line
)
1176 struct client_s
*client
= assuan_get_pointer (ctx
);
1181 if (strchr (line
, '\t'))
1182 req
= str_split (line
, "\t", 0);
1184 req
= str_split (line
, " ", 0);
1187 return GPG_ERR_SYNTAX
;
1189 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1197 * No sub-node defined. Remove the entire node (root element).
1203 rc
= is_element_owner (client
, n
);
1206 rc
= unlink_node (client
, n
);
1215 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1216 NULL
, NULL
, 0, 0, NULL
, 0);
1221 rc
= is_element_owner (client
, n
);
1224 rc
= unlink_node (client
, n
);
1232 delete_command (assuan_context_t ctx
, char *line
)
1234 struct client_s
*client
= assuan_get_pointer (ctx
);
1236 struct argv_s
*args
[] = {
1237 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1241 rc
= parse_options (&line
, args
, client
);
1243 return send_error (ctx
, rc
);
1245 if (client
->opts
& OPT_INQUIRE
)
1247 unsigned char *result
;
1250 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1252 return send_error (ctx
, rc
);
1254 line
= (char *) result
;
1257 rc
= do_delete (ctx
, line
);
1259 if (client
->opts
& OPT_INQUIRE
)
1262 return send_error (ctx
, rc
);
1266 store_command (assuan_context_t ctx
, char *line
)
1268 struct client_s
*client
= assuan_get_pointer (ctx
);
1271 unsigned char *result
;
1273 xmlNodePtr n
, parent
;
1275 char *content
= NULL
;
1277 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1279 return send_error (ctx
, rc
);
1281 req
= str_split ((char *) result
, "\t", 0);
1285 return send_error (ctx
, GPG_ERR_SYNTAX
);
1287 len
= strv_length (req
);
1288 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1289 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1292 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1295 if (has_content
|| !*req
[len
- 1])
1298 content
= req
[len
- 1];
1299 req
[len
- 1] = NULL
;
1303 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1304 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1306 rc
= new_root_element (client
, client
->doc
, *req
);
1310 return send_error (ctx
, rc
);
1319 return send_error (ctx
, rc
);
1324 if (req
[1] && *req
[1])
1327 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1329 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1330 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1336 rc
= is_element_owner (client
, parent
);
1339 n
= find_text_node (parent
->children
);
1341 xmlNodeSetContent (n
, (xmlChar
*) content
);
1343 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1345 update_element_mtime (client
, parent
);
1351 return send_error (ctx
, rc
);
1355 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1360 int progress
= config_get_integer ("global", "xfer_progress");
1364 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1365 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1366 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1374 if (sent
+ to_send
> total
)
1375 to_send
= total
- sent
;
1377 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1378 flush
? 0 : to_send
);
1381 sent
+= flush
? 0 : to_send
;
1383 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1384 (sent
== total
&& flush
))
1385 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1387 if (!flush
&& !rc
&& sent
== total
)
1394 while (!rc
&& sent
< total
);
1400 do_get (assuan_context_t ctx
, char *line
)
1402 struct client_s
*client
= assuan_get_pointer (ctx
);
1407 req
= str_split (line
, "\t", 0);
1412 return GPG_ERR_SYNTAX
;
1415 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1424 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1431 if (!n
|| !n
->children
)
1432 return GPG_ERR_NO_DATA
;
1434 n
= find_text_node (n
->children
);
1435 if (!n
|| !n
->content
|| !*n
->content
)
1436 return GPG_ERR_NO_DATA
;
1438 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1443 get_command (assuan_context_t ctx
, char *line
)
1445 struct client_s
*client
= assuan_get_pointer (ctx
);
1447 struct argv_s
*args
[] = {
1448 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1452 rc
= parse_options (&line
, args
, client
);
1454 return send_error (ctx
, rc
);
1456 if (client
->opts
& OPT_INQUIRE
)
1458 unsigned char *result
;
1461 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1463 return send_error (ctx
, rc
);
1465 line
= (char *) result
;
1468 rc
= do_get (ctx
, line
);
1470 if (client
->opts
& OPT_INQUIRE
)
1473 return send_error (ctx
, rc
);
1476 static void list_command_cleanup1 (void *arg
);
1478 realpath_command (assuan_context_t ctx
, char *line
)
1480 struct string_s
*string
= NULL
;
1482 struct client_s
*client
= assuan_get_pointer (ctx
);
1483 struct argv_s
*args
[] = {
1484 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1488 rc
= parse_options (&line
, args
, client
);
1490 return send_error (ctx
, rc
);
1492 if (client
->opts
& OPT_INQUIRE
)
1494 unsigned char *result
;
1497 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1499 return send_error (ctx
, rc
);
1501 line
= (char *) result
;
1504 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1507 pthread_cleanup_push (list_command_cleanup1
, string
);
1508 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1509 pthread_cleanup_pop (1);
1512 if (client
->opts
& OPT_INQUIRE
)
1515 return send_error (ctx
, rc
);
1519 list_command_cleanup1 (void *arg
)
1522 string_free ((struct string_s
*) arg
, 1);
1526 list_command_cleanup2 (void *arg
)
1528 struct element_list_s
*elements
= arg
;
1534 int total
= slist_length (elements
->list
);
1537 for (i
= 0; i
< total
; i
++)
1539 char *tmp
= slist_nth_data (elements
->list
, i
);
1543 slist_free (elements
->list
);
1546 if (elements
->prefix
)
1547 xfree (elements
->prefix
);
1550 strv_free (elements
->req
);
1557 parse_list_opt_norecurse (void *data
, void *value
)
1559 struct client_s
*client
= data
;
1561 client
->opts
&= ~(OPT_LIST_RECURSE
);
1566 parse_list_opt_verbose (void *data
, void *value
)
1568 struct client_s
*client
= data
;
1570 client
->opts
|= OPT_LIST_VERBOSE
;
1575 parse_list_opt_target (void *data
, void *value
)
1577 struct client_s
*client
= data
;
1579 client
->opts
|= OPT_LIST_WITH_TARGET
;
1584 parse_list_opt_all (void *data
, void *value
)
1586 struct client_s
*client
= data
;
1588 client
->opts
|= OPT_LIST_ALL
;
1593 list_path_once (struct client_s
*client
, char *line
,
1594 struct element_list_s
*elements
, struct string_s
*result
)
1598 elements
->req
= str_split (line
, " ", 0);
1600 strv_printf (&elements
->req
, "%s", line
);
1602 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1603 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1608 int total
= slist_length (elements
->list
);
1611 rc
= GPG_ERR_NO_DATA
;
1619 for (i
= 0; i
< total
; i
++)
1621 char *tmp
= slist_nth_data (elements
->list
, i
);
1623 string_append_printf (result
, "%s%s", tmp
,
1624 i
+ 1 == total
? "" : "\n");
1629 rc
= GPG_ERR_NO_DATA
;
1636 has_list_flag (char *path
, char *flags
)
1640 while (*p
&& *++p
!= ' ');
1649 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1660 do_list (assuan_context_t ctx
, char *line
)
1662 struct client_s
*client
= assuan_get_pointer (ctx
);
1664 struct element_list_s
*elements
= NULL
;
1666 elements
= xcalloc (1, sizeof (struct element_list_s
));
1668 return GPG_ERR_ENOMEM
;
1670 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1672 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1673 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1675 if (!line
|| !*line
)
1677 struct string_s
*str
= NULL
;
1679 pthread_cleanup_push (list_command_cleanup2
, elements
);
1680 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1681 elements
->with_target
);
1682 pthread_cleanup_pop (1);
1683 pthread_cleanup_push (list_command_cleanup1
, str
);
1687 if (client
->opts
& OPT_LIST_ALL
)
1689 char **roots
= str_split (str
->str
, "\n", 0);
1692 pthread_cleanup_push (req_cleanup
, roots
);
1693 string_truncate (str
, 0);
1695 for (p
= roots
; *p
; p
++)
1697 if (strchr (*p
, ' '))
1699 if (has_list_flag (*p
, "EOP"))
1701 string_append_printf (str
, "%s%s", *p
,
1702 *(p
+ 1) ? "\n" : "");
1707 elements
= xcalloc (1, sizeof (struct element_list_s
));
1710 rc
= GPG_ERR_ENOMEM
;
1714 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1716 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1717 OPT_LIST_WITH_TARGET
);
1718 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1719 pthread_cleanup_push (list_command_cleanup2
, elements
);
1720 rc
= list_path_once (client
, *p
, elements
, str
);
1721 pthread_cleanup_pop (1);
1726 string_append (str
, "\n");
1729 pthread_cleanup_pop (1);
1733 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1736 pthread_cleanup_pop (1);
1740 pthread_cleanup_push (list_command_cleanup2
, elements
);
1741 struct string_s
*str
= string_new (NULL
);
1742 pthread_cleanup_push (list_command_cleanup1
, str
);
1743 rc
= list_path_once (client
, line
, elements
, str
);
1745 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1747 pthread_cleanup_pop (1);
1748 pthread_cleanup_pop (1);
1753 list_command (assuan_context_t ctx
, char *line
)
1755 struct client_s
*client
= assuan_get_pointer (ctx
);
1757 struct argv_s
*args
[] = {
1758 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1759 parse_list_opt_norecurse
},
1760 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1761 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1762 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1763 parse_list_opt_target
},
1764 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1768 if (disable_list_and_dump
== 1)
1769 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1771 client
->opts
|= OPT_LIST_RECURSE
;
1772 rc
= parse_options (&line
, args
, client
);
1774 return send_error (ctx
, rc
);
1776 if (client
->opts
& OPT_LIST_ALL
)
1777 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1779 if (client
->opts
& OPT_INQUIRE
)
1781 unsigned char *result
;
1784 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1786 return send_error (ctx
, rc
);
1788 line
= (char *) result
;
1791 rc
= do_list (ctx
, line
);
1793 if (client
->opts
& OPT_INQUIRE
)
1796 return send_error (ctx
, rc
);
1800 * req[0] - element path
1803 attribute_list (assuan_context_t ctx
, char **req
)
1805 struct client_s
*client
= assuan_get_pointer (ctx
);
1806 char **attrlist
= NULL
;
1814 if (!req
|| !req
[0])
1815 return GPG_ERR_SYNTAX
;
1817 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1820 * The first argument may be only a root element.
1822 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1823 return GPG_ERR_SYNTAX
;
1826 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1836 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1837 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1848 for (a
= n
->properties
; a
; a
= a
->next
)
1852 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1855 strv_free (attrlist
);
1857 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1858 pwmd_strerror (GPG_ERR_ENOMEM
));
1859 return GPG_ERR_ENOMEM
;
1864 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1866 && an
->content
? (char *) an
->content
: "");
1870 strv_free (attrlist
);
1871 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1872 pwmd_strerror (GPG_ERR_ENOMEM
));
1873 return GPG_ERR_ENOMEM
;
1876 attrlist
[++i
] = NULL
;
1880 return GPG_ERR_NO_DATA
;
1882 line
= strv_join ("\n", attrlist
);
1886 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1887 pwmd_strerror (GPG_ERR_ENOMEM
));
1888 strv_free (attrlist
);
1889 return GPG_ERR_ENOMEM
;
1892 pthread_cleanup_push (xfree
, line
);
1893 pthread_cleanup_push (req_cleanup
, attrlist
);
1894 rc
= xfer_data (ctx
, line
, strlen (line
));
1895 pthread_cleanup_pop (1);
1896 pthread_cleanup_pop (1);
1901 * req[0] - attribute
1902 * req[1] - element path
1905 attribute_delete (struct client_s
*client
, char **req
)
1911 if (!req
|| !req
[0] || !req
[1])
1912 return GPG_ERR_SYNTAX
;
1914 if (!strcmp (req
[0], "_name"))
1915 return GPG_ERR_INV_ATTR
;
1917 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1920 * The first argument may be only a root element.
1922 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1923 return GPG_ERR_SYNTAX
;
1926 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1932 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1933 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1938 if (!strcmp (req
[0], (char *) "_acl"))
1940 rc
= is_element_owner (client
, n
);
1945 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
1953 create_element_path (struct client_s
*client
,
1954 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1956 char **req
= *elements
;
1957 char **req_orig
= strv_dup (req
);
1958 xmlNodePtr n
= NULL
;
1964 *rc
= GPG_ERR_ENOMEM
;
1965 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1966 pwmd_strerror (GPG_ERR_ENOMEM
));
1971 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
1974 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1977 *rc
= new_root_element (client
, client
->doc
, req
[0]);
1983 else if (n
== parent
)
1985 *rc
= GPG_ERR_CONFLICT
;
1992 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
1994 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
1995 NULL
, NULL
, create_target_elements_cb
, 0, 0,
2002 * Reset the position of the element tree now that the elements
2003 * have been created.
2008 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2012 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2013 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2020 strv_free (req_orig
);
2027 * Creates a "target" attribute. When other commands encounter an element with
2028 * this attribute, the element path is modified to the target value. If the
2029 * source element path doesn't exist when using 'ATTR SET target', it is
2030 * created, but the destination element path must exist.
2032 * req[0] - source element path
2033 * req[1] - destination element path
2036 target_attribute (struct client_s
*client
, char **req
)
2038 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
2042 if (!req
|| !req
[0] || !req
[1])
2043 return GPG_ERR_SYNTAX
;
2045 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
2048 * The first argument may be only a root element.
2050 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
2051 return GPG_ERR_SYNTAX
;
2054 if (!valid_element_path (src
, 0))
2055 return GPG_ERR_INV_VALUE
;
2057 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
2060 * The first argument may be only a root element.
2062 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
2064 rc
= GPG_ERR_SYNTAX
;
2069 odst
= strv_dup (dst
);
2072 rc
= GPG_ERR_ENOMEM
;
2076 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2078 * Make sure the destination element path exists.
2085 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
2086 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2091 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
2092 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2095 n
= create_element_path (client
, &src
, &rc
, NULL
);
2099 line
= strv_join ("\t", odst
);
2102 rc
= GPG_ERR_ENOMEM
;
2106 rc
= add_attribute (client
, n
, "target", line
);
2117 * req[0] - attribute
2118 * req[1] - element path
2121 attribute_get (assuan_context_t ctx
, char **req
)
2123 struct client_s
*client
= assuan_get_pointer (ctx
);
2129 if (!req
|| !req
[0] || !req
[1])
2130 return GPG_ERR_SYNTAX
;
2132 if (strchr (req
[1], '\t'))
2134 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2135 return GPG_ERR_SYNTAX
;
2139 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2140 return GPG_ERR_SYNTAX
;
2143 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2150 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2151 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2159 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2160 return GPG_ERR_NOT_FOUND
;
2162 pthread_cleanup_push (xmlFree
, a
);
2165 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2167 rc
= GPG_ERR_NO_DATA
;
2169 pthread_cleanup_pop (1);
2178 * req[0] - attribute
2179 * req[1] - element path
2183 attribute_set (struct client_s
*client
, char **req
)
2189 if (!req
|| !req
[0] || !req
[1])
2190 return GPG_ERR_SYNTAX
;
2193 * Reserved attribute names.
2195 if (!strcmp (req
[0], "_name"))
2196 return GPG_ERR_INV_ATTR
;
2197 else if (!strcmp (req
[0], "target"))
2198 return target_attribute (client
, req
+ 1);
2199 else if (!valid_xml_attribute (req
[0]))
2200 return GPG_ERR_INV_VALUE
;
2202 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2205 * The first argument may be only a root element.
2207 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2208 return GPG_ERR_SYNTAX
;
2211 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2218 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2219 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2225 if (!strcmp (req
[0], (char *) "_acl"))
2227 rc
= is_element_owner (client
, n
);
2232 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2241 * req[1] - attribute name or element path if command is LIST
2242 * req[2] - element path
2243 * req[2] - element path or value
2247 do_attr (assuan_context_t ctx
, char *line
)
2249 struct client_s
*client
= assuan_get_pointer (ctx
);
2253 req
= str_split (line
, " ", 4);
2254 if (!req
|| !req
[0] || !req
[1])
2257 return GPG_ERR_SYNTAX
;
2260 pthread_cleanup_push (req_cleanup
, req
);
2262 if (strcasecmp (req
[0], "SET") == 0)
2263 rc
= attribute_set (client
, req
+ 1);
2264 else if (strcasecmp (req
[0], "GET") == 0)
2265 rc
= attribute_get (ctx
, req
+ 1);
2266 else if (strcasecmp (req
[0], "DELETE") == 0)
2267 rc
= attribute_delete (client
, req
+ 1);
2268 else if (strcasecmp (req
[0], "LIST") == 0)
2269 rc
= attribute_list (ctx
, req
+ 1);
2271 rc
= GPG_ERR_SYNTAX
;
2273 pthread_cleanup_pop (1);
2278 attr_command (assuan_context_t ctx
, char *line
)
2280 struct client_s
*client
= assuan_get_pointer (ctx
);
2282 struct argv_s
*args
[] = {
2283 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2287 rc
= parse_options (&line
, args
, client
);
2289 return send_error (ctx
, rc
);
2291 if (client
->opts
& OPT_INQUIRE
)
2293 unsigned char *result
;
2296 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2298 return send_error (ctx
, rc
);
2300 line
= (char *) result
;
2303 rc
= do_attr (ctx
, line
);
2305 if (client
->opts
& OPT_INQUIRE
)
2308 return send_error (ctx
, rc
);
2312 parse_iscached_opt_lock (void *data
, void *value
)
2314 struct client_s
*client
= data
;
2317 client
->opts
|= OPT_LOCK
;
2322 iscached_command (assuan_context_t ctx
, char *line
)
2324 struct client_s
*client
= assuan_get_pointer (ctx
);
2326 struct argv_s
*args
[] = {
2327 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2331 if (!line
|| !*line
)
2332 return send_error (ctx
, GPG_ERR_SYNTAX
);
2334 rc
= parse_options (&line
, args
, client
);
2336 return send_error (ctx
, rc
);
2337 else if (!valid_filename (line
))
2338 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2340 rc
= cache_iscached (line
, NULL
);
2341 if (client
->opts
& OPT_LOCK
2342 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2344 unsigned char md5file
[16];
2345 gpg_error_t trc
= rc
;
2347 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2348 if (memcmp (md5file
, client
->md5file
, 16))
2349 cleanup_client (client
);
2351 memcpy (client
->md5file
, md5file
, 16);
2352 rc
= do_lock (client
, 1);
2357 return send_error (ctx
, rc
);
2361 clearcache_command (assuan_context_t ctx
, char *line
)
2363 gpg_error_t rc
= 0, all_rc
= 0;
2364 unsigned char md5file
[16];
2368 struct client_thread_s
*once
= NULL
;
2371 MUTEX_LOCK (&cn_mutex
);
2372 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2374 if (!line
|| !*line
)
2377 t
= slist_length (cn_thread_list
);
2379 for (i
= 0; i
< t
; i
++)
2381 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2382 assuan_peercred_t peer
;
2387 /* Lock each connected clients' file mutex to prevent any other client
2388 * from accessing the cache entry (the file mutex is locked upon
2389 * command startup). The cache for the entry is not cleared if the
2390 * file mutex is locked by another client to prevent this function
2395 if (thd
->cl
->filename
)
2397 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2398 all_rc
= !all_rc
? rc
: all_rc
;
2406 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2407 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2409 if (pthread_equal (pthread_self (), thd
->tid
))
2413 if (!thd
->cl
->filename
||
2414 cache_iscached (thd
->cl
->filename
,
2415 NULL
) == GPG_ERR_NO_DATA
)
2421 cache_defer_clear (thd
->cl
->md5file
);
2424 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2432 rc
= cache_clear (thd
->cl
->md5file
);
2433 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2441 /* A single data filename was specified. Lock only this data file
2442 * mutex and free the cache entry. */
2445 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2446 rc
= do_validate_peer (ctx
, line
, &peer
);
2448 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2450 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2452 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2454 if (pthread_equal (pthread_self (), thd
->tid
))
2461 rc
= cache_clear (thd
->cl
->md5file
);
2462 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2466 cache_defer_clear (thd
->cl
->md5file
);
2474 /* Only connected clients' cache entries have been cleared. Now clear any
2475 * remaining cache entries without clients but only if there wasn't an
2476 * error from above since this would defeat the locking check of the
2477 * remaining entries. */
2483 /* No clients are using the specified file. */
2484 else if (!all_rc
&& !rc
&& !once
)
2486 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2487 rc
= cache_clear (md5file
);
2490 /* Release the connection mutex. */
2491 pthread_cleanup_pop (1);
2495 send_status_all (STATUS_CACHE
, NULL
);
2497 /* One or more files were locked while clearing all cache entries. */
2501 return send_error (ctx
, rc
);
2505 cachetimeout_command (assuan_context_t ctx
, char *line
)
2508 char **req
= str_split (line
, " ", 0);
2511 assuan_peercred_t peer
;
2513 if (!req
|| !*req
|| !req
[1])
2516 return send_error (ctx
, GPG_ERR_SYNTAX
);
2520 timeout
= (int) strtol (req
[1], &p
, 10);
2521 if (errno
!= 0 || *p
|| timeout
< -1)
2524 return send_error (ctx
, GPG_ERR_SYNTAX
);
2527 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2530 unsigned char md5file
[16];
2532 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2533 rc
= cache_set_timeout (md5file
, timeout
);
2534 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2537 MUTEX_LOCK (&rcfile_mutex
);
2538 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2540 MUTEX_UNLOCK (&rcfile_mutex
);
2545 return send_error (ctx
, rc
);
2549 dump_command (assuan_context_t ctx
, char *line
)
2553 struct client_s
*client
= assuan_get_pointer (ctx
);
2556 if (disable_list_and_dump
== 1)
2557 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2559 rc
= peer_is_invoker(client
);
2561 return send_error (ctx
, rc
);
2563 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2567 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2568 pwmd_strerror (GPG_ERR_ENOMEM
));
2569 return send_error (ctx
, GPG_ERR_ENOMEM
);
2572 pthread_cleanup_push (xmlFree
, xml
);
2573 rc
= xfer_data (ctx
, (char *) xml
, len
);
2574 pthread_cleanup_pop (1);
2575 return send_error (ctx
, rc
);
2579 getconfig_command (assuan_context_t ctx
, char *line
)
2581 struct client_s
*client
= assuan_get_pointer (ctx
);
2583 char filename
[255] = { 0 }, param
[747] = { 0 };
2584 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2586 if (!line
|| !*line
)
2587 return send_error (ctx
, GPG_ERR_SYNTAX
);
2589 if (strchr (line
, ' '))
2591 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2596 if (fp
&& !valid_filename (fp
))
2597 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2599 paramp
= str_down (paramp
);
2600 if (!strcmp (paramp
, "cipher") && fp
)
2602 struct crypto_s
*crypto
= NULL
;
2604 rc
= init_client_crypto (&crypto
);
2607 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2611 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2621 UPDATE_AGENT_CTX (client
, crypto
);
2622 cleanup_crypto (&crypto
);
2623 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2624 return send_error (ctx
, rc
);
2629 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2631 struct crypto_s
*crypto
= NULL
;
2633 rc
= init_client_crypto (&crypto
);
2636 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2639 tmp
= str_asprintf ("%llu",
2640 (unsigned long long) crypto
->hdr
.
2643 rc
= GPG_ERR_ENOMEM
;
2647 UPDATE_AGENT_CTX (client
, crypto
);
2648 cleanup_crypto (&crypto
);
2649 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2650 return send_error (ctx
, rc
);
2655 else if (!strcmp (paramp
, "passphrase"))
2656 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2658 p
= config_get_value (fp
? fp
: "global", paramp
);
2660 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2662 tmp
= expand_homedir (p
);
2666 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2667 pwmd_strerror (GPG_ERR_ENOMEM
));
2668 return send_error (ctx
, GPG_ERR_ENOMEM
);
2673 pthread_cleanup_push (xfree
, p
);
2674 rc
= xfer_data (ctx
, p
, strlen (p
));
2675 pthread_cleanup_pop (1);
2676 return send_error (ctx
, rc
);
2681 xmlXPathContextPtr xp
;
2682 xmlXPathObjectPtr result
;
2688 xpath_command_cleanup (void *arg
)
2690 struct xpath_s
*xpath
= arg
;
2695 req_cleanup (xpath
->req
);
2698 xmlBufferFree (xpath
->buf
);
2701 xmlXPathFreeObject (xpath
->result
);
2704 xmlXPathFreeContext (xpath
->xp
);
2708 do_xpath (assuan_context_t ctx
, char *line
)
2711 struct client_s
*client
= assuan_get_pointer (ctx
);
2712 struct xpath_s _x
= { 0 };
2713 struct xpath_s
*xpath
= &_x
;
2715 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2717 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2718 return GPG_ERR_ENOMEM
;
2721 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2724 rc
= GPG_ERR_BAD_DATA
;
2729 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2732 rc
= GPG_ERR_BAD_DATA
;
2736 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2738 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2742 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2743 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2747 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2749 rc
= GPG_ERR_NO_DATA
;
2752 else if (xpath
->req
[1])
2758 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2759 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2760 xmlBufferLength (xpath
->buf
));
2761 pthread_cleanup_pop (0);
2763 xpath_command_cleanup (xpath
);
2768 xpath_command (assuan_context_t ctx
, char *line
)
2770 struct client_s
*client
= assuan_get_pointer (ctx
);
2772 struct argv_s
*args
[] = {
2773 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2777 if (disable_list_and_dump
== 1)
2778 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2780 rc
= peer_is_invoker(client
);
2782 return send_error (ctx
, rc
);
2784 rc
= parse_options (&line
, args
, client
);
2786 return send_error (ctx
, rc
);
2788 if (client
->opts
& OPT_INQUIRE
)
2790 unsigned char *result
;
2793 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2795 return send_error (ctx
, rc
);
2797 line
= (char *) result
;
2800 if (!line
|| !*line
)
2801 rc
= GPG_ERR_SYNTAX
;
2804 rc
= do_xpath (ctx
, line
);
2806 if (client
->opts
& OPT_INQUIRE
)
2809 return send_error (ctx
, rc
);
2813 do_xpathattr (assuan_context_t ctx
, char *line
)
2815 struct client_s
*client
= assuan_get_pointer (ctx
);
2819 struct xpath_s _x
= { 0 };
2820 struct xpath_s
*xpath
= &_x
;
2822 if ((req
= str_split (line
, " ", 3)) == NULL
)
2823 return GPG_ERR_ENOMEM
;
2827 rc
= GPG_ERR_SYNTAX
;
2831 if (!strcasecmp (req
[0], "SET"))
2833 else if (!strcasecmp (req
[0], "DELETE"))
2837 rc
= GPG_ERR_SYNTAX
;
2841 if (!req
[1] || !req
[2])
2843 rc
= GPG_ERR_SYNTAX
;
2847 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2849 rc
= GPG_ERR_ENOMEM
;
2853 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2855 rc
= GPG_ERR_SYNTAX
;
2859 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2862 rc
= GPG_ERR_BAD_DATA
;
2866 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2869 rc
= GPG_ERR_BAD_DATA
;
2873 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2875 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2879 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2880 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2881 (xmlChar
*) req
[1]);
2884 xpath_command_cleanup (xpath
);
2889 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2891 xpathattr_command (assuan_context_t ctx
, char *line
)
2893 struct client_s
*client
= assuan_get_pointer (ctx
);
2895 struct argv_s
*args
[] = {
2896 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2900 if (disable_list_and_dump
== 1)
2901 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2903 rc
= peer_is_invoker(client
);
2905 return send_error (ctx
, rc
);
2907 rc
= parse_options (&line
, args
, client
);
2909 return send_error (ctx
, rc
);
2911 if (client
->opts
& OPT_INQUIRE
)
2913 unsigned char *result
;
2916 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2918 return send_error (ctx
, rc
);
2920 line
= (char *) result
;
2923 if (!line
|| !*line
)
2924 rc
= GPG_ERR_SYNTAX
;
2927 rc
= do_xpathattr (ctx
, line
);
2929 if (client
->opts
& OPT_INQUIRE
)
2932 return send_error (ctx
, rc
);
2936 do_import (struct client_s
*client
, const char *root_element
,
2937 unsigned char *content
)
2939 char **dst_path
= NULL
;
2940 xmlDocPtr doc
= NULL
;
2941 xmlNodePtr n
, root
, copy
;
2944 if (!content
|| !*content
)
2947 return GPG_ERR_SYNTAX
;
2951 dst_path
= str_split (root_element
, "\t", 0);
2953 if (dst_path
&& !valid_element_path (dst_path
, 0))
2956 strv_free (dst_path
);
2958 return GPG_ERR_INV_VALUE
;
2961 struct string_s
*str
= string_new_content ((char *)content
);
2962 str
= string_prepend (str
, "<pwmd>");
2963 str
= string_append (str
, "</pwmd>");
2964 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2965 string_free (str
, 1);
2968 rc
= GPG_ERR_BAD_DATA
;
2972 root
= xmlDocGetRootElement (doc
);
2973 xmlNodePtr root_orig
= root
->children
;
2974 root
= root
->children
;
2975 rc
= validate_import (client
, root
);
2984 char **path
= strv_dup (dst_path
);
2987 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2988 pwmd_strerror (GPG_ERR_ENOMEM
));
2989 rc
= GPG_ERR_ENOMEM
;
2993 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
2997 rc
= GPG_ERR_INV_VALUE
;
3001 if (strv_printf (&path
, "%s", (char *) a
) == 0)
3004 rc
= GPG_ERR_ENOMEM
;
3009 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
3010 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3018 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
3019 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
3020 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3035 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
3037 n
= create_element_path (client
, &path
, &rc
, NULL
);
3044 copy
= xmlCopyNodeList (root
->children
);
3045 n
= xmlAddChildList (n
, copy
);
3047 rc
= GPG_ERR_ENOMEM
;
3056 /* Check if the content root element can create a DTD root element. */
3057 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
3059 rc
= GPG_ERR_SYNTAX
;
3065 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
3067 rc
= GPG_ERR_SYNTAX
;
3071 char *tmp
= str_dup ((char *) a
);
3073 int literal
= is_literal_element (&tmp
);
3075 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
3078 rc
= GPG_ERR_INV_VALUE
;
3082 if (strv_printf (&path
, "%s", tmp
) == 0)
3085 rc
= GPG_ERR_ENOMEM
;
3090 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
3091 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3093 rc
= GPG_ERR_BAD_DATA
;
3097 /* Overwriting the existing tree. */
3101 xmlFreeNodeList (n
);
3105 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
3106 n
= xmlCopyNode (root
, 1);
3107 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3112 rc
= update_element_mtime (client
, n
->parent
);
3114 for (root
= root_orig
->next
; root
; root
= root
->next
)
3116 if (root
->type
== XML_ELEMENT_NODE
)
3129 strv_free (dst_path
);
3135 parse_import_opt_root (void *data
, void *value
)
3137 struct client_s
*client
= data
;
3139 client
->import_root
= str_dup (value
);
3140 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3144 import_command (assuan_context_t ctx
, char *line
)
3147 struct client_s
*client
= assuan_get_pointer (ctx
);
3148 unsigned char *result
;
3150 struct argv_s
*args
[] = {
3151 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3155 xfree (client
->import_root
);
3156 client
->import_root
= NULL
;
3157 rc
= parse_options (&line
, args
, client
);
3159 return send_error (ctx
, rc
);
3161 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3164 xfree (client
->import_root
);
3165 client
->import_root
= NULL
;
3166 return send_error (ctx
, rc
);
3169 rc
= do_import (client
, client
->import_root
, result
);
3170 xfree (client
->import_root
);
3171 client
->import_root
= NULL
;
3172 return send_error (ctx
, rc
);
3176 do_lock (struct client_s
*client
, int add
)
3178 gpg_error_t rc
= lock_file_mutex (client
, add
);
3181 client
->flags
|= FLAG_LOCK_CMD
;
3187 lock_command (assuan_context_t ctx
, char *line
)
3189 struct client_s
*client
= assuan_get_pointer (ctx
);
3190 gpg_error_t rc
= do_lock (client
, 0);
3192 return send_error (ctx
, rc
);
3196 unlock_command (assuan_context_t ctx
, char *line
)
3198 struct client_s
*client
= assuan_get_pointer (ctx
);
3201 rc
= unlock_file_mutex (client
, 0);
3202 return send_error (ctx
, rc
);
3206 option_command (assuan_context_t ctx
, char *line
)
3208 struct client_s
*client
= assuan_get_pointer (ctx
);
3210 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3212 struct agent_s
*agent
= client
->crypto
->agent
;
3214 char namebuf
[255] = { 0 };
3215 char *name
= namebuf
;
3216 char *value
= NULL
, *p
;
3218 p
= strchr (line
, '=');
3220 strncpy (namebuf
, line
, sizeof(namebuf
));
3223 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3227 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3229 if (strcasecmp (name
, (char *) "log_level") == 0)
3235 l
= strtol (value
, NULL
, 10);
3238 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3241 MUTEX_LOCK (&rcfile_mutex
);
3242 config_set_int_param (&global_config
, "global", "log_level", value
);
3243 MUTEX_UNLOCK (&rcfile_mutex
);
3246 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3253 n
= strtol (value
, &p
, 10);
3255 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3258 client
->lock_timeout
= n
;
3261 else if (strcasecmp (name
, (char *) "NAME") == 0)
3263 char *tmp
= pthread_getspecific (thread_name_key
);
3268 if (!value
|| !*value
)
3270 pthread_setspecific (thread_name_key
, str_dup (""));
3274 pthread_setspecific (thread_name_key
, str_dup (value
));
3277 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3279 xfree (pin_opts
->lc_messages
);
3280 pin_opts
->lc_messages
= NULL
;
3281 if (value
&& *value
)
3282 pin_opts
->lc_messages
= str_dup (value
);
3285 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3288 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3290 xfree (pin_opts
->lc_ctype
);
3291 pin_opts
->lc_ctype
= NULL
;
3292 if (value
&& *value
)
3293 pin_opts
->lc_ctype
= str_dup (value
);
3296 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3299 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3301 xfree (pin_opts
->ttyname
);
3302 pin_opts
->ttyname
= NULL
;
3303 if (value
&& *value
)
3304 pin_opts
->ttyname
= str_dup (value
);
3307 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3310 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3312 xfree (pin_opts
->ttytype
);
3313 pin_opts
->ttytype
= NULL
;
3314 if (value
&& *value
)
3315 pin_opts
->ttytype
= str_dup (value
);
3318 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3321 else if (strcasecmp (name
, (char *) "display") == 0)
3323 xfree (pin_opts
->display
);
3324 pin_opts
->display
= NULL
;
3325 if (value
&& *value
)
3326 pin_opts
->display
= str_dup (value
);
3329 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3332 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3334 xfree (pin_opts
->desc
);
3335 pin_opts
->desc
= NULL
;
3336 if (value
&& *value
)
3337 pin_opts
->desc
= str_dup (value
);
3339 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3341 xfree (pin_opts
->title
);
3342 pin_opts
->title
= NULL
;
3343 if (value
&& *value
)
3344 pin_opts
->title
= str_dup (value
);
3346 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3348 xfree (pin_opts
->prompt
);
3349 pin_opts
->prompt
= NULL
;
3350 if (value
&& *value
)
3351 pin_opts
->prompt
= str_dup (value
);
3354 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3362 n
= (int) strtol (value
, &p
, 10);
3365 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3367 pin_opts
->timeout
= n
;
3368 MUTEX_LOCK (&rcfile_mutex
);
3369 config_set_int_param (&global_config
,
3370 client
->filename
? client
->filename
: "global",
3371 "pinentry_timeout", value
);
3372 MUTEX_UNLOCK (&rcfile_mutex
);
3375 else if (strcasecmp (name
, "disable-pinentry") == 0)
3380 if (value
&& *value
)
3382 n
= (int) strtol (value
, &p
, 10);
3383 if (*p
|| n
< 0 || n
> 1)
3384 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3388 client
->flags
|= FLAG_NO_PINENTRY
;
3390 client
->flags
&= ~FLAG_NO_PINENTRY
;
3395 if (client
->flags
& FLAG_NO_PINENTRY
)
3396 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3399 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3403 return send_error (ctx
, rc
);
3408 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3412 if (!rc
&& use_agent
&& agent
)
3414 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3419 return send_error (ctx
, rc
);
3423 do_rename (assuan_context_t ctx
, char *line
)
3425 struct client_s
*client
= assuan_get_pointer (ctx
);
3427 char **req
, **src
, *dst
;
3430 req
= str_split (line
, " ", 0);
3432 if (!req
|| !req
[0] || !req
[1])
3435 return GPG_ERR_SYNTAX
;
3439 is_literal_element (&dst
);
3441 if (!valid_xml_element ((xmlChar
*) dst
))
3444 return GPG_ERR_INV_VALUE
;
3447 if (strchr (req
[0], '\t'))
3448 src
= str_split (req
[0], "\t", 0);
3450 src
= str_split (req
[0], " ", 0);
3454 rc
= GPG_ERR_SYNTAX
;
3458 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3460 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3461 NULL
, 0, 0, NULL
, 0);
3466 rc
= is_element_owner (client
, n
);
3470 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3473 rc
= GPG_ERR_ENOMEM
;
3477 /* To prevent unwanted effects:
3479 * <root name="a"><b/></root>
3483 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3486 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3496 for (p
= src
; *p
; p
++)
3500 strv_printf (&tmp
, "%s", *p
);
3504 strv_printf (&tmp
, "!%s", dst
);
3505 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3506 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3513 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3514 NULL
, NULL
, 0, 0, NULL
, 0);
3517 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3522 /* Target may exist:
3525 * <root name="b" target="a"/>
3534 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3540 rc
= is_element_owner (client
, ndst
);
3544 unlink_node (client
, ndst
);
3545 xmlFreeNodeList (ndst
);
3548 rc
= add_attribute (client
, n
, "_name", dst
);
3557 rename_command (assuan_context_t ctx
, char *line
)
3559 struct client_s
*client
= assuan_get_pointer (ctx
);
3561 struct argv_s
*args
[] = {
3562 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3566 rc
= parse_options (&line
, args
, client
);
3568 return send_error (ctx
, rc
);
3570 if (client
->opts
& OPT_INQUIRE
)
3572 unsigned char *result
;
3575 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3577 return send_error (ctx
, rc
);
3579 line
= (char *) result
;
3582 rc
= do_rename (ctx
, line
);
3584 if (client
->opts
& OPT_INQUIRE
)
3587 return send_error (ctx
, rc
);
3591 do_copy (assuan_context_t ctx
, char *line
)
3593 struct client_s
*client
= assuan_get_pointer (ctx
);
3595 char **req
, **src
= NULL
, **dst
= NULL
;
3596 xmlNodePtr nsrc
, ndst
, new = NULL
;
3598 req
= str_split (line
, " ", 0);
3599 if (!req
|| !req
[0] || !req
[1])
3602 return GPG_ERR_SYNTAX
;
3605 if (strchr (req
[0], '\t'))
3606 src
= str_split (req
[0], "\t", 0);
3608 src
= str_split (req
[0], " ", 0);
3612 rc
= GPG_ERR_SYNTAX
;
3616 if (strchr (req
[1], '\t'))
3617 dst
= str_split (req
[1], "\t", 0);
3619 dst
= str_split (req
[1], " ", 0);
3623 rc
= GPG_ERR_SYNTAX
;
3627 if (!valid_element_path (dst
, 0))
3629 rc
= GPG_ERR_INV_VALUE
;
3633 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3635 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3636 NULL
, NULL
, 0, 0, NULL
, 0);
3641 new = xmlCopyNodeList (nsrc
);
3644 rc
= GPG_ERR_ENOMEM
;
3649 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3653 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3654 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3661 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3663 else if (!ndst
|| create
)
3665 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3670 rc
= is_element_owner (client
, ndst
);
3674 /* Merge any attributes from the src node to the initial dst node. */
3675 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3677 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3680 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3684 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3685 xmlNewProp (ndst
, attr
->name
, tmp
);
3687 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3690 xmlNodePtr n
= ndst
->children
;
3692 xmlFreeNodeList (n
);
3693 ndst
->children
= NULL
;
3697 n
= xmlCopyNodeList (new->children
);
3700 rc
= GPG_ERR_ENOMEM
;
3704 n
= xmlAddChildList (ndst
, n
);
3707 rc
= GPG_ERR_ENOMEM
;
3711 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3712 ndst
->parent
? ndst
: ndst
->parent
);
3718 xmlUnlinkNode (new);
3719 xmlFreeNodeList (new);
3735 copy_command (assuan_context_t ctx
, char *line
)
3737 struct client_s
*client
= assuan_get_pointer (ctx
);
3739 struct argv_s
*args
[] = {
3740 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3744 rc
= parse_options (&line
, args
, client
);
3746 return send_error (ctx
, rc
);
3748 if (client
->opts
& OPT_INQUIRE
)
3750 unsigned char *result
;
3753 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3755 return send_error (ctx
, rc
);
3757 line
= (char *) result
;
3760 rc
= do_copy (ctx
, line
);
3762 if (client
->opts
& OPT_INQUIRE
)
3765 return send_error (ctx
, rc
);
3769 do_move (assuan_context_t ctx
, char *line
)
3771 struct client_s
*client
= assuan_get_pointer (ctx
);
3773 char **req
, **src
= NULL
, **dst
= NULL
;
3774 xmlNodePtr nsrc
, ndst
= NULL
;
3776 req
= str_split (line
, " ", 0);
3778 if (!req
|| !req
[0] || !req
[1])
3781 return GPG_ERR_SYNTAX
;
3784 if (strchr (req
[0], '\t'))
3785 src
= str_split (req
[0], "\t", 0);
3787 src
= str_split (req
[0], " ", 0);
3791 rc
= GPG_ERR_SYNTAX
;
3795 if (strchr (req
[1], '\t'))
3796 dst
= str_split (req
[1], "\t", 0);
3798 dst
= str_split (req
[1], " ", 0);
3800 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3802 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3803 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3808 rc
= is_element_owner (client
, nsrc
);
3814 if (!valid_element_path (dst
, 0))
3816 rc
= GPG_ERR_INV_VALUE
;
3820 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3822 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3823 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3826 ndst
= xmlDocGetRootElement (client
->doc
);
3828 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3832 rc
= GPG_ERR_CONFLICT
;
3837 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3844 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3846 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
3853 rc
= is_element_owner (client
, ndst
);
3862 if (ndst
== xmlDocGetRootElement (client
->doc
))
3864 xmlNodePtr n
= nsrc
;
3867 while (n
->parent
&& n
->parent
!= ndst
)
3870 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3871 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3873 if (xmlStrEqual (a
, b
))
3876 xmlUnlinkNode (nsrc
);
3878 xmlFreeNodeList (n
);
3886 xmlUnlinkNode (dup
);
3887 xmlFreeNodeList (dup
);
3891 xmlUnlinkNode (dup
);
3897 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3899 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3900 && !strcmp ((char *) name
, *dst
))
3903 rc
= GPG_ERR_CONFLICT
;
3908 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3914 update_element_mtime (client
, nsrc
->parent
);
3915 xmlUnlinkNode (nsrc
);
3916 ndst
= xmlAddChildList (ndst
, nsrc
);
3919 rc
= GPG_ERR_ENOMEM
;
3921 update_element_mtime (client
, ndst
->parent
);
3937 move_command (assuan_context_t ctx
, char *line
)
3939 struct client_s
*client
= assuan_get_pointer (ctx
);
3941 struct argv_s
*args
[] = {
3942 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3946 rc
= parse_options (&line
, args
, client
);
3948 return send_error (ctx
, rc
);
3950 if (client
->opts
& OPT_INQUIRE
)
3952 unsigned char *result
;
3955 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3957 return send_error (ctx
, rc
);
3959 line
= (char *) result
;
3962 rc
= do_move (ctx
, line
);
3964 if (client
->opts
& OPT_INQUIRE
)
3967 return send_error (ctx
, rc
);
3971 ls_command (assuan_context_t ctx
, char *line
)
3974 char *tmp
= str_asprintf ("%s/data", homedir
);
3975 char *dir
= expand_homedir (tmp
);
3976 DIR *d
= opendir (dir
);
3978 rc
= gpg_error_from_errno (errno
);
3984 return send_error (ctx
, rc
);
3988 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3989 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3995 while (!readdir_r (d
, p
, &cur
) && cur
)
3997 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
3999 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
4000 && cur
->d_name
[2] == '\0')
4003 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
4010 rc
= GPG_ERR_ENOMEM
;
4022 return send_error (ctx
, rc
);
4025 return send_error (ctx
, GPG_ERR_NO_DATA
);
4027 list
[strlen (list
) - 1] = 0;
4028 rc
= xfer_data (ctx
, list
, strlen (list
));
4030 return send_error (ctx
, rc
);
4034 bye_notify (assuan_context_t ctx
, char *line
)
4036 struct client_s
*cl
= assuan_get_pointer (ctx
);
4039 if (cl
->thd
->remote
)
4045 struct timeval tv
= { 0, 50000 };
4047 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
4048 if (rc
== GNUTLS_E_AGAIN
)
4049 select (0, NULL
, NULL
, NULL
, &tv
);
4051 while (rc
== GNUTLS_E_AGAIN
);
4055 /* This will let assuan_process_next() return. */
4056 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
4057 cl
->last_rc
= 0; // BYE command result
4062 reset_notify (assuan_context_t ctx
, char *line
)
4064 struct client_s
*client
= assuan_get_pointer (ctx
);
4067 cleanup_client (client
);
4073 * This is called before every Assuan command.
4076 command_startup (assuan_context_t ctx
, const char *name
)
4078 struct client_s
*client
= assuan_get_pointer (ctx
);
4080 struct command_table_s
*cmd
= NULL
;
4082 log_write1 ("command='%s'", name
);
4083 client
->last_rc
= client
->opts
= 0;
4085 for (int i
= 0; command_table
[i
]; i
++)
4087 if (!strcasecmp (name
, command_table
[i
]->name
))
4089 if (command_table
[i
]->ignore_startup
)
4091 cmd
= command_table
[i
];
4096 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4101 * This is called after every Assuan command.
4104 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4106 struct client_s
*client
= assuan_get_pointer (ctx
);
4108 if (!(client
->flags
& FLAG_LOCK_CMD
))
4109 unlock_file_mutex (client
, 0);
4111 log_write1 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4112 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
4114 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4119 help_command (assuan_context_t ctx
, char *line
)
4124 if (!line
|| !*line
)
4127 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
4128 "For commands that take an element path as an argument, each element is "
4129 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4132 for (i
= 0; command_table
[i
]; i
++)
4134 if (!command_table
[i
]->help
)
4137 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4142 tmp
= strip_texi_and_wrap (help
);
4144 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4146 return send_error (ctx
, rc
);
4149 for (i
= 0; command_table
[i
]; i
++)
4151 if (!strcasecmp (line
, command_table
[i
]->name
))
4155 if (!command_table
[i
]->help
)
4158 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4159 tmp
= str_asprintf (_("Usage: %s"), help
);
4161 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4163 return send_error (ctx
, rc
);
4167 return send_error (ctx
, GPG_ERR_INV_NAME
);
4171 new_command (const char *name
, int ignore
, int unlock
,
4172 gpg_error_t (*handler
) (assuan_context_t
, char *),
4178 for (i
= 0; command_table
[i
]; i
++);
4181 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4182 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4183 command_table
[i
]->name
= name
;
4184 command_table
[i
]->handler
= handler
;
4185 command_table
[i
]->ignore_startup
= ignore
;
4186 command_table
[i
]->unlock
= unlock
;
4187 command_table
[i
++]->help
= help
;
4188 command_table
[i
] = NULL
;
4196 for (i
= 0; command_table
[i
]; i
++)
4197 xfree (command_table
[i
]);
4199 xfree (command_table
);
4203 sort_commands (const void *arg1
, const void *arg2
)
4205 struct command_table_s
*const *a
= arg1
;
4206 struct command_table_s
*const *b
= arg2
;
4215 return strcmp ((*a
)->name
, (*b
)->name
);
4219 passwd_command (assuan_context_t ctx
, char *line
)
4221 struct client_s
*client
= assuan_get_pointer (ctx
);
4223 struct argv_s
*args
[] = {
4224 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4225 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4226 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4230 rc
= peer_is_invoker (client
);
4231 if (rc
== GPG_ERR_EACCES
)
4232 return send_error (ctx
, rc
);
4234 if (client
->flags
& FLAG_NEW
)
4235 return send_error (ctx
, GPG_ERR_INV_STATE
);
4237 client
->crypto
->save
.s2k_count
=
4238 config_get_ulong (client
->filename
, "s2k_count");
4239 rc
= parse_options (&line
, args
, client
);
4241 return send_error (ctx
, rc
);
4243 if (!rc
&& client
->opts
& OPT_RESET
)
4245 rc
= cache_clear (client
->md5file
);
4247 send_status_all (STATUS_CACHE
, NULL
);
4252 if (!IS_PKI (client
->crypto
))
4254 struct crypto_s
*crypto
;
4256 xfree (client
->crypto
->filename
);
4257 client
->crypto
->filename
= str_dup (client
->filename
);
4258 rc
= change_passwd (ctx
, client
->filename
,
4259 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4260 (client
->opts
& OPT_NO_PASSPHRASE
));
4263 cleanup_crypto (&client
->crypto
);
4264 client
->crypto
= crypto
;
4265 update_checksum (client
);
4266 cleanup_crypto_stage1 (client
->crypto
);
4272 if (client
->crypto
->save
.s2k_count
)
4273 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4274 "OPTION s2k-count=%lu",
4275 client
->crypto
->save
.s2k_count
);
4278 rc
= agent_passwd (client
->crypto
);
4283 return send_error (ctx
, rc
);
4287 parse_keygrip_opt_sign (void *data
, void *value
)
4289 struct client_s
*client
= data
;
4292 client
->opts
|= OPT_SIGN
;
4297 keygrip_command (assuan_context_t ctx
, char *line
)
4299 struct client_s
*client
= assuan_get_pointer (ctx
);
4301 struct crypto_s
*crypto
= NULL
;
4302 struct argv_s
*args
[] = {
4303 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4307 if (!line
|| !*line
)
4308 return send_error (ctx
, GPG_ERR_SYNTAX
);
4310 rc
= parse_options (&line
, args
, client
);
4312 return send_error (ctx
, rc
);
4314 if (!valid_filename (line
))
4315 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4317 rc
= init_client_crypto (&crypto
);
4319 return send_error (ctx
, rc
);
4321 rc
= read_data_file (line
, crypto
);
4324 char *hexgrip
= NULL
;
4326 if (!IS_PKI (crypto
))
4328 cleanup_crypto (&crypto
);
4329 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4332 if (client
->opts
& OPT_SIGN
)
4334 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4335 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4339 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4342 rc
= GPG_ERR_ENOMEM
;
4344 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4349 UPDATE_AGENT_CTX (client
, crypto
);
4350 cleanup_crypto (&crypto
);
4351 return send_error (ctx
, rc
);
4355 parse_opt_data (void *data
, void *value
)
4357 struct client_s
*client
= data
;
4360 client
->opts
|= OPT_DATA
;
4365 getinfo_command (assuan_context_t ctx
, char *line
)
4367 struct client_s
*client
= assuan_get_pointer (ctx
);
4369 char buf
[ASSUAN_LINELENGTH
];
4370 struct argv_s
*args
[] = {
4371 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4375 rc
= parse_options (&line
, args
, client
);
4377 return send_error (ctx
, rc
);
4379 if (!strcasecmp (line
, "clients"))
4381 if (client
->opts
& OPT_DATA
)
4383 MUTEX_LOCK (&cn_mutex
);
4384 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4385 MUTEX_UNLOCK (&cn_mutex
);
4386 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4389 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4391 else if (!strcasecmp (line
, "cache"))
4393 if (client
->opts
& OPT_DATA
)
4395 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4396 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4399 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4401 else if (!strcasecmp (line
, "pid"))
4404 pid_t pid
= getpid ();
4406 snprintf (buf
, sizeof (buf
), "%i", pid
);
4407 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4409 else if (!strcasecmp (line
, "version"))
4411 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4418 "", use_agent
? "AGENT" : "");
4419 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4422 else if (!strcasecmp (line
, "last_error"))
4424 if (client
->last_error
)
4425 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4427 rc
= GPG_ERR_NO_DATA
;
4430 rc
= gpg_error (GPG_ERR_SYNTAX
);
4432 return send_error (ctx
, rc
);
4437 send_data_cb (void *user
, const void *buf
, size_t len
)
4439 assuan_context_t ctx
= user
;
4441 return assuan_send_data (ctx
, buf
, len
);
4445 send_status_cb (void *user
, const char *line
)
4447 assuan_context_t ctx
= user
;
4448 char keyword
[200], *k
;
4451 for (p
= line
, k
= keyword
; *p
; p
++)
4463 while (isspace (*p
))
4466 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4471 agent_command (assuan_context_t ctx
, char *line
)
4476 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4479 struct client_s
*client
= assuan_get_pointer (ctx
);
4481 if (!line
|| !*line
)
4482 return send_error (ctx
, GPG_ERR_SYNTAX
);
4484 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4485 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4486 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4487 send_status_cb
, client
->ctx
);
4488 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4493 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4496 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4498 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4502 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4504 return send_error (ctx
, rc
);
4510 /* !BEGIN-HELP-TEXT!
4512 * This comment is used as a marker to generate the offline documentation
4513 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4514 * script to determine where commands begin and end.
4516 new_command("HELP", 1, 1, help_command
, _(
4517 "HELP [<COMMAND>]\n"
4518 "Show available commands or command specific help text."
4521 new_command("AGENT", 1, 1, agent_command
, _(
4523 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4524 "@command{gpg-agent}."
4527 new_command("GETINFO", 1, 1, getinfo_command
, _(
4528 "GETINFO [--data] CACHE | CLIENTS | PID | LAST_ERROR | VERSION\n"
4529 "Get server and other information: @var{cache} returns the number of cached "
4530 "documents via a status message. @var{clients} returns the number of "
4531 "connected clients via a status message. @var{pid} returns the process ID "
4532 "number of the server via a data response. @var{VERSION} returns the server "
4533 "version number and compile-time features with a data response with each "
4534 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4535 "the last failed command when available. @xref{Status Messages}. "
4537 "When the @option{--data} option is specified then the result will be sent "
4538 "via a data response rather than a status message."
4541 new_command("PASSWD", 0, 0, passwd_command
, _(
4542 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4543 "Changes the passphrase of the secret key required to open the current "
4544 "file or the passphrase of a symmetrically encrypted data file. When the "
4545 "@option{--reset} option is passed then the cache entry for the current "
4546 "file will be reset and the passphrase, if any, will be required during the "
4547 "next @code{OPEN} (@pxref{OPEN})."
4549 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4550 "of hash iterations for a passphrase and must be either @code{0} to use "
4551 "the calibrated count of the machine (the default), or a value greater than "
4552 "or equal to @code{65536}. This option has no effect for symmetrically "
4553 "encrypted data files."
4555 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4556 "the data file, although a passphrase may be required when changing it."
4558 "This command is not available for non-invoking clients "
4559 "(@pxref{Access Control})."
4562 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4563 "KEYGRIP [--sign] <filename>\n"
4564 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4567 "When the @option{--sign} option is specified then the key used for signing "
4568 "of the specified @var{filename} will be returned."
4570 "For symmetrically encrypted data files this command returns the error "
4571 "GPG_ERR_NOT_SUPPORTED."
4574 new_command("OPEN", 1, 1, open_command
, _(
4575 "OPEN [--lock] <filename> [<passphrase>]\n"
4576 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4577 "found on the file-system then a new document will be created. If the file "
4578 "is found, it is looked for in the file cache. If cached and no "
4579 "@var{passphrase} was specified then the cached document is opened. When not "
4580 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4581 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4584 "When the @option{--lock} option is passed then the file mutex will be "
4585 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4586 "file has been opened."
4589 new_command("SAVE", 0, 0, save_command
, _(
4590 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4591 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4592 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4593 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4594 "keypair will be generated and a pinentry will be used to prompt for the "
4595 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4596 "passed in which case the data file will not be passphrase protected. "
4598 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4599 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4600 "use is enabled. The datafile will be symmetrically encrypted and will not "
4601 "use or generate any keypair."
4603 "The @option{--reset} option will clear the cache entry for the current file "
4604 "and require a passphrase, if needed, before saving."
4606 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4607 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4608 "(@pxref{Configuration}) for available ciphers."
4610 "The @option{--cipher-iterations} option specifies the number of times to "
4611 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4613 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4614 "the client to obtain the key paramaters to use when generating a new "
4615 "keypair. The inquired data is expected to be an S-expression. If not "
4616 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4617 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4618 "that when this option is specified a new keypair will be generated "
4619 "reguardless if the file is a new one and that if the data file is protected "
4620 "the passphrase to open it will be required before generating the new "
4621 "keypair. This option is not available for non-invoking clients "
4622 "(@pxref{Access Control})."
4624 "You can encrypt the data file to a public key other than the one that it "
4625 "was originally encrypted with by passing the @option{--keygrip} option with "
4626 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4627 "be of any key that @command{gpg-agent} knows about. The "
4628 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4629 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4630 "keygrip of an existing data file. This option may be needed when using a "
4631 "smartcard. This option has no effect with symmetrically encrypted data "
4632 "files. These options are not available for non-invoking clients "
4633 "(@pxref{Access Control})."
4635 "The @option{--s2k-count} option sets number of hash iterations for a "
4636 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4637 "value and is the default. This setting only affects new files. To change "
4638 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4639 "has no effect with symmetrically encrypted data files."
4642 new_command("ISCACHED", 1, 0, iscached_command
, _(
4643 "ISCACHED [--lock] <filename>\n"
4644 "An @emph{OK} response is returned if the specified @var{filename} is found "
4645 "in the file cache. If not found in the cache but exists on the filesystem "
4646 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4649 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4650 "file exists; it does not need to be opened nor cached. The lock will be "
4651 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4655 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4656 "CLEARCACHE [<filename>]\n"
4657 "Clears a file cache entry for all or the specified @var{filename}."
4660 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4661 "CACHETIMEOUT <filename> <seconds>\n"
4662 "The time in @var{seconds} until @var{filename} will be removed from the "
4663 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4664 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4665 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4669 new_command("LIST", 0, 1, list_command
, _(
4670 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4671 "If no element path is given then a newline separated list of root elements "
4672 "is returned with a data response. If given, then all reachable elements "
4673 "of the specified element path are returned unless the @option{--no-recurse} "
4674 "option is specified. If specified, only the child elements of the element "
4675 "path are returned without recursing into grandchildren. Each resulting "
4676 "element is prefixed with the literal @code{!} character when the element "
4677 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4679 "When the @option{--verbose} option is passed then each element path "
4680 "returned will have zero or more flags appened to it. These flags are "
4681 "delimited from the element path by a single space character. A flag itself "
4682 "is a single character. Flag @code{P} indicates that access to the element "
4683 "is denied. Flag @code{+} indicates that there are child nodes of "
4684 "the current element path. Flag @code{E} indicates that an element of an "
4685 "element path contained in a @var{target} attribute could not be found. Flag "
4686 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4687 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4688 "of the @var{target} attribute contained in the current element (see below)."
4690 "The @option{--with-target} option implies @option{--verbose} and will append "
4691 "an additional flag @code{T} followed by a single space then an element path. "
4692 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4693 "current element when it contains a @var{target} attribute. When no "
4694 "@var{target} attribute is found then no flag will be appended."
4696 "The @option{--no-recurse} option limits the amount of data returned to only "
4697 "the listing of children of the specified element path and not any "
4700 "The @option{--all} option lists the entire element tree for each root "
4701 "element. This option also implies option @option{--verbose}."
4703 "When the @option{--inquire} option is passed then all remaining non-option "
4704 "arguments are retrieved via a server @emph{INQUIRE}."
4707 new_command("REALPATH", 0, 1, realpath_command
, _(
4708 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4709 "Resolves all @code{target} attributes of the specified element path and "
4710 "returns the result with a data response. @xref{Target Attribute}, for details."
4712 "When the @option{--inquire} option is passed then all remaining non-option "
4713 "arguments are retrieved via a server @emph{INQUIRE}."
4716 new_command("STORE", 0, 1, store_command
, _(
4717 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4718 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4720 "Creates a new element path or modifies the @var{content} of an existing "
4721 "element. If only a single element is specified then a new root element is "
4722 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4723 "set to the final @key{TAB} delimited element. If no @var{content} is "
4724 "specified after the final @key{TAB}, then the content of an existing "
4725 "element will be removed; or empty when creating a new element."
4727 "The only restriction of an element name is that it not contain whitespace "
4728 "or begin with the literal element character @code{!} unless specifying a "
4729 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4730 "the @key{TAB} delimited elements. It is recommended that the content of an "
4731 "element be base64 encoded when it contains control or @key{TAB} characters "
4732 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4735 new_command("RENAME", 0, 1, rename_command
, _(
4736 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4737 "Renames the specified @var{element} to the new @var{value}. If an element of "
4738 "the same name as the @var{value} already exists it will be overwritten."
4740 "When the @option{--inquire} option is passed then all remaining non-option "
4741 "arguments are retrieved via a server @emph{INQUIRE}."
4744 new_command("COPY", 0, 1, copy_command
, _(
4745 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4746 "Copies the entire element tree starting from the child node of the source "
4747 "element, to the destination element path. If the destination element path "
4748 "does not exist then it will be created; otherwise it is overwritten."
4750 "Note that attributes from the source element are merged into the "
4751 "destination element when the destination element path exists. When an "
4752 "attribute of the same name exists in both the source and destination "
4753 "elements then the destination attribute will be updated to the source "
4756 "When the @option{--inquire} option is passed then all remaining non-option "
4757 "arguments are retrieved via a server @emph{INQUIRE}."
4760 new_command("MOVE", 0, 1, move_command
, _(
4761 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4762 "Moves the source element path to the destination element path. If the "
4763 "destination is not specified then it will be moved to the root node of the "
4764 "document. If the destination is specified and exists then it will be "
4765 "overwritten; otherwise non-existing elements of the destination element "
4766 "path will be created."
4768 "When the @option{--inquire} option is passed then all remaining non-option "
4769 "arguments are retrieved via a server @emph{INQUIRE}."
4772 new_command("DELETE", 0, 1, delete_command
, _(
4773 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4774 "Removes the specified element path and all of its children. This may break "
4775 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4776 "refers to this element or any of its children."
4778 "When the @option{--inquire} option is passed then all remaining non-option "
4779 "arguments are retrieved via a server @emph{INQUIRE}."
4782 new_command("GET", 0, 1, get_command
, _(
4783 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4784 "Retrieves the content of the specified element. The content is returned "
4785 "with a data response."
4787 "When the @option{--inquire} option is passed then all remaining non-option "
4788 "arguments are retrieved via a server @emph{INQUIRE}."
4791 new_command("ATTR", 0, 1, attr_command
, _(
4792 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4794 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4796 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4797 "element. When no @var{value} is specified any existing value will be removed."
4799 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4801 " Removes an @var{attribute} from an element."
4803 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4805 " Retrieves a newline separated list of attributes names and values "
4806 "from the specified element. Each attribute name and value is space delimited."
4808 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4810 " Retrieves the value of an @var{attribute} from an element."
4813 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4814 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4817 "The @code{_mtime} attribute is updated each time an element is modified by "
4818 "either storing content, editing attributes or by deleting a child element. "
4819 "The @code{_ctime} attribute is created for each new element in an element "
4822 "When the @option{--inquire} option is passed then all remaining non-option "
4823 "arguments are retrieved via a server @emph{INQUIRE}."
4825 "@xref{Target Attribute}, for details about this special attribute."
4828 new_command("XPATH", 0, 1, xpath_command
, _(
4829 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4830 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4831 "specified it is assumed the expression is a request to return a result. "
4832 "Otherwise, the result is set to the @var{value} argument and the document is "
4833 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4834 "is assumed to be empty and the document is updated. For example:"
4837 "XPATH //element[@@_name='password']@key{TAB}\n"
4840 "would clear the content of all @code{password} elements in the data file "
4841 "while leaving off the trailing @key{TAB} would return all @code{password} "
4842 "elements in @abbr{XML} format."
4844 "When the @option{--inquire} option is passed then all remaining non-option "
4845 "arguments are retrieved via a server @emph{INQUIRE}."
4847 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4848 "expression syntax."
4851 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4852 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4853 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4854 "attributes and does not return a result. For the @var{SET} operation the "
4855 "@var{value} is optional but the field is required. If not specified then "
4856 "the attribute value will be empty. For example:"
4859 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4862 "would create an @code{password} attribute for each @code{password} element "
4863 "found in the document. The attribute value will be empty but still exist."
4865 "When the @option{--inquire} option is passed then all remaining non-option "
4866 "arguments are retrieved via a server @emph{INQUIRE}."
4868 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4869 "expression syntax."
4872 new_command("IMPORT", 0, 1, import_command
, _(
4873 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4874 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4876 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4877 "argument is raw @abbr{XML} data. The content is created as a child of "
4878 "the element path specified with the @option{--root} option or at the "
4879 "document root when not specified. Existing elements of the same name will "
4882 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4886 new_command("DUMP", 0, 1, dump_command
, _(
4888 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4889 "dumping a specific node."
4892 new_command("LOCK", 0, 0, lock_command
, _(
4894 "Locks the mutex associated with the opened file. This prevents other clients "
4895 "from sending commands to the same opened file until the client "
4896 "that sent this command either disconnects or sends the @code{UNLOCK} "
4897 "command. @xref{UNLOCK}."
4900 new_command("UNLOCK", 1, 0, unlock_command
, _(
4902 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4903 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4904 "@pxref{ISCACHED})."
4907 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4908 "GETCONFIG [filename] <parameter>\n"
4909 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4910 "data response. If no file has been opened then the value for @var{filename} "
4911 "or the default from the @samp{global} section will be returned. If a file "
4912 "has been opened and no @var{filename} is specified, a value previously "
4913 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4916 new_command("OPTION", 1, 1, option_command
, _(
4917 "OPTION <NAME>=<VALUE>\n"
4918 "Sets a client option @var{name} to @var{value}. The value for an option is "
4919 "kept for the duration of the connection."
4922 "@item DISABLE-PINENTRY\n"
4923 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4924 "server inquire is sent to the client to obtain the passphrase. This option "
4925 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4926 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4928 "@item PINENTRY-TIMEOUT\n"
4929 "Sets the number of seconds before a pinentry prompt will return an error "
4930 "while waiting for user input."
4933 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4936 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4939 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4941 "@item PINENTRY-DESC\n"
4942 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4944 "@item PINENTRY-TITLE\n"
4945 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4947 "@item PINENTRY-PROMPT\n"
4948 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4951 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4953 "@item LC-MESSAGES\n"
4954 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4957 "Associates the thread ID of the connection with the specified textual "
4958 "representation. Useful for debugging log messages."
4960 "@item LOCK-TIMEOUT\n"
4961 "When not @code{0}, the duration in tenths of a second to wait for the file "
4962 "mutex which has been locked by another thread to be released before returning "
4963 "an error. When @code{-1}, then an error will be returned immediately."
4966 "An integer specifiying the logging level."
4970 new_command("LS", 1, 1, ls_command
, _(
4972 "Lists the available data files stored in the data directory "
4973 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
4976 new_command("RESET", 1, 1, NULL
, _(
4978 "Closes the currently opened file but keeps any previously set client options."
4981 new_command("NOP", 1, 1, NULL
, _(
4983 "Does nothing. Always returns successfully."
4986 /* !END-HELP-TEXT! */
4987 new_command ("CANCEL", 1, 1, NULL
, NULL
);
4988 new_command ("END", 1, 1, NULL
, NULL
);
4989 new_command ("BYE", 1, 1, NULL
, NULL
);
4992 for (i
= 0; command_table
[i
]; i
++);
4993 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4998 register_commands (assuan_context_t ctx
)
5002 for (; command_table
[i
]; i
++)
5004 if (!command_table
[i
]->handler
)
5007 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5008 command_table
[i
]->handler
,
5009 command_table
[i
]->help
);
5014 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5018 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5022 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5026 return assuan_register_post_cmd_notify (ctx
, command_finalize
);