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>
41 #include "pwmd-error.h"
46 #include "util-misc.h"
55 /* These are command option flags. */
56 #define OPT_INQUIRE 0x0001
57 #define OPT_NO_PASSPHRASE 0x0002
58 #define OPT_RESET 0x0004
59 #define OPT_LIST_RECURSE 0x0008
60 #define OPT_VERBOSE 0x0010
61 #define OPT_LOCK 0x0020
62 #define OPT_LOCK_ON_OPEN 0x0040
63 #define OPT_SIGN 0x0080
64 #define OPT_LIST_ALL 0x0100
65 #define OPT_LIST_WITH_TARGET 0x0200
66 #define OPT_DATA 0x0400
67 #define OPT_NO_AGENT 0x0800
68 #define OPT_ASK 0x1000
71 /* The GETCONFIG command, for example, may fail because pwmd lost the
72 * gpg-agent connection. Update the recovered agent ctx. */
73 #define UPDATE_AGENT_CTX(client, crypto) do { \
74 if (crypto && crypto->agent && crypto->agent->did_restart \
75 && client->crypto && client->crypto->agent \
76 && client->crypto->agent->ctx && \
77 crypto->agent->ctx != client->crypto->agent->ctx) \
79 client->crypto->agent->ctx = crypto->agent->ctx; \
80 crypto->agent->ctx = NULL; \
84 #define UPDATE_AGENT_CTX(client, crypto)
87 #define FLOCK_TYPE_NONE 0
88 #define FLOCK_TYPE_SH 0x0001
89 #define FLOCK_TYPE_EX 0x0002
90 #define FLOCK_TYPE_KEEP 0x0004
92 struct command_table_s
95 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
98 int unlock
; // unlock the file mutex after validating the checksum
102 static struct command_table_s
**command_table
;
104 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
105 static gpg_error_t
validate_checksum (struct client_s
*,
106 struct cache_data_s
*);
107 static gpg_error_t
update_checksum (struct client_s
*client
);
109 /* When 'status' is true the 'self' field of the status line will be false
110 * because we never send the STATE status message to the same client that
113 build_client_info_line (struct client_thread_s
*thd
, int status
)
115 MUTEX_LOCK (&cn_mutex
);
116 int with_state
= config_get_boolean ("global", "send_state");
117 char *uid
, *username
= NULL
;
122 uid
= str_asprintf("#%s", thd
->tls
->fp
);
125 uid
= str_asprintf("%u", thd
->peer
->uid
);
126 username
= get_username (thd
->peer
->uid
);
129 uid
= str_asprintf("%u", thd
->peer
->uid
);
130 username
= get_username (thd
->peer
->uid
);
132 line
= str_asprintf ("%p %s %s %s %u %u %u %s %s",
134 thd
->name
? thd
->name
: "-",
135 thd
->cl
&& thd
->cl
->filename
136 && (thd
->cl
->flags
& FLAG_OPEN
)
137 ? thd
->cl
->filename
: "/",
139 thd
->remote
? thd
->peeraddr
: "-",
143 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
144 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
145 with_state
? thd
->state
: CLIENT_STATE_UNKNOWN
, uid
,
147 thd
->remote
? "" : username
155 MUTEX_UNLOCK (&cn_mutex
);
160 update_client_state (struct client_s
*client
, unsigned s
)
162 client
->thd
->state
= s
;
163 int n
= config_get_boolean ("global", "send_state");
165 if (n
&& client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
167 char *line
= build_client_info_line (client
->thd
, 1);
170 send_status_all_not_self (n
== 2, STATUS_STATE
, "%s", line
);
175 unlock_file_mutex (struct client_s
*client
, int remove
)
179 // OPEN: keep the lock for the same file being reopened.
180 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
183 if (!(client
->flags
& FLAG_HAS_LOCK
))
184 return GPG_ERR_NOT_LOCKED
;
186 rc
= cache_unlock_mutex (client
->md5file
, remove
);
188 rc
= GPG_ERR_INV_STATE
;
190 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
196 lock_file_mutex (struct client_s
*client
, int add
)
199 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
201 if (client
->flags
& FLAG_HAS_LOCK
)
204 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
205 client
->lock_timeout
, add
, timeout
);
207 client
->flags
|= FLAG_HAS_LOCK
;
213 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
216 int type
= !cmd
->flock_type
|| (cmd
->flock_type
& FLOCK_TYPE_SH
)
219 if (!(client
->flags
& FLAG_OPEN
))
220 return GPG_ERR_INV_STATE
;
222 rc
= lock_file_mutex (client
, 0);
223 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
226 rc
= lock_flock (client
->ctx
, client
->filename
, type
, &client
->flock_fd
);
229 rc
= validate_checksum (client
, NULL
);
230 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
233 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
235 else if (gpg_err_code(rc
) == GPG_ERR_ENOENT
)
238 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
239 unlock_file_mutex (client
, 0);
241 if (rc
|| !(cmd
->flock_type
& FLOCK_TYPE_KEEP
))
242 unlock_flock (&client
->flock_fd
);
248 parse_xml (assuan_context_t ctx
, int new)
250 struct client_s
*client
= assuan_get_pointer (ctx
);
251 int cached
= client
->doc
!= NULL
;
256 client
->doc
= new_document ();
262 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
263 client
->crypto
->plaintext
= result
;
264 client
->crypto
->plaintext_len
= len
;
265 if (!client
->crypto
->plaintext
)
267 xmlFreeDoc (client
->doc
);
276 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
277 client
->crypto
->plaintext_len
, (xmlDocPtr
*)&client
->doc
);
283 free_client (struct client_s
*client
)
286 xmlFreeDoc (client
->doc
);
289 xfree (client
->filename
);
290 xfree (client
->last_error
);
294 cleanup_crypto_stage2 (client
->crypto
);
295 if (client
->crypto
->pkey_sexp
)
296 gcry_sexp_release (client
->crypto
->pkey_sexp
);
298 if (client
->crypto
->sigpkey_sexp
)
299 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
301 client
->crypto
->pkey_sexp
= NULL
;
302 client
->crypto
->sigpkey_sexp
= NULL
;
303 memset (client
->crypto
->sign_grip
, 0,
304 sizeof (client
->crypto
->sign_grip
));
305 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
310 cleanup_client (struct client_s
*client
)
312 assuan_context_t ctx
= client
->ctx
;
313 struct client_thread_s
*thd
= client
->thd
;
314 struct crypto_s
*crypto
= client
->crypto
;
315 long lock_timeout
= client
->lock_timeout
;
316 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
317 struct pinentry_option_s pin_opts
;
318 xmlErrorPtr xml_error
= client
->xml_error
;
319 int flock_fd
= client
->flock_fd
;
321 struct pinentry_option_s agent_pin_opts
;
323 if (crypto
&& crypto
->agent
)
324 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
325 sizeof(struct pinentry_option_s
));
328 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
329 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
330 free_client (client
);
331 memset (client
, 0, sizeof (struct client_s
));
332 client
->flock_fd
= flock_fd
;
333 client
->xml_error
= xml_error
;
334 client
->crypto
= crypto
;
337 client
->lock_timeout
= lock_timeout
;
338 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
339 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
341 if (crypto
&& crypto
->agent
)
342 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
343 sizeof(struct pinentry_option_s
));
348 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
350 struct client_s
*client
= assuan_get_pointer (ctx
);
352 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
353 int cached
= 0, keyarg
= key
? 1 : 0;
355 unsigned char *salted_key
= NULL
;
359 char *pin_title
= client
->pinentry_opts
.title
;
361 client
->crypto
->filename
= str_dup (client
->filename
);
362 if (cdata
|| client
->flags
& FLAG_NEW
)
364 if (cdata
&& !(client
->flags
& FLAG_NEW
))
366 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
371 cached
= cdata
!= NULL
;
375 if (!key
&& !IS_PKI (client
->crypto
)
376 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
378 if (client
->flags
& FLAG_NO_PINENTRY
)
380 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
387 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
390 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
397 if (!IS_PKI (client
->crypto
))
399 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
402 key
= gcry_malloc (keylen
);
403 memset (key
, 0, keylen
);
406 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
407 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
408 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
409 (void **)&salted_key
, &keysize
,
410 client
->crypto
->hdr
.version
<= 0x03000e ? COMPAT_KDFS2K_ITERATIONS
: client
->crypto
->hdr
.s2k_count
);
416 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
417 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
418 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
420 gcry_free (salted_key
);
424 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
429 if (client
->pinentry_opts
.title
!= pin_title
)
430 xfree (client
->pinentry_opts
.title
);
432 client
->pinentry_opts
.title
= pin_title
;
435 gcry_free (salted_key
);
439 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
442 gcry_free (salted_key
);
443 return GPG_ERR_ENOMEM
;
446 cdata
->key
= salted_key
;
447 cdata
->keylen
= keysize
;
451 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
453 rc
= GPG_ERR_NOT_IMPLEMENTED
;
459 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
461 free_cache_data_once (cdata
);
465 int timeout
= config_get_integer (client
->filename
,
471 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
473 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
474 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
475 client
->crypto
->plaintext_len
, &cdata
->doc
,
476 &cdata
->doclen
, &cache_iv
, &cache_blocksize
);
477 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
479 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
480 "%S", client
->crypto
->pkey_sexp
);
485 gcry_sexp_release (cdata
->sigkey
);
487 cdata
->sigkey
= NULL
;
488 if (!rc
&& IS_PKI (client
->crypto
))
489 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
490 "%S", client
->crypto
->sigpkey_sexp
);
494 rc
= cache_add_file (client
->md5file
,
495 (client
->flags
& FLAG_NEW
) ? NULL
496 : client
->crypto
->grip
, cdata
, timeout
);
501 free_cache_data_once (cdata
);
502 xmlFreeDoc (client
->doc
);
509 client
->flags
|= FLAG_OPEN
;
515 rc
= update_checksum (client
);
516 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
520 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
521 rc
= do_lock (client
, 0);
527 req_cleanup (void *arg
)
532 strv_free ((char **) arg
);
536 parse_open_opt_lock (void *data
, void *value
)
538 struct client_s
*client
= data
;
540 client
->opts
|= OPT_LOCK_ON_OPEN
;
545 parse_save_opt_inquire (void *data
, void *value
)
547 struct client_s
*client
= data
;
550 if (!(client
->flags
& FLAG_NEW
))
552 rc
= peer_is_invoker (client
);
553 if (rc
== GPG_ERR_EACCES
)
558 client
->opts
|= OPT_INQUIRE
;
563 parse_opt_inquire (void *data
, void *value
)
565 struct client_s
*client
= data
;
568 client
->opts
|= OPT_INQUIRE
;
573 update_checksum (struct client_s
*client
)
577 struct cache_data_s
*cdata
;
578 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
585 cdata
= cache_get_data (client
->md5file
);
589 cdata
->crc
= xmalloc (len
);
590 memcpy (cdata
->crc
, crc
, len
);
597 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
604 if (cdata
&& !cdata
->crc
)
605 return GPG_ERR_CHECKSUM
;
607 rc
= get_checksum (client
->filename
, &crc
, &len
);
612 n
= memcmp (cdata
->crc
, crc
, len
);
613 else if (client
->crc
)
614 n
= memcmp (client
->crc
, crc
, len
);
617 return n
? GPG_ERR_CHECKSUM
: 0;
621 do_open (assuan_context_t ctx
, const char *password
)
623 struct client_s
*client
= assuan_get_pointer (ctx
);
624 struct cache_data_s
*cdata
;
629 cdata
= cache_get_data (client
->md5file
);
630 if (cdata
&& cdata
->doc
)
634 /* This will check that the key is cached in the agent which needs to
635 * be determined for files that share a keygrip. */
638 rc
= cache_iscached (client
->filename
, &defer
);
639 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
640 rc
= GPG_ERR_KEY_EXPIRED
;
643 if (!rc
&& !(client
->flags
& FLAG_NEW
))
644 rc
= validate_checksum (client
, cdata
);
647 if (!rc
&& client
->thd
->remote
648 && config_get_boolean (client
->filename
, "tcp_require_key"))
649 rc
= GPG_ERR_KEY_EXPIRED
;
652 if (!rc
&& !password
)
654 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
658 if (IS_PKI (client
->crypto
))
660 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
662 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
663 client
->crypto
->grip
);
664 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
666 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
667 client
->crypto
->sign_grip
);
672 rc
= open_finalize (ctx
, NULL
, 0);
678 /* There was an error accessing the file so clear the cache entry. The
679 * real error will be returned from read_data_file() since the file
680 * may have only disappeared. */
683 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
684 rc
= cache_clear (client
->md5file
);
685 send_status_all (STATUS_CACHE
, NULL
);
692 rc
= read_data_file (client
->filename
, client
->crypto
);
695 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
696 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
698 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
702 client
->flags
|= FLAG_NEW
;
703 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
704 memset (client
->crypto
->sign_grip
, 0,
705 sizeof (client
->crypto
->sign_grip
));
706 rc
= open_finalize (ctx
, NULL
, 0);
710 if (password
&& IS_PKI (client
->crypto
))
713 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
719 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
724 open_command (assuan_context_t ctx
, char *line
)
727 struct client_s
*client
= assuan_get_pointer (ctx
);
728 char **req
, *filename
;
729 unsigned char md5file
[16];
731 assuan_peercred_t peer
;
732 struct argv_s
*args
[] = {
733 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
737 rc
= parse_options (&line
, args
, client
, 1);
739 return send_error (ctx
, rc
);
741 req
= str_split (line
, " ", 2);
743 return send_error (ctx
, GPG_ERR_SYNTAX
);
745 rc
= do_validate_peer (ctx
, req
[0], &peer
);
749 return send_error (ctx
, rc
);
753 if (!valid_filename (filename
))
756 return send_error (ctx
, GPG_ERR_INV_VALUE
);
759 pthread_cleanup_push (req_cleanup
, req
);
760 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
761 /* This client may have locked a different file with ISCACHED --lock than
762 * the current filename. This will remove that lock. */
763 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
764 if (client
->flags
& FLAG_OPEN
||
765 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
767 uint32_t opts
= client
->opts
;
768 uint32_t flags
= client
->flags
;
771 client
->flags
|= FLAG_KEEP_LOCK
;
772 else if (client
->flags
& FLAG_NEW
)
773 cache_clear (client
->md5file
);
775 cleanup_client (client
);
777 client
->flags
|= flags
;
778 client
->flags
&= ~(FLAG_LOCK_CMD
);
780 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
783 /* Need to lock the mutex here because file_modified() cannot without
784 * knowing the filename. */
785 memcpy (client
->md5file
, md5file
, 16);
786 rc
= lock_file_mutex (client
, 1);
788 client
->flags
&= ~FLAG_OPEN
;
792 rc
= lock_flock (ctx
, filename
, LOCK_SH
, &client
->flock_fd
);
793 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_ENOENT
)
796 client
->filename
= str_dup (filename
);
797 if (!client
->filename
)
804 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
807 if (IS_PKI (client
->crypto
))
808 rc
= set_pinentry_mode (client
->crypto
->agent
,
809 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
814 rc
= do_open (ctx
, password
);
816 cleanup_client (client
);
818 cleanup_crypto_stage1 (client
->crypto
);
822 pthread_cleanup_pop (1);
824 if (!rc
&& client
->flags
& FLAG_NEW
)
825 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
828 (void) kill_scd (client
->crypto
->agent
);
831 return send_error (ctx
, rc
);
835 parse_opt_no_passphrase (void *data
, void *value
)
837 struct client_s
*client
= data
;
840 client
->opts
|= OPT_NO_PASSPHRASE
;
845 parse_save_opt_no_agent (void *data
, void *value
)
847 struct client_s
*client
= data
;
849 client
->opts
|= OPT_NO_AGENT
;
854 parse_save_opt_cipher (void *data
, void *value
)
856 struct client_s
*client
= data
;
857 int algo
= cipher_string_to_gcrypt ((char *) value
);
858 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
862 return GPG_ERR_INV_VALUE
;
864 if (!(client
->flags
& FLAG_NEW
))
866 rc
= peer_is_invoker (client
);
867 if (rc
== GPG_ERR_EACCES
)
871 flags
&= ((uint16_t) ((uint32_t) (flags
& 0xFFFFFFFF)));
872 if (!(client
->crypto
->hdr
.flags
& gcrypt_to_cipher (algo
)))
882 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
889 permitted_to_save (struct client_s
*client
, const unsigned char *grip
,
890 size_t size
, const char *value
)
895 if (!(client
->flags
& FLAG_NEW
))
897 hexgrip
= bin2hex (grip
, size
);
898 rc
= !strcmp (hexgrip
, (char *)value
) ? 0 : GPG_ERR_EACCES
;
901 rc
= peer_is_invoker (client
);
909 parse_save_opt_keygrip (void *data
, void *value
)
912 struct client_s
*client
= data
;
913 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->grip
,
914 sizeof (client
->crypto
->grip
),
917 if (!IS_PKI (client
->crypto
))
918 return GPG_ERR_INV_ARG
;
923 if (client
->crypto
->save
.pkey
)
924 gcry_sexp_release (client
->crypto
->save
.pkey
);
926 client
->crypto
->save
.pkey
= NULL
;
927 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
929 return GPG_ERR_INV_ARG
;
934 parse_save_opt_sign_keygrip (void *data
, void *value
)
937 struct client_s
*client
= data
;
938 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->sign_grip
,
939 sizeof (client
->crypto
->sign_grip
),
942 if (!IS_PKI (client
->crypto
))
943 return GPG_ERR_INV_ARG
;
948 if (client
->crypto
->save
.sigpkey
)
949 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
951 client
->crypto
->save
.sigpkey
= NULL
;
952 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
954 return GPG_ERR_INV_ARG
;
959 parse_opt_s2k_count (void *data
, void *value
)
961 struct client_s
*client
= data
;
966 return GPG_ERR_INV_VALUE
;
969 n
= strtoull (v
, &p
, 10);
970 if (n
== UINT64_MAX
&& errno
)
971 return gpg_error_from_errno (errno
);
973 return GPG_ERR_INV_VALUE
;
975 if (!(client
->flags
& FLAG_NEW
))
977 gpg_error_t rc
= peer_is_invoker (client
);
979 if (rc
== GPG_ERR_EACCES
)
981 if (n
&& client
->crypto
->hdr
.s2k_count
!= n
)
990 // Keep compatibility with libpwmd passing --s2k-count=0. If somehow
991 // s2k_count == 0 it will be set to DEFAULT_KDFS2K_ITERATIONS in
994 client
->crypto
->save
.hdr
.s2k_count
= n
;
1000 save_finalize (assuan_context_t ctx
)
1002 struct client_s
*client
= assuan_get_pointer (ctx
);
1004 xmlChar
*xmlbuf
= NULL
;
1009 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
1011 return GPG_ERR_ENOMEM
;
1013 pthread_cleanup_push (xmlFree
, xmlbuf
);
1015 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
1016 && (client
->opts
& OPT_NO_AGENT
))
1017 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
1019 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
1020 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
1021 NULL
, &key
, &keylen
, 1, 1,
1022 (client
->opts
& OPT_NO_PASSPHRASE
));
1027 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
1028 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
1031 pubkey
= client
->crypto
->pkey_sexp
;
1035 sigkey
= client
->crypto
->sigpkey_sexp
;
1038 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
1039 sigkey
= client
->crypto
->save
.sigpkey
;
1043 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
1044 client
->filename
, xmlbuf
, xmlbuflen
);
1045 if (pubkey
== client
->crypto
->save
.pkey
)
1049 gcry_sexp_release (client
->crypto
->pkey_sexp
);
1050 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
1051 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
1052 client
->crypto
->grip
);
1055 gcry_sexp_release (pubkey
);
1057 client
->crypto
->save
.pkey
= NULL
;
1061 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
1063 if (sigkey
== client
->crypto
->save
.sigpkey
)
1067 if (client
->crypto
->sigpkey_sexp
)
1068 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
1070 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
1071 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
1072 client
->crypto
->sign_grip
);
1075 gcry_sexp_release (sigkey
);
1077 client
->crypto
->save
.sigpkey
= NULL
;
1086 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
1087 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
1090 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
1091 send_status_all (STATUS_CACHE
, NULL
);
1095 rc
= update_checksum (client
);
1096 client
->flags
&= ~(FLAG_NEW
);
1100 pthread_cleanup_pop (1); // xmlFree
1105 parse_opt_reset (void *data
, void *value
)
1107 struct client_s
*client
= data
;
1110 client
->opts
|= OPT_RESET
;
1115 parse_opt_ask (void *data
, void *value
)
1117 struct client_s
*client
= data
;
1120 client
->opts
|= OPT_ASK
;
1125 save_command (assuan_context_t ctx
, char *line
)
1127 struct client_s
*client
= assuan_get_pointer (ctx
);
1130 struct argv_s
*args
[] = {
1131 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1132 parse_opt_no_passphrase
},
1133 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
1134 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
1135 parse_save_opt_inquire
},
1136 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
1137 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
1138 parse_save_opt_sign_keygrip
},
1139 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
1140 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
1141 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
1142 parse_opt_s2k_count
},
1143 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
1144 parse_save_opt_no_agent
},
1145 &(struct argv_s
) {"ask", OPTION_TYPE_NOARG
,
1150 cleanup_save (&client
->crypto
->save
);
1151 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
1152 sizeof (file_header_t
));
1154 rc
= parse_options (&line
, args
, client
, 0);
1156 return send_error (ctx
, rc
);
1158 if (!(client
->flags
& FLAG_NEW
))
1159 client
->opts
&= ~OPT_NO_AGENT
;
1160 else if (client
->opts
& OPT_NO_AGENT
)
1162 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
1163 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
1166 /* Update to the default hash iteration count for data file
1167 * versions <= 3.0.14. */
1169 if ((!IS_PKI (client
->crypto
) && client
->crypto
->hdr
.version
<= 0x03000e
1170 && client
->crypto
->save
.hdr
.s2k_count
< DEFAULT_KDFS2K_ITERATIONS
)
1171 || (!IS_PKI (client
->crypto
) && !client
->crypto
->save
.hdr
.s2k_count
))
1173 if ((client
->crypto
->hdr
.version
<= 0x03000e
1174 && client
->crypto
->save
.hdr
.s2k_count
< DEFAULT_KDFS2K_ITERATIONS
)
1175 || !client
->crypto
->save
.hdr
.s2k_count
)
1178 client
->crypto
->save
.hdr
.s2k_count
=
1179 config_get_ulonglong (client
->filename
, "s2k_count");
1180 if (!client
->crypto
->save
.hdr
.s2k_count
)
1181 client
->crypto
->save
.hdr
.s2k_count
= DEFAULT_KDFS2K_ITERATIONS
;
1184 /* Deal with the hashed vs non-hashed cached key mess by clearing the cached
1186 if (client
->crypto
->hdr
.version
<= 0x03000e && !IS_PKI (client
->crypto
))
1187 client
->opts
|= OPT_RESET
;
1188 else if (!IS_PKI (client
->crypto
) && !(client
->flags
& FLAG_NEW
)
1189 && client
->crypto
->hdr
.s2k_count
!=
1190 client
->crypto
->save
.hdr
.s2k_count
)
1191 client
->opts
|= OPT_RESET
;
1193 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
1194 && !(client
->opts
& OPT_INQUIRE
))
1195 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1197 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1198 return send_error (ctx
, gpg_error_from_errno (errno
));
1200 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
1202 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
1203 return send_error (ctx
, GPG_ERR_ENOANO
);
1207 rc
= cache_iscached (client
->filename
, &defer
);
1208 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
1210 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1214 client
->opts
|= OPT_RESET
;
1215 else if (config_get_boolean ("global", "require_save_key"))
1216 client
->opts
|= OPT_ASK
;
1218 if (!rc
&& (client
->opts
& OPT_RESET
))
1220 rc
= cache_clear (client
->md5file
);
1222 return send_error (ctx
, rc
);
1224 log_write ("%s: %s", client
->filename
,
1225 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1226 send_status_all (STATUS_CACHE
, NULL
);
1229 if (!rc
&& client
->opts
& OPT_ASK
)
1230 rc
= crypto_try_decrypt (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
1231 client
->filename
, NULL
, NULL
, NULL
);
1234 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1237 return send_error (ctx
, rc
);
1240 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1241 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1242 && !(client
->opts
& OPT_NO_AGENT
))
1244 rc
= set_pinentry_mode (client
->crypto
->agent
,
1245 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1248 if (!(client
->flags
& FLAG_NEW
))
1250 struct crypto_s
*crypto
;
1254 /* Wanting to generate a new key. Require the key to open the
1255 current file before proceeding reguardless of the
1256 require_save_key configuration parameter. */
1257 rc
= cache_clear (client
->md5file
);
1260 rc
= init_client_crypto (&crypto
);
1262 crypto
->client_ctx
= client
->ctx
;
1266 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1267 client
->filename
, &key
, &keylen
, NULL
, NULL
);
1270 cleanup_crypto (&crypto
);
1274 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1278 struct inquire_data_s idata
= { 0 };
1279 char *params
= client
->opts
& OPT_INQUIRE
1280 ? NULL
: default_key_params (client
->crypto
);
1282 pthread_cleanup_push (xfree
, params
);
1283 idata
.crypto
= client
->crypto
;
1287 idata
.line
= params
;
1288 idata
.len
= strlen (params
);
1293 client
->crypto
->agent
->inquire_data
= &idata
;
1294 client
->crypto
->agent
->inquire_cb
= NULL
;
1295 rc
= generate_key (client
->crypto
, params
,
1296 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1297 pthread_cleanup_pop (1);
1303 rc
= save_finalize (ctx
);
1305 cleanup_crypto_stage1 (client
->crypto
);
1307 (void) kill_scd (client
->crypto
->agent
);
1309 return send_error (ctx
, rc
);
1313 do_delete (assuan_context_t ctx
, char *line
)
1315 struct client_s
*client
= assuan_get_pointer (ctx
);
1320 if (strchr (line
, '\t'))
1321 req
= str_split (line
, "\t", 0);
1323 req
= str_split (line
, " ", 0);
1326 return GPG_ERR_SYNTAX
;
1328 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1336 * No sub-node defined. Remove the entire node (root element).
1342 rc
= is_element_owner (client
, n
);
1345 rc
= unlink_node (client
, n
);
1354 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1355 NULL
, NULL
, 0, 0, NULL
, 0);
1360 rc
= is_element_owner (client
, n
);
1363 rc
= unlink_node (client
, n
);
1371 delete_command (assuan_context_t ctx
, char *line
)
1373 struct client_s
*client
= assuan_get_pointer (ctx
);
1375 struct argv_s
*args
[] = {
1376 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1380 rc
= parse_options (&line
, args
, client
, 1);
1381 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1382 rc
= GPG_ERR_SYNTAX
;
1384 return send_error (ctx
, rc
);
1386 if (client
->opts
& OPT_INQUIRE
)
1388 unsigned char *result
;
1391 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1393 return send_error (ctx
, rc
);
1395 pthread_cleanup_push (xfree
, result
);
1396 rc
= do_delete (ctx
, (char *)result
);
1397 pthread_cleanup_pop (1);
1400 rc
= do_delete (ctx
, line
);
1402 return send_error (ctx
, rc
);
1406 store_command (assuan_context_t ctx
, char *line
)
1408 struct client_s
*client
= assuan_get_pointer (ctx
);
1411 unsigned char *result
;
1413 xmlNodePtr n
, parent
;
1415 char *content
= NULL
;
1418 return send_error (ctx
, GPG_ERR_SYNTAX
);
1420 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1422 return send_error (ctx
, rc
);
1424 req
= str_split ((char *) result
, "\t", 0);
1428 return send_error (ctx
, GPG_ERR_SYNTAX
);
1430 len
= strv_length (req
);
1431 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1432 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1435 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1438 if (has_content
|| !*req
[len
- 1])
1441 content
= req
[len
- 1];
1442 req
[len
- 1] = NULL
;
1446 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1447 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1449 rc
= new_root_element (client
, client
->doc
, *req
);
1453 return send_error (ctx
, rc
);
1462 return send_error (ctx
, rc
);
1467 if (req
[1] && *req
[1])
1470 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1472 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1473 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1479 rc
= is_element_owner (client
, parent
);
1482 n
= find_text_node (parent
->children
);
1484 xmlNodeSetContent (n
, (xmlChar
*) content
);
1486 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1488 update_element_mtime (client
, parent
);
1494 return send_error (ctx
, rc
);
1498 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1503 int progress
= config_get_integer ("global", "xfer_progress");
1507 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1508 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1509 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1517 if (sent
+ to_send
> total
)
1518 to_send
= total
- sent
;
1520 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1521 flush
? 0 : to_send
);
1524 sent
+= flush
? 0 : to_send
;
1526 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1527 (sent
== total
&& flush
))
1528 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1530 if (!flush
&& !rc
&& sent
== total
)
1537 while (!rc
&& sent
< total
);
1543 do_get (assuan_context_t ctx
, char *line
)
1545 struct client_s
*client
= assuan_get_pointer (ctx
);
1550 req
= str_split (line
, "\t", 0);
1555 return GPG_ERR_SYNTAX
;
1558 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1566 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1567 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1573 if (!n
|| !n
->children
)
1574 return GPG_ERR_NO_DATA
;
1576 n
= find_text_node (n
->children
);
1577 if (!n
|| !n
->content
|| !*n
->content
)
1578 return GPG_ERR_NO_DATA
;
1580 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1585 get_command (assuan_context_t ctx
, char *line
)
1587 struct client_s
*client
= assuan_get_pointer (ctx
);
1589 struct argv_s
*args
[] = {
1590 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1594 rc
= parse_options (&line
, args
, client
, 1);
1595 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1596 rc
= GPG_ERR_SYNTAX
;
1598 return send_error (ctx
, rc
);
1600 if (client
->opts
& OPT_INQUIRE
)
1602 unsigned char *result
;
1605 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1607 return send_error (ctx
, rc
);
1609 pthread_cleanup_push (xfree
, result
);
1610 rc
= do_get (ctx
, (char *)result
);
1611 pthread_cleanup_pop (1);
1614 rc
= do_get (ctx
, line
);
1616 return send_error (ctx
, rc
);
1619 static void list_command_cleanup1 (void *arg
);
1621 realpath_command (assuan_context_t ctx
, char *line
)
1623 struct string_s
*string
= NULL
;
1625 struct client_s
*client
= assuan_get_pointer (ctx
);
1626 struct argv_s
*args
[] = {
1627 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1631 rc
= parse_options (&line
, args
, client
, 1);
1632 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1633 rc
= GPG_ERR_SYNTAX
;
1635 return send_error (ctx
, rc
);
1637 if (client
->opts
& OPT_INQUIRE
)
1639 unsigned char *result
;
1642 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1644 return send_error (ctx
, rc
);
1646 pthread_cleanup_push (xfree
, result
);
1647 rc
= build_realpath (client
, client
->doc
, (char *)result
, &string
);
1648 pthread_cleanup_pop (1);
1651 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1655 pthread_cleanup_push (list_command_cleanup1
, string
);
1656 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1657 pthread_cleanup_pop (1);
1660 return send_error (ctx
, rc
);
1664 list_command_cleanup1 (void *arg
)
1667 string_free ((struct string_s
*) arg
, 1);
1671 list_command_cleanup2 (void *arg
)
1673 struct element_list_s
*elements
= arg
;
1679 int total
= slist_length (elements
->list
);
1682 for (i
= 0; i
< total
; i
++)
1684 char *tmp
= slist_nth_data (elements
->list
, i
);
1688 slist_free (elements
->list
);
1691 if (elements
->prefix
)
1692 xfree (elements
->prefix
);
1695 strv_free (elements
->req
);
1702 parse_list_opt_norecurse (void *data
, void *value
)
1704 struct client_s
*client
= data
;
1706 client
->opts
&= ~(OPT_LIST_RECURSE
);
1711 parse_opt_verbose (void *data
, void *value
)
1713 struct client_s
*client
= data
;
1715 client
->opts
|= OPT_VERBOSE
;
1720 parse_list_opt_target (void *data
, void *value
)
1722 struct client_s
*client
= data
;
1724 client
->opts
|= OPT_LIST_WITH_TARGET
;
1729 parse_list_opt_all (void *data
, void *value
)
1731 struct client_s
*client
= data
;
1733 client
->opts
|= OPT_LIST_ALL
;
1738 list_path_once (struct client_s
*client
, char *line
,
1739 struct element_list_s
*elements
, struct string_s
*result
)
1743 elements
->req
= str_split (line
, " ", 0);
1745 strv_printf (&elements
->req
, "%s", line
);
1747 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1748 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1753 int total
= slist_length (elements
->list
);
1759 for (i
= 0; i
< total
; i
++)
1761 char *tmp
= slist_nth_data (elements
->list
, i
);
1763 string_append_printf (result
, "%s%s", tmp
,
1764 i
+ 1 == total
? "" : "\n");
1768 rc
= GPG_ERR_NO_DATA
;
1775 has_list_flag (char *path
, char *flags
)
1779 while (*p
&& *++p
!= ' ');
1788 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1799 do_list (assuan_context_t ctx
, char *line
)
1801 struct client_s
*client
= assuan_get_pointer (ctx
);
1803 struct element_list_s
*elements
= NULL
;
1805 elements
= xcalloc (1, sizeof (struct element_list_s
));
1807 return GPG_ERR_ENOMEM
;
1809 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1811 (client
->opts
& OPT_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1812 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1814 if (!line
|| !*line
)
1816 struct string_s
*str
= NULL
;
1818 pthread_cleanup_push (list_command_cleanup2
, elements
);
1819 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1820 elements
->with_target
);
1821 pthread_cleanup_pop (1);
1822 pthread_cleanup_push (list_command_cleanup1
, str
);
1826 if (client
->opts
& OPT_LIST_ALL
)
1828 char **roots
= str_split (str
->str
, "\n", 0);
1831 pthread_cleanup_push (req_cleanup
, roots
);
1832 string_truncate (str
, 0);
1834 for (p
= roots
; *p
; p
++)
1836 if (strchr (*p
, ' '))
1838 if (has_list_flag (*p
, "EOP"))
1840 string_append_printf (str
, "%s%s", *p
,
1841 *(p
+ 1) ? "\n" : "");
1846 elements
= xcalloc (1, sizeof (struct element_list_s
));
1849 rc
= GPG_ERR_ENOMEM
;
1853 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1855 (client
->opts
& OPT_VERBOSE
) | (client
->opts
&
1856 OPT_LIST_WITH_TARGET
);
1857 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1858 pthread_cleanup_push (list_command_cleanup2
, elements
);
1859 rc
= list_path_once (client
, *p
, elements
, str
);
1860 pthread_cleanup_pop (1);
1865 string_append (str
, "\n");
1868 pthread_cleanup_pop (1);
1872 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1875 pthread_cleanup_pop (1);
1879 pthread_cleanup_push (list_command_cleanup2
, elements
);
1880 struct string_s
*str
= string_new (NULL
);
1881 pthread_cleanup_push (list_command_cleanup1
, str
);
1882 rc
= list_path_once (client
, line
, elements
, str
);
1884 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1886 pthread_cleanup_pop (1);
1887 pthread_cleanup_pop (1);
1892 list_command (assuan_context_t ctx
, char *line
)
1894 struct client_s
*client
= assuan_get_pointer (ctx
);
1896 struct argv_s
*args
[] = {
1897 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1898 parse_list_opt_norecurse
},
1899 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
1900 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1901 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1902 parse_list_opt_target
},
1903 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1907 if (disable_list_and_dump
== 1)
1908 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1910 client
->opts
|= OPT_LIST_RECURSE
;
1911 rc
= parse_options (&line
, args
, client
, 1);
1912 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1913 rc
= GPG_ERR_SYNTAX
;
1915 return send_error (ctx
, rc
);
1917 if (client
->opts
& OPT_LIST_ALL
)
1918 client
->opts
|= OPT_LIST_RECURSE
| OPT_VERBOSE
;
1920 if (client
->opts
& OPT_INQUIRE
)
1922 unsigned char *result
;
1925 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1927 return send_error (ctx
, rc
);
1929 pthread_cleanup_push (xfree
, result
);
1930 rc
= do_list (ctx
, (char *)result
);
1931 pthread_cleanup_pop (1);
1934 rc
= do_list (ctx
, line
);
1936 return send_error (ctx
, rc
);
1940 * req[0] - element path
1943 attribute_list (assuan_context_t ctx
, char **req
)
1945 struct client_s
*client
= assuan_get_pointer (ctx
);
1946 char **attrlist
= NULL
;
1954 if (!req
|| !req
[0])
1955 return GPG_ERR_SYNTAX
;
1957 client
->flags
|= FLAG_ACL_IGNORE
;
1959 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1962 * The first argument may be only a root element.
1964 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1965 return GPG_ERR_SYNTAX
;
1968 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1975 if (client
->flags
& FLAG_ACL_ERROR
)
1977 client
->flags
&= ~FLAG_ACL_IGNORE
;
1981 return GPG_ERR_EACCES
;
1984 client
->flags
&= ~FLAG_ACL_ERROR
;
1989 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1990 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2000 for (a
= n
->properties
; a
; a
= a
->next
)
2004 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
2007 strv_free (attrlist
);
2009 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2010 pwmd_strerror (GPG_ERR_ENOMEM
));
2011 return GPG_ERR_ENOMEM
;
2016 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
2018 && an
->content
? (char *) an
->content
: "");
2022 strv_free (attrlist
);
2023 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2024 pwmd_strerror (GPG_ERR_ENOMEM
));
2025 return GPG_ERR_ENOMEM
;
2028 attrlist
[++i
] = NULL
;
2032 return GPG_ERR_NO_DATA
;
2034 line
= strv_join ("\n", attrlist
);
2038 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2039 pwmd_strerror (GPG_ERR_ENOMEM
));
2040 strv_free (attrlist
);
2041 return GPG_ERR_ENOMEM
;
2044 pthread_cleanup_push (xfree
, line
);
2045 pthread_cleanup_push (req_cleanup
, attrlist
);
2046 rc
= xfer_data (ctx
, line
, strlen (line
));
2047 pthread_cleanup_pop (1);
2048 pthread_cleanup_pop (1);
2053 * req[0] - attribute
2054 * req[1] - element path
2057 attribute_delete (struct client_s
*client
, char **req
)
2063 if (!req
|| !req
[0] || !req
[1])
2064 return GPG_ERR_SYNTAX
;
2066 if (!strcmp (req
[0], "_name"))
2067 return GPG_ERR_INV_ATTR
;
2069 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2072 * The first argument may be only a root element.
2074 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2075 return GPG_ERR_SYNTAX
;
2078 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2084 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2085 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2090 if (!strcmp (req
[0], (char *) "_acl"))
2092 rc
= is_element_owner (client
, n
);
2097 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
2105 create_element_path (struct client_s
*client
,
2106 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
2108 char **req
= *elements
;
2109 char **req_orig
= strv_dup (req
);
2110 xmlNodePtr n
= NULL
;
2116 *rc
= GPG_ERR_ENOMEM
;
2117 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2118 pwmd_strerror (GPG_ERR_ENOMEM
));
2123 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2126 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2129 *rc
= new_root_element (client
, client
->doc
, req
[0]);
2135 else if (n
== parent
)
2137 *rc
= GPG_ERR_CONFLICT
;
2144 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
2146 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2147 NULL
, NULL
, create_target_elements_cb
, 0, 0,
2154 * Reset the position of the element tree now that the elements
2155 * have been created.
2160 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2164 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2165 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2172 strv_free (req_orig
);
2179 * Creates a "target" attribute. When other commands encounter an element with
2180 * this attribute, the element path is modified to the target value. If the
2181 * source element path doesn't exist when using 'ATTR SET target', it is
2182 * created, but the destination element path must exist.
2184 * req[0] - source element path
2185 * req[1] - destination element path
2188 target_attribute (struct client_s
*client
, char **req
)
2190 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
2194 if (!req
|| !req
[0] || !req
[1])
2195 return GPG_ERR_SYNTAX
;
2197 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
2200 * The first argument may be only a root element.
2202 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
2203 return GPG_ERR_SYNTAX
;
2206 if (!valid_element_path (src
, 0))
2207 return GPG_ERR_INV_VALUE
;
2209 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
2212 * The first argument may be only a root element.
2214 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
2216 rc
= GPG_ERR_SYNTAX
;
2221 odst
= strv_dup (dst
);
2224 rc
= GPG_ERR_ENOMEM
;
2228 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2230 * Make sure the destination element path exists.
2232 if (rc
&& rc
!= GPG_ERR_EACCES
)
2238 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
2239 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2240 if (rc
&& rc
!= GPG_ERR_EACCES
)
2244 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
2245 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2248 n
= create_element_path (client
, &src
, &rc
, NULL
);
2252 line
= strv_join ("\t", odst
);
2255 rc
= GPG_ERR_ENOMEM
;
2259 rc
= add_attribute (client
, n
, "target", line
);
2270 * req[0] - attribute
2271 * req[1] - element path
2274 attribute_get (assuan_context_t ctx
, char **req
)
2276 struct client_s
*client
= assuan_get_pointer (ctx
);
2282 if (!req
|| !req
[0] || !req
[1])
2283 return GPG_ERR_SYNTAX
;
2285 if (strchr (req
[1], '\t'))
2287 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2288 return GPG_ERR_SYNTAX
;
2292 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2293 return GPG_ERR_SYNTAX
;
2296 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2302 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2303 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2310 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2311 return GPG_ERR_NOT_FOUND
;
2313 pthread_cleanup_push (xmlFree
, a
);
2316 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2318 rc
= GPG_ERR_NO_DATA
;
2320 pthread_cleanup_pop (1);
2329 * req[0] - attribute
2330 * req[1] - element path
2334 attribute_set (struct client_s
*client
, char **req
)
2340 if (!req
|| !req
[0] || !req
[1])
2341 return GPG_ERR_SYNTAX
;
2344 * Reserved attribute names.
2346 if (!strcmp (req
[0], "_name"))
2347 return GPG_ERR_INV_ATTR
;
2348 else if (!strcmp (req
[0], "target"))
2349 return target_attribute (client
, req
+ 1);
2350 else if (!valid_xml_attribute (req
[0]))
2351 return GPG_ERR_INV_VALUE
;
2353 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2356 * The first argument may be only a root element.
2358 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2359 return GPG_ERR_SYNTAX
;
2362 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2368 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2369 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2374 if (!strcmp (req
[0], (char *) "_acl"))
2376 rc
= is_element_owner (client
, n
);
2381 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2390 * req[1] - attribute name or element path if command is LIST
2391 * req[2] - element path
2392 * req[2] - element path or value
2396 do_attr (assuan_context_t ctx
, char *line
)
2398 struct client_s
*client
= assuan_get_pointer (ctx
);
2402 req
= str_split (line
, " ", 4);
2403 if (!req
|| !req
[0] || !req
[1])
2406 return GPG_ERR_SYNTAX
;
2409 pthread_cleanup_push (req_cleanup
, req
);
2411 if (strcasecmp (req
[0], "SET") == 0)
2412 rc
= attribute_set (client
, req
+ 1);
2413 else if (strcasecmp (req
[0], "GET") == 0)
2414 rc
= attribute_get (ctx
, req
+ 1);
2415 else if (strcasecmp (req
[0], "DELETE") == 0)
2416 rc
= attribute_delete (client
, req
+ 1);
2417 else if (strcasecmp (req
[0], "LIST") == 0)
2418 rc
= attribute_list (ctx
, req
+ 1);
2420 rc
= GPG_ERR_SYNTAX
;
2422 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2423 pthread_cleanup_pop (1);
2428 attr_command (assuan_context_t ctx
, char *line
)
2430 struct client_s
*client
= assuan_get_pointer (ctx
);
2432 struct argv_s
*args
[] = {
2433 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2437 rc
= parse_options (&line
, args
, client
, 1);
2438 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2439 rc
= GPG_ERR_SYNTAX
;
2441 return send_error (ctx
, rc
);
2443 if (client
->opts
& OPT_INQUIRE
)
2445 unsigned char *result
;
2448 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2450 return send_error (ctx
, rc
);
2452 pthread_cleanup_push (xfree
, result
);
2453 rc
= do_attr (ctx
, (char *)result
);
2454 pthread_cleanup_pop (1);
2457 rc
= do_attr (ctx
, line
);
2459 return send_error (ctx
, rc
);
2463 parse_iscached_opt_lock (void *data
, void *value
)
2465 struct client_s
*client
= data
;
2468 client
->opts
|= OPT_LOCK
;
2473 iscached_command (assuan_context_t ctx
, char *line
)
2475 struct client_s
*client
= assuan_get_pointer (ctx
);
2477 struct argv_s
*args
[] = {
2478 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2482 if (!line
|| !*line
)
2483 return send_error (ctx
, GPG_ERR_SYNTAX
);
2485 rc
= parse_options (&line
, args
, client
, 1);
2487 return send_error (ctx
, rc
);
2488 else if (!valid_filename (line
))
2489 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2491 rc
= cache_iscached (line
, NULL
);
2492 if (client
->opts
& OPT_LOCK
2493 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2495 unsigned char md5file
[16];
2496 gpg_error_t trc
= rc
;
2498 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2499 if (memcmp (md5file
, client
->md5file
, 16))
2500 cleanup_client (client
);
2502 memcpy (client
->md5file
, md5file
, 16);
2503 rc
= do_lock (client
, 1);
2508 return send_error (ctx
, rc
);
2512 clearcache_command (assuan_context_t ctx
, char *line
)
2514 gpg_error_t rc
= 0, all_rc
= 0;
2515 unsigned char md5file
[16];
2519 struct client_thread_s
*once
= NULL
;
2522 MUTEX_LOCK (&cn_mutex
);
2523 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2525 if (!line
|| !*line
)
2528 t
= slist_length (cn_thread_list
);
2530 for (i
= 0; i
< t
; i
++)
2532 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2533 assuan_peercred_t peer
;
2538 /* Lock each connected clients' file mutex to prevent any other client
2539 * from accessing the cache entry (the file mutex is locked upon
2540 * command startup). The cache for the entry is not cleared if the
2541 * file mutex is locked by another client to prevent this function
2546 if (thd
->cl
->filename
)
2548 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2549 all_rc
= !all_rc
? rc
: all_rc
;
2557 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2558 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2560 if (pthread_equal (pthread_self (), thd
->tid
))
2564 if (!thd
->cl
->filename
||
2565 cache_iscached (thd
->cl
->filename
,
2566 NULL
) == GPG_ERR_NO_DATA
)
2572 cache_defer_clear (thd
->cl
->md5file
);
2575 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2583 rc
= cache_clear (thd
->cl
->md5file
);
2584 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2592 /* A single data filename was specified. Lock only this data file
2593 * mutex and free the cache entry. */
2596 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2597 rc
= do_validate_peer (ctx
, line
, &peer
);
2599 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2601 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2603 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2605 if (pthread_equal (pthread_self (), thd
->tid
))
2612 rc
= cache_clear (thd
->cl
->md5file
);
2613 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2617 cache_defer_clear (thd
->cl
->md5file
);
2625 /* Only connected clients' cache entries have been cleared. Now clear any
2626 * remaining cache entries without clients but only if there wasn't an
2627 * error from above since this would defeat the locking check of the
2628 * remaining entries. */
2634 /* No clients are using the specified file. */
2635 else if (!all_rc
&& !rc
&& !once
)
2637 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2638 rc
= cache_clear (md5file
);
2641 /* Release the connection mutex. */
2642 pthread_cleanup_pop (1);
2646 send_status_all (STATUS_CACHE
, NULL
);
2648 /* One or more files were locked while clearing all cache entries. */
2652 return send_error (ctx
, rc
);
2656 cachetimeout_command (assuan_context_t ctx
, char *line
)
2659 char **req
= str_split (line
, " ", 0);
2662 assuan_peercred_t peer
;
2664 if (!req
|| !*req
|| !req
[1])
2667 return send_error (ctx
, GPG_ERR_SYNTAX
);
2671 timeout
= (int) strtol (req
[1], &p
, 10);
2672 if (errno
!= 0 || *p
|| timeout
< -1)
2675 return send_error (ctx
, GPG_ERR_SYNTAX
);
2678 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2681 unsigned char md5file
[16];
2683 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2684 rc
= cache_set_timeout (md5file
, timeout
);
2685 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2688 MUTEX_LOCK (&rcfile_mutex
);
2689 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2691 MUTEX_UNLOCK (&rcfile_mutex
);
2696 return send_error (ctx
, rc
);
2700 dump_command (assuan_context_t ctx
, char *line
)
2704 struct client_s
*client
= assuan_get_pointer (ctx
);
2707 if (disable_list_and_dump
== 1)
2708 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2711 return send_error (ctx
, GPG_ERR_SYNTAX
);
2713 rc
= peer_is_invoker(client
);
2715 return send_error (ctx
, rc
);
2717 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2721 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2722 pwmd_strerror (GPG_ERR_ENOMEM
));
2723 return send_error (ctx
, GPG_ERR_ENOMEM
);
2726 pthread_cleanup_push (xmlFree
, xml
);
2727 rc
= xfer_data (ctx
, (char *) xml
, len
);
2728 pthread_cleanup_pop (1);
2729 return send_error (ctx
, rc
);
2733 getconfig_command (assuan_context_t ctx
, char *line
)
2735 struct client_s
*client
= assuan_get_pointer (ctx
);
2737 char filename
[255] = { 0 }, param
[747] = { 0 };
2738 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2740 if (!line
|| !*line
)
2741 return send_error (ctx
, GPG_ERR_SYNTAX
);
2743 if (strchr (line
, ' '))
2745 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2750 if (fp
&& !valid_filename (fp
))
2751 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2753 paramp
= str_down (paramp
);
2754 if (!strcmp (paramp
, "cipher") && fp
)
2756 struct crypto_s
*crypto
= NULL
;
2758 rc
= init_client_crypto (&crypto
);
2761 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2765 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2775 UPDATE_AGENT_CTX (client
, crypto
);
2776 cleanup_crypto (&crypto
);
2777 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2778 return send_error (ctx
, rc
);
2783 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2785 struct crypto_s
*crypto
= NULL
;
2787 rc
= init_client_crypto (&crypto
);
2790 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2793 tmp
= str_asprintf ("%llu",
2794 (unsigned long long) crypto
->hdr
.s2k_count
);
2796 rc
= GPG_ERR_ENOMEM
;
2800 UPDATE_AGENT_CTX (client
, crypto
);
2801 cleanup_crypto (&crypto
);
2802 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2803 return send_error (ctx
, rc
);
2808 else if (!strcmp (paramp
, "passphrase"))
2809 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2811 p
= config_get_value (fp
? fp
: "global", paramp
);
2813 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2815 tmp
= expand_homedir (p
);
2819 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2820 pwmd_strerror (GPG_ERR_ENOMEM
));
2821 return send_error (ctx
, GPG_ERR_ENOMEM
);
2826 pthread_cleanup_push (xfree
, p
);
2827 rc
= xfer_data (ctx
, p
, strlen (p
));
2828 pthread_cleanup_pop (1);
2829 return send_error (ctx
, rc
);
2834 xmlXPathContextPtr xp
;
2835 xmlXPathObjectPtr result
;
2841 xpath_command_cleanup (void *arg
)
2843 struct xpath_s
*xpath
= arg
;
2848 req_cleanup (xpath
->req
);
2851 xmlBufferFree (xpath
->buf
);
2854 xmlXPathFreeObject (xpath
->result
);
2857 xmlXPathFreeContext (xpath
->xp
);
2861 do_xpath (assuan_context_t ctx
, char *line
)
2864 struct client_s
*client
= assuan_get_pointer (ctx
);
2865 struct xpath_s _x
= { 0 };
2866 struct xpath_s
*xpath
= &_x
;
2868 if (!line
|| !*line
)
2869 return GPG_ERR_SYNTAX
;
2871 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2873 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2874 return GPG_ERR_ENOMEM
;
2877 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2880 rc
= GPG_ERR_BAD_DATA
;
2885 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2888 rc
= GPG_ERR_BAD_DATA
;
2892 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2894 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2898 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2899 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2903 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2905 rc
= GPG_ERR_NO_DATA
;
2908 else if (xpath
->req
[1])
2914 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2915 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2916 xmlBufferLength (xpath
->buf
));
2917 pthread_cleanup_pop (0);
2919 xpath_command_cleanup (xpath
);
2924 xpath_command (assuan_context_t ctx
, char *line
)
2926 struct client_s
*client
= assuan_get_pointer (ctx
);
2928 struct argv_s
*args
[] = {
2929 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2933 if (disable_list_and_dump
== 1)
2934 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2936 rc
= peer_is_invoker(client
);
2938 return send_error (ctx
, rc
);
2940 rc
= parse_options (&line
, args
, client
, 1);
2941 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2942 rc
= GPG_ERR_SYNTAX
;
2944 return send_error (ctx
, rc
);
2946 if (client
->opts
& OPT_INQUIRE
)
2948 unsigned char *result
;
2951 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2953 return send_error (ctx
, rc
);
2955 pthread_cleanup_push (xfree
, result
);
2956 rc
= do_xpath (ctx
, (char *)result
);
2957 pthread_cleanup_pop (1);
2960 rc
= do_xpath (ctx
, line
);
2962 return send_error (ctx
, rc
);
2966 do_xpathattr (assuan_context_t ctx
, char *line
)
2968 struct client_s
*client
= assuan_get_pointer (ctx
);
2972 struct xpath_s _x
= { 0 };
2973 struct xpath_s
*xpath
= &_x
;
2975 if (!line
|| !*line
)
2976 return GPG_ERR_SYNTAX
;
2978 if ((req
= str_split (line
, " ", 3)) == NULL
)
2979 return GPG_ERR_ENOMEM
;
2983 rc
= GPG_ERR_SYNTAX
;
2987 if (!strcasecmp (req
[0], "SET"))
2989 else if (!strcasecmp (req
[0], "DELETE"))
2993 rc
= GPG_ERR_SYNTAX
;
2997 if (!req
[1] || !req
[2])
2999 rc
= GPG_ERR_SYNTAX
;
3003 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
3005 rc
= GPG_ERR_ENOMEM
;
3009 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
3011 rc
= GPG_ERR_SYNTAX
;
3015 xpath
->xp
= xmlXPathNewContext (client
->doc
);
3018 rc
= GPG_ERR_BAD_DATA
;
3022 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
3025 rc
= GPG_ERR_BAD_DATA
;
3029 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
3031 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
3035 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
3036 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
3037 (xmlChar
*) req
[1]);
3040 xpath_command_cleanup (xpath
);
3045 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
3047 xpathattr_command (assuan_context_t ctx
, char *line
)
3049 struct client_s
*client
= assuan_get_pointer (ctx
);
3051 struct argv_s
*args
[] = {
3052 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3056 if (disable_list_and_dump
== 1)
3057 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
3059 rc
= peer_is_invoker(client
);
3061 return send_error (ctx
, rc
);
3063 rc
= parse_options (&line
, args
, client
, 1);
3064 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3065 rc
= GPG_ERR_SYNTAX
;
3067 return send_error (ctx
, rc
);
3069 if (client
->opts
& OPT_INQUIRE
)
3071 unsigned char *result
;
3074 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3076 return send_error (ctx
, rc
);
3078 pthread_cleanup_push (xfree
, result
);
3079 rc
= do_xpathattr (ctx
, (char *)result
);
3080 pthread_cleanup_pop (1);
3083 rc
= do_xpathattr (ctx
, line
);
3085 return send_error (ctx
, rc
);
3089 do_import (struct client_s
*client
, const char *root_element
,
3090 unsigned char *content
)
3092 char **dst_path
= NULL
;
3093 xmlDocPtr doc
= NULL
;
3094 xmlNodePtr n
, root
, copy
;
3097 if (!content
|| !*content
)
3100 return GPG_ERR_SYNTAX
;
3104 dst_path
= str_split (root_element
, "\t", 0);
3106 if (dst_path
&& !valid_element_path (dst_path
, 0))
3109 strv_free (dst_path
);
3111 return GPG_ERR_INV_VALUE
;
3114 struct string_s
*str
= string_new_content ((char *)content
);
3115 str
= string_prepend (str
, "<pwmd>");
3116 str
= string_append (str
, "</pwmd>");
3117 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
3118 string_free (str
, 1);
3121 rc
= GPG_ERR_BAD_DATA
;
3125 root
= xmlDocGetRootElement (doc
);
3126 xmlNodePtr root_orig
= root
->children
;
3127 root
= root
->children
;
3128 rc
= validate_import (client
, root
);
3137 char **path
= strv_dup (dst_path
);
3140 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
3141 pwmd_strerror (GPG_ERR_ENOMEM
));
3142 rc
= GPG_ERR_ENOMEM
;
3146 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
3150 rc
= GPG_ERR_INV_VALUE
;
3154 if (strv_printf (&path
, "%s", (char *) a
) == 0)
3157 rc
= GPG_ERR_ENOMEM
;
3162 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
3163 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3171 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
3172 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
3173 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3188 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
3190 n
= create_element_path (client
, &path
, &rc
, NULL
);
3197 copy
= xmlCopyNodeList (root
->children
);
3198 n
= xmlAddChildList (n
, copy
);
3200 rc
= GPG_ERR_ENOMEM
;
3209 /* Check if the content root element can create a DTD root element. */
3210 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
3212 rc
= GPG_ERR_SYNTAX
;
3218 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
3220 rc
= GPG_ERR_SYNTAX
;
3224 char *tmp
= str_dup ((char *) a
);
3226 int literal
= is_literal_element (&tmp
);
3228 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
3231 rc
= GPG_ERR_INV_VALUE
;
3235 if (strv_printf (&path
, "%s", tmp
) == 0)
3238 rc
= GPG_ERR_ENOMEM
;
3243 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
3244 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3246 rc
= GPG_ERR_BAD_DATA
;
3250 /* Overwriting the existing tree. */
3254 xmlFreeNodeList (n
);
3258 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
3259 n
= xmlCopyNode (root
, 1);
3260 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3265 rc
= update_element_mtime (client
, n
->parent
);
3267 for (root
= root_orig
->next
; root
; root
= root
->next
)
3269 if (root
->type
== XML_ELEMENT_NODE
)
3282 strv_free (dst_path
);
3288 parse_import_opt_root (void *data
, void *value
)
3290 struct client_s
*client
= data
;
3292 client
->import_root
= str_dup (value
);
3293 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3297 import_command (assuan_context_t ctx
, char *line
)
3300 struct client_s
*client
= assuan_get_pointer (ctx
);
3301 unsigned char *result
;
3303 struct argv_s
*args
[] = {
3304 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3308 xfree (client
->import_root
);
3309 client
->import_root
= NULL
;
3310 rc
= parse_options (&line
, args
, client
, 0);
3312 return send_error (ctx
, rc
);
3314 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3317 xfree (client
->import_root
);
3318 client
->import_root
= NULL
;
3319 return send_error (ctx
, rc
);
3322 rc
= do_import (client
, client
->import_root
, result
);
3323 xfree (client
->import_root
);
3324 client
->import_root
= NULL
;
3325 return send_error (ctx
, rc
);
3329 do_lock (struct client_s
*client
, int add
)
3331 gpg_error_t rc
= lock_file_mutex (client
, add
);
3334 client
->flags
|= FLAG_LOCK_CMD
;
3340 lock_command (assuan_context_t ctx
, char *line
)
3342 struct client_s
*client
= assuan_get_pointer (ctx
);
3346 return send_error (ctx
, GPG_ERR_SYNTAX
);
3348 rc
= do_lock (client
, 0);
3349 return send_error (ctx
, rc
);
3353 unlock_command (assuan_context_t ctx
, char *line
)
3355 struct client_s
*client
= assuan_get_pointer (ctx
);
3359 return send_error (ctx
, GPG_ERR_SYNTAX
);
3361 rc
= unlock_file_mutex (client
, 0);
3362 return send_error (ctx
, rc
);
3366 option_command (assuan_context_t ctx
, char *line
)
3368 struct client_s
*client
= assuan_get_pointer (ctx
);
3370 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3372 struct agent_s
*agent
= client
->crypto
->agent
;
3374 char namebuf
[255] = { 0 };
3375 char *name
= namebuf
;
3376 char *value
= NULL
, *p
, *tmp
= NULL
;
3378 p
= strchr (line
, '=');
3381 strncpy (namebuf
, line
, sizeof(namebuf
));
3382 namebuf
[sizeof(namebuf
)-1] = 0;
3386 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3387 namebuf
[sizeof(namebuf
)-1] = 0;
3391 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3393 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3399 n
= strtol (value
, &tmp
, 10);
3401 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3404 client
->lock_timeout
= n
;
3407 else if (strcasecmp (name
, (char *) "NAME") == 0)
3409 if (value
&& strchr (value
, ' '))
3410 rc
= GPG_ERR_INV_VALUE
;
3413 tmp
= pthread_getspecific (thread_name_key
);
3415 MUTEX_LOCK (&cn_mutex
);
3416 xfree (client
->thd
->name
);
3417 client
->thd
->name
= NULL
;
3419 if (!value
|| !*value
)
3420 pthread_setspecific (thread_name_key
, str_dup (""));
3423 client
->thd
->name
= str_dup (value
);
3424 pthread_setspecific (thread_name_key
, str_dup (value
));
3427 MUTEX_UNLOCK (&cn_mutex
);
3431 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3433 xfree (pin_opts
->lc_messages
);
3434 pin_opts
->lc_messages
= NULL
;
3435 if (value
&& *value
)
3436 pin_opts
->lc_messages
= str_dup (value
);
3439 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3442 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3444 xfree (pin_opts
->lc_ctype
);
3445 pin_opts
->lc_ctype
= NULL
;
3446 if (value
&& *value
)
3447 pin_opts
->lc_ctype
= str_dup (value
);
3450 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3453 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3455 xfree (pin_opts
->ttyname
);
3456 pin_opts
->ttyname
= NULL
;
3457 if (value
&& *value
)
3458 pin_opts
->ttyname
= str_dup (value
);
3461 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3464 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3466 xfree (pin_opts
->ttytype
);
3467 pin_opts
->ttytype
= NULL
;
3468 if (value
&& *value
)
3469 pin_opts
->ttytype
= str_dup (value
);
3472 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3475 else if (strcasecmp (name
, (char *) "display") == 0)
3477 xfree (pin_opts
->display
);
3478 pin_opts
->display
= NULL
;
3479 if (value
&& *value
)
3480 pin_opts
->display
= str_dup (value
);
3483 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3486 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3488 xfree (pin_opts
->desc
);
3489 pin_opts
->desc
= NULL
;
3490 if (value
&& *value
)
3491 pin_opts
->desc
= str_dup (value
);
3493 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3495 xfree (pin_opts
->title
);
3496 pin_opts
->title
= NULL
;
3497 if (value
&& *value
)
3498 pin_opts
->title
= str_dup (value
);
3500 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3502 xfree (pin_opts
->prompt
);
3503 pin_opts
->prompt
= NULL
;
3504 if (value
&& *value
)
3505 pin_opts
->prompt
= str_dup (value
);
3508 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3516 n
= (int) strtol (value
, &p
, 10);
3519 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3521 pin_opts
->timeout
= n
;
3522 MUTEX_LOCK (&rcfile_mutex
);
3523 config_set_int_param (&global_config
,
3524 client
->filename
? client
->filename
: "global",
3525 "pinentry_timeout", value
);
3526 MUTEX_UNLOCK (&rcfile_mutex
);
3529 else if (strcasecmp (name
, "disable-pinentry") == 0)
3533 if (value
&& *value
)
3535 n
= (int) strtol (value
, &tmp
, 10);
3536 if (*tmp
|| n
< 0 || n
> 1)
3537 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3541 client
->flags
|= FLAG_NO_PINENTRY
;
3543 client
->flags
&= ~FLAG_NO_PINENTRY
;
3548 if (client
->flags
& FLAG_NO_PINENTRY
)
3549 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3552 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3556 return send_error (ctx
, rc
);
3561 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3565 if (!rc
&& use_agent
&& agent
)
3567 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3572 return send_error (ctx
, rc
);
3576 do_rename (assuan_context_t ctx
, char *line
)
3578 struct client_s
*client
= assuan_get_pointer (ctx
);
3580 char **req
, **src
, *dst
;
3583 req
= str_split (line
, " ", 0);
3585 if (!req
|| !req
[0] || !req
[1])
3588 return GPG_ERR_SYNTAX
;
3592 is_literal_element (&dst
);
3594 if (!valid_xml_element ((xmlChar
*) dst
))
3597 return GPG_ERR_INV_VALUE
;
3600 if (strchr (req
[0], '\t'))
3601 src
= str_split (req
[0], "\t", 0);
3603 src
= str_split (req
[0], " ", 0);
3607 rc
= GPG_ERR_SYNTAX
;
3611 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3616 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3617 NULL
, 0, 0, NULL
, 0);
3622 rc
= is_element_owner (client
, n
);
3626 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3629 rc
= GPG_ERR_ENOMEM
;
3633 /* To prevent unwanted effects:
3635 * <root name="a"><b/></root>
3639 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3642 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3652 for (p
= src
; *p
; p
++)
3656 strv_printf (&tmp
, "%s", *p
);
3660 strv_printf (&tmp
, "!%s", dst
);
3661 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3662 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3669 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3670 NULL
, NULL
, 0, 0, NULL
, 0);
3673 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3678 /* Target may exist:
3681 * <root name="b" target="a"/>
3690 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3696 rc
= is_element_owner (client
, ndst
);
3700 unlink_node (client
, ndst
);
3701 xmlFreeNodeList (ndst
);
3704 rc
= add_attribute (client
, n
, "_name", dst
);
3713 rename_command (assuan_context_t ctx
, char *line
)
3715 struct client_s
*client
= assuan_get_pointer (ctx
);
3717 struct argv_s
*args
[] = {
3718 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3722 rc
= parse_options (&line
, args
, client
, 1);
3723 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3724 rc
= GPG_ERR_SYNTAX
;
3726 return send_error (ctx
, rc
);
3728 if (client
->opts
& OPT_INQUIRE
)
3730 unsigned char *result
;
3733 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3735 return send_error (ctx
, rc
);
3737 pthread_cleanup_push (xfree
, result
);
3738 rc
= do_rename (ctx
, (char *)result
);
3739 pthread_cleanup_pop (1);
3742 rc
= do_rename (ctx
, line
);
3744 return send_error (ctx
, rc
);
3748 do_copy (assuan_context_t ctx
, char *line
)
3750 struct client_s
*client
= assuan_get_pointer (ctx
);
3752 char **req
, **src
= NULL
, **dst
= NULL
;
3753 xmlNodePtr nsrc
, ndst
, new = NULL
;
3755 req
= str_split (line
, " ", 0);
3756 if (!req
|| !req
[0] || !req
[1])
3759 return GPG_ERR_SYNTAX
;
3762 if (strchr (req
[0], '\t'))
3763 src
= str_split (req
[0], "\t", 0);
3765 src
= str_split (req
[0], " ", 0);
3769 rc
= GPG_ERR_SYNTAX
;
3773 if (strchr (req
[1], '\t'))
3774 dst
= str_split (req
[1], "\t", 0);
3776 dst
= str_split (req
[1], " ", 0);
3780 rc
= GPG_ERR_SYNTAX
;
3784 if (!valid_element_path (dst
, 0))
3786 rc
= GPG_ERR_INV_VALUE
;
3790 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3795 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3796 NULL
, NULL
, 0, 0, NULL
, 0);
3801 new = xmlCopyNodeList (nsrc
);
3804 rc
= GPG_ERR_ENOMEM
;
3809 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3810 if (rc
== GPG_ERR_EACCES
)
3816 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3817 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3824 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3826 else if (!ndst
|| create
)
3828 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3833 rc
= is_element_owner (client
, ndst
);
3837 /* Merge any attributes from the src node to the initial dst node. */
3838 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3840 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3843 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3847 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3848 xmlNewProp (ndst
, attr
->name
, tmp
);
3850 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3853 xmlNodePtr n
= ndst
->children
;
3855 xmlFreeNodeList (n
);
3856 ndst
->children
= NULL
;
3860 n
= xmlCopyNodeList (new->children
);
3863 rc
= GPG_ERR_ENOMEM
;
3867 n
= xmlAddChildList (ndst
, n
);
3870 rc
= GPG_ERR_ENOMEM
;
3874 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3875 ndst
->parent
? ndst
: ndst
->parent
);
3881 xmlUnlinkNode (new);
3882 xmlFreeNodeList (new);
3898 copy_command (assuan_context_t ctx
, char *line
)
3900 struct client_s
*client
= assuan_get_pointer (ctx
);
3902 struct argv_s
*args
[] = {
3903 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3907 rc
= parse_options (&line
, args
, client
, 1);
3908 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3909 rc
= GPG_ERR_SYNTAX
;
3911 return send_error (ctx
, rc
);
3913 if (client
->opts
& OPT_INQUIRE
)
3915 unsigned char *result
;
3918 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3920 return send_error (ctx
, rc
);
3922 pthread_cleanup_push (xfree
, result
);
3923 rc
= do_copy (ctx
, (char *)result
);
3924 pthread_cleanup_pop (1);
3927 rc
= do_copy (ctx
, line
);
3929 return send_error (ctx
, rc
);
3933 do_move (assuan_context_t ctx
, char *line
)
3935 struct client_s
*client
= assuan_get_pointer (ctx
);
3937 char **req
, **src
= NULL
, **dst
= NULL
;
3938 xmlNodePtr nsrc
, ndst
= NULL
;
3940 req
= str_split (line
, " ", 0);
3942 if (!req
|| !req
[0] || !req
[1])
3945 return GPG_ERR_SYNTAX
;
3948 if (strchr (req
[0], '\t'))
3949 src
= str_split (req
[0], "\t", 0);
3951 src
= str_split (req
[0], " ", 0);
3955 rc
= GPG_ERR_SYNTAX
;
3959 if (strchr (req
[1], '\t'))
3960 dst
= str_split (req
[1], "\t", 0);
3962 dst
= str_split (req
[1], " ", 0);
3964 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3969 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3970 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3975 rc
= is_element_owner (client
, nsrc
);
3981 if (!valid_element_path (dst
, 0))
3983 rc
= GPG_ERR_INV_VALUE
;
3987 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3992 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3993 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3996 ndst
= xmlDocGetRootElement (client
->doc
);
3998 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
4002 rc
= GPG_ERR_CONFLICT
;
4007 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
4014 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
4016 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
4028 if (ndst
== xmlDocGetRootElement (client
->doc
))
4030 xmlNodePtr n
= nsrc
;
4033 while (n
->parent
&& n
->parent
!= ndst
)
4036 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
4037 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
4039 if (xmlStrEqual (a
, b
))
4042 xmlUnlinkNode (nsrc
);
4044 xmlFreeNodeList (n
);
4052 xmlUnlinkNode (dup
);
4053 xmlFreeNodeList (dup
);
4057 xmlUnlinkNode (dup
);
4063 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
4065 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
4066 && !strcmp ((char *) name
, *dst
))
4069 rc
= GPG_ERR_CONFLICT
;
4074 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
4082 update_element_mtime (client
, nsrc
->parent
);
4083 xmlUnlinkNode (nsrc
);
4084 ndst
= xmlAddChildList (ndst
, nsrc
);
4087 rc
= GPG_ERR_ENOMEM
;
4089 update_element_mtime (client
, ndst
->parent
);
4105 move_command (assuan_context_t ctx
, char *line
)
4107 struct client_s
*client
= assuan_get_pointer (ctx
);
4109 struct argv_s
*args
[] = {
4110 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
4114 rc
= parse_options (&line
, args
, client
, 1);
4115 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
4116 rc
= GPG_ERR_SYNTAX
;
4118 return send_error (ctx
, rc
);
4120 if (client
->opts
& OPT_INQUIRE
)
4122 unsigned char *result
;
4125 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
4127 return send_error (ctx
, rc
);
4129 pthread_cleanup_push (xfree
, result
);
4130 rc
= do_move (ctx
, (char *)result
);
4131 pthread_cleanup_pop (1);
4134 rc
= do_move (ctx
, line
);
4136 return send_error (ctx
, rc
);
4140 ls_command (assuan_context_t ctx
, char *line
)
4148 return send_error (ctx
, GPG_ERR_SYNTAX
);
4150 tmp
= str_asprintf ("%s/data", homedir
);
4151 dir
= expand_homedir (tmp
);
4154 rc
= gpg_error_from_errno (errno
);
4159 return send_error (ctx
, rc
);
4163 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
4164 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
4168 pthread_cleanup_push (xfree
, p
);
4169 pthread_cleanup_push ((void *)(void *)closedir
, d
);
4172 while (!readdir_r (d
, p
, &cur
) && cur
)
4174 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
4176 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
4177 && cur
->d_name
[2] == '\0')
4180 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
4187 rc
= GPG_ERR_ENOMEM
;
4195 pthread_cleanup_pop (1); // closedir (d)
4196 pthread_cleanup_pop (1); // xfree (p)
4199 return send_error (ctx
, rc
);
4202 return send_error (ctx
, GPG_ERR_NO_DATA
);
4204 list
[strlen (list
) - 1] = 0;
4205 pthread_cleanup_push (xfree
, list
);
4206 rc
= xfer_data (ctx
, list
, strlen (list
));
4207 pthread_cleanup_pop (1);
4208 return send_error (ctx
, rc
);
4212 bye_notify (assuan_context_t ctx
, char *line
)
4214 struct client_s
*cl
= assuan_get_pointer (ctx
);
4216 cl
->thd
->state
= CLIENT_STATE_DISCON
;
4219 if (cl
->thd
->remote
)
4225 struct timeval tv
= { 0, 50000 };
4227 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
4228 if (rc
== GNUTLS_E_AGAIN
)
4229 select (0, NULL
, NULL
, NULL
, &tv
);
4231 while (rc
== GNUTLS_E_AGAIN
);
4235 /* This will let assuan_process_next() return. */
4236 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
4238 cl
->last_rc
= gpg_error_from_errno (errno
);
4242 cl
->last_rc
= 0; // BYE command result
4247 reset_notify (assuan_context_t ctx
, char *line
)
4249 struct client_s
*client
= assuan_get_pointer (ctx
);
4252 cleanup_client (client
);
4258 * This is called before every Assuan command.
4261 command_startup (assuan_context_t ctx
, const char *name
)
4263 struct client_s
*client
= assuan_get_pointer (ctx
);
4265 struct command_table_s
*cmd
= NULL
;
4267 log_write1 ("command='%s'", name
);
4268 client
->last_rc
= client
->opts
= 0;
4270 for (int i
= 0; command_table
[i
]; i
++)
4272 if (!strcasecmp (name
, command_table
[i
]->name
))
4274 if (command_table
[i
]->ignore_startup
)
4276 cmd
= command_table
[i
];
4282 return GPG_ERR_UNKNOWN_COMMAND
;
4284 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4286 update_client_state (client
, CLIENT_STATE_COMMAND
);
4292 * This is called after every Assuan command.
4295 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4297 struct client_s
*client
= assuan_get_pointer (ctx
);
4299 if (!(client
->flags
& FLAG_LOCK_CMD
))
4300 unlock_file_mutex (client
, 0);
4302 unlock_flock (&client
->flock_fd
);
4303 log_write1 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4304 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
4306 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4308 update_client_state (client
, CLIENT_STATE_IDLE
);
4312 help_command (assuan_context_t ctx
, char *line
)
4317 if (!line
|| !*line
)
4320 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
4321 "For commands that take an element path as an argument, each element is "
4322 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4325 for (i
= 0; command_table
[i
]; i
++)
4327 if (!command_table
[i
]->help
)
4330 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4335 tmp
= strip_texi_and_wrap (help
);
4337 pthread_cleanup_push (xfree
, tmp
);
4338 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4339 pthread_cleanup_pop (1);
4340 return send_error (ctx
, rc
);
4343 for (i
= 0; command_table
[i
]; i
++)
4345 if (!strcasecmp (line
, command_table
[i
]->name
))
4349 if (!command_table
[i
]->help
)
4352 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4353 tmp
= str_asprintf (_("Usage: %s"), help
);
4355 pthread_cleanup_push (xfree
, tmp
);
4356 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4357 pthread_cleanup_pop (1);
4358 return send_error (ctx
, rc
);
4362 return send_error (ctx
, GPG_ERR_INV_NAME
);
4366 new_command (const char *name
, int ignore
, int unlock
, int flock_type
,
4367 gpg_error_t (*handler
) (assuan_context_t
, char *),
4373 for (i
= 0; command_table
[i
]; i
++);
4376 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4377 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4378 command_table
[i
]->name
= name
;
4379 command_table
[i
]->handler
= handler
;
4380 command_table
[i
]->ignore_startup
= ignore
;
4381 command_table
[i
]->unlock
= unlock
;
4382 command_table
[i
]->flock_type
= flock_type
;
4383 command_table
[i
++]->help
= help
;
4384 command_table
[i
] = NULL
;
4392 for (i
= 0; command_table
[i
]; i
++)
4393 xfree (command_table
[i
]);
4395 xfree (command_table
);
4399 sort_commands (const void *arg1
, const void *arg2
)
4401 struct command_table_s
*const *a
= arg1
;
4402 struct command_table_s
*const *b
= arg2
;
4411 return strcmp ((*a
)->name
, (*b
)->name
);
4415 passwd_command (assuan_context_t ctx
, char *line
)
4417 struct client_s
*client
= assuan_get_pointer (ctx
);
4419 struct argv_s
*args
[] = {
4420 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4421 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4422 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4426 rc
= peer_is_invoker (client
);
4427 if (rc
== GPG_ERR_EACCES
)
4428 return send_error (ctx
, rc
);
4430 if (client
->flags
& FLAG_NEW
)
4431 return send_error (ctx
, GPG_ERR_INV_STATE
);
4433 client
->crypto
->save
.hdr
.s2k_count
=
4434 config_get_ulonglong (client
->filename
, "s2k_count");
4435 rc
= parse_options (&line
, args
, client
, 0);
4437 return send_error (ctx
, rc
);
4439 if (!rc
&& client
->opts
& OPT_RESET
)
4441 rc
= cache_clear (client
->md5file
);
4443 send_status_all (STATUS_CACHE
, NULL
);
4448 if (!IS_PKI (client
->crypto
))
4450 struct crypto_s
*crypto
;
4452 xfree (client
->crypto
->filename
);
4453 client
->crypto
->filename
= str_dup (client
->filename
);
4454 rc
= change_passwd (ctx
, client
->filename
,
4455 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4456 (client
->opts
& OPT_NO_PASSPHRASE
));
4459 cleanup_crypto (&client
->crypto
);
4460 client
->crypto
= crypto
;
4461 update_checksum (client
);
4462 cleanup_crypto_stage1 (client
->crypto
);
4468 if (client
->crypto
->save
.hdr
.s2k_count
)
4469 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4470 "OPTION s2k-count=%lu",
4471 client
->crypto
->save
.hdr
.s2k_count
);
4474 rc
= agent_passwd (client
->crypto
);
4476 (void) kill_scd (client
->crypto
->agent
);
4481 return send_error (ctx
, rc
);
4485 parse_keygrip_opt_sign (void *data
, void *value
)
4487 struct client_s
*client
= data
;
4490 client
->opts
|= OPT_SIGN
;
4495 keygrip_command (assuan_context_t ctx
, char *line
)
4497 struct client_s
*client
= assuan_get_pointer (ctx
);
4499 struct crypto_s
*crypto
= NULL
;
4500 struct argv_s
*args
[] = {
4501 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4505 if (!line
|| !*line
)
4506 return send_error (ctx
, GPG_ERR_SYNTAX
);
4508 rc
= parse_options (&line
, args
, client
, 1);
4510 return send_error (ctx
, rc
);
4512 if (!valid_filename (line
))
4513 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4515 rc
= init_client_crypto (&crypto
);
4517 return send_error (ctx
, rc
);
4519 rc
= read_data_file (line
, crypto
);
4522 char *hexgrip
= NULL
;
4524 if (!IS_PKI (crypto
))
4526 cleanup_crypto (&crypto
);
4527 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4530 if (client
->opts
& OPT_SIGN
)
4532 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4533 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4537 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4540 rc
= GPG_ERR_ENOMEM
;
4542 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4547 UPDATE_AGENT_CTX (client
, crypto
);
4548 cleanup_crypto (&crypto
);
4549 return send_error (ctx
, rc
);
4553 parse_opt_data (void *data
, void *value
)
4555 struct client_s
*client
= data
;
4558 client
->opts
|= OPT_DATA
;
4563 send_client_list (assuan_context_t ctx
)
4565 struct client_s
*client
= assuan_get_pointer (ctx
);
4567 char buf
[ASSUAN_LINELENGTH
];
4569 if (client
->opts
& OPT_VERBOSE
)
4575 MUTEX_LOCK (&cn_mutex
);
4576 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4577 t
= slist_length (cn_thread_list
);
4579 for (i
= 0; i
< t
; i
++)
4581 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4584 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4587 tmp
= build_client_info_line (thd
, 0);
4590 char **l
= strv_cat (list
, tmp
);
4592 rc
= GPG_ERR_ENOMEM
;
4597 rc
= GPG_ERR_ENOMEM
;
4606 pthread_cleanup_pop (1);
4610 line
= strv_join ("\n", list
);
4612 pthread_cleanup_push (xfree
, line
);
4613 rc
= xfer_data (ctx
, line
, strlen (line
));
4614 pthread_cleanup_pop (1);
4618 if (client
->opts
& OPT_DATA
)
4620 MUTEX_LOCK (&cn_mutex
);
4621 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4622 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4623 pthread_cleanup_pop (1);
4624 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4627 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4633 getinfo_command (assuan_context_t ctx
, char *line
)
4635 struct client_s
*client
= assuan_get_pointer (ctx
);
4637 char buf
[ASSUAN_LINELENGTH
];
4638 struct argv_s
*args
[] = {
4639 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4640 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4644 rc
= parse_options (&line
, args
, client
, 1);
4646 return send_error (ctx
, rc
);
4648 if (!strcasecmp (line
, "clients"))
4650 rc
= send_client_list (ctx
);
4652 else if (!strcasecmp (line
, "cache"))
4654 if (client
->opts
& OPT_DATA
)
4656 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4657 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4660 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4662 else if (!strcasecmp (line
, "pid"))
4665 pid_t pid
= getpid ();
4667 snprintf (buf
, sizeof (buf
), "%i", pid
);
4668 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4670 else if (!strcasecmp (line
, "version"))
4672 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4682 "", use_agent
? "AGENT" : "");
4683 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4686 else if (!strcasecmp (line
, "last_error"))
4688 if (client
->last_error
)
4689 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4691 rc
= GPG_ERR_NO_DATA
;
4693 else if (!strcasecmp (line
, "user"))
4698 if (client
->thd
->remote
)
4699 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4701 user
= get_username (client
->thd
->peer
->uid
);
4703 user
= get_username (client
->thd
->peer
->uid
);
4707 pthread_cleanup_push (xfree
, user
);
4708 rc
= xfer_data (ctx
, user
, strlen (user
));
4709 pthread_cleanup_pop (1);
4712 rc
= GPG_ERR_NO_DATA
;
4715 rc
= gpg_error (GPG_ERR_SYNTAX
);
4717 return send_error (ctx
, rc
);
4722 send_data_cb (void *user
, const void *buf
, size_t len
)
4724 assuan_context_t ctx
= user
;
4726 return assuan_send_data (ctx
, buf
, len
);
4730 send_status_cb (void *user
, const char *line
)
4732 assuan_context_t ctx
= user
;
4733 char keyword
[200], *k
;
4736 for (p
= line
, k
= keyword
; *p
; p
++)
4748 while (isspace (*p
))
4751 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4756 kill_command (assuan_context_t ctx
, char *line
)
4758 struct client_s
*client
= assuan_get_pointer (ctx
);
4762 if (!line
|| !*line
)
4763 return send_error (ctx
, GPG_ERR_SYNTAX
);
4765 MUTEX_LOCK (&cn_mutex
);
4766 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4767 t
= slist_length (cn_thread_list
);
4770 for (i
= 0; i
< t
; i
++)
4772 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4773 char *tmp
= str_asprintf ("%p", thd
->tid
);
4775 if (strcmp (line
, tmp
))
4782 rc
= peer_is_invoker (client
);
4785 #ifdef HAVE_PTHREAD_CANCEL
4786 rc
= pthread_cancel (thd
->tid
);
4794 rc
= GPG_ERR_EACCES
;
4795 if (config_get_boolean ("global", "strict_kill"))
4799 if (client
->thd
->remote
&& thd
->remote
)
4801 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
4803 #ifdef HAVE_PTHREAD_CANCEL
4804 rc
= pthread_cancel (thd
->tid
);
4812 else if (!client
->thd
->remote
&& !thd
->remote
)
4815 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
4817 #ifdef HAVE_PTHREAD_CANCEL
4818 rc
= pthread_cancel (thd
->tid
);
4828 pthread_cleanup_pop (1);
4829 return send_error (ctx
, rc
);
4833 agent_command (assuan_context_t ctx
, char *line
)
4838 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4841 struct client_s
*client
= assuan_get_pointer (ctx
);
4843 if (!line
|| !*line
)
4844 return send_error (ctx
, GPG_ERR_SYNTAX
);
4846 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4847 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4848 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4849 send_status_cb
, client
->ctx
);
4850 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4855 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4858 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4860 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4864 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4866 return send_error (ctx
, rc
);
4872 /* !BEGIN-HELP-TEXT!
4874 * This comment is used as a marker to generate the offline documentation
4875 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4876 * script to determine where commands begin and end.
4878 new_command("HELP", 1, 1, 0, help_command
, _(
4879 "HELP [<COMMAND>]\n"
4880 "Show available commands or command specific help text."
4883 new_command("AGENT", 1, 1, 0, agent_command
, _(
4885 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4886 "@command{gpg-agent}."
4889 new_command("KILL", 1, 0, 0, kill_command
, _(
4890 "KILL <thread_id>\n"
4891 "Terminates the client identified by @var{thread_id} and releases any file "
4892 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4893 "for details about listing connected clients. The @code{invoking_user} "
4894 "(@pxref{Configuration}) may kill any client while others may only kill "
4895 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4898 new_command("GETINFO", 1, 1, 0, getinfo_command
, _(
4899 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4900 "Get server and other information: @var{CACHE} returns the number of cached "
4901 "documents via a status message. @var{CLIENTS} returns the number of "
4902 "connected clients via a status message or a list of connected clients when "
4903 "the @option{--verbose} parameter is used. The list contains space delimited "
4904 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4905 "file lock status, whether the current client is self, client state, "
4906 "user ID or TLS fingerprint of the connected client and username if the "
4907 "client is a local one. "
4908 "Client state @code{0} is an unknown client state, @code{1} indicates the "
4909 "client has connected but hasn't completed initializing, @code{2} indicates "
4910 "that the client is idle, @code{3} means the "
4911 "client is in a command and @code{4} means the client is disconnecting. This "
4912 "line is always returned with a data response. @var{PID} returns the process "
4913 "ID number of the server via a data response. @var{VERSION} returns the server "
4914 "version number and compile-time features with a data response with each "
4915 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4916 "the last failed command when available. @var{USER} returns the username or "
4917 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4919 "When the @option{--data} option is specified then the result will be sent "
4920 "via a data response rather than a status message."
4923 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
4924 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4925 "Changes the passphrase of the secret key required to open the current "
4926 "file or the passphrase of a symmetrically encrypted data file. When the "
4927 "@option{--reset} option is passed then the cache entry for the current "
4928 "file will be reset and the passphrase, if any, will be required during the "
4929 "next @code{OPEN} (@pxref{OPEN})."
4931 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4932 "of hash iterations for a passphrase and must be either @code{0} to use "
4933 "the calibrated count of the machine (the default), or a value greater than "
4934 "or equal to @code{65536}. This option has no effect for symmetrically "
4935 "encrypted data files."
4937 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4938 "the data file, although a passphrase may be required when changing it."
4940 "This command is not available for non-invoking clients "
4941 "(@pxref{Access Control})."
4944 new_command("KEYGRIP", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, keygrip_command
, _(
4945 "KEYGRIP [--sign] <filename>\n"
4946 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4949 "When the @option{--sign} option is specified then the key used for signing "
4950 "of the specified @var{filename} will be returned."
4952 "For symmetrically encrypted data files this command returns the error "
4953 "GPG_ERR_NOT_SUPPORTED."
4956 new_command("OPEN", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
4957 "OPEN [--lock] <filename> [<passphrase>]\n"
4958 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4959 "found on the file-system then a new document will be created. If the file "
4960 "is found, it is looked for in the file cache. If cached and no "
4961 "@var{passphrase} was specified then the cached document is opened. When not "
4962 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4963 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4966 "When the @option{--lock} option is passed then the file mutex will be "
4967 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4968 "file has been opened."
4971 new_command("SAVE", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
4972 "SAVE [--no-passphrase] [--reset] [--ask] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4973 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4974 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4975 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4976 "keypair will be generated and a pinentry will be used to prompt for the "
4977 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4978 "passed in which case the data file will not be passphrase protected. "
4980 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4981 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4982 "use is enabled. The datafile will be symmetrically encrypted and will not "
4983 "use or generate any keypair."
4985 "The @option{--reset} option will clear the cache entry for the current file "
4986 "and require a passphrase, if needed, before saving."
4988 "The @option{--ask} option will prompt for the passphrase of the current file, "
4989 "if needed, before saving. This differs from the @option{--reset} option by "
4990 "keeping the cache entry in case of an invalid passphrase or some other failure "
4991 "which may otherwise cause a denial of service for other clients."
4993 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4994 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4995 "(@pxref{Configuration}) for available ciphers."
4997 "The @option{--cipher-iterations} option specifies the number of times to "
4998 "hash the passphrase before encrypting the XML data. The default is "
4999 "@code{5000000}. This option is an alias for @option{--s2k-count} since "
5000 "version @var{3.0.15} of @command{pwmd}."
5002 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
5003 "the client to obtain the key paramaters to use when generating a new "
5004 "keypair. The inquired data is expected to be an S-expression. If not "
5005 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
5006 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
5007 "that when this option is specified a new keypair will be generated "
5008 "reguardless if the file is a new one and that if the data file is protected "
5009 "the passphrase to open it will be required before generating the new "
5010 "keypair. This option is not available for non-invoking clients "
5011 "(@pxref{Access Control})."
5013 "You can encrypt the data file to a public key other than the one that it "
5014 "was originally encrypted with by passing the @option{--keygrip} option with "
5015 "the hex encoded keygrip of the public key as its argument. The keygrip may "
5016 "be of any key that @command{gpg-agent} knows about. The "
5017 "@option{--sign-keygrip} option may also be used to sign with an alternate "
5018 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
5019 "keygrip of an existing data file. This option may be needed when using a "
5020 "smartcard. This option has no effect with symmetrically encrypted data "
5021 "files. These options are not available for non-invoking clients "
5022 "(@pxref{Access Control})."
5024 "The @option{--s2k-count} option sets number of hash iterations for a "
5025 "passphrase. A value less-than @code{65536} will use the machine calibrated "
5026 "value and is the default when using @command{gpg-agent}. This setting only "
5027 "affects new files when using @command{gpg-agent}. To change the setting use "
5028 "the @code{PASSWD} command (@pxref{PASSWD}). This option is an alias for "
5029 "option @option{--cipher-iterations} when not using @command{gpg-agent}."
5032 new_command("ISCACHED", 1, 0, 0, iscached_command
, _(
5033 "ISCACHED [--lock] <filename>\n"
5034 "An @emph{OK} response is returned if the specified @var{filename} is found "
5035 "in the file cache. If not found in the cache but exists on the filesystem "
5036 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
5039 "The @option{lock} option will lock the file mutex of @var{filename} when the "
5040 "file exists; it does not need to be opened nor cached. The lock will be "
5041 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
5045 new_command("CLEARCACHE", 1, 1, 0, clearcache_command
, _(
5046 "CLEARCACHE [<filename>]\n"
5047 "Clears a file cache entry for all or the specified @var{filename}."
5050 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command
, _(
5051 "CACHETIMEOUT <filename> <seconds>\n"
5052 "The time in @var{seconds} until @var{filename} will be removed from the "
5053 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
5054 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
5055 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
5059 new_command("LIST", 0, 1, 0, list_command
, _(
5060 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
5061 "If no element path is given then a newline separated list of root elements "
5062 "is returned with a data response. If given, then all reachable elements "
5063 "of the specified element path are returned unless the @option{--no-recurse} "
5064 "option is specified. If specified, only the child elements of the element "
5065 "path are returned without recursing into grandchildren. Each resulting "
5066 "element is prefixed with the literal @code{!} character when the element "
5067 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
5069 "When the @option{--verbose} option is passed then each element path "
5070 "returned will have zero or more flags appened to it. These flags are "
5071 "delimited from the element path by a single space character. A flag itself "
5072 "is a single character. Flag @code{P} indicates that access to the element "
5073 "is denied. Flag @code{+} indicates that there are child nodes of "
5074 "the current element path. Flag @code{E} indicates that an element of an "
5075 "element path contained in a @var{target} attribute could not be found. Flag "
5076 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
5077 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
5078 "of the @var{target} attribute contained in the current element (see below)."
5080 "The @option{--with-target} option implies @option{--verbose} and will append "
5081 "an additional flag @code{T} followed by a single space then an element path. "
5082 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
5083 "current element when it contains a @var{target} attribute. When no "
5084 "@var{target} attribute is found then no flag will be appended."
5086 "The @option{--no-recurse} option limits the amount of data returned to only "
5087 "the listing of children of the specified element path and not any "
5090 "The @option{--all} option lists the entire element tree for each root "
5091 "element. This option also implies option @option{--verbose}."
5093 "When the @option{--inquire} option is passed then all remaining non-option "
5094 "arguments are retrieved via a server @emph{INQUIRE}."
5097 new_command("REALPATH", 0, 1, 0, realpath_command
, _(
5098 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
5099 "Resolves all @code{target} attributes of the specified element path and "
5100 "returns the result with a data response. @xref{Target Attribute}, for details."
5102 "When the @option{--inquire} option is passed then all remaining non-option "
5103 "arguments are retrieved via a server @emph{INQUIRE}."
5106 new_command("STORE", 0, 1, 0, store_command
, _(
5107 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
5108 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5110 "Creates a new element path or modifies the @var{content} of an existing "
5111 "element. If only a single element is specified then a new root element is "
5112 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
5113 "set to the final @key{TAB} delimited element. If no @var{content} is "
5114 "specified after the final @key{TAB}, then the content of an existing "
5115 "element will be removed; or empty when creating a new element."
5117 "The only restriction of an element name is that it not contain whitespace "
5118 "or begin with the literal element character @code{!} unless specifying a "
5119 "literal element (@pxref{Target Attribute}). There is no whitespace between "
5120 "the @key{TAB} delimited elements. It is recommended that the content of an "
5121 "element be base64 encoded when it contains control or @key{TAB} characters "
5122 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
5125 new_command("RENAME", 0, 1, 0, rename_command
, _(
5126 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
5127 "Renames the specified @var{element} to the new @var{value}. If an element of "
5128 "the same name as the @var{value} already exists it will be overwritten."
5130 "When the @option{--inquire} option is passed then all remaining non-option "
5131 "arguments are retrieved via a server @emph{INQUIRE}."
5134 new_command("COPY", 0, 1, 0, copy_command
, _(
5135 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
5136 "Copies the entire element tree starting from the child node of the source "
5137 "element, to the destination element path. If the destination element path "
5138 "does not exist then it will be created; otherwise it is overwritten."
5140 "Note that attributes from the source element are merged into the "
5141 "destination element when the destination element path exists. When an "
5142 "attribute of the same name exists in both the source and destination "
5143 "elements then the destination attribute will be updated to the source "
5146 "When the @option{--inquire} option is passed then all remaining non-option "
5147 "arguments are retrieved via a server @emph{INQUIRE}."
5150 new_command("MOVE", 0, 1, 0, move_command
, _(
5151 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
5152 "Moves the source element path to the destination element path. If the "
5153 "destination is not specified then it will be moved to the root node of the "
5154 "document. If the destination is specified and exists then it will be "
5155 "overwritten; otherwise non-existing elements of the destination element "
5156 "path will be created."
5158 "When the @option{--inquire} option is passed then all remaining non-option "
5159 "arguments are retrieved via a server @emph{INQUIRE}."
5162 new_command("DELETE", 0, 1, 0, delete_command
, _(
5163 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
5164 "Removes the specified element path and all of its children. This may break "
5165 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
5166 "refers to this element or any of its children."
5168 "When the @option{--inquire} option is passed then all remaining non-option "
5169 "arguments are retrieved via a server @emph{INQUIRE}."
5172 new_command("GET", 0, 1, 0, get_command
, _(
5173 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
5174 "Retrieves the content of the specified element. The content is returned "
5175 "with a data response."
5177 "When the @option{--inquire} option is passed then all remaining non-option "
5178 "arguments are retrieved via a server @emph{INQUIRE}."
5181 new_command("ATTR", 0, 1, 0, attr_command
, _(
5182 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
5184 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
5186 " Stores or updates an @var{attribute} name and optional @var{value} of an "
5187 "element. When no @var{value} is specified any existing value will be removed."
5189 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
5191 " Removes an @var{attribute} from an element."
5193 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
5195 " Retrieves a newline separated list of attributes names and values "
5196 "from the specified element. Each attribute name and value is space delimited."
5198 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
5200 " Retrieves the value of an @var{attribute} from an element."
5203 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
5204 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
5207 "The @code{_mtime} attribute is updated each time an element is modified by "
5208 "either storing content, editing attributes or by deleting a child element. "
5209 "The @code{_ctime} attribute is created for each new element in an element "
5212 "When the @option{--inquire} option is passed then all remaining non-option "
5213 "arguments are retrieved via a server @emph{INQUIRE}."
5215 "@xref{Target Attribute}, for details about this special attribute."
5218 new_command("XPATH", 0, 1, 0, xpath_command
, _(
5219 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
5220 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
5221 "specified it is assumed the expression is a request to return a result. "
5222 "Otherwise, the result is set to the @var{value} argument and the document is "
5223 "updated. If there is no @var{value} after the @key{TAB} character, the value "
5224 "is assumed to be empty and the document is updated. For example:"
5227 "XPATH //element[@@_name='password']@key{TAB}\n"
5230 "would clear the content of all @code{password} elements in the data file "
5231 "while leaving off the trailing @key{TAB} would return all @code{password} "
5232 "elements in @abbr{XML} format."
5234 "When the @option{--inquire} option is passed then all remaining non-option "
5235 "arguments are retrieved via a server @emph{INQUIRE}."
5237 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5238 "expression syntax."
5241 new_command("XPATHATTR", 0, 1, 0, xpathattr_command
, _(
5242 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
5243 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
5244 "attributes and does not return a result. For the @var{SET} operation the "
5245 "@var{value} is optional but the field is required. If not specified then "
5246 "the attribute value will be empty. For example:"
5249 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
5252 "would create an @code{password} attribute for each @code{password} element "
5253 "found in the document. The attribute value will be empty but still exist."
5255 "When the @option{--inquire} option is passed then all remaining non-option "
5256 "arguments are retrieved via a server @emph{INQUIRE}."
5258 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5259 "expression syntax."
5262 new_command("IMPORT", 0, 1, 0, import_command
, _(
5263 "IMPORT [--root=[!]element[<TAB>[!]child[..]]] <content>\n"
5264 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5266 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
5267 "argument is raw @abbr{XML} data. The content is created as a child of "
5268 "the element path specified with the @option{--root} option or at the "
5269 "document root when not specified. Existing elements of the same name will "
5272 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
5276 new_command("DUMP", 0, 1, 0, dump_command
, _(
5278 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
5279 "dumping a specific node."
5282 new_command("LOCK", 0, 0, 0, lock_command
, _(
5284 "Locks the mutex associated with the opened file. This prevents other clients "
5285 "from sending commands to the same opened file until the client "
5286 "that sent this command either disconnects or sends the @code{UNLOCK} "
5287 "command. @xref{UNLOCK}."
5290 new_command("UNLOCK", 1, 0, 0, unlock_command
, _(
5292 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
5293 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
5294 "@pxref{ISCACHED})."
5297 new_command("GETCONFIG", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, getconfig_command
, _(
5298 "GETCONFIG [filename] <parameter>\n"
5299 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
5300 "data response. If no file has been opened then the value for @var{filename} "
5301 "or the default from the @samp{global} section will be returned. If a file "
5302 "has been opened and no @var{filename} is specified, a value previously "
5303 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
5306 new_command("OPTION", 1, 1, 0, option_command
, _(
5307 "OPTION <NAME>=<VALUE>\n"
5308 "Sets a client option @var{name} to @var{value}. The value for an option is "
5309 "kept for the duration of the connection."
5312 "@item DISABLE-PINENTRY\n"
5313 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
5314 "server inquire is sent to the client to obtain the passphrase. This option "
5315 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
5316 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
5318 "@item PINENTRY-TIMEOUT\n"
5319 "Sets the number of seconds before a pinentry prompt will return an error "
5320 "while waiting for user input."
5323 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5326 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5329 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5331 "@item PINENTRY-DESC\n"
5332 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
5334 "@item PINENTRY-TITLE\n"
5335 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
5337 "@item PINENTRY-PROMPT\n"
5338 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
5341 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5343 "@item LC-MESSAGES\n"
5344 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5347 "Associates the thread ID of the connection with the specified textual "
5348 "representation. Useful for debugging log messages. May not contain whitespace."
5350 "@item LOCK-TIMEOUT\n"
5351 "When not @code{0}, the duration in tenths of a second to wait for the file "
5352 "mutex which has been locked by another thread to be released before returning "
5353 "an error. When @code{-1}, then an error will be returned immediately."
5357 new_command("LS", 1, 1, 0, ls_command
, _(
5359 "Lists the available data files stored in the data directory "
5360 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5363 new_command("RESET", 1, 1, 0, NULL
, _(
5365 "Closes the currently opened file but keeps any previously set client options."
5368 new_command("NOP", 1, 1, 0, NULL
, _(
5370 "Does nothing. Always returns successfully."
5373 /* !END-HELP-TEXT! */
5374 new_command ("CANCEL", 1, 1, 0, NULL
, NULL
);
5375 new_command ("END", 1, 1, 0, NULL
, NULL
);
5376 new_command ("BYE", 1, 1, 0, NULL
, NULL
);
5379 for (i
= 0; command_table
[i
]; i
++);
5380 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
5385 register_commands (assuan_context_t ctx
)
5389 for (; command_table
[i
]; i
++)
5391 if (!command_table
[i
]->handler
)
5394 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5395 command_table
[i
]->handler
,
5396 command_table
[i
]->help
);
5401 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5405 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5409 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5413 return assuan_register_post_cmd_notify (ctx
, command_finalize
);