2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
37 #include "pwmd-error.h"
42 #include "util-misc.h"
51 /* These are command option flags. */
52 #define OPT_INQUIRE 0x0001
53 #define OPT_NO_PASSPHRASE 0x0002
54 #define OPT_RESET 0x0004
55 #define OPT_LIST_RECURSE 0x0008
56 #define OPT_VERBOSE 0x0010
57 #define OPT_LOCK 0x0020
58 #define OPT_LOCK_ON_OPEN 0x0040
59 #define OPT_SIGN 0x0080
60 #define OPT_LIST_ALL 0x0100
61 #define OPT_LIST_WITH_TARGET 0x0200
62 #define OPT_DATA 0x0400
63 #define OPT_NO_AGENT 0x0800
64 #define OPT_ASK 0x1000
67 /* The GETCONFIG command, for example, may fail because pwmd lost the
68 * gpg-agent connection. Update the recovered agent ctx. */
69 #define UPDATE_AGENT_CTX(client, crypto) do { \
70 if (crypto && crypto->agent && crypto->agent->did_restart \
71 && client->crypto && client->crypto->agent \
72 && client->crypto->agent->ctx && \
73 crypto->agent->ctx != client->crypto->agent->ctx) \
75 client->crypto->agent->ctx = crypto->agent->ctx; \
76 crypto->agent->ctx = NULL; \
80 #define UPDATE_AGENT_CTX(client, crypto)
83 #define FLOCK_TYPE_NONE 0
84 #define FLOCK_TYPE_SH 0x0001
85 #define FLOCK_TYPE_EX 0x0002
86 #define FLOCK_TYPE_KEEP 0x0004
88 struct command_table_s
91 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
94 int unlock
; // unlock the file mutex after validating the checksum
98 static struct command_table_s
**command_table
;
100 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
101 static gpg_error_t
validate_checksum (struct client_s
*,
102 struct cache_data_s
*);
103 static gpg_error_t
update_checksum (struct client_s
*client
);
105 /* When 'status' is true the 'self' field of the status line will be false
106 * because we never send the STATE status message to the same client that
109 build_client_info_line (struct client_thread_s
*thd
, int status
)
111 MUTEX_LOCK (&cn_mutex
);
112 int with_state
= config_get_boolean ("global", "send_state");
113 char *uid
, *username
= NULL
;
118 uid
= str_asprintf("#%s", thd
->tls
->fp
);
121 uid
= str_asprintf("%u", thd
->peer
->uid
);
122 username
= get_username (thd
->peer
->uid
);
125 uid
= str_asprintf("%u", thd
->peer
->uid
);
126 username
= get_username (thd
->peer
->uid
);
128 line
= str_asprintf ("%p %s %s %s %u %u %u %s %s",
130 thd
->name
? thd
->name
: "-",
131 thd
->cl
&& thd
->cl
->filename
132 && (thd
->cl
->flags
& FLAG_OPEN
)
133 ? thd
->cl
->filename
: "/",
135 thd
->remote
? thd
->peeraddr
: "-",
139 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
140 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
141 with_state
? thd
->state
: CLIENT_STATE_UNKNOWN
, uid
,
143 thd
->remote
? "" : username
151 MUTEX_UNLOCK (&cn_mutex
);
156 update_client_state (struct client_s
*client
, unsigned s
)
158 client
->thd
->state
= s
;
159 int n
= config_get_boolean ("global", "send_state");
161 if (n
&& client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
163 char *line
= build_client_info_line (client
->thd
, 1);
166 send_status_all_not_self (n
== 2, STATUS_STATE
, "%s", line
);
171 unlock_file_mutex (struct client_s
*client
, int remove
)
175 // OPEN: keep the lock for the same file being reopened.
176 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
179 if (!(client
->flags
& FLAG_HAS_LOCK
))
180 return GPG_ERR_NOT_LOCKED
;
182 rc
= cache_unlock_mutex (client
->md5file
, remove
);
184 rc
= GPG_ERR_INV_STATE
;
186 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
192 lock_file_mutex (struct client_s
*client
, int add
)
195 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
197 if (client
->flags
& FLAG_HAS_LOCK
)
200 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
201 client
->lock_timeout
, add
, timeout
);
203 client
->flags
|= FLAG_HAS_LOCK
;
209 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
212 int type
= !cmd
->flock_type
|| (cmd
->flock_type
& FLOCK_TYPE_SH
)
215 if (!(client
->flags
& FLAG_OPEN
))
216 return GPG_ERR_INV_STATE
;
218 rc
= lock_flock (client
->ctx
, client
->filename
, type
, &client
->flock_fd
);
219 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
222 rc
= lock_file_mutex (client
, 0);
223 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
225 rc
= validate_checksum (client
, NULL
);
226 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
229 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
232 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
233 unlock_file_mutex (client
, 0);
235 if (rc
|| !(cmd
->flock_type
& FLOCK_TYPE_KEEP
))
236 unlock_flock (&client
->flock_fd
);
242 parse_xml (assuan_context_t ctx
, int new)
244 struct client_s
*client
= assuan_get_pointer (ctx
);
245 int cached
= client
->doc
!= NULL
;
250 client
->doc
= new_document ();
256 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
257 client
->crypto
->plaintext
= result
;
258 client
->crypto
->plaintext_len
= len
;
259 if (!client
->crypto
->plaintext
)
261 xmlFreeDoc (client
->doc
);
270 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
271 client
->crypto
->plaintext_len
, (xmlDocPtr
*)&client
->doc
);
277 free_client (struct client_s
*client
)
280 xmlFreeDoc (client
->doc
);
283 xfree (client
->filename
);
284 xfree (client
->last_error
);
288 cleanup_crypto_stage2 (client
->crypto
);
289 if (client
->crypto
->pkey_sexp
)
290 gcry_sexp_release (client
->crypto
->pkey_sexp
);
292 if (client
->crypto
->sigpkey_sexp
)
293 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
295 client
->crypto
->pkey_sexp
= NULL
;
296 client
->crypto
->sigpkey_sexp
= NULL
;
297 memset (client
->crypto
->sign_grip
, 0,
298 sizeof (client
->crypto
->sign_grip
));
299 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
304 cleanup_client (struct client_s
*client
)
306 assuan_context_t ctx
= client
->ctx
;
307 struct client_thread_s
*thd
= client
->thd
;
308 struct crypto_s
*crypto
= client
->crypto
;
309 long lock_timeout
= client
->lock_timeout
;
310 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
311 struct pinentry_option_s pin_opts
;
312 xmlErrorPtr xml_error
= client
->xml_error
;
313 int flock_fd
= client
->flock_fd
;
315 struct pinentry_option_s agent_pin_opts
;
317 if (crypto
&& crypto
->agent
)
318 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
319 sizeof(struct pinentry_option_s
));
322 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
323 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
324 free_client (client
);
325 memset (client
, 0, sizeof (struct client_s
));
326 client
->flock_fd
= flock_fd
;
327 client
->xml_error
= xml_error
;
328 client
->crypto
= crypto
;
331 client
->lock_timeout
= lock_timeout
;
332 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
333 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
335 if (crypto
&& crypto
->agent
)
336 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
337 sizeof(struct pinentry_option_s
));
342 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
344 struct client_s
*client
= assuan_get_pointer (ctx
);
346 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
347 int cached
= 0, keyarg
= key
? 1 : 0;
349 unsigned char *salted_key
= NULL
;
353 char *pin_title
= client
->pinentry_opts
.title
;
355 client
->crypto
->filename
= str_dup (client
->filename
);
356 if (cdata
|| client
->flags
& FLAG_NEW
)
358 if (cdata
&& !(client
->flags
& FLAG_NEW
))
360 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
365 cached
= cdata
!= NULL
;
369 if (!key
&& !IS_PKI (client
->crypto
)
370 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
372 if (client
->flags
& FLAG_NO_PINENTRY
)
374 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
381 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
384 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
391 if (!IS_PKI (client
->crypto
))
393 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
396 key
= gcry_malloc (keylen
);
397 memset (key
, 0, keylen
);
400 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
401 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
402 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
403 (void **)&salted_key
, &keysize
,
404 client
->crypto
->hdr
.version
<= 0x03000e ? COMPAT_KDFS2K_ITERATIONS
: client
->crypto
->hdr
.s2k_count
);
410 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
411 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
412 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
414 gcry_free (salted_key
);
418 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
423 if (client
->pinentry_opts
.title
!= pin_title
)
424 xfree (client
->pinentry_opts
.title
);
426 client
->pinentry_opts
.title
= pin_title
;
429 gcry_free (salted_key
);
433 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
436 gcry_free (salted_key
);
437 return GPG_ERR_ENOMEM
;
440 cdata
->key
= salted_key
;
441 cdata
->keylen
= keysize
;
445 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
447 rc
= GPG_ERR_NOT_IMPLEMENTED
;
453 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
455 free_cache_data_once (cdata
);
459 int timeout
= config_get_integer (client
->filename
,
465 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
467 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
468 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
469 client
->crypto
->plaintext_len
, &cdata
->doc
,
470 &cdata
->doclen
, &cache_iv
, &cache_blocksize
);
471 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
473 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
474 "%S", client
->crypto
->pkey_sexp
);
479 gcry_sexp_release (cdata
->sigkey
);
481 cdata
->sigkey
= NULL
;
482 if (!rc
&& IS_PKI (client
->crypto
))
483 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
484 "%S", client
->crypto
->sigpkey_sexp
);
488 rc
= cache_add_file (client
->md5file
,
489 (client
->flags
& FLAG_NEW
) ? NULL
490 : client
->crypto
->grip
, cdata
, timeout
);
495 free_cache_data_once (cdata
);
496 xmlFreeDoc (client
->doc
);
503 client
->flags
|= FLAG_OPEN
;
508 rc
= update_checksum (client
);
510 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
511 rc
= do_lock (client
, 0);
517 req_cleanup (void *arg
)
522 strv_free ((char **) arg
);
526 parse_open_opt_lock (void *data
, void *value
)
528 struct client_s
*client
= data
;
530 client
->opts
|= OPT_LOCK_ON_OPEN
;
535 parse_save_opt_inquire (void *data
, void *value
)
537 struct client_s
*client
= data
;
540 if (!(client
->flags
& FLAG_NEW
))
542 rc
= peer_is_invoker (client
);
543 if (rc
== GPG_ERR_EACCES
)
548 client
->opts
|= OPT_INQUIRE
;
553 parse_opt_inquire (void *data
, void *value
)
555 struct client_s
*client
= data
;
558 client
->opts
|= OPT_INQUIRE
;
563 update_checksum (struct client_s
*client
)
567 struct cache_data_s
*cdata
;
568 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
575 cdata
= cache_get_data (client
->md5file
);
579 cdata
->crc
= xmalloc (len
);
580 memcpy (cdata
->crc
, crc
, len
);
587 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
594 if (cdata
&& !cdata
->crc
)
595 return GPG_ERR_CHECKSUM
;
597 rc
= get_checksum (client
->filename
, &crc
, &len
);
602 n
= memcmp (cdata
->crc
, crc
, len
);
603 else if (client
->crc
)
604 n
= memcmp (client
->crc
, crc
, len
);
607 return n
? GPG_ERR_CHECKSUM
: 0;
611 do_open (assuan_context_t ctx
, const char *password
)
613 struct client_s
*client
= assuan_get_pointer (ctx
);
614 struct cache_data_s
*cdata
;
619 cdata
= cache_get_data (client
->md5file
);
620 if (cdata
&& cdata
->doc
)
624 /* This will check that the key is cached in the agent which needs to
625 * be determined for files that share a keygrip. */
628 rc
= cache_iscached (client
->filename
, &defer
);
629 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
630 rc
= GPG_ERR_KEY_EXPIRED
;
633 if (!rc
&& !(client
->flags
& FLAG_NEW
))
634 rc
= validate_checksum (client
, cdata
);
637 if (!rc
&& client
->thd
->remote
638 && config_get_boolean (client
->filename
, "tcp_require_key"))
639 rc
= GPG_ERR_KEY_EXPIRED
;
642 if (!rc
&& !password
)
644 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
648 if (IS_PKI (client
->crypto
))
650 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
652 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
653 client
->crypto
->grip
);
654 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
656 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
657 client
->crypto
->sign_grip
);
662 rc
= open_finalize (ctx
, NULL
, 0);
668 /* There was an error accessing the file so clear the cache entry. The
669 * real error will be returned from read_data_file() since the file
670 * may have only disappeared. */
673 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
674 rc
= cache_clear (client
->md5file
);
675 send_status_all (STATUS_CACHE
, NULL
);
682 rc
= read_data_file (client
->filename
, client
->crypto
);
685 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
686 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
688 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
692 client
->flags
|= FLAG_NEW
;
693 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
694 memset (client
->crypto
->sign_grip
, 0,
695 sizeof (client
->crypto
->sign_grip
));
696 rc
= open_finalize (ctx
, NULL
, 0);
700 if (password
&& IS_PKI (client
->crypto
))
703 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
709 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
714 open_command (assuan_context_t ctx
, char *line
)
717 struct client_s
*client
= assuan_get_pointer (ctx
);
718 char **req
, *filename
;
719 unsigned char md5file
[16];
721 assuan_peercred_t peer
;
722 struct argv_s
*args
[] = {
723 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
727 rc
= parse_options (&line
, args
, client
, 1);
729 return send_error (ctx
, rc
);
731 req
= str_split (line
, " ", 2);
733 return send_error (ctx
, GPG_ERR_SYNTAX
);
735 rc
= do_validate_peer (ctx
, req
[0], &peer
);
739 return send_error (ctx
, rc
);
743 if (!valid_filename (filename
))
746 return send_error (ctx
, GPG_ERR_INV_VALUE
);
749 pthread_cleanup_push (req_cleanup
, req
);
750 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
751 /* This client may have locked a different file with ISCACHED --lock than
752 * the current filename. This will remove that lock. */
753 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
754 if (client
->flags
& FLAG_OPEN
||
755 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
757 uint32_t opts
= client
->opts
;
758 uint32_t flags
= client
->flags
;
761 client
->flags
|= FLAG_KEEP_LOCK
;
762 else if (client
->flags
& FLAG_NEW
)
763 cache_clear (client
->md5file
);
765 cleanup_client (client
);
767 client
->flags
|= flags
;
768 client
->flags
&= ~(FLAG_LOCK_CMD
);
770 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
773 rc
= lock_flock (ctx
, filename
, LOCK_SH
, &client
->flock_fd
);
774 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
777 return send_error(ctx
, rc
);
780 memcpy (client
->md5file
, md5file
, 16);
781 client
->filename
= str_dup (filename
);
782 if (!client
->filename
)
785 return send_error(ctx
, GPG_ERR_ENOMEM
);
788 /* Need to lock the mutex here because file_modified() cannot without
789 * knowing the filename. */
790 rc
= lock_file_mutex (client
, 1);
792 client
->flags
&= ~FLAG_OPEN
;
796 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
799 if (IS_PKI (client
->crypto
))
800 rc
= set_pinentry_mode (client
->crypto
->agent
,
801 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
806 rc
= do_open (ctx
, password
);
808 cleanup_client (client
);
810 cleanup_crypto_stage1 (client
->crypto
);
814 pthread_cleanup_pop (1);
816 if (!rc
&& client
->flags
& FLAG_NEW
)
817 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
820 (void) kill_scd (client
->crypto
->agent
);
823 return send_error (ctx
, rc
);
827 parse_opt_no_passphrase (void *data
, void *value
)
829 struct client_s
*client
= data
;
832 client
->opts
|= OPT_NO_PASSPHRASE
;
837 parse_save_opt_no_agent (void *data
, void *value
)
839 struct client_s
*client
= data
;
841 client
->opts
|= OPT_NO_AGENT
;
846 parse_save_opt_cipher (void *data
, void *value
)
848 struct client_s
*client
= data
;
849 int algo
= cipher_string_to_gcrypt ((char *) value
);
850 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
854 return GPG_ERR_INV_VALUE
;
856 if (!(client
->flags
& FLAG_NEW
))
858 rc
= peer_is_invoker (client
);
859 if (rc
== GPG_ERR_EACCES
)
863 flags
&= ((uint16_t) ((uint32_t) (flags
& 0xFFFFFFFF)));
864 if (!(client
->crypto
->hdr
.flags
& gcrypt_to_cipher (algo
)))
874 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
881 permitted_to_save (struct client_s
*client
, const unsigned char *grip
,
882 size_t size
, const char *value
)
887 if (!(client
->flags
& FLAG_NEW
))
889 hexgrip
= bin2hex (grip
, size
);
890 rc
= !strcmp (hexgrip
, (char *)value
) ? 0 : GPG_ERR_EACCES
;
893 rc
= peer_is_invoker (client
);
901 parse_save_opt_keygrip (void *data
, void *value
)
904 struct client_s
*client
= data
;
905 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->grip
,
906 sizeof (client
->crypto
->grip
),
909 if (!IS_PKI (client
->crypto
))
910 return GPG_ERR_INV_ARG
;
915 if (client
->crypto
->save
.pkey
)
916 gcry_sexp_release (client
->crypto
->save
.pkey
);
918 client
->crypto
->save
.pkey
= NULL
;
919 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
921 return GPG_ERR_INV_ARG
;
926 parse_save_opt_sign_keygrip (void *data
, void *value
)
929 struct client_s
*client
= data
;
930 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->sign_grip
,
931 sizeof (client
->crypto
->sign_grip
),
934 if (!IS_PKI (client
->crypto
))
935 return GPG_ERR_INV_ARG
;
940 if (client
->crypto
->save
.sigpkey
)
941 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
943 client
->crypto
->save
.sigpkey
= NULL
;
944 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
946 return GPG_ERR_INV_ARG
;
951 parse_opt_s2k_count (void *data
, void *value
)
953 struct client_s
*client
= data
;
958 return GPG_ERR_INV_VALUE
;
961 n
= strtoull (v
, &p
, 10);
962 if (n
== UINT64_MAX
&& errno
)
963 return gpg_error_from_errno (errno
);
965 return GPG_ERR_INV_VALUE
;
967 if (!(client
->flags
& FLAG_NEW
))
969 gpg_error_t rc
= peer_is_invoker (client
);
971 if (rc
== GPG_ERR_EACCES
)
973 if (n
&& client
->crypto
->hdr
.s2k_count
!= n
)
982 // Keep compatibility with libpwmd passing --s2k-count=0. If somehow
983 // s2k_count == 0 it will be set to DEFAULT_KDFS2K_ITERATIONS in
986 client
->crypto
->save
.hdr
.s2k_count
= n
;
992 save_finalize (assuan_context_t ctx
)
994 struct client_s
*client
= assuan_get_pointer (ctx
);
996 xmlChar
*xmlbuf
= NULL
;
1001 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
1003 return GPG_ERR_ENOMEM
;
1005 pthread_cleanup_push (xmlFree
, xmlbuf
);
1007 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
1008 && (client
->opts
& OPT_NO_AGENT
))
1009 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
1011 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
1012 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
1013 NULL
, &key
, &keylen
, 1, 1,
1014 (client
->opts
& OPT_NO_PASSPHRASE
));
1019 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
1020 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
1023 pubkey
= client
->crypto
->pkey_sexp
;
1027 sigkey
= client
->crypto
->sigpkey_sexp
;
1030 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
1031 sigkey
= client
->crypto
->save
.sigpkey
;
1035 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
1036 client
->filename
, xmlbuf
, xmlbuflen
);
1037 if (pubkey
== client
->crypto
->save
.pkey
)
1041 gcry_sexp_release (client
->crypto
->pkey_sexp
);
1042 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
1043 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
1044 client
->crypto
->grip
);
1047 gcry_sexp_release (pubkey
);
1049 client
->crypto
->save
.pkey
= NULL
;
1053 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
1055 if (sigkey
== client
->crypto
->save
.sigpkey
)
1059 if (client
->crypto
->sigpkey_sexp
)
1060 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
1062 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
1063 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
1064 client
->crypto
->sign_grip
);
1067 gcry_sexp_release (sigkey
);
1069 client
->crypto
->save
.sigpkey
= NULL
;
1078 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
1079 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
1082 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
1083 send_status_all (STATUS_CACHE
, NULL
);
1087 rc
= update_checksum (client
);
1088 client
->flags
&= ~(FLAG_NEW
);
1092 pthread_cleanup_pop (1); // xmlFree
1097 parse_opt_reset (void *data
, void *value
)
1099 struct client_s
*client
= data
;
1102 client
->opts
|= OPT_RESET
;
1107 parse_opt_ask (void *data
, void *value
)
1109 struct client_s
*client
= data
;
1112 client
->opts
|= OPT_ASK
;
1117 save_command (assuan_context_t ctx
, char *line
)
1119 struct client_s
*client
= assuan_get_pointer (ctx
);
1122 struct argv_s
*args
[] = {
1123 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1124 parse_opt_no_passphrase
},
1125 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
1126 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
1127 parse_save_opt_inquire
},
1128 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
1129 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
1130 parse_save_opt_sign_keygrip
},
1131 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
1132 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
1133 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
1134 parse_opt_s2k_count
},
1135 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
1136 parse_save_opt_no_agent
},
1137 &(struct argv_s
) {"ask", OPTION_TYPE_NOARG
,
1142 cleanup_save (&client
->crypto
->save
);
1143 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
1144 sizeof (file_header_t
));
1146 rc
= parse_options (&line
, args
, client
, 0);
1148 return send_error (ctx
, rc
);
1150 if (!(client
->flags
& FLAG_NEW
))
1151 client
->opts
&= ~OPT_NO_AGENT
;
1152 else if (client
->opts
& OPT_NO_AGENT
)
1154 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
1155 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
1158 /* Update to the default hash iteration count for data file
1159 * versions <= 3.0.14. */
1161 if ((!IS_PKI (client
->crypto
) && client
->crypto
->hdr
.version
<= 0x03000e
1162 && client
->crypto
->save
.hdr
.s2k_count
< DEFAULT_KDFS2K_ITERATIONS
)
1163 || (!IS_PKI (client
->crypto
) && !client
->crypto
->save
.hdr
.s2k_count
))
1165 if ((client
->crypto
->hdr
.version
<= 0x03000e
1166 && client
->crypto
->save
.hdr
.s2k_count
< DEFAULT_KDFS2K_ITERATIONS
)
1167 || !client
->crypto
->save
.hdr
.s2k_count
)
1170 client
->crypto
->save
.hdr
.s2k_count
=
1171 config_get_ulonglong (client
->filename
, "s2k_count");
1172 if (!client
->crypto
->save
.hdr
.s2k_count
)
1173 client
->crypto
->save
.hdr
.s2k_count
= DEFAULT_KDFS2K_ITERATIONS
;
1176 /* Deal with the hashed vs non-hashed cached key mess by clearing the cached
1178 if (client
->crypto
->hdr
.version
<= 0x03000e && !IS_PKI (client
->crypto
))
1179 client
->opts
|= OPT_RESET
;
1180 else if (!IS_PKI (client
->crypto
) && !(client
->flags
& FLAG_NEW
)
1181 && client
->crypto
->hdr
.s2k_count
!=
1182 client
->crypto
->save
.hdr
.s2k_count
)
1183 client
->opts
|= OPT_RESET
;
1185 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
1186 && !(client
->opts
& OPT_INQUIRE
))
1187 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1189 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1190 return send_error (ctx
, gpg_error_from_errno (errno
));
1192 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
1194 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
1195 return send_error (ctx
, GPG_ERR_ENOANO
);
1199 rc
= cache_iscached (client
->filename
, &defer
);
1200 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
1202 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1206 client
->opts
|= OPT_RESET
;
1207 else if (config_get_boolean ("global", "require_save_key"))
1208 client
->opts
|= OPT_ASK
;
1210 if (!rc
&& (client
->opts
& OPT_RESET
))
1212 rc
= cache_clear (client
->md5file
);
1214 return send_error (ctx
, rc
);
1216 log_write ("%s: %s", client
->filename
,
1217 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1218 send_status_all (STATUS_CACHE
, NULL
);
1221 if (!rc
&& client
->opts
& OPT_ASK
)
1222 rc
= crypto_try_decrypt (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
1223 client
->filename
, NULL
, NULL
, NULL
);
1226 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1229 return send_error (ctx
, rc
);
1232 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1233 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1234 && !(client
->opts
& OPT_NO_AGENT
))
1236 rc
= set_pinentry_mode (client
->crypto
->agent
,
1237 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1240 if (!(client
->flags
& FLAG_NEW
))
1242 struct crypto_s
*crypto
;
1246 /* Wanting to generate a new key. Require the key to open the
1247 current file before proceeding reguardless of the
1248 require_save_key configuration parameter. */
1249 rc
= cache_clear (client
->md5file
);
1252 rc
= init_client_crypto (&crypto
);
1254 crypto
->client_ctx
= client
->ctx
;
1258 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1259 client
->filename
, &key
, &keylen
, NULL
, NULL
);
1262 cleanup_crypto (&crypto
);
1266 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1270 struct inquire_data_s idata
= { 0 };
1271 char *params
= client
->opts
& OPT_INQUIRE
1272 ? NULL
: default_key_params (client
->crypto
);
1274 pthread_cleanup_push (xfree
, params
);
1275 idata
.crypto
= client
->crypto
;
1279 idata
.line
= params
;
1280 idata
.len
= strlen (params
);
1285 client
->crypto
->agent
->inquire_data
= &idata
;
1286 client
->crypto
->agent
->inquire_cb
= NULL
;
1287 rc
= generate_key (client
->crypto
, params
,
1288 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1289 pthread_cleanup_pop (1);
1295 rc
= save_finalize (ctx
);
1297 cleanup_crypto_stage1 (client
->crypto
);
1299 (void) kill_scd (client
->crypto
->agent
);
1301 return send_error (ctx
, rc
);
1305 do_delete (assuan_context_t ctx
, char *line
)
1307 struct client_s
*client
= assuan_get_pointer (ctx
);
1312 if (strchr (line
, '\t'))
1313 req
= str_split (line
, "\t", 0);
1315 req
= str_split (line
, " ", 0);
1318 return GPG_ERR_SYNTAX
;
1320 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1328 * No sub-node defined. Remove the entire node (root element).
1334 rc
= is_element_owner (client
, n
);
1337 rc
= unlink_node (client
, n
);
1346 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1347 NULL
, NULL
, 0, 0, NULL
, 0);
1352 rc
= is_element_owner (client
, n
);
1355 rc
= unlink_node (client
, n
);
1363 delete_command (assuan_context_t ctx
, char *line
)
1365 struct client_s
*client
= assuan_get_pointer (ctx
);
1367 struct argv_s
*args
[] = {
1368 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1372 rc
= parse_options (&line
, args
, client
, 1);
1373 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1374 rc
= GPG_ERR_SYNTAX
;
1376 return send_error (ctx
, rc
);
1378 if (client
->opts
& OPT_INQUIRE
)
1380 unsigned char *result
;
1383 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1385 return send_error (ctx
, rc
);
1387 pthread_cleanup_push (xfree
, result
);
1388 rc
= do_delete (ctx
, (char *)result
);
1389 pthread_cleanup_pop (1);
1392 rc
= do_delete (ctx
, line
);
1394 return send_error (ctx
, rc
);
1398 store_command (assuan_context_t ctx
, char *line
)
1400 struct client_s
*client
= assuan_get_pointer (ctx
);
1403 unsigned char *result
;
1405 xmlNodePtr n
, parent
;
1407 char *content
= NULL
;
1410 return send_error (ctx
, GPG_ERR_SYNTAX
);
1412 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1414 return send_error (ctx
, rc
);
1416 req
= str_split ((char *) result
, "\t", 0);
1420 return send_error (ctx
, GPG_ERR_SYNTAX
);
1422 len
= strv_length (req
);
1423 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1424 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1427 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1430 if (has_content
|| !*req
[len
- 1])
1433 content
= req
[len
- 1];
1434 req
[len
- 1] = NULL
;
1438 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1439 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1441 rc
= new_root_element (client
, client
->doc
, *req
);
1445 return send_error (ctx
, rc
);
1454 return send_error (ctx
, rc
);
1459 if (req
[1] && *req
[1])
1462 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1464 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1465 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1471 rc
= is_element_owner (client
, parent
);
1474 n
= find_text_node (parent
->children
);
1476 xmlNodeSetContent (n
, (xmlChar
*) content
);
1478 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1480 update_element_mtime (client
, parent
);
1486 return send_error (ctx
, rc
);
1490 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1495 int progress
= config_get_integer ("global", "xfer_progress");
1499 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1500 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1501 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1509 if (sent
+ to_send
> total
)
1510 to_send
= total
- sent
;
1512 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1513 flush
? 0 : to_send
);
1516 sent
+= flush
? 0 : to_send
;
1518 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1519 (sent
== total
&& flush
))
1520 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1522 if (!flush
&& !rc
&& sent
== total
)
1529 while (!rc
&& sent
< total
);
1535 do_get (assuan_context_t ctx
, char *line
)
1537 struct client_s
*client
= assuan_get_pointer (ctx
);
1542 req
= str_split (line
, "\t", 0);
1547 return GPG_ERR_SYNTAX
;
1550 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1558 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1559 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1565 if (!n
|| !n
->children
)
1566 return GPG_ERR_NO_DATA
;
1568 n
= find_text_node (n
->children
);
1569 if (!n
|| !n
->content
|| !*n
->content
)
1570 return GPG_ERR_NO_DATA
;
1572 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1577 get_command (assuan_context_t ctx
, char *line
)
1579 struct client_s
*client
= assuan_get_pointer (ctx
);
1581 struct argv_s
*args
[] = {
1582 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1586 rc
= parse_options (&line
, args
, client
, 1);
1587 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1588 rc
= GPG_ERR_SYNTAX
;
1590 return send_error (ctx
, rc
);
1592 if (client
->opts
& OPT_INQUIRE
)
1594 unsigned char *result
;
1597 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1599 return send_error (ctx
, rc
);
1601 pthread_cleanup_push (xfree
, result
);
1602 rc
= do_get (ctx
, (char *)result
);
1603 pthread_cleanup_pop (1);
1606 rc
= do_get (ctx
, line
);
1608 return send_error (ctx
, rc
);
1611 static void list_command_cleanup1 (void *arg
);
1613 realpath_command (assuan_context_t ctx
, char *line
)
1615 struct string_s
*string
= NULL
;
1617 struct client_s
*client
= assuan_get_pointer (ctx
);
1618 struct argv_s
*args
[] = {
1619 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1623 rc
= parse_options (&line
, args
, client
, 1);
1624 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1625 rc
= GPG_ERR_SYNTAX
;
1627 return send_error (ctx
, rc
);
1629 if (client
->opts
& OPT_INQUIRE
)
1631 unsigned char *result
;
1634 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1636 return send_error (ctx
, rc
);
1638 pthread_cleanup_push (xfree
, result
);
1639 rc
= build_realpath (client
, client
->doc
, (char *)result
, &string
);
1640 pthread_cleanup_pop (1);
1643 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1647 pthread_cleanup_push (list_command_cleanup1
, string
);
1648 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1649 pthread_cleanup_pop (1);
1652 return send_error (ctx
, rc
);
1656 list_command_cleanup1 (void *arg
)
1659 string_free ((struct string_s
*) arg
, 1);
1663 list_command_cleanup2 (void *arg
)
1665 struct element_list_s
*elements
= arg
;
1671 int total
= slist_length (elements
->list
);
1674 for (i
= 0; i
< total
; i
++)
1676 char *tmp
= slist_nth_data (elements
->list
, i
);
1680 slist_free (elements
->list
);
1683 if (elements
->prefix
)
1684 xfree (elements
->prefix
);
1687 strv_free (elements
->req
);
1694 parse_list_opt_norecurse (void *data
, void *value
)
1696 struct client_s
*client
= data
;
1698 client
->opts
&= ~(OPT_LIST_RECURSE
);
1703 parse_opt_verbose (void *data
, void *value
)
1705 struct client_s
*client
= data
;
1707 client
->opts
|= OPT_VERBOSE
;
1712 parse_list_opt_target (void *data
, void *value
)
1714 struct client_s
*client
= data
;
1716 client
->opts
|= OPT_LIST_WITH_TARGET
;
1721 parse_list_opt_all (void *data
, void *value
)
1723 struct client_s
*client
= data
;
1725 client
->opts
|= OPT_LIST_ALL
;
1730 list_path_once (struct client_s
*client
, char *line
,
1731 struct element_list_s
*elements
, struct string_s
*result
)
1735 elements
->req
= str_split (line
, " ", 0);
1737 strv_printf (&elements
->req
, "%s", line
);
1739 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1740 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1745 int total
= slist_length (elements
->list
);
1751 for (i
= 0; i
< total
; i
++)
1753 char *tmp
= slist_nth_data (elements
->list
, i
);
1755 string_append_printf (result
, "%s%s", tmp
,
1756 i
+ 1 == total
? "" : "\n");
1760 rc
= GPG_ERR_NO_DATA
;
1767 has_list_flag (char *path
, char *flags
)
1771 while (*p
&& *++p
!= ' ');
1780 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1791 do_list (assuan_context_t ctx
, char *line
)
1793 struct client_s
*client
= assuan_get_pointer (ctx
);
1795 struct element_list_s
*elements
= NULL
;
1797 elements
= xcalloc (1, sizeof (struct element_list_s
));
1799 return GPG_ERR_ENOMEM
;
1801 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1803 (client
->opts
& OPT_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1804 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1806 if (!line
|| !*line
)
1808 struct string_s
*str
= NULL
;
1810 pthread_cleanup_push (list_command_cleanup2
, elements
);
1811 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1812 elements
->with_target
);
1813 pthread_cleanup_pop (1);
1814 pthread_cleanup_push (list_command_cleanup1
, str
);
1818 if (client
->opts
& OPT_LIST_ALL
)
1820 char **roots
= str_split (str
->str
, "\n", 0);
1823 pthread_cleanup_push (req_cleanup
, roots
);
1824 string_truncate (str
, 0);
1826 for (p
= roots
; *p
; p
++)
1828 if (strchr (*p
, ' '))
1830 if (has_list_flag (*p
, "EOP"))
1832 string_append_printf (str
, "%s%s", *p
,
1833 *(p
+ 1) ? "\n" : "");
1838 elements
= xcalloc (1, sizeof (struct element_list_s
));
1841 rc
= GPG_ERR_ENOMEM
;
1845 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1847 (client
->opts
& OPT_VERBOSE
) | (client
->opts
&
1848 OPT_LIST_WITH_TARGET
);
1849 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1850 pthread_cleanup_push (list_command_cleanup2
, elements
);
1851 rc
= list_path_once (client
, *p
, elements
, str
);
1852 pthread_cleanup_pop (1);
1857 string_append (str
, "\n");
1860 pthread_cleanup_pop (1);
1864 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1867 pthread_cleanup_pop (1);
1871 pthread_cleanup_push (list_command_cleanup2
, elements
);
1872 struct string_s
*str
= string_new (NULL
);
1873 pthread_cleanup_push (list_command_cleanup1
, str
);
1874 rc
= list_path_once (client
, line
, elements
, str
);
1876 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1878 pthread_cleanup_pop (1);
1879 pthread_cleanup_pop (1);
1884 list_command (assuan_context_t ctx
, char *line
)
1886 struct client_s
*client
= assuan_get_pointer (ctx
);
1888 struct argv_s
*args
[] = {
1889 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1890 parse_list_opt_norecurse
},
1891 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
1892 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1893 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1894 parse_list_opt_target
},
1895 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1899 if (disable_list_and_dump
== 1)
1900 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1902 client
->opts
|= OPT_LIST_RECURSE
;
1903 rc
= parse_options (&line
, args
, client
, 1);
1904 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1905 rc
= GPG_ERR_SYNTAX
;
1907 return send_error (ctx
, rc
);
1909 if (client
->opts
& OPT_LIST_ALL
)
1910 client
->opts
|= OPT_LIST_RECURSE
| OPT_VERBOSE
;
1912 if (client
->opts
& OPT_INQUIRE
)
1914 unsigned char *result
;
1917 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1919 return send_error (ctx
, rc
);
1921 pthread_cleanup_push (xfree
, result
);
1922 rc
= do_list (ctx
, (char *)result
);
1923 pthread_cleanup_pop (1);
1926 rc
= do_list (ctx
, line
);
1928 return send_error (ctx
, rc
);
1932 * req[0] - element path
1935 attribute_list (assuan_context_t ctx
, char **req
)
1937 struct client_s
*client
= assuan_get_pointer (ctx
);
1938 char **attrlist
= NULL
;
1946 if (!req
|| !req
[0])
1947 return GPG_ERR_SYNTAX
;
1949 client
->flags
|= FLAG_ACL_IGNORE
;
1951 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1954 * The first argument may be only a root element.
1956 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1957 return GPG_ERR_SYNTAX
;
1960 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1967 if (client
->flags
& FLAG_ACL_ERROR
)
1969 client
->flags
&= ~FLAG_ACL_IGNORE
;
1973 return GPG_ERR_EACCES
;
1976 client
->flags
&= ~FLAG_ACL_ERROR
;
1981 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1982 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1992 for (a
= n
->properties
; a
; a
= a
->next
)
1996 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1999 strv_free (attrlist
);
2001 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2002 pwmd_strerror (GPG_ERR_ENOMEM
));
2003 return GPG_ERR_ENOMEM
;
2008 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
2010 && an
->content
? (char *) an
->content
: "");
2014 strv_free (attrlist
);
2015 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2016 pwmd_strerror (GPG_ERR_ENOMEM
));
2017 return GPG_ERR_ENOMEM
;
2020 attrlist
[++i
] = NULL
;
2024 return GPG_ERR_NO_DATA
;
2026 line
= strv_join ("\n", attrlist
);
2030 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2031 pwmd_strerror (GPG_ERR_ENOMEM
));
2032 strv_free (attrlist
);
2033 return GPG_ERR_ENOMEM
;
2036 pthread_cleanup_push (xfree
, line
);
2037 pthread_cleanup_push (req_cleanup
, attrlist
);
2038 rc
= xfer_data (ctx
, line
, strlen (line
));
2039 pthread_cleanup_pop (1);
2040 pthread_cleanup_pop (1);
2045 * req[0] - attribute
2046 * req[1] - element path
2049 attribute_delete (struct client_s
*client
, char **req
)
2055 if (!req
|| !req
[0] || !req
[1])
2056 return GPG_ERR_SYNTAX
;
2058 if (!strcmp (req
[0], "_name"))
2059 return GPG_ERR_INV_ATTR
;
2061 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2064 * The first argument may be only a root element.
2066 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2067 return GPG_ERR_SYNTAX
;
2070 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2076 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2077 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2082 if (!strcmp (req
[0], (char *) "_acl"))
2084 rc
= is_element_owner (client
, n
);
2089 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
2097 create_element_path (struct client_s
*client
,
2098 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
2100 char **req
= *elements
;
2101 char **req_orig
= strv_dup (req
);
2102 xmlNodePtr n
= NULL
;
2108 *rc
= GPG_ERR_ENOMEM
;
2109 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2110 pwmd_strerror (GPG_ERR_ENOMEM
));
2115 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2118 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2121 *rc
= new_root_element (client
, client
->doc
, req
[0]);
2127 else if (n
== parent
)
2129 *rc
= GPG_ERR_CONFLICT
;
2136 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
2138 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2139 NULL
, NULL
, create_target_elements_cb
, 0, 0,
2146 * Reset the position of the element tree now that the elements
2147 * have been created.
2152 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2156 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2157 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2164 strv_free (req_orig
);
2171 * Creates a "target" attribute. When other commands encounter an element with
2172 * this attribute, the element path is modified to the target value. If the
2173 * source element path doesn't exist when using 'ATTR SET target', it is
2174 * created, but the destination element path must exist.
2176 * req[0] - source element path
2177 * req[1] - destination element path
2180 target_attribute (struct client_s
*client
, char **req
)
2182 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
2186 if (!req
|| !req
[0] || !req
[1])
2187 return GPG_ERR_SYNTAX
;
2189 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
2192 * The first argument may be only a root element.
2194 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
2195 return GPG_ERR_SYNTAX
;
2198 if (!valid_element_path (src
, 0))
2199 return GPG_ERR_INV_VALUE
;
2201 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
2204 * The first argument may be only a root element.
2206 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
2208 rc
= GPG_ERR_SYNTAX
;
2213 odst
= strv_dup (dst
);
2216 rc
= GPG_ERR_ENOMEM
;
2220 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2222 * Make sure the destination element path exists.
2224 if (rc
&& rc
!= GPG_ERR_EACCES
)
2230 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
2231 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2232 if (rc
&& rc
!= GPG_ERR_EACCES
)
2236 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
2237 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2240 n
= create_element_path (client
, &src
, &rc
, NULL
);
2244 line
= strv_join ("\t", odst
);
2247 rc
= GPG_ERR_ENOMEM
;
2251 rc
= add_attribute (client
, n
, "target", line
);
2262 * req[0] - attribute
2263 * req[1] - element path
2266 attribute_get (assuan_context_t ctx
, char **req
)
2268 struct client_s
*client
= assuan_get_pointer (ctx
);
2274 if (!req
|| !req
[0] || !req
[1])
2275 return GPG_ERR_SYNTAX
;
2277 if (strchr (req
[1], '\t'))
2279 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2280 return GPG_ERR_SYNTAX
;
2284 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2285 return GPG_ERR_SYNTAX
;
2288 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2294 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2295 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2302 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2303 return GPG_ERR_NOT_FOUND
;
2305 pthread_cleanup_push (xmlFree
, a
);
2308 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2310 rc
= GPG_ERR_NO_DATA
;
2312 pthread_cleanup_pop (1);
2321 * req[0] - attribute
2322 * req[1] - element path
2326 attribute_set (struct client_s
*client
, char **req
)
2332 if (!req
|| !req
[0] || !req
[1])
2333 return GPG_ERR_SYNTAX
;
2336 * Reserved attribute names.
2338 if (!strcmp (req
[0], "_name"))
2339 return GPG_ERR_INV_ATTR
;
2340 else if (!strcmp (req
[0], "target"))
2341 return target_attribute (client
, req
+ 1);
2342 else if (!valid_xml_attribute (req
[0]))
2343 return GPG_ERR_INV_VALUE
;
2345 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2348 * The first argument may be only a root element.
2350 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2351 return GPG_ERR_SYNTAX
;
2354 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2360 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2361 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2366 if (!strcmp (req
[0], (char *) "_acl"))
2368 rc
= is_element_owner (client
, n
);
2373 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2382 * req[1] - attribute name or element path if command is LIST
2383 * req[2] - element path
2384 * req[2] - element path or value
2388 do_attr (assuan_context_t ctx
, char *line
)
2390 struct client_s
*client
= assuan_get_pointer (ctx
);
2394 req
= str_split (line
, " ", 4);
2395 if (!req
|| !req
[0] || !req
[1])
2398 return GPG_ERR_SYNTAX
;
2401 pthread_cleanup_push (req_cleanup
, req
);
2403 if (strcasecmp (req
[0], "SET") == 0)
2404 rc
= attribute_set (client
, req
+ 1);
2405 else if (strcasecmp (req
[0], "GET") == 0)
2406 rc
= attribute_get (ctx
, req
+ 1);
2407 else if (strcasecmp (req
[0], "DELETE") == 0)
2408 rc
= attribute_delete (client
, req
+ 1);
2409 else if (strcasecmp (req
[0], "LIST") == 0)
2410 rc
= attribute_list (ctx
, req
+ 1);
2412 rc
= GPG_ERR_SYNTAX
;
2414 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2415 pthread_cleanup_pop (1);
2420 attr_command (assuan_context_t ctx
, char *line
)
2422 struct client_s
*client
= assuan_get_pointer (ctx
);
2424 struct argv_s
*args
[] = {
2425 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2429 rc
= parse_options (&line
, args
, client
, 1);
2430 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2431 rc
= GPG_ERR_SYNTAX
;
2433 return send_error (ctx
, rc
);
2435 if (client
->opts
& OPT_INQUIRE
)
2437 unsigned char *result
;
2440 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2442 return send_error (ctx
, rc
);
2444 pthread_cleanup_push (xfree
, result
);
2445 rc
= do_attr (ctx
, (char *)result
);
2446 pthread_cleanup_pop (1);
2449 rc
= do_attr (ctx
, line
);
2451 return send_error (ctx
, rc
);
2455 parse_iscached_opt_lock (void *data
, void *value
)
2457 struct client_s
*client
= data
;
2460 client
->opts
|= OPT_LOCK
;
2465 iscached_command (assuan_context_t ctx
, char *line
)
2467 struct client_s
*client
= assuan_get_pointer (ctx
);
2469 struct argv_s
*args
[] = {
2470 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2474 if (!line
|| !*line
)
2475 return send_error (ctx
, GPG_ERR_SYNTAX
);
2477 rc
= parse_options (&line
, args
, client
, 1);
2479 return send_error (ctx
, rc
);
2480 else if (!valid_filename (line
))
2481 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2483 rc
= cache_iscached (line
, NULL
);
2484 if (client
->opts
& OPT_LOCK
2485 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2487 unsigned char md5file
[16];
2488 gpg_error_t trc
= rc
;
2490 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2491 if (memcmp (md5file
, client
->md5file
, 16))
2492 cleanup_client (client
);
2494 memcpy (client
->md5file
, md5file
, 16);
2495 rc
= do_lock (client
, 1);
2500 return send_error (ctx
, rc
);
2504 clearcache_command (assuan_context_t ctx
, char *line
)
2506 gpg_error_t rc
= 0, all_rc
= 0;
2507 unsigned char md5file
[16];
2511 struct client_thread_s
*once
= NULL
;
2514 MUTEX_LOCK (&cn_mutex
);
2515 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2517 if (!line
|| !*line
)
2520 t
= slist_length (cn_thread_list
);
2522 for (i
= 0; i
< t
; i
++)
2524 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2525 assuan_peercred_t peer
;
2530 /* Lock each connected clients' file mutex to prevent any other client
2531 * from accessing the cache entry (the file mutex is locked upon
2532 * command startup). The cache for the entry is not cleared if the
2533 * file mutex is locked by another client to prevent this function
2538 if (thd
->cl
->filename
)
2540 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2541 all_rc
= !all_rc
? rc
: all_rc
;
2549 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2550 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2552 if (pthread_equal (pthread_self (), thd
->tid
))
2556 if (!thd
->cl
->filename
||
2557 cache_iscached (thd
->cl
->filename
,
2558 NULL
) == GPG_ERR_NO_DATA
)
2564 cache_defer_clear (thd
->cl
->md5file
);
2567 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2575 rc
= cache_clear (thd
->cl
->md5file
);
2576 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2584 /* A single data filename was specified. Lock only this data file
2585 * mutex and free the cache entry. */
2588 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2589 rc
= do_validate_peer (ctx
, line
, &peer
);
2591 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2593 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2595 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2597 if (pthread_equal (pthread_self (), thd
->tid
))
2604 rc
= cache_clear (thd
->cl
->md5file
);
2605 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2609 cache_defer_clear (thd
->cl
->md5file
);
2617 /* Only connected clients' cache entries have been cleared. Now clear any
2618 * remaining cache entries without clients but only if there wasn't an
2619 * error from above since this would defeat the locking check of the
2620 * remaining entries. */
2626 /* No clients are using the specified file. */
2627 else if (!all_rc
&& !rc
&& !once
)
2629 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2630 rc
= cache_clear (md5file
);
2633 /* Release the connection mutex. */
2634 pthread_cleanup_pop (1);
2638 send_status_all (STATUS_CACHE
, NULL
);
2640 /* One or more files were locked while clearing all cache entries. */
2644 return send_error (ctx
, rc
);
2648 cachetimeout_command (assuan_context_t ctx
, char *line
)
2651 char **req
= str_split (line
, " ", 0);
2654 assuan_peercred_t peer
;
2656 if (!req
|| !*req
|| !req
[1])
2659 return send_error (ctx
, GPG_ERR_SYNTAX
);
2663 timeout
= (int) strtol (req
[1], &p
, 10);
2664 if (errno
!= 0 || *p
|| timeout
< -1)
2667 return send_error (ctx
, GPG_ERR_SYNTAX
);
2670 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2673 unsigned char md5file
[16];
2675 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2676 rc
= cache_set_timeout (md5file
, timeout
);
2677 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2680 MUTEX_LOCK (&rcfile_mutex
);
2681 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2683 MUTEX_UNLOCK (&rcfile_mutex
);
2688 return send_error (ctx
, rc
);
2692 dump_command (assuan_context_t ctx
, char *line
)
2696 struct client_s
*client
= assuan_get_pointer (ctx
);
2699 if (disable_list_and_dump
== 1)
2700 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2703 return send_error (ctx
, GPG_ERR_SYNTAX
);
2705 rc
= peer_is_invoker(client
);
2707 return send_error (ctx
, rc
);
2709 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2713 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2714 pwmd_strerror (GPG_ERR_ENOMEM
));
2715 return send_error (ctx
, GPG_ERR_ENOMEM
);
2718 pthread_cleanup_push (xmlFree
, xml
);
2719 rc
= xfer_data (ctx
, (char *) xml
, len
);
2720 pthread_cleanup_pop (1);
2721 return send_error (ctx
, rc
);
2725 getconfig_command (assuan_context_t ctx
, char *line
)
2727 struct client_s
*client
= assuan_get_pointer (ctx
);
2729 char filename
[255] = { 0 }, param
[747] = { 0 };
2730 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2732 if (!line
|| !*line
)
2733 return send_error (ctx
, GPG_ERR_SYNTAX
);
2735 if (strchr (line
, ' '))
2737 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2742 if (fp
&& !valid_filename (fp
))
2743 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2745 paramp
= str_down (paramp
);
2746 if (!strcmp (paramp
, "cipher") && fp
)
2748 struct crypto_s
*crypto
= NULL
;
2750 rc
= init_client_crypto (&crypto
);
2753 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2757 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2767 UPDATE_AGENT_CTX (client
, crypto
);
2768 cleanup_crypto (&crypto
);
2769 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2770 return send_error (ctx
, rc
);
2775 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2777 struct crypto_s
*crypto
= NULL
;
2779 rc
= init_client_crypto (&crypto
);
2782 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2785 tmp
= str_asprintf ("%llu",
2786 (unsigned long long) crypto
->hdr
.s2k_count
);
2788 rc
= GPG_ERR_ENOMEM
;
2792 UPDATE_AGENT_CTX (client
, crypto
);
2793 cleanup_crypto (&crypto
);
2794 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2795 return send_error (ctx
, rc
);
2800 else if (!strcmp (paramp
, "passphrase"))
2801 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2803 p
= config_get_value (fp
? fp
: "global", paramp
);
2805 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2807 tmp
= expand_homedir (p
);
2811 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2812 pwmd_strerror (GPG_ERR_ENOMEM
));
2813 return send_error (ctx
, GPG_ERR_ENOMEM
);
2818 pthread_cleanup_push (xfree
, p
);
2819 rc
= xfer_data (ctx
, p
, strlen (p
));
2820 pthread_cleanup_pop (1);
2821 return send_error (ctx
, rc
);
2826 xmlXPathContextPtr xp
;
2827 xmlXPathObjectPtr result
;
2833 xpath_command_cleanup (void *arg
)
2835 struct xpath_s
*xpath
= arg
;
2840 req_cleanup (xpath
->req
);
2843 xmlBufferFree (xpath
->buf
);
2846 xmlXPathFreeObject (xpath
->result
);
2849 xmlXPathFreeContext (xpath
->xp
);
2853 do_xpath (assuan_context_t ctx
, char *line
)
2856 struct client_s
*client
= assuan_get_pointer (ctx
);
2857 struct xpath_s _x
= { 0 };
2858 struct xpath_s
*xpath
= &_x
;
2860 if (!line
|| !*line
)
2861 return GPG_ERR_SYNTAX
;
2863 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2865 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2866 return GPG_ERR_ENOMEM
;
2869 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2872 rc
= GPG_ERR_BAD_DATA
;
2877 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2880 rc
= GPG_ERR_BAD_DATA
;
2884 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2886 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2890 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2891 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2895 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2897 rc
= GPG_ERR_NO_DATA
;
2900 else if (xpath
->req
[1])
2906 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2907 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2908 xmlBufferLength (xpath
->buf
));
2909 pthread_cleanup_pop (0);
2911 xpath_command_cleanup (xpath
);
2916 xpath_command (assuan_context_t ctx
, char *line
)
2918 struct client_s
*client
= assuan_get_pointer (ctx
);
2920 struct argv_s
*args
[] = {
2921 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2925 if (disable_list_and_dump
== 1)
2926 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2928 rc
= peer_is_invoker(client
);
2930 return send_error (ctx
, rc
);
2932 rc
= parse_options (&line
, args
, client
, 1);
2933 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2934 rc
= GPG_ERR_SYNTAX
;
2936 return send_error (ctx
, rc
);
2938 if (client
->opts
& OPT_INQUIRE
)
2940 unsigned char *result
;
2943 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2945 return send_error (ctx
, rc
);
2947 pthread_cleanup_push (xfree
, result
);
2948 rc
= do_xpath (ctx
, (char *)result
);
2949 pthread_cleanup_pop (1);
2952 rc
= do_xpath (ctx
, line
);
2954 return send_error (ctx
, rc
);
2958 do_xpathattr (assuan_context_t ctx
, char *line
)
2960 struct client_s
*client
= assuan_get_pointer (ctx
);
2964 struct xpath_s _x
= { 0 };
2965 struct xpath_s
*xpath
= &_x
;
2967 if (!line
|| !*line
)
2968 return GPG_ERR_SYNTAX
;
2970 if ((req
= str_split (line
, " ", 3)) == NULL
)
2971 return GPG_ERR_ENOMEM
;
2975 rc
= GPG_ERR_SYNTAX
;
2979 if (!strcasecmp (req
[0], "SET"))
2981 else if (!strcasecmp (req
[0], "DELETE"))
2985 rc
= GPG_ERR_SYNTAX
;
2989 if (!req
[1] || !req
[2])
2991 rc
= GPG_ERR_SYNTAX
;
2995 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2997 rc
= GPG_ERR_ENOMEM
;
3001 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
3003 rc
= GPG_ERR_SYNTAX
;
3007 xpath
->xp
= xmlXPathNewContext (client
->doc
);
3010 rc
= GPG_ERR_BAD_DATA
;
3014 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
3017 rc
= GPG_ERR_BAD_DATA
;
3021 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
3023 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
3027 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
3028 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
3029 (xmlChar
*) req
[1]);
3032 xpath_command_cleanup (xpath
);
3037 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
3039 xpathattr_command (assuan_context_t ctx
, char *line
)
3041 struct client_s
*client
= assuan_get_pointer (ctx
);
3043 struct argv_s
*args
[] = {
3044 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3048 if (disable_list_and_dump
== 1)
3049 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
3051 rc
= peer_is_invoker(client
);
3053 return send_error (ctx
, rc
);
3055 rc
= parse_options (&line
, args
, client
, 1);
3056 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3057 rc
= GPG_ERR_SYNTAX
;
3059 return send_error (ctx
, rc
);
3061 if (client
->opts
& OPT_INQUIRE
)
3063 unsigned char *result
;
3066 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3068 return send_error (ctx
, rc
);
3070 pthread_cleanup_push (xfree
, result
);
3071 rc
= do_xpathattr (ctx
, (char *)result
);
3072 pthread_cleanup_pop (1);
3075 rc
= do_xpathattr (ctx
, line
);
3077 return send_error (ctx
, rc
);
3081 do_import (struct client_s
*client
, const char *root_element
,
3082 unsigned char *content
)
3084 char **dst_path
= NULL
;
3085 xmlDocPtr doc
= NULL
;
3086 xmlNodePtr n
, root
, copy
;
3089 if (!content
|| !*content
)
3092 return GPG_ERR_SYNTAX
;
3096 dst_path
= str_split (root_element
, "\t", 0);
3098 if (dst_path
&& !valid_element_path (dst_path
, 0))
3101 strv_free (dst_path
);
3103 return GPG_ERR_INV_VALUE
;
3106 struct string_s
*str
= string_new_content ((char *)content
);
3107 str
= string_prepend (str
, "<pwmd>");
3108 str
= string_append (str
, "</pwmd>");
3109 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
3110 string_free (str
, 1);
3113 rc
= GPG_ERR_BAD_DATA
;
3117 root
= xmlDocGetRootElement (doc
);
3118 xmlNodePtr root_orig
= root
->children
;
3119 root
= root
->children
;
3120 rc
= validate_import (client
, root
);
3129 char **path
= strv_dup (dst_path
);
3132 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
3133 pwmd_strerror (GPG_ERR_ENOMEM
));
3134 rc
= GPG_ERR_ENOMEM
;
3138 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
3142 rc
= GPG_ERR_INV_VALUE
;
3146 if (strv_printf (&path
, "%s", (char *) a
) == 0)
3149 rc
= GPG_ERR_ENOMEM
;
3154 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
3155 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3163 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
3164 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
3165 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3180 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
3182 n
= create_element_path (client
, &path
, &rc
, NULL
);
3189 copy
= xmlCopyNodeList (root
->children
);
3190 n
= xmlAddChildList (n
, copy
);
3192 rc
= GPG_ERR_ENOMEM
;
3201 /* Check if the content root element can create a DTD root element. */
3202 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
3204 rc
= GPG_ERR_SYNTAX
;
3210 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
3212 rc
= GPG_ERR_SYNTAX
;
3216 char *tmp
= str_dup ((char *) a
);
3218 int literal
= is_literal_element (&tmp
);
3220 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
3223 rc
= GPG_ERR_INV_VALUE
;
3227 if (strv_printf (&path
, "%s", tmp
) == 0)
3230 rc
= GPG_ERR_ENOMEM
;
3235 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
3236 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3238 rc
= GPG_ERR_BAD_DATA
;
3242 /* Overwriting the existing tree. */
3246 xmlFreeNodeList (n
);
3250 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
3251 n
= xmlCopyNode (root
, 1);
3252 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3257 rc
= update_element_mtime (client
, n
->parent
);
3259 for (root
= root_orig
->next
; root
; root
= root
->next
)
3261 if (root
->type
== XML_ELEMENT_NODE
)
3274 strv_free (dst_path
);
3280 parse_import_opt_root (void *data
, void *value
)
3282 struct client_s
*client
= data
;
3284 client
->import_root
= str_dup (value
);
3285 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3289 import_command (assuan_context_t ctx
, char *line
)
3292 struct client_s
*client
= assuan_get_pointer (ctx
);
3293 unsigned char *result
;
3295 struct argv_s
*args
[] = {
3296 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3300 xfree (client
->import_root
);
3301 client
->import_root
= NULL
;
3302 rc
= parse_options (&line
, args
, client
, 0);
3304 return send_error (ctx
, rc
);
3306 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3309 xfree (client
->import_root
);
3310 client
->import_root
= NULL
;
3311 return send_error (ctx
, rc
);
3314 rc
= do_import (client
, client
->import_root
, result
);
3315 xfree (client
->import_root
);
3316 client
->import_root
= NULL
;
3317 return send_error (ctx
, rc
);
3321 do_lock (struct client_s
*client
, int add
)
3323 gpg_error_t rc
= lock_file_mutex (client
, add
);
3326 client
->flags
|= FLAG_LOCK_CMD
;
3332 lock_command (assuan_context_t ctx
, char *line
)
3334 struct client_s
*client
= assuan_get_pointer (ctx
);
3338 return send_error (ctx
, GPG_ERR_SYNTAX
);
3340 rc
= do_lock (client
, 0);
3341 return send_error (ctx
, rc
);
3345 unlock_command (assuan_context_t ctx
, char *line
)
3347 struct client_s
*client
= assuan_get_pointer (ctx
);
3351 return send_error (ctx
, GPG_ERR_SYNTAX
);
3353 rc
= unlock_file_mutex (client
, 0);
3354 return send_error (ctx
, rc
);
3358 option_command (assuan_context_t ctx
, char *line
)
3360 struct client_s
*client
= assuan_get_pointer (ctx
);
3362 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3364 struct agent_s
*agent
= client
->crypto
->agent
;
3366 char namebuf
[255] = { 0 };
3367 char *name
= namebuf
;
3368 char *value
= NULL
, *p
, *tmp
= NULL
;
3370 p
= strchr (line
, '=');
3373 strncpy (namebuf
, line
, sizeof(namebuf
));
3374 namebuf
[sizeof(namebuf
)-1] = 0;
3378 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3379 namebuf
[sizeof(namebuf
)-1] = 0;
3383 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3385 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3391 n
= strtol (value
, &tmp
, 10);
3393 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3396 client
->lock_timeout
= n
;
3399 else if (strcasecmp (name
, (char *) "NAME") == 0)
3401 if (value
&& strchr (value
, ' '))
3402 rc
= GPG_ERR_INV_VALUE
;
3405 tmp
= pthread_getspecific (thread_name_key
);
3407 MUTEX_LOCK (&cn_mutex
);
3408 xfree (client
->thd
->name
);
3409 client
->thd
->name
= NULL
;
3411 if (!value
|| !*value
)
3412 pthread_setspecific (thread_name_key
, str_dup (""));
3415 client
->thd
->name
= str_dup (value
);
3416 pthread_setspecific (thread_name_key
, str_dup (value
));
3419 MUTEX_UNLOCK (&cn_mutex
);
3423 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3425 xfree (pin_opts
->lc_messages
);
3426 pin_opts
->lc_messages
= NULL
;
3427 if (value
&& *value
)
3428 pin_opts
->lc_messages
= str_dup (value
);
3431 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3434 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3436 xfree (pin_opts
->lc_ctype
);
3437 pin_opts
->lc_ctype
= NULL
;
3438 if (value
&& *value
)
3439 pin_opts
->lc_ctype
= str_dup (value
);
3442 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3445 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3447 xfree (pin_opts
->ttyname
);
3448 pin_opts
->ttyname
= NULL
;
3449 if (value
&& *value
)
3450 pin_opts
->ttyname
= str_dup (value
);
3453 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3456 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3458 xfree (pin_opts
->ttytype
);
3459 pin_opts
->ttytype
= NULL
;
3460 if (value
&& *value
)
3461 pin_opts
->ttytype
= str_dup (value
);
3464 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3467 else if (strcasecmp (name
, (char *) "display") == 0)
3469 xfree (pin_opts
->display
);
3470 pin_opts
->display
= NULL
;
3471 if (value
&& *value
)
3472 pin_opts
->display
= str_dup (value
);
3475 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3478 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3480 xfree (pin_opts
->desc
);
3481 pin_opts
->desc
= NULL
;
3482 if (value
&& *value
)
3483 pin_opts
->desc
= str_dup (value
);
3485 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3487 xfree (pin_opts
->title
);
3488 pin_opts
->title
= NULL
;
3489 if (value
&& *value
)
3490 pin_opts
->title
= str_dup (value
);
3492 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3494 xfree (pin_opts
->prompt
);
3495 pin_opts
->prompt
= NULL
;
3496 if (value
&& *value
)
3497 pin_opts
->prompt
= str_dup (value
);
3500 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3508 n
= (int) strtol (value
, &p
, 10);
3511 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3513 pin_opts
->timeout
= n
;
3514 MUTEX_LOCK (&rcfile_mutex
);
3515 config_set_int_param (&global_config
,
3516 client
->filename
? client
->filename
: "global",
3517 "pinentry_timeout", value
);
3518 MUTEX_UNLOCK (&rcfile_mutex
);
3521 else if (strcasecmp (name
, "disable-pinentry") == 0)
3525 if (value
&& *value
)
3527 n
= (int) strtol (value
, &tmp
, 10);
3528 if (*tmp
|| n
< 0 || n
> 1)
3529 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3533 client
->flags
|= FLAG_NO_PINENTRY
;
3535 client
->flags
&= ~FLAG_NO_PINENTRY
;
3540 if (client
->flags
& FLAG_NO_PINENTRY
)
3541 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3544 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3548 return send_error (ctx
, rc
);
3553 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3557 if (!rc
&& use_agent
&& agent
)
3559 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3564 return send_error (ctx
, rc
);
3568 do_rename (assuan_context_t ctx
, char *line
)
3570 struct client_s
*client
= assuan_get_pointer (ctx
);
3572 char **req
, **src
, *dst
;
3575 req
= str_split (line
, " ", 0);
3577 if (!req
|| !req
[0] || !req
[1])
3580 return GPG_ERR_SYNTAX
;
3584 is_literal_element (&dst
);
3586 if (!valid_xml_element ((xmlChar
*) dst
))
3589 return GPG_ERR_INV_VALUE
;
3592 if (strchr (req
[0], '\t'))
3593 src
= str_split (req
[0], "\t", 0);
3595 src
= str_split (req
[0], " ", 0);
3599 rc
= GPG_ERR_SYNTAX
;
3603 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3608 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3609 NULL
, 0, 0, NULL
, 0);
3614 rc
= is_element_owner (client
, n
);
3618 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3621 rc
= GPG_ERR_ENOMEM
;
3625 /* To prevent unwanted effects:
3627 * <root name="a"><b/></root>
3631 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3634 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3644 for (p
= src
; *p
; p
++)
3648 strv_printf (&tmp
, "%s", *p
);
3652 strv_printf (&tmp
, "!%s", dst
);
3653 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3654 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3661 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3662 NULL
, NULL
, 0, 0, NULL
, 0);
3665 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3670 /* Target may exist:
3673 * <root name="b" target="a"/>
3682 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3688 rc
= is_element_owner (client
, ndst
);
3692 unlink_node (client
, ndst
);
3693 xmlFreeNodeList (ndst
);
3696 rc
= add_attribute (client
, n
, "_name", dst
);
3705 rename_command (assuan_context_t ctx
, char *line
)
3707 struct client_s
*client
= assuan_get_pointer (ctx
);
3709 struct argv_s
*args
[] = {
3710 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3714 rc
= parse_options (&line
, args
, client
, 1);
3715 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3716 rc
= GPG_ERR_SYNTAX
;
3718 return send_error (ctx
, rc
);
3720 if (client
->opts
& OPT_INQUIRE
)
3722 unsigned char *result
;
3725 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3727 return send_error (ctx
, rc
);
3729 pthread_cleanup_push (xfree
, result
);
3730 rc
= do_rename (ctx
, (char *)result
);
3731 pthread_cleanup_pop (1);
3734 rc
= do_rename (ctx
, line
);
3736 return send_error (ctx
, rc
);
3740 do_copy (assuan_context_t ctx
, char *line
)
3742 struct client_s
*client
= assuan_get_pointer (ctx
);
3744 char **req
, **src
= NULL
, **dst
= NULL
;
3745 xmlNodePtr nsrc
, ndst
, new = NULL
;
3747 req
= str_split (line
, " ", 0);
3748 if (!req
|| !req
[0] || !req
[1])
3751 return GPG_ERR_SYNTAX
;
3754 if (strchr (req
[0], '\t'))
3755 src
= str_split (req
[0], "\t", 0);
3757 src
= str_split (req
[0], " ", 0);
3761 rc
= GPG_ERR_SYNTAX
;
3765 if (strchr (req
[1], '\t'))
3766 dst
= str_split (req
[1], "\t", 0);
3768 dst
= str_split (req
[1], " ", 0);
3772 rc
= GPG_ERR_SYNTAX
;
3776 if (!valid_element_path (dst
, 0))
3778 rc
= GPG_ERR_INV_VALUE
;
3782 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3787 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3788 NULL
, NULL
, 0, 0, NULL
, 0);
3793 new = xmlCopyNodeList (nsrc
);
3796 rc
= GPG_ERR_ENOMEM
;
3801 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3802 if (rc
== GPG_ERR_EACCES
)
3808 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3809 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3816 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3818 else if (!ndst
|| create
)
3820 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3825 rc
= is_element_owner (client
, ndst
);
3829 /* Merge any attributes from the src node to the initial dst node. */
3830 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3832 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3835 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3839 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3840 xmlNewProp (ndst
, attr
->name
, tmp
);
3842 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3845 xmlNodePtr n
= ndst
->children
;
3847 xmlFreeNodeList (n
);
3848 ndst
->children
= NULL
;
3852 n
= xmlCopyNodeList (new->children
);
3855 rc
= GPG_ERR_ENOMEM
;
3859 n
= xmlAddChildList (ndst
, n
);
3862 rc
= GPG_ERR_ENOMEM
;
3866 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3867 ndst
->parent
? ndst
: ndst
->parent
);
3873 xmlUnlinkNode (new);
3874 xmlFreeNodeList (new);
3890 copy_command (assuan_context_t ctx
, char *line
)
3892 struct client_s
*client
= assuan_get_pointer (ctx
);
3894 struct argv_s
*args
[] = {
3895 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3899 rc
= parse_options (&line
, args
, client
, 1);
3900 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3901 rc
= GPG_ERR_SYNTAX
;
3903 return send_error (ctx
, rc
);
3905 if (client
->opts
& OPT_INQUIRE
)
3907 unsigned char *result
;
3910 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3912 return send_error (ctx
, rc
);
3914 pthread_cleanup_push (xfree
, result
);
3915 rc
= do_copy (ctx
, (char *)result
);
3916 pthread_cleanup_pop (1);
3919 rc
= do_copy (ctx
, line
);
3921 return send_error (ctx
, rc
);
3925 do_move (assuan_context_t ctx
, char *line
)
3927 struct client_s
*client
= assuan_get_pointer (ctx
);
3929 char **req
, **src
= NULL
, **dst
= NULL
;
3930 xmlNodePtr nsrc
, ndst
= NULL
;
3932 req
= str_split (line
, " ", 0);
3934 if (!req
|| !req
[0] || !req
[1])
3937 return GPG_ERR_SYNTAX
;
3940 if (strchr (req
[0], '\t'))
3941 src
= str_split (req
[0], "\t", 0);
3943 src
= str_split (req
[0], " ", 0);
3947 rc
= GPG_ERR_SYNTAX
;
3951 if (strchr (req
[1], '\t'))
3952 dst
= str_split (req
[1], "\t", 0);
3954 dst
= str_split (req
[1], " ", 0);
3956 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3961 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3962 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3967 rc
= is_element_owner (client
, nsrc
);
3973 if (!valid_element_path (dst
, 0))
3975 rc
= GPG_ERR_INV_VALUE
;
3979 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3984 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3985 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3988 ndst
= xmlDocGetRootElement (client
->doc
);
3990 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3994 rc
= GPG_ERR_CONFLICT
;
3999 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
4006 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
4008 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
4020 if (ndst
== xmlDocGetRootElement (client
->doc
))
4022 xmlNodePtr n
= nsrc
;
4025 while (n
->parent
&& n
->parent
!= ndst
)
4028 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
4029 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
4031 if (xmlStrEqual (a
, b
))
4034 xmlUnlinkNode (nsrc
);
4036 xmlFreeNodeList (n
);
4044 xmlUnlinkNode (dup
);
4045 xmlFreeNodeList (dup
);
4049 xmlUnlinkNode (dup
);
4055 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
4057 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
4058 && !strcmp ((char *) name
, *dst
))
4061 rc
= GPG_ERR_CONFLICT
;
4066 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
4074 update_element_mtime (client
, nsrc
->parent
);
4075 xmlUnlinkNode (nsrc
);
4076 ndst
= xmlAddChildList (ndst
, nsrc
);
4079 rc
= GPG_ERR_ENOMEM
;
4081 update_element_mtime (client
, ndst
->parent
);
4097 move_command (assuan_context_t ctx
, char *line
)
4099 struct client_s
*client
= assuan_get_pointer (ctx
);
4101 struct argv_s
*args
[] = {
4102 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
4106 rc
= parse_options (&line
, args
, client
, 1);
4107 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
4108 rc
= GPG_ERR_SYNTAX
;
4110 return send_error (ctx
, rc
);
4112 if (client
->opts
& OPT_INQUIRE
)
4114 unsigned char *result
;
4117 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
4119 return send_error (ctx
, rc
);
4121 pthread_cleanup_push (xfree
, result
);
4122 rc
= do_move (ctx
, (char *)result
);
4123 pthread_cleanup_pop (1);
4126 rc
= do_move (ctx
, line
);
4128 return send_error (ctx
, rc
);
4132 ls_command (assuan_context_t ctx
, char *line
)
4140 return send_error (ctx
, GPG_ERR_SYNTAX
);
4142 tmp
= str_asprintf ("%s/data", homedir
);
4143 dir
= expand_homedir (tmp
);
4146 rc
= gpg_error_from_errno (errno
);
4151 return send_error (ctx
, rc
);
4155 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
4156 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
4160 pthread_cleanup_push (xfree
, p
);
4161 pthread_cleanup_push ((void *)(void *)closedir
, d
);
4164 while (!readdir_r (d
, p
, &cur
) && cur
)
4166 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
4168 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
4169 && cur
->d_name
[2] == '\0')
4172 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
4179 rc
= GPG_ERR_ENOMEM
;
4187 pthread_cleanup_pop (1); // closedir (d)
4188 pthread_cleanup_pop (1); // xfree (p)
4191 return send_error (ctx
, rc
);
4194 return send_error (ctx
, GPG_ERR_NO_DATA
);
4196 list
[strlen (list
) - 1] = 0;
4197 pthread_cleanup_push (xfree
, list
);
4198 rc
= xfer_data (ctx
, list
, strlen (list
));
4199 pthread_cleanup_pop (1);
4200 return send_error (ctx
, rc
);
4204 bye_notify (assuan_context_t ctx
, char *line
)
4206 struct client_s
*cl
= assuan_get_pointer (ctx
);
4208 cl
->thd
->state
= CLIENT_STATE_DISCON
;
4211 if (cl
->thd
->remote
)
4217 struct timeval tv
= { 0, 50000 };
4219 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
4220 if (rc
== GNUTLS_E_AGAIN
)
4221 select (0, NULL
, NULL
, NULL
, &tv
);
4223 while (rc
== GNUTLS_E_AGAIN
);
4227 /* This will let assuan_process_next() return. */
4228 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
4230 cl
->last_rc
= gpg_error_from_errno (errno
);
4234 cl
->last_rc
= 0; // BYE command result
4239 reset_notify (assuan_context_t ctx
, char *line
)
4241 struct client_s
*client
= assuan_get_pointer (ctx
);
4244 cleanup_client (client
);
4250 * This is called before every Assuan command.
4253 command_startup (assuan_context_t ctx
, const char *name
)
4255 struct client_s
*client
= assuan_get_pointer (ctx
);
4257 struct command_table_s
*cmd
= NULL
;
4259 log_write1 ("command='%s'", name
);
4260 client
->last_rc
= client
->opts
= 0;
4262 for (int i
= 0; command_table
[i
]; i
++)
4264 if (!strcasecmp (name
, command_table
[i
]->name
))
4266 if (command_table
[i
]->ignore_startup
)
4268 cmd
= command_table
[i
];
4274 return GPG_ERR_UNKNOWN_COMMAND
;
4276 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4278 update_client_state (client
, CLIENT_STATE_COMMAND
);
4284 * This is called after every Assuan command.
4287 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4289 struct client_s
*client
= assuan_get_pointer (ctx
);
4291 if (!(client
->flags
& FLAG_LOCK_CMD
))
4292 unlock_file_mutex (client
, 0);
4294 unlock_flock (&client
->flock_fd
);
4295 log_write1 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4296 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
4298 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4300 update_client_state (client
, CLIENT_STATE_IDLE
);
4304 help_command (assuan_context_t ctx
, char *line
)
4309 if (!line
|| !*line
)
4312 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
4313 "For commands that take an element path as an argument, each element is "
4314 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4317 for (i
= 0; command_table
[i
]; i
++)
4319 if (!command_table
[i
]->help
)
4322 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4327 tmp
= strip_texi_and_wrap (help
);
4329 pthread_cleanup_push (xfree
, tmp
);
4330 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4331 pthread_cleanup_pop (1);
4332 return send_error (ctx
, rc
);
4335 for (i
= 0; command_table
[i
]; i
++)
4337 if (!strcasecmp (line
, command_table
[i
]->name
))
4341 if (!command_table
[i
]->help
)
4344 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4345 tmp
= str_asprintf (_("Usage: %s"), help
);
4347 pthread_cleanup_push (xfree
, tmp
);
4348 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4349 pthread_cleanup_pop (1);
4350 return send_error (ctx
, rc
);
4354 return send_error (ctx
, GPG_ERR_INV_NAME
);
4358 new_command (const char *name
, int ignore
, int unlock
, int flock_type
,
4359 gpg_error_t (*handler
) (assuan_context_t
, char *),
4365 for (i
= 0; command_table
[i
]; i
++);
4368 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4369 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4370 command_table
[i
]->name
= name
;
4371 command_table
[i
]->handler
= handler
;
4372 command_table
[i
]->ignore_startup
= ignore
;
4373 command_table
[i
]->unlock
= unlock
;
4374 command_table
[i
]->flock_type
= flock_type
;
4375 command_table
[i
++]->help
= help
;
4376 command_table
[i
] = NULL
;
4384 for (i
= 0; command_table
[i
]; i
++)
4385 xfree (command_table
[i
]);
4387 xfree (command_table
);
4391 sort_commands (const void *arg1
, const void *arg2
)
4393 struct command_table_s
*const *a
= arg1
;
4394 struct command_table_s
*const *b
= arg2
;
4403 return strcmp ((*a
)->name
, (*b
)->name
);
4407 passwd_command (assuan_context_t ctx
, char *line
)
4409 struct client_s
*client
= assuan_get_pointer (ctx
);
4411 struct argv_s
*args
[] = {
4412 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4413 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4414 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4418 rc
= peer_is_invoker (client
);
4419 if (rc
== GPG_ERR_EACCES
)
4420 return send_error (ctx
, rc
);
4422 if (client
->flags
& FLAG_NEW
)
4423 return send_error (ctx
, GPG_ERR_INV_STATE
);
4425 client
->crypto
->save
.hdr
.s2k_count
=
4426 config_get_ulonglong (client
->filename
, "s2k_count");
4427 rc
= parse_options (&line
, args
, client
, 0);
4429 return send_error (ctx
, rc
);
4431 if (!rc
&& client
->opts
& OPT_RESET
)
4433 rc
= cache_clear (client
->md5file
);
4435 send_status_all (STATUS_CACHE
, NULL
);
4440 if (!IS_PKI (client
->crypto
))
4442 struct crypto_s
*crypto
;
4444 xfree (client
->crypto
->filename
);
4445 client
->crypto
->filename
= str_dup (client
->filename
);
4446 rc
= change_passwd (ctx
, client
->filename
,
4447 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4448 (client
->opts
& OPT_NO_PASSPHRASE
));
4451 cleanup_crypto (&client
->crypto
);
4452 client
->crypto
= crypto
;
4453 update_checksum (client
);
4454 cleanup_crypto_stage1 (client
->crypto
);
4460 if (client
->crypto
->save
.hdr
.s2k_count
)
4461 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4462 "OPTION s2k-count=%lu",
4463 client
->crypto
->save
.hdr
.s2k_count
);
4466 rc
= agent_passwd (client
->crypto
);
4468 (void) kill_scd (client
->crypto
->agent
);
4473 return send_error (ctx
, rc
);
4477 parse_keygrip_opt_sign (void *data
, void *value
)
4479 struct client_s
*client
= data
;
4482 client
->opts
|= OPT_SIGN
;
4487 keygrip_command (assuan_context_t ctx
, char *line
)
4489 struct client_s
*client
= assuan_get_pointer (ctx
);
4491 struct crypto_s
*crypto
= NULL
;
4492 struct argv_s
*args
[] = {
4493 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4497 if (!line
|| !*line
)
4498 return send_error (ctx
, GPG_ERR_SYNTAX
);
4500 rc
= parse_options (&line
, args
, client
, 1);
4502 return send_error (ctx
, rc
);
4504 if (!valid_filename (line
))
4505 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4507 rc
= init_client_crypto (&crypto
);
4509 return send_error (ctx
, rc
);
4511 rc
= read_data_file (line
, crypto
);
4514 char *hexgrip
= NULL
;
4516 if (!IS_PKI (crypto
))
4518 cleanup_crypto (&crypto
);
4519 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4522 if (client
->opts
& OPT_SIGN
)
4524 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4525 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4529 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4532 rc
= GPG_ERR_ENOMEM
;
4534 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4539 UPDATE_AGENT_CTX (client
, crypto
);
4540 cleanup_crypto (&crypto
);
4541 return send_error (ctx
, rc
);
4545 parse_opt_data (void *data
, void *value
)
4547 struct client_s
*client
= data
;
4550 client
->opts
|= OPT_DATA
;
4555 send_client_list (assuan_context_t ctx
)
4557 struct client_s
*client
= assuan_get_pointer (ctx
);
4559 char buf
[ASSUAN_LINELENGTH
];
4561 if (client
->opts
& OPT_VERBOSE
)
4567 MUTEX_LOCK (&cn_mutex
);
4568 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4569 t
= slist_length (cn_thread_list
);
4571 for (i
= 0; i
< t
; i
++)
4573 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4576 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4579 tmp
= build_client_info_line (thd
, 0);
4582 char **l
= strv_cat (list
, tmp
);
4584 rc
= GPG_ERR_ENOMEM
;
4589 rc
= GPG_ERR_ENOMEM
;
4598 pthread_cleanup_pop (1);
4602 line
= strv_join ("\n", list
);
4604 pthread_cleanup_push (xfree
, line
);
4605 rc
= xfer_data (ctx
, line
, strlen (line
));
4606 pthread_cleanup_pop (1);
4610 if (client
->opts
& OPT_DATA
)
4612 MUTEX_LOCK (&cn_mutex
);
4613 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4614 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4615 pthread_cleanup_pop (1);
4616 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4619 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4625 getinfo_command (assuan_context_t ctx
, char *line
)
4627 struct client_s
*client
= assuan_get_pointer (ctx
);
4629 char buf
[ASSUAN_LINELENGTH
];
4630 struct argv_s
*args
[] = {
4631 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4632 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4636 rc
= parse_options (&line
, args
, client
, 1);
4638 return send_error (ctx
, rc
);
4640 if (!strcasecmp (line
, "clients"))
4642 rc
= send_client_list (ctx
);
4644 else if (!strcasecmp (line
, "cache"))
4646 if (client
->opts
& OPT_DATA
)
4648 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4649 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4652 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4654 else if (!strcasecmp (line
, "pid"))
4657 pid_t pid
= getpid ();
4659 snprintf (buf
, sizeof (buf
), "%i", pid
);
4660 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4662 else if (!strcasecmp (line
, "version"))
4664 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4671 "", use_agent
? "AGENT" : "");
4672 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4675 else if (!strcasecmp (line
, "last_error"))
4677 if (client
->last_error
)
4678 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4680 rc
= GPG_ERR_NO_DATA
;
4682 else if (!strcasecmp (line
, "user"))
4687 if (client
->thd
->remote
)
4688 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4690 user
= get_username (client
->thd
->peer
->uid
);
4692 user
= get_username (client
->thd
->peer
->uid
);
4696 pthread_cleanup_push (xfree
, user
);
4697 rc
= xfer_data (ctx
, user
, strlen (user
));
4698 pthread_cleanup_pop (1);
4701 rc
= GPG_ERR_NO_DATA
;
4704 rc
= gpg_error (GPG_ERR_SYNTAX
);
4706 return send_error (ctx
, rc
);
4711 send_data_cb (void *user
, const void *buf
, size_t len
)
4713 assuan_context_t ctx
= user
;
4715 return assuan_send_data (ctx
, buf
, len
);
4719 send_status_cb (void *user
, const char *line
)
4721 assuan_context_t ctx
= user
;
4722 char keyword
[200], *k
;
4725 for (p
= line
, k
= keyword
; *p
; p
++)
4737 while (isspace (*p
))
4740 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4745 kill_command (assuan_context_t ctx
, char *line
)
4747 struct client_s
*client
= assuan_get_pointer (ctx
);
4751 if (!line
|| !*line
)
4752 return send_error (ctx
, GPG_ERR_SYNTAX
);
4754 MUTEX_LOCK (&cn_mutex
);
4755 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4756 t
= slist_length (cn_thread_list
);
4759 for (i
= 0; i
< t
; i
++)
4761 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4762 char *tmp
= str_asprintf ("%p", thd
->tid
);
4764 if (strcmp (line
, tmp
))
4771 rc
= peer_is_invoker (client
);
4774 #ifdef HAVE_PTHREAD_CANCEL
4775 rc
= pthread_cancel (thd
->tid
);
4783 rc
= GPG_ERR_EACCES
;
4784 if (config_get_boolean ("global", "strict_kill"))
4788 if (client
->thd
->remote
&& thd
->remote
)
4790 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
4792 #ifdef HAVE_PTHREAD_CANCEL
4793 rc
= pthread_cancel (thd
->tid
);
4801 else if (!client
->thd
->remote
&& !thd
->remote
)
4804 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
4806 #ifdef HAVE_PTHREAD_CANCEL
4807 rc
= pthread_cancel (thd
->tid
);
4817 pthread_cleanup_pop (1);
4818 return send_error (ctx
, rc
);
4822 agent_command (assuan_context_t ctx
, char *line
)
4827 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4830 struct client_s
*client
= assuan_get_pointer (ctx
);
4832 if (!line
|| !*line
)
4833 return send_error (ctx
, GPG_ERR_SYNTAX
);
4835 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4836 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4837 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4838 send_status_cb
, client
->ctx
);
4839 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4844 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4847 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4849 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4853 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4855 return send_error (ctx
, rc
);
4861 /* !BEGIN-HELP-TEXT!
4863 * This comment is used as a marker to generate the offline documentation
4864 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4865 * script to determine where commands begin and end.
4867 new_command("HELP", 1, 1, 0, help_command
, _(
4868 "HELP [<COMMAND>]\n"
4869 "Show available commands or command specific help text."
4872 new_command("AGENT", 1, 1, 0, agent_command
, _(
4874 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4875 "@command{gpg-agent}."
4878 new_command("KILL", 1, 0, 0, kill_command
, _(
4879 "KILL <thread_id>\n"
4880 "Terminates the client identified by @var{thread_id} and releases any file "
4881 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4882 "for details about listing connected clients. The @code{invoking_user} "
4883 "(@pxref{Configuration}) may kill any client while others may only kill "
4884 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4887 new_command("GETINFO", 1, 1, 0, getinfo_command
, _(
4888 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4889 "Get server and other information: @var{CACHE} returns the number of cached "
4890 "documents via a status message. @var{CLIENTS} returns the number of "
4891 "connected clients via a status message or a list of connected clients when "
4892 "the @option{--verbose} parameter is used. The list contains space delimited "
4893 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4894 "file lock status, whether the current client is self, client state, "
4895 "user ID or TLS fingerprint of the connected client and username if the "
4896 "client is a local one. "
4897 "Client state @code{0} is an unknown client state, @code{1} indicates the "
4898 "client has connected but hasn't completed initializing, @code{2} indicates "
4899 "that the client is idle, @code{3} means the "
4900 "client is in a command and @code{4} means the client is disconnecting. This "
4901 "line is always returned with a data response. @var{PID} returns the process "
4902 "ID number of the server via a data response. @var{VERSION} returns the server "
4903 "version number and compile-time features with a data response with each "
4904 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4905 "the last failed command when available. @var{USER} returns the username or "
4906 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4908 "When the @option{--data} option is specified then the result will be sent "
4909 "via a data response rather than a status message."
4912 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
4913 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4914 "Changes the passphrase of the secret key required to open the current "
4915 "file or the passphrase of a symmetrically encrypted data file. When the "
4916 "@option{--reset} option is passed then the cache entry for the current "
4917 "file will be reset and the passphrase, if any, will be required during the "
4918 "next @code{OPEN} (@pxref{OPEN})."
4920 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4921 "of hash iterations for a passphrase and must be either @code{0} to use "
4922 "the calibrated count of the machine (the default), or a value greater than "
4923 "or equal to @code{65536}. This option has no effect for symmetrically "
4924 "encrypted data files."
4926 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4927 "the data file, although a passphrase may be required when changing it."
4929 "This command is not available for non-invoking clients "
4930 "(@pxref{Access Control})."
4933 new_command("KEYGRIP", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, keygrip_command
, _(
4934 "KEYGRIP [--sign] <filename>\n"
4935 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4938 "When the @option{--sign} option is specified then the key used for signing "
4939 "of the specified @var{filename} will be returned."
4941 "For symmetrically encrypted data files this command returns the error "
4942 "GPG_ERR_NOT_SUPPORTED."
4945 new_command("OPEN", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
4946 "OPEN [--lock] <filename> [<passphrase>]\n"
4947 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4948 "found on the file-system then a new document will be created. If the file "
4949 "is found, it is looked for in the file cache. If cached and no "
4950 "@var{passphrase} was specified then the cached document is opened. When not "
4951 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4952 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4955 "When the @option{--lock} option is passed then the file mutex will be "
4956 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4957 "file has been opened."
4960 new_command("SAVE", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
4961 "SAVE [--no-passphrase] [--reset] [--ask] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4962 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4963 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4964 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4965 "keypair will be generated and a pinentry will be used to prompt for the "
4966 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4967 "passed in which case the data file will not be passphrase protected. "
4969 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4970 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4971 "use is enabled. The datafile will be symmetrically encrypted and will not "
4972 "use or generate any keypair."
4974 "The @option{--reset} option will clear the cache entry for the current file "
4975 "and require a passphrase, if needed, before saving."
4977 "The @option{--ask} option will prompt for the passphrase of the current file, "
4978 "if needed, before saving. This differs from the @option{--reset} option by "
4979 "keeping the cache entry in case of an invalid passphrase or some other failure "
4980 "which may otherwise cause a denial of service for other clients."
4982 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4983 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4984 "(@pxref{Configuration}) for available ciphers."
4986 "The @option{--cipher-iterations} option specifies the number of times to "
4987 "hash the passphrase before encrypting the XML data. The default is "
4988 "@code{5000000}. This option is an alias for @option{--s2k-count} since "
4989 "version @var{3.0.15} of @command{pwmd}."
4991 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4992 "the client to obtain the key paramaters to use when generating a new "
4993 "keypair. The inquired data is expected to be an S-expression. If not "
4994 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4995 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4996 "that when this option is specified a new keypair will be generated "
4997 "reguardless if the file is a new one and that if the data file is protected "
4998 "the passphrase to open it will be required before generating the new "
4999 "keypair. This option is not available for non-invoking clients "
5000 "(@pxref{Access Control})."
5002 "You can encrypt the data file to a public key other than the one that it "
5003 "was originally encrypted with by passing the @option{--keygrip} option with "
5004 "the hex encoded keygrip of the public key as its argument. The keygrip may "
5005 "be of any key that @command{gpg-agent} knows about. The "
5006 "@option{--sign-keygrip} option may also be used to sign with an alternate "
5007 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
5008 "keygrip of an existing data file. This option may be needed when using a "
5009 "smartcard. This option has no effect with symmetrically encrypted data "
5010 "files. These options are not available for non-invoking clients "
5011 "(@pxref{Access Control})."
5013 "The @option{--s2k-count} option sets number of hash iterations for a "
5014 "passphrase. A value less-than @code{65536} will use the machine calibrated "
5015 "value and is the default when using @command{gpg-agent}. This setting only "
5016 "affects new files when using @command{gpg-agent}. To change the setting use "
5017 "the @code{PASSWD} command (@pxref{PASSWD}). This option is an alias for "
5018 "option @option{--cipher-iterations} when not using @command{gpg-agent}."
5021 new_command("ISCACHED", 1, 0, 0, iscached_command
, _(
5022 "ISCACHED [--lock] <filename>\n"
5023 "An @emph{OK} response is returned if the specified @var{filename} is found "
5024 "in the file cache. If not found in the cache but exists on the filesystem "
5025 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
5028 "The @option{lock} option will lock the file mutex of @var{filename} when the "
5029 "file exists; it does not need to be opened nor cached. The lock will be "
5030 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
5034 new_command("CLEARCACHE", 1, 1, 0, clearcache_command
, _(
5035 "CLEARCACHE [<filename>]\n"
5036 "Clears a file cache entry for all or the specified @var{filename}."
5039 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command
, _(
5040 "CACHETIMEOUT <filename> <seconds>\n"
5041 "The time in @var{seconds} until @var{filename} will be removed from the "
5042 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
5043 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
5044 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
5048 new_command("LIST", 0, 1, 0, list_command
, _(
5049 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
5050 "If no element path is given then a newline separated list of root elements "
5051 "is returned with a data response. If given, then all reachable elements "
5052 "of the specified element path are returned unless the @option{--no-recurse} "
5053 "option is specified. If specified, only the child elements of the element "
5054 "path are returned without recursing into grandchildren. Each resulting "
5055 "element is prefixed with the literal @code{!} character when the element "
5056 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
5058 "When the @option{--verbose} option is passed then each element path "
5059 "returned will have zero or more flags appened to it. These flags are "
5060 "delimited from the element path by a single space character. A flag itself "
5061 "is a single character. Flag @code{P} indicates that access to the element "
5062 "is denied. Flag @code{+} indicates that there are child nodes of "
5063 "the current element path. Flag @code{E} indicates that an element of an "
5064 "element path contained in a @var{target} attribute could not be found. Flag "
5065 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
5066 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
5067 "of the @var{target} attribute contained in the current element (see below)."
5069 "The @option{--with-target} option implies @option{--verbose} and will append "
5070 "an additional flag @code{T} followed by a single space then an element path. "
5071 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
5072 "current element when it contains a @var{target} attribute. When no "
5073 "@var{target} attribute is found then no flag will be appended."
5075 "The @option{--no-recurse} option limits the amount of data returned to only "
5076 "the listing of children of the specified element path and not any "
5079 "The @option{--all} option lists the entire element tree for each root "
5080 "element. This option also implies option @option{--verbose}."
5082 "When the @option{--inquire} option is passed then all remaining non-option "
5083 "arguments are retrieved via a server @emph{INQUIRE}."
5086 new_command("REALPATH", 0, 1, 0, realpath_command
, _(
5087 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
5088 "Resolves all @code{target} attributes of the specified element path and "
5089 "returns the result with a data response. @xref{Target Attribute}, for details."
5091 "When the @option{--inquire} option is passed then all remaining non-option "
5092 "arguments are retrieved via a server @emph{INQUIRE}."
5095 new_command("STORE", 0, 1, 0, store_command
, _(
5096 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
5097 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5099 "Creates a new element path or modifies the @var{content} of an existing "
5100 "element. If only a single element is specified then a new root element is "
5101 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
5102 "set to the final @key{TAB} delimited element. If no @var{content} is "
5103 "specified after the final @key{TAB}, then the content of an existing "
5104 "element will be removed; or empty when creating a new element."
5106 "The only restriction of an element name is that it not contain whitespace "
5107 "or begin with the literal element character @code{!} unless specifying a "
5108 "literal element (@pxref{Target Attribute}). There is no whitespace between "
5109 "the @key{TAB} delimited elements. It is recommended that the content of an "
5110 "element be base64 encoded when it contains control or @key{TAB} characters "
5111 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
5114 new_command("RENAME", 0, 1, 0, rename_command
, _(
5115 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
5116 "Renames the specified @var{element} to the new @var{value}. If an element of "
5117 "the same name as the @var{value} already exists it will be overwritten."
5119 "When the @option{--inquire} option is passed then all remaining non-option "
5120 "arguments are retrieved via a server @emph{INQUIRE}."
5123 new_command("COPY", 0, 1, 0, copy_command
, _(
5124 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
5125 "Copies the entire element tree starting from the child node of the source "
5126 "element, to the destination element path. If the destination element path "
5127 "does not exist then it will be created; otherwise it is overwritten."
5129 "Note that attributes from the source element are merged into the "
5130 "destination element when the destination element path exists. When an "
5131 "attribute of the same name exists in both the source and destination "
5132 "elements then the destination attribute will be updated to the source "
5135 "When the @option{--inquire} option is passed then all remaining non-option "
5136 "arguments are retrieved via a server @emph{INQUIRE}."
5139 new_command("MOVE", 0, 1, 0, move_command
, _(
5140 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
5141 "Moves the source element path to the destination element path. If the "
5142 "destination is not specified then it will be moved to the root node of the "
5143 "document. If the destination is specified and exists then it will be "
5144 "overwritten; otherwise non-existing elements of the destination element "
5145 "path will be created."
5147 "When the @option{--inquire} option is passed then all remaining non-option "
5148 "arguments are retrieved via a server @emph{INQUIRE}."
5151 new_command("DELETE", 0, 1, 0, delete_command
, _(
5152 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
5153 "Removes the specified element path and all of its children. This may break "
5154 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
5155 "refers to this element or any of its children."
5157 "When the @option{--inquire} option is passed then all remaining non-option "
5158 "arguments are retrieved via a server @emph{INQUIRE}."
5161 new_command("GET", 0, 1, 0, get_command
, _(
5162 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
5163 "Retrieves the content of the specified element. The content is returned "
5164 "with a data response."
5166 "When the @option{--inquire} option is passed then all remaining non-option "
5167 "arguments are retrieved via a server @emph{INQUIRE}."
5170 new_command("ATTR", 0, 1, 0, attr_command
, _(
5171 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
5173 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
5175 " Stores or updates an @var{attribute} name and optional @var{value} of an "
5176 "element. When no @var{value} is specified any existing value will be removed."
5178 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
5180 " Removes an @var{attribute} from an element."
5182 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
5184 " Retrieves a newline separated list of attributes names and values "
5185 "from the specified element. Each attribute name and value is space delimited."
5187 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
5189 " Retrieves the value of an @var{attribute} from an element."
5192 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
5193 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
5196 "The @code{_mtime} attribute is updated each time an element is modified by "
5197 "either storing content, editing attributes or by deleting a child element. "
5198 "The @code{_ctime} attribute is created for each new element in an element "
5201 "When the @option{--inquire} option is passed then all remaining non-option "
5202 "arguments are retrieved via a server @emph{INQUIRE}."
5204 "@xref{Target Attribute}, for details about this special attribute."
5207 new_command("XPATH", 0, 1, 0, xpath_command
, _(
5208 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
5209 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
5210 "specified it is assumed the expression is a request to return a result. "
5211 "Otherwise, the result is set to the @var{value} argument and the document is "
5212 "updated. If there is no @var{value} after the @key{TAB} character, the value "
5213 "is assumed to be empty and the document is updated. For example:"
5216 "XPATH //element[@@_name='password']@key{TAB}\n"
5219 "would clear the content of all @code{password} elements in the data file "
5220 "while leaving off the trailing @key{TAB} would return all @code{password} "
5221 "elements in @abbr{XML} format."
5223 "When the @option{--inquire} option is passed then all remaining non-option "
5224 "arguments are retrieved via a server @emph{INQUIRE}."
5226 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5227 "expression syntax."
5230 new_command("XPATHATTR", 0, 1, 0, xpathattr_command
, _(
5231 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
5232 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
5233 "attributes and does not return a result. For the @var{SET} operation the "
5234 "@var{value} is optional but the field is required. If not specified then "
5235 "the attribute value will be empty. For example:"
5238 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
5241 "would create an @code{password} attribute for each @code{password} element "
5242 "found in the document. The attribute value will be empty but still exist."
5244 "When the @option{--inquire} option is passed then all remaining non-option "
5245 "arguments are retrieved via a server @emph{INQUIRE}."
5247 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5248 "expression syntax."
5251 new_command("IMPORT", 0, 1, 0, import_command
, _(
5252 "IMPORT [--root=[!]element[<TAB>[!]child[..]]] <content>\n"
5253 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5255 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
5256 "argument is raw @abbr{XML} data. The content is created as a child of "
5257 "the element path specified with the @option{--root} option or at the "
5258 "document root when not specified. Existing elements of the same name will "
5261 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
5265 new_command("DUMP", 0, 1, 0, dump_command
, _(
5267 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
5268 "dumping a specific node."
5271 new_command("LOCK", 0, 0, 0, lock_command
, _(
5273 "Locks the mutex associated with the opened file. This prevents other clients "
5274 "from sending commands to the same opened file until the client "
5275 "that sent this command either disconnects or sends the @code{UNLOCK} "
5276 "command. @xref{UNLOCK}."
5279 new_command("UNLOCK", 1, 0, 0, unlock_command
, _(
5281 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
5282 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
5283 "@pxref{ISCACHED})."
5286 new_command("GETCONFIG", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, getconfig_command
, _(
5287 "GETCONFIG [filename] <parameter>\n"
5288 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
5289 "data response. If no file has been opened then the value for @var{filename} "
5290 "or the default from the @samp{global} section will be returned. If a file "
5291 "has been opened and no @var{filename} is specified, a value previously "
5292 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
5295 new_command("OPTION", 1, 1, 0, option_command
, _(
5296 "OPTION <NAME>=<VALUE>\n"
5297 "Sets a client option @var{name} to @var{value}. The value for an option is "
5298 "kept for the duration of the connection."
5301 "@item DISABLE-PINENTRY\n"
5302 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
5303 "server inquire is sent to the client to obtain the passphrase. This option "
5304 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
5305 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
5307 "@item PINENTRY-TIMEOUT\n"
5308 "Sets the number of seconds before a pinentry prompt will return an error "
5309 "while waiting for user input."
5312 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5315 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5318 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5320 "@item PINENTRY-DESC\n"
5321 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
5323 "@item PINENTRY-TITLE\n"
5324 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
5326 "@item PINENTRY-PROMPT\n"
5327 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
5330 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5332 "@item LC-MESSAGES\n"
5333 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5336 "Associates the thread ID of the connection with the specified textual "
5337 "representation. Useful for debugging log messages. May not contain whitespace."
5339 "@item LOCK-TIMEOUT\n"
5340 "When not @code{0}, the duration in tenths of a second to wait for the file "
5341 "mutex which has been locked by another thread to be released before returning "
5342 "an error. When @code{-1}, then an error will be returned immediately."
5346 new_command("LS", 1, 1, 0, ls_command
, _(
5348 "Lists the available data files stored in the data directory "
5349 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5352 new_command("RESET", 1, 1, 0, NULL
, _(
5354 "Closes the currently opened file but keeps any previously set client options."
5357 new_command("NOP", 1, 1, 0, NULL
, _(
5359 "Does nothing. Always returns successfully."
5362 /* !END-HELP-TEXT! */
5363 new_command ("CANCEL", 1, 1, 0, NULL
, NULL
);
5364 new_command ("END", 1, 1, 0, NULL
, NULL
);
5365 new_command ("BYE", 1, 1, 0, NULL
, NULL
);
5368 for (i
= 0; command_table
[i
]; i
++);
5369 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
5374 register_commands (assuan_context_t ctx
)
5378 for (; command_table
[i
]; i
++)
5380 if (!command_table
[i
]->handler
)
5383 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5384 command_table
[i
]->handler
,
5385 command_table
[i
]->help
);
5390 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5394 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5398 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5402 return assuan_register_post_cmd_notify (ctx
, command_finalize
);