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 /* Flags needed to be retained for a client across commands. */
52 #define FLAG_NEW 0x0001
53 #define FLAG_HAS_LOCK 0x0002
54 #define FLAG_LOCK_CMD 0x0004
55 #define FLAG_NO_PINENTRY 0x0008
56 #define FLAG_OPEN 0x0010
57 #define FLAG_KEEP_LOCK 0x0020
59 /* These are command option flags. */
60 #define OPT_INQUIRE 0x0001
61 #define OPT_NO_PASSPHRASE 0x0002
62 #define OPT_RESET 0x0004
63 #define OPT_LIST_RECURSE 0x0008
64 #define OPT_VERBOSE 0x0010
65 #define OPT_LOCK 0x0020
66 #define OPT_LOCK_ON_OPEN 0x0040
67 #define OPT_SIGN 0x0080
68 #define OPT_LIST_ALL 0x0100
69 #define OPT_LIST_WITH_TARGET 0x0200
70 #define OPT_DATA 0x0400
71 #define OPT_NO_AGENT 0x0800
74 /* The GETCONFIG command, for example, may fail because pwmd lost the
75 * gpg-agent connection. Update the recovered agent ctx. */
76 #define UPDATE_AGENT_CTX(client, crypto) do { \
77 if (crypto && crypto->agent && crypto->agent->did_restart \
78 && client->crypto && client->crypto->agent \
79 && client->crypto->agent->ctx && \
80 crypto->agent->ctx != client->crypto->agent->ctx) \
82 client->crypto->agent->ctx = crypto->agent->ctx; \
83 crypto->agent->ctx = NULL; \
87 #define UPDATE_AGENT_CTX(client, crypto)
90 struct command_table_s
93 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
96 int unlock
; // unlock the file mutex after validating the checksum
99 static struct command_table_s
**command_table
;
101 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
102 static gpg_error_t
validate_checksum (struct client_s
*,
103 struct cache_data_s
*);
104 static gpg_error_t
update_checksum (struct client_s
*client
);
106 /* When 'status' is true the 'self' field of the status line will be false
107 * because we never send the STATE status message to the same client that
110 build_client_info_line (struct client_thread_s
*thd
, int status
)
112 MUTEX_LOCK (&cn_mutex
);
113 int with_state
= config_get_boolean ("global", "send_state");
114 char *uid
= str_asprintf("%u", thd
->peer
->uid
);
115 char *line
= str_asprintf ("%p %s %s %s %u %u %u %s%s",
117 thd
->name
? thd
->name
: "-",
118 thd
->cl
&& thd
->cl
->filename
119 ? thd
->cl
->filename
: "/",
121 thd
->remote
? thd
->peeraddr
: "-",
125 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
126 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
127 with_state
? thd
->state
: CLIENT_STATE_UNKNOWN
,
129 thd
->remote
? "#" : "",
130 thd
->remote
? thd
->tls
->fp
: uid
137 MUTEX_UNLOCK (&cn_mutex
);
142 update_client_state (struct client_s
*client
, unsigned s
)
144 client
->thd
->state
= s
;
145 int n
= config_get_boolean ("global", "send_state");
147 if (n
&& client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
149 char *line
= build_client_info_line (client
->thd
, 1);
152 send_status_all_not_self (n
== 2, STATUS_STATE
, "%s", line
);
157 unlock_file_mutex (struct client_s
*client
, int remove
)
161 // OPEN: keep the lock for the same file being reopened.
162 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
165 if (!(client
->flags
& FLAG_HAS_LOCK
))
166 return GPG_ERR_NOT_LOCKED
;
168 rc
= cache_unlock_mutex (client
->md5file
, remove
);
170 rc
= GPG_ERR_INV_STATE
;
172 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
178 lock_file_mutex (struct client_s
*client
, int add
)
181 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
183 if (client
->flags
& FLAG_HAS_LOCK
)
186 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
187 client
->lock_timeout
, add
, timeout
);
189 client
->flags
|= FLAG_HAS_LOCK
;
195 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
199 if (!(client
->flags
& FLAG_OPEN
))
200 return GPG_ERR_INV_STATE
;
202 rc
= lock_file_mutex (client
, 0);
203 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
205 rc
= validate_checksum (client
, NULL
);
206 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
209 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
212 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
213 unlock_file_mutex (client
, 0);
219 parse_xml (assuan_context_t ctx
, int new)
221 struct client_s
*client
= assuan_get_pointer (ctx
);
222 int cached
= client
->doc
!= NULL
;
227 client
->doc
= new_document ();
233 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
234 client
->crypto
->plaintext
= result
;
235 client
->crypto
->plaintext_len
= len
;
236 if (!client
->crypto
->plaintext
)
238 xmlFreeDoc (client
->doc
);
244 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
245 client
->crypto
->plaintext_len
, (xmlDocPtr
*)&client
->doc
);
251 free_client (struct client_s
*client
)
254 xmlFreeDoc (client
->doc
);
257 xfree (client
->filename
);
258 xfree (client
->last_error
);
262 cleanup_crypto_stage2 (client
->crypto
);
263 if (client
->crypto
->pkey_sexp
)
264 gcry_sexp_release (client
->crypto
->pkey_sexp
);
266 if (client
->crypto
->sigpkey_sexp
)
267 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
269 client
->crypto
->pkey_sexp
= NULL
;
270 client
->crypto
->sigpkey_sexp
= NULL
;
271 memset (client
->crypto
->sign_grip
, 0,
272 sizeof (client
->crypto
->sign_grip
));
273 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
278 cleanup_client (struct client_s
*client
)
280 assuan_context_t ctx
= client
->ctx
;
281 struct client_thread_s
*thd
= client
->thd
;
282 struct crypto_s
*crypto
= client
->crypto
;
283 long lock_timeout
= client
->lock_timeout
;
284 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
285 struct pinentry_option_s pin_opts
;
286 xmlErrorPtr xml_error
= client
->xml_error
;
288 struct pinentry_option_s agent_pin_opts
;
290 if (crypto
&& crypto
->agent
)
291 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
292 sizeof(struct pinentry_option_s
));
295 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
296 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
297 free_client (client
);
298 memset (client
, 0, sizeof (struct client_s
));
299 client
->xml_error
= xml_error
;
300 client
->crypto
= crypto
;
303 client
->lock_timeout
= lock_timeout
;
304 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
305 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
307 if (crypto
&& crypto
->agent
)
308 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
309 sizeof(struct pinentry_option_s
));
314 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
316 struct client_s
*client
= assuan_get_pointer (ctx
);
318 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
319 int cached
= 0, keyarg
= key
? 1 : 0;
321 unsigned char *salted_key
= NULL
;
325 char *pin_title
= client
->pinentry_opts
.title
;
327 client
->crypto
->filename
= str_dup (client
->filename
);
328 if (cdata
|| client
->flags
& FLAG_NEW
)
330 if (cdata
&& !(client
->flags
& FLAG_NEW
))
332 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
337 cached
= cdata
!= NULL
;
341 if (!key
&& !IS_PKI (client
->crypto
)
342 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
344 if (client
->flags
& FLAG_NO_PINENTRY
)
346 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
353 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
356 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
363 if (!IS_PKI (client
->crypto
))
365 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
368 key
= gcry_malloc (keylen
);
369 memset (key
, 0, keylen
);
372 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
373 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
374 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
375 (void **)&salted_key
, &keysize
);
381 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
382 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
383 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
385 gcry_free (salted_key
);
389 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
394 if (client
->pinentry_opts
.title
!= pin_title
)
395 xfree (client
->pinentry_opts
.title
);
397 client
->pinentry_opts
.title
= pin_title
;
400 gcry_free (salted_key
);
404 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
407 gcry_free (salted_key
);
408 return GPG_ERR_ENOMEM
;
411 cdata
->key
= salted_key
;
412 cdata
->keylen
= keysize
;
415 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
420 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
422 free_cache_data_once (cdata
);
426 int timeout
= config_get_integer (client
->filename
,
432 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
434 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
435 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
436 client
->crypto
->plaintext_len
, &cdata
->doc
,
437 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
439 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
441 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
442 "%S", client
->crypto
->pkey_sexp
);
447 gcry_sexp_release (cdata
->sigkey
);
449 cdata
->sigkey
= NULL
;
450 if (!rc
&& IS_PKI (client
->crypto
))
451 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
452 "%S", client
->crypto
->sigpkey_sexp
);
456 rc
= cache_add_file (client
->md5file
,
457 (client
->flags
& FLAG_NEW
) ? NULL
458 : client
->crypto
->grip
, cdata
, timeout
);
463 free_cache_data_once (cdata
);
464 xmlFreeDoc (client
->doc
);
471 client
->flags
|= FLAG_OPEN
;
476 update_checksum (client
);
478 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
479 rc
= do_lock (client
, 0);
485 req_cleanup (void *arg
)
490 strv_free ((char **) arg
);
494 parse_open_opt_lock (void *data
, void *value
)
496 struct client_s
*client
= data
;
498 client
->opts
|= OPT_LOCK_ON_OPEN
;
503 parse_save_opt_inquire (void *data
, void *value
)
505 struct client_s
*client
= data
;
508 if (!(client
->flags
& FLAG_NEW
))
510 rc
= peer_is_invoker (client
);
511 if (rc
== GPG_ERR_EACCES
)
516 client
->opts
|= OPT_INQUIRE
;
521 parse_opt_inquire (void *data
, void *value
)
523 struct client_s
*client
= data
;
526 client
->opts
|= OPT_INQUIRE
;
531 update_checksum (struct client_s
*client
)
535 struct cache_data_s
*cdata
;
536 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
543 cdata
= cache_get_data (client
->md5file
);
547 cdata
->crc
= xmalloc (len
);
548 memcpy (cdata
->crc
, crc
, len
);
555 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
562 if (cdata
&& !cdata
->crc
)
563 return GPG_ERR_CHECKSUM
;
565 rc
= get_checksum (client
->filename
, &crc
, &len
);
570 n
= memcmp (cdata
->crc
, crc
, len
);
571 else if (client
->crc
)
572 n
= memcmp (client
->crc
, crc
, len
);
575 return n
? GPG_ERR_CHECKSUM
: 0;
579 do_open (assuan_context_t ctx
, const char *password
)
581 struct client_s
*client
= assuan_get_pointer (ctx
);
582 struct cache_data_s
*cdata
;
587 cdata
= cache_get_data (client
->md5file
);
588 if (cdata
&& cdata
->doc
)
592 /* This will check that the key is cached in the agent which needs to
593 * be determined for files that share a keygrip. */
596 rc
= cache_iscached (client
->filename
, &defer
);
597 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
598 rc
= GPG_ERR_KEY_EXPIRED
;
601 if (!rc
&& !(client
->flags
& FLAG_NEW
))
602 rc
= validate_checksum (client
, cdata
);
605 if (!rc
&& client
->thd
->remote
606 && config_get_boolean (client
->filename
, "tcp_require_key"))
607 rc
= GPG_ERR_KEY_EXPIRED
;
610 if (!rc
&& !password
)
612 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
616 if (IS_PKI (client
->crypto
))
618 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
620 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
621 client
->crypto
->grip
);
622 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
624 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
625 client
->crypto
->sign_grip
);
630 rc
= open_finalize (ctx
, NULL
, 0);
636 /* There was an error accessing the file so clear the cache entry. The
637 * real error will be returned from read_data_file() since the file
638 * may have only disappeared. */
641 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
642 rc
= cache_clear (client
->md5file
);
643 send_status_all (STATUS_CACHE
, NULL
);
650 rc
= read_data_file (client
->filename
, client
->crypto
);
653 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
654 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
656 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
660 client
->flags
|= FLAG_NEW
;
661 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
662 memset (client
->crypto
->sign_grip
, 0,
663 sizeof (client
->crypto
->sign_grip
));
664 rc
= open_finalize (ctx
, NULL
, 0);
668 if (password
&& IS_PKI (client
->crypto
))
671 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
677 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
682 open_command (assuan_context_t ctx
, char *line
)
685 struct client_s
*client
= assuan_get_pointer (ctx
);
686 char **req
, *filename
;
687 unsigned char md5file
[16];
689 assuan_peercred_t peer
;
690 struct argv_s
*args
[] = {
691 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
695 rc
= parse_options (&line
, args
, client
);
697 return send_error (ctx
, rc
);
699 req
= str_split (line
, " ", 2);
701 return send_error (ctx
, GPG_ERR_SYNTAX
);
703 rc
= do_validate_peer (ctx
, req
[0], &peer
);
707 return send_error (ctx
, rc
);
711 if (!valid_filename (filename
))
714 return send_error (ctx
, GPG_ERR_INV_VALUE
);
717 pthread_cleanup_push (req_cleanup
, req
);
718 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
719 /* This client may have locked a different file with ISCACHED --lock than
720 * the current filename. This will remove that lock. */
721 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
722 if (client
->flags
& FLAG_OPEN
||
723 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
725 uint32_t opts
= client
->opts
;
726 uint32_t flags
= client
->flags
;
729 client
->flags
|= FLAG_KEEP_LOCK
;
730 else if (client
->flags
& FLAG_NEW
)
731 cache_clear (client
->md5file
);
733 cleanup_client (client
);
735 client
->flags
|= flags
;
736 client
->flags
&= ~(FLAG_LOCK_CMD
);
738 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
741 memcpy (client
->md5file
, md5file
, 16);
742 client
->filename
= str_dup (filename
);
743 if (!client
->filename
)
746 return send_error(ctx
, GPG_ERR_ENOMEM
);
749 /* Need to lock the mutex here because file_modified() cannot without
750 * knowing the filename. */
751 rc
= lock_file_mutex (client
, 1);
753 client
->flags
&= ~FLAG_OPEN
;
757 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
760 if (IS_PKI (client
->crypto
))
761 rc
= set_pinentry_mode (client
->crypto
->agent
,
762 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
767 rc
= do_open (ctx
, password
);
769 cleanup_client (client
);
771 cleanup_crypto_stage1 (client
->crypto
);
775 pthread_cleanup_pop (1);
777 if (!rc
&& client
->flags
& FLAG_NEW
)
778 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
781 (void) kill_scd (client
->crypto
->agent
);
784 return send_error (ctx
, rc
);
788 parse_opt_no_passphrase (void *data
, void *value
)
790 struct client_s
*client
= data
;
793 client
->opts
|= OPT_NO_PASSPHRASE
;
798 parse_save_opt_no_agent (void *data
, void *value
)
800 struct client_s
*client
= data
;
802 client
->opts
|= OPT_NO_AGENT
;
807 parse_save_opt_cipher (void *data
, void *value
)
809 struct client_s
*client
= data
;
810 int algo
= cipher_string_to_gcrypt ((char *) value
);
811 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
815 return GPG_ERR_INV_VALUE
;
817 if (!(client
->flags
& FLAG_NEW
))
819 rc
= peer_is_invoker (client
);
820 if (rc
== GPG_ERR_EACCES
)
824 flags
&= ((uint16_t) ((uint32_t) (flags
& 0xFFFFFFFF)));
825 if (!(client
->crypto
->hdr
.flags
& gcrypt_to_cipher (algo
)))
835 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
842 permitted_to_save (struct client_s
*client
, const unsigned char *grip
,
843 size_t size
, const char *value
)
848 if (!(client
->flags
& FLAG_NEW
))
850 hexgrip
= bin2hex (grip
, size
);
851 rc
= !strcmp (hexgrip
, (char *)value
) ? 0 : GPG_ERR_EACCES
;
854 rc
= peer_is_invoker (client
);
862 parse_save_opt_keygrip (void *data
, void *value
)
865 struct client_s
*client
= data
;
866 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->grip
,
867 sizeof (client
->crypto
->grip
),
870 if (!IS_PKI (client
->crypto
))
871 return GPG_ERR_INV_ARG
;
876 if (client
->crypto
->save
.pkey
)
877 gcry_sexp_release (client
->crypto
->save
.pkey
);
879 client
->crypto
->save
.pkey
= NULL
;
880 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
882 return GPG_ERR_INV_ARG
;
887 parse_save_opt_sign_keygrip (void *data
, void *value
)
890 struct client_s
*client
= data
;
891 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->sign_grip
,
892 sizeof (client
->crypto
->sign_grip
),
895 if (!IS_PKI (client
->crypto
))
896 return GPG_ERR_INV_ARG
;
901 if (client
->crypto
->save
.sigpkey
)
902 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
904 client
->crypto
->save
.sigpkey
= NULL
;
905 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
907 return GPG_ERR_INV_ARG
;
912 parse_opt_s2k_count (void *data
, void *value
)
914 struct client_s
*client
= data
;
918 return GPG_ERR_INV_VALUE
;
920 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
925 parse_save_opt_iterations (void *data
, void *value
)
927 struct client_s
*client
= data
;
932 return GPG_ERR_INV_VALUE
;
935 n
= strtoull (v
, &p
, 10);
936 if (n
== UINT64_MAX
&& errno
)
937 return gpg_error_from_errno (errno
);
939 return GPG_ERR_INV_VALUE
;
941 if (!(client
->flags
& FLAG_NEW
))
943 gpg_error_t rc
= peer_is_invoker (client
);
945 if (rc
== GPG_ERR_EACCES
)
947 if (client
->crypto
->hdr
.iterations
!= n
)
956 client
->crypto
->save
.hdr
.iterations
= n
;
961 save_finalize (assuan_context_t ctx
)
963 struct client_s
*client
= assuan_get_pointer (ctx
);
965 xmlChar
*xmlbuf
= NULL
;
970 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
972 return GPG_ERR_ENOMEM
;
974 pthread_cleanup_push (xmlFree
, xmlbuf
);
976 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
977 && (client
->opts
& OPT_NO_AGENT
))
978 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
980 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
981 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
982 NULL
, &key
, &keylen
, 1, 1,
983 (client
->opts
& OPT_NO_PASSPHRASE
));
988 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
989 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
992 pubkey
= client
->crypto
->pkey_sexp
;
996 sigkey
= client
->crypto
->sigpkey_sexp
;
999 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
1000 sigkey
= client
->crypto
->save
.sigpkey
;
1004 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
1005 client
->filename
, xmlbuf
, xmlbuflen
);
1006 if (pubkey
== client
->crypto
->save
.pkey
)
1010 gcry_sexp_release (client
->crypto
->pkey_sexp
);
1011 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
1012 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
1013 client
->crypto
->grip
);
1016 gcry_sexp_release (pubkey
);
1018 client
->crypto
->save
.pkey
= NULL
;
1022 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
1024 if (sigkey
== client
->crypto
->save
.sigpkey
)
1028 if (client
->crypto
->sigpkey_sexp
)
1029 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
1031 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
1032 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
1033 client
->crypto
->sign_grip
);
1036 gcry_sexp_release (sigkey
);
1038 client
->crypto
->save
.sigpkey
= NULL
;
1047 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
1048 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
1052 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
1053 send_status_all (STATUS_CACHE
, NULL
);
1057 rc
= update_checksum (client
);
1058 client
->flags
&= ~(FLAG_NEW
);
1062 pthread_cleanup_pop (1); // xmlFree
1067 parse_opt_reset (void *data
, void *value
)
1069 struct client_s
*client
= data
;
1072 client
->opts
|= OPT_RESET
;
1077 save_command (assuan_context_t ctx
, char *line
)
1079 struct client_s
*client
= assuan_get_pointer (ctx
);
1082 struct argv_s
*args
[] = {
1083 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1084 parse_opt_no_passphrase
},
1085 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
1086 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
1087 parse_save_opt_inquire
},
1088 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
1089 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
1090 parse_save_opt_sign_keygrip
},
1091 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
1092 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
1093 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
1094 parse_save_opt_iterations
},
1095 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
1096 parse_save_opt_no_agent
},
1100 cleanup_save (&client
->crypto
->save
);
1101 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
1102 sizeof (file_header_t
));
1103 client
->crypto
->save
.s2k_count
=
1104 config_get_ulong (client
->filename
, "s2k_count");
1106 if (client
->flags
& FLAG_NEW
)
1107 client
->crypto
->save
.hdr
.iterations
=
1108 config_get_ulonglong (client
->filename
, "cipher_iterations");
1110 rc
= parse_options (&line
, args
, client
);
1112 return send_error (ctx
, rc
);
1114 if (!(client
->flags
& FLAG_NEW
))
1115 client
->opts
&= ~OPT_NO_AGENT
;
1116 else if (client
->opts
& OPT_NO_AGENT
)
1118 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
1119 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
1122 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
1123 && !(client
->opts
& OPT_INQUIRE
))
1124 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1126 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1127 return send_error (ctx
, gpg_error_from_errno (errno
));
1129 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
1131 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
1132 return send_error (ctx
, GPG_ERR_ENOANO
);
1136 rc
= cache_iscached (client
->filename
, &defer
);
1137 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
1139 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1142 if ((!rc
&& defer
) || config_get_boolean ("global", "require_save_key"))
1143 client
->opts
|= OPT_RESET
;
1145 if (!rc
&& (client
->opts
& OPT_RESET
))
1147 rc
= cache_clear (client
->md5file
);
1149 return send_error (ctx
, rc
);
1151 log_write ("%s: %s", client
->filename
,
1152 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1153 send_status_all (STATUS_CACHE
, NULL
);
1157 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1160 return send_error (ctx
, rc
);
1163 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1164 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1165 && !(client
->opts
& OPT_NO_AGENT
))
1167 rc
= set_pinentry_mode (client
->crypto
->agent
,
1168 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1171 if (!(client
->flags
& FLAG_NEW
))
1173 struct crypto_s
*crypto
;
1177 /* Wanting to generate a new key. Require the key to open the
1178 current file before proceeding reguardless of the
1179 require_save_key configuration parameter. */
1180 rc
= cache_clear (client
->md5file
);
1183 rc
= init_client_crypto (&crypto
);
1185 crypto
->client_ctx
= client
->ctx
;
1189 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1190 client
->filename
, &key
, &keylen
);
1193 cleanup_crypto (&crypto
);
1197 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1201 struct inquire_data_s idata
= { 0 };
1202 char *params
= client
->opts
& OPT_INQUIRE
1203 ? NULL
: default_key_params (client
->crypto
);
1205 pthread_cleanup_push (xfree
, params
);
1206 idata
.crypto
= client
->crypto
;
1210 idata
.line
= params
;
1211 idata
.len
= strlen (params
);
1216 client
->crypto
->agent
->inquire_data
= &idata
;
1217 client
->crypto
->agent
->inquire_cb
= NULL
;
1218 rc
= generate_key (client
->crypto
, params
,
1219 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1220 pthread_cleanup_pop (1);
1226 rc
= save_finalize (ctx
);
1228 cleanup_crypto_stage1 (client
->crypto
);
1230 (void) kill_scd (client
->crypto
->agent
);
1232 return send_error (ctx
, rc
);
1236 do_delete (assuan_context_t ctx
, char *line
)
1238 struct client_s
*client
= assuan_get_pointer (ctx
);
1243 if (strchr (line
, '\t'))
1244 req
= str_split (line
, "\t", 0);
1246 req
= str_split (line
, " ", 0);
1249 return GPG_ERR_SYNTAX
;
1251 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1259 * No sub-node defined. Remove the entire node (root element).
1265 rc
= is_element_owner (client
, n
);
1268 rc
= unlink_node (client
, n
);
1277 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1278 NULL
, NULL
, 0, 0, NULL
, 0);
1283 rc
= is_element_owner (client
, n
);
1286 rc
= unlink_node (client
, n
);
1294 delete_command (assuan_context_t ctx
, char *line
)
1296 struct client_s
*client
= assuan_get_pointer (ctx
);
1298 struct argv_s
*args
[] = {
1299 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1303 rc
= parse_options (&line
, args
, client
);
1305 return send_error (ctx
, rc
);
1307 if (client
->opts
& OPT_INQUIRE
)
1309 unsigned char *result
;
1312 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1314 return send_error (ctx
, rc
);
1316 pthread_cleanup_push (xfree
, result
);
1317 rc
= do_delete (ctx
, (char *)result
);
1318 pthread_cleanup_pop (1);
1321 rc
= do_delete (ctx
, line
);
1323 return send_error (ctx
, rc
);
1327 store_command (assuan_context_t ctx
, char *line
)
1329 struct client_s
*client
= assuan_get_pointer (ctx
);
1332 unsigned char *result
;
1334 xmlNodePtr n
, parent
;
1336 char *content
= NULL
;
1338 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1340 return send_error (ctx
, rc
);
1342 req
= str_split ((char *) result
, "\t", 0);
1346 return send_error (ctx
, GPG_ERR_SYNTAX
);
1348 len
= strv_length (req
);
1349 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1350 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1353 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1356 if (has_content
|| !*req
[len
- 1])
1359 content
= req
[len
- 1];
1360 req
[len
- 1] = NULL
;
1364 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1365 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1367 rc
= new_root_element (client
, client
->doc
, *req
);
1371 return send_error (ctx
, rc
);
1380 return send_error (ctx
, rc
);
1385 if (req
[1] && *req
[1])
1388 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1390 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1391 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1397 rc
= is_element_owner (client
, parent
);
1400 n
= find_text_node (parent
->children
);
1402 xmlNodeSetContent (n
, (xmlChar
*) content
);
1404 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1406 update_element_mtime (client
, parent
);
1412 return send_error (ctx
, rc
);
1416 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1421 int progress
= config_get_integer ("global", "xfer_progress");
1425 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1426 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1427 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1435 if (sent
+ to_send
> total
)
1436 to_send
= total
- sent
;
1438 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1439 flush
? 0 : to_send
);
1442 sent
+= flush
? 0 : to_send
;
1444 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1445 (sent
== total
&& flush
))
1446 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1448 if (!flush
&& !rc
&& sent
== total
)
1455 while (!rc
&& sent
< total
);
1461 do_get (assuan_context_t ctx
, char *line
)
1463 struct client_s
*client
= assuan_get_pointer (ctx
);
1468 req
= str_split (line
, "\t", 0);
1473 return GPG_ERR_SYNTAX
;
1476 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1485 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1492 if (!n
|| !n
->children
)
1493 return GPG_ERR_NO_DATA
;
1495 n
= find_text_node (n
->children
);
1496 if (!n
|| !n
->content
|| !*n
->content
)
1497 return GPG_ERR_NO_DATA
;
1499 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1504 get_command (assuan_context_t ctx
, char *line
)
1506 struct client_s
*client
= assuan_get_pointer (ctx
);
1508 struct argv_s
*args
[] = {
1509 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1513 rc
= parse_options (&line
, args
, client
);
1515 return send_error (ctx
, rc
);
1517 if (client
->opts
& OPT_INQUIRE
)
1519 unsigned char *result
;
1522 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1524 return send_error (ctx
, rc
);
1526 pthread_cleanup_push (xfree
, result
);
1527 rc
= do_get (ctx
, (char *)result
);
1528 pthread_cleanup_pop (1);
1531 rc
= do_get (ctx
, line
);
1533 return send_error (ctx
, rc
);
1536 static void list_command_cleanup1 (void *arg
);
1538 realpath_command (assuan_context_t ctx
, char *line
)
1540 struct string_s
*string
= NULL
;
1542 struct client_s
*client
= assuan_get_pointer (ctx
);
1543 struct argv_s
*args
[] = {
1544 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1548 rc
= parse_options (&line
, args
, client
);
1550 return send_error (ctx
, rc
);
1552 if (client
->opts
& OPT_INQUIRE
)
1554 unsigned char *result
;
1557 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1559 return send_error (ctx
, rc
);
1561 pthread_cleanup_push (xfree
, result
);
1562 rc
= build_realpath (client
, client
->doc
, (char *)result
, &string
);
1563 pthread_cleanup_pop (1);
1566 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1570 pthread_cleanup_push (list_command_cleanup1
, string
);
1571 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1572 pthread_cleanup_pop (1);
1575 return send_error (ctx
, rc
);
1579 list_command_cleanup1 (void *arg
)
1582 string_free ((struct string_s
*) arg
, 1);
1586 list_command_cleanup2 (void *arg
)
1588 struct element_list_s
*elements
= arg
;
1594 int total
= slist_length (elements
->list
);
1597 for (i
= 0; i
< total
; i
++)
1599 char *tmp
= slist_nth_data (elements
->list
, i
);
1603 slist_free (elements
->list
);
1606 if (elements
->prefix
)
1607 xfree (elements
->prefix
);
1610 strv_free (elements
->req
);
1617 parse_list_opt_norecurse (void *data
, void *value
)
1619 struct client_s
*client
= data
;
1621 client
->opts
&= ~(OPT_LIST_RECURSE
);
1626 parse_opt_verbose (void *data
, void *value
)
1628 struct client_s
*client
= data
;
1630 client
->opts
|= OPT_VERBOSE
;
1635 parse_list_opt_target (void *data
, void *value
)
1637 struct client_s
*client
= data
;
1639 client
->opts
|= OPT_LIST_WITH_TARGET
;
1644 parse_list_opt_all (void *data
, void *value
)
1646 struct client_s
*client
= data
;
1648 client
->opts
|= OPT_LIST_ALL
;
1653 list_path_once (struct client_s
*client
, char *line
,
1654 struct element_list_s
*elements
, struct string_s
*result
)
1658 elements
->req
= str_split (line
, " ", 0);
1660 strv_printf (&elements
->req
, "%s", line
);
1662 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1663 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1668 int total
= slist_length (elements
->list
);
1671 rc
= GPG_ERR_NO_DATA
;
1679 for (i
= 0; i
< total
; i
++)
1681 char *tmp
= slist_nth_data (elements
->list
, i
);
1683 string_append_printf (result
, "%s%s", tmp
,
1684 i
+ 1 == total
? "" : "\n");
1689 rc
= GPG_ERR_NO_DATA
;
1696 has_list_flag (char *path
, char *flags
)
1700 while (*p
&& *++p
!= ' ');
1709 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1720 do_list (assuan_context_t ctx
, char *line
)
1722 struct client_s
*client
= assuan_get_pointer (ctx
);
1724 struct element_list_s
*elements
= NULL
;
1726 elements
= xcalloc (1, sizeof (struct element_list_s
));
1728 return GPG_ERR_ENOMEM
;
1730 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1732 (client
->opts
& OPT_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1733 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1735 if (!line
|| !*line
)
1737 struct string_s
*str
= NULL
;
1739 pthread_cleanup_push (list_command_cleanup2
, elements
);
1740 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1741 elements
->with_target
);
1742 pthread_cleanup_pop (1);
1743 pthread_cleanup_push (list_command_cleanup1
, str
);
1747 if (client
->opts
& OPT_LIST_ALL
)
1749 char **roots
= str_split (str
->str
, "\n", 0);
1752 pthread_cleanup_push (req_cleanup
, roots
);
1753 string_truncate (str
, 0);
1755 for (p
= roots
; *p
; p
++)
1757 if (strchr (*p
, ' '))
1759 if (has_list_flag (*p
, "EOP"))
1761 string_append_printf (str
, "%s%s", *p
,
1762 *(p
+ 1) ? "\n" : "");
1767 elements
= xcalloc (1, sizeof (struct element_list_s
));
1770 rc
= GPG_ERR_ENOMEM
;
1774 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1776 (client
->opts
& OPT_VERBOSE
) | (client
->opts
&
1777 OPT_LIST_WITH_TARGET
);
1778 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1779 pthread_cleanup_push (list_command_cleanup2
, elements
);
1780 rc
= list_path_once (client
, *p
, elements
, str
);
1781 pthread_cleanup_pop (1);
1786 string_append (str
, "\n");
1789 pthread_cleanup_pop (1);
1793 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1796 pthread_cleanup_pop (1);
1800 pthread_cleanup_push (list_command_cleanup2
, elements
);
1801 struct string_s
*str
= string_new (NULL
);
1802 pthread_cleanup_push (list_command_cleanup1
, str
);
1803 rc
= list_path_once (client
, line
, elements
, str
);
1805 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1807 pthread_cleanup_pop (1);
1808 pthread_cleanup_pop (1);
1813 list_command (assuan_context_t ctx
, char *line
)
1815 struct client_s
*client
= assuan_get_pointer (ctx
);
1817 struct argv_s
*args
[] = {
1818 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1819 parse_list_opt_norecurse
},
1820 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
1821 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1822 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1823 parse_list_opt_target
},
1824 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1828 if (disable_list_and_dump
== 1)
1829 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1831 client
->opts
|= OPT_LIST_RECURSE
;
1832 rc
= parse_options (&line
, args
, client
);
1834 return send_error (ctx
, rc
);
1836 if (client
->opts
& OPT_LIST_ALL
)
1837 client
->opts
|= OPT_LIST_RECURSE
| OPT_VERBOSE
;
1839 if (client
->opts
& OPT_INQUIRE
)
1841 unsigned char *result
;
1844 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1846 return send_error (ctx
, rc
);
1848 pthread_cleanup_push (xfree
, result
);
1849 rc
= do_list (ctx
, (char *)result
);
1850 pthread_cleanup_pop (1);
1853 rc
= do_list (ctx
, line
);
1855 return send_error (ctx
, rc
);
1859 * req[0] - element path
1862 attribute_list (assuan_context_t ctx
, char **req
)
1864 struct client_s
*client
= assuan_get_pointer (ctx
);
1865 char **attrlist
= NULL
;
1873 if (!req
|| !req
[0])
1874 return GPG_ERR_SYNTAX
;
1876 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1879 * The first argument may be only a root element.
1881 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1882 return GPG_ERR_SYNTAX
;
1885 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1895 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1896 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1907 for (a
= n
->properties
; a
; a
= a
->next
)
1911 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1914 strv_free (attrlist
);
1916 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1917 pwmd_strerror (GPG_ERR_ENOMEM
));
1918 return GPG_ERR_ENOMEM
;
1923 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1925 && an
->content
? (char *) an
->content
: "");
1929 strv_free (attrlist
);
1930 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1931 pwmd_strerror (GPG_ERR_ENOMEM
));
1932 return GPG_ERR_ENOMEM
;
1935 attrlist
[++i
] = NULL
;
1939 return GPG_ERR_NO_DATA
;
1941 line
= strv_join ("\n", attrlist
);
1945 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1946 pwmd_strerror (GPG_ERR_ENOMEM
));
1947 strv_free (attrlist
);
1948 return GPG_ERR_ENOMEM
;
1951 pthread_cleanup_push (xfree
, line
);
1952 pthread_cleanup_push (req_cleanup
, attrlist
);
1953 rc
= xfer_data (ctx
, line
, strlen (line
));
1954 pthread_cleanup_pop (1);
1955 pthread_cleanup_pop (1);
1960 * req[0] - attribute
1961 * req[1] - element path
1964 attribute_delete (struct client_s
*client
, char **req
)
1970 if (!req
|| !req
[0] || !req
[1])
1971 return GPG_ERR_SYNTAX
;
1973 if (!strcmp (req
[0], "_name"))
1974 return GPG_ERR_INV_ATTR
;
1976 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1979 * The first argument may be only a root element.
1981 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1982 return GPG_ERR_SYNTAX
;
1985 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1991 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1992 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1997 if (!strcmp (req
[0], (char *) "_acl"))
1999 rc
= is_element_owner (client
, n
);
2004 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
2012 create_element_path (struct client_s
*client
,
2013 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
2015 char **req
= *elements
;
2016 char **req_orig
= strv_dup (req
);
2017 xmlNodePtr n
= NULL
;
2023 *rc
= GPG_ERR_ENOMEM
;
2024 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2025 pwmd_strerror (GPG_ERR_ENOMEM
));
2030 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2033 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2036 *rc
= new_root_element (client
, client
->doc
, req
[0]);
2042 else if (n
== parent
)
2044 *rc
= GPG_ERR_CONFLICT
;
2051 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
2053 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2054 NULL
, NULL
, create_target_elements_cb
, 0, 0,
2061 * Reset the position of the element tree now that the elements
2062 * have been created.
2067 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2071 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2072 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2079 strv_free (req_orig
);
2086 * Creates a "target" attribute. When other commands encounter an element with
2087 * this attribute, the element path is modified to the target value. If the
2088 * source element path doesn't exist when using 'ATTR SET target', it is
2089 * created, but the destination element path must exist.
2091 * req[0] - source element path
2092 * req[1] - destination element path
2095 target_attribute (struct client_s
*client
, char **req
)
2097 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
2101 if (!req
|| !req
[0] || !req
[1])
2102 return GPG_ERR_SYNTAX
;
2104 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
2107 * The first argument may be only a root element.
2109 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
2110 return GPG_ERR_SYNTAX
;
2113 if (!valid_element_path (src
, 0))
2114 return GPG_ERR_INV_VALUE
;
2116 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
2119 * The first argument may be only a root element.
2121 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
2123 rc
= GPG_ERR_SYNTAX
;
2128 odst
= strv_dup (dst
);
2131 rc
= GPG_ERR_ENOMEM
;
2135 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2137 * Make sure the destination element path exists.
2144 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
2145 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2150 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
2151 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2154 n
= create_element_path (client
, &src
, &rc
, NULL
);
2158 line
= strv_join ("\t", odst
);
2161 rc
= GPG_ERR_ENOMEM
;
2165 rc
= add_attribute (client
, n
, "target", line
);
2176 * req[0] - attribute
2177 * req[1] - element path
2180 attribute_get (assuan_context_t ctx
, char **req
)
2182 struct client_s
*client
= assuan_get_pointer (ctx
);
2188 if (!req
|| !req
[0] || !req
[1])
2189 return GPG_ERR_SYNTAX
;
2191 if (strchr (req
[1], '\t'))
2193 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2194 return GPG_ERR_SYNTAX
;
2198 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2199 return GPG_ERR_SYNTAX
;
2202 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2209 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2210 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2218 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2219 return GPG_ERR_NOT_FOUND
;
2221 pthread_cleanup_push (xmlFree
, a
);
2224 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2226 rc
= GPG_ERR_NO_DATA
;
2228 pthread_cleanup_pop (1);
2237 * req[0] - attribute
2238 * req[1] - element path
2242 attribute_set (struct client_s
*client
, char **req
)
2248 if (!req
|| !req
[0] || !req
[1])
2249 return GPG_ERR_SYNTAX
;
2252 * Reserved attribute names.
2254 if (!strcmp (req
[0], "_name"))
2255 return GPG_ERR_INV_ATTR
;
2256 else if (!strcmp (req
[0], "target"))
2257 return target_attribute (client
, req
+ 1);
2258 else if (!valid_xml_attribute (req
[0]))
2259 return GPG_ERR_INV_VALUE
;
2261 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2264 * The first argument may be only a root element.
2266 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2267 return GPG_ERR_SYNTAX
;
2270 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2277 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2278 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2284 if (!strcmp (req
[0], (char *) "_acl"))
2286 rc
= is_element_owner (client
, n
);
2291 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2300 * req[1] - attribute name or element path if command is LIST
2301 * req[2] - element path
2302 * req[2] - element path or value
2306 do_attr (assuan_context_t ctx
, char *line
)
2308 struct client_s
*client
= assuan_get_pointer (ctx
);
2312 req
= str_split (line
, " ", 4);
2313 if (!req
|| !req
[0] || !req
[1])
2316 return GPG_ERR_SYNTAX
;
2319 pthread_cleanup_push (req_cleanup
, req
);
2321 if (strcasecmp (req
[0], "SET") == 0)
2322 rc
= attribute_set (client
, req
+ 1);
2323 else if (strcasecmp (req
[0], "GET") == 0)
2324 rc
= attribute_get (ctx
, req
+ 1);
2325 else if (strcasecmp (req
[0], "DELETE") == 0)
2326 rc
= attribute_delete (client
, req
+ 1);
2327 else if (strcasecmp (req
[0], "LIST") == 0)
2328 rc
= attribute_list (ctx
, req
+ 1);
2330 rc
= GPG_ERR_SYNTAX
;
2332 pthread_cleanup_pop (1);
2337 attr_command (assuan_context_t ctx
, char *line
)
2339 struct client_s
*client
= assuan_get_pointer (ctx
);
2341 struct argv_s
*args
[] = {
2342 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2346 rc
= parse_options (&line
, args
, client
);
2348 return send_error (ctx
, rc
);
2350 if (client
->opts
& OPT_INQUIRE
)
2352 unsigned char *result
;
2355 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2357 return send_error (ctx
, rc
);
2359 pthread_cleanup_push (xfree
, result
);
2360 rc
= do_attr (ctx
, (char *)result
);
2361 pthread_cleanup_pop (1);
2364 rc
= do_attr (ctx
, line
);
2366 return send_error (ctx
, rc
);
2370 parse_iscached_opt_lock (void *data
, void *value
)
2372 struct client_s
*client
= data
;
2375 client
->opts
|= OPT_LOCK
;
2380 iscached_command (assuan_context_t ctx
, char *line
)
2382 struct client_s
*client
= assuan_get_pointer (ctx
);
2384 struct argv_s
*args
[] = {
2385 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2389 if (!line
|| !*line
)
2390 return send_error (ctx
, GPG_ERR_SYNTAX
);
2392 rc
= parse_options (&line
, args
, client
);
2394 return send_error (ctx
, rc
);
2395 else if (!valid_filename (line
))
2396 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2398 rc
= cache_iscached (line
, NULL
);
2399 if (client
->opts
& OPT_LOCK
2400 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2402 unsigned char md5file
[16];
2403 gpg_error_t trc
= rc
;
2405 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2406 if (memcmp (md5file
, client
->md5file
, 16))
2407 cleanup_client (client
);
2409 memcpy (client
->md5file
, md5file
, 16);
2410 rc
= do_lock (client
, 1);
2415 return send_error (ctx
, rc
);
2419 clearcache_command (assuan_context_t ctx
, char *line
)
2421 gpg_error_t rc
= 0, all_rc
= 0;
2422 unsigned char md5file
[16];
2426 struct client_thread_s
*once
= NULL
;
2429 MUTEX_LOCK (&cn_mutex
);
2430 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2432 if (!line
|| !*line
)
2435 t
= slist_length (cn_thread_list
);
2437 for (i
= 0; i
< t
; i
++)
2439 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2440 assuan_peercred_t peer
;
2445 /* Lock each connected clients' file mutex to prevent any other client
2446 * from accessing the cache entry (the file mutex is locked upon
2447 * command startup). The cache for the entry is not cleared if the
2448 * file mutex is locked by another client to prevent this function
2453 if (thd
->cl
->filename
)
2455 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2456 all_rc
= !all_rc
? rc
: all_rc
;
2464 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2465 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2467 if (pthread_equal (pthread_self (), thd
->tid
))
2471 if (!thd
->cl
->filename
||
2472 cache_iscached (thd
->cl
->filename
,
2473 NULL
) == GPG_ERR_NO_DATA
)
2479 cache_defer_clear (thd
->cl
->md5file
);
2482 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2490 rc
= cache_clear (thd
->cl
->md5file
);
2491 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2499 /* A single data filename was specified. Lock only this data file
2500 * mutex and free the cache entry. */
2503 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2504 rc
= do_validate_peer (ctx
, line
, &peer
);
2506 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2508 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2510 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2512 if (pthread_equal (pthread_self (), thd
->tid
))
2519 rc
= cache_clear (thd
->cl
->md5file
);
2520 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2524 cache_defer_clear (thd
->cl
->md5file
);
2532 /* Only connected clients' cache entries have been cleared. Now clear any
2533 * remaining cache entries without clients but only if there wasn't an
2534 * error from above since this would defeat the locking check of the
2535 * remaining entries. */
2541 /* No clients are using the specified file. */
2542 else if (!all_rc
&& !rc
&& !once
)
2544 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2545 rc
= cache_clear (md5file
);
2548 /* Release the connection mutex. */
2549 pthread_cleanup_pop (1);
2553 send_status_all (STATUS_CACHE
, NULL
);
2555 /* One or more files were locked while clearing all cache entries. */
2559 return send_error (ctx
, rc
);
2563 cachetimeout_command (assuan_context_t ctx
, char *line
)
2566 char **req
= str_split (line
, " ", 0);
2569 assuan_peercred_t peer
;
2571 if (!req
|| !*req
|| !req
[1])
2574 return send_error (ctx
, GPG_ERR_SYNTAX
);
2578 timeout
= (int) strtol (req
[1], &p
, 10);
2579 if (errno
!= 0 || *p
|| timeout
< -1)
2582 return send_error (ctx
, GPG_ERR_SYNTAX
);
2585 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2588 unsigned char md5file
[16];
2590 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2591 rc
= cache_set_timeout (md5file
, timeout
);
2592 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2595 MUTEX_LOCK (&rcfile_mutex
);
2596 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2598 MUTEX_UNLOCK (&rcfile_mutex
);
2603 return send_error (ctx
, rc
);
2607 dump_command (assuan_context_t ctx
, char *line
)
2611 struct client_s
*client
= assuan_get_pointer (ctx
);
2614 if (disable_list_and_dump
== 1)
2615 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2617 rc
= peer_is_invoker(client
);
2619 return send_error (ctx
, rc
);
2621 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2625 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2626 pwmd_strerror (GPG_ERR_ENOMEM
));
2627 return send_error (ctx
, GPG_ERR_ENOMEM
);
2630 pthread_cleanup_push (xmlFree
, xml
);
2631 rc
= xfer_data (ctx
, (char *) xml
, len
);
2632 pthread_cleanup_pop (1);
2633 return send_error (ctx
, rc
);
2637 getconfig_command (assuan_context_t ctx
, char *line
)
2639 struct client_s
*client
= assuan_get_pointer (ctx
);
2641 char filename
[255] = { 0 }, param
[747] = { 0 };
2642 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2644 if (!line
|| !*line
)
2645 return send_error (ctx
, GPG_ERR_SYNTAX
);
2647 if (strchr (line
, ' '))
2649 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2654 if (fp
&& !valid_filename (fp
))
2655 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2657 paramp
= str_down (paramp
);
2658 if (!strcmp (paramp
, "cipher") && fp
)
2660 struct crypto_s
*crypto
= NULL
;
2662 rc
= init_client_crypto (&crypto
);
2665 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2669 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2679 UPDATE_AGENT_CTX (client
, crypto
);
2680 cleanup_crypto (&crypto
);
2681 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2682 return send_error (ctx
, rc
);
2687 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2689 struct crypto_s
*crypto
= NULL
;
2691 rc
= init_client_crypto (&crypto
);
2694 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2697 tmp
= str_asprintf ("%llu",
2698 (unsigned long long) crypto
->hdr
.
2701 rc
= GPG_ERR_ENOMEM
;
2705 UPDATE_AGENT_CTX (client
, crypto
);
2706 cleanup_crypto (&crypto
);
2707 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2708 return send_error (ctx
, rc
);
2713 else if (!strcmp (paramp
, "passphrase"))
2714 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2716 p
= config_get_value (fp
? fp
: "global", paramp
);
2718 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2720 tmp
= expand_homedir (p
);
2724 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2725 pwmd_strerror (GPG_ERR_ENOMEM
));
2726 return send_error (ctx
, GPG_ERR_ENOMEM
);
2731 pthread_cleanup_push (xfree
, p
);
2732 rc
= xfer_data (ctx
, p
, strlen (p
));
2733 pthread_cleanup_pop (1);
2734 return send_error (ctx
, rc
);
2739 xmlXPathContextPtr xp
;
2740 xmlXPathObjectPtr result
;
2746 xpath_command_cleanup (void *arg
)
2748 struct xpath_s
*xpath
= arg
;
2753 req_cleanup (xpath
->req
);
2756 xmlBufferFree (xpath
->buf
);
2759 xmlXPathFreeObject (xpath
->result
);
2762 xmlXPathFreeContext (xpath
->xp
);
2766 do_xpath (assuan_context_t ctx
, char *line
)
2769 struct client_s
*client
= assuan_get_pointer (ctx
);
2770 struct xpath_s _x
= { 0 };
2771 struct xpath_s
*xpath
= &_x
;
2773 if (!line
|| !*line
)
2774 return GPG_ERR_SYNTAX
;
2776 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2778 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2779 return GPG_ERR_ENOMEM
;
2782 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2785 rc
= GPG_ERR_BAD_DATA
;
2790 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2793 rc
= GPG_ERR_BAD_DATA
;
2797 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2799 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2803 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2804 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2808 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2810 rc
= GPG_ERR_NO_DATA
;
2813 else if (xpath
->req
[1])
2819 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2820 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2821 xmlBufferLength (xpath
->buf
));
2822 pthread_cleanup_pop (0);
2824 xpath_command_cleanup (xpath
);
2829 xpath_command (assuan_context_t ctx
, char *line
)
2831 struct client_s
*client
= assuan_get_pointer (ctx
);
2833 struct argv_s
*args
[] = {
2834 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2838 if (disable_list_and_dump
== 1)
2839 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2841 rc
= peer_is_invoker(client
);
2843 return send_error (ctx
, rc
);
2845 rc
= parse_options (&line
, args
, client
);
2847 return send_error (ctx
, rc
);
2849 if (client
->opts
& OPT_INQUIRE
)
2851 unsigned char *result
;
2854 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2856 return send_error (ctx
, rc
);
2858 pthread_cleanup_push (xfree
, result
);
2859 rc
= do_xpath (ctx
, (char *)result
);
2860 pthread_cleanup_pop (1);
2863 rc
= do_xpath (ctx
, line
);
2865 return send_error (ctx
, rc
);
2869 do_xpathattr (assuan_context_t ctx
, char *line
)
2871 struct client_s
*client
= assuan_get_pointer (ctx
);
2875 struct xpath_s _x
= { 0 };
2876 struct xpath_s
*xpath
= &_x
;
2878 if (!line
|| !*line
)
2879 return GPG_ERR_SYNTAX
;
2881 if ((req
= str_split (line
, " ", 3)) == NULL
)
2882 return GPG_ERR_ENOMEM
;
2886 rc
= GPG_ERR_SYNTAX
;
2890 if (!strcasecmp (req
[0], "SET"))
2892 else if (!strcasecmp (req
[0], "DELETE"))
2896 rc
= GPG_ERR_SYNTAX
;
2900 if (!req
[1] || !req
[2])
2902 rc
= GPG_ERR_SYNTAX
;
2906 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2908 rc
= GPG_ERR_ENOMEM
;
2912 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2914 rc
= GPG_ERR_SYNTAX
;
2918 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2921 rc
= GPG_ERR_BAD_DATA
;
2925 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2928 rc
= GPG_ERR_BAD_DATA
;
2932 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2934 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2938 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2939 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2940 (xmlChar
*) req
[1]);
2943 xpath_command_cleanup (xpath
);
2948 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2950 xpathattr_command (assuan_context_t ctx
, char *line
)
2952 struct client_s
*client
= assuan_get_pointer (ctx
);
2954 struct argv_s
*args
[] = {
2955 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2959 if (disable_list_and_dump
== 1)
2960 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2962 rc
= peer_is_invoker(client
);
2964 return send_error (ctx
, rc
);
2966 rc
= parse_options (&line
, args
, client
);
2968 return send_error (ctx
, rc
);
2970 if (client
->opts
& OPT_INQUIRE
)
2972 unsigned char *result
;
2975 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2977 return send_error (ctx
, rc
);
2979 pthread_cleanup_push (xfree
, result
);
2980 rc
= do_xpathattr (ctx
, (char *)result
);
2981 pthread_cleanup_pop (1);
2984 rc
= do_xpathattr (ctx
, line
);
2986 return send_error (ctx
, rc
);
2990 do_import (struct client_s
*client
, const char *root_element
,
2991 unsigned char *content
)
2993 char **dst_path
= NULL
;
2994 xmlDocPtr doc
= NULL
;
2995 xmlNodePtr n
, root
, copy
;
2998 if (!content
|| !*content
)
3001 return GPG_ERR_SYNTAX
;
3005 dst_path
= str_split (root_element
, "\t", 0);
3007 if (dst_path
&& !valid_element_path (dst_path
, 0))
3010 strv_free (dst_path
);
3012 return GPG_ERR_INV_VALUE
;
3015 struct string_s
*str
= string_new_content ((char *)content
);
3016 str
= string_prepend (str
, "<pwmd>");
3017 str
= string_append (str
, "</pwmd>");
3018 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
3019 string_free (str
, 1);
3022 rc
= GPG_ERR_BAD_DATA
;
3026 root
= xmlDocGetRootElement (doc
);
3027 xmlNodePtr root_orig
= root
->children
;
3028 root
= root
->children
;
3029 rc
= validate_import (client
, root
);
3038 char **path
= strv_dup (dst_path
);
3041 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
3042 pwmd_strerror (GPG_ERR_ENOMEM
));
3043 rc
= GPG_ERR_ENOMEM
;
3047 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
3051 rc
= GPG_ERR_INV_VALUE
;
3055 if (strv_printf (&path
, "%s", (char *) a
) == 0)
3058 rc
= GPG_ERR_ENOMEM
;
3063 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
3064 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3072 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
3073 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
3074 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3089 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
3091 n
= create_element_path (client
, &path
, &rc
, NULL
);
3098 copy
= xmlCopyNodeList (root
->children
);
3099 n
= xmlAddChildList (n
, copy
);
3101 rc
= GPG_ERR_ENOMEM
;
3110 /* Check if the content root element can create a DTD root element. */
3111 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
3113 rc
= GPG_ERR_SYNTAX
;
3119 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
3121 rc
= GPG_ERR_SYNTAX
;
3125 char *tmp
= str_dup ((char *) a
);
3127 int literal
= is_literal_element (&tmp
);
3129 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
3132 rc
= GPG_ERR_INV_VALUE
;
3136 if (strv_printf (&path
, "%s", tmp
) == 0)
3139 rc
= GPG_ERR_ENOMEM
;
3144 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
3145 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3147 rc
= GPG_ERR_BAD_DATA
;
3151 /* Overwriting the existing tree. */
3155 xmlFreeNodeList (n
);
3159 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
3160 n
= xmlCopyNode (root
, 1);
3161 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3166 rc
= update_element_mtime (client
, n
->parent
);
3168 for (root
= root_orig
->next
; root
; root
= root
->next
)
3170 if (root
->type
== XML_ELEMENT_NODE
)
3183 strv_free (dst_path
);
3189 parse_import_opt_root (void *data
, void *value
)
3191 struct client_s
*client
= data
;
3193 client
->import_root
= str_dup (value
);
3194 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3198 import_command (assuan_context_t ctx
, char *line
)
3201 struct client_s
*client
= assuan_get_pointer (ctx
);
3202 unsigned char *result
;
3204 struct argv_s
*args
[] = {
3205 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3209 xfree (client
->import_root
);
3210 client
->import_root
= NULL
;
3211 rc
= parse_options (&line
, args
, client
);
3213 return send_error (ctx
, rc
);
3215 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3218 xfree (client
->import_root
);
3219 client
->import_root
= NULL
;
3220 return send_error (ctx
, rc
);
3223 rc
= do_import (client
, client
->import_root
, result
);
3224 xfree (client
->import_root
);
3225 client
->import_root
= NULL
;
3226 return send_error (ctx
, rc
);
3230 do_lock (struct client_s
*client
, int add
)
3232 gpg_error_t rc
= lock_file_mutex (client
, add
);
3235 client
->flags
|= FLAG_LOCK_CMD
;
3241 lock_command (assuan_context_t ctx
, char *line
)
3243 struct client_s
*client
= assuan_get_pointer (ctx
);
3244 gpg_error_t rc
= do_lock (client
, 0);
3246 return send_error (ctx
, rc
);
3250 unlock_command (assuan_context_t ctx
, char *line
)
3252 struct client_s
*client
= assuan_get_pointer (ctx
);
3255 rc
= unlock_file_mutex (client
, 0);
3256 return send_error (ctx
, rc
);
3260 option_command (assuan_context_t ctx
, char *line
)
3262 struct client_s
*client
= assuan_get_pointer (ctx
);
3264 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3266 struct agent_s
*agent
= client
->crypto
->agent
;
3268 char namebuf
[255] = { 0 };
3269 char *name
= namebuf
;
3270 char *value
= NULL
, *p
, *tmp
= NULL
;
3272 p
= strchr (line
, '=');
3274 strncpy (namebuf
, line
, sizeof(namebuf
));
3277 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3281 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3283 if (strcasecmp (name
, (char *) "log_level") == 0)
3289 l
= strtol (value
, NULL
, 10);
3292 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3295 MUTEX_LOCK (&rcfile_mutex
);
3296 config_set_int_param (&global_config
, "global", "log_level", value
);
3297 MUTEX_UNLOCK (&rcfile_mutex
);
3300 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3306 n
= strtol (value
, &tmp
, 10);
3308 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3311 client
->lock_timeout
= n
;
3314 else if (strcasecmp (name
, (char *) "NAME") == 0)
3316 if (value
&& strchr (value
, ' '))
3317 rc
= GPG_ERR_INV_VALUE
;
3320 tmp
= pthread_getspecific (thread_name_key
);
3322 MUTEX_LOCK (&cn_mutex
);
3323 xfree (client
->thd
->name
);
3324 client
->thd
->name
= NULL
;
3326 if (!value
|| !*value
)
3327 pthread_setspecific (thread_name_key
, str_dup (""));
3330 client
->thd
->name
= str_dup (value
);
3331 pthread_setspecific (thread_name_key
, str_dup (value
));
3334 MUTEX_UNLOCK (&cn_mutex
);
3338 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3340 xfree (pin_opts
->lc_messages
);
3341 pin_opts
->lc_messages
= NULL
;
3342 if (value
&& *value
)
3343 pin_opts
->lc_messages
= str_dup (value
);
3346 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3349 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3351 xfree (pin_opts
->lc_ctype
);
3352 pin_opts
->lc_ctype
= NULL
;
3353 if (value
&& *value
)
3354 pin_opts
->lc_ctype
= str_dup (value
);
3357 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3360 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3362 xfree (pin_opts
->ttyname
);
3363 pin_opts
->ttyname
= NULL
;
3364 if (value
&& *value
)
3365 pin_opts
->ttyname
= str_dup (value
);
3368 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3371 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3373 xfree (pin_opts
->ttytype
);
3374 pin_opts
->ttytype
= NULL
;
3375 if (value
&& *value
)
3376 pin_opts
->ttytype
= str_dup (value
);
3379 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3382 else if (strcasecmp (name
, (char *) "display") == 0)
3384 xfree (pin_opts
->display
);
3385 pin_opts
->display
= NULL
;
3386 if (value
&& *value
)
3387 pin_opts
->display
= str_dup (value
);
3390 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3393 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3395 xfree (pin_opts
->desc
);
3396 pin_opts
->desc
= NULL
;
3397 if (value
&& *value
)
3398 pin_opts
->desc
= str_dup (value
);
3400 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3402 xfree (pin_opts
->title
);
3403 pin_opts
->title
= NULL
;
3404 if (value
&& *value
)
3405 pin_opts
->title
= str_dup (value
);
3407 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3409 xfree (pin_opts
->prompt
);
3410 pin_opts
->prompt
= NULL
;
3411 if (value
&& *value
)
3412 pin_opts
->prompt
= str_dup (value
);
3415 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3423 n
= (int) strtol (value
, &p
, 10);
3426 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3428 pin_opts
->timeout
= n
;
3429 MUTEX_LOCK (&rcfile_mutex
);
3430 config_set_int_param (&global_config
,
3431 client
->filename
? client
->filename
: "global",
3432 "pinentry_timeout", value
);
3433 MUTEX_UNLOCK (&rcfile_mutex
);
3436 else if (strcasecmp (name
, "disable-pinentry") == 0)
3440 if (value
&& *value
)
3442 n
= (int) strtol (value
, &tmp
, 10);
3443 if (*tmp
|| n
< 0 || n
> 1)
3444 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3448 client
->flags
|= FLAG_NO_PINENTRY
;
3450 client
->flags
&= ~FLAG_NO_PINENTRY
;
3455 if (client
->flags
& FLAG_NO_PINENTRY
)
3456 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3459 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3463 return send_error (ctx
, rc
);
3468 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3472 if (!rc
&& use_agent
&& agent
)
3474 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3479 return send_error (ctx
, rc
);
3483 do_rename (assuan_context_t ctx
, char *line
)
3485 struct client_s
*client
= assuan_get_pointer (ctx
);
3487 char **req
, **src
, *dst
;
3490 req
= str_split (line
, " ", 0);
3492 if (!req
|| !req
[0] || !req
[1])
3495 return GPG_ERR_SYNTAX
;
3499 is_literal_element (&dst
);
3501 if (!valid_xml_element ((xmlChar
*) dst
))
3504 return GPG_ERR_INV_VALUE
;
3507 if (strchr (req
[0], '\t'))
3508 src
= str_split (req
[0], "\t", 0);
3510 src
= str_split (req
[0], " ", 0);
3514 rc
= GPG_ERR_SYNTAX
;
3518 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3520 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3521 NULL
, 0, 0, NULL
, 0);
3526 rc
= is_element_owner (client
, n
);
3530 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3533 rc
= GPG_ERR_ENOMEM
;
3537 /* To prevent unwanted effects:
3539 * <root name="a"><b/></root>
3543 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3546 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3556 for (p
= src
; *p
; p
++)
3560 strv_printf (&tmp
, "%s", *p
);
3564 strv_printf (&tmp
, "!%s", dst
);
3565 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3566 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3573 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3574 NULL
, NULL
, 0, 0, NULL
, 0);
3577 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3582 /* Target may exist:
3585 * <root name="b" target="a"/>
3594 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3600 rc
= is_element_owner (client
, ndst
);
3604 unlink_node (client
, ndst
);
3605 xmlFreeNodeList (ndst
);
3608 rc
= add_attribute (client
, n
, "_name", dst
);
3617 rename_command (assuan_context_t ctx
, char *line
)
3619 struct client_s
*client
= assuan_get_pointer (ctx
);
3621 struct argv_s
*args
[] = {
3622 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3626 rc
= parse_options (&line
, args
, client
);
3628 return send_error (ctx
, rc
);
3630 if (client
->opts
& OPT_INQUIRE
)
3632 unsigned char *result
;
3635 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3637 return send_error (ctx
, rc
);
3639 pthread_cleanup_push (xfree
, result
);
3640 rc
= do_rename (ctx
, (char *)result
);
3641 pthread_cleanup_pop (1);
3644 rc
= do_rename (ctx
, line
);
3646 return send_error (ctx
, rc
);
3650 do_copy (assuan_context_t ctx
, char *line
)
3652 struct client_s
*client
= assuan_get_pointer (ctx
);
3654 char **req
, **src
= NULL
, **dst
= NULL
;
3655 xmlNodePtr nsrc
, ndst
, new = NULL
;
3657 req
= str_split (line
, " ", 0);
3658 if (!req
|| !req
[0] || !req
[1])
3661 return GPG_ERR_SYNTAX
;
3664 if (strchr (req
[0], '\t'))
3665 src
= str_split (req
[0], "\t", 0);
3667 src
= str_split (req
[0], " ", 0);
3671 rc
= GPG_ERR_SYNTAX
;
3675 if (strchr (req
[1], '\t'))
3676 dst
= str_split (req
[1], "\t", 0);
3678 dst
= str_split (req
[1], " ", 0);
3682 rc
= GPG_ERR_SYNTAX
;
3686 if (!valid_element_path (dst
, 0))
3688 rc
= GPG_ERR_INV_VALUE
;
3692 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3694 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3695 NULL
, NULL
, 0, 0, NULL
, 0);
3700 new = xmlCopyNodeList (nsrc
);
3703 rc
= GPG_ERR_ENOMEM
;
3708 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3712 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3713 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3720 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3722 else if (!ndst
|| create
)
3724 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3729 rc
= is_element_owner (client
, ndst
);
3733 /* Merge any attributes from the src node to the initial dst node. */
3734 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3736 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3739 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3743 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3744 xmlNewProp (ndst
, attr
->name
, tmp
);
3746 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3749 xmlNodePtr n
= ndst
->children
;
3751 xmlFreeNodeList (n
);
3752 ndst
->children
= NULL
;
3756 n
= xmlCopyNodeList (new->children
);
3759 rc
= GPG_ERR_ENOMEM
;
3763 n
= xmlAddChildList (ndst
, n
);
3766 rc
= GPG_ERR_ENOMEM
;
3770 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3771 ndst
->parent
? ndst
: ndst
->parent
);
3777 xmlUnlinkNode (new);
3778 xmlFreeNodeList (new);
3794 copy_command (assuan_context_t ctx
, char *line
)
3796 struct client_s
*client
= assuan_get_pointer (ctx
);
3798 struct argv_s
*args
[] = {
3799 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3803 rc
= parse_options (&line
, args
, client
);
3805 return send_error (ctx
, rc
);
3807 if (client
->opts
& OPT_INQUIRE
)
3809 unsigned char *result
;
3812 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3814 return send_error (ctx
, rc
);
3816 pthread_cleanup_push (xfree
, result
);
3817 rc
= do_copy (ctx
, (char *)result
);
3818 pthread_cleanup_pop (1);
3821 rc
= do_copy (ctx
, line
);
3823 return send_error (ctx
, rc
);
3827 do_move (assuan_context_t ctx
, char *line
)
3829 struct client_s
*client
= assuan_get_pointer (ctx
);
3831 char **req
, **src
= NULL
, **dst
= NULL
;
3832 xmlNodePtr nsrc
, ndst
= NULL
;
3834 req
= str_split (line
, " ", 0);
3836 if (!req
|| !req
[0] || !req
[1])
3839 return GPG_ERR_SYNTAX
;
3842 if (strchr (req
[0], '\t'))
3843 src
= str_split (req
[0], "\t", 0);
3845 src
= str_split (req
[0], " ", 0);
3849 rc
= GPG_ERR_SYNTAX
;
3853 if (strchr (req
[1], '\t'))
3854 dst
= str_split (req
[1], "\t", 0);
3856 dst
= str_split (req
[1], " ", 0);
3858 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3860 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3861 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3866 rc
= is_element_owner (client
, nsrc
);
3872 if (!valid_element_path (dst
, 0))
3874 rc
= GPG_ERR_INV_VALUE
;
3878 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3880 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3881 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3884 ndst
= xmlDocGetRootElement (client
->doc
);
3886 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3890 rc
= GPG_ERR_CONFLICT
;
3895 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3902 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3904 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
3916 if (ndst
== xmlDocGetRootElement (client
->doc
))
3918 xmlNodePtr n
= nsrc
;
3921 while (n
->parent
&& n
->parent
!= ndst
)
3924 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3925 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3927 if (xmlStrEqual (a
, b
))
3930 xmlUnlinkNode (nsrc
);
3932 xmlFreeNodeList (n
);
3940 xmlUnlinkNode (dup
);
3941 xmlFreeNodeList (dup
);
3945 xmlUnlinkNode (dup
);
3951 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3953 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3954 && !strcmp ((char *) name
, *dst
))
3957 rc
= GPG_ERR_CONFLICT
;
3962 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3968 update_element_mtime (client
, nsrc
->parent
);
3969 xmlUnlinkNode (nsrc
);
3970 ndst
= xmlAddChildList (ndst
, nsrc
);
3973 rc
= GPG_ERR_ENOMEM
;
3975 update_element_mtime (client
, ndst
->parent
);
3991 move_command (assuan_context_t ctx
, char *line
)
3993 struct client_s
*client
= assuan_get_pointer (ctx
);
3995 struct argv_s
*args
[] = {
3996 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
4000 rc
= parse_options (&line
, args
, client
);
4002 return send_error (ctx
, rc
);
4004 if (client
->opts
& OPT_INQUIRE
)
4006 unsigned char *result
;
4009 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
4011 return send_error (ctx
, rc
);
4013 pthread_cleanup_push (xfree
, result
);
4014 rc
= do_move (ctx
, (char *)result
);
4015 pthread_cleanup_pop (1);
4018 rc
= do_move (ctx
, line
);
4020 return send_error (ctx
, rc
);
4024 ls_command (assuan_context_t ctx
, char *line
)
4027 char *tmp
= str_asprintf ("%s/data", homedir
);
4028 char *dir
= expand_homedir (tmp
);
4029 DIR *d
= opendir (dir
);
4031 rc
= gpg_error_from_errno (errno
);
4037 return send_error (ctx
, rc
);
4041 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
4042 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
4046 pthread_cleanup_push (xfree
, p
);
4047 pthread_cleanup_push ((void *)(void *)closedir
, d
);
4050 while (!readdir_r (d
, p
, &cur
) && cur
)
4052 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
4054 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
4055 && cur
->d_name
[2] == '\0')
4058 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
4065 rc
= GPG_ERR_ENOMEM
;
4073 pthread_cleanup_pop (1); // closedir (d)
4074 pthread_cleanup_pop (1); // xfree (p)
4077 return send_error (ctx
, rc
);
4080 return send_error (ctx
, GPG_ERR_NO_DATA
);
4082 list
[strlen (list
) - 1] = 0;
4083 pthread_cleanup_push (xfree
, list
);
4084 rc
= xfer_data (ctx
, list
, strlen (list
));
4085 pthread_cleanup_pop (1);
4086 return send_error (ctx
, rc
);
4090 bye_notify (assuan_context_t ctx
, char *line
)
4092 struct client_s
*cl
= assuan_get_pointer (ctx
);
4094 cl
->thd
->state
= CLIENT_STATE_DISCON
;
4097 if (cl
->thd
->remote
)
4103 struct timeval tv
= { 0, 50000 };
4105 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
4106 if (rc
== GNUTLS_E_AGAIN
)
4107 select (0, NULL
, NULL
, NULL
, &tv
);
4109 while (rc
== GNUTLS_E_AGAIN
);
4113 /* This will let assuan_process_next() return. */
4114 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
4115 cl
->last_rc
= 0; // BYE command result
4120 reset_notify (assuan_context_t ctx
, char *line
)
4122 struct client_s
*client
= assuan_get_pointer (ctx
);
4125 cleanup_client (client
);
4131 * This is called before every Assuan command.
4134 command_startup (assuan_context_t ctx
, const char *name
)
4136 struct client_s
*client
= assuan_get_pointer (ctx
);
4138 struct command_table_s
*cmd
= NULL
;
4140 log_write1 ("command='%s'", name
);
4141 client
->last_rc
= client
->opts
= 0;
4143 for (int i
= 0; command_table
[i
]; i
++)
4145 if (!strcasecmp (name
, command_table
[i
]->name
))
4147 if (command_table
[i
]->ignore_startup
)
4149 cmd
= command_table
[i
];
4154 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4156 update_client_state (client
, CLIENT_STATE_COMMAND
);
4162 * This is called after every Assuan command.
4165 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4167 struct client_s
*client
= assuan_get_pointer (ctx
);
4169 if (!(client
->flags
& FLAG_LOCK_CMD
))
4170 unlock_file_mutex (client
, 0);
4172 log_write1 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4173 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
4175 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4177 update_client_state (client
, CLIENT_STATE_IDLE
);
4181 help_command (assuan_context_t ctx
, char *line
)
4186 if (!line
|| !*line
)
4189 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
4190 "For commands that take an element path as an argument, each element is "
4191 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4194 for (i
= 0; command_table
[i
]; i
++)
4196 if (!command_table
[i
]->help
)
4199 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4204 tmp
= strip_texi_and_wrap (help
);
4206 pthread_cleanup_push (xfree
, tmp
);
4207 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4208 pthread_cleanup_pop (1);
4209 return send_error (ctx
, rc
);
4212 for (i
= 0; command_table
[i
]; i
++)
4214 if (!strcasecmp (line
, command_table
[i
]->name
))
4218 if (!command_table
[i
]->help
)
4221 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4222 tmp
= str_asprintf (_("Usage: %s"), help
);
4224 pthread_cleanup_push (xfree
, tmp
);
4225 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4226 pthread_cleanup_pop (1);
4227 return send_error (ctx
, rc
);
4231 return send_error (ctx
, GPG_ERR_INV_NAME
);
4235 new_command (const char *name
, int ignore
, int unlock
,
4236 gpg_error_t (*handler
) (assuan_context_t
, char *),
4242 for (i
= 0; command_table
[i
]; i
++);
4245 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4246 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4247 command_table
[i
]->name
= name
;
4248 command_table
[i
]->handler
= handler
;
4249 command_table
[i
]->ignore_startup
= ignore
;
4250 command_table
[i
]->unlock
= unlock
;
4251 command_table
[i
++]->help
= help
;
4252 command_table
[i
] = NULL
;
4260 for (i
= 0; command_table
[i
]; i
++)
4261 xfree (command_table
[i
]);
4263 xfree (command_table
);
4267 sort_commands (const void *arg1
, const void *arg2
)
4269 struct command_table_s
*const *a
= arg1
;
4270 struct command_table_s
*const *b
= arg2
;
4279 return strcmp ((*a
)->name
, (*b
)->name
);
4282 // FIXME cleanup/implement options
4284 passwd_command (assuan_context_t ctx
, char *line
)
4286 struct client_s
*client
= assuan_get_pointer (ctx
);
4288 struct argv_s
*args
[] = {
4289 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4290 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4291 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4295 rc
= peer_is_invoker (client
);
4296 if (rc
== GPG_ERR_EACCES
)
4297 return send_error (ctx
, rc
);
4299 if (client
->flags
& FLAG_NEW
)
4300 return send_error (ctx
, GPG_ERR_INV_STATE
);
4302 client
->crypto
->save
.s2k_count
=
4303 config_get_ulong (client
->filename
, "s2k_count");
4304 rc
= parse_options (&line
, args
, client
);
4306 return send_error (ctx
, rc
);
4308 if (!rc
&& client
->opts
& OPT_RESET
)
4310 rc
= cache_clear (client
->md5file
);
4312 send_status_all (STATUS_CACHE
, NULL
);
4317 if (!IS_PKI (client
->crypto
))
4319 struct crypto_s
*crypto
;
4321 xfree (client
->crypto
->filename
);
4322 client
->crypto
->filename
= str_dup (client
->filename
);
4323 rc
= change_passwd (ctx
, client
->filename
,
4324 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4325 (client
->opts
& OPT_NO_PASSPHRASE
));
4328 cleanup_crypto (&client
->crypto
);
4329 client
->crypto
= crypto
;
4330 update_checksum (client
);
4331 cleanup_crypto_stage1 (client
->crypto
);
4337 if (client
->crypto
->save
.s2k_count
)
4338 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4339 "OPTION s2k-count=%lu",
4340 client
->crypto
->save
.s2k_count
);
4343 rc
= agent_passwd (client
->crypto
);
4348 return send_error (ctx
, rc
);
4352 parse_keygrip_opt_sign (void *data
, void *value
)
4354 struct client_s
*client
= data
;
4357 client
->opts
|= OPT_SIGN
;
4362 keygrip_command (assuan_context_t ctx
, char *line
)
4364 struct client_s
*client
= assuan_get_pointer (ctx
);
4366 struct crypto_s
*crypto
= NULL
;
4367 struct argv_s
*args
[] = {
4368 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4372 if (!line
|| !*line
)
4373 return send_error (ctx
, GPG_ERR_SYNTAX
);
4375 rc
= parse_options (&line
, args
, client
);
4377 return send_error (ctx
, rc
);
4379 if (!valid_filename (line
))
4380 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4382 rc
= init_client_crypto (&crypto
);
4384 return send_error (ctx
, rc
);
4386 rc
= read_data_file (line
, crypto
);
4389 char *hexgrip
= NULL
;
4391 if (!IS_PKI (crypto
))
4393 cleanup_crypto (&crypto
);
4394 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4397 if (client
->opts
& OPT_SIGN
)
4399 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4400 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4404 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4407 rc
= GPG_ERR_ENOMEM
;
4409 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4414 UPDATE_AGENT_CTX (client
, crypto
);
4415 cleanup_crypto (&crypto
);
4416 return send_error (ctx
, rc
);
4420 parse_opt_data (void *data
, void *value
)
4422 struct client_s
*client
= data
;
4425 client
->opts
|= OPT_DATA
;
4430 send_client_list (assuan_context_t ctx
)
4432 struct client_s
*client
= assuan_get_pointer (ctx
);
4434 char buf
[ASSUAN_LINELENGTH
];
4436 if (client
->opts
& OPT_VERBOSE
)
4442 MUTEX_LOCK (&cn_mutex
);
4443 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4444 t
= slist_length (cn_thread_list
);
4446 for (i
= 0; i
< t
; i
++)
4448 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4451 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4454 tmp
= build_client_info_line (thd
, 0);
4457 char **l
= strv_cat (list
, tmp
);
4459 rc
= GPG_ERR_ENOMEM
;
4464 rc
= GPG_ERR_ENOMEM
;
4473 pthread_cleanup_pop (1);
4477 line
= strv_join ("\n", list
);
4479 pthread_cleanup_push (xfree
, line
);
4480 rc
= xfer_data (ctx
, line
, strlen (line
));
4481 pthread_cleanup_pop (1);
4485 if (client
->opts
& OPT_DATA
)
4487 MUTEX_LOCK (&cn_mutex
);
4488 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4489 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4490 pthread_cleanup_pop (1);
4491 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4494 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4500 getinfo_command (assuan_context_t ctx
, char *line
)
4502 struct client_s
*client
= assuan_get_pointer (ctx
);
4504 char buf
[ASSUAN_LINELENGTH
];
4505 struct argv_s
*args
[] = {
4506 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4507 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4511 rc
= parse_options (&line
, args
, client
);
4513 return send_error (ctx
, rc
);
4515 if (!strcasecmp (line
, "clients"))
4517 rc
= send_client_list (ctx
);
4519 else if (!strcasecmp (line
, "cache"))
4521 if (client
->opts
& OPT_DATA
)
4523 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4524 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4527 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4529 else if (!strcasecmp (line
, "pid"))
4532 pid_t pid
= getpid ();
4534 snprintf (buf
, sizeof (buf
), "%i", pid
);
4535 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4537 else if (!strcasecmp (line
, "version"))
4539 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4546 "", use_agent
? "AGENT" : "");
4547 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4550 else if (!strcasecmp (line
, "last_error"))
4552 if (client
->last_error
)
4553 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4555 rc
= GPG_ERR_NO_DATA
;
4557 else if (!strcasecmp (line
, "user"))
4562 if (client
->thd
->remote
)
4563 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4565 user
= get_username (client
->thd
->peer
->uid
);
4567 user
= get_username (client
->thd
->peer
->uid
);
4571 pthread_cleanup_push (xfree
, user
);
4572 rc
= xfer_data (ctx
, user
, strlen (user
));
4573 pthread_cleanup_pop (1);
4576 rc
= GPG_ERR_NO_DATA
;
4579 rc
= gpg_error (GPG_ERR_SYNTAX
);
4581 return send_error (ctx
, rc
);
4586 send_data_cb (void *user
, const void *buf
, size_t len
)
4588 assuan_context_t ctx
= user
;
4590 return assuan_send_data (ctx
, buf
, len
);
4594 send_status_cb (void *user
, const char *line
)
4596 assuan_context_t ctx
= user
;
4597 char keyword
[200], *k
;
4600 for (p
= line
, k
= keyword
; *p
; p
++)
4612 while (isspace (*p
))
4615 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4620 kill_command (assuan_context_t ctx
, char *line
)
4622 #ifdef HAVE_PTHREAD_CANCEL
4623 struct client_s
*client
= assuan_get_pointer (ctx
);
4626 if (!line
|| !*line
)
4627 return send_error (ctx
, GPG_ERR_SYNTAX
);
4629 rc
= peer_is_invoker (client
);
4634 MUTEX_LOCK (&cn_mutex
);
4635 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
4636 t
= slist_length (cn_thread_list
);
4639 for (i
= 0; i
< t
; i
++)
4641 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4642 char *tmp
= str_asprintf ("%p", thd
->tid
);
4644 if (strcmp (line
, tmp
))
4651 rc
= pthread_cancel (thd
->tid
);
4655 pthread_cleanup_pop (1);
4658 return send_error (ctx
, rc
);
4660 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4665 agent_command (assuan_context_t ctx
, char *line
)
4670 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4673 struct client_s
*client
= assuan_get_pointer (ctx
);
4675 if (!line
|| !*line
)
4676 return send_error (ctx
, GPG_ERR_SYNTAX
);
4678 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4679 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4680 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4681 send_status_cb
, client
->ctx
);
4682 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4687 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4690 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4692 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4696 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4698 return send_error (ctx
, rc
);
4704 /* !BEGIN-HELP-TEXT!
4706 * This comment is used as a marker to generate the offline documentation
4707 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4708 * script to determine where commands begin and end.
4710 new_command("HELP", 1, 1, help_command
, _(
4711 "HELP [<COMMAND>]\n"
4712 "Show available commands or command specific help text."
4715 new_command("AGENT", 1, 1, agent_command
, _(
4717 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4718 "@command{gpg-agent}."
4721 new_command("KILL", 1, 0, kill_command
, _(
4722 "KILL <thread_id>\n"
4723 "Terminates the client identified by @var{thread_id} and releases any file "
4724 "lock or other resources it has held. @xref{GETINFO} for details about listing "
4725 "connected clients.\n"
4728 new_command("GETINFO", 1, 1, getinfo_command
, _(
4729 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4730 "Get server and other information: @var{CACHE} returns the number of cached "
4731 "documents via a status message. @var{CLIENTS} returns the number of "
4732 "connected clients via a status message or a list of connected clients when "
4733 "the @option{--verbose} parameter is used. The list contains space delimited "
4734 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4735 "file lock status, whether the current client is self, client state and "
4736 "user ID or TLS fingerprint of the connected client. "
4737 "Client state @code{0} is an unknown client state, @code{1} indicates the "
4738 "client has connected but hasn't completed initializing, @code{2} indicates "
4739 "that the client is idle, @code{3} means the "
4740 "client is in a command and @code{4} means the client is disconnecting. This "
4741 "line is always returned with a data response. @var{PID} returns the process "
4742 "ID number of the server via a data response. @var{VERSION} returns the server "
4743 "version number and compile-time features with a data response with each "
4744 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4745 "the last failed command when available. @var{USER} returns the username or "
4746 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4748 "When the @option{--data} option is specified then the result will be sent "
4749 "via a data response rather than a status message."
4752 new_command("PASSWD", 0, 0, passwd_command
, _(
4753 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4754 "Changes the passphrase of the secret key required to open the current "
4755 "file or the passphrase of a symmetrically encrypted data file. When the "
4756 "@option{--reset} option is passed then the cache entry for the current "
4757 "file will be reset and the passphrase, if any, will be required during the "
4758 "next @code{OPEN} (@pxref{OPEN})."
4760 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4761 "of hash iterations for a passphrase and must be either @code{0} to use "
4762 "the calibrated count of the machine (the default), or a value greater than "
4763 "or equal to @code{65536}. This option has no effect for symmetrically "
4764 "encrypted data files."
4766 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4767 "the data file, although a passphrase may be required when changing it."
4769 "This command is not available for non-invoking clients "
4770 "(@pxref{Access Control})."
4773 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4774 "KEYGRIP [--sign] <filename>\n"
4775 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4778 "When the @option{--sign} option is specified then the key used for signing "
4779 "of the specified @var{filename} will be returned."
4781 "For symmetrically encrypted data files this command returns the error "
4782 "GPG_ERR_NOT_SUPPORTED."
4785 new_command("OPEN", 1, 1, open_command
, _(
4786 "OPEN [--lock] <filename> [<passphrase>]\n"
4787 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4788 "found on the file-system then a new document will be created. If the file "
4789 "is found, it is looked for in the file cache. If cached and no "
4790 "@var{passphrase} was specified then the cached document is opened. When not "
4791 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4792 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4795 "When the @option{--lock} option is passed then the file mutex will be "
4796 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4797 "file has been opened."
4800 new_command("SAVE", 0, 0, save_command
, _(
4801 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4802 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4803 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4804 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4805 "keypair will be generated and a pinentry will be used to prompt for the "
4806 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4807 "passed in which case the data file will not be passphrase protected. "
4809 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4810 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4811 "use is enabled. The datafile will be symmetrically encrypted and will not "
4812 "use or generate any keypair."
4814 "The @option{--reset} option will clear the cache entry for the current file "
4815 "and require a passphrase, if needed, before saving."
4817 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4818 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4819 "(@pxref{Configuration}) for available ciphers."
4821 "The @option{--cipher-iterations} option specifies the number of times to "
4822 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4824 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4825 "the client to obtain the key paramaters to use when generating a new "
4826 "keypair. The inquired data is expected to be an S-expression. If not "
4827 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4828 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4829 "that when this option is specified a new keypair will be generated "
4830 "reguardless if the file is a new one and that if the data file is protected "
4831 "the passphrase to open it will be required before generating the new "
4832 "keypair. This option is not available for non-invoking clients "
4833 "(@pxref{Access Control})."
4835 "You can encrypt the data file to a public key other than the one that it "
4836 "was originally encrypted with by passing the @option{--keygrip} option with "
4837 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4838 "be of any key that @command{gpg-agent} knows about. The "
4839 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4840 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4841 "keygrip of an existing data file. This option may be needed when using a "
4842 "smartcard. This option has no effect with symmetrically encrypted data "
4843 "files. These options are not available for non-invoking clients "
4844 "(@pxref{Access Control})."
4846 "The @option{--s2k-count} option sets number of hash iterations for a "
4847 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4848 "value and is the default. This setting only affects new files. To change "
4849 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4850 "has no effect with symmetrically encrypted data files."
4853 new_command("ISCACHED", 1, 0, iscached_command
, _(
4854 "ISCACHED [--lock] <filename>\n"
4855 "An @emph{OK} response is returned if the specified @var{filename} is found "
4856 "in the file cache. If not found in the cache but exists on the filesystem "
4857 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4860 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4861 "file exists; it does not need to be opened nor cached. The lock will be "
4862 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4866 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4867 "CLEARCACHE [<filename>]\n"
4868 "Clears a file cache entry for all or the specified @var{filename}."
4871 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4872 "CACHETIMEOUT <filename> <seconds>\n"
4873 "The time in @var{seconds} until @var{filename} will be removed from the "
4874 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4875 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4876 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4880 new_command("LIST", 0, 1, list_command
, _(
4881 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4882 "If no element path is given then a newline separated list of root elements "
4883 "is returned with a data response. If given, then all reachable elements "
4884 "of the specified element path are returned unless the @option{--no-recurse} "
4885 "option is specified. If specified, only the child elements of the element "
4886 "path are returned without recursing into grandchildren. Each resulting "
4887 "element is prefixed with the literal @code{!} character when the element "
4888 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4890 "When the @option{--verbose} option is passed then each element path "
4891 "returned will have zero or more flags appened to it. These flags are "
4892 "delimited from the element path by a single space character. A flag itself "
4893 "is a single character. Flag @code{P} indicates that access to the element "
4894 "is denied. Flag @code{+} indicates that there are child nodes of "
4895 "the current element path. Flag @code{E} indicates that an element of an "
4896 "element path contained in a @var{target} attribute could not be found. Flag "
4897 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4898 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4899 "of the @var{target} attribute contained in the current element (see below)."
4901 "The @option{--with-target} option implies @option{--verbose} and will append "
4902 "an additional flag @code{T} followed by a single space then an element path. "
4903 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4904 "current element when it contains a @var{target} attribute. When no "
4905 "@var{target} attribute is found then no flag will be appended."
4907 "The @option{--no-recurse} option limits the amount of data returned to only "
4908 "the listing of children of the specified element path and not any "
4911 "The @option{--all} option lists the entire element tree for each root "
4912 "element. This option also implies option @option{--verbose}."
4914 "When the @option{--inquire} option is passed then all remaining non-option "
4915 "arguments are retrieved via a server @emph{INQUIRE}."
4918 new_command("REALPATH", 0, 1, realpath_command
, _(
4919 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4920 "Resolves all @code{target} attributes of the specified element path and "
4921 "returns the result with a data response. @xref{Target Attribute}, for details."
4923 "When the @option{--inquire} option is passed then all remaining non-option "
4924 "arguments are retrieved via a server @emph{INQUIRE}."
4927 new_command("STORE", 0, 1, store_command
, _(
4928 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4929 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4931 "Creates a new element path or modifies the @var{content} of an existing "
4932 "element. If only a single element is specified then a new root element is "
4933 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4934 "set to the final @key{TAB} delimited element. If no @var{content} is "
4935 "specified after the final @key{TAB}, then the content of an existing "
4936 "element will be removed; or empty when creating a new element."
4938 "The only restriction of an element name is that it not contain whitespace "
4939 "or begin with the literal element character @code{!} unless specifying a "
4940 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4941 "the @key{TAB} delimited elements. It is recommended that the content of an "
4942 "element be base64 encoded when it contains control or @key{TAB} characters "
4943 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4946 new_command("RENAME", 0, 1, rename_command
, _(
4947 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4948 "Renames the specified @var{element} to the new @var{value}. If an element of "
4949 "the same name as the @var{value} already exists it will be overwritten."
4951 "When the @option{--inquire} option is passed then all remaining non-option "
4952 "arguments are retrieved via a server @emph{INQUIRE}."
4955 new_command("COPY", 0, 1, copy_command
, _(
4956 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4957 "Copies the entire element tree starting from the child node of the source "
4958 "element, to the destination element path. If the destination element path "
4959 "does not exist then it will be created; otherwise it is overwritten."
4961 "Note that attributes from the source element are merged into the "
4962 "destination element when the destination element path exists. When an "
4963 "attribute of the same name exists in both the source and destination "
4964 "elements then the destination attribute will be updated to the source "
4967 "When the @option{--inquire} option is passed then all remaining non-option "
4968 "arguments are retrieved via a server @emph{INQUIRE}."
4971 new_command("MOVE", 0, 1, move_command
, _(
4972 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4973 "Moves the source element path to the destination element path. If the "
4974 "destination is not specified then it will be moved to the root node of the "
4975 "document. If the destination is specified and exists then it will be "
4976 "overwritten; otherwise non-existing elements of the destination element "
4977 "path will be created."
4979 "When the @option{--inquire} option is passed then all remaining non-option "
4980 "arguments are retrieved via a server @emph{INQUIRE}."
4983 new_command("DELETE", 0, 1, delete_command
, _(
4984 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4985 "Removes the specified element path and all of its children. This may break "
4986 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4987 "refers to this element or any of its children."
4989 "When the @option{--inquire} option is passed then all remaining non-option "
4990 "arguments are retrieved via a server @emph{INQUIRE}."
4993 new_command("GET", 0, 1, get_command
, _(
4994 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4995 "Retrieves the content of the specified element. The content is returned "
4996 "with a data response."
4998 "When the @option{--inquire} option is passed then all remaining non-option "
4999 "arguments are retrieved via a server @emph{INQUIRE}."
5002 new_command("ATTR", 0, 1, attr_command
, _(
5003 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
5005 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
5007 " Stores or updates an @var{attribute} name and optional @var{value} of an "
5008 "element. When no @var{value} is specified any existing value will be removed."
5010 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
5012 " Removes an @var{attribute} from an element."
5014 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
5016 " Retrieves a newline separated list of attributes names and values "
5017 "from the specified element. Each attribute name and value is space delimited."
5019 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
5021 " Retrieves the value of an @var{attribute} from an element."
5024 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
5025 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
5028 "The @code{_mtime} attribute is updated each time an element is modified by "
5029 "either storing content, editing attributes or by deleting a child element. "
5030 "The @code{_ctime} attribute is created for each new element in an element "
5033 "When the @option{--inquire} option is passed then all remaining non-option "
5034 "arguments are retrieved via a server @emph{INQUIRE}."
5036 "@xref{Target Attribute}, for details about this special attribute."
5039 new_command("XPATH", 0, 1, xpath_command
, _(
5040 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
5041 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
5042 "specified it is assumed the expression is a request to return a result. "
5043 "Otherwise, the result is set to the @var{value} argument and the document is "
5044 "updated. If there is no @var{value} after the @key{TAB} character, the value "
5045 "is assumed to be empty and the document is updated. For example:"
5048 "XPATH //element[@@_name='password']@key{TAB}\n"
5051 "would clear the content of all @code{password} elements in the data file "
5052 "while leaving off the trailing @key{TAB} would return all @code{password} "
5053 "elements in @abbr{XML} format."
5055 "When the @option{--inquire} option is passed then all remaining non-option "
5056 "arguments are retrieved via a server @emph{INQUIRE}."
5058 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5059 "expression syntax."
5062 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
5063 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
5064 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
5065 "attributes and does not return a result. For the @var{SET} operation the "
5066 "@var{value} is optional but the field is required. If not specified then "
5067 "the attribute value will be empty. For example:"
5070 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
5073 "would create an @code{password} attribute for each @code{password} element "
5074 "found in the document. The attribute value will be empty but still exist."
5076 "When the @option{--inquire} option is passed then all remaining non-option "
5077 "arguments are retrieved via a server @emph{INQUIRE}."
5079 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
5080 "expression syntax."
5083 new_command("IMPORT", 0, 1, import_command
, _(
5084 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
5085 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5087 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
5088 "argument is raw @abbr{XML} data. The content is created as a child of "
5089 "the element path specified with the @option{--root} option or at the "
5090 "document root when not specified. Existing elements of the same name will "
5093 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
5097 new_command("DUMP", 0, 1, dump_command
, _(
5099 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
5100 "dumping a specific node."
5103 new_command("LOCK", 0, 0, lock_command
, _(
5105 "Locks the mutex associated with the opened file. This prevents other clients "
5106 "from sending commands to the same opened file until the client "
5107 "that sent this command either disconnects or sends the @code{UNLOCK} "
5108 "command. @xref{UNLOCK}."
5111 new_command("UNLOCK", 1, 0, unlock_command
, _(
5113 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
5114 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
5115 "@pxref{ISCACHED})."
5118 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
5119 "GETCONFIG [filename] <parameter>\n"
5120 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
5121 "data response. If no file has been opened then the value for @var{filename} "
5122 "or the default from the @samp{global} section will be returned. If a file "
5123 "has been opened and no @var{filename} is specified, a value previously "
5124 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
5127 new_command("OPTION", 1, 1, option_command
, _(
5128 "OPTION <NAME>=<VALUE>\n"
5129 "Sets a client option @var{name} to @var{value}. The value for an option is "
5130 "kept for the duration of the connection."
5133 "@item DISABLE-PINENTRY\n"
5134 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
5135 "server inquire is sent to the client to obtain the passphrase. This option "
5136 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
5137 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
5139 "@item PINENTRY-TIMEOUT\n"
5140 "Sets the number of seconds before a pinentry prompt will return an error "
5141 "while waiting for user input."
5144 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5147 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5150 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5152 "@item PINENTRY-DESC\n"
5153 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
5155 "@item PINENTRY-TITLE\n"
5156 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
5158 "@item PINENTRY-PROMPT\n"
5159 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
5162 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5164 "@item LC-MESSAGES\n"
5165 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
5168 "Associates the thread ID of the connection with the specified textual "
5169 "representation. Useful for debugging log messages. May not contain whitespace."
5171 "@item LOCK-TIMEOUT\n"
5172 "When not @code{0}, the duration in tenths of a second to wait for the file "
5173 "mutex which has been locked by another thread to be released before returning "
5174 "an error. When @code{-1}, then an error will be returned immediately."
5177 "An integer specifiying the logging level."
5181 new_command("LS", 1, 1, ls_command
, _(
5183 "Lists the available data files stored in the data directory "
5184 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5187 new_command("RESET", 1, 1, NULL
, _(
5189 "Closes the currently opened file but keeps any previously set client options."
5192 new_command("NOP", 1, 1, NULL
, _(
5194 "Does nothing. Always returns successfully."
5197 /* !END-HELP-TEXT! */
5198 new_command ("CANCEL", 1, 1, NULL
, NULL
);
5199 new_command ("END", 1, 1, NULL
, NULL
);
5200 new_command ("BYE", 1, 1, NULL
, NULL
);
5203 for (i
= 0; command_table
[i
]; i
++);
5204 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
5209 register_commands (assuan_context_t ctx
)
5213 for (; command_table
[i
]; i
++)
5215 if (!command_table
[i
]->handler
)
5218 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5219 command_table
[i
]->handler
,
5220 command_table
[i
]->help
);
5225 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5229 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5233 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5237 return assuan_register_post_cmd_notify (ctx
, command_finalize
);