2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014
3 Ben Kibbey <bjk@luxsci.net>
5 This file is part of pwmd.
7 Pwmd is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 Pwmd is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
37 #include "pwmd-error.h"
42 #include "util-misc.h"
51 /* Flags needed to be retained for a client across commands. */
52 #define FLAG_NEW 0x0001
53 #define FLAG_HAS_LOCK 0x0002
54 #define FLAG_LOCK_CMD 0x0004
55 #define FLAG_NO_PINENTRY 0x0008
56 #define FLAG_OPEN 0x0010
57 #define FLAG_KEEP_LOCK 0x0020
59 /* These are command option flags. */
60 #define OPT_INQUIRE 0x0001
61 #define OPT_NO_PASSPHRASE 0x0002
62 #define OPT_RESET 0x0004
63 #define OPT_LIST_RECURSE 0x0008
64 #define OPT_LIST_VERBOSE 0x0010
65 #define OPT_LOCK 0x0020
66 #define OPT_LOCK_ON_OPEN 0x0040
67 #define OPT_SIGN 0x0080
68 #define OPT_LIST_ALL 0x0100
69 #define OPT_LIST_WITH_TARGET 0x0200
70 #define OPT_DATA 0x0400
71 #define OPT_NO_AGENT 0x0800
74 /* The GETCONFIG command, for example, may fail because pwmd lost the
75 * gpg-agent connection. Update the recovered agent ctx. */
76 #define UPDATE_AGENT_CTX(client, crypto) do { \
77 if (crypto && crypto->agent && crypto->agent->did_restart \
78 && client->crypto && client->crypto->agent \
79 && client->crypto->agent->ctx && \
80 crypto->agent->ctx != client->crypto->agent->ctx) \
82 client->crypto->agent->ctx = crypto->agent->ctx; \
83 crypto->agent->ctx = NULL; \
87 #define UPDATE_AGENT_CTX(client, crypto)
90 struct command_table_s
93 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
96 int unlock
; // unlock the file mutex after validating the checksum
99 static struct command_table_s
**command_table
;
101 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
102 static gpg_error_t
validate_checksum (struct client_s
*,
103 struct cache_data_s
*);
104 static gpg_error_t
update_checksum (struct client_s
*client
);
107 unlock_file_mutex (struct client_s
*client
, int remove
)
111 // OPEN: keep the lock for the same file being reopened.
112 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
115 if (!(client
->flags
& FLAG_HAS_LOCK
))
116 return GPG_ERR_NOT_LOCKED
;
118 rc
= cache_unlock_mutex (client
->md5file
, remove
);
120 rc
= GPG_ERR_INV_STATE
;
122 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
128 lock_file_mutex (struct client_s
*client
, int add
)
131 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
133 if (client
->flags
& FLAG_HAS_LOCK
)
136 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
137 client
->lock_timeout
, add
, timeout
);
139 client
->flags
|= FLAG_HAS_LOCK
;
145 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
149 if (!(client
->flags
& FLAG_OPEN
))
150 return GPG_ERR_INV_STATE
;
152 rc
= lock_file_mutex (client
, 0);
153 if (!rc
|| rc
== GPG_ERR_NO_DATA
)
155 rc
= validate_checksum (client
, NULL
);
156 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
159 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
162 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
163 unlock_file_mutex (client
, 0);
169 parse_xml (assuan_context_t ctx
, int new)
171 struct client_s
*client
= assuan_get_pointer (ctx
);
172 int cached
= client
->doc
!= NULL
;
177 client
->doc
= new_document ();
183 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
184 client
->crypto
->plaintext
= result
;
185 client
->crypto
->plaintext_len
= len
;
186 if (!client
->crypto
->plaintext
)
188 xmlFreeDoc (client
->doc
);
194 rc
= parse_doc ((char *) client
->crypto
->plaintext
,
195 client
->crypto
->plaintext_len
, (xmlDocPtr
*)&client
->doc
);
201 free_client (struct client_s
*client
)
204 xmlFreeDoc (client
->doc
);
207 xfree (client
->filename
);
208 xfree (client
->last_error
);
212 cleanup_crypto_stage2 (client
->crypto
);
213 if (client
->crypto
->pkey_sexp
)
214 gcry_sexp_release (client
->crypto
->pkey_sexp
);
216 if (client
->crypto
->sigpkey_sexp
)
217 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
219 client
->crypto
->pkey_sexp
= NULL
;
220 client
->crypto
->sigpkey_sexp
= NULL
;
221 memset (client
->crypto
->sign_grip
, 0,
222 sizeof (client
->crypto
->sign_grip
));
223 memset (client
->crypto
->grip
, 0, sizeof(client
->crypto
->grip
));
228 cleanup_client (struct client_s
*client
)
230 assuan_context_t ctx
= client
->ctx
;
231 struct client_thread_s
*thd
= client
->thd
;
232 struct crypto_s
*crypto
= client
->crypto
;
233 long lock_timeout
= client
->lock_timeout
;
234 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
235 struct pinentry_option_s pin_opts
;
236 xmlErrorPtr xml_error
= client
->xml_error
;
238 struct pinentry_option_s agent_pin_opts
;
240 if (crypto
&& crypto
->agent
)
241 memcpy (&agent_pin_opts
, &crypto
->agent
->pinentry_opts
,
242 sizeof(struct pinentry_option_s
));
245 memcpy (&pin_opts
, &client
->pinentry_opts
, sizeof(struct pinentry_option_s
));
246 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
247 free_client (client
);
248 memset (client
, 0, sizeof (struct client_s
));
249 client
->xml_error
= xml_error
;
250 client
->crypto
= crypto
;
253 client
->lock_timeout
= lock_timeout
;
254 memcpy (&client
->pinentry_opts
, &pin_opts
, sizeof(struct pinentry_option_s
));
255 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
257 if (crypto
&& crypto
->agent
)
258 memcpy (&crypto
->agent
->pinentry_opts
, &agent_pin_opts
,
259 sizeof(struct pinentry_option_s
));
264 open_finalize (assuan_context_t ctx
, char *key
, size_t keylen
)
266 struct client_s
*client
= assuan_get_pointer (ctx
);
268 struct cache_data_s
*cdata
= cache_get_data (client
->md5file
);
269 int cached
= 0, keyarg
= key
? 1 : 0;
271 unsigned char *salted_key
= NULL
;
275 char *pin_title
= client
->pinentry_opts
.title
;
277 client
->crypto
->filename
= str_dup (client
->filename
);
278 if (cdata
|| client
->flags
& FLAG_NEW
)
280 if (cdata
&& !(client
->flags
& FLAG_NEW
))
282 rc
= decrypt_cache (client
->crypto
, cdata
->doc
, cdata
->doclen
);
287 cached
= cdata
!= NULL
;
291 if (!key
&& !IS_PKI (client
->crypto
)
292 && !(client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
))
294 if (client
->flags
& FLAG_NO_PINENTRY
)
296 rc
= inquire_passphrase (ctx
, "PASSPHRASE", (unsigned char **)&key
,
303 client
->pinentry_opts
.timeout
= config_get_integer (client
->filename
,
306 rc
= getpin_common (client
->ctx
, client
->filename
, PINENTRY_OPEN
,
313 if (!IS_PKI (client
->crypto
))
315 if (client
->crypto
->hdr
.flags
& PWMD_FLAG_NO_PASSPHRASE
)
318 key
= gcry_malloc (keylen
);
319 memset (key
, 0, keylen
);
322 algo
= cipher_to_gcrypt (client
->crypto
->hdr
.flags
);
323 rc
= hash_key (algo
, client
->crypto
->hdr
.salt
,
324 sizeof(client
->crypto
->hdr
.salt
), key
, keylen
,
325 (void **)&salted_key
, &keysize
);
331 rc
= decrypt_data (client
->ctx
, client
->crypto
, salted_key
, keysize
);
332 if (gpg_err_code (rc
) == GPG_ERR_BAD_PASSPHRASE
333 && ++pin_try
<= pin_tries
&& !(client
->flags
& FLAG_NO_PINENTRY
))
335 gcry_free (salted_key
);
339 client
->pinentry_opts
.title
= str_asprintf (_ ("Bad passphrase (try %i of %i)"), pin_try
, pin_tries
);
344 if (client
->pinentry_opts
.title
!= pin_title
)
345 xfree (client
->pinentry_opts
.title
);
347 client
->pinentry_opts
.title
= pin_title
;
350 gcry_free (salted_key
);
354 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
357 gcry_free (salted_key
);
358 return GPG_ERR_ENOMEM
;
361 cdata
->key
= salted_key
;
362 cdata
->keylen
= keysize
;
365 rc
= decrypt_data (client
->ctx
, client
->crypto
, NULL
, 0);
370 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
372 free_cache_data_once (cdata
);
376 int timeout
= config_get_integer (client
->filename
,
382 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
384 rc
= encrypt_xml (NULL
, cache_key
, cache_keysize
,
385 GCRY_CIPHER_AES
, client
->crypto
->plaintext
,
386 client
->crypto
->plaintext_len
, &cdata
->doc
,
387 &cdata
->doclen
, &cache_iv
, &cache_blocksize
,
389 if (!rc
&& !(client
->flags
& FLAG_NEW
) && IS_PKI (client
->crypto
))
391 rc
= gcry_sexp_build ((gcry_sexp_t
*) & cdata
->pubkey
, NULL
,
392 "%S", client
->crypto
->pkey_sexp
);
397 gcry_sexp_release (cdata
->sigkey
);
399 cdata
->sigkey
= NULL
;
400 if (!rc
&& IS_PKI (client
->crypto
))
401 rc
= gcry_sexp_build ((gcry_sexp_t
*) &cdata
->sigkey
, NULL
,
402 "%S", client
->crypto
->sigpkey_sexp
);
406 rc
= cache_add_file (client
->md5file
,
407 (client
->flags
& FLAG_NEW
) ? NULL
408 : client
->crypto
->grip
, cdata
, timeout
);
413 free_cache_data_once (cdata
);
414 xmlFreeDoc (client
->doc
);
421 client
->flags
|= FLAG_OPEN
;
426 update_checksum (client
);
428 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
429 rc
= do_lock (client
, 0);
435 req_cleanup (void *arg
)
440 strv_free ((char **) arg
);
444 parse_open_opt_lock (void *data
, void *value
)
446 struct client_s
*client
= data
;
448 client
->opts
|= OPT_LOCK_ON_OPEN
;
453 parse_save_opt_inquire (void *data
, void *value
)
455 struct client_s
*client
= data
;
458 if (!(client
->flags
& FLAG_NEW
))
460 rc
= peer_is_invoker (client
);
461 if (rc
== GPG_ERR_EACCES
)
466 client
->opts
|= OPT_INQUIRE
;
471 parse_opt_inquire (void *data
, void *value
)
473 struct client_s
*client
= data
;
476 client
->opts
|= OPT_INQUIRE
;
481 update_checksum (struct client_s
*client
)
485 struct cache_data_s
*cdata
;
486 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
493 cdata
= cache_get_data (client
->md5file
);
497 cdata
->crc
= xmalloc (len
);
498 memcpy (cdata
->crc
, crc
, len
);
505 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
)
512 if (cdata
&& !cdata
->crc
)
513 return GPG_ERR_CHECKSUM
;
515 rc
= get_checksum (client
->filename
, &crc
, &len
);
520 n
= memcmp (cdata
->crc
, crc
, len
);
521 else if (client
->crc
)
522 n
= memcmp (client
->crc
, crc
, len
);
525 return n
? GPG_ERR_CHECKSUM
: 0;
529 do_open (assuan_context_t ctx
, const char *password
)
531 struct client_s
*client
= assuan_get_pointer (ctx
);
532 struct cache_data_s
*cdata
;
537 cdata
= cache_get_data (client
->md5file
);
538 if (cdata
&& cdata
->doc
)
542 /* This will check that the key is cached in the agent which needs to
543 * be determined for files that share a keygrip. */
546 rc
= cache_iscached (client
->filename
, &defer
);
547 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
548 rc
= GPG_ERR_KEY_EXPIRED
;
551 if (!rc
&& !(client
->flags
& FLAG_NEW
))
552 rc
= validate_checksum (client
, cdata
);
555 if (!rc
&& client
->thd
->remote
556 && config_get_boolean (client
->filename
, "tcp_require_key"))
557 rc
= GPG_ERR_KEY_EXPIRED
;
560 if (!rc
&& !password
)
562 rc
= read_data_header (client
->filename
, &client
->crypto
->hdr
,
566 if (IS_PKI (client
->crypto
))
568 gcry_sexp_build (&client
->crypto
->pkey_sexp
, NULL
, "%S",
570 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
571 client
->crypto
->grip
);
572 gcry_sexp_build (&client
->crypto
->sigpkey_sexp
, NULL
, "%S",
574 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
575 client
->crypto
->sign_grip
);
580 rc
= open_finalize (ctx
, NULL
, 0);
586 /* There was an error accessing the file so clear the cache entry. The
587 * real error will be returned from read_data_file() since the file
588 * may have only disappeared. */
591 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
592 rc
= cache_clear (client
->md5file
);
593 send_status_all (STATUS_CACHE
, NULL
);
600 rc
= read_data_file (client
->filename
, client
->crypto
);
603 if (gpg_err_source (rc
) != GPG_ERR_SOURCE_DEFAULT
||
604 gpg_err_code (rc
) != GPG_ERR_ENOENT
)
606 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
610 client
->flags
|= FLAG_NEW
;
611 memset (client
->crypto
->grip
, 0, sizeof (client
->crypto
->grip
));
612 memset (client
->crypto
->sign_grip
, 0,
613 sizeof (client
->crypto
->sign_grip
));
614 rc
= open_finalize (ctx
, NULL
, 0);
618 if (password
&& IS_PKI (client
->crypto
))
621 rc
= set_agent_passphrase (client
->crypto
, password
, strlen (password
));
627 rc
= open_finalize (ctx
, (char *)password
, password
? strlen (password
) : 0);
632 open_command (assuan_context_t ctx
, char *line
)
635 struct client_s
*client
= assuan_get_pointer (ctx
);
636 char **req
, *filename
;
637 unsigned char md5file
[16];
639 assuan_peercred_t peer
;
640 struct argv_s
*args
[] = {
641 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
645 rc
= parse_options (&line
, args
, client
);
647 return send_error (ctx
, rc
);
649 req
= str_split (line
, " ", 2);
651 return send_error (ctx
, GPG_ERR_SYNTAX
);
653 rc
= do_validate_peer (ctx
, req
[0], &peer
);
657 return send_error (ctx
, rc
);
661 if (!valid_filename (filename
))
664 return send_error (ctx
, GPG_ERR_INV_VALUE
);
667 pthread_cleanup_push (req_cleanup
, req
);
668 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
669 /* This client may have locked a different file with ISCACHED --lock than
670 * the current filename. This will remove that lock. */
671 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
672 if (client
->flags
& FLAG_OPEN
||
673 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
675 uint32_t opts
= client
->opts
;
676 uint32_t flags
= client
->flags
;
679 client
->flags
|= FLAG_KEEP_LOCK
;
680 else if (client
->flags
& FLAG_NEW
)
681 cache_clear (client
->md5file
);
683 cleanup_client (client
);
685 client
->flags
|= flags
;
686 client
->flags
&= ~(FLAG_LOCK_CMD
);
688 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
691 memcpy (client
->md5file
, md5file
, 16);
692 client
->filename
= str_dup (filename
);
693 if (!client
->filename
)
696 return send_error(ctx
, GPG_ERR_ENOMEM
);
699 /* Need to lock the mutex here because file_modified() cannot without
700 * knowing the filename. */
701 rc
= lock_file_mutex (client
, 1);
703 client
->flags
&= ~FLAG_OPEN
;
707 char *password
= req
[1] && *req
[1] ? req
[1] : NULL
;
710 if (IS_PKI (client
->crypto
))
711 rc
= set_pinentry_mode (client
->crypto
->agent
,
712 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
717 rc
= do_open (ctx
, password
);
719 cleanup_client (client
);
721 cleanup_crypto_stage1 (client
->crypto
);
725 pthread_cleanup_pop (1);
727 if (!rc
&& client
->flags
& FLAG_NEW
)
728 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
731 (void) kill_scd (client
->crypto
->agent
);
734 return send_error (ctx
, rc
);
738 parse_opt_no_passphrase (void *data
, void *value
)
740 struct client_s
*client
= data
;
743 client
->opts
|= OPT_NO_PASSPHRASE
;
748 parse_save_opt_no_agent (void *data
, void *value
)
750 struct client_s
*client
= data
;
752 client
->opts
|= OPT_NO_AGENT
;
757 parse_save_opt_cipher (void *data
, void *value
)
759 struct client_s
*client
= data
;
760 int algo
= cipher_string_to_gcrypt ((char *) value
);
761 file_header_t
*hdr
= &client
->crypto
->save
.hdr
;
765 return GPG_ERR_INV_VALUE
;
767 if (!(client
->flags
& FLAG_NEW
))
769 rc
= peer_is_invoker (client
);
770 if (rc
== GPG_ERR_EACCES
)
774 flags
&= ((uint16_t) ((uint32_t) (flags
& 0xFFFFFFFF)));
775 if (!(client
->crypto
->hdr
.flags
& gcrypt_to_cipher (algo
)))
785 hdr
->flags
= set_cipher_flag (hdr
->flags
, algo
);
792 permitted_to_save (struct client_s
*client
, const unsigned char *grip
,
793 size_t size
, const char *value
)
798 if (!(client
->flags
& FLAG_NEW
))
800 hexgrip
= bin2hex (grip
, size
);
801 rc
= !strcmp (hexgrip
, (char *)value
) ? 0 : GPG_ERR_EACCES
;
804 rc
= peer_is_invoker (client
);
812 parse_save_opt_keygrip (void *data
, void *value
)
815 struct client_s
*client
= data
;
816 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->grip
,
817 sizeof (client
->crypto
->grip
),
820 if (!IS_PKI (client
->crypto
))
821 return GPG_ERR_INV_ARG
;
826 if (client
->crypto
->save
.pkey
)
827 gcry_sexp_release (client
->crypto
->save
.pkey
);
829 client
->crypto
->save
.pkey
= NULL
;
830 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.pkey
);
832 return GPG_ERR_INV_ARG
;
837 parse_save_opt_sign_keygrip (void *data
, void *value
)
840 struct client_s
*client
= data
;
841 gpg_error_t rc
= permitted_to_save (client
, client
->crypto
->sign_grip
,
842 sizeof (client
->crypto
->sign_grip
),
845 if (!IS_PKI (client
->crypto
))
846 return GPG_ERR_INV_ARG
;
851 if (client
->crypto
->save
.sigpkey
)
852 gcry_sexp_release (client
->crypto
->save
.sigpkey
);
854 client
->crypto
->save
.sigpkey
= NULL
;
855 return get_pubkey (client
->crypto
, value
, &client
->crypto
->save
.sigpkey
);
857 return GPG_ERR_INV_ARG
;
862 parse_opt_s2k_count (void *data
, void *value
)
864 struct client_s
*client
= data
;
868 return GPG_ERR_INV_VALUE
;
870 client
->crypto
->save
.s2k_count
= strtoul (v
, NULL
, 10);
875 parse_save_opt_iterations (void *data
, void *value
)
877 struct client_s
*client
= data
;
882 return GPG_ERR_INV_VALUE
;
885 n
= strtoull (v
, &p
, 10);
886 if (n
== UINT64_MAX
&& errno
)
887 return gpg_error_from_errno (errno
);
889 return GPG_ERR_INV_VALUE
;
891 if (!(client
->flags
& FLAG_NEW
))
893 gpg_error_t rc
= peer_is_invoker (client
);
895 if (rc
== GPG_ERR_EACCES
)
897 if (client
->crypto
->hdr
.iterations
!= n
)
906 client
->crypto
->save
.hdr
.iterations
= n
;
911 save_finalize (assuan_context_t ctx
)
913 struct client_s
*client
= assuan_get_pointer (ctx
);
915 xmlChar
*xmlbuf
= NULL
;
920 xmlDocDumpFormatMemory (client
->doc
, &xmlbuf
, &xmlbuflen
, 0);
922 return GPG_ERR_ENOMEM
;
924 pthread_cleanup_push (xmlFree
, xmlbuf
);
926 if (!use_agent
|| ((client
->flags
& FLAG_NEW
)
927 && (client
->opts
& OPT_NO_AGENT
))
928 || !(client
->crypto
->hdr
.flags
& PWMD_FLAG_PKI
))
930 rc
= export_common (ctx
, client
->flags
& FLAG_NO_PINENTRY
,
931 client
->crypto
, xmlbuf
, xmlbuflen
, client
->filename
,
932 NULL
, &key
, &keylen
, 1, 1,
933 (client
->opts
& OPT_NO_PASSPHRASE
));
938 gcry_sexp_t pubkey
= client
->crypto
->save
.pkey
;
939 gcry_sexp_t sigkey
= client
->crypto
->save
.sigpkey
;
942 pubkey
= client
->crypto
->pkey_sexp
;
946 sigkey
= client
->crypto
->sigpkey_sexp
;
949 gcry_sexp_build (&client
->crypto
->save
.sigpkey
, 0, "%S", pubkey
);
950 sigkey
= client
->crypto
->save
.sigpkey
;
954 rc
= encrypt_data_file (client
->ctx
, client
->crypto
, pubkey
, sigkey
,
955 client
->filename
, xmlbuf
, xmlbuflen
);
956 if (pubkey
== client
->crypto
->save
.pkey
)
960 gcry_sexp_release (client
->crypto
->pkey_sexp
);
961 client
->crypto
->pkey_sexp
= client
->crypto
->save
.pkey
;
962 gcry_pk_get_keygrip (client
->crypto
->pkey_sexp
,
963 client
->crypto
->grip
);
966 gcry_sexp_release (pubkey
);
968 client
->crypto
->save
.pkey
= NULL
;
972 gcry_pk_get_keygrip (sigkey
, client
->crypto
->sign_grip
);
974 if (sigkey
== client
->crypto
->save
.sigpkey
)
978 if (client
->crypto
->sigpkey_sexp
)
979 gcry_sexp_release (client
->crypto
->sigpkey_sexp
);
981 client
->crypto
->sigpkey_sexp
= client
->crypto
->save
.sigpkey
;
982 gcry_pk_get_keygrip (client
->crypto
->sigpkey_sexp
,
983 client
->crypto
->sign_grip
);
986 gcry_sexp_release (sigkey
);
988 client
->crypto
->save
.sigpkey
= NULL
;
997 rc
= save_common (client
->filename
, client
->crypto
, xmlbuf
, xmlbuflen
,
998 key
, keylen
, &cached
, client
->opts
& OPT_NO_AGENT
);
1002 if (!rc
&& (!cached
|| (client
->flags
& FLAG_NEW
)))
1003 send_status_all (STATUS_CACHE
, NULL
);
1007 rc
= update_checksum (client
);
1008 client
->flags
&= ~(FLAG_NEW
);
1012 pthread_cleanup_pop (1); // xmlFree
1017 parse_opt_reset (void *data
, void *value
)
1019 struct client_s
*client
= data
;
1022 client
->opts
|= OPT_RESET
;
1027 save_command (assuan_context_t ctx
, char *line
)
1029 struct client_s
*client
= assuan_get_pointer (ctx
);
1032 struct argv_s
*args
[] = {
1033 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1034 parse_opt_no_passphrase
},
1035 &(struct argv_s
) {"cipher", OPTION_TYPE_ARG
, parse_save_opt_cipher
},
1036 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
1037 parse_save_opt_inquire
},
1038 &(struct argv_s
) {"keygrip", OPTION_TYPE_ARG
, parse_save_opt_keygrip
},
1039 &(struct argv_s
) {"sign-keygrip", OPTION_TYPE_ARG
,
1040 parse_save_opt_sign_keygrip
},
1041 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
1042 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
1043 &(struct argv_s
) {"cipher-iterations", OPTION_TYPE_ARG
,
1044 parse_save_opt_iterations
},
1045 &(struct argv_s
) {"no-agent", OPTION_TYPE_NOARG
,
1046 parse_save_opt_no_agent
},
1050 cleanup_save (&client
->crypto
->save
);
1051 memcpy (&client
->crypto
->save
.hdr
, &client
->crypto
->hdr
,
1052 sizeof (file_header_t
));
1053 client
->crypto
->save
.s2k_count
=
1054 config_get_ulong (client
->filename
, "s2k_count");
1056 if (client
->flags
& FLAG_NEW
)
1057 client
->crypto
->save
.hdr
.iterations
=
1058 config_get_ulonglong (client
->filename
, "cipher_iterations");
1060 rc
= parse_options (&line
, args
, client
);
1062 return send_error (ctx
, rc
);
1064 if (!(client
->flags
& FLAG_NEW
))
1065 client
->opts
&= ~OPT_NO_AGENT
;
1066 else if (client
->opts
& OPT_NO_AGENT
)
1068 client
->crypto
->save
.hdr
.flags
&= ~PWMD_FLAG_PKI
;
1069 client
->crypto
->hdr
.flags
&= ~PWMD_FLAG_PKI
;
1072 if ((client
->opts
& OPT_NO_PASSPHRASE
) && !(client
->flags
& FLAG_NEW
)
1073 && !(client
->opts
& OPT_INQUIRE
))
1074 return send_error (ctx
, GPG_ERR_WRONG_KEY_USAGE
);
1076 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
1077 return send_error (ctx
, gpg_error_from_errno (errno
));
1079 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
1081 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
1082 return send_error (ctx
, GPG_ERR_ENOANO
);
1086 rc
= cache_iscached (client
->filename
, &defer
);
1087 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& client
->flags
& FLAG_NEW
)
1089 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1092 if ((!rc
&& defer
) || config_get_boolean ("global", "require_save_key"))
1093 client
->opts
|= OPT_RESET
;
1095 if (!rc
&& (client
->opts
& OPT_RESET
))
1097 rc
= cache_clear (client
->md5file
);
1099 return send_error (ctx
, rc
);
1101 log_write ("%s: %s", client
->filename
,
1102 pwmd_strerror (GPG_ERR_KEY_EXPIRED
));
1103 send_status_all (STATUS_CACHE
, NULL
);
1107 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1110 return send_error (ctx
, rc
);
1113 if (!rc
&& use_agent
&& !client
->crypto
->save
.pkey
&&
1114 (client
->flags
& FLAG_NEW
|| client
->opts
& OPT_INQUIRE
)
1115 && !(client
->opts
& OPT_NO_AGENT
))
1117 rc
= set_pinentry_mode (client
->crypto
->agent
,
1118 (client
->flags
& FLAG_NO_PINENTRY
) ? "loopback"
1121 if (!(client
->flags
& FLAG_NEW
))
1123 struct crypto_s
*crypto
;
1127 /* Wanting to generate a new key. Require the key to open the
1128 current file before proceeding reguardless of the
1129 require_save_key configuration parameter. */
1130 rc
= cache_clear (client
->md5file
);
1133 rc
= init_client_crypto (&crypto
);
1135 crypto
->client_ctx
= client
->ctx
;
1139 rc
= decrypt_common (client
->ctx
, client
->opts
&OPT_INQUIRE
, crypto
,
1140 client
->filename
, &key
, &keylen
);
1143 cleanup_crypto (&crypto
);
1147 rc
= send_status (client
->ctx
, STATUS_GENKEY
, NULL
);
1151 struct inquire_data_s idata
= { 0 };
1152 char *params
= client
->opts
& OPT_INQUIRE
1153 ? NULL
: default_key_params (client
->crypto
);
1155 pthread_cleanup_push (xfree
, params
);
1156 idata
.crypto
= client
->crypto
;
1160 idata
.line
= params
;
1161 idata
.len
= strlen (params
);
1166 client
->crypto
->agent
->inquire_data
= &idata
;
1167 client
->crypto
->agent
->inquire_cb
= NULL
;
1168 rc
= generate_key (client
->crypto
, params
,
1169 (client
->opts
& OPT_NO_PASSPHRASE
), 1);
1170 pthread_cleanup_pop (1);
1176 rc
= save_finalize (ctx
);
1178 cleanup_crypto_stage1 (client
->crypto
);
1180 (void) kill_scd (client
->crypto
->agent
);
1182 return send_error (ctx
, rc
);
1186 do_delete (assuan_context_t ctx
, char *line
)
1188 struct client_s
*client
= assuan_get_pointer (ctx
);
1193 if (strchr (line
, '\t'))
1194 req
= str_split (line
, "\t", 0);
1196 req
= str_split (line
, " ", 0);
1199 return GPG_ERR_SYNTAX
;
1201 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1209 * No sub-node defined. Remove the entire node (root element).
1215 rc
= is_element_owner (client
, n
);
1218 rc
= unlink_node (client
, n
);
1227 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
,
1228 NULL
, NULL
, 0, 0, NULL
, 0);
1233 rc
= is_element_owner (client
, n
);
1236 rc
= unlink_node (client
, n
);
1244 delete_command (assuan_context_t ctx
, char *line
)
1246 struct client_s
*client
= assuan_get_pointer (ctx
);
1248 struct argv_s
*args
[] = {
1249 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1253 rc
= parse_options (&line
, args
, client
);
1255 return send_error (ctx
, rc
);
1257 if (client
->opts
& OPT_INQUIRE
)
1259 unsigned char *result
;
1262 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1264 return send_error (ctx
, rc
);
1266 line
= (char *) result
;
1269 rc
= do_delete (ctx
, line
);
1271 if (client
->opts
& OPT_INQUIRE
)
1274 return send_error (ctx
, rc
);
1278 store_command (assuan_context_t ctx
, char *line
)
1280 struct client_s
*client
= assuan_get_pointer (ctx
);
1283 unsigned char *result
;
1285 xmlNodePtr n
, parent
;
1287 char *content
= NULL
;
1289 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1291 return send_error (ctx
, rc
);
1293 req
= str_split ((char *) result
, "\t", 0);
1297 return send_error (ctx
, GPG_ERR_SYNTAX
);
1299 len
= strv_length (req
);
1300 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1301 if (*(req
+ 1) && !valid_element_path (req
, has_content
))
1304 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1307 if (has_content
|| !*req
[len
- 1])
1310 content
= req
[len
- 1];
1311 req
[len
- 1] = NULL
;
1315 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1316 if (rc
&& rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
1318 rc
= new_root_element (client
, client
->doc
, *req
);
1322 return send_error (ctx
, rc
);
1331 return send_error (ctx
, rc
);
1336 if (req
[1] && *req
[1])
1339 parent
= create_elements_cb (client
, 1, n
, req
+ 1, &rc
, NULL
);
1341 parent
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
,
1342 NULL
, NULL
, create_elements_cb
, 0, 0, NULL
,
1348 rc
= is_element_owner (client
, parent
);
1351 n
= find_text_node (parent
->children
);
1353 xmlNodeSetContent (n
, (xmlChar
*) content
);
1355 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1357 update_element_mtime (client
, parent
);
1363 return send_error (ctx
, rc
);
1367 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1372 int progress
= config_get_integer ("global", "xfer_progress");
1376 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1377 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1378 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1386 if (sent
+ to_send
> total
)
1387 to_send
= total
- sent
;
1389 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1390 flush
? 0 : to_send
);
1393 sent
+= flush
? 0 : to_send
;
1395 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1396 (sent
== total
&& flush
))
1397 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1399 if (!flush
&& !rc
&& sent
== total
)
1406 while (!rc
&& sent
< total
);
1412 do_get (assuan_context_t ctx
, char *line
)
1414 struct client_s
*client
= assuan_get_pointer (ctx
);
1419 req
= str_split (line
, "\t", 0);
1424 return GPG_ERR_SYNTAX
;
1427 n
= find_root_element (client
, client
->doc
, &req
, &rc
, NULL
, 0, 0);
1436 find_elements (client
, client
->doc
, n
->children
, req
+ 1, &rc
, NULL
, NULL
, NULL
,
1443 if (!n
|| !n
->children
)
1444 return GPG_ERR_NO_DATA
;
1446 n
= find_text_node (n
->children
);
1447 if (!n
|| !n
->content
|| !*n
->content
)
1448 return GPG_ERR_NO_DATA
;
1450 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1455 get_command (assuan_context_t ctx
, char *line
)
1457 struct client_s
*client
= assuan_get_pointer (ctx
);
1459 struct argv_s
*args
[] = {
1460 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1464 rc
= parse_options (&line
, args
, client
);
1466 return send_error (ctx
, rc
);
1468 if (client
->opts
& OPT_INQUIRE
)
1470 unsigned char *result
;
1473 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1475 return send_error (ctx
, rc
);
1477 line
= (char *) result
;
1480 rc
= do_get (ctx
, line
);
1482 if (client
->opts
& OPT_INQUIRE
)
1485 return send_error (ctx
, rc
);
1488 static void list_command_cleanup1 (void *arg
);
1490 realpath_command (assuan_context_t ctx
, char *line
)
1492 struct string_s
*string
= NULL
;
1494 struct client_s
*client
= assuan_get_pointer (ctx
);
1495 struct argv_s
*args
[] = {
1496 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1500 rc
= parse_options (&line
, args
, client
);
1502 return send_error (ctx
, rc
);
1504 if (client
->opts
& OPT_INQUIRE
)
1506 unsigned char *result
;
1509 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1511 return send_error (ctx
, rc
);
1513 line
= (char *) result
;
1516 rc
= build_realpath (client
, client
->doc
, line
, &string
);
1519 pthread_cleanup_push (list_command_cleanup1
, string
);
1520 rc
= xfer_data (ctx
, string
->str
, string
->len
);
1521 pthread_cleanup_pop (1);
1524 if (client
->opts
& OPT_INQUIRE
)
1527 return send_error (ctx
, rc
);
1531 list_command_cleanup1 (void *arg
)
1534 string_free ((struct string_s
*) arg
, 1);
1538 list_command_cleanup2 (void *arg
)
1540 struct element_list_s
*elements
= arg
;
1546 int total
= slist_length (elements
->list
);
1549 for (i
= 0; i
< total
; i
++)
1551 char *tmp
= slist_nth_data (elements
->list
, i
);
1555 slist_free (elements
->list
);
1558 if (elements
->prefix
)
1559 xfree (elements
->prefix
);
1562 strv_free (elements
->req
);
1569 parse_list_opt_norecurse (void *data
, void *value
)
1571 struct client_s
*client
= data
;
1573 client
->opts
&= ~(OPT_LIST_RECURSE
);
1578 parse_list_opt_verbose (void *data
, void *value
)
1580 struct client_s
*client
= data
;
1582 client
->opts
|= OPT_LIST_VERBOSE
;
1587 parse_list_opt_target (void *data
, void *value
)
1589 struct client_s
*client
= data
;
1591 client
->opts
|= OPT_LIST_WITH_TARGET
;
1596 parse_list_opt_all (void *data
, void *value
)
1598 struct client_s
*client
= data
;
1600 client
->opts
|= OPT_LIST_ALL
;
1605 list_path_once (struct client_s
*client
, char *line
,
1606 struct element_list_s
*elements
, struct string_s
*result
)
1610 elements
->req
= str_split (line
, " ", 0);
1612 strv_printf (&elements
->req
, "%s", line
);
1614 rc
= create_path_list (client
, client
->doc
, elements
, *elements
->req
);
1615 if ((rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
) && elements
->verbose
)
1620 int total
= slist_length (elements
->list
);
1623 rc
= GPG_ERR_NO_DATA
;
1631 for (i
= 0; i
< total
; i
++)
1633 char *tmp
= slist_nth_data (elements
->list
, i
);
1635 string_append_printf (result
, "%s%s", tmp
,
1636 i
+ 1 == total
? "" : "\n");
1641 rc
= GPG_ERR_NO_DATA
;
1648 has_list_flag (char *path
, char *flags
)
1652 while (*p
&& *++p
!= ' ');
1661 for (f
= flags
; *f
&& *f
!= ' '; f
++)
1672 do_list (assuan_context_t ctx
, char *line
)
1674 struct client_s
*client
= assuan_get_pointer (ctx
);
1676 struct element_list_s
*elements
= NULL
;
1678 elements
= xcalloc (1, sizeof (struct element_list_s
));
1680 return GPG_ERR_ENOMEM
;
1682 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1684 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
& OPT_LIST_WITH_TARGET
);
1685 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1687 if (!line
|| !*line
)
1689 struct string_s
*str
= NULL
;
1691 pthread_cleanup_push (list_command_cleanup2
, elements
);
1692 rc
= list_root_elements (client
, client
->doc
, &str
, elements
->verbose
,
1693 elements
->with_target
);
1694 pthread_cleanup_pop (1);
1695 pthread_cleanup_push (list_command_cleanup1
, str
);
1699 if (client
->opts
& OPT_LIST_ALL
)
1701 char **roots
= str_split (str
->str
, "\n", 0);
1704 pthread_cleanup_push (req_cleanup
, roots
);
1705 string_truncate (str
, 0);
1707 for (p
= roots
; *p
; p
++)
1709 if (strchr (*p
, ' '))
1711 if (has_list_flag (*p
, "EOP"))
1713 string_append_printf (str
, "%s%s", *p
,
1714 *(p
+ 1) ? "\n" : "");
1719 elements
= xcalloc (1, sizeof (struct element_list_s
));
1722 rc
= GPG_ERR_ENOMEM
;
1726 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1728 (client
->opts
& OPT_LIST_VERBOSE
) | (client
->opts
&
1729 OPT_LIST_WITH_TARGET
);
1730 elements
->with_target
= client
->opts
& OPT_LIST_WITH_TARGET
;
1731 pthread_cleanup_push (list_command_cleanup2
, elements
);
1732 rc
= list_path_once (client
, *p
, elements
, str
);
1733 pthread_cleanup_pop (1);
1738 string_append (str
, "\n");
1741 pthread_cleanup_pop (1);
1745 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1748 pthread_cleanup_pop (1);
1752 pthread_cleanup_push (list_command_cleanup2
, elements
);
1753 struct string_s
*str
= string_new (NULL
);
1754 pthread_cleanup_push (list_command_cleanup1
, str
);
1755 rc
= list_path_once (client
, line
, elements
, str
);
1757 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1759 pthread_cleanup_pop (1);
1760 pthread_cleanup_pop (1);
1765 list_command (assuan_context_t ctx
, char *line
)
1767 struct client_s
*client
= assuan_get_pointer (ctx
);
1769 struct argv_s
*args
[] = {
1770 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
,
1771 parse_list_opt_norecurse
},
1772 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_list_opt_verbose
},
1773 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1774 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
,
1775 parse_list_opt_target
},
1776 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_all
},
1780 if (disable_list_and_dump
== 1)
1781 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1783 client
->opts
|= OPT_LIST_RECURSE
;
1784 rc
= parse_options (&line
, args
, client
);
1786 return send_error (ctx
, rc
);
1788 if (client
->opts
& OPT_LIST_ALL
)
1789 client
->opts
|= OPT_LIST_RECURSE
| OPT_LIST_VERBOSE
;
1791 if (client
->opts
& OPT_INQUIRE
)
1793 unsigned char *result
;
1796 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1798 return send_error (ctx
, rc
);
1800 line
= (char *) result
;
1803 rc
= do_list (ctx
, line
);
1805 if (client
->opts
& OPT_INQUIRE
)
1808 return send_error (ctx
, rc
);
1812 * req[0] - element path
1815 attribute_list (assuan_context_t ctx
, char **req
)
1817 struct client_s
*client
= assuan_get_pointer (ctx
);
1818 char **attrlist
= NULL
;
1826 if (!req
|| !req
[0])
1827 return GPG_ERR_SYNTAX
;
1829 if ((path
= str_split (req
[0], "\t", 0)) == NULL
)
1832 * The first argument may be only a root element.
1834 if ((path
= str_split (req
[0], " ", 0)) == NULL
)
1835 return GPG_ERR_SYNTAX
;
1838 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1848 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1849 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1860 for (a
= n
->properties
; a
; a
= a
->next
)
1864 if ((pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *))) == NULL
)
1867 strv_free (attrlist
);
1869 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1870 pwmd_strerror (GPG_ERR_ENOMEM
));
1871 return GPG_ERR_ENOMEM
;
1876 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
,
1878 && an
->content
? (char *) an
->content
: "");
1882 strv_free (attrlist
);
1883 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1884 pwmd_strerror (GPG_ERR_ENOMEM
));
1885 return GPG_ERR_ENOMEM
;
1888 attrlist
[++i
] = NULL
;
1892 return GPG_ERR_NO_DATA
;
1894 line
= strv_join ("\n", attrlist
);
1898 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1899 pwmd_strerror (GPG_ERR_ENOMEM
));
1900 strv_free (attrlist
);
1901 return GPG_ERR_ENOMEM
;
1904 pthread_cleanup_push (xfree
, line
);
1905 pthread_cleanup_push (req_cleanup
, attrlist
);
1906 rc
= xfer_data (ctx
, line
, strlen (line
));
1907 pthread_cleanup_pop (1);
1908 pthread_cleanup_pop (1);
1913 * req[0] - attribute
1914 * req[1] - element path
1917 attribute_delete (struct client_s
*client
, char **req
)
1923 if (!req
|| !req
[0] || !req
[1])
1924 return GPG_ERR_SYNTAX
;
1926 if (!strcmp (req
[0], "_name"))
1927 return GPG_ERR_INV_ATTR
;
1929 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
1932 * The first argument may be only a root element.
1934 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
1935 return GPG_ERR_SYNTAX
;
1938 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
1944 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
1945 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
1950 if (!strcmp (req
[0], (char *) "_acl"))
1952 rc
= is_element_owner (client
, n
);
1957 rc
= delete_attribute (client
, n
, (xmlChar
*) req
[0]);
1965 create_element_path (struct client_s
*client
,
1966 char ***elements
, gpg_error_t
* rc
, xmlNodePtr parent
)
1968 char **req
= *elements
;
1969 char **req_orig
= strv_dup (req
);
1970 xmlNodePtr n
= NULL
;
1976 *rc
= GPG_ERR_ENOMEM
;
1977 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1978 pwmd_strerror (GPG_ERR_ENOMEM
));
1983 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
1986 if (*rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1989 *rc
= new_root_element (client
, client
->doc
, req
[0]);
1995 else if (n
== parent
)
1997 *rc
= GPG_ERR_CONFLICT
;
2004 n
= create_target_elements_cb (client
, 1, n
, req
+ 1, rc
, NULL
);
2006 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2007 NULL
, NULL
, create_target_elements_cb
, 0, 0,
2014 * Reset the position of the element tree now that the elements
2015 * have been created.
2020 n
= find_root_element (client
, client
->doc
, &req
, rc
, NULL
, 0, 0);
2024 n
= find_elements (client
, client
->doc
, n
->children
, req
+ 1, rc
,
2025 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2032 strv_free (req_orig
);
2039 * Creates a "target" attribute. When other commands encounter an element with
2040 * this attribute, the element path is modified to the target value. If the
2041 * source element path doesn't exist when using 'ATTR SET target', it is
2042 * created, but the destination element path must exist.
2044 * req[0] - source element path
2045 * req[1] - destination element path
2048 target_attribute (struct client_s
*client
, char **req
)
2050 char **src
, **dst
, *line
= NULL
, **odst
= NULL
;
2054 if (!req
|| !req
[0] || !req
[1])
2055 return GPG_ERR_SYNTAX
;
2057 if ((src
= str_split (req
[0], "\t", 0)) == NULL
)
2060 * The first argument may be only a root element.
2062 if ((src
= str_split (req
[0], " ", 0)) == NULL
)
2063 return GPG_ERR_SYNTAX
;
2066 if (!valid_element_path (src
, 0))
2067 return GPG_ERR_INV_VALUE
;
2069 if ((dst
= str_split (req
[1], "\t", 0)) == NULL
)
2072 * The first argument may be only a root element.
2074 if ((dst
= str_split (req
[1], " ", 0)) == NULL
)
2076 rc
= GPG_ERR_SYNTAX
;
2081 odst
= strv_dup (dst
);
2084 rc
= GPG_ERR_ENOMEM
;
2088 n
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
2090 * Make sure the destination element path exists.
2097 n
= find_elements (client
, client
->doc
, n
->children
, dst
+ 1, &rc
,
2098 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2103 rc
= validate_target_attribute (client
, client
->doc
, req
[0], n
);
2104 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2107 n
= create_element_path (client
, &src
, &rc
, NULL
);
2111 line
= strv_join ("\t", odst
);
2114 rc
= GPG_ERR_ENOMEM
;
2118 rc
= add_attribute (client
, n
, "target", line
);
2129 * req[0] - attribute
2130 * req[1] - element path
2133 attribute_get (assuan_context_t ctx
, char **req
)
2135 struct client_s
*client
= assuan_get_pointer (ctx
);
2141 if (!req
|| !req
[0] || !req
[1])
2142 return GPG_ERR_SYNTAX
;
2144 if (strchr (req
[1], '\t'))
2146 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2147 return GPG_ERR_SYNTAX
;
2151 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2152 return GPG_ERR_SYNTAX
;
2155 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2162 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2163 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2171 if ((a
= xmlGetProp (n
, (xmlChar
*) req
[0])) == NULL
)
2172 return GPG_ERR_NOT_FOUND
;
2174 pthread_cleanup_push (xmlFree
, a
);
2177 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2179 rc
= GPG_ERR_NO_DATA
;
2181 pthread_cleanup_pop (1);
2190 * req[0] - attribute
2191 * req[1] - element path
2195 attribute_set (struct client_s
*client
, char **req
)
2201 if (!req
|| !req
[0] || !req
[1])
2202 return GPG_ERR_SYNTAX
;
2205 * Reserved attribute names.
2207 if (!strcmp (req
[0], "_name"))
2208 return GPG_ERR_INV_ATTR
;
2209 else if (!strcmp (req
[0], "target"))
2210 return target_attribute (client
, req
+ 1);
2211 else if (!valid_xml_attribute (req
[0]))
2212 return GPG_ERR_INV_VALUE
;
2214 if ((path
= str_split (req
[1], "\t", 0)) == NULL
)
2217 * The first argument may be only a root element.
2219 if ((path
= str_split (req
[1], " ", 0)) == NULL
)
2220 return GPG_ERR_SYNTAX
;
2223 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
2230 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
2231 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
2237 if (!strcmp (req
[0], (char *) "_acl"))
2239 rc
= is_element_owner (client
, n
);
2244 rc
= add_attribute (client
, n
, req
[0], req
[2]);
2253 * req[1] - attribute name or element path if command is LIST
2254 * req[2] - element path
2255 * req[2] - element path or value
2259 do_attr (assuan_context_t ctx
, char *line
)
2261 struct client_s
*client
= assuan_get_pointer (ctx
);
2265 req
= str_split (line
, " ", 4);
2266 if (!req
|| !req
[0] || !req
[1])
2269 return GPG_ERR_SYNTAX
;
2272 pthread_cleanup_push (req_cleanup
, req
);
2274 if (strcasecmp (req
[0], "SET") == 0)
2275 rc
= attribute_set (client
, req
+ 1);
2276 else if (strcasecmp (req
[0], "GET") == 0)
2277 rc
= attribute_get (ctx
, req
+ 1);
2278 else if (strcasecmp (req
[0], "DELETE") == 0)
2279 rc
= attribute_delete (client
, req
+ 1);
2280 else if (strcasecmp (req
[0], "LIST") == 0)
2281 rc
= attribute_list (ctx
, req
+ 1);
2283 rc
= GPG_ERR_SYNTAX
;
2285 pthread_cleanup_pop (1);
2290 attr_command (assuan_context_t ctx
, char *line
)
2292 struct client_s
*client
= assuan_get_pointer (ctx
);
2294 struct argv_s
*args
[] = {
2295 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2299 rc
= parse_options (&line
, args
, client
);
2301 return send_error (ctx
, rc
);
2303 if (client
->opts
& OPT_INQUIRE
)
2305 unsigned char *result
;
2308 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2310 return send_error (ctx
, rc
);
2312 line
= (char *) result
;
2315 rc
= do_attr (ctx
, line
);
2317 if (client
->opts
& OPT_INQUIRE
)
2320 return send_error (ctx
, rc
);
2324 parse_iscached_opt_lock (void *data
, void *value
)
2326 struct client_s
*client
= data
;
2329 client
->opts
|= OPT_LOCK
;
2334 iscached_command (assuan_context_t ctx
, char *line
)
2336 struct client_s
*client
= assuan_get_pointer (ctx
);
2338 struct argv_s
*args
[] = {
2339 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2343 if (!line
|| !*line
)
2344 return send_error (ctx
, GPG_ERR_SYNTAX
);
2346 rc
= parse_options (&line
, args
, client
);
2348 return send_error (ctx
, rc
);
2349 else if (!valid_filename (line
))
2350 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2352 rc
= cache_iscached (line
, NULL
);
2353 if (client
->opts
& OPT_LOCK
2354 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2356 unsigned char md5file
[16];
2357 gpg_error_t trc
= rc
;
2359 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2360 if (memcmp (md5file
, client
->md5file
, 16))
2361 cleanup_client (client
);
2363 memcpy (client
->md5file
, md5file
, 16);
2364 rc
= do_lock (client
, 1);
2369 return send_error (ctx
, rc
);
2373 clearcache_command (assuan_context_t ctx
, char *line
)
2375 gpg_error_t rc
= 0, all_rc
= 0;
2376 unsigned char md5file
[16];
2380 struct client_thread_s
*once
= NULL
;
2383 MUTEX_LOCK (&cn_mutex
);
2384 pthread_cleanup_push (cleanup_mutex_cb
, &cn_mutex
);
2386 if (!line
|| !*line
)
2389 t
= slist_length (cn_thread_list
);
2391 for (i
= 0; i
< t
; i
++)
2393 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2394 assuan_peercred_t peer
;
2399 /* Lock each connected clients' file mutex to prevent any other client
2400 * from accessing the cache entry (the file mutex is locked upon
2401 * command startup). The cache for the entry is not cleared if the
2402 * file mutex is locked by another client to prevent this function
2407 if (thd
->cl
->filename
)
2409 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2410 all_rc
= !all_rc
? rc
: all_rc
;
2418 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2419 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2421 if (pthread_equal (pthread_self (), thd
->tid
))
2425 if (!thd
->cl
->filename
||
2426 cache_iscached (thd
->cl
->filename
,
2427 NULL
) == GPG_ERR_NO_DATA
)
2433 cache_defer_clear (thd
->cl
->md5file
);
2436 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2444 rc
= cache_clear (thd
->cl
->md5file
);
2445 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2453 /* A single data filename was specified. Lock only this data file
2454 * mutex and free the cache entry. */
2457 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2458 rc
= do_validate_peer (ctx
, line
, &peer
);
2460 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2462 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2464 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2466 if (pthread_equal (pthread_self (), thd
->tid
))
2473 rc
= cache_clear (thd
->cl
->md5file
);
2474 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2478 cache_defer_clear (thd
->cl
->md5file
);
2486 /* Only connected clients' cache entries have been cleared. Now clear any
2487 * remaining cache entries without clients but only if there wasn't an
2488 * error from above since this would defeat the locking check of the
2489 * remaining entries. */
2495 /* No clients are using the specified file. */
2496 else if (!all_rc
&& !rc
&& !once
)
2498 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2499 rc
= cache_clear (md5file
);
2502 /* Release the connection mutex. */
2503 pthread_cleanup_pop (1);
2507 send_status_all (STATUS_CACHE
, NULL
);
2509 /* One or more files were locked while clearing all cache entries. */
2513 return send_error (ctx
, rc
);
2517 cachetimeout_command (assuan_context_t ctx
, char *line
)
2520 char **req
= str_split (line
, " ", 0);
2523 assuan_peercred_t peer
;
2525 if (!req
|| !*req
|| !req
[1])
2528 return send_error (ctx
, GPG_ERR_SYNTAX
);
2532 timeout
= (int) strtol (req
[1], &p
, 10);
2533 if (errno
!= 0 || *p
|| timeout
< -1)
2536 return send_error (ctx
, GPG_ERR_SYNTAX
);
2539 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2542 unsigned char md5file
[16];
2544 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2545 rc
= cache_set_timeout (md5file
, timeout
);
2546 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2549 MUTEX_LOCK (&rcfile_mutex
);
2550 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2552 MUTEX_UNLOCK (&rcfile_mutex
);
2557 return send_error (ctx
, rc
);
2561 dump_command (assuan_context_t ctx
, char *line
)
2565 struct client_s
*client
= assuan_get_pointer (ctx
);
2568 if (disable_list_and_dump
== 1)
2569 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2571 rc
= peer_is_invoker(client
);
2573 return send_error (ctx
, rc
);
2575 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2579 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2580 pwmd_strerror (GPG_ERR_ENOMEM
));
2581 return send_error (ctx
, GPG_ERR_ENOMEM
);
2584 pthread_cleanup_push (xmlFree
, xml
);
2585 rc
= xfer_data (ctx
, (char *) xml
, len
);
2586 pthread_cleanup_pop (1);
2587 return send_error (ctx
, rc
);
2591 getconfig_command (assuan_context_t ctx
, char *line
)
2593 struct client_s
*client
= assuan_get_pointer (ctx
);
2595 char filename
[255] = { 0 }, param
[747] = { 0 };
2596 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2598 if (!line
|| !*line
)
2599 return send_error (ctx
, GPG_ERR_SYNTAX
);
2601 if (strchr (line
, ' '))
2603 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2608 if (fp
&& !valid_filename (fp
))
2609 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2611 paramp
= str_down (paramp
);
2612 if (!strcmp (paramp
, "cipher") && fp
)
2614 struct crypto_s
*crypto
= NULL
;
2616 rc
= init_client_crypto (&crypto
);
2619 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2623 gcry_cipher_algo_name (cipher_to_gcrypt (crypto
->hdr
.flags
));
2633 UPDATE_AGENT_CTX (client
, crypto
);
2634 cleanup_crypto (&crypto
);
2635 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2636 return send_error (ctx
, rc
);
2641 else if (!strcmp (paramp
, "cipher_iterations") && fp
)
2643 struct crypto_s
*crypto
= NULL
;
2645 rc
= init_client_crypto (&crypto
);
2648 rc
= read_data_header (fp
, &crypto
->hdr
, NULL
, NULL
);
2651 tmp
= str_asprintf ("%llu",
2652 (unsigned long long) crypto
->hdr
.
2655 rc
= GPG_ERR_ENOMEM
;
2659 UPDATE_AGENT_CTX (client
, crypto
);
2660 cleanup_crypto (&crypto
);
2661 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
2662 return send_error (ctx
, rc
);
2667 else if (!strcmp (paramp
, "passphrase"))
2668 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2670 p
= config_get_value (fp
? fp
: "global", paramp
);
2672 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2674 tmp
= expand_homedir (p
);
2678 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2679 pwmd_strerror (GPG_ERR_ENOMEM
));
2680 return send_error (ctx
, GPG_ERR_ENOMEM
);
2685 pthread_cleanup_push (xfree
, p
);
2686 rc
= xfer_data (ctx
, p
, strlen (p
));
2687 pthread_cleanup_pop (1);
2688 return send_error (ctx
, rc
);
2693 xmlXPathContextPtr xp
;
2694 xmlXPathObjectPtr result
;
2700 xpath_command_cleanup (void *arg
)
2702 struct xpath_s
*xpath
= arg
;
2707 req_cleanup (xpath
->req
);
2710 xmlBufferFree (xpath
->buf
);
2713 xmlXPathFreeObject (xpath
->result
);
2716 xmlXPathFreeContext (xpath
->xp
);
2720 do_xpath (assuan_context_t ctx
, char *line
)
2723 struct client_s
*client
= assuan_get_pointer (ctx
);
2724 struct xpath_s _x
= { 0 };
2725 struct xpath_s
*xpath
= &_x
;
2727 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2729 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2730 return GPG_ERR_ENOMEM
;
2733 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2736 rc
= GPG_ERR_BAD_DATA
;
2741 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2744 rc
= GPG_ERR_BAD_DATA
;
2748 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2750 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2754 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2755 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2759 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2761 rc
= GPG_ERR_NO_DATA
;
2764 else if (xpath
->req
[1])
2770 pthread_cleanup_push (xpath_command_cleanup
, &xpath
);
2771 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2772 xmlBufferLength (xpath
->buf
));
2773 pthread_cleanup_pop (0);
2775 xpath_command_cleanup (xpath
);
2780 xpath_command (assuan_context_t ctx
, char *line
)
2782 struct client_s
*client
= assuan_get_pointer (ctx
);
2784 struct argv_s
*args
[] = {
2785 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2789 if (disable_list_and_dump
== 1)
2790 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2792 rc
= peer_is_invoker(client
);
2794 return send_error (ctx
, rc
);
2796 rc
= parse_options (&line
, args
, client
);
2798 return send_error (ctx
, rc
);
2800 if (client
->opts
& OPT_INQUIRE
)
2802 unsigned char *result
;
2805 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2807 return send_error (ctx
, rc
);
2809 line
= (char *) result
;
2812 if (!line
|| !*line
)
2813 rc
= GPG_ERR_SYNTAX
;
2816 rc
= do_xpath (ctx
, line
);
2818 if (client
->opts
& OPT_INQUIRE
)
2821 return send_error (ctx
, rc
);
2825 do_xpathattr (assuan_context_t ctx
, char *line
)
2827 struct client_s
*client
= assuan_get_pointer (ctx
);
2831 struct xpath_s _x
= { 0 };
2832 struct xpath_s
*xpath
= &_x
;
2834 if ((req
= str_split (line
, " ", 3)) == NULL
)
2835 return GPG_ERR_ENOMEM
;
2839 rc
= GPG_ERR_SYNTAX
;
2843 if (!strcasecmp (req
[0], "SET"))
2845 else if (!strcasecmp (req
[0], "DELETE"))
2849 rc
= GPG_ERR_SYNTAX
;
2853 if (!req
[1] || !req
[2])
2855 rc
= GPG_ERR_SYNTAX
;
2859 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2861 rc
= GPG_ERR_ENOMEM
;
2865 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2867 rc
= GPG_ERR_SYNTAX
;
2871 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2874 rc
= GPG_ERR_BAD_DATA
;
2878 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2881 rc
= GPG_ERR_BAD_DATA
;
2885 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2887 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2891 rc
= recurse_xpath_nodeset (client
, client
->doc
, xpath
->result
->nodesetval
,
2892 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2893 (xmlChar
*) req
[1]);
2896 xpath_command_cleanup (xpath
);
2901 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2903 xpathattr_command (assuan_context_t ctx
, char *line
)
2905 struct client_s
*client
= assuan_get_pointer (ctx
);
2907 struct argv_s
*args
[] = {
2908 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2912 if (disable_list_and_dump
== 1)
2913 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2915 rc
= peer_is_invoker(client
);
2917 return send_error (ctx
, rc
);
2919 rc
= parse_options (&line
, args
, client
);
2921 return send_error (ctx
, rc
);
2923 if (client
->opts
& OPT_INQUIRE
)
2925 unsigned char *result
;
2928 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2930 return send_error (ctx
, rc
);
2932 line
= (char *) result
;
2935 if (!line
|| !*line
)
2936 rc
= GPG_ERR_SYNTAX
;
2939 rc
= do_xpathattr (ctx
, line
);
2941 if (client
->opts
& OPT_INQUIRE
)
2944 return send_error (ctx
, rc
);
2948 do_import (struct client_s
*client
, const char *root_element
,
2949 unsigned char *content
)
2951 char **dst_path
= NULL
;
2952 xmlDocPtr doc
= NULL
;
2953 xmlNodePtr n
, root
, copy
;
2956 if (!content
|| !*content
)
2959 return GPG_ERR_SYNTAX
;
2963 dst_path
= str_split (root_element
, "\t", 0);
2965 if (dst_path
&& !valid_element_path (dst_path
, 0))
2968 strv_free (dst_path
);
2970 return GPG_ERR_INV_VALUE
;
2973 struct string_s
*str
= string_new_content ((char *)content
);
2974 str
= string_prepend (str
, "<pwmd>");
2975 str
= string_append (str
, "</pwmd>");
2976 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2977 string_free (str
, 1);
2980 rc
= GPG_ERR_BAD_DATA
;
2984 root
= xmlDocGetRootElement (doc
);
2985 xmlNodePtr root_orig
= root
->children
;
2986 root
= root
->children
;
2987 rc
= validate_import (client
, root
);
2996 char **path
= strv_dup (dst_path
);
2999 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
3000 pwmd_strerror (GPG_ERR_ENOMEM
));
3001 rc
= GPG_ERR_ENOMEM
;
3005 xmlChar
*a
= xmlGetProp (root
, (xmlChar
*) "_name");
3009 rc
= GPG_ERR_INV_VALUE
;
3013 if (strv_printf (&path
, "%s", (char *) a
) == 0)
3016 rc
= GPG_ERR_ENOMEM
;
3021 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 0);
3022 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3030 n
= find_elements (client
, client
->doc
, n
->children
, path
+ 1, &rc
,
3031 NULL
, NULL
, NULL
, 0, 0, NULL
, 1);
3032 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3047 if (rc
== GPG_ERR_ELEMENT_NOT_FOUND
)
3049 n
= create_element_path (client
, &path
, &rc
, NULL
);
3056 copy
= xmlCopyNodeList (root
->children
);
3057 n
= xmlAddChildList (n
, copy
);
3059 rc
= GPG_ERR_ENOMEM
;
3068 /* Check if the content root element can create a DTD root element. */
3069 if (!xmlStrEqual ((xmlChar
*) "element", root
->name
))
3071 rc
= GPG_ERR_SYNTAX
;
3077 if ((a
= xmlGetProp (root
, (xmlChar
*) "_name")) == NULL
)
3079 rc
= GPG_ERR_SYNTAX
;
3083 char *tmp
= str_dup ((char *) a
);
3085 int literal
= is_literal_element (&tmp
);
3087 if (!valid_xml_element ((xmlChar
*) tmp
) || literal
)
3090 rc
= GPG_ERR_INV_VALUE
;
3094 if (strv_printf (&path
, "%s", tmp
) == 0)
3097 rc
= GPG_ERR_ENOMEM
;
3102 n
= find_root_element (client
, client
->doc
, &path
, &rc
, NULL
, 0, 1);
3103 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3105 rc
= GPG_ERR_BAD_DATA
;
3109 /* Overwriting the existing tree. */
3113 xmlFreeNodeList (n
);
3117 xmlSetProp (root
, (xmlChar
*) "_name", (xmlChar
*) path
[0]);
3118 n
= xmlCopyNode (root
, 1);
3119 n
= xmlAddChildList (xmlDocGetRootElement (client
->doc
), n
);
3124 rc
= update_element_mtime (client
, n
->parent
);
3126 for (root
= root_orig
->next
; root
; root
= root
->next
)
3128 if (root
->type
== XML_ELEMENT_NODE
)
3141 strv_free (dst_path
);
3147 parse_import_opt_root (void *data
, void *value
)
3149 struct client_s
*client
= data
;
3151 client
->import_root
= str_dup (value
);
3152 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3156 import_command (assuan_context_t ctx
, char *line
)
3159 struct client_s
*client
= assuan_get_pointer (ctx
);
3160 unsigned char *result
;
3162 struct argv_s
*args
[] = {
3163 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3167 xfree (client
->import_root
);
3168 client
->import_root
= NULL
;
3169 rc
= parse_options (&line
, args
, client
);
3171 return send_error (ctx
, rc
);
3173 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3176 xfree (client
->import_root
);
3177 client
->import_root
= NULL
;
3178 return send_error (ctx
, rc
);
3181 rc
= do_import (client
, client
->import_root
, result
);
3182 xfree (client
->import_root
);
3183 client
->import_root
= NULL
;
3184 return send_error (ctx
, rc
);
3188 do_lock (struct client_s
*client
, int add
)
3190 gpg_error_t rc
= lock_file_mutex (client
, add
);
3193 client
->flags
|= FLAG_LOCK_CMD
;
3199 lock_command (assuan_context_t ctx
, char *line
)
3201 struct client_s
*client
= assuan_get_pointer (ctx
);
3202 gpg_error_t rc
= do_lock (client
, 0);
3204 return send_error (ctx
, rc
);
3208 unlock_command (assuan_context_t ctx
, char *line
)
3210 struct client_s
*client
= assuan_get_pointer (ctx
);
3213 rc
= unlock_file_mutex (client
, 0);
3214 return send_error (ctx
, rc
);
3218 option_command (assuan_context_t ctx
, char *line
)
3220 struct client_s
*client
= assuan_get_pointer (ctx
);
3222 struct pinentry_option_s
*pin_opts
= &client
->pinentry_opts
;
3224 struct agent_s
*agent
= client
->crypto
->agent
;
3226 char namebuf
[255] = { 0 };
3227 char *name
= namebuf
;
3228 char *value
= NULL
, *p
;
3230 p
= strchr (line
, '=');
3232 strncpy (namebuf
, line
, sizeof(namebuf
));
3235 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3239 log_write1 ("OPTION name='%s' value='%s'", name
, value
);
3241 if (strcasecmp (name
, (char *) "log_level") == 0)
3247 l
= strtol (value
, NULL
, 10);
3250 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3253 MUTEX_LOCK (&rcfile_mutex
);
3254 config_set_int_param (&global_config
, "global", "log_level", value
);
3255 MUTEX_UNLOCK (&rcfile_mutex
);
3258 else if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3265 n
= strtol (value
, &p
, 10);
3267 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3270 client
->lock_timeout
= n
;
3273 else if (strcasecmp (name
, (char *) "NAME") == 0)
3275 char *tmp
= pthread_getspecific (thread_name_key
);
3280 if (!value
|| !*value
)
3282 pthread_setspecific (thread_name_key
, str_dup (""));
3286 pthread_setspecific (thread_name_key
, str_dup (value
));
3289 else if (strcasecmp (name
, (char *) "lc-messages") == 0)
3291 xfree (pin_opts
->lc_messages
);
3292 pin_opts
->lc_messages
= NULL
;
3293 if (value
&& *value
)
3294 pin_opts
->lc_messages
= str_dup (value
);
3297 rc
= set_agent_option (client
->crypto
->agent
, "lc-messages", value
);
3300 else if (strcasecmp (name
, (char *) "lc-ctype") == 0)
3302 xfree (pin_opts
->lc_ctype
);
3303 pin_opts
->lc_ctype
= NULL
;
3304 if (value
&& *value
)
3305 pin_opts
->lc_ctype
= str_dup (value
);
3308 rc
= set_agent_option (client
->crypto
->agent
, "lc-ctype", value
);
3311 else if (strcasecmp (name
, (char *) "ttyname") == 0)
3313 xfree (pin_opts
->ttyname
);
3314 pin_opts
->ttyname
= NULL
;
3315 if (value
&& *value
)
3316 pin_opts
->ttyname
= str_dup (value
);
3319 rc
= set_agent_option (client
->crypto
->agent
, "ttyname", value
);
3322 else if (strcasecmp (name
, (char *) "ttytype") == 0)
3324 xfree (pin_opts
->ttytype
);
3325 pin_opts
->ttytype
= NULL
;
3326 if (value
&& *value
)
3327 pin_opts
->ttytype
= str_dup (value
);
3330 rc
= set_agent_option (client
->crypto
->agent
, "ttytype", value
);
3333 else if (strcasecmp (name
, (char *) "display") == 0)
3335 xfree (pin_opts
->display
);
3336 pin_opts
->display
= NULL
;
3337 if (value
&& *value
)
3338 pin_opts
->display
= str_dup (value
);
3341 rc
= set_agent_option (client
->crypto
->agent
, "display", value
);
3344 else if (strcasecmp (name
, (char *) "pinentry-desc") == 0)
3346 xfree (pin_opts
->desc
);
3347 pin_opts
->desc
= NULL
;
3348 if (value
&& *value
)
3349 pin_opts
->desc
= str_dup (value
);
3351 else if (strcasecmp (name
, (char *) "pinentry-title") == 0)
3353 xfree (pin_opts
->title
);
3354 pin_opts
->title
= NULL
;
3355 if (value
&& *value
)
3356 pin_opts
->title
= str_dup (value
);
3358 else if (strcasecmp (name
, (char *) "pinentry-prompt") == 0)
3360 xfree (pin_opts
->prompt
);
3361 pin_opts
->prompt
= NULL
;
3362 if (value
&& *value
)
3363 pin_opts
->prompt
= str_dup (value
);
3366 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3374 n
= (int) strtol (value
, &p
, 10);
3377 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3379 pin_opts
->timeout
= n
;
3380 MUTEX_LOCK (&rcfile_mutex
);
3381 config_set_int_param (&global_config
,
3382 client
->filename
? client
->filename
: "global",
3383 "pinentry_timeout", value
);
3384 MUTEX_UNLOCK (&rcfile_mutex
);
3387 else if (strcasecmp (name
, "disable-pinentry") == 0)
3392 if (value
&& *value
)
3394 n
= (int) strtol (value
, &p
, 10);
3395 if (*p
|| n
< 0 || n
> 1)
3396 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3400 client
->flags
|= FLAG_NO_PINENTRY
;
3402 client
->flags
&= ~FLAG_NO_PINENTRY
;
3407 if (client
->flags
& FLAG_NO_PINENTRY
)
3408 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3411 rc
= set_agent_option (client
->crypto
->agent
, "pinentry-mode",
3415 return send_error (ctx
, rc
);
3420 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3424 if (!rc
&& use_agent
&& agent
)
3426 rc
= pinentry_merge_options (&client
->crypto
->agent
->pinentry_opts
,
3431 return send_error (ctx
, rc
);
3435 do_rename (assuan_context_t ctx
, char *line
)
3437 struct client_s
*client
= assuan_get_pointer (ctx
);
3439 char **req
, **src
, *dst
;
3442 req
= str_split (line
, " ", 0);
3444 if (!req
|| !req
[0] || !req
[1])
3447 return GPG_ERR_SYNTAX
;
3451 is_literal_element (&dst
);
3453 if (!valid_xml_element ((xmlChar
*) dst
))
3456 return GPG_ERR_INV_VALUE
;
3459 if (strchr (req
[0], '\t'))
3460 src
= str_split (req
[0], "\t", 0);
3462 src
= str_split (req
[0], " ", 0);
3466 rc
= GPG_ERR_SYNTAX
;
3470 n
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3472 n
= find_elements (client
, client
->doc
, n
->children
, src
+ 1, &rc
, NULL
, NULL
,
3473 NULL
, 0, 0, NULL
, 0);
3478 rc
= is_element_owner (client
, n
);
3482 xmlChar
*a
= xmlGetProp (n
, (xmlChar
*) "_name");
3485 rc
= GPG_ERR_ENOMEM
;
3489 /* To prevent unwanted effects:
3491 * <root name="a"><b/></root>
3495 if (xmlStrEqual (a
, (xmlChar
*) dst
))
3498 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3508 for (p
= src
; *p
; p
++)
3512 strv_printf (&tmp
, "%s", *p
);
3516 strv_printf (&tmp
, "!%s", dst
);
3517 ndst
= find_root_element (client
, client
->doc
, &tmp
, &rc
, NULL
, 0, 0);
3518 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3525 ndst
= find_elements (client
, client
->doc
, ndst
->children
, tmp
+ 1, &rc
, NULL
,
3526 NULL
, NULL
, 0, 0, NULL
, 0);
3529 if (!ndst
&& rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3534 /* Target may exist:
3537 * <root name="b" target="a"/>
3546 rc
= GPG_ERR_AMBIGUOUS_NAME
;
3552 rc
= is_element_owner (client
, ndst
);
3556 unlink_node (client
, ndst
);
3557 xmlFreeNodeList (ndst
);
3560 rc
= add_attribute (client
, n
, "_name", dst
);
3569 rename_command (assuan_context_t ctx
, char *line
)
3571 struct client_s
*client
= assuan_get_pointer (ctx
);
3573 struct argv_s
*args
[] = {
3574 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3578 rc
= parse_options (&line
, args
, client
);
3580 return send_error (ctx
, rc
);
3582 if (client
->opts
& OPT_INQUIRE
)
3584 unsigned char *result
;
3587 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3589 return send_error (ctx
, rc
);
3591 line
= (char *) result
;
3594 rc
= do_rename (ctx
, line
);
3596 if (client
->opts
& OPT_INQUIRE
)
3599 return send_error (ctx
, rc
);
3603 do_copy (assuan_context_t ctx
, char *line
)
3605 struct client_s
*client
= assuan_get_pointer (ctx
);
3607 char **req
, **src
= NULL
, **dst
= NULL
;
3608 xmlNodePtr nsrc
, ndst
, new = NULL
;
3610 req
= str_split (line
, " ", 0);
3611 if (!req
|| !req
[0] || !req
[1])
3614 return GPG_ERR_SYNTAX
;
3617 if (strchr (req
[0], '\t'))
3618 src
= str_split (req
[0], "\t", 0);
3620 src
= str_split (req
[0], " ", 0);
3624 rc
= GPG_ERR_SYNTAX
;
3628 if (strchr (req
[1], '\t'))
3629 dst
= str_split (req
[1], "\t", 0);
3631 dst
= str_split (req
[1], " ", 0);
3635 rc
= GPG_ERR_SYNTAX
;
3639 if (!valid_element_path (dst
, 0))
3641 rc
= GPG_ERR_INV_VALUE
;
3645 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3647 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
, NULL
,
3648 NULL
, NULL
, 0, 0, NULL
, 0);
3653 new = xmlCopyNodeList (nsrc
);
3656 rc
= GPG_ERR_ENOMEM
;
3661 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3665 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1, &rc
, NULL
,
3666 NULL
, create_target_elements_cb
, 0, 0, NULL
, 0);
3673 if (!ndst
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3675 else if (!ndst
|| create
)
3677 ndst
= create_element_path (client
, &dst
, &rc
, NULL
);
3682 rc
= is_element_owner (client
, ndst
);
3686 /* Merge any attributes from the src node to the initial dst node. */
3687 for (xmlAttrPtr attr
= new->properties
; attr
; attr
= attr
->next
)
3689 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3692 xmlAttrPtr a
= xmlHasProp (ndst
, attr
->name
);
3696 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3697 xmlNewProp (ndst
, attr
->name
, tmp
);
3699 rc
= add_attribute (client
, ndst
, NULL
, NULL
);
3702 xmlNodePtr n
= ndst
->children
;
3704 xmlFreeNodeList (n
);
3705 ndst
->children
= NULL
;
3709 n
= xmlCopyNodeList (new->children
);
3712 rc
= GPG_ERR_ENOMEM
;
3716 n
= xmlAddChildList (ndst
, n
);
3719 rc
= GPG_ERR_ENOMEM
;
3723 rc
= update_element_mtime (client
, xmlDocGetRootElement (client
->doc
) ==
3724 ndst
->parent
? ndst
: ndst
->parent
);
3730 xmlUnlinkNode (new);
3731 xmlFreeNodeList (new);
3747 copy_command (assuan_context_t ctx
, char *line
)
3749 struct client_s
*client
= assuan_get_pointer (ctx
);
3751 struct argv_s
*args
[] = {
3752 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3756 rc
= parse_options (&line
, args
, client
);
3758 return send_error (ctx
, rc
);
3760 if (client
->opts
& OPT_INQUIRE
)
3762 unsigned char *result
;
3765 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3767 return send_error (ctx
, rc
);
3769 line
= (char *) result
;
3772 rc
= do_copy (ctx
, line
);
3774 if (client
->opts
& OPT_INQUIRE
)
3777 return send_error (ctx
, rc
);
3781 do_move (assuan_context_t ctx
, char *line
)
3783 struct client_s
*client
= assuan_get_pointer (ctx
);
3785 char **req
, **src
= NULL
, **dst
= NULL
;
3786 xmlNodePtr nsrc
, ndst
= NULL
;
3788 req
= str_split (line
, " ", 0);
3790 if (!req
|| !req
[0] || !req
[1])
3793 return GPG_ERR_SYNTAX
;
3796 if (strchr (req
[0], '\t'))
3797 src
= str_split (req
[0], "\t", 0);
3799 src
= str_split (req
[0], " ", 0);
3803 rc
= GPG_ERR_SYNTAX
;
3807 if (strchr (req
[1], '\t'))
3808 dst
= str_split (req
[1], "\t", 0);
3810 dst
= str_split (req
[1], " ", 0);
3812 nsrc
= find_root_element (client
, client
->doc
, &src
, &rc
, NULL
, 0, 0);
3814 nsrc
= find_elements (client
, client
->doc
, nsrc
->children
, src
+ 1, &rc
,
3815 NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3820 rc
= is_element_owner (client
, nsrc
);
3826 if (!valid_element_path (dst
, 0))
3828 rc
= GPG_ERR_INV_VALUE
;
3832 ndst
= find_root_element (client
, client
->doc
, &dst
, &rc
, NULL
, 0, 0);
3834 ndst
= find_elements (client
, client
->doc
, ndst
->children
, dst
+ 1,
3835 &rc
, NULL
, NULL
, NULL
, 0, 0, NULL
, 0);
3838 ndst
= xmlDocGetRootElement (client
->doc
);
3840 for (xmlNodePtr n
= ndst
; n
; n
= n
->parent
)
3844 rc
= GPG_ERR_CONFLICT
;
3849 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3856 xmlChar
*a
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3858 xmlNodePtr dup
= find_element (client
, ndst
->children
, (char *) a
,
3870 if (ndst
== xmlDocGetRootElement (client
->doc
))
3872 xmlNodePtr n
= nsrc
;
3875 while (n
->parent
&& n
->parent
!= ndst
)
3878 xmlChar
*a
= node_has_attribute (n
, (xmlChar
*) "_name");
3879 xmlChar
*b
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3881 if (xmlStrEqual (a
, b
))
3884 xmlUnlinkNode (nsrc
);
3886 xmlFreeNodeList (n
);
3894 xmlUnlinkNode (dup
);
3895 xmlFreeNodeList (dup
);
3899 xmlUnlinkNode (dup
);
3905 xmlChar
*name
= node_has_attribute (nsrc
, (xmlChar
*) "_name");
3907 if (nsrc
->parent
== xmlDocGetRootElement (client
->doc
)
3908 && !strcmp ((char *) name
, *dst
))
3911 rc
= GPG_ERR_CONFLICT
;
3916 ndst
= create_element_path (client
, &dst
, &rc
, nsrc
);
3922 update_element_mtime (client
, nsrc
->parent
);
3923 xmlUnlinkNode (nsrc
);
3924 ndst
= xmlAddChildList (ndst
, nsrc
);
3927 rc
= GPG_ERR_ENOMEM
;
3929 update_element_mtime (client
, ndst
->parent
);
3945 move_command (assuan_context_t ctx
, char *line
)
3947 struct client_s
*client
= assuan_get_pointer (ctx
);
3949 struct argv_s
*args
[] = {
3950 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3954 rc
= parse_options (&line
, args
, client
);
3956 return send_error (ctx
, rc
);
3958 if (client
->opts
& OPT_INQUIRE
)
3960 unsigned char *result
;
3963 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3965 return send_error (ctx
, rc
);
3967 line
= (char *) result
;
3970 rc
= do_move (ctx
, line
);
3972 if (client
->opts
& OPT_INQUIRE
)
3975 return send_error (ctx
, rc
);
3979 ls_command (assuan_context_t ctx
, char *line
)
3982 char *tmp
= str_asprintf ("%s/data", homedir
);
3983 char *dir
= expand_homedir (tmp
);
3984 DIR *d
= opendir (dir
);
3986 rc
= gpg_error_from_errno (errno
);
3992 return send_error (ctx
, rc
);
3996 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3997 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
4003 while (!readdir_r (d
, p
, &cur
) && cur
)
4005 if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '\0')
4007 else if (cur
->d_name
[0] == '.' && cur
->d_name
[1] == '.'
4008 && cur
->d_name
[2] == '\0')
4011 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
4018 rc
= GPG_ERR_ENOMEM
;
4030 return send_error (ctx
, rc
);
4033 return send_error (ctx
, GPG_ERR_NO_DATA
);
4035 list
[strlen (list
) - 1] = 0;
4036 rc
= xfer_data (ctx
, list
, strlen (list
));
4038 return send_error (ctx
, rc
);
4042 bye_notify (assuan_context_t ctx
, char *line
)
4044 struct client_s
*cl
= assuan_get_pointer (ctx
);
4047 if (cl
->thd
->remote
)
4053 struct timeval tv
= { 0, 50000 };
4055 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
4056 if (rc
== GNUTLS_E_AGAIN
)
4057 select (0, NULL
, NULL
, NULL
, &tv
);
4059 while (rc
== GNUTLS_E_AGAIN
);
4063 /* This will let assuan_process_next() return. */
4064 fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
);
4065 cl
->last_rc
= 0; // BYE command result
4070 reset_notify (assuan_context_t ctx
, char *line
)
4072 struct client_s
*client
= assuan_get_pointer (ctx
);
4075 cleanup_client (client
);
4081 * This is called before every Assuan command.
4084 command_startup (assuan_context_t ctx
, const char *name
)
4086 struct client_s
*client
= assuan_get_pointer (ctx
);
4088 struct command_table_s
*cmd
= NULL
;
4090 log_write1 ("command='%s'", name
);
4091 client
->last_rc
= client
->opts
= 0;
4093 for (int i
= 0; command_table
[i
]; i
++)
4095 if (!strcasecmp (name
, command_table
[i
]->name
))
4097 if (command_table
[i
]->ignore_startup
)
4099 cmd
= command_table
[i
];
4104 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4109 * This is called after every Assuan command.
4112 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4114 struct client_s
*client
= assuan_get_pointer (ctx
);
4116 if (!(client
->flags
& FLAG_LOCK_CMD
))
4117 unlock_file_mutex (client
, 0);
4119 log_write1 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4120 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
4122 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4127 help_command (assuan_context_t ctx
, char *line
)
4132 if (!line
|| !*line
)
4135 char *help
= str_dup (_("Usage: HELP [<COMMAND>]\n"
4136 "For commands that take an element path as an argument, each element is "
4137 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
4140 for (i
= 0; command_table
[i
]; i
++)
4142 if (!command_table
[i
]->help
)
4145 tmp
= str_asprintf ("%s %s", help
, command_table
[i
]->name
);
4150 tmp
= strip_texi_and_wrap (help
);
4152 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4154 return send_error (ctx
, rc
);
4157 for (i
= 0; command_table
[i
]; i
++)
4159 if (!strcasecmp (line
, command_table
[i
]->name
))
4163 if (!command_table
[i
]->help
)
4166 help
= strip_texi_and_wrap (command_table
[i
]->help
);
4167 tmp
= str_asprintf (_("Usage: %s"), help
);
4169 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4171 return send_error (ctx
, rc
);
4175 return send_error (ctx
, GPG_ERR_INV_NAME
);
4179 new_command (const char *name
, int ignore
, int unlock
,
4180 gpg_error_t (*handler
) (assuan_context_t
, char *),
4186 for (i
= 0; command_table
[i
]; i
++);
4189 xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4190 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4191 command_table
[i
]->name
= name
;
4192 command_table
[i
]->handler
= handler
;
4193 command_table
[i
]->ignore_startup
= ignore
;
4194 command_table
[i
]->unlock
= unlock
;
4195 command_table
[i
++]->help
= help
;
4196 command_table
[i
] = NULL
;
4204 for (i
= 0; command_table
[i
]; i
++)
4205 xfree (command_table
[i
]);
4207 xfree (command_table
);
4211 sort_commands (const void *arg1
, const void *arg2
)
4213 struct command_table_s
*const *a
= arg1
;
4214 struct command_table_s
*const *b
= arg2
;
4223 return strcmp ((*a
)->name
, (*b
)->name
);
4227 passwd_command (assuan_context_t ctx
, char *line
)
4229 struct client_s
*client
= assuan_get_pointer (ctx
);
4231 struct argv_s
*args
[] = {
4232 &(struct argv_s
) {"reset", OPTION_TYPE_NOARG
, parse_opt_reset
},
4233 &(struct argv_s
) {"s2k-count", OPTION_TYPE_ARG
, parse_opt_s2k_count
},
4234 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
, parse_opt_no_passphrase
},
4238 rc
= peer_is_invoker (client
);
4239 if (rc
== GPG_ERR_EACCES
)
4240 return send_error (ctx
, rc
);
4242 if (client
->flags
& FLAG_NEW
)
4243 return send_error (ctx
, GPG_ERR_INV_STATE
);
4245 client
->crypto
->save
.s2k_count
=
4246 config_get_ulong (client
->filename
, "s2k_count");
4247 rc
= parse_options (&line
, args
, client
);
4249 return send_error (ctx
, rc
);
4251 if (!rc
&& client
->opts
& OPT_RESET
)
4253 rc
= cache_clear (client
->md5file
);
4255 send_status_all (STATUS_CACHE
, NULL
);
4260 if (!IS_PKI (client
->crypto
))
4262 struct crypto_s
*crypto
;
4264 xfree (client
->crypto
->filename
);
4265 client
->crypto
->filename
= str_dup (client
->filename
);
4266 rc
= change_passwd (ctx
, client
->filename
,
4267 client
->flags
& FLAG_NO_PINENTRY
, &crypto
,
4268 (client
->opts
& OPT_NO_PASSPHRASE
));
4271 cleanup_crypto (&client
->crypto
);
4272 client
->crypto
= crypto
;
4273 update_checksum (client
);
4274 cleanup_crypto_stage1 (client
->crypto
);
4280 if (client
->crypto
->save
.s2k_count
)
4281 rc
= send_to_agent (client
->crypto
->agent
, NULL
, NULL
,
4282 "OPTION s2k-count=%lu",
4283 client
->crypto
->save
.s2k_count
);
4286 rc
= agent_passwd (client
->crypto
);
4291 return send_error (ctx
, rc
);
4295 parse_keygrip_opt_sign (void *data
, void *value
)
4297 struct client_s
*client
= data
;
4300 client
->opts
|= OPT_SIGN
;
4305 keygrip_command (assuan_context_t ctx
, char *line
)
4307 struct client_s
*client
= assuan_get_pointer (ctx
);
4309 struct crypto_s
*crypto
= NULL
;
4310 struct argv_s
*args
[] = {
4311 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_keygrip_opt_sign
},
4315 if (!line
|| !*line
)
4316 return send_error (ctx
, GPG_ERR_SYNTAX
);
4318 rc
= parse_options (&line
, args
, client
);
4320 return send_error (ctx
, rc
);
4322 if (!valid_filename (line
))
4323 return send_error (ctx
, GPG_ERR_INV_VALUE
);
4325 rc
= init_client_crypto (&crypto
);
4327 return send_error (ctx
, rc
);
4329 rc
= read_data_file (line
, crypto
);
4332 char *hexgrip
= NULL
;
4334 if (!IS_PKI (crypto
))
4336 cleanup_crypto (&crypto
);
4337 return send_error (ctx
, GPG_ERR_NOT_SUPPORTED
);
4340 if (client
->opts
& OPT_SIGN
)
4342 if (valid_keygrip (crypto
->sign_grip
, sizeof (crypto
->sign_grip
)))
4343 hexgrip
= bin2hex (crypto
->sign_grip
, sizeof (crypto
->sign_grip
));
4347 hexgrip
= bin2hex (crypto
->grip
, sizeof (crypto
->grip
));
4350 rc
= GPG_ERR_ENOMEM
;
4352 rc
= xfer_data (ctx
, hexgrip
, strlen (hexgrip
));
4357 UPDATE_AGENT_CTX (client
, crypto
);
4358 cleanup_crypto (&crypto
);
4359 return send_error (ctx
, rc
);
4363 parse_opt_data (void *data
, void *value
)
4365 struct client_s
*client
= data
;
4368 client
->opts
|= OPT_DATA
;
4373 getinfo_command (assuan_context_t ctx
, char *line
)
4375 struct client_s
*client
= assuan_get_pointer (ctx
);
4377 char buf
[ASSUAN_LINELENGTH
];
4378 struct argv_s
*args
[] = {
4379 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4383 rc
= parse_options (&line
, args
, client
);
4385 return send_error (ctx
, rc
);
4387 if (!strcasecmp (line
, "clients"))
4389 if (client
->opts
& OPT_DATA
)
4391 MUTEX_LOCK (&cn_mutex
);
4392 snprintf (buf
, sizeof (buf
), "%i", slist_length (cn_thread_list
));
4393 MUTEX_UNLOCK (&cn_mutex
);
4394 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4397 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4399 else if (!strcasecmp (line
, "cache"))
4401 if (client
->opts
& OPT_DATA
)
4403 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4404 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4407 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4409 else if (!strcasecmp (line
, "pid"))
4412 pid_t pid
= getpid ();
4414 snprintf (buf
, sizeof (buf
), "%i", pid
);
4415 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4417 else if (!strcasecmp (line
, "version"))
4419 char *buf
= str_asprintf ("0x%06x %s%s", VERSION_HEX
,
4426 "", use_agent
? "AGENT" : "");
4427 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4430 else if (!strcasecmp (line
, "last_error"))
4432 if (client
->last_error
)
4433 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4435 rc
= GPG_ERR_NO_DATA
;
4437 else if (!strcasecmp (line
, "user"))
4442 if (client
->thd
->remote
)
4443 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4445 user
= get_username (client
->thd
->peer
->uid
);
4447 user
= get_username (client
->thd
->peer
->uid
);
4450 rc
= xfer_data (ctx
, user
, strlen (user
));
4452 rc
= GPG_ERR_NO_DATA
;
4457 rc
= gpg_error (GPG_ERR_SYNTAX
);
4459 return send_error (ctx
, rc
);
4464 send_data_cb (void *user
, const void *buf
, size_t len
)
4466 assuan_context_t ctx
= user
;
4468 return assuan_send_data (ctx
, buf
, len
);
4472 send_status_cb (void *user
, const char *line
)
4474 assuan_context_t ctx
= user
;
4475 char keyword
[200], *k
;
4478 for (p
= line
, k
= keyword
; *p
; p
++)
4490 while (isspace (*p
))
4493 return assuan_write_status (ctx
, keyword
, *p
? p
: NULL
);
4498 agent_command (assuan_context_t ctx
, char *line
)
4503 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
4506 struct client_s
*client
= assuan_get_pointer (ctx
);
4508 if (!line
|| !*line
)
4509 return send_error (ctx
, GPG_ERR_SYNTAX
);
4511 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 1);
4512 rc
= assuan_transact (client
->crypto
->agent
->ctx
, line
, send_data_cb
,
4513 client
->ctx
, agent_loopback_cb
, client
->crypto
,
4514 send_status_cb
, client
->ctx
);
4515 if (gpg_err_code (rc
) == GPG_ERR_ASS_CANCELED
)
4520 rc
= assuan_write_line (client
->crypto
->agent
->ctx
, "CAN");
4523 rc
= assuan_read_line (client
->crypto
->agent
->ctx
, &line
, &len
);
4525 rc
= gpg_error (GPG_ERR_ASS_CANCELED
);
4529 assuan_set_flag (client
->crypto
->agent
->ctx
, ASSUAN_CONVEY_COMMENTS
, 0);
4531 return send_error (ctx
, rc
);
4537 /* !BEGIN-HELP-TEXT!
4539 * This comment is used as a marker to generate the offline documentation
4540 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4541 * script to determine where commands begin and end.
4543 new_command("HELP", 1, 1, help_command
, _(
4544 "HELP [<COMMAND>]\n"
4545 "Show available commands or command specific help text."
4548 new_command("AGENT", 1, 1, agent_command
, _(
4550 "Send a @command{gpg-agent} protocol @var{command} directly to the "
4551 "@command{gpg-agent}."
4554 new_command("GETINFO", 1, 1, getinfo_command
, _(
4555 "GETINFO [--data] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4556 "Get server and other information: @var{cache} returns the number of cached "
4557 "documents via a status message. @var{clients} returns the number of "
4558 "connected clients via a status message. @var{pid} returns the process ID "
4559 "number of the server via a data response. @var{VERSION} returns the server "
4560 "version number and compile-time features with a data response with each "
4561 "being space delimited. @var{LAST_ERROR} returns a detailed description of "
4562 "the last failed command when available. @var{USER} returns the username or "
4563 "@abbr{TLS} hash of the connected client. @xref{Status Messages}. "
4565 "When the @option{--data} option is specified then the result will be sent "
4566 "via a data response rather than a status message."
4569 new_command("PASSWD", 0, 0, passwd_command
, _(
4570 "PASSWD [--reset] [--s2k-count=N] [--no-passphrase]\n"
4571 "Changes the passphrase of the secret key required to open the current "
4572 "file or the passphrase of a symmetrically encrypted data file. When the "
4573 "@option{--reset} option is passed then the cache entry for the current "
4574 "file will be reset and the passphrase, if any, will be required during the "
4575 "next @code{OPEN} (@pxref{OPEN})."
4577 "The @option{--s2k-count} option sets or changes (@pxref{SAVE}) the number "
4578 "of hash iterations for a passphrase and must be either @code{0} to use "
4579 "the calibrated count of the machine (the default), or a value greater than "
4580 "or equal to @code{65536}. This option has no effect for symmetrically "
4581 "encrypted data files."
4583 "The @option{--no-passphrase} option will prevent requiring a passphrase for "
4584 "the data file, although a passphrase may be required when changing it."
4586 "This command is not available for non-invoking clients "
4587 "(@pxref{Access Control})."
4590 new_command("KEYGRIP", 1, 1, keygrip_command
, _(
4591 "KEYGRIP [--sign] <filename>\n"
4592 "Returns the hex encoded keygrip of the specified @var{filename} with a "
4595 "When the @option{--sign} option is specified then the key used for signing "
4596 "of the specified @var{filename} will be returned."
4598 "For symmetrically encrypted data files this command returns the error "
4599 "GPG_ERR_NOT_SUPPORTED."
4602 new_command("OPEN", 1, 1, open_command
, _(
4603 "OPEN [--lock] <filename> [<passphrase>]\n"
4604 "Opens @var{filename} using @var{passphrase}. When the filename is not "
4605 "found on the file-system then a new document will be created. If the file "
4606 "is found, it is looked for in the file cache. If cached and no "
4607 "@var{passphrase} was specified then the cached document is opened. When not "
4608 "cached, @cite{pinentry(1)} will be used to retrieve the passphrase to use "
4609 "for decryption unless @option{disable-pinentry} (@pxref{OPTION}) was "
4612 "When the @option{--lock} option is passed then the file mutex will be "
4613 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4614 "file has been opened."
4617 new_command("SAVE", 0, 0, save_command
, _(
4618 "SAVE [--no-passphrase] [--reset] [--no-agent] [--s2k-count=N] [--cipher=<algo>] [--cipher-iterations=N] [--inquire-keyparam] [--keygrip=hexstring] [--sign-keygrip=hexstring]\n"
4619 "Writes the @abbr{XML} document to disk. The file written to is the file that "
4620 "was opened using the @code{OPEN} command (@pxref{OPEN}). If the file is a "
4621 "new one or the option @option{--inquire-keyparam} was passed, then a new "
4622 "keypair will be generated and a pinentry will be used to prompt for the "
4623 "passphrase to encrypt with unless the @option{--no-passphrase} option was "
4624 "passed in which case the data file will not be passphrase protected. "
4626 "The @option{--no-agent} option disables use of @command{gpg-agent} for "
4627 "passphrase retrieval and caching of new files when @command{gpg-agent} "
4628 "use is enabled. The datafile will be symmetrically encrypted and will not "
4629 "use or generate any keypair."
4631 "The @option{--reset} option will clear the cache entry for the current file "
4632 "and require a passphrase, if needed, before saving."
4634 "The @option{--cipher} option can be used to encrypt the @abbr{XML} data to "
4635 "an alternate cipher. The default is @code{aes256}. See the Configuration "
4636 "(@pxref{Configuration}) for available ciphers."
4638 "The @option{--cipher-iterations} option specifies the number of times to "
4639 "encrypt the XML data. The default is 0 although 1 iteration is still done."
4641 "The @option{--inquire-keyparam} option will send a server @emph{INQUIRE} to "
4642 "the client to obtain the key paramaters to use when generating a new "
4643 "keypair. The inquired data is expected to be an S-expression. If not "
4644 "specified then an @samp{RSA} key of @samp{2048} bits will be generated "
4645 "unless otherwise set in the configuration file (@pxref{Configuration}). Note "
4646 "that when this option is specified a new keypair will be generated "
4647 "reguardless if the file is a new one and that if the data file is protected "
4648 "the passphrase to open it will be required before generating the new "
4649 "keypair. This option is not available for non-invoking clients "
4650 "(@pxref{Access Control})."
4652 "You can encrypt the data file to a public key other than the one that it "
4653 "was originally encrypted with by passing the @option{--keygrip} option with "
4654 "the hex encoded keygrip of the public key as its argument. The keygrip may "
4655 "be of any key that @command{gpg-agent} knows about. The "
4656 "@option{--sign-keygrip} option may also be used to sign with an alternate "
4657 "secret key. Use the @code{KEYGRIP} (@pxref{KEYGRIP}) command to obtain the "
4658 "keygrip of an existing data file. This option may be needed when using a "
4659 "smartcard. This option has no effect with symmetrically encrypted data "
4660 "files. These options are not available for non-invoking clients "
4661 "(@pxref{Access Control})."
4663 "The @option{--s2k-count} option sets number of hash iterations for a "
4664 "passphrase. A value less-than @code{65536} will use the machine calibrated "
4665 "value and is the default. This setting only affects new files. To change "
4666 "the setting use the @code{PASSWD} command (@pxref{PASSWD}). This option "
4667 "has no effect with symmetrically encrypted data files."
4670 new_command("ISCACHED", 1, 0, iscached_command
, _(
4671 "ISCACHED [--lock] <filename>\n"
4672 "An @emph{OK} response is returned if the specified @var{filename} is found "
4673 "in the file cache. If not found in the cache but exists on the filesystem "
4674 "then @var{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4677 "The @option{lock} option will lock the file mutex of @var{filename} when the "
4678 "file exists; it does not need to be opened nor cached. The lock will be "
4679 "released when the client exits or sends the @code{UNLOCK} (@pxref{UNLOCK}) "
4683 new_command("CLEARCACHE", 1, 1, clearcache_command
, _(
4684 "CLEARCACHE [<filename>]\n"
4685 "Clears a file cache entry for all or the specified @var{filename}."
4688 new_command("CACHETIMEOUT", 1, 1, cachetimeout_command
, _(
4689 "CACHETIMEOUT <filename> <seconds>\n"
4690 "The time in @var{seconds} until @var{filename} will be removed from the "
4691 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4692 "the passphrase for each @code{OPEN} or @code{SAVE} command (@pxref{OPEN}, "
4693 "@pxref{SAVE}). @xref{Configuration}, and the @code{cache_timeout} "
4697 new_command("LIST", 0, 1, list_command
, _(
4698 "LIST [--inquire] [--no-recurse] [--verbose] [--with-target] [--all] [[!]element[<TAB>[!]child[..]]]\n"
4699 "If no element path is given then a newline separated list of root elements "
4700 "is returned with a data response. If given, then all reachable elements "
4701 "of the specified element path are returned unless the @option{--no-recurse} "
4702 "option is specified. If specified, only the child elements of the element "
4703 "path are returned without recursing into grandchildren. Each resulting "
4704 "element is prefixed with the literal @code{!} character when the element "
4705 "contains no @code{target} attribute. @xref{Target Attribute}, for details."
4707 "When the @option{--verbose} option is passed then each element path "
4708 "returned will have zero or more flags appened to it. These flags are "
4709 "delimited from the element path by a single space character. A flag itself "
4710 "is a single character. Flag @code{P} indicates that access to the element "
4711 "is denied. Flag @code{+} indicates that there are child nodes of "
4712 "the current element path. Flag @code{E} indicates that an element of an "
4713 "element path contained in a @var{target} attribute could not be found. Flag "
4714 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4715 "(@pxref{Configuration}). Flag @code{T} will append the resolved element path "
4716 "of the @var{target} attribute contained in the current element (see below)."
4718 "The @option{--with-target} option implies @option{--verbose} and will append "
4719 "an additional flag @code{T} followed by a single space then an element path. "
4720 "The appended element path is the resolved path (@pxref{REALPATH}) of the "
4721 "current element when it contains a @var{target} attribute. When no "
4722 "@var{target} attribute is found then no flag will be appended."
4724 "The @option{--no-recurse} option limits the amount of data returned to only "
4725 "the listing of children of the specified element path and not any "
4728 "The @option{--all} option lists the entire element tree for each root "
4729 "element. This option also implies option @option{--verbose}."
4731 "When the @option{--inquire} option is passed then all remaining non-option "
4732 "arguments are retrieved via a server @emph{INQUIRE}."
4735 new_command("REALPATH", 0, 1, realpath_command
, _(
4736 "REALPATH [--inquire] [!]element[<TAB>[!]child[..]]\n"
4737 "Resolves all @code{target} attributes of the specified element path and "
4738 "returns the result with a data response. @xref{Target Attribute}, for details."
4740 "When the @option{--inquire} option is passed then all remaining non-option "
4741 "arguments are retrieved via a server @emph{INQUIRE}."
4744 new_command("STORE", 0, 1, store_command
, _(
4745 "STORE [!]element[<TAB>[!]child[..]]<TAB>[content]\n"
4746 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4748 "Creates a new element path or modifies the @var{content} of an existing "
4749 "element. If only a single element is specified then a new root element is "
4750 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4751 "set to the final @key{TAB} delimited element. If no @var{content} is "
4752 "specified after the final @key{TAB}, then the content of an existing "
4753 "element will be removed; or empty when creating a new element."
4755 "The only restriction of an element name is that it not contain whitespace "
4756 "or begin with the literal element character @code{!} unless specifying a "
4757 "literal element (@pxref{Target Attribute}). There is no whitespace between "
4758 "the @key{TAB} delimited elements. It is recommended that the content of an "
4759 "element be base64 encoded when it contains control or @key{TAB} characters "
4760 "to prevent @abbr{XML} parsing and @command{pwmd} syntax errors."
4763 new_command("RENAME", 0, 1, rename_command
, _(
4764 "RENAME [--inquire] [!]element[<TAB>[!]child[..]] <value>\n"
4765 "Renames the specified @var{element} to the new @var{value}. If an element of "
4766 "the same name as the @var{value} already exists it will be overwritten."
4768 "When the @option{--inquire} option is passed then all remaining non-option "
4769 "arguments are retrieved via a server @emph{INQUIRE}."
4772 new_command("COPY", 0, 1, copy_command
, _(
4773 "COPY [--inquire] [!]source[<TAB>[!]child[..]] [!]dest[<TAB>[!]child[..]]\n"
4774 "Copies the entire element tree starting from the child node of the source "
4775 "element, to the destination element path. If the destination element path "
4776 "does not exist then it will be created; otherwise it is overwritten."
4778 "Note that attributes from the source element are merged into the "
4779 "destination element when the destination element path exists. When an "
4780 "attribute of the same name exists in both the source and destination "
4781 "elements then the destination attribute will be updated to the source "
4784 "When the @option{--inquire} option is passed then all remaining non-option "
4785 "arguments are retrieved via a server @emph{INQUIRE}."
4788 new_command("MOVE", 0, 1, move_command
, _(
4789 "MOVE [--inquire] [!]source[<TAB>[!]child[..]] [[!]dest[<TAB>[!]child[..]]]\n"
4790 "Moves the source element path to the destination element path. If the "
4791 "destination is not specified then it will be moved to the root node of the "
4792 "document. If the destination is specified and exists then it will be "
4793 "overwritten; otherwise non-existing elements of the destination element "
4794 "path will be created."
4796 "When the @option{--inquire} option is passed then all remaining non-option "
4797 "arguments are retrieved via a server @emph{INQUIRE}."
4800 new_command("DELETE", 0, 1, delete_command
, _(
4801 "DELETE [--inquire] [!]element[<TAB>[!]child[..]]\n"
4802 "Removes the specified element path and all of its children. This may break "
4803 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4804 "refers to this element or any of its children."
4806 "When the @option{--inquire} option is passed then all remaining non-option "
4807 "arguments are retrieved via a server @emph{INQUIRE}."
4810 new_command("GET", 0, 1, get_command
, _(
4811 "GET [--inquire] [!]element[<TAB>[!]child[..]]\n"
4812 "Retrieves the content of the specified element. The content is returned "
4813 "with a data response."
4815 "When the @option{--inquire} option is passed then all remaining non-option "
4816 "arguments are retrieved via a server @emph{INQUIRE}."
4819 new_command("ATTR", 0, 1, attr_command
, _(
4820 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] [!]element[<TAB>[!]child[..]] ..\n"
4822 "@item ATTR SET attribute [!]element[<TAB>[!]child[..]] [value]\n"
4824 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4825 "element. When no @var{value} is specified any existing value will be removed."
4827 "@item ATTR DELETE attribute [!]element[<TAB>[!]child[..]]\n"
4829 " Removes an @var{attribute} from an element."
4831 "@item ATTR LIST [!]element[<TAB>[!]child[..]]\n"
4833 " Retrieves a newline separated list of attributes names and values "
4834 "from the specified element. Each attribute name and value is space delimited."
4836 "@item ATTR GET attribute [!]element[<TAB>[!]child[..]]\n"
4838 " Retrieves the value of an @var{attribute} from an element."
4841 "The @code{_name} attribute (case sensitive) cannot be removed nor modified. "
4842 "Use the @code{DELETE} (@pxref{DELETE}) or @code{RENAME} (@pxref{RENAME}) "
4845 "The @code{_mtime} attribute is updated each time an element is modified by "
4846 "either storing content, editing attributes or by deleting a child element. "
4847 "The @code{_ctime} attribute is created for each new element in an element "
4850 "When the @option{--inquire} option is passed then all remaining non-option "
4851 "arguments are retrieved via a server @emph{INQUIRE}."
4853 "@xref{Target Attribute}, for details about this special attribute."
4856 new_command("XPATH", 0, 1, xpath_command
, _(
4857 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4858 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4859 "specified it is assumed the expression is a request to return a result. "
4860 "Otherwise, the result is set to the @var{value} argument and the document is "
4861 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4862 "is assumed to be empty and the document is updated. For example:"
4865 "XPATH //element[@@_name='password']@key{TAB}\n"
4868 "would clear the content of all @code{password} elements in the data file "
4869 "while leaving off the trailing @key{TAB} would return all @code{password} "
4870 "elements in @abbr{XML} format."
4872 "When the @option{--inquire} option is passed then all remaining non-option "
4873 "arguments are retrieved via a server @emph{INQUIRE}."
4875 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4876 "expression syntax."
4879 new_command("XPATHATTR", 0, 1, xpathattr_command
, _(
4880 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4881 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4882 "attributes and does not return a result. For the @var{SET} operation the "
4883 "@var{value} is optional but the field is required. If not specified then "
4884 "the attribute value will be empty. For example:"
4887 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4890 "would create an @code{password} attribute for each @code{password} element "
4891 "found in the document. The attribute value will be empty but still exist."
4893 "When the @option{--inquire} option is passed then all remaining non-option "
4894 "arguments are retrieved via a server @emph{INQUIRE}."
4896 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4897 "expression syntax."
4900 new_command("IMPORT", 0, 1, import_command
, _(
4901 "IMPORT [--root [!]element[<TAB>[!]child[..]]] <content>\n"
4902 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4904 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4905 "argument is raw @abbr{XML} data. The content is created as a child of "
4906 "the element path specified with the @option{--root} option or at the "
4907 "document root when not specified. Existing elements of the same name will "
4910 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4914 new_command("DUMP", 0, 1, dump_command
, _(
4916 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4917 "dumping a specific node."
4920 new_command("LOCK", 0, 0, lock_command
, _(
4922 "Locks the mutex associated with the opened file. This prevents other clients "
4923 "from sending commands to the same opened file until the client "
4924 "that sent this command either disconnects or sends the @code{UNLOCK} "
4925 "command. @xref{UNLOCK}."
4928 new_command("UNLOCK", 1, 0, unlock_command
, _(
4930 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4931 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4932 "@pxref{ISCACHED})."
4935 new_command("GETCONFIG", 1, 1, getconfig_command
, _(
4936 "GETCONFIG [filename] <parameter>\n"
4937 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4938 "data response. If no file has been opened then the value for @var{filename} "
4939 "or the default from the @samp{global} section will be returned. If a file "
4940 "has been opened and no @var{filename} is specified, a value previously "
4941 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4944 new_command("OPTION", 1, 1, option_command
, _(
4945 "OPTION <NAME>=<VALUE>\n"
4946 "Sets a client option @var{name} to @var{value}. The value for an option is "
4947 "kept for the duration of the connection."
4950 "@item DISABLE-PINENTRY\n"
4951 "Disable use of @command{pinentry} for passphrase retrieval. When set, a "
4952 "server inquire is sent to the client to obtain the passphrase. This option "
4953 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4954 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands."
4956 "@item PINENTRY-TIMEOUT\n"
4957 "Sets the number of seconds before a pinentry prompt will return an error "
4958 "while waiting for user input."
4961 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4964 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4967 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4969 "@item PINENTRY-DESC\n"
4970 "Sets the description string of the @command{gpg-agent} and @command{pinentry} dialog."
4972 "@item PINENTRY-TITLE\n"
4973 "Sets the title string of the @command{gpg-agent} and @command{pinentry} dialog."
4975 "@item PINENTRY-PROMPT\n"
4976 "Sets the prompt string of the @command{gpg-agent} and @command{pinentry} dialog."
4979 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4981 "@item LC-MESSAGES\n"
4982 "Passed to the @command{gpg-agent} and used for the @command{pinentry} dialog."
4985 "Associates the thread ID of the connection with the specified textual "
4986 "representation. Useful for debugging log messages."
4988 "@item LOCK-TIMEOUT\n"
4989 "When not @code{0}, the duration in tenths of a second to wait for the file "
4990 "mutex which has been locked by another thread to be released before returning "
4991 "an error. When @code{-1}, then an error will be returned immediately."
4994 "An integer specifiying the logging level."
4998 new_command("LS", 1, 1, ls_command
, _(
5000 "Lists the available data files stored in the data directory "
5001 "(@file{~/.pwmd/data}). The result is a newline separated list of filenames."
5004 new_command("RESET", 1, 1, NULL
, _(
5006 "Closes the currently opened file but keeps any previously set client options."
5009 new_command("NOP", 1, 1, NULL
, _(
5011 "Does nothing. Always returns successfully."
5014 /* !END-HELP-TEXT! */
5015 new_command ("CANCEL", 1, 1, NULL
, NULL
);
5016 new_command ("END", 1, 1, NULL
, NULL
);
5017 new_command ("BYE", 1, 1, NULL
, NULL
);
5020 for (i
= 0; command_table
[i
]; i
++);
5021 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
5026 register_commands (assuan_context_t ctx
)
5030 for (; command_table
[i
]; i
++)
5032 if (!command_table
[i
]->handler
)
5035 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5036 command_table
[i
]->handler
,
5037 command_table
[i
]->help
);
5042 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5046 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5050 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5054 return assuan_register_post_cmd_notify (ctx
, command_finalize
);