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_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 if (client
->crypto
->sigpkey_sexp
)
217 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
219 client
->crypto
->pkey_sexp
= NULL
;
220 client
->crypto
->sigpkey_sexp
= NULL
;
221 memset (client
->crypto
->sign_grip
, 0,
222 sizeof (client
->crypto
->sign_grip
));
223 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
228 cleanup_client (struct client_s
*client
)
230 assuan_context_t ctx
= client
->ctx
;
231 struct client_thread_s
*thd
= client
->thd
;
232 struct crypto_s
*crypto
= client
->crypto
;
233 long lock_timeout
= client
->lock_timeout
;
234 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
235 struct pinentry_option_s pin_opts
;
236 xmlErrorPtr xml_error
= client
->xml_error
;
238 struct pinentry_option_s agent_pin_opts
;
240 if (crypto
&& crypto
->agent
)
241 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
242 sizeof(struct pinentry_option_s
));
245 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
246 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
247 free_client (client
);
248 memset (client
, 0, sizeof (struct client_s
));
249 client
->xml_error
= xml_error
;
250 client
->crypto
= crypto
;
253 client
->lock_timeout
= lock_timeout
;
254 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
255 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
257 if (crypto
&& crypto
->agent
)
258 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
259 sizeof(struct pinentry_option_s
));
264 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
266 struct client_s
*client
= assuan_get_pointer (ctx
);
268 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
269 int cached
= 0, keyarg
= key
? 1 : 0;
271 unsigned char *salted_key
= NULL
;
275 char *pin_title
= client
->pinentry_opts
.title
;
277 client
->crypto
->filename
= str_dup (client
->filename
);
278 if (cdata
|| client
->flags
& FLAG_NEW
)
280 if (cdata
&& !(client
->flags
& FLAG_NEW
))
282 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
287 cached
= cdata
!= NULL
;
291 if (!key
&& !IS_PKI (client
->crypto
)
292 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
294 if (client
->flags
& FLAG_NO_PINENTRY
)
296 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
303 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
306 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
313 if (!IS_PKI (client
->crypto
))
315 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
318 key
= gcry_malloc (keylen
);
319 memset (key
, 0, keylen
);
322 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
323 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
324 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
325 (void **)&salted_key
, &keysize
);
331 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
332 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
333 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
335 gcry_free (salted_key
);
339 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
344 if (client
->pinentry_opts
.title
!= pin_title
)
345 xfree (client
->pinentry_opts
.title
);
347 client
->pinentry_opts
.title
= pin_title
;
350 gcry_free (salted_key
);
354 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
357 gcry_free (salted_key
);
358 return GPG_ERR_ENOMEM
;
361 cdata
->key
= salted_key
;
362 cdata
->keylen
= keysize
;
365 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
370 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
372 free_cache_data_once (cdata
);
376 int timeout
= config_get_integer (client
->filename
,
382 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
384 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
385 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
386 client
->crypto
->plaintext_len
, &cdata
->doc
,
387 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
389 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
391 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
392 "%S", client
->crypto
->pkey_sexp
);
397 gcry_sexp_release (cdata
->sigkey
);
399 cdata
->sigkey
= NULL
;
400 if (!rc
&& IS_PKI (client
->crypto
))
401 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
402 "%S", client
->crypto
->sigpkey_sexp
);
406 rc
= cache_add_file (client
->md5file
,
407 (client
->flags
& FLAG_NEW
) ? NULL
408 : client
->crypto
->grip
, cdata
, timeout
);
413 free_cache_data_once (cdata
);
414 xmlFreeDoc (client
->doc
);
421 client
->flags
|= FLAG_OPEN
;
426 update_checksum (client
);
428 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
429 rc
= do_lock (client
, 0);
435 req_cleanup (void *arg
)
440 strv_free ((char **) arg
);
444 parse_open_opt_lock (void *data
, void *value
)
446 struct client_s
*client
= data
;
448 client
->opts
|= OPT_LOCK_ON_OPEN
;
453 parse_save_opt_inquire (void *data
, void *value
)
455 struct client_s
*client
= data
;
458 if (!(client
->flags
& FLAG_NEW
))
460 rc
= peer_is_invoker (client
);
461 if (rc
== GPG_ERR_EACCES
)
466 client
->opts
|= OPT_INQUIRE
;
471 parse_opt_inquire (void *data
, void *value
)
473 struct client_s
*client
= data
;
476 client
->opts
|= OPT_INQUIRE
;
481 update_checksum (struct client_s
*client
)
485 struct cache_data_s
*cdata
;
486 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
493 cdata
= cache_get_data (client
->md5file
);
497 cdata
->crc
= xmalloc (len
);
498 memcpy (cdata
->crc
, crc
, len
);
505 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
512 if (cdata
&& !cdata
->crc
)
513 return GPG_ERR_CHECKSUM
;
515 rc
= get_checksum (client
->filename
, &crc
, &len
);
520 n
= memcmp (cdata
->crc
, crc
, len
);
521 else if (client
->crc
)
522 n
= memcmp (client
->crc
, crc
, len
);
525 return n
? GPG_ERR_CHECKSUM
: 0;
529 do_open (assuan_context_t ctx
, const char *password
)
531 struct client_s
*client
= assuan_get_pointer (ctx
);
532 struct cache_data_s
*cdata
;
537 cdata
= cache_get_data (client
->md5file
);
538 if (cdata
&& cdata
->doc
)
542 /* This will check that the key is cached in the agent which needs to
543 * be determined for files that share a keygrip. */
546 rc
= cache_iscached (client
->filename
, &defer
);
547 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
548 rc
= GPG_ERR_KEY_EXPIRED
;
551 if (!rc
&& !(client
->flags
& FLAG_NEW
))
552 rc
= validate_checksum (client
, cdata
);
555 if (!rc
&& client
->thd
->remote
556 && config_get_boolean (client
->filename
, "tcp_require_key"))
557 rc
= GPG_ERR_KEY_EXPIRED
;
560 if (!rc
&& !password
)
562 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
566 if (IS_PKI (client
->crypto
))
568 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
570 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
571 client
->crypto
->grip
);
572 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
574 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
575 client
->crypto
->sign_grip
);
580 rc
= open_finalize (ctx
, NULL
, 0);
586 /* There was an error accessing the file so clear the cache entry. The
587 * real error will be returned from read_data_file() since the file
588 * may have only disappeared. */
591 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
592 rc
= cache_clear (client
->md5file
);
593 send_status_all (STATUS_CACHE
, NULL
);
600 rc
= read_data_file (client
->filename
, client
->crypto
);
603 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
604 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
606 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
610 client
->flags
|= FLAG_NEW
;
611 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
612 memset (client
->crypto
->sign_grip
, 0,
613 sizeof (client
->crypto
->sign_grip
));
614 rc
= open_finalize (ctx
, NULL
, 0);
618 if (password
&& IS_PKI (client
->crypto
))
621 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
627 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
632 open_command (assuan_context_t ctx
, char *line
)
635 struct client_s
*client
= assuan_get_pointer (ctx
);
636 char **req
, *filename
;
637 unsigned char md5file
[16];
639 assuan_peercred_t peer
;
640 struct argv_s
*args
[] = {
641 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
645 rc
= parse_options (&line
, args
, client
);
647 return send_error (ctx
, rc
);
649 req
= str_split (line
, " ", 2);
651 return send_error (ctx
, GPG_ERR_SYNTAX
);
653 rc
= do_validate_peer (ctx
, req
[0], &peer
);
657 return send_error (ctx
, rc
);
661 if (!valid_filename (filename
))
664 return send_error (ctx
, GPG_ERR_INV_VALUE
);
667 pthread_cleanup_push (req_cleanup
, req
);
668 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
669 /* This client may have locked a different file with ISCACHED --lock than
670 * the current filename. This will remove that lock. */
671 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
672 if (client
->flags
& FLAG_OPEN
||
673 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
675 uint32_t opts
= client
->opts
;
676 uint32_t flags
= client
->flags
;
679 client
->flags
|= FLAG_KEEP_LOCK
;
680 else if (client
->flags
& FLAG_NEW
)
681 cache_clear (client
->md5file
);
683 cleanup_client (client
);
685 client
->flags
|= flags
;
686 client
->flags
&= ~(FLAG_LOCK_CMD
);
688 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
691 memcpy (client
->md5file
, md5file
, 16);
692 client
->filename
= str_dup (filename
);
693 if (!client
->filename
)
696 return send_error(ctx
, GPG_ERR_ENOMEM
);
699 /* Need to lock the mutex here because file_modified() cannot without
700 * knowing the filename. */
701 rc
= lock_file_mutex (client
, 1);
703 client
->flags
&= ~FLAG_OPEN
;
707 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
710 if (IS_PKI (client
->crypto
))
711 rc
= set_pinentry_mode (client
->crypto
->agent
,
712 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
717 rc
= do_open (ctx
, password
);
719 cleanup_client (client
);
721 cleanup_crypto_stage1 (client
->crypto
);
725 pthread_cleanup_pop (1);
727 if (!rc
&& client
->flags
& FLAG_NEW
)
728 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
731 (void) kill_scd (client
->crypto
->agent
);
734 return send_error (ctx
, rc
);
738 parse_opt_no_passphrase (void *data
, void *value
)
740 struct client_s
*client
= data
;
743 client
->opts
|= OPT_NO_PASSPHRASE
;
748 parse_save_opt_no_agent (void *data
, void *value
)
750 struct client_s
*client
= data
;
752 client
->opts
|= OPT_NO_AGENT
;
757 parse_save_opt_cipher (void *data
, void *value
)
759 struct client_s
*client
= data
;
760 int algo
= cipher_string_to_gcrypt ((char *) value
);
761 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
765 return GPG_ERR_INV_VALUE
;
767 if (!(client
->flags
& FLAG_NEW
))
769 rc
= peer_is_invoker (client
);
770 if (rc
== GPG_ERR_EACCES
)
774 flags
&= ((uint16_t) ((uint32_t) (flags
& 0xFFFFFFFF)));
775 if (!(client
->crypto
->hdr
.flags
& gcrypt_to_cipher (algo
)))
785 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
792 permitted_to_save (struct client_s
*client
, const unsigned char *grip
,
793 size_t size
, const char *value
)
798 if (!(client
->flags
& FLAG_NEW
))
800 hexgrip
= bin2hex (grip
, size
);
801 rc
= !strcmp (hexgrip
, (char *)value
) ? 0 : GPG_ERR_EACCES
;
804 rc
= peer_is_invoker (client
);
812 parse_save_opt_keygrip (void *data
, void *value
)
815 struct client_s
*client
= data
;
816 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->grip
,
817 sizeof (client
->crypto
->grip
),
820 if (!IS_PKI (client
->crypto
))
821 return GPG_ERR_INV_ARG
;
826 if (client
->crypto
->save
.pkey
)
827 gcry_sexp_release (client
->crypto
->save
.pkey
);
829 client
->crypto
->save
.pkey
= NULL
;
830 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
832 return GPG_ERR_INV_ARG
;
837 parse_save_opt_sign_keygrip (void *data
, void *value
)
840 struct client_s
*client
= data
;
841 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->sign_grip
,
842 sizeof (client
->crypto
->sign_grip
),
845 if (!IS_PKI (client
->crypto
))
846 return GPG_ERR_INV_ARG
;
851 if (client
->crypto
->save
.sigpkey
)
852 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
854 client
->crypto
->save
.sigpkey
= NULL
;
855 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
857 return GPG_ERR_INV_ARG
;
862 parse_opt_s2k_count (void *data
, void *value
)
864 struct client_s
*client
= data
;
868 return GPG_ERR_INV_VALUE
;
870 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
875 parse_save_opt_iterations (void *data
, void *value
)
877 struct client_s
*client
= data
;
882 return GPG_ERR_INV_VALUE
;
885 n
= strtoull (v
, &p
, 10);
886 if (n
== UINT64_MAX
&& errno
)
887 return gpg_error_from_errno (errno
);
889 return GPG_ERR_INV_VALUE
;
891 if (!(client
->flags
& FLAG_NEW
))
893 gpg_error_t rc
= peer_is_invoker (client
);
895 if (rc
== GPG_ERR_EACCES
)
897 if (client
->crypto
->hdr
.iterations
!= n
)
906 client
->crypto
->save
.hdr
.iterations
= n
;
911 save_finalize (assuan_context_t ctx
)
913 struct client_s
*client
= assuan_get_pointer (ctx
);
915 xmlChar
*xmlbuf
= NULL
;
920 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
922 return GPG_ERR_ENOMEM
;
924 pthread_cleanup_push (xmlFree
, xmlbuf
);
926 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
927 && (client
->opts
& OPT_NO_AGENT
))
928 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
930 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
931 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
932 NULL
, &key
, &keylen
, 1, 1,
933 (client
->opts
& OPT_NO_PASSPHRASE
));
938 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
939 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
942 pubkey
= client
->crypto
->pkey_sexp
;
946 sigkey
= client
->crypto
->sigpkey_sexp
;
949 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
950 sigkey
= client
->crypto
->save
.sigpkey
;
954 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
955 client
->filename
, xmlbuf
, xmlbuflen
);
956 if (pubkey
== client
->crypto
->save
.pkey
)
960 gcry_sexp_release (client
->crypto
->pkey_sexp
);
961 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
962 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
963 client
->crypto
->grip
);
966 gcry_sexp_release (pubkey
);
968 client
->crypto
->save
.pkey
= NULL
;
972 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
974 if (sigkey
== client
->crypto
->save
.sigpkey
)
978 if (client
->crypto
->sigpkey_sexp
)
979 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
981 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
982 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
983 client
->crypto
->sign_grip
);
986 gcry_sexp_release (sigkey
);
988 client
->crypto
->save
.sigpkey
= NULL
;
997 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
998 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
1002 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
1003 send_status_all (STATUS_CACHE
, NULL
);
1007 rc
= update_checksum (client
);
1008 client
->flags
&= ~(FLAG_NEW
);
1012 pthread_cleanup_pop (1); // xmlFree
1017 parse_opt_reset (void *data
, void *value
)
1019 struct client_s
*client
= data
;
1022 client
->opts
|= OPT_RESET
;
1027 save_command (assuan_context_t ctx
, char *line
)
1029 struct client_s
*client
= assuan_get_pointer (ctx
);
1032 struct argv_s
*args
[] = {
1033 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1034 parse_opt_no_passphrase
},
1035 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
1036 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
1037 parse_save_opt_inquire
},
1038 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
1039 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
1040 parse_save_opt_sign_keygrip
},
1041 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
1042 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
1043 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
1044 parse_save_opt_iterations
},
1045 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
1046 parse_save_opt_no_agent
},
1050 cleanup_save (&client
->crypto
->save
);
1051 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
1052 sizeof (file_header_t
));
1053 client
->crypto
->save
.s2k_count
=
1054 config_get_ulong (client
->filename
, "s2k_count");
1056 if (client
->flags
& FLAG_NEW
)
1057 client
->crypto
->save
.hdr
.iterations
=
1058 config_get_ulonglong (client
->filename
, "cipher_iterations");
1060 rc
= parse_options (&line
, args
, client
);
1062 return send_error (ctx
, rc
);
1064 if (!(client
->flags
& FLAG_NEW
))
1065 client
->opts
&= ~OPT_NO_AGENT
;
1066 else if (client
->opts
& OPT_NO_AGENT
)
1068 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
1069 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
1072 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
1073 && !(client
->opts
& OPT_INQUIRE
))
1074 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1076 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1077 return send_error (ctx
, gpg_error_from_errno (errno
));
1079 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
1081 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
1082 return send_error (ctx
, GPG_ERR_ENOANO
);
1086 rc
= cache_iscached (client
->filename
, &defer
);
1087 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
1089 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1092 if ((!rc
&& defer
) || config_get_boolean ("global", "require_save_key"))
1093 client
->opts
|= OPT_RESET
;
1095 if (!rc
&& (client
->opts
& OPT_RESET
))
1097 rc
= cache_clear (client
->md5file
);
1099 return send_error (ctx
, rc
);
1101 log_write ("%s: %s", client
->filename
,
1102 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1103 send_status_all (STATUS_CACHE
, NULL
);
1107 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1110 return send_error (ctx
, rc
);
1113 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1114 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1115 && !(client
->opts
& OPT_NO_AGENT
))
1117 rc
= set_pinentry_mode (client
->crypto
->agent
,
1118 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1121 if (!(client
->flags
& FLAG_NEW
))
1123 struct crypto_s
*crypto
;
1127 /* Wanting to generate a new key. Require the key to open the
1128 current file before proceeding reguardless of the
1129 require_save_key configuration parameter. */
1130 rc
= cache_clear (client
->md5file
);
1133 rc
= init_client_crypto (&crypto
);
1135 crypto
->client_ctx
= client
->ctx
;
1139 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1140 client
->filename
, &key
, &keylen
);
1143 cleanup_crypto (&crypto
);
1147 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1151 struct inquire_data_s idata
= { 0 };
1152 char *params
= client
->opts
& OPT_INQUIRE
1153 ? NULL
: default_key_params (client
->crypto
);
1155 pthread_cleanup_push (xfree
, params
);
1156 idata
.crypto
= client
->crypto
;
1160 idata
.line
= params
;
1161 idata
.len
= strlen (params
);
1166 client
->crypto
->agent
->inquire_data
= &idata
;
1167 client
->crypto
->agent
->inquire_cb
= NULL
;
1168 rc
= generate_key (client
->crypto
, params
,
1169 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1170 pthread_cleanup_pop (1);
1176 rc
= save_finalize (ctx
);
1178 cleanup_crypto_stage1 (client
->crypto
);
1180 (void) kill_scd (client
->crypto
->agent
);
1182 return send_error (ctx
, rc
);
1186 do_delete (assuan_context_t ctx
, char *line
)
1188 struct client_s
*client
= assuan_get_pointer (ctx
);
1193 if (strchr (line
, '\t'))
1194 req
= str_split (line
, "\t", 0);
1196 req
= str_split (line
, " ", 0);
1199 return GPG_ERR_SYNTAX
;
1201 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1209 * No sub-node defined. Remove the entire node (root element).
1215 rc
= is_element_owner (client
, n
);
1218 rc
= unlink_node (client
, n
);
1227 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1228 NULL
, NULL
, 0, 0, NULL
, 0);
1233 rc
= is_element_owner (client
, n
);
1236 rc
= unlink_node (client
, n
);
1244 delete_command (assuan_context_t ctx
, char *line
)
1246 struct client_s
*client
= assuan_get_pointer (ctx
);
1248 struct argv_s
*args
[] = {
1249 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1253 rc
= parse_options (&line
, args
, client
);
1255 return send_error (ctx
, rc
);
1257 if (client
->opts
& OPT_INQUIRE
)
1259 unsigned char *result
;
1262 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1264 return send_error (ctx
, rc
);
1266 pthread_cleanup_push (xfree
, result
);
1267 rc
= do_delete (ctx
, (char *)result
);
1268 pthread_cleanup_pop (1);
1271 rc
= do_delete (ctx
, line
);
1273 return send_error (ctx
, rc
);
1277 store_command (assuan_context_t ctx
, char *line
)
1279 struct client_s
*client
= assuan_get_pointer (ctx
);
1282 unsigned char *result
;
1284 xmlNodePtr n
, parent
;
1286 char *content
= NULL
;
1288 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1290 return send_error (ctx
, rc
);
1292 req
= str_split ((char *) result
, "\t", 0);
1296 return send_error (ctx
, GPG_ERR_SYNTAX
);
1298 len
= strv_length (req
);
1299 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1300 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1303 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1306 if (has_content
|| !*req
[len
- 1])
1309 content
= req
[len
- 1];
1310 req
[len
- 1] = NULL
;
1314 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1315 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1317 rc
= new_root_element (client
, client
->doc
, *req
);
1321 return send_error (ctx
, rc
);
1330 return send_error (ctx
, rc
);
1335 if (req
[1] && *req
[1])
1338 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1340 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1341 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1347 rc
= is_element_owner (client
, parent
);
1350 n
= find_text_node (parent
->children
);
1352 xmlNodeSetContent (n
, (xmlChar
*) content
);
1354 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1356 update_element_mtime (client
, parent
);
1362 return send_error (ctx
, rc
);
1366 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1371 int progress
= config_get_integer ("global", "xfer_progress");
1375 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1376 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1377 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1385 if (sent
+ to_send
> total
)
1386 to_send
= total
- sent
;
1388 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1389 flush
? 0 : to_send
);
1392 sent
+= flush
? 0 : to_send
;
1394 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1395 (sent
== total
&& flush
))
1396 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1398 if (!flush
&& !rc
&& sent
== total
)
1405 while (!rc
&& sent
< total
);
1411 do_get (assuan_context_t ctx
, char *line
)
1413 struct client_s
*client
= assuan_get_pointer (ctx
);
1418 req
= str_split (line
, "\t", 0);
1423 return GPG_ERR_SYNTAX
;
1426 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1435 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1442 if (!n
|| !n
->children
)
1443 return GPG_ERR_NO_DATA
;
1445 n
= find_text_node (n
->children
);
1446 if (!n
|| !n
->content
|| !*n
->content
)
1447 return GPG_ERR_NO_DATA
;
1449 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1454 get_command (assuan_context_t ctx
, char *line
)
1456 struct client_s
*client
= assuan_get_pointer (ctx
);
1458 struct argv_s
*args
[] = {
1459 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1463 rc
= parse_options (&line
, args
, client
);
1465 return send_error (ctx
, rc
);
1467 if (client
->opts
& OPT_INQUIRE
)
1469 unsigned char *result
;
1472 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1474 return send_error (ctx
, rc
);
1476 pthread_cleanup_push (xfree
, result
);
1477 rc
= do_get (ctx
, (char *)result
);
1478 pthread_cleanup_pop (1);
1481 rc
= do_get (ctx
, line
);
1483 return send_error (ctx
, rc
);
1486 static void list_command_cleanup1 (void *arg
);
1488 realpath_command (assuan_context_t ctx
, char *line
)
1490 struct string_s
*string
= NULL
;
1492 struct client_s
*client
= assuan_get_pointer (ctx
);
1493 struct argv_s
*args
[] = {
1494 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1498 rc
= parse_options (&line
, args
, client
);
1500 return send_error (ctx
, rc
);
1502 if (client
->opts
& OPT_INQUIRE
)
1504 unsigned char *result
;
1507 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1509 return send_error (ctx
, rc
);
1511 pthread_cleanup_push (xfree
, result
);
1512 rc
= build_realpath (client
, client
->doc
, (char *)result
, &string
);
1513 pthread_cleanup_pop (1);
1516 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1520 pthread_cleanup_push (list_command_cleanup1
, string
);
1521 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1522 pthread_cleanup_pop (1);
1525 return send_error (ctx
, rc
);
1529 list_command_cleanup1 (void *arg
)
1532 string_free ((struct string_s
*) arg
, 1);
1536 list_command_cleanup2 (void *arg
)
1538 struct element_list_s
*elements
= arg
;
1544 int total
= slist_length (elements
->list
);
1547 for (i
= 0; i
< total
; i
++)
1549 char *tmp
= slist_nth_data (elements
->list
, i
);
1553 slist_free (elements
->list
);
1556 if (elements
->prefix
)
1557 xfree (elements
->prefix
);
1560 strv_free (elements
->req
);
1567 parse_list_opt_norecurse (void *data
, void *value
)
1569 struct client_s
*client
= data
;
1571 client
->opts
&= ~(OPT_LIST_RECURSE
);
1576 parse_opt_verbose (void *data
, void *value
)
1578 struct client_s
*client
= data
;
1580 client
->opts
|= OPT_VERBOSE
;
1585 parse_list_opt_target (void *data
, void *value
)
1587 struct client_s
*client
= data
;
1589 client
->opts
|= OPT_LIST_WITH_TARGET
;
1594 parse_list_opt_all (void *data
, void *value
)
1596 struct client_s
*client
= data
;
1598 client
->opts
|= OPT_LIST_ALL
;
1603 list_path_once (struct client_s
*client
, char *line
,
1604 struct element_list_s
*elements
, struct string_s
*result
)
1608 elements
->req
= str_split (line
, " ", 0);
1610 strv_printf (&elements
->req
, "%s", line
);
1612 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1613 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1618 int total
= slist_length (elements
->list
);
1621 rc
= GPG_ERR_NO_DATA
;
1629 for (i
= 0; i
< total
; i
++)
1631 char *tmp
= slist_nth_data (elements
->list
, i
);
1633 string_append_printf (result
, "%s%s", tmp
,
1634 i
+ 1 == total
? "" : "\n");
1639 rc
= GPG_ERR_NO_DATA
;
1646 has_list_flag (char *path
, char *flags
)
1650 while (*p
&& *++p
!= ' ');
1659 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1670 do_list (assuan_context_t ctx
, char *line
)
1672 struct client_s
*client
= assuan_get_pointer (ctx
);
1674 struct element_list_s
*elements
= NULL
;
1676 elements
= xcalloc (1, sizeof (struct element_list_s
));
1678 return GPG_ERR_ENOMEM
;
1680 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1682 (client
->opts
& OPT_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1683 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1685 if (!line
|| !*line
)
1687 struct string_s
*str
= NULL
;
1689 pthread_cleanup_push (list_command_cleanup2
, elements
);
1690 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1691 elements
->with_target
);
1692 pthread_cleanup_pop (1);
1693 pthread_cleanup_push (list_command_cleanup1
, str
);
1697 if (client
->opts
& OPT_LIST_ALL
)
1699 char **roots
= str_split (str
->str
, "\n", 0);
1702 pthread_cleanup_push (req_cleanup
, roots
);
1703 string_truncate (str
, 0);
1705 for (p
= roots
; *p
; p
++)
1707 if (strchr (*p
, ' '))
1709 if (has_list_flag (*p
, "EOP"))
1711 string_append_printf (str
, "%s%s", *p
,
1712 *(p
+ 1) ? "\n" : "");
1717 elements
= xcalloc (1, sizeof (struct element_list_s
));
1720 rc
= GPG_ERR_ENOMEM
;
1724 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1726 (client
->opts
& OPT_VERBOSE
) | (client
->opts
&
1727 OPT_LIST_WITH_TARGET
);
1728 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1729 pthread_cleanup_push (list_command_cleanup2
, elements
);
1730 rc
= list_path_once (client
, *p
, elements
, str
);
1731 pthread_cleanup_pop (1);
1736 string_append (str
, "\n");
1739 pthread_cleanup_pop (1);
1743 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1746 pthread_cleanup_pop (1);
1750 pthread_cleanup_push (list_command_cleanup2
, elements
);
1751 struct string_s
*str
= string_new (NULL
);
1752 pthread_cleanup_push (list_command_cleanup1
, str
);
1753 rc
= list_path_once (client
, line
, elements
, str
);
1755 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1757 pthread_cleanup_pop (1);
1758 pthread_cleanup_pop (1);
1763 list_command (assuan_context_t ctx
, char *line
)
1765 struct client_s
*client
= assuan_get_pointer (ctx
);
1767 struct argv_s
*args
[] = {
1768 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1769 parse_list_opt_norecurse
},
1770 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
1771 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1772 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1773 parse_list_opt_target
},
1774 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1778 if (disable_list_and_dump
== 1)
1779 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1781 client
->opts
|= OPT_LIST_RECURSE
;
1782 rc
= parse_options (&line
, args
, client
);
1784 return send_error (ctx
, rc
);
1786 if (client
->opts
& OPT_LIST_ALL
)
1787 client
->opts
|= OPT_LIST_RECURSE
| OPT_VERBOSE
;
1789 if (client
->opts
& OPT_INQUIRE
)
1791 unsigned char *result
;
1794 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1796 return send_error (ctx
, rc
);
1798 pthread_cleanup_push (xfree
, result
);
1799 rc
= do_list (ctx
, (char *)result
);
1800 pthread_cleanup_pop (1);
1803 rc
= do_list (ctx
, line
);
1805 return send_error (ctx
, rc
);
1809 * req[0] - element path
1812 attribute_list (assuan_context_t ctx
, char **req
)
1814 struct client_s
*client
= assuan_get_pointer (ctx
);
1815 char **attrlist
= NULL
;
1823 if (!req
|| !req
[0])
1824 return GPG_ERR_SYNTAX
;
1826 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1829 * The first argument may be only a root element.
1831 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1832 return GPG_ERR_SYNTAX
;
1835 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1845 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1846 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1857 for (a
= n
->properties
; a
; a
= a
->next
)
1861 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1864 strv_free (attrlist
);
1866 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1867 pwmd_strerror (GPG_ERR_ENOMEM
));
1868 return GPG_ERR_ENOMEM
;
1873 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1875 && an
->content
? (char *) an
->content
: "");
1879 strv_free (attrlist
);
1880 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1881 pwmd_strerror (GPG_ERR_ENOMEM
));
1882 return GPG_ERR_ENOMEM
;
1885 attrlist
[++i
] = NULL
;
1889 return GPG_ERR_NO_DATA
;
1891 line
= strv_join ("\n", attrlist
);
1895 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1896 pwmd_strerror (GPG_ERR_ENOMEM
));
1897 strv_free (attrlist
);
1898 return GPG_ERR_ENOMEM
;
1901 pthread_cleanup_push (xfree
, line
);
1902 pthread_cleanup_push (req_cleanup
, attrlist
);
1903 rc
= xfer_data (ctx
, line
, strlen (line
));
1904 pthread_cleanup_pop (1);
1905 pthread_cleanup_pop (1);
1910 * req[0] - attribute
1911 * req[1] - element path
1914 attribute_delete (struct client_s
*client
, char **req
)
1920 if (!req
|| !req
[0] || !req
[1])
1921 return GPG_ERR_SYNTAX
;
1923 if (!strcmp (req
[0], "_name"))
1924 return GPG_ERR_INV_ATTR
;
1926 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1929 * The first argument may be only a root element.
1931 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1932 return GPG_ERR_SYNTAX
;
1935 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1941 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1942 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1947 if (!strcmp (req
[0], (char *) "_acl"))
1949 rc
= is_element_owner (client
, n
);
1954 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
1962 create_element_path (struct client_s
*client
,
1963 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1965 char **req
= *elements
;
1966 char **req_orig
= strv_dup (req
);
1967 xmlNodePtr n
= NULL
;
1973 *rc
= GPG_ERR_ENOMEM
;
1974 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1975 pwmd_strerror (GPG_ERR_ENOMEM
));
1980 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
1983 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1986 *rc
= new_root_element (client
, client
->doc
, req
[0]);
1992 else if (n
== parent
)
1994 *rc
= GPG_ERR_CONFLICT
;
2001 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
2003 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2004 NULL
, NULL
, create_target_elements_cb
, 0, 0,
2011 * Reset the position of the element tree now that the elements
2012 * have been created.
2017 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2021 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2022 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2029 strv_free (req_orig
);
2036 * Creates a "target" attribute. When other commands encounter an element with
2037 * this attribute, the element path is modified to the target value. If the
2038 * source element path doesn't exist when using 'ATTR SET target', it is
2039 * created, but the destination element path must exist.
2041 * req[0] - source element path
2042 * req[1] - destination element path
2045 target_attribute (struct client_s
*client
, char **req
)
2047 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
2051 if (!req
|| !req
[0] || !req
[1])
2052 return GPG_ERR_SYNTAX
;
2054 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
2057 * The first argument may be only a root element.
2059 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
2060 return GPG_ERR_SYNTAX
;
2063 if (!valid_element_path (src
, 0))
2064 return GPG_ERR_INV_VALUE
;
2066 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
2069 * The first argument may be only a root element.
2071 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
2073 rc
= GPG_ERR_SYNTAX
;
2078 odst
= strv_dup (dst
);
2081 rc
= GPG_ERR_ENOMEM
;
2085 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2087 * Make sure the destination element path exists.
2094 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
2095 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2100 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
2101 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2104 n
= create_element_path (client
, &src
, &rc
, NULL
);
2108 line
= strv_join ("\t", odst
);
2111 rc
= GPG_ERR_ENOMEM
;
2115 rc
= add_attribute (client
, n
, "target", line
);
2126 * req[0] - attribute
2127 * req[1] - element path
2130 attribute_get (assuan_context_t ctx
, char **req
)
2132 struct client_s
*client
= assuan_get_pointer (ctx
);
2138 if (!req
|| !req
[0] || !req
[1])
2139 return GPG_ERR_SYNTAX
;
2141 if (strchr (req
[1], '\t'))
2143 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2144 return GPG_ERR_SYNTAX
;
2148 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2149 return GPG_ERR_SYNTAX
;
2152 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2159 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2160 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2168 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2169 return GPG_ERR_NOT_FOUND
;
2171 pthread_cleanup_push (xmlFree
, a
);
2174 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2176 rc
= GPG_ERR_NO_DATA
;
2178 pthread_cleanup_pop (1);
2187 * req[0] - attribute
2188 * req[1] - element path
2192 attribute_set (struct client_s
*client
, char **req
)
2198 if (!req
|| !req
[0] || !req
[1])
2199 return GPG_ERR_SYNTAX
;
2202 * Reserved attribute names.
2204 if (!strcmp (req
[0], "_name"))
2205 return GPG_ERR_INV_ATTR
;
2206 else if (!strcmp (req
[0], "target"))
2207 return target_attribute (client
, req
+ 1);
2208 else if (!valid_xml_attribute (req
[0]))
2209 return GPG_ERR_INV_VALUE
;
2211 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2214 * The first argument may be only a root element.
2216 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2217 return GPG_ERR_SYNTAX
;
2220 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2227 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2228 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2234 if (!strcmp (req
[0], (char *) "_acl"))
2236 rc
= is_element_owner (client
, n
);
2241 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2250 * req[1] - attribute name or element path if command is LIST
2251 * req[2] - element path
2252 * req[2] - element path or value
2256 do_attr (assuan_context_t ctx
, char *line
)
2258 struct client_s
*client
= assuan_get_pointer (ctx
);
2262 req
= str_split (line
, " ", 4);
2263 if (!req
|| !req
[0] || !req
[1])
2266 return GPG_ERR_SYNTAX
;
2269 pthread_cleanup_push (req_cleanup
, req
);
2271 if (strcasecmp (req
[0], "SET") == 0)
2272 rc
= attribute_set (client
, req
+ 1);
2273 else if (strcasecmp (req
[0], "GET") == 0)
2274 rc
= attribute_get (ctx
, req
+ 1);
2275 else if (strcasecmp (req
[0], "DELETE") == 0)
2276 rc
= attribute_delete (client
, req
+ 1);
2277 else if (strcasecmp (req
[0], "LIST") == 0)
2278 rc
= attribute_list (ctx
, req
+ 1);
2280 rc
= GPG_ERR_SYNTAX
;
2282 pthread_cleanup_pop (1);
2287 attr_command (assuan_context_t ctx
, char *line
)
2289 struct client_s
*client
= assuan_get_pointer (ctx
);
2291 struct argv_s
*args
[] = {
2292 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2296 rc
= parse_options (&line
, args
, client
);
2298 return send_error (ctx
, rc
);
2300 if (client
->opts
& OPT_INQUIRE
)
2302 unsigned char *result
;
2305 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2307 return send_error (ctx
, rc
);
2309 pthread_cleanup_push (xfree
, result
);
2310 rc
= do_attr (ctx
, (char *)result
);
2311 pthread_cleanup_pop (1);
2314 rc
= do_attr (ctx
, line
);
2316 return send_error (ctx
, rc
);
2320 parse_iscached_opt_lock (void *data
, void *value
)
2322 struct client_s
*client
= data
;
2325 client
->opts
|= OPT_LOCK
;
2330 iscached_command (assuan_context_t ctx
, char *line
)
2332 struct client_s
*client
= assuan_get_pointer (ctx
);
2334 struct argv_s
*args
[] = {
2335 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2339 if (!line
|| !*line
)
2340 return send_error (ctx
, GPG_ERR_SYNTAX
);
2342 rc
= parse_options (&line
, args
, client
);
2344 return send_error (ctx
, rc
);
2345 else if (!valid_filename (line
))
2346 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2348 rc
= cache_iscached (line
, NULL
);
2349 if (client
->opts
& OPT_LOCK
2350 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2352 unsigned char md5file
[16];
2353 gpg_error_t trc
= rc
;
2355 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2356 if (memcmp (md5file
, client
->md5file
, 16))
2357 cleanup_client (client
);
2359 memcpy (client
->md5file
, md5file
, 16);
2360 rc
= do_lock (client
, 1);
2365 return send_error (ctx
, rc
);
2369 clearcache_command (assuan_context_t ctx
, char *line
)
2371 gpg_error_t rc
= 0, all_rc
= 0;
2372 unsigned char md5file
[16];
2376 struct client_thread_s
*once
= NULL
;
2379 MUTEX_LOCK (&cn_mutex
);
2380 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2382 if (!line
|| !*line
)
2385 t
= slist_length (cn_thread_list
);
2387 for (i
= 0; i
< t
; i
++)
2389 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2390 assuan_peercred_t peer
;
2395 /* Lock each connected clients' file mutex to prevent any other client
2396 * from accessing the cache entry (the file mutex is locked upon
2397 * command startup). The cache for the entry is not cleared if the
2398 * file mutex is locked by another client to prevent this function
2403 if (thd
->cl
->filename
)
2405 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2406 all_rc
= !all_rc
? rc
: all_rc
;
2414 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2415 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2417 if (pthread_equal (pthread_self (), thd
->tid
))
2421 if (!thd
->cl
->filename
||
2422 cache_iscached (thd
->cl
->filename
,
2423 NULL
) == GPG_ERR_NO_DATA
)
2429 cache_defer_clear (thd
->cl
->md5file
);
2432 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2440 rc
= cache_clear (thd
->cl
->md5file
);
2441 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2449 /* A single data filename was specified. Lock only this data file
2450 * mutex and free the cache entry. */
2453 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2454 rc
= do_validate_peer (ctx
, line
, &peer
);
2456 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2458 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2460 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2462 if (pthread_equal (pthread_self (), thd
->tid
))
2469 rc
= cache_clear (thd
->cl
->md5file
);
2470 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2474 cache_defer_clear (thd
->cl
->md5file
);
2482 /* Only connected clients' cache entries have been cleared. Now clear any
2483 * remaining cache entries without clients but only if there wasn't an
2484 * error from above since this would defeat the locking check of the
2485 * remaining entries. */
2491 /* No clients are using the specified file. */
2492 else if (!all_rc
&& !rc
&& !once
)
2494 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2495 rc
= cache_clear (md5file
);
2498 /* Release the connection mutex. */
2499 pthread_cleanup_pop (1);
2503 send_status_all (STATUS_CACHE
, NULL
);
2505 /* One or more files were locked while clearing all cache entries. */
2509 return send_error (ctx
, rc
);
2513 cachetimeout_command (assuan_context_t ctx
, char *line
)
2516 char **req
= str_split (line
, " ", 0);
2519 assuan_peercred_t peer
;
2521 if (!req
|| !*req
|| !req
[1])
2524 return send_error (ctx
, GPG_ERR_SYNTAX
);
2528 timeout
= (int) strtol (req
[1], &p
, 10);
2529 if (errno
!= 0 || *p
|| timeout
< -1)
2532 return send_error (ctx
, GPG_ERR_SYNTAX
);
2535 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2538 unsigned char md5file
[16];
2540 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2541 rc
= cache_set_timeout (md5file
, timeout
);
2542 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2545 MUTEX_LOCK (&rcfile_mutex
);
2546 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2548 MUTEX_UNLOCK (&rcfile_mutex
);
2553 return send_error (ctx
, rc
);
2557 dump_command (assuan_context_t ctx
, char *line
)
2561 struct client_s
*client
= assuan_get_pointer (ctx
);
2564 if (disable_list_and_dump
== 1)
2565 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2567 rc
= peer_is_invoker(client
);
2569 return send_error (ctx
, rc
);
2571 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2575 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2576 pwmd_strerror (GPG_ERR_ENOMEM
));
2577 return send_error (ctx
, GPG_ERR_ENOMEM
);
2580 pthread_cleanup_push (xmlFree
, xml
);
2581 rc
= xfer_data (ctx
, (char *) xml
, len
);
2582 pthread_cleanup_pop (1);
2583 return send_error (ctx
, rc
);
2587 getconfig_command (assuan_context_t ctx
, char *line
)
2589 struct client_s
*client
= assuan_get_pointer (ctx
);
2591 char filename
[255] = { 0 }, param
[747] = { 0 };
2592 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2594 if (!line
|| !*line
)
2595 return send_error (ctx
, GPG_ERR_SYNTAX
);
2597 if (strchr (line
, ' '))
2599 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2604 if (fp
&& !valid_filename (fp
))
2605 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2607 paramp
= str_down (paramp
);
2608 if (!strcmp (paramp
, "cipher") && fp
)
2610 struct crypto_s
*crypto
= NULL
;
2612 rc
= init_client_crypto (&crypto
);
2615 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2619 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2629 UPDATE_AGENT_CTX (client
, crypto
);
2630 cleanup_crypto (&crypto
);
2631 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2632 return send_error (ctx
, rc
);
2637 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2639 struct crypto_s
*crypto
= NULL
;
2641 rc
= init_client_crypto (&crypto
);
2644 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2647 tmp
= str_asprintf ("%llu",
2648 (unsigned long long) crypto
->hdr
.
2651 rc
= GPG_ERR_ENOMEM
;
2655 UPDATE_AGENT_CTX (client
, crypto
);
2656 cleanup_crypto (&crypto
);
2657 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2658 return send_error (ctx
, rc
);
2663 else if (!strcmp (paramp
, "passphrase"))
2664 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2666 p
= config_get_value (fp
? fp
: "global", paramp
);
2668 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2670 tmp
= expand_homedir (p
);
2674 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2675 pwmd_strerror (GPG_ERR_ENOMEM
));
2676 return send_error (ctx
, GPG_ERR_ENOMEM
);
2681 pthread_cleanup_push (xfree
, p
);
2682 rc
= xfer_data (ctx
, p
, strlen (p
));
2683 pthread_cleanup_pop (1);
2684 return send_error (ctx
, rc
);
2689 xmlXPathContextPtr xp
;
2690 xmlXPathObjectPtr result
;
2696 xpath_command_cleanup (void *arg
)
2698 struct xpath_s
*xpath
= arg
;
2703 req_cleanup (xpath
->req
);
2706 xmlBufferFree (xpath
->buf
);
2709 xmlXPathFreeObject (xpath
->result
);
2712 xmlXPathFreeContext (xpath
->xp
);
2716 do_xpath (assuan_context_t ctx
, char *line
)
2719 struct client_s
*client
= assuan_get_pointer (ctx
);
2720 struct xpath_s _x
= { 0 };
2721 struct xpath_s
*xpath
= &_x
;
2723 if (!line
|| !*line
)
2724 return GPG_ERR_SYNTAX
;
2726 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2728 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2729 return GPG_ERR_ENOMEM
;
2732 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2735 rc
= GPG_ERR_BAD_DATA
;
2740 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2743 rc
= GPG_ERR_BAD_DATA
;
2747 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2749 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2753 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2754 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2758 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2760 rc
= GPG_ERR_NO_DATA
;
2763 else if (xpath
->req
[1])
2769 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2770 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2771 xmlBufferLength (xpath
->buf
));
2772 pthread_cleanup_pop (0);
2774 xpath_command_cleanup (xpath
);
2779 xpath_command (assuan_context_t ctx
, char *line
)
2781 struct client_s
*client
= assuan_get_pointer (ctx
);
2783 struct argv_s
*args
[] = {
2784 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2788 if (disable_list_and_dump
== 1)
2789 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2791 rc
= peer_is_invoker(client
);
2793 return send_error (ctx
, rc
);
2795 rc
= parse_options (&line
, args
, client
);
2797 return send_error (ctx
, rc
);
2799 if (client
->opts
& OPT_INQUIRE
)
2801 unsigned char *result
;
2804 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2806 return send_error (ctx
, rc
);
2808 pthread_cleanup_push (xfree
, result
);
2809 rc
= do_xpath (ctx
, (char *)result
);
2810 pthread_cleanup_pop (1);
2813 rc
= do_xpath (ctx
, line
);
2815 return send_error (ctx
, rc
);
2819 do_xpathattr (assuan_context_t ctx
, char *line
)
2821 struct client_s
*client
= assuan_get_pointer (ctx
);
2825 struct xpath_s _x
= { 0 };
2826 struct xpath_s
*xpath
= &_x
;
2828 if (!line
|| !*line
)
2829 return GPG_ERR_SYNTAX
;
2831 if ((req
= str_split (line
, " ", 3)) == NULL
)
2832 return GPG_ERR_ENOMEM
;
2836 rc
= GPG_ERR_SYNTAX
;
2840 if (!strcasecmp (req
[0], "SET"))
2842 else if (!strcasecmp (req
[0], "DELETE"))
2846 rc
= GPG_ERR_SYNTAX
;
2850 if (!req
[1] || !req
[2])
2852 rc
= GPG_ERR_SYNTAX
;
2856 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2858 rc
= GPG_ERR_ENOMEM
;
2862 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2864 rc
= GPG_ERR_SYNTAX
;
2868 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2871 rc
= GPG_ERR_BAD_DATA
;
2875 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2878 rc
= GPG_ERR_BAD_DATA
;
2882 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2884 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2888 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2889 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2890 (xmlChar
*) req
[1]);
2893 xpath_command_cleanup (xpath
);
2898 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2900 xpathattr_command (assuan_context_t ctx
, char *line
)
2902 struct client_s
*client
= assuan_get_pointer (ctx
);
2904 struct argv_s
*args
[] = {
2905 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2909 if (disable_list_and_dump
== 1)
2910 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2912 rc
= peer_is_invoker(client
);
2914 return send_error (ctx
, rc
);
2916 rc
= parse_options (&line
, args
, client
);
2918 return send_error (ctx
, rc
);
2920 if (client
->opts
& OPT_INQUIRE
)
2922 unsigned char *result
;
2925 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2927 return send_error (ctx
, rc
);
2929 pthread_cleanup_push (xfree
, result
);
2930 rc
= do_xpathattr (ctx
, (char *)result
);
2931 pthread_cleanup_pop (1);
2934 rc
= do_xpathattr (ctx
, line
);
2936 return send_error (ctx
, rc
);
2940 do_import (struct client_s
*client
, const char *root_element
,
2941 unsigned char *content
)
2943 char **dst_path
= NULL
;
2944 xmlDocPtr doc
= NULL
;
2945 xmlNodePtr n
, root
, copy
;
2948 if (!content
|| !*content
)
2951 return GPG_ERR_SYNTAX
;
2955 dst_path
= str_split (root_element
, "\t", 0);
2957 if (dst_path
&& !valid_element_path (dst_path
, 0))
2960 strv_free (dst_path
);
2962 return GPG_ERR_INV_VALUE
;
2965 struct string_s
*str
= string_new_content ((char *)content
);
2966 str
= string_prepend (str
, "<pwmd>");
2967 str
= string_append (str
, "</pwmd>");
2968 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2969 string_free (str
, 1);
2972 rc
= GPG_ERR_BAD_DATA
;
2976 root
= xmlDocGetRootElement (doc
);
2977 xmlNodePtr root_orig
= root
->children
;
2978 root
= root
->children
;
2979 rc
= validate_import (client
, root
);
2988 char **path
= strv_dup (dst_path
);
2991 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2992 pwmd_strerror (GPG_ERR_ENOMEM
));
2993 rc
= GPG_ERR_ENOMEM
;
2997 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
3001 rc
= GPG_ERR_INV_VALUE
;
3005 if (strv_printf (&path
, "%s", (char *) a
) == 0)
3008 rc
= GPG_ERR_ENOMEM
;
3013 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
3014 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3022 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
3023 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
3024 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3039 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
3041 n
= create_element_path (client
, &path
, &rc
, NULL
);
3048 copy
= xmlCopyNodeList (root
->children
);
3049 n
= xmlAddChildList (n
, copy
);
3051 rc
= GPG_ERR_ENOMEM
;
3060 /* Check if the content root element can create a DTD root element. */
3061 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
3063 rc
= GPG_ERR_SYNTAX
;
3069 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
3071 rc
= GPG_ERR_SYNTAX
;
3075 char *tmp
= str_dup ((char *) a
);
3077 int literal
= is_literal_element (&tmp
);
3079 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
3082 rc
= GPG_ERR_INV_VALUE
;
3086 if (strv_printf (&path
, "%s", tmp
) == 0)
3089 rc
= GPG_ERR_ENOMEM
;
3094 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
3095 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3097 rc
= GPG_ERR_BAD_DATA
;
3101 /* Overwriting the existing tree. */
3105 xmlFreeNodeList (n
);
3109 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
3110 n
= xmlCopyNode (root
, 1);
3111 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3116 rc
= update_element_mtime (client
, n
->parent
);
3118 for (root
= root_orig
->next
; root
; root
= root
->next
)
3120 if (root
->type
== XML_ELEMENT_NODE
)
3133 strv_free (dst_path
);
3139 parse_import_opt_root (void *data
, void *value
)
3141 struct client_s
*client
= data
;
3143 client
->import_root
= str_dup (value
);
3144 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3148 import_command (assuan_context_t ctx
, char *line
)
3151 struct client_s
*client
= assuan_get_pointer (ctx
);
3152 unsigned char *result
;
3154 struct argv_s
*args
[] = {
3155 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3159 xfree (client
->import_root
);
3160 client
->import_root
= NULL
;
3161 rc
= parse_options (&line
, args
, client
);
3163 return send_error (ctx
, rc
);
3165 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3168 xfree (client
->import_root
);
3169 client
->import_root
= NULL
;
3170 return send_error (ctx
, rc
);
3173 rc
= do_import (client
, client
->import_root
, result
);
3174 xfree (client
->import_root
);
3175 client
->import_root
= NULL
;
3176 return send_error (ctx
, rc
);
3180 do_lock (struct client_s
*client
, int add
)
3182 gpg_error_t rc
= lock_file_mutex (client
, add
);
3185 client
->flags
|= FLAG_LOCK_CMD
;
3191 lock_command (assuan_context_t ctx
, char *line
)
3193 struct client_s
*client
= assuan_get_pointer (ctx
);
3194 gpg_error_t rc
= do_lock (client
, 0);
3196 return send_error (ctx
, rc
);
3200 unlock_command (assuan_context_t ctx
, char *line
)
3202 struct client_s
*client
= assuan_get_pointer (ctx
);
3205 rc
= unlock_file_mutex (client
, 0);
3206 return send_error (ctx
, rc
);
3210 option_command (assuan_context_t ctx
, char *line
)
3212 struct client_s
*client
= assuan_get_pointer (ctx
);
3214 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3216 struct agent_s
*agent
= client
->crypto
->agent
;
3218 char namebuf
[255] = { 0 };
3219 char *name
= namebuf
;
3220 char *value
= NULL
, *p
;
3222 p
= strchr (line
, '=');
3224 strncpy (namebuf
, line
, sizeof(namebuf
));
3227 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3231 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3233 if (strcasecmp (name
, (char *) "log_level") == 0)
3239 l
= strtol (value
, NULL
, 10);
3242 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3245 MUTEX_LOCK (&rcfile_mutex
);
3246 config_set_int_param (&global_config
, "global", "log_level", value
);
3247 MUTEX_UNLOCK (&rcfile_mutex
);
3250 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3257 n
= strtol (value
, &p
, 10);
3259 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3262 client
->lock_timeout
= n
;
3265 else if (strcasecmp (name
, (char *) "NAME") == 0)
3267 char *tmp
= pthread_getspecific (thread_name_key
);
3272 MUTEX_LOCK (&cn_mutex
);
3273 xfree (client
->thd
->name
);
3274 client
->thd
->name
= NULL
;
3276 if (!value
|| !*value
)
3277 pthread_setspecific (thread_name_key
, str_dup (""));
3280 client
->thd
->name
= str_dup (value
);
3281 pthread_setspecific (thread_name_key
, str_dup (value
));
3284 MUTEX_UNLOCK (&cn_mutex
);
3287 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3289 xfree (pin_opts
->lc_messages
);
3290 pin_opts
->lc_messages
= NULL
;
3291 if (value
&& *value
)
3292 pin_opts
->lc_messages
= str_dup (value
);
3295 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3298 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3300 xfree (pin_opts
->lc_ctype
);
3301 pin_opts
->lc_ctype
= NULL
;
3302 if (value
&& *value
)
3303 pin_opts
->lc_ctype
= str_dup (value
);
3306 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3309 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3311 xfree (pin_opts
->ttyname
);
3312 pin_opts
->ttyname
= NULL
;
3313 if (value
&& *value
)
3314 pin_opts
->ttyname
= str_dup (value
);
3317 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3320 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3322 xfree (pin_opts
->ttytype
);
3323 pin_opts
->ttytype
= NULL
;
3324 if (value
&& *value
)
3325 pin_opts
->ttytype
= str_dup (value
);
3328 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3331 else if (strcasecmp (name
, (char *) "display") == 0)
3333 xfree (pin_opts
->display
);
3334 pin_opts
->display
= NULL
;
3335 if (value
&& *value
)
3336 pin_opts
->display
= str_dup (value
);
3339 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3342 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3344 xfree (pin_opts
->desc
);
3345 pin_opts
->desc
= NULL
;
3346 if (value
&& *value
)
3347 pin_opts
->desc
= str_dup (value
);
3349 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3351 xfree (pin_opts
->title
);
3352 pin_opts
->title
= NULL
;
3353 if (value
&& *value
)
3354 pin_opts
->title
= str_dup (value
);
3356 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3358 xfree (pin_opts
->prompt
);
3359 pin_opts
->prompt
= NULL
;
3360 if (value
&& *value
)
3361 pin_opts
->prompt
= str_dup (value
);
3364 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3372 n
= (int) strtol (value
, &p
, 10);
3375 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3377 pin_opts
->timeout
= n
;
3378 MUTEX_LOCK (&rcfile_mutex
);
3379 config_set_int_param (&global_config
,
3380 client
->filename
? client
->filename
: "global",
3381 "pinentry_timeout", value
);
3382 MUTEX_UNLOCK (&rcfile_mutex
);
3385 else if (strcasecmp (name
, "disable-pinentry") == 0)
3390 if (value
&& *value
)
3392 n
= (int) strtol (value
, &p
, 10);
3393 if (*p
|| n
< 0 || n
> 1)
3394 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3398 client
->flags
|= FLAG_NO_PINENTRY
;
3400 client
->flags
&= ~FLAG_NO_PINENTRY
;
3405 if (client
->flags
& FLAG_NO_PINENTRY
)
3406 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3409 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3413 return send_error (ctx
, rc
);
3418 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3422 if (!rc
&& use_agent
&& agent
)
3424 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3429 return send_error (ctx
, rc
);
3433 do_rename (assuan_context_t ctx
, char *line
)
3435 struct client_s
*client
= assuan_get_pointer (ctx
);
3437 char **req
, **src
, *dst
;
3440 req
= str_split (line
, " ", 0);
3442 if (!req
|| !req
[0] || !req
[1])
3445 return GPG_ERR_SYNTAX
;
3449 is_literal_element (&dst
);
3451 if (!valid_xml_element ((xmlChar
*) dst
))
3454 return GPG_ERR_INV_VALUE
;
3457 if (strchr (req
[0], '\t'))
3458 src
= str_split (req
[0], "\t", 0);
3460 src
= str_split (req
[0], " ", 0);
3464 rc
= GPG_ERR_SYNTAX
;
3468 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3470 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3471 NULL
, 0, 0, NULL
, 0);
3476 rc
= is_element_owner (client
, n
);
3480 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3483 rc
= GPG_ERR_ENOMEM
;
3487 /* To prevent unwanted effects:
3489 * <root name="a"><b/></root>
3493 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3496 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3506 for (p
= src
; *p
; p
++)
3510 strv_printf (&tmp
, "%s", *p
);
3514 strv_printf (&tmp
, "!%s", dst
);
3515 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3516 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3523 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3524 NULL
, NULL
, 0, 0, NULL
, 0);
3527 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3532 /* Target may exist:
3535 * <root name="b" target="a"/>
3544 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3550 rc
= is_element_owner (client
, ndst
);
3554 unlink_node (client
, ndst
);
3555 xmlFreeNodeList (ndst
);
3558 rc
= add_attribute (client
, n
, "_name", dst
);
3567 rename_command (assuan_context_t ctx
, char *line
)
3569 struct client_s
*client
= assuan_get_pointer (ctx
);
3571 struct argv_s
*args
[] = {
3572 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3576 rc
= parse_options (&line
, args
, client
);
3578 return send_error (ctx
, rc
);
3580 if (client
->opts
& OPT_INQUIRE
)
3582 unsigned char *result
;
3585 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3587 return send_error (ctx
, rc
);
3589 pthread_cleanup_push (xfree
, result
);
3590 rc
= do_rename (ctx
, (char *)result
);
3591 pthread_cleanup_pop (1);
3594 rc
= do_rename (ctx
, line
);
3596 return send_error (ctx
, rc
);
3600 do_copy (assuan_context_t ctx
, char *line
)
3602 struct client_s
*client
= assuan_get_pointer (ctx
);
3604 char **req
, **src
= NULL
, **dst
= NULL
;
3605 xmlNodePtr nsrc
, ndst
, new = NULL
;
3607 req
= str_split (line
, " ", 0);
3608 if (!req
|| !req
[0] || !req
[1])
3611 return GPG_ERR_SYNTAX
;
3614 if (strchr (req
[0], '\t'))
3615 src
= str_split (req
[0], "\t", 0);
3617 src
= str_split (req
[0], " ", 0);
3621 rc
= GPG_ERR_SYNTAX
;
3625 if (strchr (req
[1], '\t'))
3626 dst
= str_split (req
[1], "\t", 0);
3628 dst
= str_split (req
[1], " ", 0);
3632 rc
= GPG_ERR_SYNTAX
;
3636 if (!valid_element_path (dst
, 0))
3638 rc
= GPG_ERR_INV_VALUE
;
3642 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3644 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3645 NULL
, NULL
, 0, 0, NULL
, 0);
3650 new = xmlCopyNodeList (nsrc
);
3653 rc
= GPG_ERR_ENOMEM
;
3658 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3662 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3663 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3670 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3672 else if (!ndst
|| create
)
3674 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3679 rc
= is_element_owner (client
, ndst
);
3683 /* Merge any attributes from the src node to the initial dst node. */
3684 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3686 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3689 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3693 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3694 xmlNewProp (ndst
, attr
->name
, tmp
);
3696 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3699 xmlNodePtr n
= ndst
->children
;
3701 xmlFreeNodeList (n
);
3702 ndst
->children
= NULL
;
3706 n
= xmlCopyNodeList (new->children
);
3709 rc
= GPG_ERR_ENOMEM
;
3713 n
= xmlAddChildList (ndst
, n
);
3716 rc
= GPG_ERR_ENOMEM
;
3720 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3721 ndst
->parent
? ndst
: ndst
->parent
);
3727 xmlUnlinkNode (new);
3728 xmlFreeNodeList (new);
3744 copy_command (assuan_context_t ctx
, char *line
)
3746 struct client_s
*client
= assuan_get_pointer (ctx
);
3748 struct argv_s
*args
[] = {
3749 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3753 rc
= parse_options (&line
, args
, client
);
3755 return send_error (ctx
, rc
);
3757 if (client
->opts
& OPT_INQUIRE
)
3759 unsigned char *result
;
3762 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3764 return send_error (ctx
, rc
);
3766 pthread_cleanup_push (xfree
, result
);
3767 rc
= do_copy (ctx
, (char *)result
);
3768 pthread_cleanup_pop (1);
3771 rc
= do_copy (ctx
, line
);
3773 return send_error (ctx
, rc
);
3777 do_move (assuan_context_t ctx
, char *line
)
3779 struct client_s
*client
= assuan_get_pointer (ctx
);
3781 char **req
, **src
= NULL
, **dst
= NULL
;
3782 xmlNodePtr nsrc
, ndst
= NULL
;
3784 req
= str_split (line
, " ", 0);
3786 if (!req
|| !req
[0] || !req
[1])
3789 return GPG_ERR_SYNTAX
;
3792 if (strchr (req
[0], '\t'))
3793 src
= str_split (req
[0], "\t", 0);
3795 src
= str_split (req
[0], " ", 0);
3799 rc
= GPG_ERR_SYNTAX
;
3803 if (strchr (req
[1], '\t'))
3804 dst
= str_split (req
[1], "\t", 0);
3806 dst
= str_split (req
[1], " ", 0);
3808 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3810 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3811 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3816 rc
= is_element_owner (client
, nsrc
);
3822 if (!valid_element_path (dst
, 0))
3824 rc
= GPG_ERR_INV_VALUE
;
3828 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3830 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3831 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3834 ndst
= xmlDocGetRootElement (client
->doc
);
3836 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3840 rc
= GPG_ERR_CONFLICT
;
3845 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3852 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3854 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
3866 if (ndst
== xmlDocGetRootElement (client
->doc
))
3868 xmlNodePtr n
= nsrc
;
3871 while (n
->parent
&& n
->parent
!= ndst
)
3874 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3875 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3877 if (xmlStrEqual (a
, b
))
3880 xmlUnlinkNode (nsrc
);
3882 xmlFreeNodeList (n
);
3890 xmlUnlinkNode (dup
);
3891 xmlFreeNodeList (dup
);
3895 xmlUnlinkNode (dup
);
3901 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3903 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3904 && !strcmp ((char *) name
, *dst
))
3907 rc
= GPG_ERR_CONFLICT
;
3912 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3918 update_element_mtime (client
, nsrc
->parent
);
3919 xmlUnlinkNode (nsrc
);
3920 ndst
= xmlAddChildList (ndst
, nsrc
);
3923 rc
= GPG_ERR_ENOMEM
;
3925 update_element_mtime (client
, ndst
->parent
);
3941 move_command (assuan_context_t ctx
, char *line
)
3943 struct client_s
*client
= assuan_get_pointer (ctx
);
3945 struct argv_s
*args
[] = {
3946 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3950 rc
= parse_options (&line
, args
, client
);
3952 return send_error (ctx
, rc
);
3954 if (client
->opts
& OPT_INQUIRE
)
3956 unsigned char *result
;
3959 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3961 return send_error (ctx
, rc
);
3963 pthread_cleanup_push (xfree
, result
);
3964 rc
= do_move (ctx
, (char *)result
);
3965 pthread_cleanup_pop (1);
3968 rc
= do_move (ctx
, line
);
3970 return send_error (ctx
, rc
);
3974 ls_command (assuan_context_t ctx
, char *line
)
3977 char *tmp
= str_asprintf ("%s/data", homedir
);
3978 char *dir
= expand_homedir (tmp
);
3979 DIR *d
= opendir (dir
);
3981 rc
= gpg_error_from_errno (errno
);
3987 return send_error (ctx
, rc
);
3991 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3992 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3996 pthread_cleanup_push (xfree
, p
);
3997 pthread_cleanup_push (closedir
, d
);
4000 while (!readdir_r (d
, p
, &cur
) && cur
)
4002 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
4004 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
4005 && cur
->d_name
[2] == '\0')
4008 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
4015 rc
= GPG_ERR_ENOMEM
;
4023 pthread_cleanup_pop (1); // closedir (d)
4024 pthread_cleanup_pop (1); // xfree (p)
4027 return send_error (ctx
, rc
);
4030 return send_error (ctx
, GPG_ERR_NO_DATA
);
4032 list
[strlen (list
) - 1] = 0;
4033 pthread_cleanup_push (xfree
, list
);
4034 rc
= xfer_data (ctx
, list
, strlen (list
));
4035 pthread_cleanup_pop (1);
4036 return send_error (ctx
, rc
);
4040 bye_notify (assuan_context_t ctx
, char *line
)
4042 struct client_s
*cl
= assuan_get_pointer (ctx
);
4045 if (cl
->thd
->remote
)
4051 struct timeval tv
= { 0, 50000 };
4053 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
4054 if (rc
== GNUTLS_E_AGAIN
)
4055 select (0, NULL
, NULL
, NULL
, &tv
);
4057 while (rc
== GNUTLS_E_AGAIN
);
4061 /* This will let assuan_process_next() return. */
4062 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
4063 cl
->last_rc
= 0; // BYE command result
4068 reset_notify (assuan_context_t ctx
, char *line
)
4070 struct client_s
*client
= assuan_get_pointer (ctx
);
4073 cleanup_client (client
);
4079 * This is called before every Assuan command.
4082 command_startup (assuan_context_t ctx
, const char *name
)
4084 struct client_s
*client
= assuan_get_pointer (ctx
);
4086 struct command_table_s
*cmd
= NULL
;
4088 log_write1 ("command='%s'", name
);
4089 client
->last_rc
= client
->opts
= 0;
4091 for (int i
= 0; command_table
[i
]; i
++)
4093 if (!strcasecmp (name
, command_table
[i
]->name
))
4095 if (command_table
[i
]->ignore_startup
)
4097 cmd
= command_table
[i
];
4102 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4107 * This is called after every Assuan command.
4110 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4112 struct client_s
*client
= assuan_get_pointer (ctx
);
4114 if (!(client
->flags
& FLAG_LOCK_CMD
))
4115 unlock_file_mutex (client
, 0);
4117 log_write1 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4118 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
4120 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4125 help_command (assuan_context_t ctx
, char *line
)
4130 if (!line
|| !*line
)
4133 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
4134 "For commands that take an element path as an argument, each element is "
4135 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4138 for (i
= 0; command_table
[i
]; i
++)
4140 if (!command_table
[i
]->help
)
4143 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4148 tmp
= strip_texi_and_wrap (help
);
4150 pthread_cleanup_push (xfree
, tmp
);
4151 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4152 pthread_cleanup_pop (1);
4153 return send_error (ctx
, rc
);
4156 for (i
= 0; command_table
[i
]; i
++)
4158 if (!strcasecmp (line
, command_table
[i
]->name
))
4162 if (!command_table
[i
]->help
)
4165 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4166 tmp
= str_asprintf (_("Usage: %s"), help
);
4168 pthread_cleanup_push (xfree
, tmp
);
4169 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4170 pthread_cleanup_pop (1);
4171 return send_error (ctx
, rc
);
4175 return send_error (ctx
, GPG_ERR_INV_NAME
);
4179 new_command (const char *name
, int ignore
, int unlock
,
4180 gpg_error_t (*handler
) (assuan_context_t
, char *),
4186 for (i
= 0; command_table
[i
]; i
++);
4189 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4190 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4191 command_table
[i
]->name
= name
;
4192 command_table
[i
]->handler
= handler
;
4193 command_table
[i
]->ignore_startup
= ignore
;
4194 command_table
[i
]->unlock
= unlock
;
4195 command_table
[i
++]->help
= help
;
4196 command_table
[i
] = NULL
;
4204 for (i
= 0; command_table
[i
]; i
++)
4205 xfree (command_table
[i
]);
4207 xfree (command_table
);
4211 sort_commands (const void *arg1
, const void *arg2
)
4213 struct command_table_s
*const *a
= arg1
;
4214 struct command_table_s
*const *b
= arg2
;
4223 return strcmp ((*a
)->name
, (*b
)->name
);
4226 // FIXME cleanup/implement options
4228 passwd_command (assuan_context_t ctx
, char *line
)
4230 struct client_s
*client
= assuan_get_pointer (ctx
);
4232 struct argv_s
*args
[] = {
4233 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4234 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4235 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4239 rc
= peer_is_invoker (client
);
4240 if (rc
== GPG_ERR_EACCES
)
4241 return send_error (ctx
, rc
);
4243 if (client
->flags
& FLAG_NEW
)
4244 return send_error (ctx
, GPG_ERR_INV_STATE
);
4246 client
->crypto
->save
.s2k_count
=
4247 config_get_ulong (client
->filename
, "s2k_count");
4248 rc
= parse_options (&line
, args
, client
);
4250 return send_error (ctx
, rc
);
4252 if (!rc
&& client
->opts
& OPT_RESET
)
4254 rc
= cache_clear (client
->md5file
);
4256 send_status_all (STATUS_CACHE
, NULL
);
4261 if (!IS_PKI (client
->crypto
))
4263 struct crypto_s
*crypto
;
4265 xfree (client
->crypto
->filename
);
4266 client
->crypto
->filename
= str_dup (client
->filename
);
4267 rc
= change_passwd (ctx
, client
->filename
,
4268 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4269 (client
->opts
& OPT_NO_PASSPHRASE
));
4272 cleanup_crypto (&client
->crypto
);
4273 client
->crypto
= crypto
;
4274 update_checksum (client
);
4275 cleanup_crypto_stage1 (client
->crypto
);
4281 if (client
->crypto
->save
.s2k_count
)
4282 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4283 "OPTION s2k-count=%lu",
4284 client
->crypto
->save
.s2k_count
);
4287 rc
= agent_passwd (client
->crypto
);
4292 return send_error (ctx
, rc
);
4296 parse_keygrip_opt_sign (void *data
, void *value
)
4298 struct client_s
*client
= data
;
4301 client
->opts
|= OPT_SIGN
;
4306 keygrip_command (assuan_context_t ctx
, char *line
)
4308 struct client_s
*client
= assuan_get_pointer (ctx
);
4310 struct crypto_s
*crypto
= NULL
;
4311 struct argv_s
*args
[] = {
4312 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4316 if (!line
|| !*line
)
4317 return send_error (ctx
, GPG_ERR_SYNTAX
);
4319 rc
= parse_options (&line
, args
, client
);
4321 return send_error (ctx
, rc
);
4323 if (!valid_filename (line
))
4324 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4326 rc
= init_client_crypto (&crypto
);
4328 return send_error (ctx
, rc
);
4330 rc
= read_data_file (line
, crypto
);
4333 char *hexgrip
= NULL
;
4335 if (!IS_PKI (crypto
))
4337 cleanup_crypto (&crypto
);
4338 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4341 if (client
->opts
& OPT_SIGN
)
4343 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4344 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4348 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4351 rc
= GPG_ERR_ENOMEM
;
4353 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4358 UPDATE_AGENT_CTX (client
, crypto
);
4359 cleanup_crypto (&crypto
);
4360 return send_error (ctx
, rc
);
4364 parse_opt_data (void *data
, void *value
)
4366 struct client_s
*client
= data
;
4369 client
->opts
|= OPT_DATA
;
4374 send_client_list (assuan_context_t ctx
)
4376 struct client_s
*client
= assuan_get_pointer (ctx
);
4378 char buf
[ASSUAN_LINELENGTH
];
4380 if (client
->opts
& OPT_VERBOSE
)
4386 MUTEX_LOCK (&cn_mutex
);
4387 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4388 t
= slist_length (cn_thread_list
);
4390 for (i
= 0; i
< t
; i
++)
4392 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4394 if (!strv_printf (&list
, "%p %s %s", thd
->tid
,
4395 thd
->name
? thd
->name
: "-",
4396 thd
->cl
&& thd
->cl
->filename
4397 ? thd
->cl
->filename
: "-"))
4400 rc
= GPG_ERR_ENOMEM
;
4405 pthread_cleanup_pop (1);
4409 line
= strv_join ("\n", list
);
4411 pthread_cleanup_push (xfree
, line
);
4412 rc
= xfer_data (ctx
, line
, strlen (line
));
4413 pthread_cleanup_pop (1);
4417 if (client
->opts
& OPT_DATA
)
4419 MUTEX_LOCK (&cn_mutex
);
4420 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4421 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4422 pthread_cleanup_pop (1);
4423 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4426 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4432 getinfo_command (assuan_context_t ctx
, char *line
)
4434 struct client_s
*client
= assuan_get_pointer (ctx
);
4436 char buf
[ASSUAN_LINELENGTH
];
4437 struct argv_s
*args
[] = {
4438 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4439 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4443 rc
= parse_options (&line
, args
, client
);
4445 return send_error (ctx
, rc
);
4447 if (!strcasecmp (line
, "clients"))
4449 rc
= send_client_list (ctx
);
4451 else if (!strcasecmp (line
, "cache"))
4453 if (client
->opts
& OPT_DATA
)
4455 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4456 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4459 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4461 else if (!strcasecmp (line
, "pid"))
4464 pid_t pid
= getpid ();
4466 snprintf (buf
, sizeof (buf
), "%i", pid
);
4467 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4469 else if (!strcasecmp (line
, "version"))
4471 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4478 "", use_agent
? "AGENT" : "");
4479 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4482 else if (!strcasecmp (line
, "last_error"))
4484 if (client
->last_error
)
4485 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4487 rc
= GPG_ERR_NO_DATA
;
4489 else if (!strcasecmp (line
, "user"))
4494 if (client
->thd
->remote
)
4495 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4497 user
= get_username (client
->thd
->peer
->uid
);
4499 user
= get_username (client
->thd
->peer
->uid
);
4503 pthread_cleanup_push (xfree
, user
);
4504 rc
= xfer_data (ctx
, user
, strlen (user
));
4505 pthread_cleanup_pop (1);
4508 rc
= GPG_ERR_NO_DATA
;
4511 rc
= gpg_error (GPG_ERR_SYNTAX
);
4513 return send_error (ctx
, rc
);
4518 send_data_cb (void *user
, const void *buf
, size_t len
)
4520 assuan_context_t ctx
= user
;
4522 return assuan_send_data (ctx
, buf
, len
);
4526 send_status_cb (void *user
, const char *line
)
4528 assuan_context_t ctx
= user
;
4529 char keyword
[200], *k
;
4532 for (p
= line
, k
= keyword
; *p
; p
++)
4544 while (isspace (*p
))
4547 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4552 kill_command (assuan_context_t ctx
, char *line
)
4554 #ifdef HAVE_PTHREAD_CANCEL
4555 struct client_s
*client
= assuan_get_pointer (ctx
);
4558 if (!line
|| !*line
)
4559 return send_error (ctx
, GPG_ERR_SYNTAX
);
4561 rc
= peer_is_invoker (client
);
4566 MUTEX_LOCK (&cn_mutex
);
4567 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4568 t
= slist_length (cn_thread_list
);
4570 for (i
= 0; i
< t
; i
++)
4572 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4573 char *tmp
= str_asprintf ("%p", thd
->tid
);
4575 if (strcmp (line
, tmp
))
4582 rc
= pthread_cancel (thd
->tid
);
4586 pthread_cleanup_pop (1);
4589 return send_error (ctx
, rc
);
4591 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4596 agent_command (assuan_context_t ctx
, char *line
)
4601 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4604 struct client_s
*client
= assuan_get_pointer (ctx
);
4606 if (!line
|| !*line
)
4607 return send_error (ctx
, GPG_ERR_SYNTAX
);
4609 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4610 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4611 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4612 send_status_cb
, client
->ctx
);
4613 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4618 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4621 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4623 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4627 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4629 return send_error (ctx
, rc
);
4635 /* !BEGIN-HELP-TEXT!
4637 * This comment is used as a marker to generate the offline documentation
4638 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4639 * script to determine where commands begin and end.
4641 new_command("HELP", 1, 1, help_command
, _(
4642 "HELP [<COMMAND>]\n"
4643 "Show available commands or command specific help text."
4646 new_command("AGENT", 1, 1, agent_command
, _(
4648 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4649 "@command{gpg-agent}."
4652 new_command("KILL", 1, 0, kill_command
, _(
4653 "KILL <thread_id>\n"
4654 "Terminates the client identified by @var{thread_id} and releases any file "
4655 "lock or other resources it has held. @xref{GETINFO} for details about listing "
4656 "connected clients.\n"
4659 new_command("GETINFO", 1, 1, getinfo_command
, _(
4660 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4661 "Get server and other information: @var{CACHE} returns the number of cached "
4662 "documents via a status message. @var{CLIENTS} returns the number of "
4663 "connected clients via a status message or a list of connected clients when "
4664 "the @option{--verbose} parameter is used. The list contains space delimited "
4665 "fields specifying the thread ID, client name and opened file and is always "
4666 "returned with a data response. @var{PID} returns the process ID "
4667 "number of the server via a data response. @var{VERSION} returns the server "
4668 "version number and compile-time features with a data response with each "
4669 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4670 "the last failed command when available. @var{USER} returns the username or "
4671 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4673 "When the @option{--data} option is specified then the result will be sent "
4674 "via a data response rather than a status message."
4677 new_command("PASSWD", 0, 0, passwd_command
, _(
4678 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4679 "Changes the passphrase of the secret key required to open the current "
4680 "file or the passphrase of a symmetrically encrypted data file. When the "
4681 "@option{--reset} option is passed then the cache entry for the current "
4682 "file will be reset and the passphrase, if any, will be required during the "
4683 "next @code{OPEN} (@pxref{OPEN})."
4685 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4686 "of hash iterations for a passphrase and must be either @code{0} to use "
4687 "the calibrated count of the machine (the default), or a value greater than "
4688 "or equal to @code{65536}. This option has no effect for symmetrically "
4689 "encrypted data files."
4691 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4692 "the data file, although a passphrase may be required when changing it."
4694 "This command is not available for non-invoking clients "
4695 "(@pxref{Access Control})."
4698 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4699 "KEYGRIP [--sign] <filename>\n"
4700 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4703 "When the @option{--sign} option is specified then the key used for signing "
4704 "of the specified @var{filename} will be returned."
4706 "For symmetrically encrypted data files this command returns the error "
4707 "GPG_ERR_NOT_SUPPORTED."
4710 new_command("OPEN", 1, 1, open_command
, _(
4711 "OPEN [--lock] <filename> [<passphrase>]\n"
4712 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4713 "found on the file-system then a new document will be created. If the file "
4714 "is found, it is looked for in the file cache. If cached and no "
4715 "@var{passphrase} was specified then the cached document is opened. When not "
4716 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4717 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4720 "When the @option{--lock} option is passed then the file mutex will be "
4721 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4722 "file has been opened."
4725 new_command("SAVE", 0, 0, save_command
, _(
4726 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4727 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4728 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4729 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4730 "keypair will be generated and a pinentry will be used to prompt for the "
4731 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4732 "passed in which case the data file will not be passphrase protected. "
4734 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4735 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4736 "use is enabled. The datafile will be symmetrically encrypted and will not "
4737 "use or generate any keypair."
4739 "The @option{--reset} option will clear the cache entry for the current file "
4740 "and require a passphrase, if needed, before saving."
4742 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4743 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4744 "(@pxref{Configuration}) for available ciphers."
4746 "The @option{--cipher-iterations} option specifies the number of times to "
4747 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4749 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4750 "the client to obtain the key paramaters to use when generating a new "
4751 "keypair. The inquired data is expected to be an S-expression. If not "
4752 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4753 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4754 "that when this option is specified a new keypair will be generated "
4755 "reguardless if the file is a new one and that if the data file is protected "
4756 "the passphrase to open it will be required before generating the new "
4757 "keypair. This option is not available for non-invoking clients "
4758 "(@pxref{Access Control})."
4760 "You can encrypt the data file to a public key other than the one that it "
4761 "was originally encrypted with by passing the @option{--keygrip} option with "
4762 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4763 "be of any key that @command{gpg-agent} knows about. The "
4764 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4765 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4766 "keygrip of an existing data file. This option may be needed when using a "
4767 "smartcard. This option has no effect with symmetrically encrypted data "
4768 "files. These options are not available for non-invoking clients "
4769 "(@pxref{Access Control})."
4771 "The @option{--s2k-count} option sets number of hash iterations for a "
4772 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4773 "value and is the default. This setting only affects new files. To change "
4774 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4775 "has no effect with symmetrically encrypted data files."
4778 new_command("ISCACHED", 1, 0, iscached_command
, _(
4779 "ISCACHED [--lock] <filename>\n"
4780 "An @emph{OK} response is returned if the specified @var{filename} is found "
4781 "in the file cache. If not found in the cache but exists on the filesystem "
4782 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4785 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4786 "file exists; it does not need to be opened nor cached. The lock will be "
4787 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4791 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4792 "CLEARCACHE [<filename>]\n"
4793 "Clears a file cache entry for all or the specified @var{filename}."
4796 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4797 "CACHETIMEOUT <filename> <seconds>\n"
4798 "The time in @var{seconds} until @var{filename} will be removed from the "
4799 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4800 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4801 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4805 new_command("LIST", 0, 1, list_command
, _(
4806 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4807 "If no element path is given then a newline separated list of root elements "
4808 "is returned with a data response. If given, then all reachable elements "
4809 "of the specified element path are returned unless the @option{--no-recurse} "
4810 "option is specified. If specified, only the child elements of the element "
4811 "path are returned without recursing into grandchildren. Each resulting "
4812 "element is prefixed with the literal @code{!} character when the element "
4813 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4815 "When the @option{--verbose} option is passed then each element path "
4816 "returned will have zero or more flags appened to it. These flags are "
4817 "delimited from the element path by a single space character. A flag itself "
4818 "is a single character. Flag @code{P} indicates that access to the element "
4819 "is denied. Flag @code{+} indicates that there are child nodes of "
4820 "the current element path. Flag @code{E} indicates that an element of an "
4821 "element path contained in a @var{target} attribute could not be found. Flag "
4822 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4823 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4824 "of the @var{target} attribute contained in the current element (see below)."
4826 "The @option{--with-target} option implies @option{--verbose} and will append "
4827 "an additional flag @code{T} followed by a single space then an element path. "
4828 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4829 "current element when it contains a @var{target} attribute. When no "
4830 "@var{target} attribute is found then no flag will be appended."
4832 "The @option{--no-recurse} option limits the amount of data returned to only "
4833 "the listing of children of the specified element path and not any "
4836 "The @option{--all} option lists the entire element tree for each root "
4837 "element. This option also implies option @option{--verbose}."
4839 "When the @option{--inquire} option is passed then all remaining non-option "
4840 "arguments are retrieved via a server @emph{INQUIRE}."
4843 new_command("REALPATH", 0, 1, realpath_command
, _(
4844 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4845 "Resolves all @code{target} attributes of the specified element path and "
4846 "returns the result with a data response. @xref{Target Attribute}, for details."
4848 "When the @option{--inquire} option is passed then all remaining non-option "
4849 "arguments are retrieved via a server @emph{INQUIRE}."
4852 new_command("STORE", 0, 1, store_command
, _(
4853 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4854 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4856 "Creates a new element path or modifies the @var{content} of an existing "
4857 "element. If only a single element is specified then a new root element is "
4858 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4859 "set to the final @key{TAB} delimited element. If no @var{content} is "
4860 "specified after the final @key{TAB}, then the content of an existing "
4861 "element will be removed; or empty when creating a new element."
4863 "The only restriction of an element name is that it not contain whitespace "
4864 "or begin with the literal element character @code{!} unless specifying a "
4865 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4866 "the @key{TAB} delimited elements. It is recommended that the content of an "
4867 "element be base64 encoded when it contains control or @key{TAB} characters "
4868 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4871 new_command("RENAME", 0, 1, rename_command
, _(
4872 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4873 "Renames the specified @var{element} to the new @var{value}. If an element of "
4874 "the same name as the @var{value} already exists it will be overwritten."
4876 "When the @option{--inquire} option is passed then all remaining non-option "
4877 "arguments are retrieved via a server @emph{INQUIRE}."
4880 new_command("COPY", 0, 1, copy_command
, _(
4881 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4882 "Copies the entire element tree starting from the child node of the source "
4883 "element, to the destination element path. If the destination element path "
4884 "does not exist then it will be created; otherwise it is overwritten."
4886 "Note that attributes from the source element are merged into the "
4887 "destination element when the destination element path exists. When an "
4888 "attribute of the same name exists in both the source and destination "
4889 "elements then the destination attribute will be updated to the source "
4892 "When the @option{--inquire} option is passed then all remaining non-option "
4893 "arguments are retrieved via a server @emph{INQUIRE}."
4896 new_command("MOVE", 0, 1, move_command
, _(
4897 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4898 "Moves the source element path to the destination element path. If the "
4899 "destination is not specified then it will be moved to the root node of the "
4900 "document. If the destination is specified and exists then it will be "
4901 "overwritten; otherwise non-existing elements of the destination element "
4902 "path will be created."
4904 "When the @option{--inquire} option is passed then all remaining non-option "
4905 "arguments are retrieved via a server @emph{INQUIRE}."
4908 new_command("DELETE", 0, 1, delete_command
, _(
4909 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4910 "Removes the specified element path and all of its children. This may break "
4911 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4912 "refers to this element or any of its children."
4914 "When the @option{--inquire} option is passed then all remaining non-option "
4915 "arguments are retrieved via a server @emph{INQUIRE}."
4918 new_command("GET", 0, 1, get_command
, _(
4919 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4920 "Retrieves the content of the specified element. The content is returned "
4921 "with a data response."
4923 "When the @option{--inquire} option is passed then all remaining non-option "
4924 "arguments are retrieved via a server @emph{INQUIRE}."
4927 new_command("ATTR", 0, 1, attr_command
, _(
4928 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4930 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4932 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4933 "element. When no @var{value} is specified any existing value will be removed."
4935 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4937 " Removes an @var{attribute} from an element."
4939 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4941 " Retrieves a newline separated list of attributes names and values "
4942 "from the specified element. Each attribute name and value is space delimited."
4944 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4946 " Retrieves the value of an @var{attribute} from an element."
4949 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4950 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4953 "The @code{_mtime} attribute is updated each time an element is modified by "
4954 "either storing content, editing attributes or by deleting a child element. "
4955 "The @code{_ctime} attribute is created for each new element in an element "
4958 "When the @option{--inquire} option is passed then all remaining non-option "
4959 "arguments are retrieved via a server @emph{INQUIRE}."
4961 "@xref{Target Attribute}, for details about this special attribute."
4964 new_command("XPATH", 0, 1, xpath_command
, _(
4965 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4966 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4967 "specified it is assumed the expression is a request to return a result. "
4968 "Otherwise, the result is set to the @var{value} argument and the document is "
4969 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4970 "is assumed to be empty and the document is updated. For example:"
4973 "XPATH //element[@@_name='password']@key{TAB}\n"
4976 "would clear the content of all @code{password} elements in the data file "
4977 "while leaving off the trailing @key{TAB} would return all @code{password} "
4978 "elements in @abbr{XML} format."
4980 "When the @option{--inquire} option is passed then all remaining non-option "
4981 "arguments are retrieved via a server @emph{INQUIRE}."
4983 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4984 "expression syntax."
4987 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4988 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4989 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4990 "attributes and does not return a result. For the @var{SET} operation the "
4991 "@var{value} is optional but the field is required. If not specified then "
4992 "the attribute value will be empty. For example:"
4995 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4998 "would create an @code{password} attribute for each @code{password} element "
4999 "found in the document. The attribute value will be empty but still exist."
5001 "When the @option{--inquire} option is passed then all remaining non-option "
5002 "arguments are retrieved via a server @emph{INQUIRE}."
5004 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5005 "expression syntax."
5008 new_command("IMPORT", 0, 1, import_command
, _(
5009 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
5010 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5012 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
5013 "argument is raw @abbr{XML} data. The content is created as a child of "
5014 "the element path specified with the @option{--root} option or at the "
5015 "document root when not specified. Existing elements of the same name will "
5018 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
5022 new_command("DUMP", 0, 1, dump_command
, _(
5024 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
5025 "dumping a specific node."
5028 new_command("LOCK", 0, 0, lock_command
, _(
5030 "Locks the mutex associated with the opened file. This prevents other clients "
5031 "from sending commands to the same opened file until the client "
5032 "that sent this command either disconnects or sends the @code{UNLOCK} "
5033 "command. @xref{UNLOCK}."
5036 new_command("UNLOCK", 1, 0, unlock_command
, _(
5038 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
5039 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
5040 "@pxref{ISCACHED})."
5043 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
5044 "GETCONFIG [filename] <parameter>\n"
5045 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
5046 "data response. If no file has been opened then the value for @var{filename} "
5047 "or the default from the @samp{global} section will be returned. If a file "
5048 "has been opened and no @var{filename} is specified, a value previously "
5049 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
5052 new_command("OPTION", 1, 1, option_command
, _(
5053 "OPTION <NAME>=<VALUE>\n"
5054 "Sets a client option @var{name} to @var{value}. The value for an option is "
5055 "kept for the duration of the connection."
5058 "@item DISABLE-PINENTRY\n"
5059 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
5060 "server inquire is sent to the client to obtain the passphrase. This option "
5061 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
5062 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
5064 "@item PINENTRY-TIMEOUT\n"
5065 "Sets the number of seconds before a pinentry prompt will return an error "
5066 "while waiting for user input."
5069 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5072 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5075 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5077 "@item PINENTRY-DESC\n"
5078 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
5080 "@item PINENTRY-TITLE\n"
5081 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
5083 "@item PINENTRY-PROMPT\n"
5084 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
5087 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5089 "@item LC-MESSAGES\n"
5090 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5093 "Associates the thread ID of the connection with the specified textual "
5094 "representation. Useful for debugging log messages."
5096 "@item LOCK-TIMEOUT\n"
5097 "When not @code{0}, the duration in tenths of a second to wait for the file "
5098 "mutex which has been locked by another thread to be released before returning "
5099 "an error. When @code{-1}, then an error will be returned immediately."
5102 "An integer specifiying the logging level."
5106 new_command("LS", 1, 1, ls_command
, _(
5108 "Lists the available data files stored in the data directory "
5109 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5112 new_command("RESET", 1, 1, NULL
, _(
5114 "Closes the currently opened file but keeps any previously set client options."
5117 new_command("NOP", 1, 1, NULL
, _(
5119 "Does nothing. Always returns successfully."
5122 /* !END-HELP-TEXT! */
5123 new_command ("CANCEL", 1, 1, NULL
, NULL
);
5124 new_command ("END", 1, 1, NULL
, NULL
);
5125 new_command ("BYE", 1, 1, NULL
, NULL
);
5128 for (i
= 0; command_table
[i
]; i
++);
5129 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
5134 register_commands (assuan_context_t ctx
)
5138 for (; command_table
[i
]; i
++)
5140 if (!command_table
[i
]->handler
)
5143 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5144 command_table
[i
]->handler
,
5145 command_table
[i
]->help
);
5150 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5154 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5158 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5162 return assuan_register_post_cmd_notify (ctx
, command_finalize
);