2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
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 /* These are command option flags. */
52 #define OPT_INQUIRE 0x0001
53 #define OPT_NO_PASSPHRASE 0x0002
54 #define OPT_RESET 0x0004
55 #define OPT_LIST_RECURSE 0x0008
56 #define OPT_VERBOSE 0x0010
57 #define OPT_LOCK 0x0020
58 #define OPT_LOCK_ON_OPEN 0x0040
59 #define OPT_SIGN 0x0080
60 #define OPT_LIST_ALL 0x0100
61 #define OPT_LIST_WITH_TARGET 0x0200
62 #define OPT_DATA 0x0400
63 #define OPT_NO_AGENT 0x0800
66 /* The GETCONFIG command, for example, may fail because pwmd lost the
67 * gpg-agent connection. Update the recovered agent ctx. */
68 #define UPDATE_AGENT_CTX(client, crypto) do { \
69 if (crypto && crypto->agent && crypto->agent->did_restart \
70 && client->crypto && client->crypto->agent \
71 && client->crypto->agent->ctx && \
72 crypto->agent->ctx != client->crypto->agent->ctx) \
74 client->crypto->agent->ctx = crypto->agent->ctx; \
75 crypto->agent->ctx = NULL; \
79 #define UPDATE_AGENT_CTX(client, crypto)
82 struct command_table_s
85 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
88 int unlock
; // unlock the file mutex after validating the checksum
91 static struct command_table_s
**command_table
;
93 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
94 static gpg_error_t
validate_checksum (struct client_s
*,
95 struct cache_data_s
*);
96 static gpg_error_t
update_checksum (struct client_s
*client
);
98 /* When 'status' is true the 'self' field of the status line will be false
99 * because we never send the STATE status message to the same client that
102 build_client_info_line (struct client_thread_s
*thd
, int status
)
104 MUTEX_LOCK (&cn_mutex
);
105 int with_state
= config_get_boolean ("global", "send_state");
111 uid
= str_asprintf("#%s", thd
->tls
->fp
);
113 uid
= str_asprintf("%u", thd
->peer
->uid
);
115 uid
= str_asprintf("%u", thd
->peer
->uid
);
117 line
= str_asprintf ("%p %s %s %s %u %u %u %s",
119 thd
->name
? thd
->name
: "-",
120 thd
->cl
&& thd
->cl
->filename
121 ? thd
->cl
->filename
: "/",
123 thd
->remote
? thd
->peeraddr
: "-",
127 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
128 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
129 with_state
? thd
->state
: CLIENT_STATE_UNKNOWN
, uid
133 MUTEX_UNLOCK (&cn_mutex
);
138 update_client_state (struct client_s
*client
, unsigned s
)
140 client
->thd
->state
= s
;
141 int n
= config_get_boolean ("global", "send_state");
143 if (n
&& client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
145 char *line
= build_client_info_line (client
->thd
, 1);
148 send_status_all_not_self (n
== 2, STATUS_STATE
, "%s", line
);
153 unlock_file_mutex (struct client_s
*client
, int remove
)
157 // OPEN: keep the lock for the same file being reopened.
158 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
161 if (!(client
->flags
& FLAG_HAS_LOCK
))
162 return GPG_ERR_NOT_LOCKED
;
164 rc
= cache_unlock_mutex (client
->md5file
, remove
);
166 rc
= GPG_ERR_INV_STATE
;
168 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
174 lock_file_mutex (struct client_s
*client
, int add
)
177 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
179 if (client
->flags
& FLAG_HAS_LOCK
)
182 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
183 client
->lock_timeout
, add
, timeout
);
185 client
->flags
|= FLAG_HAS_LOCK
;
191 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
195 if (!(client
->flags
& FLAG_OPEN
))
196 return GPG_ERR_INV_STATE
;
198 rc
= lock_file_mutex (client
, 0);
199 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
201 rc
= validate_checksum (client
, NULL
);
202 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
205 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
208 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
209 unlock_file_mutex (client
, 0);
215 parse_xml (assuan_context_t ctx
, int new)
217 struct client_s
*client
= assuan_get_pointer (ctx
);
218 int cached
= client
->doc
!= NULL
;
223 client
->doc
= new_document ();
229 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
230 client
->crypto
->plaintext
= result
;
231 client
->crypto
->plaintext_len
= len
;
232 if (!client
->crypto
->plaintext
)
234 xmlFreeDoc (client
->doc
);
243 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
244 client
->crypto
->plaintext_len
, (xmlDocPtr
*)&client
->doc
);
250 free_client (struct client_s
*client
)
253 xmlFreeDoc (client
->doc
);
256 xfree (client
->filename
);
257 xfree (client
->last_error
);
261 cleanup_crypto_stage2 (client
->crypto
);
262 if (client
->crypto
->pkey_sexp
)
263 gcry_sexp_release (client
->crypto
->pkey_sexp
);
265 if (client
->crypto
->sigpkey_sexp
)
266 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
268 client
->crypto
->pkey_sexp
= NULL
;
269 client
->crypto
->sigpkey_sexp
= NULL
;
270 memset (client
->crypto
->sign_grip
, 0,
271 sizeof (client
->crypto
->sign_grip
));
272 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
277 cleanup_client (struct client_s
*client
)
279 assuan_context_t ctx
= client
->ctx
;
280 struct client_thread_s
*thd
= client
->thd
;
281 struct crypto_s
*crypto
= client
->crypto
;
282 long lock_timeout
= client
->lock_timeout
;
283 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
284 struct pinentry_option_s pin_opts
;
285 xmlErrorPtr xml_error
= client
->xml_error
;
287 struct pinentry_option_s agent_pin_opts
;
289 if (crypto
&& crypto
->agent
)
290 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
291 sizeof(struct pinentry_option_s
));
294 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
295 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
296 free_client (client
);
297 memset (client
, 0, sizeof (struct client_s
));
298 client
->xml_error
= xml_error
;
299 client
->crypto
= crypto
;
302 client
->lock_timeout
= lock_timeout
;
303 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
304 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
306 if (crypto
&& crypto
->agent
)
307 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
308 sizeof(struct pinentry_option_s
));
313 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
315 struct client_s
*client
= assuan_get_pointer (ctx
);
317 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
318 int cached
= 0, keyarg
= key
? 1 : 0;
320 unsigned char *salted_key
= NULL
;
324 char *pin_title
= client
->pinentry_opts
.title
;
326 client
->crypto
->filename
= str_dup (client
->filename
);
327 if (cdata
|| client
->flags
& FLAG_NEW
)
329 if (cdata
&& !(client
->flags
& FLAG_NEW
))
331 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
336 cached
= cdata
!= NULL
;
340 if (!key
&& !IS_PKI (client
->crypto
)
341 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
343 if (client
->flags
& FLAG_NO_PINENTRY
)
345 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
352 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
355 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
362 if (!IS_PKI (client
->crypto
))
364 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
367 key
= gcry_malloc (keylen
);
368 memset (key
, 0, keylen
);
371 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
372 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
373 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
374 (void **)&salted_key
, &keysize
);
380 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
381 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
382 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
384 gcry_free (salted_key
);
388 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
393 if (client
->pinentry_opts
.title
!= pin_title
)
394 xfree (client
->pinentry_opts
.title
);
396 client
->pinentry_opts
.title
= pin_title
;
399 gcry_free (salted_key
);
403 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
406 gcry_free (salted_key
);
407 return GPG_ERR_ENOMEM
;
410 cdata
->key
= salted_key
;
411 cdata
->keylen
= keysize
;
415 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
417 rc
= GPG_ERR_NOT_IMPLEMENTED
;
423 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
425 free_cache_data_once (cdata
);
429 int timeout
= config_get_integer (client
->filename
,
435 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
437 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
438 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
439 client
->crypto
->plaintext_len
, &cdata
->doc
,
440 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
442 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
444 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
445 "%S", client
->crypto
->pkey_sexp
);
450 gcry_sexp_release (cdata
->sigkey
);
452 cdata
->sigkey
= NULL
;
453 if (!rc
&& IS_PKI (client
->crypto
))
454 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
455 "%S", client
->crypto
->sigpkey_sexp
);
459 rc
= cache_add_file (client
->md5file
,
460 (client
->flags
& FLAG_NEW
) ? NULL
461 : client
->crypto
->grip
, cdata
, timeout
);
466 free_cache_data_once (cdata
);
467 xmlFreeDoc (client
->doc
);
474 client
->flags
|= FLAG_OPEN
;
479 update_checksum (client
);
481 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
482 rc
= do_lock (client
, 0);
488 req_cleanup (void *arg
)
493 strv_free ((char **) arg
);
497 parse_open_opt_lock (void *data
, void *value
)
499 struct client_s
*client
= data
;
501 client
->opts
|= OPT_LOCK_ON_OPEN
;
506 parse_save_opt_inquire (void *data
, void *value
)
508 struct client_s
*client
= data
;
511 if (!(client
->flags
& FLAG_NEW
))
513 rc
= peer_is_invoker (client
);
514 if (rc
== GPG_ERR_EACCES
)
519 client
->opts
|= OPT_INQUIRE
;
524 parse_opt_inquire (void *data
, void *value
)
526 struct client_s
*client
= data
;
529 client
->opts
|= OPT_INQUIRE
;
534 update_checksum (struct client_s
*client
)
538 struct cache_data_s
*cdata
;
539 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
546 cdata
= cache_get_data (client
->md5file
);
550 cdata
->crc
= xmalloc (len
);
551 memcpy (cdata
->crc
, crc
, len
);
558 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
565 if (cdata
&& !cdata
->crc
)
566 return GPG_ERR_CHECKSUM
;
568 rc
= get_checksum (client
->filename
, &crc
, &len
);
573 n
= memcmp (cdata
->crc
, crc
, len
);
574 else if (client
->crc
)
575 n
= memcmp (client
->crc
, crc
, len
);
578 return n
? GPG_ERR_CHECKSUM
: 0;
582 do_open (assuan_context_t ctx
, const char *password
)
584 struct client_s
*client
= assuan_get_pointer (ctx
);
585 struct cache_data_s
*cdata
;
590 cdata
= cache_get_data (client
->md5file
);
591 if (cdata
&& cdata
->doc
)
595 /* This will check that the key is cached in the agent which needs to
596 * be determined for files that share a keygrip. */
599 rc
= cache_iscached (client
->filename
, &defer
);
600 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
601 rc
= GPG_ERR_KEY_EXPIRED
;
604 if (!rc
&& !(client
->flags
& FLAG_NEW
))
605 rc
= validate_checksum (client
, cdata
);
608 if (!rc
&& client
->thd
->remote
609 && config_get_boolean (client
->filename
, "tcp_require_key"))
610 rc
= GPG_ERR_KEY_EXPIRED
;
613 if (!rc
&& !password
)
615 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
619 if (IS_PKI (client
->crypto
))
621 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
623 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
624 client
->crypto
->grip
);
625 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
627 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
628 client
->crypto
->sign_grip
);
633 rc
= open_finalize (ctx
, NULL
, 0);
639 /* There was an error accessing the file so clear the cache entry. The
640 * real error will be returned from read_data_file() since the file
641 * may have only disappeared. */
644 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
645 rc
= cache_clear (client
->md5file
);
646 send_status_all (STATUS_CACHE
, NULL
);
653 rc
= read_data_file (client
->filename
, client
->crypto
);
656 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
657 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
659 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
663 client
->flags
|= FLAG_NEW
;
664 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
665 memset (client
->crypto
->sign_grip
, 0,
666 sizeof (client
->crypto
->sign_grip
));
667 rc
= open_finalize (ctx
, NULL
, 0);
671 if (password
&& IS_PKI (client
->crypto
))
674 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
680 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
685 open_command (assuan_context_t ctx
, char *line
)
688 struct client_s
*client
= assuan_get_pointer (ctx
);
689 char **req
, *filename
;
690 unsigned char md5file
[16];
692 assuan_peercred_t peer
;
693 struct argv_s
*args
[] = {
694 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
698 rc
= parse_options (&line
, args
, client
);
700 return send_error (ctx
, rc
);
702 req
= str_split (line
, " ", 2);
704 return send_error (ctx
, GPG_ERR_SYNTAX
);
706 rc
= do_validate_peer (ctx
, req
[0], &peer
);
710 return send_error (ctx
, rc
);
714 if (!valid_filename (filename
))
717 return send_error (ctx
, GPG_ERR_INV_VALUE
);
720 pthread_cleanup_push (req_cleanup
, req
);
721 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
722 /* This client may have locked a different file with ISCACHED --lock than
723 * the current filename. This will remove that lock. */
724 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
725 if (client
->flags
& FLAG_OPEN
||
726 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
728 uint32_t opts
= client
->opts
;
729 uint32_t flags
= client
->flags
;
732 client
->flags
|= FLAG_KEEP_LOCK
;
733 else if (client
->flags
& FLAG_NEW
)
734 cache_clear (client
->md5file
);
736 cleanup_client (client
);
738 client
->flags
|= flags
;
739 client
->flags
&= ~(FLAG_LOCK_CMD
);
741 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
744 memcpy (client
->md5file
, md5file
, 16);
745 client
->filename
= str_dup (filename
);
746 if (!client
->filename
)
749 return send_error(ctx
, GPG_ERR_ENOMEM
);
752 /* Need to lock the mutex here because file_modified() cannot without
753 * knowing the filename. */
754 rc
= lock_file_mutex (client
, 1);
756 client
->flags
&= ~FLAG_OPEN
;
760 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
763 if (IS_PKI (client
->crypto
))
764 rc
= set_pinentry_mode (client
->crypto
->agent
,
765 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
770 rc
= do_open (ctx
, password
);
772 cleanup_client (client
);
774 cleanup_crypto_stage1 (client
->crypto
);
778 pthread_cleanup_pop (1);
780 if (!rc
&& client
->flags
& FLAG_NEW
)
781 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
784 (void) kill_scd (client
->crypto
->agent
);
787 return send_error (ctx
, rc
);
791 parse_opt_no_passphrase (void *data
, void *value
)
793 struct client_s
*client
= data
;
796 client
->opts
|= OPT_NO_PASSPHRASE
;
801 parse_save_opt_no_agent (void *data
, void *value
)
803 struct client_s
*client
= data
;
805 client
->opts
|= OPT_NO_AGENT
;
810 parse_save_opt_cipher (void *data
, void *value
)
812 struct client_s
*client
= data
;
813 int algo
= cipher_string_to_gcrypt ((char *) value
);
814 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
818 return GPG_ERR_INV_VALUE
;
820 if (!(client
->flags
& FLAG_NEW
))
822 rc
= peer_is_invoker (client
);
823 if (rc
== GPG_ERR_EACCES
)
827 flags
&= ((uint16_t) ((uint32_t) (flags
& 0xFFFFFFFF)));
828 if (!(client
->crypto
->hdr
.flags
& gcrypt_to_cipher (algo
)))
838 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
845 permitted_to_save (struct client_s
*client
, const unsigned char *grip
,
846 size_t size
, const char *value
)
851 if (!(client
->flags
& FLAG_NEW
))
853 hexgrip
= bin2hex (grip
, size
);
854 rc
= !strcmp (hexgrip
, (char *)value
) ? 0 : GPG_ERR_EACCES
;
857 rc
= peer_is_invoker (client
);
865 parse_save_opt_keygrip (void *data
, void *value
)
868 struct client_s
*client
= data
;
869 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->grip
,
870 sizeof (client
->crypto
->grip
),
873 if (!IS_PKI (client
->crypto
))
874 return GPG_ERR_INV_ARG
;
879 if (client
->crypto
->save
.pkey
)
880 gcry_sexp_release (client
->crypto
->save
.pkey
);
882 client
->crypto
->save
.pkey
= NULL
;
883 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
885 return GPG_ERR_INV_ARG
;
890 parse_save_opt_sign_keygrip (void *data
, void *value
)
893 struct client_s
*client
= data
;
894 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->sign_grip
,
895 sizeof (client
->crypto
->sign_grip
),
898 if (!IS_PKI (client
->crypto
))
899 return GPG_ERR_INV_ARG
;
904 if (client
->crypto
->save
.sigpkey
)
905 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
907 client
->crypto
->save
.sigpkey
= NULL
;
908 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
910 return GPG_ERR_INV_ARG
;
915 parse_opt_s2k_count (void *data
, void *value
)
917 struct client_s
*client
= data
;
921 return GPG_ERR_INV_VALUE
;
923 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
928 parse_save_opt_iterations (void *data
, void *value
)
930 struct client_s
*client
= data
;
935 return GPG_ERR_INV_VALUE
;
938 n
= strtoull (v
, &p
, 10);
939 if (n
== UINT64_MAX
&& errno
)
940 return gpg_error_from_errno (errno
);
942 return GPG_ERR_INV_VALUE
;
944 if (!(client
->flags
& FLAG_NEW
))
946 gpg_error_t rc
= peer_is_invoker (client
);
948 if (rc
== GPG_ERR_EACCES
)
950 if (client
->crypto
->hdr
.iterations
!= n
)
959 client
->crypto
->save
.hdr
.iterations
= n
;
964 save_finalize (assuan_context_t ctx
)
966 struct client_s
*client
= assuan_get_pointer (ctx
);
968 xmlChar
*xmlbuf
= NULL
;
973 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
975 return GPG_ERR_ENOMEM
;
977 pthread_cleanup_push (xmlFree
, xmlbuf
);
979 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
980 && (client
->opts
& OPT_NO_AGENT
))
981 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
983 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
984 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
985 NULL
, &key
, &keylen
, 1, 1,
986 (client
->opts
& OPT_NO_PASSPHRASE
));
991 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
992 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
995 pubkey
= client
->crypto
->pkey_sexp
;
999 sigkey
= client
->crypto
->sigpkey_sexp
;
1002 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
1003 sigkey
= client
->crypto
->save
.sigpkey
;
1007 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
1008 client
->filename
, xmlbuf
, xmlbuflen
);
1009 if (pubkey
== client
->crypto
->save
.pkey
)
1013 gcry_sexp_release (client
->crypto
->pkey_sexp
);
1014 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
1015 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
1016 client
->crypto
->grip
);
1019 gcry_sexp_release (pubkey
);
1021 client
->crypto
->save
.pkey
= NULL
;
1025 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
1027 if (sigkey
== client
->crypto
->save
.sigpkey
)
1031 if (client
->crypto
->sigpkey_sexp
)
1032 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
1034 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
1035 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
1036 client
->crypto
->sign_grip
);
1039 gcry_sexp_release (sigkey
);
1041 client
->crypto
->save
.sigpkey
= NULL
;
1050 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
1051 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
1055 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
1056 send_status_all (STATUS_CACHE
, NULL
);
1060 rc
= update_checksum (client
);
1061 client
->flags
&= ~(FLAG_NEW
);
1065 pthread_cleanup_pop (1); // xmlFree
1070 parse_opt_reset (void *data
, void *value
)
1072 struct client_s
*client
= data
;
1075 client
->opts
|= OPT_RESET
;
1080 save_command (assuan_context_t ctx
, char *line
)
1082 struct client_s
*client
= assuan_get_pointer (ctx
);
1085 struct argv_s
*args
[] = {
1086 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1087 parse_opt_no_passphrase
},
1088 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
1089 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
1090 parse_save_opt_inquire
},
1091 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
1092 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
1093 parse_save_opt_sign_keygrip
},
1094 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
1095 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
1096 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
1097 parse_save_opt_iterations
},
1098 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
1099 parse_save_opt_no_agent
},
1103 cleanup_save (&client
->crypto
->save
);
1104 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
1105 sizeof (file_header_t
));
1106 client
->crypto
->save
.s2k_count
=
1107 config_get_ulong (client
->filename
, "s2k_count");
1109 if (client
->flags
& FLAG_NEW
)
1110 client
->crypto
->save
.hdr
.iterations
=
1111 config_get_ulonglong (client
->filename
, "cipher_iterations");
1113 rc
= parse_options (&line
, args
, client
);
1115 return send_error (ctx
, rc
);
1117 if (!(client
->flags
& FLAG_NEW
))
1118 client
->opts
&= ~OPT_NO_AGENT
;
1119 else if (client
->opts
& OPT_NO_AGENT
)
1121 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
1122 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
1125 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
1126 && !(client
->opts
& OPT_INQUIRE
))
1127 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1129 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1130 return send_error (ctx
, gpg_error_from_errno (errno
));
1132 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
1134 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
1135 return send_error (ctx
, GPG_ERR_ENOANO
);
1139 rc
= cache_iscached (client
->filename
, &defer
);
1140 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
1142 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1145 if ((!rc
&& defer
) || config_get_boolean ("global", "require_save_key"))
1146 client
->opts
|= OPT_RESET
;
1148 if (!rc
&& (client
->opts
& OPT_RESET
))
1150 rc
= cache_clear (client
->md5file
);
1152 return send_error (ctx
, rc
);
1154 log_write ("%s: %s", client
->filename
,
1155 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1156 send_status_all (STATUS_CACHE
, NULL
);
1160 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1163 return send_error (ctx
, rc
);
1166 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1167 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1168 && !(client
->opts
& OPT_NO_AGENT
))
1170 rc
= set_pinentry_mode (client
->crypto
->agent
,
1171 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1174 if (!(client
->flags
& FLAG_NEW
))
1176 struct crypto_s
*crypto
;
1180 /* Wanting to generate a new key. Require the key to open the
1181 current file before proceeding reguardless of the
1182 require_save_key configuration parameter. */
1183 rc
= cache_clear (client
->md5file
);
1186 rc
= init_client_crypto (&crypto
);
1188 crypto
->client_ctx
= client
->ctx
;
1192 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1193 client
->filename
, &key
, &keylen
);
1196 cleanup_crypto (&crypto
);
1200 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1204 struct inquire_data_s idata
= { 0 };
1205 char *params
= client
->opts
& OPT_INQUIRE
1206 ? NULL
: default_key_params (client
->crypto
);
1208 pthread_cleanup_push (xfree
, params
);
1209 idata
.crypto
= client
->crypto
;
1213 idata
.line
= params
;
1214 idata
.len
= strlen (params
);
1219 client
->crypto
->agent
->inquire_data
= &idata
;
1220 client
->crypto
->agent
->inquire_cb
= NULL
;
1221 rc
= generate_key (client
->crypto
, params
,
1222 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1223 pthread_cleanup_pop (1);
1229 rc
= save_finalize (ctx
);
1231 cleanup_crypto_stage1 (client
->crypto
);
1233 (void) kill_scd (client
->crypto
->agent
);
1235 return send_error (ctx
, rc
);
1239 do_delete (assuan_context_t ctx
, char *line
)
1241 struct client_s
*client
= assuan_get_pointer (ctx
);
1246 if (strchr (line
, '\t'))
1247 req
= str_split (line
, "\t", 0);
1249 req
= str_split (line
, " ", 0);
1252 return GPG_ERR_SYNTAX
;
1254 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1262 * No sub-node defined. Remove the entire node (root element).
1268 rc
= is_element_owner (client
, n
);
1271 rc
= unlink_node (client
, n
);
1280 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1281 NULL
, NULL
, 0, 0, NULL
, 0);
1286 rc
= is_element_owner (client
, n
);
1289 rc
= unlink_node (client
, n
);
1297 delete_command (assuan_context_t ctx
, char *line
)
1299 struct client_s
*client
= assuan_get_pointer (ctx
);
1301 struct argv_s
*args
[] = {
1302 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1306 rc
= parse_options (&line
, args
, client
);
1308 return send_error (ctx
, rc
);
1310 if (client
->opts
& OPT_INQUIRE
)
1312 unsigned char *result
;
1315 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1317 return send_error (ctx
, rc
);
1319 pthread_cleanup_push (xfree
, result
);
1320 rc
= do_delete (ctx
, (char *)result
);
1321 pthread_cleanup_pop (1);
1324 rc
= do_delete (ctx
, line
);
1326 return send_error (ctx
, rc
);
1330 store_command (assuan_context_t ctx
, char *line
)
1332 struct client_s
*client
= assuan_get_pointer (ctx
);
1335 unsigned char *result
;
1337 xmlNodePtr n
, parent
;
1339 char *content
= NULL
;
1341 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1343 return send_error (ctx
, rc
);
1345 req
= str_split ((char *) result
, "\t", 0);
1349 return send_error (ctx
, GPG_ERR_SYNTAX
);
1351 len
= strv_length (req
);
1352 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1353 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1356 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1359 if (has_content
|| !*req
[len
- 1])
1362 content
= req
[len
- 1];
1363 req
[len
- 1] = NULL
;
1367 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1368 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1370 rc
= new_root_element (client
, client
->doc
, *req
);
1374 return send_error (ctx
, rc
);
1383 return send_error (ctx
, rc
);
1388 if (req
[1] && *req
[1])
1391 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1393 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1394 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1400 rc
= is_element_owner (client
, parent
);
1403 n
= find_text_node (parent
->children
);
1405 xmlNodeSetContent (n
, (xmlChar
*) content
);
1407 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1409 update_element_mtime (client
, parent
);
1415 return send_error (ctx
, rc
);
1419 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1424 int progress
= config_get_integer ("global", "xfer_progress");
1428 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1429 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1430 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1438 if (sent
+ to_send
> total
)
1439 to_send
= total
- sent
;
1441 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1442 flush
? 0 : to_send
);
1445 sent
+= flush
? 0 : to_send
;
1447 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1448 (sent
== total
&& flush
))
1449 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1451 if (!flush
&& !rc
&& sent
== total
)
1458 while (!rc
&& sent
< total
);
1464 do_get (assuan_context_t ctx
, char *line
)
1466 struct client_s
*client
= assuan_get_pointer (ctx
);
1471 req
= str_split (line
, "\t", 0);
1476 return GPG_ERR_SYNTAX
;
1479 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1488 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1495 if (!n
|| !n
->children
)
1496 return GPG_ERR_NO_DATA
;
1498 n
= find_text_node (n
->children
);
1499 if (!n
|| !n
->content
|| !*n
->content
)
1500 return GPG_ERR_NO_DATA
;
1502 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1507 get_command (assuan_context_t ctx
, char *line
)
1509 struct client_s
*client
= assuan_get_pointer (ctx
);
1511 struct argv_s
*args
[] = {
1512 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1516 rc
= parse_options (&line
, args
, client
);
1518 return send_error (ctx
, rc
);
1520 if (client
->opts
& OPT_INQUIRE
)
1522 unsigned char *result
;
1525 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1527 return send_error (ctx
, rc
);
1529 pthread_cleanup_push (xfree
, result
);
1530 rc
= do_get (ctx
, (char *)result
);
1531 pthread_cleanup_pop (1);
1534 rc
= do_get (ctx
, line
);
1536 return send_error (ctx
, rc
);
1539 static void list_command_cleanup1 (void *arg
);
1541 realpath_command (assuan_context_t ctx
, char *line
)
1543 struct string_s
*string
= NULL
;
1545 struct client_s
*client
= assuan_get_pointer (ctx
);
1546 struct argv_s
*args
[] = {
1547 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1551 rc
= parse_options (&line
, args
, client
);
1553 return send_error (ctx
, rc
);
1555 if (client
->opts
& OPT_INQUIRE
)
1557 unsigned char *result
;
1560 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1562 return send_error (ctx
, rc
);
1564 pthread_cleanup_push (xfree
, result
);
1565 rc
= build_realpath (client
, client
->doc
, (char *)result
, &string
);
1566 pthread_cleanup_pop (1);
1569 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1573 pthread_cleanup_push (list_command_cleanup1
, string
);
1574 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1575 pthread_cleanup_pop (1);
1578 return send_error (ctx
, rc
);
1582 list_command_cleanup1 (void *arg
)
1585 string_free ((struct string_s
*) arg
, 1);
1589 list_command_cleanup2 (void *arg
)
1591 struct element_list_s
*elements
= arg
;
1597 int total
= slist_length (elements
->list
);
1600 for (i
= 0; i
< total
; i
++)
1602 char *tmp
= slist_nth_data (elements
->list
, i
);
1606 slist_free (elements
->list
);
1609 if (elements
->prefix
)
1610 xfree (elements
->prefix
);
1613 strv_free (elements
->req
);
1620 parse_list_opt_norecurse (void *data
, void *value
)
1622 struct client_s
*client
= data
;
1624 client
->opts
&= ~(OPT_LIST_RECURSE
);
1629 parse_opt_verbose (void *data
, void *value
)
1631 struct client_s
*client
= data
;
1633 client
->opts
|= OPT_VERBOSE
;
1638 parse_list_opt_target (void *data
, void *value
)
1640 struct client_s
*client
= data
;
1642 client
->opts
|= OPT_LIST_WITH_TARGET
;
1647 parse_list_opt_all (void *data
, void *value
)
1649 struct client_s
*client
= data
;
1651 client
->opts
|= OPT_LIST_ALL
;
1656 list_path_once (struct client_s
*client
, char *line
,
1657 struct element_list_s
*elements
, struct string_s
*result
)
1661 elements
->req
= str_split (line
, " ", 0);
1663 strv_printf (&elements
->req
, "%s", line
);
1665 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1666 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1671 int total
= slist_length (elements
->list
);
1674 rc
= GPG_ERR_NO_DATA
;
1682 for (i
= 0; i
< total
; i
++)
1684 char *tmp
= slist_nth_data (elements
->list
, i
);
1686 string_append_printf (result
, "%s%s", tmp
,
1687 i
+ 1 == total
? "" : "\n");
1692 rc
= GPG_ERR_NO_DATA
;
1699 has_list_flag (char *path
, char *flags
)
1703 while (*p
&& *++p
!= ' ');
1712 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1723 do_list (assuan_context_t ctx
, char *line
)
1725 struct client_s
*client
= assuan_get_pointer (ctx
);
1727 struct element_list_s
*elements
= NULL
;
1729 elements
= xcalloc (1, sizeof (struct element_list_s
));
1731 return GPG_ERR_ENOMEM
;
1733 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1735 (client
->opts
& OPT_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1736 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1738 if (!line
|| !*line
)
1740 struct string_s
*str
= NULL
;
1742 pthread_cleanup_push (list_command_cleanup2
, elements
);
1743 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1744 elements
->with_target
);
1745 pthread_cleanup_pop (1);
1746 pthread_cleanup_push (list_command_cleanup1
, str
);
1750 if (client
->opts
& OPT_LIST_ALL
)
1752 char **roots
= str_split (str
->str
, "\n", 0);
1755 pthread_cleanup_push (req_cleanup
, roots
);
1756 string_truncate (str
, 0);
1758 for (p
= roots
; *p
; p
++)
1760 if (strchr (*p
, ' '))
1762 if (has_list_flag (*p
, "EOP"))
1764 string_append_printf (str
, "%s%s", *p
,
1765 *(p
+ 1) ? "\n" : "");
1770 elements
= xcalloc (1, sizeof (struct element_list_s
));
1773 rc
= GPG_ERR_ENOMEM
;
1777 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1779 (client
->opts
& OPT_VERBOSE
) | (client
->opts
&
1780 OPT_LIST_WITH_TARGET
);
1781 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1782 pthread_cleanup_push (list_command_cleanup2
, elements
);
1783 rc
= list_path_once (client
, *p
, elements
, str
);
1784 pthread_cleanup_pop (1);
1789 string_append (str
, "\n");
1792 pthread_cleanup_pop (1);
1796 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1799 pthread_cleanup_pop (1);
1803 pthread_cleanup_push (list_command_cleanup2
, elements
);
1804 struct string_s
*str
= string_new (NULL
);
1805 pthread_cleanup_push (list_command_cleanup1
, str
);
1806 rc
= list_path_once (client
, line
, elements
, str
);
1808 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1810 pthread_cleanup_pop (1);
1811 pthread_cleanup_pop (1);
1816 list_command (assuan_context_t ctx
, char *line
)
1818 struct client_s
*client
= assuan_get_pointer (ctx
);
1820 struct argv_s
*args
[] = {
1821 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1822 parse_list_opt_norecurse
},
1823 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
1824 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1825 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1826 parse_list_opt_target
},
1827 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1831 if (disable_list_and_dump
== 1)
1832 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1834 client
->opts
|= OPT_LIST_RECURSE
;
1835 rc
= parse_options (&line
, args
, client
);
1837 return send_error (ctx
, rc
);
1839 if (client
->opts
& OPT_LIST_ALL
)
1840 client
->opts
|= OPT_LIST_RECURSE
| OPT_VERBOSE
;
1842 if (client
->opts
& OPT_INQUIRE
)
1844 unsigned char *result
;
1847 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1849 return send_error (ctx
, rc
);
1851 pthread_cleanup_push (xfree
, result
);
1852 rc
= do_list (ctx
, (char *)result
);
1853 pthread_cleanup_pop (1);
1856 rc
= do_list (ctx
, line
);
1858 return send_error (ctx
, rc
);
1862 * req[0] - element path
1865 attribute_list (assuan_context_t ctx
, char **req
)
1867 struct client_s
*client
= assuan_get_pointer (ctx
);
1868 char **attrlist
= NULL
;
1876 if (!req
|| !req
[0])
1877 return GPG_ERR_SYNTAX
;
1879 client
->flags
|= FLAG_ACL_IGNORE
;
1881 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1884 * The first argument may be only a root element.
1886 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1887 return GPG_ERR_SYNTAX
;
1890 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1897 if (client
->flags
& FLAG_ACL_ERROR
)
1899 client
->flags
&= ~FLAG_ACL_IGNORE
;
1903 return GPG_ERR_EACCES
;
1906 client
->flags
&= ~FLAG_ACL_ERROR
;
1911 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1912 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1923 for (a
= n
->properties
; a
; a
= a
->next
)
1927 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1930 strv_free (attrlist
);
1932 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1933 pwmd_strerror (GPG_ERR_ENOMEM
));
1934 return GPG_ERR_ENOMEM
;
1939 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1941 && an
->content
? (char *) an
->content
: "");
1945 strv_free (attrlist
);
1946 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1947 pwmd_strerror (GPG_ERR_ENOMEM
));
1948 return GPG_ERR_ENOMEM
;
1951 attrlist
[++i
] = NULL
;
1955 return GPG_ERR_NO_DATA
;
1957 line
= strv_join ("\n", attrlist
);
1961 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1962 pwmd_strerror (GPG_ERR_ENOMEM
));
1963 strv_free (attrlist
);
1964 return GPG_ERR_ENOMEM
;
1967 pthread_cleanup_push (xfree
, line
);
1968 pthread_cleanup_push (req_cleanup
, attrlist
);
1969 rc
= xfer_data (ctx
, line
, strlen (line
));
1970 pthread_cleanup_pop (1);
1971 pthread_cleanup_pop (1);
1976 * req[0] - attribute
1977 * req[1] - element path
1980 attribute_delete (struct client_s
*client
, char **req
)
1986 if (!req
|| !req
[0] || !req
[1])
1987 return GPG_ERR_SYNTAX
;
1989 if (!strcmp (req
[0], "_name"))
1990 return GPG_ERR_INV_ATTR
;
1992 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1995 * The first argument may be only a root element.
1997 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1998 return GPG_ERR_SYNTAX
;
2001 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2007 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2008 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2013 if (!strcmp (req
[0], (char *) "_acl"))
2015 rc
= is_element_owner (client
, n
);
2020 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
2028 create_element_path (struct client_s
*client
,
2029 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
2031 char **req
= *elements
;
2032 char **req_orig
= strv_dup (req
);
2033 xmlNodePtr n
= NULL
;
2039 *rc
= GPG_ERR_ENOMEM
;
2040 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2041 pwmd_strerror (GPG_ERR_ENOMEM
));
2046 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2049 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2052 *rc
= new_root_element (client
, client
->doc
, req
[0]);
2058 else if (n
== parent
)
2060 *rc
= GPG_ERR_CONFLICT
;
2067 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
2069 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2070 NULL
, NULL
, create_target_elements_cb
, 0, 0,
2077 * Reset the position of the element tree now that the elements
2078 * have been created.
2083 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2087 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2088 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2095 strv_free (req_orig
);
2102 * Creates a "target" attribute. When other commands encounter an element with
2103 * this attribute, the element path is modified to the target value. If the
2104 * source element path doesn't exist when using 'ATTR SET target', it is
2105 * created, but the destination element path must exist.
2107 * req[0] - source element path
2108 * req[1] - destination element path
2111 target_attribute (struct client_s
*client
, char **req
)
2113 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
2117 if (!req
|| !req
[0] || !req
[1])
2118 return GPG_ERR_SYNTAX
;
2120 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
2123 * The first argument may be only a root element.
2125 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
2126 return GPG_ERR_SYNTAX
;
2129 if (!valid_element_path (src
, 0))
2130 return GPG_ERR_INV_VALUE
;
2132 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
2135 * The first argument may be only a root element.
2137 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
2139 rc
= GPG_ERR_SYNTAX
;
2144 odst
= strv_dup (dst
);
2147 rc
= GPG_ERR_ENOMEM
;
2151 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2153 * Make sure the destination element path exists.
2160 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
2161 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2166 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
2167 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2170 n
= create_element_path (client
, &src
, &rc
, NULL
);
2174 line
= strv_join ("\t", odst
);
2177 rc
= GPG_ERR_ENOMEM
;
2181 rc
= add_attribute (client
, n
, "target", line
);
2192 * req[0] - attribute
2193 * req[1] - element path
2196 attribute_get (assuan_context_t ctx
, char **req
)
2198 struct client_s
*client
= assuan_get_pointer (ctx
);
2204 if (!req
|| !req
[0] || !req
[1])
2205 return GPG_ERR_SYNTAX
;
2207 if (strchr (req
[1], '\t'))
2209 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2210 return GPG_ERR_SYNTAX
;
2214 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2215 return GPG_ERR_SYNTAX
;
2218 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2225 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2226 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2234 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2235 return GPG_ERR_NOT_FOUND
;
2237 pthread_cleanup_push (xmlFree
, a
);
2240 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2242 rc
= GPG_ERR_NO_DATA
;
2244 pthread_cleanup_pop (1);
2253 * req[0] - attribute
2254 * req[1] - element path
2258 attribute_set (struct client_s
*client
, char **req
)
2264 if (!req
|| !req
[0] || !req
[1])
2265 return GPG_ERR_SYNTAX
;
2268 * Reserved attribute names.
2270 if (!strcmp (req
[0], "_name"))
2271 return GPG_ERR_INV_ATTR
;
2272 else if (!strcmp (req
[0], "target"))
2273 return target_attribute (client
, req
+ 1);
2274 else if (!valid_xml_attribute (req
[0]))
2275 return GPG_ERR_INV_VALUE
;
2277 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2280 * The first argument may be only a root element.
2282 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2283 return GPG_ERR_SYNTAX
;
2286 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2293 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2294 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2300 if (!strcmp (req
[0], (char *) "_acl"))
2302 rc
= is_element_owner (client
, n
);
2307 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2316 * req[1] - attribute name or element path if command is LIST
2317 * req[2] - element path
2318 * req[2] - element path or value
2322 do_attr (assuan_context_t ctx
, char *line
)
2324 struct client_s
*client
= assuan_get_pointer (ctx
);
2328 req
= str_split (line
, " ", 4);
2329 if (!req
|| !req
[0] || !req
[1])
2332 return GPG_ERR_SYNTAX
;
2335 pthread_cleanup_push (req_cleanup
, req
);
2337 if (strcasecmp (req
[0], "SET") == 0)
2338 rc
= attribute_set (client
, req
+ 1);
2339 else if (strcasecmp (req
[0], "GET") == 0)
2340 rc
= attribute_get (ctx
, req
+ 1);
2341 else if (strcasecmp (req
[0], "DELETE") == 0)
2342 rc
= attribute_delete (client
, req
+ 1);
2343 else if (strcasecmp (req
[0], "LIST") == 0)
2344 rc
= attribute_list (ctx
, req
+ 1);
2346 rc
= GPG_ERR_SYNTAX
;
2348 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2349 pthread_cleanup_pop (1);
2354 attr_command (assuan_context_t ctx
, char *line
)
2356 struct client_s
*client
= assuan_get_pointer (ctx
);
2358 struct argv_s
*args
[] = {
2359 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2363 rc
= parse_options (&line
, args
, client
);
2365 return send_error (ctx
, rc
);
2367 if (client
->opts
& OPT_INQUIRE
)
2369 unsigned char *result
;
2372 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2374 return send_error (ctx
, rc
);
2376 pthread_cleanup_push (xfree
, result
);
2377 rc
= do_attr (ctx
, (char *)result
);
2378 pthread_cleanup_pop (1);
2381 rc
= do_attr (ctx
, line
);
2383 return send_error (ctx
, rc
);
2387 parse_iscached_opt_lock (void *data
, void *value
)
2389 struct client_s
*client
= data
;
2392 client
->opts
|= OPT_LOCK
;
2397 iscached_command (assuan_context_t ctx
, char *line
)
2399 struct client_s
*client
= assuan_get_pointer (ctx
);
2401 struct argv_s
*args
[] = {
2402 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2406 if (!line
|| !*line
)
2407 return send_error (ctx
, GPG_ERR_SYNTAX
);
2409 rc
= parse_options (&line
, args
, client
);
2411 return send_error (ctx
, rc
);
2412 else if (!valid_filename (line
))
2413 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2415 rc
= cache_iscached (line
, NULL
);
2416 if (client
->opts
& OPT_LOCK
2417 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2419 unsigned char md5file
[16];
2420 gpg_error_t trc
= rc
;
2422 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2423 if (memcmp (md5file
, client
->md5file
, 16))
2424 cleanup_client (client
);
2426 memcpy (client
->md5file
, md5file
, 16);
2427 rc
= do_lock (client
, 1);
2432 return send_error (ctx
, rc
);
2436 clearcache_command (assuan_context_t ctx
, char *line
)
2438 gpg_error_t rc
= 0, all_rc
= 0;
2439 unsigned char md5file
[16];
2443 struct client_thread_s
*once
= NULL
;
2446 MUTEX_LOCK (&cn_mutex
);
2447 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2449 if (!line
|| !*line
)
2452 t
= slist_length (cn_thread_list
);
2454 for (i
= 0; i
< t
; i
++)
2456 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2457 assuan_peercred_t peer
;
2462 /* Lock each connected clients' file mutex to prevent any other client
2463 * from accessing the cache entry (the file mutex is locked upon
2464 * command startup). The cache for the entry is not cleared if the
2465 * file mutex is locked by another client to prevent this function
2470 if (thd
->cl
->filename
)
2472 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2473 all_rc
= !all_rc
? rc
: all_rc
;
2481 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2482 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2484 if (pthread_equal (pthread_self (), thd
->tid
))
2488 if (!thd
->cl
->filename
||
2489 cache_iscached (thd
->cl
->filename
,
2490 NULL
) == GPG_ERR_NO_DATA
)
2496 cache_defer_clear (thd
->cl
->md5file
);
2499 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2507 rc
= cache_clear (thd
->cl
->md5file
);
2508 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2516 /* A single data filename was specified. Lock only this data file
2517 * mutex and free the cache entry. */
2520 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2521 rc
= do_validate_peer (ctx
, line
, &peer
);
2523 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2525 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2527 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2529 if (pthread_equal (pthread_self (), thd
->tid
))
2536 rc
= cache_clear (thd
->cl
->md5file
);
2537 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2541 cache_defer_clear (thd
->cl
->md5file
);
2549 /* Only connected clients' cache entries have been cleared. Now clear any
2550 * remaining cache entries without clients but only if there wasn't an
2551 * error from above since this would defeat the locking check of the
2552 * remaining entries. */
2558 /* No clients are using the specified file. */
2559 else if (!all_rc
&& !rc
&& !once
)
2561 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2562 rc
= cache_clear (md5file
);
2565 /* Release the connection mutex. */
2566 pthread_cleanup_pop (1);
2570 send_status_all (STATUS_CACHE
, NULL
);
2572 /* One or more files were locked while clearing all cache entries. */
2576 return send_error (ctx
, rc
);
2580 cachetimeout_command (assuan_context_t ctx
, char *line
)
2583 char **req
= str_split (line
, " ", 0);
2586 assuan_peercred_t peer
;
2588 if (!req
|| !*req
|| !req
[1])
2591 return send_error (ctx
, GPG_ERR_SYNTAX
);
2595 timeout
= (int) strtol (req
[1], &p
, 10);
2596 if (errno
!= 0 || *p
|| timeout
< -1)
2599 return send_error (ctx
, GPG_ERR_SYNTAX
);
2602 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2605 unsigned char md5file
[16];
2607 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2608 rc
= cache_set_timeout (md5file
, timeout
);
2609 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2612 MUTEX_LOCK (&rcfile_mutex
);
2613 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2615 MUTEX_UNLOCK (&rcfile_mutex
);
2620 return send_error (ctx
, rc
);
2624 dump_command (assuan_context_t ctx
, char *line
)
2628 struct client_s
*client
= assuan_get_pointer (ctx
);
2631 if (disable_list_and_dump
== 1)
2632 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2634 rc
= peer_is_invoker(client
);
2636 return send_error (ctx
, rc
);
2638 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2642 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2643 pwmd_strerror (GPG_ERR_ENOMEM
));
2644 return send_error (ctx
, GPG_ERR_ENOMEM
);
2647 pthread_cleanup_push (xmlFree
, xml
);
2648 rc
= xfer_data (ctx
, (char *) xml
, len
);
2649 pthread_cleanup_pop (1);
2650 return send_error (ctx
, rc
);
2654 getconfig_command (assuan_context_t ctx
, char *line
)
2656 struct client_s
*client
= assuan_get_pointer (ctx
);
2658 char filename
[255] = { 0 }, param
[747] = { 0 };
2659 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2661 if (!line
|| !*line
)
2662 return send_error (ctx
, GPG_ERR_SYNTAX
);
2664 if (strchr (line
, ' '))
2666 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2671 if (fp
&& !valid_filename (fp
))
2672 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2674 paramp
= str_down (paramp
);
2675 if (!strcmp (paramp
, "cipher") && fp
)
2677 struct crypto_s
*crypto
= NULL
;
2679 rc
= init_client_crypto (&crypto
);
2682 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2686 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2696 UPDATE_AGENT_CTX (client
, crypto
);
2697 cleanup_crypto (&crypto
);
2698 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2699 return send_error (ctx
, rc
);
2704 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2706 struct crypto_s
*crypto
= NULL
;
2708 rc
= init_client_crypto (&crypto
);
2711 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2714 tmp
= str_asprintf ("%llu",
2715 (unsigned long long) crypto
->hdr
.
2718 rc
= GPG_ERR_ENOMEM
;
2722 UPDATE_AGENT_CTX (client
, crypto
);
2723 cleanup_crypto (&crypto
);
2724 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2725 return send_error (ctx
, rc
);
2730 else if (!strcmp (paramp
, "passphrase"))
2731 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2733 p
= config_get_value (fp
? fp
: "global", paramp
);
2735 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2737 tmp
= expand_homedir (p
);
2741 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2742 pwmd_strerror (GPG_ERR_ENOMEM
));
2743 return send_error (ctx
, GPG_ERR_ENOMEM
);
2748 pthread_cleanup_push (xfree
, p
);
2749 rc
= xfer_data (ctx
, p
, strlen (p
));
2750 pthread_cleanup_pop (1);
2751 return send_error (ctx
, rc
);
2756 xmlXPathContextPtr xp
;
2757 xmlXPathObjectPtr result
;
2763 xpath_command_cleanup (void *arg
)
2765 struct xpath_s
*xpath
= arg
;
2770 req_cleanup (xpath
->req
);
2773 xmlBufferFree (xpath
->buf
);
2776 xmlXPathFreeObject (xpath
->result
);
2779 xmlXPathFreeContext (xpath
->xp
);
2783 do_xpath (assuan_context_t ctx
, char *line
)
2786 struct client_s
*client
= assuan_get_pointer (ctx
);
2787 struct xpath_s _x
= { 0 };
2788 struct xpath_s
*xpath
= &_x
;
2790 if (!line
|| !*line
)
2791 return GPG_ERR_SYNTAX
;
2793 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2795 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2796 return GPG_ERR_ENOMEM
;
2799 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2802 rc
= GPG_ERR_BAD_DATA
;
2807 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2810 rc
= GPG_ERR_BAD_DATA
;
2814 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2816 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2820 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2821 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2825 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2827 rc
= GPG_ERR_NO_DATA
;
2830 else if (xpath
->req
[1])
2836 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2837 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2838 xmlBufferLength (xpath
->buf
));
2839 pthread_cleanup_pop (0);
2841 xpath_command_cleanup (xpath
);
2846 xpath_command (assuan_context_t ctx
, char *line
)
2848 struct client_s
*client
= assuan_get_pointer (ctx
);
2850 struct argv_s
*args
[] = {
2851 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2855 if (disable_list_and_dump
== 1)
2856 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2858 rc
= peer_is_invoker(client
);
2860 return send_error (ctx
, rc
);
2862 rc
= parse_options (&line
, args
, client
);
2864 return send_error (ctx
, rc
);
2866 if (client
->opts
& OPT_INQUIRE
)
2868 unsigned char *result
;
2871 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2873 return send_error (ctx
, rc
);
2875 pthread_cleanup_push (xfree
, result
);
2876 rc
= do_xpath (ctx
, (char *)result
);
2877 pthread_cleanup_pop (1);
2880 rc
= do_xpath (ctx
, line
);
2882 return send_error (ctx
, rc
);
2886 do_xpathattr (assuan_context_t ctx
, char *line
)
2888 struct client_s
*client
= assuan_get_pointer (ctx
);
2892 struct xpath_s _x
= { 0 };
2893 struct xpath_s
*xpath
= &_x
;
2895 if (!line
|| !*line
)
2896 return GPG_ERR_SYNTAX
;
2898 if ((req
= str_split (line
, " ", 3)) == NULL
)
2899 return GPG_ERR_ENOMEM
;
2903 rc
= GPG_ERR_SYNTAX
;
2907 if (!strcasecmp (req
[0], "SET"))
2909 else if (!strcasecmp (req
[0], "DELETE"))
2913 rc
= GPG_ERR_SYNTAX
;
2917 if (!req
[1] || !req
[2])
2919 rc
= GPG_ERR_SYNTAX
;
2923 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2925 rc
= GPG_ERR_ENOMEM
;
2929 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2931 rc
= GPG_ERR_SYNTAX
;
2935 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2938 rc
= GPG_ERR_BAD_DATA
;
2942 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2945 rc
= GPG_ERR_BAD_DATA
;
2949 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2951 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2955 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2956 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2957 (xmlChar
*) req
[1]);
2960 xpath_command_cleanup (xpath
);
2965 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2967 xpathattr_command (assuan_context_t ctx
, char *line
)
2969 struct client_s
*client
= assuan_get_pointer (ctx
);
2971 struct argv_s
*args
[] = {
2972 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2976 if (disable_list_and_dump
== 1)
2977 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2979 rc
= peer_is_invoker(client
);
2981 return send_error (ctx
, rc
);
2983 rc
= parse_options (&line
, args
, client
);
2985 return send_error (ctx
, rc
);
2987 if (client
->opts
& OPT_INQUIRE
)
2989 unsigned char *result
;
2992 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2994 return send_error (ctx
, rc
);
2996 pthread_cleanup_push (xfree
, result
);
2997 rc
= do_xpathattr (ctx
, (char *)result
);
2998 pthread_cleanup_pop (1);
3001 rc
= do_xpathattr (ctx
, line
);
3003 return send_error (ctx
, rc
);
3007 do_import (struct client_s
*client
, const char *root_element
,
3008 unsigned char *content
)
3010 char **dst_path
= NULL
;
3011 xmlDocPtr doc
= NULL
;
3012 xmlNodePtr n
, root
, copy
;
3015 if (!content
|| !*content
)
3018 return GPG_ERR_SYNTAX
;
3022 dst_path
= str_split (root_element
, "\t", 0);
3024 if (dst_path
&& !valid_element_path (dst_path
, 0))
3027 strv_free (dst_path
);
3029 return GPG_ERR_INV_VALUE
;
3032 struct string_s
*str
= string_new_content ((char *)content
);
3033 str
= string_prepend (str
, "<pwmd>");
3034 str
= string_append (str
, "</pwmd>");
3035 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
3036 string_free (str
, 1);
3039 rc
= GPG_ERR_BAD_DATA
;
3043 root
= xmlDocGetRootElement (doc
);
3044 xmlNodePtr root_orig
= root
->children
;
3045 root
= root
->children
;
3046 rc
= validate_import (client
, root
);
3055 char **path
= strv_dup (dst_path
);
3058 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
3059 pwmd_strerror (GPG_ERR_ENOMEM
));
3060 rc
= GPG_ERR_ENOMEM
;
3064 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
3068 rc
= GPG_ERR_INV_VALUE
;
3072 if (strv_printf (&path
, "%s", (char *) a
) == 0)
3075 rc
= GPG_ERR_ENOMEM
;
3080 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
3081 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3089 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
3090 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
3091 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3106 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
3108 n
= create_element_path (client
, &path
, &rc
, NULL
);
3115 copy
= xmlCopyNodeList (root
->children
);
3116 n
= xmlAddChildList (n
, copy
);
3118 rc
= GPG_ERR_ENOMEM
;
3127 /* Check if the content root element can create a DTD root element. */
3128 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
3130 rc
= GPG_ERR_SYNTAX
;
3136 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
3138 rc
= GPG_ERR_SYNTAX
;
3142 char *tmp
= str_dup ((char *) a
);
3144 int literal
= is_literal_element (&tmp
);
3146 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
3149 rc
= GPG_ERR_INV_VALUE
;
3153 if (strv_printf (&path
, "%s", tmp
) == 0)
3156 rc
= GPG_ERR_ENOMEM
;
3161 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
3162 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3164 rc
= GPG_ERR_BAD_DATA
;
3168 /* Overwriting the existing tree. */
3172 xmlFreeNodeList (n
);
3176 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
3177 n
= xmlCopyNode (root
, 1);
3178 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3183 rc
= update_element_mtime (client
, n
->parent
);
3185 for (root
= root_orig
->next
; root
; root
= root
->next
)
3187 if (root
->type
== XML_ELEMENT_NODE
)
3200 strv_free (dst_path
);
3206 parse_import_opt_root (void *data
, void *value
)
3208 struct client_s
*client
= data
;
3210 client
->import_root
= str_dup (value
);
3211 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3215 import_command (assuan_context_t ctx
, char *line
)
3218 struct client_s
*client
= assuan_get_pointer (ctx
);
3219 unsigned char *result
;
3221 struct argv_s
*args
[] = {
3222 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3226 xfree (client
->import_root
);
3227 client
->import_root
= NULL
;
3228 rc
= parse_options (&line
, args
, client
);
3230 return send_error (ctx
, rc
);
3232 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3235 xfree (client
->import_root
);
3236 client
->import_root
= NULL
;
3237 return send_error (ctx
, rc
);
3240 rc
= do_import (client
, client
->import_root
, result
);
3241 xfree (client
->import_root
);
3242 client
->import_root
= NULL
;
3243 return send_error (ctx
, rc
);
3247 do_lock (struct client_s
*client
, int add
)
3249 gpg_error_t rc
= lock_file_mutex (client
, add
);
3252 client
->flags
|= FLAG_LOCK_CMD
;
3258 lock_command (assuan_context_t ctx
, char *line
)
3260 struct client_s
*client
= assuan_get_pointer (ctx
);
3261 gpg_error_t rc
= do_lock (client
, 0);
3263 return send_error (ctx
, rc
);
3267 unlock_command (assuan_context_t ctx
, char *line
)
3269 struct client_s
*client
= assuan_get_pointer (ctx
);
3272 rc
= unlock_file_mutex (client
, 0);
3273 return send_error (ctx
, rc
);
3277 option_command (assuan_context_t ctx
, char *line
)
3279 struct client_s
*client
= assuan_get_pointer (ctx
);
3281 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3283 struct agent_s
*agent
= client
->crypto
->agent
;
3285 char namebuf
[255] = { 0 };
3286 char *name
= namebuf
;
3287 char *value
= NULL
, *p
, *tmp
= NULL
;
3289 p
= strchr (line
, '=');
3292 strncpy (namebuf
, line
, sizeof(namebuf
));
3293 namebuf
[sizeof(namebuf
)-1] = 0;
3297 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3298 namebuf
[sizeof(namebuf
)-1] = 0;
3302 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3304 if (strcasecmp (name
, (char *) "log_level") == 0)
3310 l
= strtol (value
, NULL
, 10);
3313 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3316 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3318 MUTEX_LOCK (&rcfile_mutex
);
3319 config_set_int_param (&global_config
, "global", "log_level", value
);
3320 MUTEX_UNLOCK (&rcfile_mutex
);
3323 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3329 n
= strtol (value
, &tmp
, 10);
3331 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3334 client
->lock_timeout
= n
;
3337 else if (strcasecmp (name
, (char *) "NAME") == 0)
3339 if (value
&& strchr (value
, ' '))
3340 rc
= GPG_ERR_INV_VALUE
;
3343 tmp
= pthread_getspecific (thread_name_key
);
3345 MUTEX_LOCK (&cn_mutex
);
3346 xfree (client
->thd
->name
);
3347 client
->thd
->name
= NULL
;
3349 if (!value
|| !*value
)
3350 pthread_setspecific (thread_name_key
, str_dup (""));
3353 client
->thd
->name
= str_dup (value
);
3354 pthread_setspecific (thread_name_key
, str_dup (value
));
3357 MUTEX_UNLOCK (&cn_mutex
);
3361 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3363 xfree (pin_opts
->lc_messages
);
3364 pin_opts
->lc_messages
= NULL
;
3365 if (value
&& *value
)
3366 pin_opts
->lc_messages
= str_dup (value
);
3369 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3372 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3374 xfree (pin_opts
->lc_ctype
);
3375 pin_opts
->lc_ctype
= NULL
;
3376 if (value
&& *value
)
3377 pin_opts
->lc_ctype
= str_dup (value
);
3380 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3383 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3385 xfree (pin_opts
->ttyname
);
3386 pin_opts
->ttyname
= NULL
;
3387 if (value
&& *value
)
3388 pin_opts
->ttyname
= str_dup (value
);
3391 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3394 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3396 xfree (pin_opts
->ttytype
);
3397 pin_opts
->ttytype
= NULL
;
3398 if (value
&& *value
)
3399 pin_opts
->ttytype
= str_dup (value
);
3402 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3405 else if (strcasecmp (name
, (char *) "display") == 0)
3407 xfree (pin_opts
->display
);
3408 pin_opts
->display
= NULL
;
3409 if (value
&& *value
)
3410 pin_opts
->display
= str_dup (value
);
3413 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3416 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3418 xfree (pin_opts
->desc
);
3419 pin_opts
->desc
= NULL
;
3420 if (value
&& *value
)
3421 pin_opts
->desc
= str_dup (value
);
3423 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3425 xfree (pin_opts
->title
);
3426 pin_opts
->title
= NULL
;
3427 if (value
&& *value
)
3428 pin_opts
->title
= str_dup (value
);
3430 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3432 xfree (pin_opts
->prompt
);
3433 pin_opts
->prompt
= NULL
;
3434 if (value
&& *value
)
3435 pin_opts
->prompt
= str_dup (value
);
3438 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3446 n
= (int) strtol (value
, &p
, 10);
3449 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3451 pin_opts
->timeout
= n
;
3452 MUTEX_LOCK (&rcfile_mutex
);
3453 config_set_int_param (&global_config
,
3454 client
->filename
? client
->filename
: "global",
3455 "pinentry_timeout", value
);
3456 MUTEX_UNLOCK (&rcfile_mutex
);
3459 else if (strcasecmp (name
, "disable-pinentry") == 0)
3463 if (value
&& *value
)
3465 n
= (int) strtol (value
, &tmp
, 10);
3466 if (*tmp
|| n
< 0 || n
> 1)
3467 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3471 client
->flags
|= FLAG_NO_PINENTRY
;
3473 client
->flags
&= ~FLAG_NO_PINENTRY
;
3478 if (client
->flags
& FLAG_NO_PINENTRY
)
3479 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3482 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3486 return send_error (ctx
, rc
);
3491 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3495 if (!rc
&& use_agent
&& agent
)
3497 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3502 return send_error (ctx
, rc
);
3506 do_rename (assuan_context_t ctx
, char *line
)
3508 struct client_s
*client
= assuan_get_pointer (ctx
);
3510 char **req
, **src
, *dst
;
3513 req
= str_split (line
, " ", 0);
3515 if (!req
|| !req
[0] || !req
[1])
3518 return GPG_ERR_SYNTAX
;
3522 is_literal_element (&dst
);
3524 if (!valid_xml_element ((xmlChar
*) dst
))
3527 return GPG_ERR_INV_VALUE
;
3530 if (strchr (req
[0], '\t'))
3531 src
= str_split (req
[0], "\t", 0);
3533 src
= str_split (req
[0], " ", 0);
3537 rc
= GPG_ERR_SYNTAX
;
3541 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3543 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3544 NULL
, 0, 0, NULL
, 0);
3549 rc
= is_element_owner (client
, n
);
3553 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3556 rc
= GPG_ERR_ENOMEM
;
3560 /* To prevent unwanted effects:
3562 * <root name="a"><b/></root>
3566 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3569 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3579 for (p
= src
; *p
; p
++)
3583 strv_printf (&tmp
, "%s", *p
);
3587 strv_printf (&tmp
, "!%s", dst
);
3588 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3589 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3596 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3597 NULL
, NULL
, 0, 0, NULL
, 0);
3600 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3605 /* Target may exist:
3608 * <root name="b" target="a"/>
3617 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3623 rc
= is_element_owner (client
, ndst
);
3627 unlink_node (client
, ndst
);
3628 xmlFreeNodeList (ndst
);
3631 rc
= add_attribute (client
, n
, "_name", dst
);
3640 rename_command (assuan_context_t ctx
, char *line
)
3642 struct client_s
*client
= assuan_get_pointer (ctx
);
3644 struct argv_s
*args
[] = {
3645 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3649 rc
= parse_options (&line
, args
, client
);
3651 return send_error (ctx
, rc
);
3653 if (client
->opts
& OPT_INQUIRE
)
3655 unsigned char *result
;
3658 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3660 return send_error (ctx
, rc
);
3662 pthread_cleanup_push (xfree
, result
);
3663 rc
= do_rename (ctx
, (char *)result
);
3664 pthread_cleanup_pop (1);
3667 rc
= do_rename (ctx
, line
);
3669 return send_error (ctx
, rc
);
3673 do_copy (assuan_context_t ctx
, char *line
)
3675 struct client_s
*client
= assuan_get_pointer (ctx
);
3677 char **req
, **src
= NULL
, **dst
= NULL
;
3678 xmlNodePtr nsrc
, ndst
, new = NULL
;
3680 req
= str_split (line
, " ", 0);
3681 if (!req
|| !req
[0] || !req
[1])
3684 return GPG_ERR_SYNTAX
;
3687 if (strchr (req
[0], '\t'))
3688 src
= str_split (req
[0], "\t", 0);
3690 src
= str_split (req
[0], " ", 0);
3694 rc
= GPG_ERR_SYNTAX
;
3698 if (strchr (req
[1], '\t'))
3699 dst
= str_split (req
[1], "\t", 0);
3701 dst
= str_split (req
[1], " ", 0);
3705 rc
= GPG_ERR_SYNTAX
;
3709 if (!valid_element_path (dst
, 0))
3711 rc
= GPG_ERR_INV_VALUE
;
3715 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3717 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3718 NULL
, NULL
, 0, 0, NULL
, 0);
3723 new = xmlCopyNodeList (nsrc
);
3726 rc
= GPG_ERR_ENOMEM
;
3731 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3735 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3736 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3743 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3745 else if (!ndst
|| create
)
3747 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3752 rc
= is_element_owner (client
, ndst
);
3756 /* Merge any attributes from the src node to the initial dst node. */
3757 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3759 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3762 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3766 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3767 xmlNewProp (ndst
, attr
->name
, tmp
);
3769 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3772 xmlNodePtr n
= ndst
->children
;
3774 xmlFreeNodeList (n
);
3775 ndst
->children
= NULL
;
3779 n
= xmlCopyNodeList (new->children
);
3782 rc
= GPG_ERR_ENOMEM
;
3786 n
= xmlAddChildList (ndst
, n
);
3789 rc
= GPG_ERR_ENOMEM
;
3793 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3794 ndst
->parent
? ndst
: ndst
->parent
);
3800 xmlUnlinkNode (new);
3801 xmlFreeNodeList (new);
3817 copy_command (assuan_context_t ctx
, char *line
)
3819 struct client_s
*client
= assuan_get_pointer (ctx
);
3821 struct argv_s
*args
[] = {
3822 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3826 rc
= parse_options (&line
, args
, client
);
3828 return send_error (ctx
, rc
);
3830 if (client
->opts
& OPT_INQUIRE
)
3832 unsigned char *result
;
3835 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3837 return send_error (ctx
, rc
);
3839 pthread_cleanup_push (xfree
, result
);
3840 rc
= do_copy (ctx
, (char *)result
);
3841 pthread_cleanup_pop (1);
3844 rc
= do_copy (ctx
, line
);
3846 return send_error (ctx
, rc
);
3850 do_move (assuan_context_t ctx
, char *line
)
3852 struct client_s
*client
= assuan_get_pointer (ctx
);
3854 char **req
, **src
= NULL
, **dst
= NULL
;
3855 xmlNodePtr nsrc
, ndst
= NULL
;
3857 req
= str_split (line
, " ", 0);
3859 if (!req
|| !req
[0] || !req
[1])
3862 return GPG_ERR_SYNTAX
;
3865 if (strchr (req
[0], '\t'))
3866 src
= str_split (req
[0], "\t", 0);
3868 src
= str_split (req
[0], " ", 0);
3872 rc
= GPG_ERR_SYNTAX
;
3876 if (strchr (req
[1], '\t'))
3877 dst
= str_split (req
[1], "\t", 0);
3879 dst
= str_split (req
[1], " ", 0);
3881 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3883 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3884 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3889 rc
= is_element_owner (client
, nsrc
);
3895 if (!valid_element_path (dst
, 0))
3897 rc
= GPG_ERR_INV_VALUE
;
3901 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3903 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3904 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3907 ndst
= xmlDocGetRootElement (client
->doc
);
3909 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3913 rc
= GPG_ERR_CONFLICT
;
3918 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3925 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3927 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
3939 if (ndst
== xmlDocGetRootElement (client
->doc
))
3941 xmlNodePtr n
= nsrc
;
3944 while (n
->parent
&& n
->parent
!= ndst
)
3947 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3948 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3950 if (xmlStrEqual (a
, b
))
3953 xmlUnlinkNode (nsrc
);
3955 xmlFreeNodeList (n
);
3963 xmlUnlinkNode (dup
);
3964 xmlFreeNodeList (dup
);
3968 xmlUnlinkNode (dup
);
3974 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3976 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3977 && !strcmp ((char *) name
, *dst
))
3980 rc
= GPG_ERR_CONFLICT
;
3985 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3991 update_element_mtime (client
, nsrc
->parent
);
3992 xmlUnlinkNode (nsrc
);
3993 ndst
= xmlAddChildList (ndst
, nsrc
);
3996 rc
= GPG_ERR_ENOMEM
;
3998 update_element_mtime (client
, ndst
->parent
);
4014 move_command (assuan_context_t ctx
, char *line
)
4016 struct client_s
*client
= assuan_get_pointer (ctx
);
4018 struct argv_s
*args
[] = {
4019 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
4023 rc
= parse_options (&line
, args
, client
);
4025 return send_error (ctx
, rc
);
4027 if (client
->opts
& OPT_INQUIRE
)
4029 unsigned char *result
;
4032 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
4034 return send_error (ctx
, rc
);
4036 pthread_cleanup_push (xfree
, result
);
4037 rc
= do_move (ctx
, (char *)result
);
4038 pthread_cleanup_pop (1);
4041 rc
= do_move (ctx
, line
);
4043 return send_error (ctx
, rc
);
4047 ls_command (assuan_context_t ctx
, char *line
)
4050 char *tmp
= str_asprintf ("%s/data", homedir
);
4051 char *dir
= expand_homedir (tmp
);
4052 DIR *d
= opendir (dir
);
4054 rc
= gpg_error_from_errno (errno
);
4060 return send_error (ctx
, rc
);
4064 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
4065 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
4069 pthread_cleanup_push (xfree
, p
);
4070 pthread_cleanup_push ((void *)(void *)closedir
, d
);
4073 while (!readdir_r (d
, p
, &cur
) && cur
)
4075 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
4077 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
4078 && cur
->d_name
[2] == '\0')
4081 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
4088 rc
= GPG_ERR_ENOMEM
;
4096 pthread_cleanup_pop (1); // closedir (d)
4097 pthread_cleanup_pop (1); // xfree (p)
4100 return send_error (ctx
, rc
);
4103 return send_error (ctx
, GPG_ERR_NO_DATA
);
4105 list
[strlen (list
) - 1] = 0;
4106 pthread_cleanup_push (xfree
, list
);
4107 rc
= xfer_data (ctx
, list
, strlen (list
));
4108 pthread_cleanup_pop (1);
4109 return send_error (ctx
, rc
);
4113 bye_notify (assuan_context_t ctx
, char *line
)
4115 struct client_s
*cl
= assuan_get_pointer (ctx
);
4117 cl
->thd
->state
= CLIENT_STATE_DISCON
;
4120 if (cl
->thd
->remote
)
4126 struct timeval tv
= { 0, 50000 };
4128 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
4129 if (rc
== GNUTLS_E_AGAIN
)
4130 select (0, NULL
, NULL
, NULL
, &tv
);
4132 while (rc
== GNUTLS_E_AGAIN
);
4136 /* This will let assuan_process_next() return. */
4137 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
4139 cl
->last_rc
= gpg_error_from_errno (errno
);
4143 cl
->last_rc
= 0; // BYE command result
4148 reset_notify (assuan_context_t ctx
, char *line
)
4150 struct client_s
*client
= assuan_get_pointer (ctx
);
4153 cleanup_client (client
);
4159 * This is called before every Assuan command.
4162 command_startup (assuan_context_t ctx
, const char *name
)
4164 struct client_s
*client
= assuan_get_pointer (ctx
);
4166 struct command_table_s
*cmd
= NULL
;
4168 log_write1 ("command='%s'", name
);
4169 client
->last_rc
= client
->opts
= 0;
4171 for (int i
= 0; command_table
[i
]; i
++)
4173 if (!strcasecmp (name
, command_table
[i
]->name
))
4175 if (command_table
[i
]->ignore_startup
)
4177 cmd
= command_table
[i
];
4183 return GPG_ERR_UNKNOWN_COMMAND
;
4185 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4187 update_client_state (client
, CLIENT_STATE_COMMAND
);
4193 * This is called after every Assuan command.
4196 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4198 struct client_s
*client
= assuan_get_pointer (ctx
);
4200 if (!(client
->flags
& FLAG_LOCK_CMD
))
4201 unlock_file_mutex (client
, 0);
4203 log_write1 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4204 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
4206 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4208 update_client_state (client
, CLIENT_STATE_IDLE
);
4212 help_command (assuan_context_t ctx
, char *line
)
4217 if (!line
|| !*line
)
4220 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
4221 "For commands that take an element path as an argument, each element is "
4222 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4225 for (i
= 0; command_table
[i
]; i
++)
4227 if (!command_table
[i
]->help
)
4230 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4235 tmp
= strip_texi_and_wrap (help
);
4237 pthread_cleanup_push (xfree
, tmp
);
4238 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4239 pthread_cleanup_pop (1);
4240 return send_error (ctx
, rc
);
4243 for (i
= 0; command_table
[i
]; i
++)
4245 if (!strcasecmp (line
, command_table
[i
]->name
))
4249 if (!command_table
[i
]->help
)
4252 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4253 tmp
= str_asprintf (_("Usage: %s"), help
);
4255 pthread_cleanup_push (xfree
, tmp
);
4256 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4257 pthread_cleanup_pop (1);
4258 return send_error (ctx
, rc
);
4262 return send_error (ctx
, GPG_ERR_INV_NAME
);
4266 new_command (const char *name
, int ignore
, int unlock
,
4267 gpg_error_t (*handler
) (assuan_context_t
, char *),
4273 for (i
= 0; command_table
[i
]; i
++);
4276 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4277 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4278 command_table
[i
]->name
= name
;
4279 command_table
[i
]->handler
= handler
;
4280 command_table
[i
]->ignore_startup
= ignore
;
4281 command_table
[i
]->unlock
= unlock
;
4282 command_table
[i
++]->help
= help
;
4283 command_table
[i
] = NULL
;
4291 for (i
= 0; command_table
[i
]; i
++)
4292 xfree (command_table
[i
]);
4294 xfree (command_table
);
4298 sort_commands (const void *arg1
, const void *arg2
)
4300 struct command_table_s
*const *a
= arg1
;
4301 struct command_table_s
*const *b
= arg2
;
4310 return strcmp ((*a
)->name
, (*b
)->name
);
4313 // FIXME cleanup/implement options
4315 passwd_command (assuan_context_t ctx
, char *line
)
4317 struct client_s
*client
= assuan_get_pointer (ctx
);
4319 struct argv_s
*args
[] = {
4320 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4321 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4322 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4326 rc
= peer_is_invoker (client
);
4327 if (rc
== GPG_ERR_EACCES
)
4328 return send_error (ctx
, rc
);
4330 if (client
->flags
& FLAG_NEW
)
4331 return send_error (ctx
, GPG_ERR_INV_STATE
);
4333 client
->crypto
->save
.s2k_count
=
4334 config_get_ulong (client
->filename
, "s2k_count");
4335 rc
= parse_options (&line
, args
, client
);
4337 return send_error (ctx
, rc
);
4339 if (!rc
&& client
->opts
& OPT_RESET
)
4341 rc
= cache_clear (client
->md5file
);
4343 send_status_all (STATUS_CACHE
, NULL
);
4348 if (!IS_PKI (client
->crypto
))
4350 struct crypto_s
*crypto
;
4352 xfree (client
->crypto
->filename
);
4353 client
->crypto
->filename
= str_dup (client
->filename
);
4354 rc
= change_passwd (ctx
, client
->filename
,
4355 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4356 (client
->opts
& OPT_NO_PASSPHRASE
));
4359 cleanup_crypto (&client
->crypto
);
4360 client
->crypto
= crypto
;
4361 update_checksum (client
);
4362 cleanup_crypto_stage1 (client
->crypto
);
4368 if (client
->crypto
->save
.s2k_count
)
4369 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4370 "OPTION s2k-count=%lu",
4371 client
->crypto
->save
.s2k_count
);
4374 rc
= agent_passwd (client
->crypto
);
4379 return send_error (ctx
, rc
);
4383 parse_keygrip_opt_sign (void *data
, void *value
)
4385 struct client_s
*client
= data
;
4388 client
->opts
|= OPT_SIGN
;
4393 keygrip_command (assuan_context_t ctx
, char *line
)
4395 struct client_s
*client
= assuan_get_pointer (ctx
);
4397 struct crypto_s
*crypto
= NULL
;
4398 struct argv_s
*args
[] = {
4399 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4403 if (!line
|| !*line
)
4404 return send_error (ctx
, GPG_ERR_SYNTAX
);
4406 rc
= parse_options (&line
, args
, client
);
4408 return send_error (ctx
, rc
);
4410 if (!valid_filename (line
))
4411 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4413 rc
= init_client_crypto (&crypto
);
4415 return send_error (ctx
, rc
);
4417 rc
= read_data_file (line
, crypto
);
4420 char *hexgrip
= NULL
;
4422 if (!IS_PKI (crypto
))
4424 cleanup_crypto (&crypto
);
4425 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4428 if (client
->opts
& OPT_SIGN
)
4430 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4431 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4435 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4438 rc
= GPG_ERR_ENOMEM
;
4440 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4445 UPDATE_AGENT_CTX (client
, crypto
);
4446 cleanup_crypto (&crypto
);
4447 return send_error (ctx
, rc
);
4451 parse_opt_data (void *data
, void *value
)
4453 struct client_s
*client
= data
;
4456 client
->opts
|= OPT_DATA
;
4461 send_client_list (assuan_context_t ctx
)
4463 struct client_s
*client
= assuan_get_pointer (ctx
);
4465 char buf
[ASSUAN_LINELENGTH
];
4467 if (client
->opts
& OPT_VERBOSE
)
4473 MUTEX_LOCK (&cn_mutex
);
4474 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4475 t
= slist_length (cn_thread_list
);
4477 for (i
= 0; i
< t
; i
++)
4479 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4482 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4485 tmp
= build_client_info_line (thd
, 0);
4488 char **l
= strv_cat (list
, tmp
);
4490 rc
= GPG_ERR_ENOMEM
;
4495 rc
= GPG_ERR_ENOMEM
;
4504 pthread_cleanup_pop (1);
4508 line
= strv_join ("\n", list
);
4510 pthread_cleanup_push (xfree
, line
);
4511 rc
= xfer_data (ctx
, line
, strlen (line
));
4512 pthread_cleanup_pop (1);
4516 if (client
->opts
& OPT_DATA
)
4518 MUTEX_LOCK (&cn_mutex
);
4519 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4520 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4521 pthread_cleanup_pop (1);
4522 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4525 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4531 getinfo_command (assuan_context_t ctx
, char *line
)
4533 struct client_s
*client
= assuan_get_pointer (ctx
);
4535 char buf
[ASSUAN_LINELENGTH
];
4536 struct argv_s
*args
[] = {
4537 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4538 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4542 rc
= parse_options (&line
, args
, client
);
4544 return send_error (ctx
, rc
);
4546 if (!strcasecmp (line
, "clients"))
4548 rc
= send_client_list (ctx
);
4550 else if (!strcasecmp (line
, "cache"))
4552 if (client
->opts
& OPT_DATA
)
4554 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4555 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4558 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4560 else if (!strcasecmp (line
, "pid"))
4563 pid_t pid
= getpid ();
4565 snprintf (buf
, sizeof (buf
), "%i", pid
);
4566 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4568 else if (!strcasecmp (line
, "version"))
4570 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4577 "", use_agent
? "AGENT" : "");
4578 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4581 else if (!strcasecmp (line
, "last_error"))
4583 if (client
->last_error
)
4584 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4586 rc
= GPG_ERR_NO_DATA
;
4588 else if (!strcasecmp (line
, "user"))
4593 if (client
->thd
->remote
)
4594 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4596 user
= get_username (client
->thd
->peer
->uid
);
4598 user
= get_username (client
->thd
->peer
->uid
);
4602 pthread_cleanup_push (xfree
, user
);
4603 rc
= xfer_data (ctx
, user
, strlen (user
));
4604 pthread_cleanup_pop (1);
4607 rc
= GPG_ERR_NO_DATA
;
4610 rc
= gpg_error (GPG_ERR_SYNTAX
);
4612 return send_error (ctx
, rc
);
4617 send_data_cb (void *user
, const void *buf
, size_t len
)
4619 assuan_context_t ctx
= user
;
4621 return assuan_send_data (ctx
, buf
, len
);
4625 send_status_cb (void *user
, const char *line
)
4627 assuan_context_t ctx
= user
;
4628 char keyword
[200], *k
;
4631 for (p
= line
, k
= keyword
; *p
; p
++)
4643 while (isspace (*p
))
4646 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4651 kill_command (assuan_context_t ctx
, char *line
)
4653 #ifdef HAVE_PTHREAD_CANCEL
4654 struct client_s
*client
= assuan_get_pointer (ctx
);
4657 if (!line
|| !*line
)
4658 return send_error (ctx
, GPG_ERR_SYNTAX
);
4660 rc
= peer_is_invoker (client
);
4665 MUTEX_LOCK (&cn_mutex
);
4666 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4667 t
= slist_length (cn_thread_list
);
4670 for (i
= 0; i
< t
; i
++)
4672 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4673 char *tmp
= str_asprintf ("%p", thd
->tid
);
4675 if (strcmp (line
, tmp
))
4682 rc
= pthread_cancel (thd
->tid
);
4686 pthread_cleanup_pop (1);
4689 return send_error (ctx
, rc
);
4691 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4696 agent_command (assuan_context_t ctx
, char *line
)
4701 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4704 struct client_s
*client
= assuan_get_pointer (ctx
);
4706 if (!line
|| !*line
)
4707 return send_error (ctx
, GPG_ERR_SYNTAX
);
4709 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4710 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4711 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4712 send_status_cb
, client
->ctx
);
4713 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4718 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4721 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4723 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4727 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4729 return send_error (ctx
, rc
);
4735 /* !BEGIN-HELP-TEXT!
4737 * This comment is used as a marker to generate the offline documentation
4738 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4739 * script to determine where commands begin and end.
4741 new_command("HELP", 1, 1, help_command
, _(
4742 "HELP [<COMMAND>]\n"
4743 "Show available commands or command specific help text."
4746 new_command("AGENT", 1, 1, agent_command
, _(
4748 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4749 "@command{gpg-agent}."
4752 new_command("KILL", 1, 0, kill_command
, _(
4753 "KILL <thread_id>\n"
4754 "Terminates the client identified by @var{thread_id} and releases any file "
4755 "lock or other resources it has held. @xref{GETINFO} for details about listing "
4756 "connected clients.\n"
4759 new_command("GETINFO", 1, 1, getinfo_command
, _(
4760 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4761 "Get server and other information: @var{CACHE} returns the number of cached "
4762 "documents via a status message. @var{CLIENTS} returns the number of "
4763 "connected clients via a status message or a list of connected clients when "
4764 "the @option{--verbose} parameter is used. The list contains space delimited "
4765 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4766 "file lock status, whether the current client is self, client state and "
4767 "user ID or TLS fingerprint of the connected client. "
4768 "Client state @code{0} is an unknown client state, @code{1} indicates the "
4769 "client has connected but hasn't completed initializing, @code{2} indicates "
4770 "that the client is idle, @code{3} means the "
4771 "client is in a command and @code{4} means the client is disconnecting. This "
4772 "line is always returned with a data response. @var{PID} returns the process "
4773 "ID number of the server via a data response. @var{VERSION} returns the server "
4774 "version number and compile-time features with a data response with each "
4775 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4776 "the last failed command when available. @var{USER} returns the username or "
4777 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4779 "When the @option{--data} option is specified then the result will be sent "
4780 "via a data response rather than a status message."
4783 new_command("PASSWD", 0, 0, passwd_command
, _(
4784 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4785 "Changes the passphrase of the secret key required to open the current "
4786 "file or the passphrase of a symmetrically encrypted data file. When the "
4787 "@option{--reset} option is passed then the cache entry for the current "
4788 "file will be reset and the passphrase, if any, will be required during the "
4789 "next @code{OPEN} (@pxref{OPEN})."
4791 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4792 "of hash iterations for a passphrase and must be either @code{0} to use "
4793 "the calibrated count of the machine (the default), or a value greater than "
4794 "or equal to @code{65536}. This option has no effect for symmetrically "
4795 "encrypted data files."
4797 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4798 "the data file, although a passphrase may be required when changing it."
4800 "This command is not available for non-invoking clients "
4801 "(@pxref{Access Control})."
4804 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4805 "KEYGRIP [--sign] <filename>\n"
4806 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4809 "When the @option{--sign} option is specified then the key used for signing "
4810 "of the specified @var{filename} will be returned."
4812 "For symmetrically encrypted data files this command returns the error "
4813 "GPG_ERR_NOT_SUPPORTED."
4816 new_command("OPEN", 1, 1, open_command
, _(
4817 "OPEN [--lock] <filename> [<passphrase>]\n"
4818 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4819 "found on the file-system then a new document will be created. If the file "
4820 "is found, it is looked for in the file cache. If cached and no "
4821 "@var{passphrase} was specified then the cached document is opened. When not "
4822 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4823 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4826 "When the @option{--lock} option is passed then the file mutex will be "
4827 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4828 "file has been opened."
4831 new_command("SAVE", 0, 0, save_command
, _(
4832 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4833 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4834 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4835 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4836 "keypair will be generated and a pinentry will be used to prompt for the "
4837 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4838 "passed in which case the data file will not be passphrase protected. "
4840 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4841 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4842 "use is enabled. The datafile will be symmetrically encrypted and will not "
4843 "use or generate any keypair."
4845 "The @option{--reset} option will clear the cache entry for the current file "
4846 "and require a passphrase, if needed, before saving."
4848 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4849 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4850 "(@pxref{Configuration}) for available ciphers."
4852 "The @option{--cipher-iterations} option specifies the number of times to "
4853 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4855 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4856 "the client to obtain the key paramaters to use when generating a new "
4857 "keypair. The inquired data is expected to be an S-expression. If not "
4858 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4859 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4860 "that when this option is specified a new keypair will be generated "
4861 "reguardless if the file is a new one and that if the data file is protected "
4862 "the passphrase to open it will be required before generating the new "
4863 "keypair. This option is not available for non-invoking clients "
4864 "(@pxref{Access Control})."
4866 "You can encrypt the data file to a public key other than the one that it "
4867 "was originally encrypted with by passing the @option{--keygrip} option with "
4868 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4869 "be of any key that @command{gpg-agent} knows about. The "
4870 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4871 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4872 "keygrip of an existing data file. This option may be needed when using a "
4873 "smartcard. This option has no effect with symmetrically encrypted data "
4874 "files. These options are not available for non-invoking clients "
4875 "(@pxref{Access Control})."
4877 "The @option{--s2k-count} option sets number of hash iterations for a "
4878 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4879 "value and is the default. This setting only affects new files. To change "
4880 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4881 "has no effect with symmetrically encrypted data files."
4884 new_command("ISCACHED", 1, 0, iscached_command
, _(
4885 "ISCACHED [--lock] <filename>\n"
4886 "An @emph{OK} response is returned if the specified @var{filename} is found "
4887 "in the file cache. If not found in the cache but exists on the filesystem "
4888 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4891 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4892 "file exists; it does not need to be opened nor cached. The lock will be "
4893 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4897 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4898 "CLEARCACHE [<filename>]\n"
4899 "Clears a file cache entry for all or the specified @var{filename}."
4902 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4903 "CACHETIMEOUT <filename> <seconds>\n"
4904 "The time in @var{seconds} until @var{filename} will be removed from the "
4905 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4906 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4907 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4911 new_command("LIST", 0, 1, list_command
, _(
4912 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4913 "If no element path is given then a newline separated list of root elements "
4914 "is returned with a data response. If given, then all reachable elements "
4915 "of the specified element path are returned unless the @option{--no-recurse} "
4916 "option is specified. If specified, only the child elements of the element "
4917 "path are returned without recursing into grandchildren. Each resulting "
4918 "element is prefixed with the literal @code{!} character when the element "
4919 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4921 "When the @option{--verbose} option is passed then each element path "
4922 "returned will have zero or more flags appened to it. These flags are "
4923 "delimited from the element path by a single space character. A flag itself "
4924 "is a single character. Flag @code{P} indicates that access to the element "
4925 "is denied. Flag @code{+} indicates that there are child nodes of "
4926 "the current element path. Flag @code{E} indicates that an element of an "
4927 "element path contained in a @var{target} attribute could not be found. Flag "
4928 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4929 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4930 "of the @var{target} attribute contained in the current element (see below)."
4932 "The @option{--with-target} option implies @option{--verbose} and will append "
4933 "an additional flag @code{T} followed by a single space then an element path. "
4934 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4935 "current element when it contains a @var{target} attribute. When no "
4936 "@var{target} attribute is found then no flag will be appended."
4938 "The @option{--no-recurse} option limits the amount of data returned to only "
4939 "the listing of children of the specified element path and not any "
4942 "The @option{--all} option lists the entire element tree for each root "
4943 "element. This option also implies option @option{--verbose}."
4945 "When the @option{--inquire} option is passed then all remaining non-option "
4946 "arguments are retrieved via a server @emph{INQUIRE}."
4949 new_command("REALPATH", 0, 1, realpath_command
, _(
4950 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4951 "Resolves all @code{target} attributes of the specified element path and "
4952 "returns the result with a data response. @xref{Target Attribute}, for details."
4954 "When the @option{--inquire} option is passed then all remaining non-option "
4955 "arguments are retrieved via a server @emph{INQUIRE}."
4958 new_command("STORE", 0, 1, store_command
, _(
4959 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4960 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4962 "Creates a new element path or modifies the @var{content} of an existing "
4963 "element. If only a single element is specified then a new root element is "
4964 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4965 "set to the final @key{TAB} delimited element. If no @var{content} is "
4966 "specified after the final @key{TAB}, then the content of an existing "
4967 "element will be removed; or empty when creating a new element."
4969 "The only restriction of an element name is that it not contain whitespace "
4970 "or begin with the literal element character @code{!} unless specifying a "
4971 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4972 "the @key{TAB} delimited elements. It is recommended that the content of an "
4973 "element be base64 encoded when it contains control or @key{TAB} characters "
4974 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4977 new_command("RENAME", 0, 1, rename_command
, _(
4978 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4979 "Renames the specified @var{element} to the new @var{value}. If an element of "
4980 "the same name as the @var{value} already exists it will be overwritten."
4982 "When the @option{--inquire} option is passed then all remaining non-option "
4983 "arguments are retrieved via a server @emph{INQUIRE}."
4986 new_command("COPY", 0, 1, copy_command
, _(
4987 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4988 "Copies the entire element tree starting from the child node of the source "
4989 "element, to the destination element path. If the destination element path "
4990 "does not exist then it will be created; otherwise it is overwritten."
4992 "Note that attributes from the source element are merged into the "
4993 "destination element when the destination element path exists. When an "
4994 "attribute of the same name exists in both the source and destination "
4995 "elements then the destination attribute will be updated to the source "
4998 "When the @option{--inquire} option is passed then all remaining non-option "
4999 "arguments are retrieved via a server @emph{INQUIRE}."
5002 new_command("MOVE", 0, 1, move_command
, _(
5003 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
5004 "Moves the source element path to the destination element path. If the "
5005 "destination is not specified then it will be moved to the root node of the "
5006 "document. If the destination is specified and exists then it will be "
5007 "overwritten; otherwise non-existing elements of the destination element "
5008 "path will be created."
5010 "When the @option{--inquire} option is passed then all remaining non-option "
5011 "arguments are retrieved via a server @emph{INQUIRE}."
5014 new_command("DELETE", 0, 1, delete_command
, _(
5015 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
5016 "Removes the specified element path and all of its children. This may break "
5017 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
5018 "refers to this element or any of its children."
5020 "When the @option{--inquire} option is passed then all remaining non-option "
5021 "arguments are retrieved via a server @emph{INQUIRE}."
5024 new_command("GET", 0, 1, get_command
, _(
5025 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
5026 "Retrieves the content of the specified element. The content is returned "
5027 "with a data response."
5029 "When the @option{--inquire} option is passed then all remaining non-option "
5030 "arguments are retrieved via a server @emph{INQUIRE}."
5033 new_command("ATTR", 0, 1, attr_command
, _(
5034 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
5036 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
5038 " Stores or updates an @var{attribute} name and optional @var{value} of an "
5039 "element. When no @var{value} is specified any existing value will be removed."
5041 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
5043 " Removes an @var{attribute} from an element."
5045 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
5047 " Retrieves a newline separated list of attributes names and values "
5048 "from the specified element. Each attribute name and value is space delimited."
5050 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
5052 " Retrieves the value of an @var{attribute} from an element."
5055 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
5056 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
5059 "The @code{_mtime} attribute is updated each time an element is modified by "
5060 "either storing content, editing attributes or by deleting a child element. "
5061 "The @code{_ctime} attribute is created for each new element in an element "
5064 "When the @option{--inquire} option is passed then all remaining non-option "
5065 "arguments are retrieved via a server @emph{INQUIRE}."
5067 "@xref{Target Attribute}, for details about this special attribute."
5070 new_command("XPATH", 0, 1, xpath_command
, _(
5071 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
5072 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
5073 "specified it is assumed the expression is a request to return a result. "
5074 "Otherwise, the result is set to the @var{value} argument and the document is "
5075 "updated. If there is no @var{value} after the @key{TAB} character, the value "
5076 "is assumed to be empty and the document is updated. For example:"
5079 "XPATH //element[@@_name='password']@key{TAB}\n"
5082 "would clear the content of all @code{password} elements in the data file "
5083 "while leaving off the trailing @key{TAB} would return all @code{password} "
5084 "elements in @abbr{XML} format."
5086 "When the @option{--inquire} option is passed then all remaining non-option "
5087 "arguments are retrieved via a server @emph{INQUIRE}."
5089 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5090 "expression syntax."
5093 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
5094 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
5095 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
5096 "attributes and does not return a result. For the @var{SET} operation the "
5097 "@var{value} is optional but the field is required. If not specified then "
5098 "the attribute value will be empty. For example:"
5101 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
5104 "would create an @code{password} attribute for each @code{password} element "
5105 "found in the document. The attribute value will be empty but still exist."
5107 "When the @option{--inquire} option is passed then all remaining non-option "
5108 "arguments are retrieved via a server @emph{INQUIRE}."
5110 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5111 "expression syntax."
5114 new_command("IMPORT", 0, 1, import_command
, _(
5115 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
5116 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5118 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
5119 "argument is raw @abbr{XML} data. The content is created as a child of "
5120 "the element path specified with the @option{--root} option or at the "
5121 "document root when not specified. Existing elements of the same name will "
5124 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
5128 new_command("DUMP", 0, 1, dump_command
, _(
5130 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
5131 "dumping a specific node."
5134 new_command("LOCK", 0, 0, lock_command
, _(
5136 "Locks the mutex associated with the opened file. This prevents other clients "
5137 "from sending commands to the same opened file until the client "
5138 "that sent this command either disconnects or sends the @code{UNLOCK} "
5139 "command. @xref{UNLOCK}."
5142 new_command("UNLOCK", 1, 0, unlock_command
, _(
5144 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
5145 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
5146 "@pxref{ISCACHED})."
5149 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
5150 "GETCONFIG [filename] <parameter>\n"
5151 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
5152 "data response. If no file has been opened then the value for @var{filename} "
5153 "or the default from the @samp{global} section will be returned. If a file "
5154 "has been opened and no @var{filename} is specified, a value previously "
5155 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
5158 new_command("OPTION", 1, 1, option_command
, _(
5159 "OPTION <NAME>=<VALUE>\n"
5160 "Sets a client option @var{name} to @var{value}. The value for an option is "
5161 "kept for the duration of the connection."
5164 "@item DISABLE-PINENTRY\n"
5165 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
5166 "server inquire is sent to the client to obtain the passphrase. This option "
5167 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
5168 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
5170 "@item PINENTRY-TIMEOUT\n"
5171 "Sets the number of seconds before a pinentry prompt will return an error "
5172 "while waiting for user input."
5175 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5178 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5181 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5183 "@item PINENTRY-DESC\n"
5184 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
5186 "@item PINENTRY-TITLE\n"
5187 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
5189 "@item PINENTRY-PROMPT\n"
5190 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
5193 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5195 "@item LC-MESSAGES\n"
5196 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5199 "Associates the thread ID of the connection with the specified textual "
5200 "representation. Useful for debugging log messages. May not contain whitespace."
5202 "@item LOCK-TIMEOUT\n"
5203 "When not @code{0}, the duration in tenths of a second to wait for the file "
5204 "mutex which has been locked by another thread to be released before returning "
5205 "an error. When @code{-1}, then an error will be returned immediately."
5208 "An integer specifiying the logging level."
5212 new_command("LS", 1, 1, ls_command
, _(
5214 "Lists the available data files stored in the data directory "
5215 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5218 new_command("RESET", 1, 1, NULL
, _(
5220 "Closes the currently opened file but keeps any previously set client options."
5223 new_command("NOP", 1, 1, NULL
, _(
5225 "Does nothing. Always returns successfully."
5228 /* !END-HELP-TEXT! */
5229 new_command ("CANCEL", 1, 1, NULL
, NULL
);
5230 new_command ("END", 1, 1, NULL
, NULL
);
5231 new_command ("BYE", 1, 1, NULL
, NULL
);
5234 for (i
= 0; command_table
[i
]; i
++);
5235 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
5240 register_commands (assuan_context_t ctx
)
5244 for (; command_table
[i
]; i
++)
5246 if (!command_table
[i
]->handler
)
5249 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5250 command_table
[i
]->handler
,
5251 command_table
[i
]->help
);
5256 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5260 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5264 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5268 return assuan_register_post_cmd_notify (ctx
, command_finalize
);