2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
39 #include "pwmd-error.h"
44 #include "util-misc.h"
52 /* These are command option flags. */
53 #define OPT_INQUIRE 0x0001
54 #define OPT_NO_PASSPHRASE 0x0002
55 #define OPT_ASK 0x0004
56 #define OPT_LIST_RECURSE 0x0008
57 #define OPT_VERBOSE 0x0010
58 #define OPT_LOCK 0x0020
59 #define OPT_LOCK_ON_OPEN 0x0040
60 #define OPT_SIGN 0x0080
61 #define OPT_LIST_ALL 0x0100
62 #define OPT_DATA 0x0200
63 #define OPT_NO_AGENT 0x0400
64 #define OPT_SECRET_ONLY 0x0800
65 #define OPT_INQUIRE_KEYID 0x1000
66 #define OPT_INQUIRE_SIGN_KEYID 0x2000
67 #define OPT_SYMMETRIC 0x4000
68 #define OPT_NO_SIGNER 0x8000
69 #define OPT_HTML 0x10000
70 #define OPT_CACHE_AGENT 0x20000
71 #define OPT_CACHE_SIGN 0x40000
73 #define FLOCK_TYPE_NONE 0
74 #define FLOCK_TYPE_SH 0x0001
75 #define FLOCK_TYPE_EX 0x0002
76 #define FLOCK_TYPE_KEEP 0x0004
78 struct command_table_s
81 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
84 int unlock
; // unlock the file mutex after validating the checksum
88 static struct command_table_s
**command_table
;
90 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
91 static gpg_error_t
validate_checksum (struct client_s
*, const char *filename
,
92 struct cache_data_s
*, unsigned char **,
94 static gpg_error_t
update_checksum (struct client_s
*client
);
96 /* When 'status' is true the 'self' field of the status line will be false
97 * because we never send the STATE status message to the same client that
100 build_client_info_line (struct client_thread_s
*thd
, int status
)
102 int with_state
= config_get_boolean ("global", "send_state");
103 char *uid
, *username
= NULL
;
108 uid
= str_asprintf("#%s", thd
->tls
->fp
);
111 uid
= str_asprintf("%u", thd
->peer
->uid
);
112 username
= get_username (thd
->peer
->uid
);
115 uid
= str_asprintf("%u", thd
->peer
->uid
);
116 username
= get_username (thd
->peer
->uid
);
118 line
= str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
120 thd
->name
? thd
->name
: "-",
121 thd
->cl
&& thd
->cl
->filename
122 && (thd
->cl
->flags
& FLAG_OPEN
)
123 ? thd
->cl
->filename
: "/",
125 thd
->remote
? thd
->peeraddr
: "-",
129 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
130 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
131 with_state
? thd
->state
: CLIENT_STATE_UNKNOWN
, uid
,
133 thd
->remote
? "-" : username
,
146 update_client_state (struct client_s
*client
, unsigned s
)
148 MUTEX_LOCK (&cn_mutex
);
149 client
->thd
->state
= s
;
150 MUTEX_UNLOCK (&cn_mutex
);
151 int n
= config_get_boolean ("global", "send_state");
153 if (n
&& client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
155 char *line
= build_client_info_line (client
->thd
, 1);
158 send_status_all_not_self (n
== 2, STATUS_STATE
, "%s", line
);
165 unlock_file_mutex (struct client_s
*client
, int remove
)
169 // OPEN: keep the lock for the same file being reopened.
170 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
173 if (!(client
->flags
& FLAG_HAS_LOCK
))
174 return GPG_ERR_NOT_LOCKED
;
176 rc
= cache_unlock_mutex (client
->md5file
, remove
);
178 rc
= GPG_ERR_INV_STATE
;
180 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
186 lock_file_mutex (struct client_s
*client
, int add
)
189 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
191 if (client
->flags
& FLAG_HAS_LOCK
)
194 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
195 client
->lock_timeout
, add
, timeout
);
197 client
->flags
|= FLAG_HAS_LOCK
;
203 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
206 int type
= !cmd
->flock_type
|| (cmd
->flock_type
& FLOCK_TYPE_SH
)
209 if (!(client
->flags
& FLAG_OPEN
))
210 return GPG_ERR_INV_STATE
;
212 rc
= lock_file_mutex (client
, 0);
213 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
216 rc
= lock_flock (client
->ctx
, client
->filename
, type
, &client
->flock_fd
);
219 rc
= validate_checksum (client
, client
->filename
, NULL
, NULL
, NULL
);
220 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
223 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
225 else if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
228 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
229 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
230 unlock_file_mutex (client
, 0);
232 if (rc
|| !(cmd
->flock_type
& FLOCK_TYPE_KEEP
))
233 unlock_flock (&client
->flock_fd
);
239 parse_xml (assuan_context_t ctx
, int new)
241 struct client_s
*client
= assuan_get_pointer (ctx
);
242 int cached
= client
->doc
!= NULL
;
247 client
->doc
= xml_new_document ();
253 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
254 client
->crypto
->plaintext
= result
;
255 client
->crypto
->plaintext_size
= len
;
256 if (!client
->crypto
->plaintext
)
258 xmlFreeDoc (client
->doc
);
267 rc
= xml_parse_doc ((char *) client
->crypto
->plaintext
,
268 client
->crypto
->plaintext_size
,
269 (xmlDocPtr
*)&client
->doc
);
275 free_client (struct client_s
*client
)
278 xmlFreeDoc (client
->doc
);
281 xfree (client
->filename
);
282 xfree (client
->last_error
);
283 crypto_free (client
->crypto
);
284 client
->crypto
= NULL
;
288 cleanup_client (struct client_s
*client
)
290 assuan_context_t ctx
= client
->ctx
;
291 struct client_thread_s
*thd
= client
->thd
;
292 long lock_timeout
= client
->lock_timeout
;
293 xmlErrorPtr xml_error
= client
->xml_error
;
294 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
295 int flock_fd
= client
->flock_fd
;
297 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
298 free_client (client
);
299 memset (client
, 0, sizeof (struct client_s
));
300 client
->flock_fd
= flock_fd
;
301 client
->xml_error
= xml_error
;
304 client
->lock_timeout
= lock_timeout
;
305 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
309 req_cleanup (void *arg
)
314 strv_free ((char **) arg
);
318 parse_open_opt_lock (void *data
, void *value
)
320 struct client_s
*client
= data
;
322 client
->opts
|= OPT_LOCK_ON_OPEN
;
327 parse_opt_inquire (void *data
, void *value
)
329 struct client_s
*client
= data
;
332 client
->opts
|= OPT_INQUIRE
;
337 update_checksum (struct client_s
*client
)
341 struct cache_data_s
*cdata
;
342 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
349 cdata
= cache_get_data (client
->md5file
);
353 cdata
->crc
= xmalloc (len
);
354 memcpy (cdata
->crc
, crc
, len
);
361 validate_checksum (struct client_s
*client
, const char *filename
,
362 struct cache_data_s
*cdata
, unsigned char **r_crc
,
370 if (cdata
&& !cdata
->crc
)
371 return GPG_ERR_CHECKSUM
;
373 rc
= get_checksum (filename
, &crc
, &len
);
378 n
= memcmp (cdata
->crc
, crc
, len
);
379 else if (client
->crc
)
380 n
= memcmp (client
->crc
, crc
, len
);
390 return n
? GPG_ERR_CHECKSUM
: 0;
394 open_command (assuan_context_t ctx
, char *line
)
397 struct client_s
*client
= assuan_get_pointer (ctx
);
398 char **req
, *filename
;
399 unsigned char md5file
[16];
401 assuan_peercred_t peer
;
402 struct cache_data_s
*cdata
= NULL
;
404 unsigned char *crc
= NULL
;
406 struct argv_s
*args
[] = {
407 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
411 rc
= parse_options (&line
, args
, client
, 1);
413 return send_error (ctx
, rc
);
415 req
= str_split (line
, " ", 2);
417 return send_error (ctx
, GPG_ERR_SYNTAX
);
419 rc
= do_validate_peer (ctx
, req
[0], &peer
);
423 return send_error (ctx
, rc
);
427 if (!valid_filename (filename
))
430 return send_error (ctx
, GPG_ERR_INV_VALUE
);
433 pthread_cleanup_push ((void *)req_cleanup
, req
);
434 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
435 /* This client may have locked a different file with ISCACHED --lock than
436 * the current filename. This will remove that lock. */
437 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
438 if (client
->flags
& FLAG_OPEN
||
439 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
441 uint32_t opts
= client
->opts
;
442 uint32_t flags
= client
->flags
;
445 client
->flags
|= FLAG_KEEP_LOCK
;
446 else if (client
->flags
& FLAG_NEW
)
447 cache_clear (client
->md5file
, 0);
449 cleanup_client (client
);
451 client
->flags
|= flags
;
452 client
->flags
&= ~(FLAG_LOCK_CMD
);
454 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
457 memcpy (client
->md5file
, md5file
, 16);
458 client
->filename
= str_dup (filename
);
459 if (!client
->filename
)
462 return send_error(ctx
, GPG_ERR_ENOMEM
);
465 /* Need to lock the mutex here because file_modified() cannot without
466 * knowing the filename. */
467 rc
= lock_file_mutex (client
, 1);
469 client
->flags
&= ~FLAG_OPEN
;
473 rc
= lock_flock (ctx
, filename
, LOCK_SH
, &client
->flock_fd
);
474 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
481 char *keyfile
= config_get_string (client
->filename
, "passphrase_file");
483 rc
= crypto_init (&client
->crypto
, client
->ctx
, client
->filename
,
484 client
->flags
& FLAG_NO_PINENTRY
, keyfile
);
488 if (!rc
&& stat (client
->filename
, &st
) == -1)
489 rc
= gpg_error_from_errno (errno
);
490 else if (!rc
&& !S_ISREG (st
.st_mode
)) // to match LS output
491 rc
= gpg_error_from_errno (ENOANO
);
494 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_ENOENT
)
496 cdata
= cache_get_data (client
->md5file
);
501 client
->flags
|= FLAG_NEW
;
502 // data file disappeared. clear the cache entry.
503 cache_clear (client
->md5file
, 1);
506 else if (cdata
&& cdata
->doc
) // cached document
511 if (!rc
&& !(client
->flags
& FLAG_NEW
))
513 rc
= validate_checksum (client
, client
->filename
, cdata
, &crc
,
515 if (rc
== GPG_ERR_CHECKSUM
)
524 rc
= cache_iscached (client
->filename
, &defer
, 0, 0);
525 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
535 log_write ("%s: %s", client
->filename
,
536 pwmd_strerror (GPG_ERR_CHECKSUM
));
537 cache_clear (client
->md5file
, 1);
539 rc
= crypto_decrypt (client
, client
->crypto
);
544 if (client
->thd
->remote
545 && config_get_boolean (client
->filename
, "tcp_require_key")
546 && !(client
->flags
& FLAG_NEW
))
547 rc
= crypto_try_decrypt (client
,
548 (client
->flags
& FLAG_NO_PINENTRY
));
554 client
->crypto
->plaintext
= cdata
->doc
;
555 client
->crypto
->plaintext_size
= cdata
->size
;
556 rc
= cache_decrypt (client
->crypto
);
561 strv_free (client
->crypto
->pubkey
);
562 strv_free (client
->crypto
->sigkey
);
563 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
564 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
569 client
->crypto
->plaintext
= NULL
;
570 client
->crypto
->plaintext_size
= 0;
574 else // existing file
576 cached
= cdata
!= NULL
;
577 rc
= crypto_decrypt (client
, client
->crypto
);
583 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
586 rc
= cache_encrypt (client
->crypto
);
588 cache_clear (client
->md5file
, 1);
591 int timeout
= config_get_integer (client
->filename
,
594 free_cache_data_once (cdata
);
595 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
596 cdata
->doc
= client
->crypto
->plaintext
;
597 cdata
->size
= client
->crypto
->plaintext_size
;
598 cdata
->pubkey
= strv_dup(client
->crypto
->pubkey
);
599 cdata
->sigkey
= strv_dup(client
->crypto
->sigkey
);
600 client
->crypto
->plaintext
= NULL
;
601 client
->crypto
->plaintext_size
= 0;
603 if (cached
) // wont increment the refcount
605 /* Prevent using another FD to update the checksum for a
606 * cached data file. The validity has already been
609 client
->crc
= xmalloc (crclen
);
610 memcpy (client
->crc
, crc
, crclen
);
612 cdata
->crc
= xmalloc (crclen
);
613 memcpy (cdata
->crc
, crc
, crclen
);
615 rc
= cache_set_data (client
->md5file
, cdata
);
616 /* The cache entry may have been removed and cache_set_data()
617 * already sent STATUS_CACHE. */
618 if (!cache_iscached (client
->filename
, NULL
, 0, 0))
619 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
622 rc
= cache_add_file (client
->md5file
, cdata
, timeout
);
627 pthread_cleanup_pop (1);
630 if (!rc
&& !(client
->flags
& FLAG_NEW
) && !cached
)
631 rc
= update_checksum (client
);
635 client
->flags
|= FLAG_OPEN
;
637 if (client
->flags
& FLAG_NEW
)
638 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
640 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
641 rc
= do_lock (client
, 0);
646 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
648 if (client
->flags
& FLAG_NEW
)
649 cache_clear (client
->md5file
, 1);
651 crypto_free (client
->crypto
);
652 client
->crypto
= NULL
;
653 client
->flags
&= ~FLAG_OPEN
;
656 crypto_free_non_keys (client
->crypto
);
658 return send_error (ctx
, rc
);
661 /* If not the invoking_user or is an existing file, check that the list of user
662 * supplied key ID's are in the list of current key ID's obtained when
663 * decrypting the data file. Both short and long form keyid fingerprints are
666 * 8 byte with optional "0x" prefix
670 * Although, the key ID's are converted to the 8 byte form before calling this
671 * function in parse_save_opt_keyid_common() to keep consistancy with KEYINFO
675 permitted_to_save (struct client_s
*client
, const char **keys
,
681 if ((client
->flags
& FLAG_NEW
) || (client
->opts
& OPT_SYMMETRIC
))
684 rc
= peer_is_invoker (client
);
689 if ((!value
&& !keys
) || ((value
&& !*value
) && (keys
&& !*keys
)))
692 for (v
= value
; v
&& *v
; v
++)
694 const char **k
, *pv
= *v
;
697 if (*pv
== '0' && *(pv
+1) == 'x')
700 for (k
= keys
; k
&& *k
; k
++)
704 if (*pk
== '0' && *(pk
+1) == 'x')
707 rc
= !strcmp (pv
, pk
) ? 0 : GPG_ERR_FORBIDDEN
;
709 rc
= !strcmp (pv
, *k
) ? 0 : GPG_ERR_FORBIDDEN
;
719 return GPG_ERR_FORBIDDEN
;
726 parse_save_opt_keyid_common (struct client_s
*client
, const char **list
,
727 const char *value
, char ***dst
)
734 p
= str_split (value
, ",", 0);
736 return GPG_ERR_ENOMEM
;
739 crypto_keyid_to_16b (p
);
740 rc
= permitted_to_save (client
, list
, (const char **)p
);
750 parse_save_opt_keyid (void *data
, void *value
)
752 struct client_s
*client
= data
;
753 const char *str
= value
;
757 rc
= parse_save_opt_keyid_common (client
,
758 (const char **)client
->crypto
->pubkey
,
763 client
->crypto
->save
.pubkey
= dst
;
768 parse_save_opt_sign_keyid (void *data
, void *value
)
770 struct client_s
*client
= data
;
771 const char *str
= value
;
775 rc
= parse_save_opt_keyid_common (client
,
776 (const char **)client
->crypto
->sigkey
,
782 client
->opts
|= OPT_NO_SIGNER
;
784 client
->crypto
->save
.sigkey
= dst
;
789 parse_save_opt_inquire (struct client_s
*client
, uint32_t opt
)
791 if (opt
== OPT_INQUIRE
&& !(client
->flags
& FLAG_NEW
))
793 gpg_error_t rc
= peer_is_invoker (client
);
804 parse_save_opt_inquire_keyparam (void *data
, void *value
)
806 return parse_save_opt_inquire (data
, OPT_INQUIRE
);
810 parse_save_opt_inquire_keyid (void *data
, void *value
)
812 return parse_save_opt_inquire (data
, OPT_INQUIRE_KEYID
);
816 parse_save_opt_inquire_sign_keyid (void *data
, void *value
)
818 return parse_save_opt_inquire (data
, OPT_INQUIRE_SIGN_KEYID
);
822 parse_save_opt_symmetric (void *data
, void *value
)
824 struct client_s
*client
= data
;
826 client
->opts
|= OPT_SYMMETRIC
;
830 /* Tests that the keys in new_keys are also in old_keys. */
832 compare_keys (char **new_keys
, char **old_keys
)
836 if (!old_keys
|| !*old_keys
)
839 crypto_keyid_to_16b (new_keys
);
841 for (o
= old_keys
; *o
; o
++)
845 for (n
= new_keys
; *n
; n
++)
847 if (!strcmp (*n
, *o
))
852 return GPG_ERR_NOT_FOUND
;
859 inquire_keyid (struct client_s
*client
, uint32_t opt
)
862 unsigned char *result
= NULL
;
868 if (opt
== OPT_INQUIRE_KEYID
)
871 orig
= &client
->crypto
->pubkey
;
872 save
= &client
->crypto
->save
.pubkey
;
876 s
= "INQUIRE_SIGN_KEYID";
877 orig
= &client
->crypto
->sigkey
;
878 save
= &client
->crypto
->save
.sigkey
;
881 rc
= assuan_inquire (client
->ctx
, s
, &result
, &len
, 0);
886 rc
= parse_save_opt_keyid_common (client
, (const char **)*orig
,
887 (char *)result
, &dst
);
890 if (!dst
&& opt
== OPT_INQUIRE_SIGN_KEYID
891 && client
->opts
& OPT_SYMMETRIC
)
892 client
->opts
|= OPT_NO_SIGNER
;
902 /* The caching test of gpg-agent is both a blessing and a curse. When a key
903 * lookup in its cache fails it tries the last successful key to be unlocked.
904 * This prevents pwmd from successfully clearing a signing key since the
905 * previous key, which more than likely belongs to the same keypair as the
906 * encryption/decryption key, will have the passphrase cached and therefore the
907 * signing key also cached. So we need to clear both the signing and encryption
908 * keys to get the effect of requiring a passphrase when generating a new
909 * keypair for an existing data file, or when the "require_save_key"
910 * configuration parameter is set. The "require_save_key" parameter is mostly a
911 * failsafe of the gpg-agent option --ignore-cache-for-signing since
912 * some/most/all users may fail to set it.
915 save_command (assuan_context_t ctx
, char *line
)
917 struct client_s
*client
= assuan_get_pointer (ctx
);
918 struct cache_data_s
*cdata
= NULL
;
919 gpg_error_t rc
= 0, cached
= 0;
922 struct argv_s
*args
[] = {
923 &(struct argv_s
) {"keyid", OPTION_TYPE_ARG
, parse_save_opt_keyid
},
924 &(struct argv_s
) {"sign-keyid", OPTION_TYPE_OPTARG
,
925 parse_save_opt_sign_keyid
},
926 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
927 parse_save_opt_inquire_keyparam
},
928 &(struct argv_s
) {"inquire-keyid", OPTION_TYPE_NOARG
,
929 parse_save_opt_inquire_keyid
},
930 &(struct argv_s
) {"inquire-sign-keyid", OPTION_TYPE_NOARG
,
931 parse_save_opt_inquire_sign_keyid
},
932 &(struct argv_s
) {"symmetric", OPTION_TYPE_NOARG
,
933 parse_save_opt_symmetric
},
937 crypto_free_save (&client
->crypto
->save
);
939 if (!(client
->flags
& FLAG_NEW
))
941 rc
= crypto_is_symmetric (client
->filename
);
942 if (!rc
|| rc
== GPG_ERR_BAD_DATA
)
945 client
->opts
|= OPT_SYMMETRIC
;
952 return send_error (ctx
, rc
);
954 rc
= parse_options (&line
, args
, client
, 0);
956 return send_error (ctx
, rc
);
958 if (config_get_boolean (client
->filename
, "require_save_key"))
959 client
->opts
|= OPT_ASK
;
961 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
962 return send_error (ctx
, gpg_error_from_errno (errno
));
964 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
966 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
967 return send_error (ctx
, GPG_ERR_ENOANO
);
971 cached
= rc
= cache_iscached (client
->filename
, &defer
, 0, 1);
973 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
975 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
979 return send_error (ctx
, rc
);
981 /* Specifying both a recipient and symmetric encryption is an error. */
982 if (client
->opts
& OPT_INQUIRE_KEYID
&& client
->opts
& OPT_SYMMETRIC
)
984 return send_error (ctx
, GPG_ERR_CONFLICT
);
986 else if (client
->opts
& OPT_INQUIRE
&& client
->opts
& OPT_SYMMETRIC
)
988 return send_error (ctx
, GPG_ERR_CONFLICT
);
990 /* Existing file with a recipient and wanting symmetric is an error. */
991 else if (client
->opts
& OPT_SYMMETRIC
&& client
->crypto
->save
.pubkey
)
993 return send_error (ctx
, GPG_ERR_CONFLICT
);
995 else if (((client
->opts
& OPT_INQUIRE_KEYID
)
996 || (client
->opts
& OPT_INQUIRE_SIGN_KEYID
)))
998 if (client
->opts
& OPT_INQUIRE
)
999 return send_error (ctx
, GPG_ERR_CONFLICT
);
1001 if (client
->opts
& OPT_INQUIRE_KEYID
)
1002 rc
= inquire_keyid (client
, OPT_INQUIRE_KEYID
);
1004 if (!rc
&& (client
->opts
& OPT_INQUIRE_SIGN_KEYID
))
1005 rc
= inquire_keyid (client
, OPT_INQUIRE_SIGN_KEYID
);
1007 else if ((client
->crypto
->save
.pubkey
|| client
->crypto
->save
.sigkey
)
1008 && client
->opts
& OPT_INQUIRE
)
1009 return send_error (ctx
, GPG_ERR_CONFLICT
);
1013 client
->crypto
->keyfile
= config_get_string (client
->filename
,
1015 rc
= crypto_init_ctx (client
->crypto
, (client
->flags
& FLAG_NO_PINENTRY
),
1016 client
->crypto
->keyfile
);
1019 if (!rc
&& (client
->opts
& OPT_INQUIRE
))
1021 /* Require a passphrase when generating a new key pair for an existing
1024 if (!(client
->flags
& FLAG_NEW
))
1026 rc
= cache_clear_agent_keys (client
->md5file
, 1, 1);
1029 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1030 client
->opts
&= ~OPT_ASK
;
1036 unsigned char *params
= NULL
;
1039 rc
= assuan_inquire (client
->ctx
, "KEYPARAM", ¶ms
, &len
, 0);
1042 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1043 pthread_cleanup_push ((void *)xfree
, params
);
1044 rc
= crypto_genkey (client
, client
->crypto
, params
);
1045 pthread_cleanup_pop (1);
1049 else if (!rc
&& (client
->flags
& FLAG_NEW
))
1051 if (!client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
1052 && !(client
->opts
& OPT_SYMMETRIC
))
1054 char *params
= crypto_default_key_params ();
1057 rc
= GPG_ERR_ENOMEM
;
1061 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1062 pthread_cleanup_push ((void *)xfree
, params
);
1063 rc
= crypto_genkey (client
, client
->crypto
,
1064 (unsigned char *)params
);
1065 pthread_cleanup_pop (1);
1070 if (!client
->crypto
->save
.pubkey
&& client
->crypto
->save
.sigkey
1071 && !(client
->opts
& OPT_SYMMETRIC
))
1072 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->save
.sigkey
);
1074 if (client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
)
1075 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->save
.pubkey
);
1080 cdata
= cache_get_data (client
->md5file
);
1083 if (client
->crypto
->save
.pubkey
)
1084 rc
= compare_keys (client
->crypto
->save
.pubkey
, cdata
->pubkey
);
1086 /* Always allow a signer for symmetric data files. */
1087 if (!rc
&& client
->crypto
->save
.sigkey
1088 && !(client
->opts
& OPT_SYMMETRIC
))
1089 rc
= compare_keys (client
->crypto
->save
.sigkey
, cdata
->sigkey
);
1091 /* Prevent saving to a recipient who is not in the original recipient
1092 * list without a passphrase. */
1093 if (rc
|| (client
->crypto
->sigkey
&& client
->opts
& OPT_NO_SIGNER
))
1094 client
->opts
|= OPT_ASK
;
1096 if (rc
== GPG_ERR_NOT_FOUND
)
1098 rc
= peer_is_invoker (client
);
1099 if (rc
== GPG_ERR_EACCES
)
1100 rc
= GPG_ERR_FORBIDDEN
;
1103 if (!client
->crypto
->save
.pubkey
1104 && !(client
->opts
& OPT_SYMMETRIC
))
1105 client
->crypto
->save
.pubkey
= strv_dup (cdata
->pubkey
);
1107 if (!rc
&& !client
->crypto
->save
.sigkey
&& cdata
->sigkey
1108 && !(client
->opts
& OPT_NO_SIGNER
))
1109 client
->crypto
->save
.sigkey
= strv_dup (cdata
->sigkey
);
1110 else if (!rc
&& !client
->crypto
->save
.sigkey
1111 && (client
->opts
& OPT_NO_SIGNER
)
1112 && !(client
->opts
& OPT_SYMMETRIC
))
1113 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
1117 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->pubkey
);
1118 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->sigkey
);
1122 if (!rc
&& !(client
->flags
& FLAG_NEW
))
1124 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1125 if (client
->opts
& OPT_SYMMETRIC
)
1126 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
1128 if (client
->opts
& OPT_ASK
)
1130 rc
= cache_clear_agent_keys (client
->md5file
, 1, 1);
1132 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1136 if (!rc
&& client
->opts
& OPT_SYMMETRIC
)
1137 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
1140 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1146 xmlDocDumpFormatMemory (client
->doc
, &client
->crypto
->plaintext
, &size
,
1149 client
->crypto
->plaintext_size
= (size_t) size
;
1151 rc
= GPG_ERR_ENOMEM
;
1155 client
->crypto
->flags
|= (client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_SYMMETRIC
: 0;
1156 client
->crypto
->flags
|= (client
->flags
& FLAG_NEW
) ? CRYPTO_FLAG_NEWFILE
: 0;
1157 rc
= crypto_encrypt (client
, client
->crypto
);
1158 client
->crypto
->flags
&= ~CRYPTO_FLAG_SYMMETRIC
;
1163 rc
= crypto_write_file (client
->crypto
);
1166 if (!cached
) // no error
1167 cdata
= cache_get_data (client
->md5file
);
1172 rc
= cache_encrypt (client
->crypto
);
1175 cache_clear (client
->md5file
, 1);
1176 strv_free (client
->crypto
->pubkey
);
1177 client
->crypto
->pubkey
= strv_dup (client
->crypto
->save
.pubkey
);
1178 strv_free (client
->crypto
->sigkey
);
1179 client
->crypto
->sigkey
= strv_dup (client
->crypto
->save
.sigkey
);
1180 client
->flags
&= ~(FLAG_NEW
);
1186 int timeout
= config_get_integer (client
->filename
,
1189 free_cache_data_once (cdata
);
1190 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1191 cdata
->doc
= client
->crypto
->plaintext
;
1192 client
->crypto
->plaintext
= NULL
;
1193 cdata
->size
= client
->crypto
->plaintext_size
;
1194 client
->crypto
->plaintext_size
= 0;
1195 cdata
->pubkey
= client
->crypto
->save
.pubkey
;
1196 client
->crypto
->save
.pubkey
= NULL
;
1197 cdata
->sigkey
= client
->crypto
->save
.sigkey
;
1198 client
->crypto
->save
.sigkey
= NULL
;
1200 /* Update in case the cache entry expires the next SAVE may not
1201 * have any known keys. */
1202 strv_free (client
->crypto
->pubkey
);
1203 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
1204 strv_free (client
->crypto
->sigkey
);
1205 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
1207 if (!cached
) // no error and wont increment refcount
1208 rc
= cache_set_data (client
->md5file
, cdata
);
1210 rc
= cache_add_file (client
->md5file
, cdata
, timeout
);
1212 if (!rc
&& (cached
|| (client
->flags
& FLAG_NEW
)))
1213 send_status_all (STATUS_CACHE
, NULL
);
1217 rc
= update_checksum (client
);
1218 client
->flags
&= ~(FLAG_NEW
);
1224 crypto_free_non_keys (client
->crypto
);
1225 return send_error (ctx
, rc
);
1229 do_delete (assuan_context_t ctx
, char *line
)
1231 struct client_s
*client
= assuan_get_pointer (ctx
);
1232 struct xml_request_s
*req
;
1236 rc
= xml_new_request (client
, line
, XML_CMD_DELETE
, &req
);
1240 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1241 xml_free_request (req
);
1245 rc
= xml_is_element_owner (client
, n
);
1247 rc
= xml_unlink_node (client
, n
);
1253 delete_command (assuan_context_t ctx
, char *line
)
1255 struct client_s
*client
= assuan_get_pointer (ctx
);
1257 struct argv_s
*args
[] = {
1258 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1262 rc
= parse_options (&line
, args
, client
, 1);
1263 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1264 rc
= GPG_ERR_SYNTAX
;
1266 return send_error (ctx
, rc
);
1268 if (client
->opts
& OPT_INQUIRE
)
1270 unsigned char *result
;
1273 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1275 return send_error (ctx
, rc
);
1277 pthread_cleanup_push ((void *)xfree
, result
);
1278 rc
= do_delete (ctx
, (char *)result
);
1279 pthread_cleanup_pop (1);
1282 rc
= do_delete (ctx
, line
);
1284 return send_error (ctx
, rc
);
1288 update_element_expirey (struct client_s
*client
, xmlNodePtr n
)
1291 xmlChar
*expire
, *incr
;
1293 time_t e
, e_orig
, i
;
1294 time_t now
= time (NULL
);
1296 expire
= xml_attribute_value (n
, (xmlChar
*)"expire");
1301 e
= strtoul ((char *)expire
, &p
, 10);
1302 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1305 rc
= GPG_ERR_ERANGE
;
1310 incr
= xml_attribute_value (n
, (xmlChar
*)"expire_increment");
1314 i
= strtoul ((char *)incr
, &p
, 10);
1315 if (errno
|| *p
|| i
== ULONG_MAX
|| *incr
== '-')
1318 rc
= GPG_ERR_ERANGE
;
1325 p
= str_asprintf ("%lu", e
);
1328 rc
= GPG_ERR_ENOMEM
;
1332 rc
= xml_add_attribute (client
, n
, "expire", p
);
1335 rc
= send_status (client
->ctx
, STATUS_EXPIRE
, "%lu %lu", e_orig
, e
);
1342 store_command (assuan_context_t ctx
, char *line
)
1344 struct client_s
*client
= assuan_get_pointer (ctx
);
1347 unsigned char *result
;
1348 xmlNodePtr n
, parent
;
1350 char *content
= NULL
;
1351 struct xml_request_s
*req
;
1354 return send_error (ctx
, GPG_ERR_SYNTAX
);
1356 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1358 return send_error (ctx
, rc
);
1360 rc
= xml_new_request (client
, (char *)result
, XML_CMD_STORE
, &req
);
1363 return send_error (ctx
, rc
);
1365 /* Prevent passing the element content around to save some memory. */
1366 len
= strv_length (req
->args
);
1367 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1368 if (*(req
->args
+1) && !xml_valid_element_path (req
->args
, has_content
))
1370 xml_free_request (req
);
1371 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1374 if (has_content
|| !*req
->args
[len
-1])
1377 content
= req
->args
[len
-1];
1378 req
->args
[len
-1] = NULL
;
1381 if (strv_length (req
->args
) > 1)
1383 rc
= xml_check_recursion (client
, req
);
1384 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1388 parent
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1392 rc
= xml_is_element_owner (client
, parent
);
1395 n
= xml_find_text_node (parent
->children
);
1397 xmlNodeSetContent (n
, (xmlChar
*) content
);
1399 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1401 (void)xml_update_element_mtime (client
, parent
);
1402 (void)update_element_expirey (client
, parent
);
1408 xml_free_request (req
);
1409 return send_error (ctx
, rc
);
1413 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1415 struct client_s
*client
= assuan_get_pointer (ctx
);
1419 int progress
= config_get_integer ("global", "xfer_progress");
1422 if (!(client
->flags
& FLAG_LOCK_CMD
))
1423 rc
= unlock_file_mutex (client
, 0);
1424 if (rc
&& rc
!= GPG_ERR_NOT_LOCKED
)
1428 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1429 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1430 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1438 if (sent
+ to_send
> total
)
1439 to_send
= total
- sent
;
1441 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1442 flush
? 0 : to_send
);
1445 sent
+= flush
? 0 : to_send
;
1447 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1448 (sent
== total
&& flush
))
1449 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1451 if (!flush
&& !rc
&& sent
== total
)
1458 while (!rc
&& sent
< total
);
1464 do_get (assuan_context_t ctx
, char *line
)
1466 struct client_s
*client
= assuan_get_pointer (ctx
);
1468 struct xml_request_s
*req
;
1471 rc
= xml_new_request (client
, line
, XML_CMD_NONE
, &req
);
1475 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1478 if (n
&& n
->children
)
1482 n
= xml_find_text_node (n
->children
);
1483 if (!n
|| !n
->content
|| !*n
->content
)
1484 rc
= GPG_ERR_NO_DATA
;
1487 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1490 xmlChar
*expire
= xml_attribute_value (tmp
,
1491 (xmlChar
*)"expire");
1494 time_t now
= time (NULL
);
1499 e
= strtoul ((char *)expire
, &p
, 10);
1500 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1501 log_write (_("invalid expire attribute value: %s"),
1504 rc
= send_status (ctx
, STATUS_EXPIRE
, "%lu 0", e
);
1512 rc
= GPG_ERR_NO_DATA
;
1515 xml_free_request (req
);
1520 get_command (assuan_context_t ctx
, char *line
)
1522 struct client_s
*client
= assuan_get_pointer (ctx
);
1524 struct argv_s
*args
[] = {
1525 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1529 rc
= parse_options (&line
, args
, client
, 1);
1530 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1531 rc
= GPG_ERR_SYNTAX
;
1533 return send_error (ctx
, rc
);
1535 if (client
->opts
& OPT_INQUIRE
)
1537 unsigned char *result
;
1540 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1542 return send_error (ctx
, rc
);
1544 pthread_cleanup_push ((void *)xfree
, result
);
1545 rc
= do_get (ctx
, (char *)result
);
1546 pthread_cleanup_pop (1);
1549 rc
= do_get (ctx
, line
);
1551 return send_error (ctx
, rc
);
1554 static void list_command_cleanup1 (void *arg
);
1556 realpath_command (assuan_context_t ctx
, char *line
)
1559 char **realpath
= NULL
;
1560 struct client_s
*client
= assuan_get_pointer (ctx
);
1561 struct argv_s
*args
[] = {
1562 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1566 rc
= parse_options (&line
, args
, client
, 1);
1567 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1568 rc
= GPG_ERR_SYNTAX
;
1570 return send_error (ctx
, rc
);
1572 if (client
->opts
& OPT_INQUIRE
)
1574 unsigned char *result
;
1577 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1579 return send_error (ctx
, rc
);
1581 pthread_cleanup_push ((void *)xfree
, result
);
1582 (void)xml_resolve_path (client
, client
->doc
, result
, &realpath
, &rc
);
1583 pthread_cleanup_pop (1);
1586 (void)xml_resolve_path (client
, client
->doc
, (unsigned char *)line
,
1591 char *tmp
= strv_join ("\t", realpath
);
1593 strv_free (realpath
);
1595 return send_error (ctx
, GPG_ERR_ENOMEM
);
1597 pthread_cleanup_push ((void *)xfree
, tmp
);
1598 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
1599 pthread_cleanup_pop (1);
1602 return send_error (ctx
, rc
);
1606 list_command_cleanup1 (void *arg
)
1609 string_free ((struct string_s
*) arg
, 1);
1613 parse_list_opt_recurse (void *data
, void *value
)
1615 struct client_s
*client
= data
;
1617 client
->opts
|= OPT_LIST_RECURSE
;
1622 parse_opt_verbose (void *data
, void *value
)
1624 struct client_s
*client
= data
;
1626 client
->opts
|= OPT_VERBOSE
;
1631 list_path_once (struct client_s
*client
, char *line
,
1632 struct element_list_s
*elements
, struct string_s
*result
)
1636 rc
= xml_create_path_list (client
, client
->doc
, NULL
, elements
, line
, 0);
1637 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
)
1642 int total
= slist_length (elements
->list
);
1648 for (i
= 0; i
< total
; i
++)
1650 char *tmp
= slist_nth_data (elements
->list
, i
);
1652 string_append_printf (result
, "%s%s", tmp
,
1653 i
+ 1 == total
? "" : "\n");
1657 rc
= GPG_ERR_NO_DATA
;
1664 do_list (assuan_context_t ctx
, char *line
)
1666 struct client_s
*client
= assuan_get_pointer (ctx
);
1667 gpg_error_t rc
= GPG_ERR_NO_DATA
;
1668 struct element_list_s
*elements
= NULL
;
1670 if (!line
|| !*line
)
1672 struct string_s
*str
= string_new (NULL
);
1676 return GPG_ERR_ENOMEM
;
1678 pthread_cleanup_push ((void *)list_command_cleanup1
, str
);
1679 n
= xmlDocGetRootElement (client
->doc
);
1680 for (n
= n
->children
; n
; n
= n
->next
)
1684 if (n
->type
!= XML_ELEMENT_NODE
)
1687 name
= xmlGetProp (n
, (xmlChar
*)"_name");
1690 rc
= GPG_ERR_ENOMEM
;
1694 elements
= xcalloc (1, sizeof (struct element_list_s
));
1698 rc
= GPG_ERR_ENOMEM
;
1702 elements
->prefix
= string_new (NULL
);
1703 if (!elements
->prefix
)
1706 xml_free_element_list (elements
);
1707 rc
= GPG_ERR_ENOMEM
;
1711 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1712 elements
->root_only
= !elements
->recurse
;
1713 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1714 rc
= list_path_once (client
, (char *)name
, elements
, str
);
1716 pthread_cleanup_pop (1);
1721 string_append (str
, "\n");
1725 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1727 pthread_cleanup_pop (1);
1731 elements
= xcalloc (1, sizeof (struct element_list_s
));
1733 return GPG_ERR_ENOMEM
;
1735 elements
->prefix
= string_new (NULL
);
1736 if (!elements
->prefix
)
1738 xml_free_element_list (elements
);
1739 return GPG_ERR_ENOMEM
;
1742 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1743 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1744 struct string_s
*str
= string_new (NULL
);
1745 pthread_cleanup_push ((void *)list_command_cleanup1
, str
);
1746 rc
= list_path_once (client
, line
, elements
, str
);
1748 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1750 pthread_cleanup_pop (1);
1751 pthread_cleanup_pop (1);
1756 list_command (assuan_context_t ctx
, char *line
)
1758 struct client_s
*client
= assuan_get_pointer (ctx
);
1760 struct argv_s
*args
[] = {
1761 &(struct argv_s
) {"recurse", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1762 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1763 /* These are deprecated but kept for backward compatibility with older
1765 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, NULL
},
1766 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
, NULL
},
1767 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1768 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
, NULL
},
1772 if (disable_list_and_dump
== 1)
1773 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1775 rc
= parse_options (&line
, args
, client
, 1);
1776 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1777 rc
= GPG_ERR_SYNTAX
;
1779 return send_error (ctx
, rc
);
1781 if (client
->opts
& OPT_INQUIRE
)
1783 unsigned char *result
;
1786 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1788 return send_error (ctx
, rc
);
1790 pthread_cleanup_push ((void *)xfree
, result
);
1791 rc
= do_list (ctx
, (char *)result
);
1792 pthread_cleanup_pop (1);
1795 rc
= do_list (ctx
, line
);
1797 return send_error (ctx
, rc
);
1800 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1801 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1803 * args[0] - element path
1806 attribute_list (assuan_context_t ctx
, char **args
)
1808 struct client_s
*client
= assuan_get_pointer (ctx
);
1809 char **attrlist
= NULL
;
1813 xmlNodePtr n
, an
, r
;
1816 struct xml_request_s
*req
;
1818 if (!args
|| !args
[0] || !*args
[0])
1819 return GPG_ERR_SYNTAX
;
1821 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_LIST
, &req
);
1825 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1826 xml_free_request (req
);
1831 for (a
= n
->properties
; a
; a
= a
->next
)
1835 if (target
&& xml_reserved_attribute ((char *)a
->name
))
1838 pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *));
1842 strv_free (attrlist
);
1844 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1845 pwmd_strerror (GPG_ERR_ENOMEM
));
1846 return GPG_ERR_ENOMEM
;
1851 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
, an
&& an
->content
1852 ? (char *)an
->content
: "");
1856 strv_free (attrlist
);
1857 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1858 pwmd_strerror (GPG_ERR_ENOMEM
));
1859 return GPG_ERR_ENOMEM
;
1862 attrlist
[++i
] = NULL
;
1866 return GPG_ERR_NO_DATA
;
1870 r
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[0], NULL
, &rc
);
1871 if (!RESUMABLE_ERROR (rc
))
1873 strv_free (attrlist
);
1876 else if (!rc
&& r
!= n
)
1886 line
= strv_join ("\n", attrlist
);
1889 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1890 pwmd_strerror (GPG_ERR_ENOMEM
));
1891 strv_free (attrlist
);
1892 return GPG_ERR_ENOMEM
;
1895 pthread_cleanup_push ((void *)xfree
, line
);
1896 pthread_cleanup_push ((void *)req_cleanup
, attrlist
);
1897 rc
= xfer_data (ctx
, line
, strlen (line
));
1898 pthread_cleanup_pop (1);
1899 pthread_cleanup_pop (1);
1904 * args[0] - attribute
1905 * args[1] - element path
1908 attribute_delete (struct client_s
*client
, char **args
)
1913 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
1914 return GPG_ERR_SYNTAX
;
1916 if (!strcmp (args
[0], "_name") || !strcmp (args
[0], "target"))
1917 return GPG_ERR_INV_ATTR
;
1919 if (!xml_reserved_attribute (args
[0]))
1920 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
1923 struct xml_request_s
*req
;
1925 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
1929 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1930 xml_free_request (req
);
1934 rc
= xml_is_element_owner (client
, n
);
1937 rc
= xml_delete_attribute (client
, n
, (xmlChar
*) args
[0]);
1943 * Creates the "target" attribute. When other commands encounter an element
1944 * with this attribute they follow the "target" after appending remaining
1945 * elements from the original element path to the target. The exception is the
1946 * ATTR command that operates on the element itself (for reserved attribute
1947 * names) and not the target.
1949 * If the source element path doesn't exist when using 'ATTR SET target', it is
1950 * created, but the destination element path must exist. This is simliar to the
1951 * ls(1) command: ln -s src dst.
1953 * args[0] - source element path
1954 * args[1] - destination element path
1957 set_target_attribute (struct client_s
*client
, char **args
)
1959 struct xml_request_s
*req
= NULL
;
1964 if (!args
|| !args
[0] || !args
[1])
1965 return GPG_ERR_SYNTAX
;
1967 src
= str_split (args
[0], "\t", 0);
1969 return GPG_ERR_SYNTAX
;
1971 if (!xml_valid_element_path (src
, 0))
1974 return GPG_ERR_INV_VALUE
;
1977 dst
= str_split (args
[1], "\t", 0);
1981 return GPG_ERR_SYNTAX
;
1984 // Be sure the destination element path exists. */
1985 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
1989 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1990 if (rc
&& rc
!= GPG_ERR_EACCES
)
1993 xml_free_request (req
);
1996 if (!strcmp (args
[0], args
[1]))
1998 rc
= GPG_ERR_EEXIST
;
2002 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_TARGET
, &req
);
2006 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2007 if (rc
&& rc
!= GPG_ERR_EACCES
)
2012 rc
= xml_add_attribute (client
, n
, "target", args
[1]);
2014 rc
= xml_unlink_node (client
, n
->children
);
2020 xml_free_request (req
);
2025 * args[0] - attribute
2026 * args[1] - element path
2029 attribute_get (assuan_context_t ctx
, char **args
)
2031 struct client_s
*client
= assuan_get_pointer (ctx
);
2035 struct xml_request_s
*req
;
2037 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
2038 return GPG_ERR_SYNTAX
;
2040 if (!xml_reserved_attribute (args
[0]))
2041 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2044 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2048 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2049 xml_free_request (req
);
2055 a
= xmlGetProp (n
, (xmlChar
*) args
[0]);
2057 return GPG_ERR_NOT_FOUND
;
2059 pthread_cleanup_push ((void *)xmlFree
, a
);
2062 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2064 rc
= GPG_ERR_NO_DATA
;
2066 pthread_cleanup_pop (1);
2071 * args[0] - attribute
2072 * args[1] - element path
2076 attribute_set (struct client_s
*client
, char **args
)
2078 struct xml_request_s
*req
;
2082 if (!args
|| !args
[0] || !args
[1])
2083 return GPG_ERR_SYNTAX
;
2086 * Reserved attribute names.
2088 if (!strcmp (args
[0], "_name")) // Use the RENAME command instead
2089 return GPG_ERR_INV_ATTR
;
2090 else if (!strcmp (args
[0], "target"))
2091 return set_target_attribute (client
, args
+ 1);
2092 else if (!xml_valid_attribute (args
[0]))
2093 return GPG_ERR_INV_VALUE
;
2095 if (!xml_valid_attribute_value (args
[2]))
2096 return GPG_ERR_INV_VALUE
;
2098 if (!xml_reserved_attribute (args
[0]))
2100 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2103 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
2106 rc
= xml_check_recursion (client
, req
);
2107 xml_free_request (req
);
2113 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2117 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2118 xml_free_request (req
);
2122 rc
= xml_is_element_owner (client
, n
);
2125 rc
= xml_add_attribute (client
, n
, args
[0], args
[2]);
2132 * req[1] - attribute name or element path if command is LIST
2133 * req[2] - element path
2134 * req[2] - element path or value
2138 do_attr (assuan_context_t ctx
, char *line
)
2140 struct client_s
*client
= assuan_get_pointer (ctx
);
2144 req
= str_split (line
, " ", 4);
2145 if (!req
|| !req
[0] || !req
[1])
2148 return GPG_ERR_SYNTAX
;
2151 pthread_cleanup_push ((void *)req_cleanup
, req
);
2153 if (strcasecmp (req
[0], "SET") == 0)
2154 rc
= attribute_set (client
, req
+ 1);
2155 else if (strcasecmp (req
[0], "GET") == 0)
2156 rc
= attribute_get (ctx
, req
+ 1);
2157 else if (strcasecmp (req
[0], "DELETE") == 0)
2158 rc
= attribute_delete (client
, req
+ 1);
2159 else if (strcasecmp (req
[0], "LIST") == 0)
2160 rc
= attribute_list (ctx
, req
+ 1);
2162 rc
= GPG_ERR_SYNTAX
;
2164 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2165 pthread_cleanup_pop (1);
2170 attr_command (assuan_context_t ctx
, char *line
)
2172 struct client_s
*client
= assuan_get_pointer (ctx
);
2174 struct argv_s
*args
[] = {
2175 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2179 rc
= parse_options (&line
, args
, client
, 1);
2180 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2181 rc
= GPG_ERR_SYNTAX
;
2183 return send_error (ctx
, rc
);
2185 if (client
->opts
& OPT_INQUIRE
)
2187 unsigned char *result
;
2190 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2192 return send_error (ctx
, rc
);
2194 pthread_cleanup_push ((void *)xfree
, result
);
2195 rc
= do_attr (ctx
, (char *)result
);
2196 pthread_cleanup_pop (1);
2199 rc
= do_attr (ctx
, line
);
2201 return send_error (ctx
, rc
);
2205 parse_iscached_opt_lock (void *data
, void *value
)
2207 struct client_s
*client
= data
;
2210 client
->opts
|= OPT_LOCK
;
2215 parse_iscached_opt_agent (void *data
, void *value
)
2217 struct client_s
*client
= data
;
2220 client
->opts
|= OPT_CACHE_AGENT
;
2225 parse_iscached_opt_sign (void *data
, void *value
)
2227 struct client_s
*client
= data
;
2230 client
->opts
|= OPT_CACHE_SIGN
;
2235 iscached_command (assuan_context_t ctx
, char *line
)
2237 struct client_s
*client
= assuan_get_pointer (ctx
);
2239 unsigned char md5file
[16];
2240 struct argv_s
*args
[] = {
2241 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2242 &(struct argv_s
) {"agent", OPTION_TYPE_NOARG
, parse_iscached_opt_agent
},
2243 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_iscached_opt_sign
},
2247 if (!line
|| !*line
)
2248 return send_error (ctx
, GPG_ERR_SYNTAX
);
2250 rc
= parse_options (&line
, args
, client
, 1);
2252 return send_error (ctx
, rc
);
2253 else if (!valid_filename (line
))
2254 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2256 if (!(client
->flags
& FLAG_OPEN
)
2257 && ((client
->opts
& OPT_CACHE_AGENT
) || (client
->opts
& OPT_CACHE_SIGN
)))
2258 return send_error (ctx
, GPG_ERR_INV_STATE
);
2260 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2261 rc
= cache_iscached (line
, NULL
, (client
->opts
& OPT_CACHE_AGENT
),
2262 (client
->opts
& OPT_CACHE_SIGN
));
2265 struct cache_data_s
*cdata
= cache_get_data (md5file
);
2269 rc
= validate_checksum (client
, line
, cdata
, NULL
, NULL
);
2270 if (rc
== GPG_ERR_CHECKSUM
)
2271 rc
= GPG_ERR_NO_DATA
;
2275 if (client
->opts
& OPT_LOCK
2276 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2278 gpg_error_t trc
= rc
;
2280 if (memcmp (md5file
, client
->md5file
, 16))
2281 cleanup_client (client
);
2283 memcpy (client
->md5file
, md5file
, 16);
2284 rc
= do_lock (client
, 1);
2289 return send_error (ctx
, rc
);
2293 clearcache_command (assuan_context_t ctx
, char *line
)
2295 gpg_error_t rc
= 0, all_rc
= 0;
2296 unsigned char md5file
[16];
2300 struct client_thread_s
*once
= NULL
;
2303 MUTEX_LOCK (&cn_mutex
);
2304 pthread_cleanup_push ((void *)cleanup_mutex_cb
, &cn_mutex
);
2306 if (!line
|| !*line
)
2309 t
= slist_length (cn_thread_list
);
2311 for (i
= 0; i
< t
; i
++)
2313 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2314 assuan_peercred_t peer
;
2319 /* Lock each connected clients' file mutex to prevent any other client
2320 * from accessing the cache entry (the file mutex is locked upon
2321 * command startup). The cache for the entry is not cleared if the
2322 * file mutex is locked by another client to prevent this function
2327 if (thd
->cl
->filename
)
2329 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2330 all_rc
= !all_rc
? rc
: all_rc
;
2338 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2339 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2341 if (pthread_equal (pthread_self (), thd
->tid
))
2345 if (!thd
->cl
->filename
||
2346 cache_iscached (thd
->cl
->filename
, NULL
, 0, 0) == GPG_ERR_NO_DATA
)
2352 cache_defer_clear (thd
->cl
->md5file
);
2355 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2363 rc
= cache_clear (thd
->cl
->md5file
, 1);
2364 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2372 /* A single data filename was specified. Lock only this data file
2373 * mutex and free the cache entry. */
2376 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2377 rc
= do_validate_peer (ctx
, line
, &peer
);
2379 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2381 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2383 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2385 if (pthread_equal (pthread_self (), thd
->tid
))
2392 rc
= cache_clear (thd
->cl
->md5file
, 1);
2393 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2397 cache_defer_clear (thd
->cl
->md5file
);
2405 /* Only connected clients' cache entries have been cleared. Now clear any
2406 * remaining cache entries without clients but only if there wasn't an
2407 * error from above since this would defeat the locking check of the
2408 * remaining entries. */
2411 cache_clear (NULL
, 1);
2414 /* No clients are using the specified file. */
2415 else if (!all_rc
&& !rc
&& !once
)
2417 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2418 rc
= cache_clear (md5file
, 1);
2421 /* Release the connection mutex. */
2422 pthread_cleanup_pop (1);
2426 send_status_all (STATUS_CACHE
, NULL
);
2428 /* One or more files were locked while clearing all cache entries. */
2432 return send_error (ctx
, rc
);
2436 cachetimeout_command (assuan_context_t ctx
, char *line
)
2439 char **req
= str_split (line
, " ", 0);
2442 assuan_peercred_t peer
;
2444 if (!req
|| !*req
|| !req
[1])
2447 return send_error (ctx
, GPG_ERR_SYNTAX
);
2451 timeout
= (int) strtol (req
[1], &p
, 10);
2452 if (errno
!= 0 || *p
|| timeout
< -1)
2455 return send_error (ctx
, GPG_ERR_SYNTAX
);
2458 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2461 unsigned char md5file
[16];
2463 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2464 rc
= cache_set_timeout (md5file
, timeout
);
2465 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2468 MUTEX_LOCK (&rcfile_mutex
);
2469 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2471 MUTEX_UNLOCK (&rcfile_mutex
);
2476 return send_error (ctx
, rc
);
2480 dump_command (assuan_context_t ctx
, char *line
)
2484 struct client_s
*client
= assuan_get_pointer (ctx
);
2487 if (disable_list_and_dump
== 1)
2488 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2491 return send_error (ctx
, GPG_ERR_SYNTAX
);
2493 rc
= peer_is_invoker(client
);
2495 return send_error (ctx
, rc
);
2497 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2501 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2502 pwmd_strerror (GPG_ERR_ENOMEM
));
2503 return send_error (ctx
, GPG_ERR_ENOMEM
);
2506 pthread_cleanup_push ((void *)xmlFree
, xml
);
2507 rc
= xfer_data (ctx
, (char *) xml
, len
);
2508 pthread_cleanup_pop (1);
2509 return send_error (ctx
, rc
);
2513 getconfig_command (assuan_context_t ctx
, char *line
)
2515 struct client_s
*client
= assuan_get_pointer (ctx
);
2517 char filename
[255] = { 0 }, param
[747] = { 0 };
2518 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2520 if (!line
|| !*line
)
2521 return send_error (ctx
, GPG_ERR_SYNTAX
);
2523 if (strchr (line
, ' '))
2525 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2530 if (fp
&& !valid_filename (fp
))
2531 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2533 paramp
= str_down (paramp
);
2534 if (!strcmp (paramp
, "passphrase"))
2535 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2537 p
= config_get_value (fp
? fp
: "global", paramp
);
2539 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2541 tmp
= expand_homedir (p
);
2545 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2546 pwmd_strerror (GPG_ERR_ENOMEM
));
2547 return send_error (ctx
, GPG_ERR_ENOMEM
);
2551 pthread_cleanup_push ((void *)xfree
, p
);
2552 rc
= xfer_data (ctx
, p
, strlen (p
));
2553 pthread_cleanup_pop (1);
2554 return send_error (ctx
, rc
);
2559 xmlXPathContextPtr xp
;
2560 xmlXPathObjectPtr result
;
2566 xpath_command_cleanup (void *arg
)
2568 struct xpath_s
*xpath
= arg
;
2573 req_cleanup (xpath
->req
);
2576 xmlBufferFree (xpath
->buf
);
2579 xmlXPathFreeObject (xpath
->result
);
2582 xmlXPathFreeContext (xpath
->xp
);
2586 do_xpath (assuan_context_t ctx
, char *line
)
2589 struct client_s
*client
= assuan_get_pointer (ctx
);
2590 struct xpath_s _x
= { 0 };
2591 struct xpath_s
*xpath
= &_x
;
2593 if (!line
|| !*line
)
2594 return GPG_ERR_SYNTAX
;
2596 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2598 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2599 return GPG_ERR_ENOMEM
;
2602 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2605 rc
= GPG_ERR_BAD_DATA
;
2610 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2613 rc
= GPG_ERR_BAD_DATA
;
2617 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2619 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2623 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2624 xpath
->result
->nodesetval
,
2625 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2629 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2631 rc
= GPG_ERR_NO_DATA
;
2634 else if (xpath
->req
[1])
2640 pthread_cleanup_push ((void *)xpath_command_cleanup
, &xpath
);
2641 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2642 xmlBufferLength (xpath
->buf
));
2643 pthread_cleanup_pop (0);
2645 xpath_command_cleanup (xpath
);
2650 xpath_command (assuan_context_t ctx
, char *line
)
2652 struct client_s
*client
= assuan_get_pointer (ctx
);
2654 struct argv_s
*args
[] = {
2655 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2659 if (disable_list_and_dump
== 1)
2660 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2662 rc
= peer_is_invoker(client
);
2664 return send_error (ctx
, rc
);
2666 rc
= parse_options (&line
, args
, client
, 1);
2667 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2668 rc
= GPG_ERR_SYNTAX
;
2670 return send_error (ctx
, rc
);
2672 if (client
->opts
& OPT_INQUIRE
)
2674 unsigned char *result
;
2677 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2679 return send_error (ctx
, rc
);
2681 pthread_cleanup_push ((void *)xfree
, result
);
2682 rc
= do_xpath (ctx
, (char *)result
);
2683 pthread_cleanup_pop (1);
2686 rc
= do_xpath (ctx
, line
);
2688 return send_error (ctx
, rc
);
2692 do_xpathattr (assuan_context_t ctx
, char *line
)
2694 struct client_s
*client
= assuan_get_pointer (ctx
);
2698 struct xpath_s _x
= { 0 };
2699 struct xpath_s
*xpath
= &_x
;
2701 if (!line
|| !*line
)
2702 return GPG_ERR_SYNTAX
;
2704 if ((req
= str_split (line
, " ", 3)) == NULL
)
2705 return GPG_ERR_ENOMEM
;
2709 rc
= GPG_ERR_SYNTAX
;
2713 if (!strcasecmp (req
[0], "SET"))
2715 else if (!strcasecmp (req
[0], "DELETE"))
2719 rc
= GPG_ERR_SYNTAX
;
2723 if (!req
[1] || !req
[2])
2725 rc
= GPG_ERR_SYNTAX
;
2729 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2731 rc
= GPG_ERR_ENOMEM
;
2735 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2737 rc
= GPG_ERR_SYNTAX
;
2741 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2744 rc
= GPG_ERR_BAD_DATA
;
2748 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2751 rc
= GPG_ERR_BAD_DATA
;
2755 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2757 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2761 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2762 xpath
->result
->nodesetval
,
2763 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2764 (xmlChar
*) req
[1]);
2767 xpath_command_cleanup (xpath
);
2772 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2774 xpathattr_command (assuan_context_t ctx
, char *line
)
2776 struct client_s
*client
= assuan_get_pointer (ctx
);
2778 struct argv_s
*args
[] = {
2779 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2783 if (disable_list_and_dump
== 1)
2784 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2786 rc
= peer_is_invoker(client
);
2788 return send_error (ctx
, rc
);
2790 rc
= parse_options (&line
, args
, client
, 1);
2791 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2792 rc
= GPG_ERR_SYNTAX
;
2794 return send_error (ctx
, rc
);
2796 if (client
->opts
& OPT_INQUIRE
)
2798 unsigned char *result
;
2801 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2803 return send_error (ctx
, rc
);
2805 pthread_cleanup_push ((void *)xfree
, result
);
2806 rc
= do_xpathattr (ctx
, (char *)result
);
2807 pthread_cleanup_pop (1);
2810 rc
= do_xpathattr (ctx
, line
);
2812 return send_error (ctx
, rc
);
2816 do_import (struct client_s
*client
, const char *root_element
,
2817 unsigned char *content
)
2819 xmlDocPtr doc
= NULL
;
2820 xmlNodePtr n
= NULL
, root
;
2822 struct string_s
*str
= NULL
;
2823 struct xml_request_s
*req
= NULL
;
2825 if (!content
|| !*content
)
2826 return GPG_ERR_SYNTAX
;
2830 rc
= xml_new_request (client
, root_element
, XML_CMD_STORE
, &req
);
2837 if (!xml_valid_element_path (req
->args
, 0))
2839 xml_free_request (req
);
2841 return GPG_ERR_INV_VALUE
;
2845 str
= string_new_content ((char *)content
);
2846 str
= string_prepend (str
, "<pwmd>");
2847 str
= string_append (str
, "</pwmd>");
2848 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2849 string_free (str
, 1);
2852 rc
= GPG_ERR_BAD_DATA
;
2856 root
= xmlDocGetRootElement (doc
);
2857 root
= root
->children
;
2858 if (root
->type
!= XML_ELEMENT_NODE
)
2860 rc
= GPG_ERR_BAD_DATA
;
2864 rc
= xml_validate_import (client
, root
);
2870 /* Create the new specified root element path, if needed. */
2871 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
2876 /* Overwrite the children of the specified root element path. */
2877 (void)xml_unlink_node (client
, n
->children
);
2881 n
= xmlDocGetRootElement (client
->doc
);
2886 xmlChar
*name
= xml_attribute_value (root
, (xmlChar
*)"_name");
2889 rc
= GPG_ERR_BAD_DATA
;
2892 /* No --root argument passed. Overwrite the current documents' root
2893 * element matching the root element of the imported data. */
2894 xml_free_request (req
);
2896 rc
= xml_new_request (client
, (char *)name
, XML_CMD_DELETE
, &req
);
2902 tmp
= xml_find_elements (client
, req
,
2903 xmlDocGetRootElement (req
->doc
), &rc
);
2904 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2908 (void)xml_unlink_node (client
, tmp
);
2916 copy
= xmlCopyNodeList (root
);
2918 rc
= GPG_ERR_ENOMEM
;
2921 n
= xmlAddChildList (n
, copy
);
2923 rc
= GPG_ERR_ENOMEM
;
2928 if (!rc
&& n
&& n
->parent
)
2929 rc
= xml_update_element_mtime (client
, n
->parent
);
2932 xml_free_request (req
);
2941 parse_import_opt_root (void *data
, void *value
)
2943 struct client_s
*client
= data
;
2945 client
->import_root
= str_dup (value
);
2946 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
2950 import_command (assuan_context_t ctx
, char *line
)
2953 struct client_s
*client
= assuan_get_pointer (ctx
);
2954 unsigned char *result
;
2956 struct argv_s
*args
[] = {
2957 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
2961 xfree (client
->import_root
);
2962 client
->import_root
= NULL
;
2963 rc
= parse_options (&line
, args
, client
, 0);
2965 return send_error (ctx
, rc
);
2967 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2970 xfree (client
->import_root
);
2971 client
->import_root
= NULL
;
2972 return send_error (ctx
, rc
);
2975 rc
= do_import (client
, client
->import_root
, result
);
2976 xfree (client
->import_root
);
2977 client
->import_root
= NULL
;
2978 return send_error (ctx
, rc
);
2982 do_lock (struct client_s
*client
, int add
)
2984 gpg_error_t rc
= lock_file_mutex (client
, add
);
2987 client
->flags
|= FLAG_LOCK_CMD
;
2993 lock_command (assuan_context_t ctx
, char *line
)
2995 struct client_s
*client
= assuan_get_pointer (ctx
);
2999 return send_error (ctx
, GPG_ERR_SYNTAX
);
3001 rc
= do_lock (client
, 0);
3002 return send_error (ctx
, rc
);
3006 unlock_command (assuan_context_t ctx
, char *line
)
3008 struct client_s
*client
= assuan_get_pointer (ctx
);
3012 return send_error (ctx
, GPG_ERR_SYNTAX
);
3014 rc
= unlock_file_mutex (client
, 0);
3015 return send_error (ctx
, rc
);
3019 get_set_env (const char *name
, const char *value
)
3021 if (value
&& *value
)
3022 setenv (name
, value
, 1);
3028 option_command (assuan_context_t ctx
, char *line
)
3030 struct client_s
*client
= assuan_get_pointer (ctx
);
3032 char namebuf
[255] = { 0 };
3033 char *name
= namebuf
;
3034 char *value
= NULL
, *p
, *tmp
= NULL
;
3036 p
= strchr (line
, '=');
3039 strncpy (namebuf
, line
, sizeof(namebuf
));
3040 namebuf
[sizeof(namebuf
)-1] = 0;
3044 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3045 namebuf
[sizeof(namebuf
)-1] = 0;
3049 log_write2 ("OPTION name='%s' value='%s'", name
, value
);
3051 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3057 n
= strtol (value
, &tmp
, 10);
3059 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3062 client
->lock_timeout
= n
;
3064 else if (strcasecmp (name
, (char *) "NAME") == 0)
3066 if (value
&& strchr (value
, ' '))
3067 rc
= GPG_ERR_INV_VALUE
;
3070 tmp
= pthread_getspecific (thread_name_key
);
3072 MUTEX_LOCK (&cn_mutex
);
3073 xfree (client
->thd
->name
);
3074 client
->thd
->name
= NULL
;
3076 if (!value
|| !*value
)
3077 pthread_setspecific (thread_name_key
, str_dup (""));
3080 client
->thd
->name
= str_dup (value
);
3081 pthread_setspecific (thread_name_key
, str_dup (value
));
3084 MUTEX_UNLOCK (&cn_mutex
);
3087 else if (strcasecmp (name
, "disable-pinentry") == 0)
3091 if (value
&& *value
)
3093 n
= (int) strtol (value
, &tmp
, 10);
3094 if (*tmp
|| n
< 0 || n
> 1)
3095 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3099 client
->flags
|= FLAG_NO_PINENTRY
;
3101 client
->flags
&= ~FLAG_NO_PINENTRY
;
3103 else if (strcasecmp (name
, "ttyname") == 0)
3105 get_set_env ("GPG_TTY", value
);
3107 else if (strcasecmp (name
, "ttytype") == 0)
3109 get_set_env ("TERM", value
);
3111 else if (strcasecmp (name
, "display") == 0)
3113 get_set_env ("DISPLAY", value
);
3115 else if (strcasecmp (name
, "lc_messages") == 0)
3118 else if (strcasecmp (name
, "lc_ctype") == 0)
3121 else if (strcasecmp (name
, "desc") == 0)
3124 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3128 rc
= GPG_ERR_UNKNOWN_OPTION
;
3130 return send_error (ctx
, rc
);
3134 do_rename (assuan_context_t ctx
, char *line
)
3136 struct client_s
*client
= assuan_get_pointer (ctx
);
3138 xmlNodePtr src
, dst
;
3139 struct string_s
*str
= NULL
, *tstr
;
3140 struct xml_request_s
*req
;
3143 args
= str_split (line
, " ", 0);
3144 if (!args
|| strv_length (args
) != 2)
3147 return GPG_ERR_SYNTAX
;
3150 if (!xml_valid_element ((xmlChar
*) args
[1]))
3153 return GPG_ERR_INV_VALUE
;
3156 rc
= xml_new_request (client
, args
[0], XML_CMD_RENAME
, &req
);
3163 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3167 rc
= xml_is_element_owner (client
, src
);
3171 xmlChar
*a
= xmlGetProp (src
, (xmlChar
*) "_name");
3174 rc
= GPG_ERR_ENOMEM
;
3178 /* To prevent unwanted effects:
3180 * <element _name="a"><element _name="b"/></element>
3184 if (xmlStrEqual (a
, (xmlChar
*) args
[1]))
3187 rc
= GPG_ERR_EEXIST
;
3192 str
= string_new (args
[0]);
3195 rc
= GPG_ERR_ENOMEM
;
3199 p
= strrchr (str
->str
, '\t');
3201 tstr
= string_truncate (str
, strlen (str
->str
)-strlen (p
));
3203 tstr
= string_truncate (str
, 0);
3207 string_free (str
, 1);
3208 rc
= GPG_ERR_ENOMEM
;
3213 tstr
= string_append_printf (str
, "%s%s", str
->len
? "\t" : "", args
[1]);
3216 string_free (str
, 1);
3217 rc
= GPG_ERR_ENOMEM
;
3222 xml_free_request (req
);
3223 rc
= xml_new_request (client
, str
->str
, XML_CMD_RENAME
, &req
);
3224 string_free (str
, 1);
3231 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3232 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3237 /* Target may exist:
3239 * <element _name="a"/>
3240 * <element _name="b" target="a"/>
3246 rc
= GPG_ERR_EEXIST
;
3252 rc
= xml_is_element_owner (client
, dst
);
3256 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3258 xml_unlink_node (client
, dst
);
3261 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3264 xml_free_request (req
);
3270 rename_command (assuan_context_t ctx
, char *line
)
3272 struct client_s
*client
= assuan_get_pointer (ctx
);
3274 struct argv_s
*args
[] = {
3275 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3279 rc
= parse_options (&line
, args
, client
, 1);
3280 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3281 rc
= GPG_ERR_SYNTAX
;
3283 return send_error (ctx
, rc
);
3285 if (client
->opts
& OPT_INQUIRE
)
3287 unsigned char *result
;
3290 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3292 return send_error (ctx
, rc
);
3294 pthread_cleanup_push ((void *)xfree
, result
);
3295 rc
= do_rename (ctx
, (char *)result
);
3296 pthread_cleanup_pop (1);
3299 rc
= do_rename (ctx
, line
);
3301 return send_error (ctx
, rc
);
3305 do_copy (assuan_context_t ctx
, char *line
)
3307 struct client_s
*client
= assuan_get_pointer (ctx
);
3308 char **args
, **targs
;
3309 xmlNodePtr src
, dst
, tree
= NULL
;
3310 struct xml_request_s
*req
;
3313 args
= str_split (line
, " ", 0);
3314 if (!args
|| strv_length (args
) != 2)
3317 return GPG_ERR_SYNTAX
;
3320 targs
= str_split (args
[1], "\t", 0);
3324 return GPG_ERR_SYNTAX
;
3327 if (!xml_valid_element_path (targs
, 0))
3331 return GPG_ERR_INV_VALUE
;
3335 rc
= xml_new_request (client
, args
[0], XML_CMD_NONE
, &req
);
3342 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3346 tree
= xmlCopyNodeList (src
);
3349 rc
= GPG_ERR_ENOMEM
;
3353 xml_free_request (req
);
3354 /* Create the destination element path if it does not exist. */
3355 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &req
);
3362 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3366 rc
= xml_is_element_owner (client
, dst
);
3370 rc
= xml_validate_target (client
, req
->doc
, args
[1], src
);
3371 if (rc
|| src
== dst
)
3373 rc
= rc
? rc
: GPG_ERR_EEXIST
;
3377 /* Merge any attributes from the src node to the initial dst node. */
3378 for (xmlAttrPtr attr
= tree
->properties
; attr
; attr
= attr
->next
)
3380 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3383 xmlAttrPtr a
= xmlHasProp (dst
, attr
->name
);
3387 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3388 xmlNewProp (dst
, attr
->name
, tmp
);
3390 /* Create the default attributes. */
3391 rc
= xml_add_attribute (client
, dst
, NULL
, NULL
);
3394 xmlNodePtr n
= dst
->children
;
3395 (void)xml_unlink_node (client
, n
);
3396 dst
->children
= NULL
;
3400 n
= xmlCopyNodeList (tree
->children
);
3403 rc
= GPG_ERR_ENOMEM
;
3407 n
= xmlAddChildList (dst
, n
);
3410 rc
= GPG_ERR_ENOMEM
;
3414 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
)
3415 == dst
->parent
? dst
: dst
->parent
);
3420 (void)xml_unlink_node (client
, tree
);
3422 xml_free_request (req
);
3428 copy_command (assuan_context_t ctx
, char *line
)
3430 struct client_s
*client
= assuan_get_pointer (ctx
);
3432 struct argv_s
*args
[] = {
3433 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3437 rc
= parse_options (&line
, args
, client
, 1);
3438 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3439 rc
= GPG_ERR_SYNTAX
;
3441 return send_error (ctx
, rc
);
3443 if (client
->opts
& OPT_INQUIRE
)
3445 unsigned char *result
;
3448 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3450 return send_error (ctx
, rc
);
3452 pthread_cleanup_push ((void *)xfree
, result
);
3453 rc
= do_copy (ctx
, (char *)result
);
3454 pthread_cleanup_pop (1);
3457 rc
= do_copy (ctx
, line
);
3459 return send_error (ctx
, rc
);
3463 do_move (assuan_context_t ctx
, char *line
)
3465 struct client_s
*client
= assuan_get_pointer (ctx
);
3467 xmlNodePtr src
, dst
;
3468 struct xml_request_s
*req
;
3471 args
= str_split (line
, " ", 0);
3472 if (!args
|| strv_length (args
) != 2)
3475 return GPG_ERR_SYNTAX
;
3480 char **targs
= str_split (args
[1], "\t", 0);
3484 return GPG_ERR_ENOMEM
;
3487 if (!xml_valid_element_path (targs
, 0))
3491 return GPG_ERR_INV_VALUE
;
3497 rc
= xml_new_request (client
, args
[0], XML_CMD_MOVE
, &req
);
3504 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3508 rc
= xml_is_element_owner (client
, src
);
3512 xml_free_request (req
);
3516 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
3520 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
3524 dst
= xmlDocGetRootElement (client
->doc
);
3526 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3531 for (xmlNodePtr n
= dst
; n
; n
= n
->parent
)
3535 rc
= GPG_ERR_EEXIST
;
3542 xmlChar
*a
= xml_attribute_value (src
, (xmlChar
*) "_name");
3543 xmlNodePtr dup
= xml_find_element (client
, dst
->children
, (char *) a
, &rc
);
3546 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3555 rc
= GPG_ERR_EEXIST
;
3559 if (dst
== xmlDocGetRootElement (client
->doc
))
3564 while (n
->parent
&& n
->parent
!= dst
)
3567 xmlChar
*a
= xml_attribute_value (n
, (xmlChar
*) "_name");
3568 xmlChar
*b
= xml_attribute_value (src
, (xmlChar
*) "_name");
3570 if (xmlStrEqual (a
, b
))
3573 xmlUnlinkNode (src
);
3574 (void)xml_unlink_node (client
, n
);
3581 (void)xml_unlink_node (client
, dup
);
3584 xmlUnlinkNode (dup
);
3588 if (!dst
&& *req
->args
)
3590 struct xml_request_s
*nreq
= NULL
;
3591 xmlChar
*name
= xml_attribute_value (src
, (xmlChar
*) "_name");
3593 if (src
->parent
== xmlDocGetRootElement (client
->doc
)
3594 && !strcmp ((char *) name
, *req
->args
))
3597 rc
= GPG_ERR_EEXIST
;
3602 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &nreq
);
3605 dst
= xml_find_elements (client
, nreq
,
3606 xmlDocGetRootElement (nreq
->doc
), &rc
);
3607 xml_free_request (nreq
);
3614 xml_update_element_mtime (client
, src
->parent
);
3615 xmlUnlinkNode (src
);
3617 dst
= xmlAddChildList (dst
, src
);
3619 rc
= GPG_ERR_ENOMEM
;
3621 xml_update_element_mtime (client
, dst
->parent
);
3624 xml_free_request (req
);
3630 move_command (assuan_context_t ctx
, char *line
)
3632 struct client_s
*client
= assuan_get_pointer (ctx
);
3634 struct argv_s
*args
[] = {
3635 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3639 rc
= parse_options (&line
, args
, client
, 1);
3640 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3641 rc
= GPG_ERR_SYNTAX
;
3643 return send_error (ctx
, rc
);
3645 if (client
->opts
& OPT_INQUIRE
)
3647 unsigned char *result
;
3650 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3652 return send_error (ctx
, rc
);
3654 pthread_cleanup_push ((void *)xfree
, result
);
3655 rc
= do_move (ctx
, (char *)result
);
3656 pthread_cleanup_pop (1);
3659 rc
= do_move (ctx
, line
);
3661 return send_error (ctx
, rc
);
3665 ls_command (assuan_context_t ctx
, char *line
)
3673 return send_error (ctx
, GPG_ERR_SYNTAX
);
3675 tmp
= str_asprintf ("%s/data", homedir
);
3676 dir
= expand_homedir (tmp
);
3679 rc
= gpg_error_from_errno (errno
);
3684 return send_error (ctx
, rc
);
3688 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3689 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3693 pthread_cleanup_push ((void *)xfree
, p
);
3694 pthread_cleanup_push ((void *)(void *)(void *)closedir
, d
);
3697 while (!readdir_r (d
, p
, &cur
) && cur
)
3701 if (stat (cur
->d_name
, &st
) == -1 || !S_ISREG (st
.st_mode
))
3704 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3711 rc
= GPG_ERR_ENOMEM
;
3719 pthread_cleanup_pop (1); // closedir (d)
3720 pthread_cleanup_pop (1); // xfree (p)
3723 return send_error (ctx
, rc
);
3726 return send_error (ctx
, GPG_ERR_NO_DATA
);
3728 list
[strlen (list
) - 1] = 0;
3729 pthread_cleanup_push ((void *)xfree
, list
);
3730 rc
= xfer_data (ctx
, list
, strlen (list
));
3731 pthread_cleanup_pop (1);
3732 return send_error (ctx
, rc
);
3736 bye_notify (assuan_context_t ctx
, char *line
)
3738 struct client_s
*cl
= assuan_get_pointer (ctx
);
3739 gpg_error_t ret
= 0;
3741 update_client_state (cl
, CLIENT_STATE_DISCON
);
3744 if (cl
->thd
->remote
)
3750 struct timeval tv
= { 0, 50000 };
3752 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3753 if (rc
== GNUTLS_E_AGAIN
)
3754 select (0, NULL
, NULL
, NULL
, &tv
);
3756 while (rc
== GNUTLS_E_AGAIN
);
3760 /* This will let assuan_process_next() return. */
3761 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
3763 cl
->last_rc
= gpg_error_from_errno (errno
);
3767 cl
->last_rc
= 0; // BYE command result
3772 reset_notify (assuan_context_t ctx
, char *line
)
3774 struct client_s
*client
= assuan_get_pointer (ctx
);
3777 cleanup_client (client
);
3783 * This is called before every Assuan command.
3786 command_startup (assuan_context_t ctx
, const char *name
)
3788 struct client_s
*client
= assuan_get_pointer (ctx
);
3790 struct command_table_s
*cmd
= NULL
;
3792 log_write2 ("command='%s'", name
);
3793 client
->last_rc
= client
->opts
= 0;
3795 for (int i
= 0; command_table
[i
]; i
++)
3797 if (!strcasecmp (name
, command_table
[i
]->name
))
3799 if (command_table
[i
]->ignore_startup
)
3801 cmd
= command_table
[i
];
3807 return GPG_ERR_UNKNOWN_COMMAND
;
3809 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3811 update_client_state (client
, CLIENT_STATE_COMMAND
);
3817 * This is called after every Assuan command.
3820 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3822 struct client_s
*client
= assuan_get_pointer (ctx
);
3824 if (!(client
->flags
& FLAG_LOCK_CMD
))
3825 unlock_file_mutex (client
, 0);
3827 unlock_flock (&client
->flock_fd
);
3828 log_write2 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
3829 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3831 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3833 if (client
->thd
->state
!= CLIENT_STATE_DISCON
)
3834 update_client_state (client
, CLIENT_STATE_IDLE
);
3838 parse_help_opt_html (void *data
, void *value
)
3840 struct client_s
*client
= data
;
3843 client
->opts
|= OPT_HTML
;
3848 help_command (assuan_context_t ctx
, char *line
)
3852 struct client_s
*client
= assuan_get_pointer (ctx
);
3853 struct argv_s
*args
[] = {
3854 &(struct argv_s
) {"html", OPTION_TYPE_NOARG
, parse_help_opt_html
},
3858 rc
= parse_options (&line
, args
, client
, 1);
3860 return send_error (ctx
, rc
);
3862 if (!line
|| !*line
)
3865 char *help
= str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3866 "For commands that take an element path as an argument, each element is "
3867 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3870 for (i
= 0; command_table
[i
]; i
++)
3872 if (!command_table
[i
]->help
)
3875 /* @npxref{} won't put a "See " or "see " in front of the command.
3876 * It's not a texinfo command but needed for --html. */
3877 tmp
= str_asprintf ("%s %s%s%s", help
,
3878 client
->opts
& OPT_HTML
? "@npxref{" : "",
3879 command_table
[i
]->name
,
3880 client
->opts
& OPT_HTML
? "}" : "");
3885 tmp
= strip_texi_and_wrap (help
, client
->opts
& OPT_HTML
);
3887 pthread_cleanup_push ((void *)xfree
, tmp
);
3888 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3889 pthread_cleanup_pop (1);
3890 return send_error (ctx
, rc
);
3893 for (i
= 0; command_table
[i
]; i
++)
3895 if (!strcasecmp (line
, command_table
[i
]->name
))
3899 if (!command_table
[i
]->help
)
3902 help
= strip_texi_and_wrap (command_table
[i
]->help
,
3903 client
->opts
& OPT_HTML
);
3904 tmp
= str_asprintf ("%s%s",
3905 (client
->opts
& OPT_HTML
) ? "" : _("Usage: "),
3908 pthread_cleanup_push ((void *)xfree
, tmp
);
3909 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3910 pthread_cleanup_pop (1);
3911 return send_error (ctx
, rc
);
3915 return send_error (ctx
, GPG_ERR_INV_NAME
);
3919 new_command (const char *name
, int ignore
, int unlock
, int flock_type
,
3920 gpg_error_t (*handler
) (assuan_context_t
, char *),
3924 struct command_table_s
**tmp
;
3927 for (i
= 0; command_table
[i
]; i
++);
3929 tmp
= xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3931 command_table
= tmp
;
3932 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3933 command_table
[i
]->name
= name
;
3934 command_table
[i
]->handler
= handler
;
3935 command_table
[i
]->ignore_startup
= ignore
;
3936 command_table
[i
]->unlock
= unlock
;
3937 command_table
[i
]->flock_type
= flock_type
;
3938 command_table
[i
++]->help
= help
;
3939 command_table
[i
] = NULL
;
3947 for (i
= 0; command_table
[i
]; i
++)
3948 xfree (command_table
[i
]);
3950 xfree (command_table
);
3954 sort_commands (const void *arg1
, const void *arg2
)
3956 struct command_table_s
*const *a
= arg1
;
3957 struct command_table_s
*const *b
= arg2
;
3966 return strcmp ((*a
)->name
, (*b
)->name
);
3970 passwd_command (assuan_context_t ctx
, char *line
)
3972 struct client_s
*client
= assuan_get_pointer (ctx
);
3975 rc
= peer_is_invoker (client
);
3976 if (rc
== GPG_ERR_EACCES
)
3977 return send_error (ctx
, GPG_ERR_FORBIDDEN
);
3979 return send_error (ctx
, rc
);
3981 if (client
->flags
& FLAG_NEW
)
3982 return send_error (ctx
, GPG_ERR_INV_STATE
);
3984 client
->crypto
->keyfile
= config_get_string (client
->filename
,
3986 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
,
3987 client
->crypto
->keyfile
);
3989 return send_error (ctx
, rc
);
3992 rc
= crypto_passwd (client
, client
->crypto
);
3994 crypto_free_non_keys (client
->crypto
);
3995 return send_error (ctx
, rc
);
3999 parse_opt_data (void *data
, void *value
)
4001 struct client_s
*client
= data
;
4004 client
->opts
|= OPT_DATA
;
4009 send_client_list (assuan_context_t ctx
)
4011 struct client_s
*client
= assuan_get_pointer (ctx
);
4014 if (client
->opts
& OPT_VERBOSE
)
4020 MUTEX_LOCK (&cn_mutex
);
4021 pthread_cleanup_push ((void *)cleanup_mutex_cb
, &cn_mutex
);
4022 t
= slist_length (cn_thread_list
);
4024 for (i
= 0; i
< t
; i
++)
4026 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4029 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4032 tmp
= build_client_info_line (thd
, 0);
4035 char **l
= strv_cat (list
, tmp
);
4037 rc
= GPG_ERR_ENOMEM
;
4042 rc
= GPG_ERR_ENOMEM
;
4051 pthread_cleanup_pop (1);
4055 line
= strv_join ("\n", list
);
4057 pthread_cleanup_push ((void *)xfree
, line
);
4058 rc
= xfer_data (ctx
, line
, strlen (line
));
4059 pthread_cleanup_pop (1);
4063 if (client
->opts
& OPT_DATA
)
4065 char buf
[ASSUAN_LINELENGTH
];
4067 MUTEX_LOCK (&cn_mutex
);
4068 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
4069 MUTEX_UNLOCK (&cn_mutex
);
4070 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4073 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4079 getinfo_command (assuan_context_t ctx
, char *line
)
4081 struct client_s
*client
= assuan_get_pointer (ctx
);
4083 char buf
[ASSUAN_LINELENGTH
];
4084 struct argv_s
*args
[] = {
4085 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4086 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4090 rc
= parse_options (&line
, args
, client
, 1);
4092 return send_error (ctx
, rc
);
4094 if (!strcasecmp (line
, "clients"))
4096 rc
= send_client_list (ctx
);
4098 else if (!strcasecmp (line
, "cache"))
4100 if (client
->opts
& OPT_DATA
)
4102 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4103 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4106 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4108 else if (!strcasecmp (line
, "pid"))
4111 pid_t pid
= getpid ();
4113 snprintf (buf
, sizeof (buf
), "%i", pid
);
4114 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4116 else if (!strcasecmp (line
, "version"))
4118 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4126 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4129 else if (!strcasecmp (line
, "last_error"))
4131 if (client
->last_error
)
4132 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4134 rc
= GPG_ERR_NO_DATA
;
4136 else if (!strcasecmp (line
, "user"))
4141 if (client
->thd
->remote
)
4142 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4144 user
= get_username (client
->thd
->peer
->uid
);
4146 user
= get_username (client
->thd
->peer
->uid
);
4150 pthread_cleanup_push ((void *)xfree
, user
);
4151 rc
= xfer_data (ctx
, user
, strlen (user
));
4152 pthread_cleanup_pop (1);
4155 rc
= GPG_ERR_NO_DATA
;
4158 rc
= gpg_error (GPG_ERR_SYNTAX
);
4160 return send_error (ctx
, rc
);
4164 parse_listkeys_opt_secret_only (void *data
, void *value
)
4166 struct client_s
*client
= data
;
4169 client
->opts
|= OPT_SECRET_ONLY
;
4174 keyinfo_command (assuan_context_t ctx
, char *line
)
4176 struct client_s
*client
= assuan_get_pointer (ctx
);
4178 char **keys
= NULL
, **p
= NULL
;
4182 return send_error (ctx
, GPG_ERR_SYNTAX
);
4184 if (!(client
->flags
& FLAG_OPEN
))
4185 return send_error (ctx
, GPG_ERR_INV_STATE
);
4187 if (client
->flags
& FLAG_NEW
)
4188 return send_error (ctx
, GPG_ERR_NO_DATA
);
4190 rc
= lock_flock (ctx
, client
->filename
, FLOCK_TYPE_SH
, &client
->flock_fd
);
4192 return send_error (ctx
, rc
);
4194 rc
= crypto_is_symmetric (client
->filename
);
4195 unlock_flock (&client
->flock_fd
);
4198 else if (rc
!= GPG_ERR_BAD_DATA
)
4199 return send_error (ctx
, rc
);
4204 p
= strv_catv(keys
, client
->crypto
->pubkey
);
4206 rc
= GPG_ERR_ENOMEM
;
4212 for (p
= client
->crypto
->sigkey
; p
&& *p
; p
++)
4214 if (!strv_printf(&keys
, "S%s", *p
))
4217 return send_error (ctx
, GPG_ERR_ENOMEM
);
4222 rc
= GPG_ERR_ENOMEM
;
4226 line
= strv_join ("\n", keys
);
4228 pthread_cleanup_push ((void *)xfree
, line
);
4230 rc
= xfer_data (ctx
, line
, strlen (line
));
4232 rc
= GPG_ERR_ENOMEM
;
4234 pthread_cleanup_pop (1);
4237 rc
= GPG_ERR_NO_DATA
;
4241 return send_error (ctx
, rc
);
4245 kill_command (assuan_context_t ctx
, char *line
)
4247 struct client_s
*client
= assuan_get_pointer (ctx
);
4251 if (!line
|| !*line
)
4252 return send_error (ctx
, GPG_ERR_SYNTAX
);
4254 MUTEX_LOCK (&cn_mutex
);
4255 pthread_cleanup_push ((void *)cleanup_mutex_cb
, &cn_mutex
);
4256 t
= slist_length (cn_thread_list
);
4259 for (i
= 0; i
< t
; i
++)
4261 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4262 char *tmp
= str_asprintf ("%p", thd
->tid
);
4264 if (strcmp (line
, tmp
))
4271 rc
= peer_is_invoker (client
);
4274 #ifdef HAVE_PTHREAD_CANCEL
4275 rc
= pthread_cancel (thd
->tid
);
4283 rc
= GPG_ERR_FORBIDDEN
;
4284 if (config_get_boolean ("global", "strict_kill"))
4288 if (client
->thd
->remote
&& thd
->remote
)
4290 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
4292 #ifdef HAVE_PTHREAD_CANCEL
4293 rc
= pthread_cancel (thd
->tid
);
4301 else if (!client
->thd
->remote
&& !thd
->remote
)
4304 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
4306 #ifdef HAVE_PTHREAD_CANCEL
4307 rc
= pthread_cancel (thd
->tid
);
4317 pthread_cleanup_pop (1);
4318 return send_error (ctx
, rc
);
4322 listkeys_command (assuan_context_t ctx
, char *line
)
4324 struct client_s
*client
= assuan_get_pointer (ctx
);
4325 struct crypto_s
*crypto
= NULL
;
4326 char **pattern
= NULL
;
4327 gpgme_key_t
*keys
= NULL
;
4328 char **result
= NULL
;
4330 struct argv_s
*args
[] = {
4331 &(struct argv_s
) {"secret-only", OPTION_TYPE_NOARG
,
4332 parse_listkeys_opt_secret_only
},
4336 rc
= parse_options (&line
, args
, client
, 1);
4338 return send_error (ctx
, rc
);
4340 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
4341 client
->flags
& FLAG_NO_PINENTRY
, NULL
);
4343 return send_error (ctx
, rc
);
4345 pthread_cleanup_push ((void *)crypto_free
, crypto
);
4346 pattern
= str_split (line
, ",", 0);
4347 pthread_cleanup_push ((void *)(void *)strv_free
, pattern
);
4348 rc
= crypto_list_keys (crypto
, pattern
, client
->opts
& OPT_SECRET_ONLY
,
4350 pthread_cleanup_pop (1);
4351 pthread_cleanup_pop (1);
4356 for (i
= 0; keys
[i
]; i
++)
4358 char *p
= crypto_key_info (keys
[i
]);
4363 rc
= GPG_ERR_ENOMEM
;
4367 r
= strv_cat (result
, p
);
4370 rc
= GPG_ERR_ENOMEM
;
4378 rc
= GPG_ERR_NO_DATA
;
4380 crypto_free_key_list (keys
);
4385 line
= strv_join ("\n", result
);
4387 pthread_cleanup_push ((void *)xfree
, line
);
4389 rc
= GPG_ERR_ENOMEM
;
4391 rc
= xfer_data (ctx
, line
, strlen (line
));
4393 pthread_cleanup_pop (1);
4398 return send_error (ctx
, rc
);
4404 /* !BEGIN-HELP-TEXT!
4406 * This comment is used as a marker to generate the offline documentation
4407 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4408 * script to determine where commands begin and end.
4410 new_command("HELP", 1, 1, 0, help_command
, _(
4411 "HELP [--html] [<COMMAND>]\n"
4412 "Show available commands or command specific help text."
4414 "The @option{--html} option will output the help text in HTML format."
4417 new_command("KILL", 1, 0, 0, kill_command
, _(
4418 "KILL <thread_id>\n"
4419 "Terminates the client identified by @var{thread_id} and releases any file "
4420 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4421 "for details about listing connected clients. An @code{invoking_user} "
4422 "(@pxref{Configuration}) may kill any client while others may only kill "
4423 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4426 new_command("LISTKEYS", 0, 0, 0, listkeys_command
, _(
4427 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4428 "Returns a new line separated list of key information matching a comma "
4429 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4430 "specified, only keys matching @var{pattern} that also have a secret key "
4431 "available will be returned."
4434 new_command("KEYINFO", 1, 0, 0, keyinfo_command
, _(
4436 "Returns a new line separated list of key ID's that the currently opened "
4437 "data file has recipients and signers for. If the key is a signing key it "
4438 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4439 "signers in the case of being symmetrically encrypted, the error code "
4440 "@code{GPG_ERR_NO_DATA} is returned."
4443 new_command("GETINFO", 1, 1, 0, getinfo_command
, _(
4444 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4445 "Get server and other information. The information is returned via a status "
4446 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4449 "@var{CACHE} returns the number of cached documents."
4451 "@var{CLIENTS} returns the number of "
4452 "connected clients via a status message or a list of connected clients when "
4453 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4454 "verbose line of a client list contains "
4456 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4457 "IP address if remote, file lock status, whether the current client is self "
4458 "or not, client state (see below), "
4459 "user ID or TLS fingerprint of the connected client, username if the "
4460 "client is a local one else @code{-}, and finally the time stamp of when the "
4463 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4464 "the client has connected but hasn't completed initializing, state @code{2} "
4465 "indicates that the client is idle, state @code{3} means the "
4466 "client is in a command and state @code{4} means the client is disconnecting. "
4468 "@var{PID} returns the process ID number of the server via a data response."
4470 "@var{VERSION} returns the server version number and compile-time features "
4471 "via a data response with each being space delimited."
4473 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4474 "via a data response, when available."
4476 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4477 "via a data response."
4480 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
4482 "Changes the passphrase of the secret key required to open the current "
4483 "data file. If the data file is symmetrically encrypted, the error "
4484 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4485 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4486 "this command saving any unwanted changes to the @abbr{XML} document."
4488 "This command is not available to non-invoking clients "
4489 "(@pxref{Access Control})."
4492 new_command("OPEN", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
4493 "OPEN [--lock] <filename>\n"
4494 "Opens @var{filename}. When the @var{filename} is not found on the "
4495 "file-system then a new in-memory document will be created. If the file is "
4496 "found, it is looked for in the file cache and when found no passphrase will "
4497 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4498 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4499 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4500 "@emph{INQUIRE} the client for the passphrase."
4502 "When the @option{--lock} option is passed then the file mutex will be "
4503 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4504 "file had been opened."
4507 new_command("SAVE", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
4508 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<keyid>[,..] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,..] | [--inquire-sign-keyid]]\n"
4509 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4510 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4511 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4512 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4513 "for the passphrase of the secret key used for signing."
4515 "The @option{--inquire-keyparam} option will send an "
4516 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4517 "generating the new keypair. The inquired data is expected to be in "
4518 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4519 "details. Note that when this option is specified a new keypair will be "
4520 "generated reguardless if the file is a new one and that the passphrase for "
4521 "the current file will be required before generating the new keypair. This "
4522 "option is available to non-invoking clients (@pxref{Access Control}) only "
4523 "when the file is a new one."
4525 "You can encrypt the data file to a recipient other than the one that it "
4526 "was encrypted with by passing the @option{--keyid} or "
4527 "@option{--inquire-keyid} option with "
4528 "the key ID of a public encryption key as its argument. Use the "
4529 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4530 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4531 "may also be used to sign the data "
4532 "file with an alternate key by specifying the key ID of a secret key. "
4533 "A passphrase to decrypt the data file "
4534 "will be required if one or more of the original encryption or signing keys "
4535 "are not found in either of these two options' arguments. The original "
4536 "encryption or signing keys will be used when either of these options are "
4539 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4540 "for non-invoking clients "
4541 "(@pxref{Access Control}) when the recipients or signers do not match those "
4542 "that were used when the file was @code{OPEN}'ed."
4544 "The @option{--symmetric} option specifies that a new data file be "
4545 "conventionally encrypted. These types of data files do not use a recipient "
4546 "public key but may be signed by using the @option{--sign-keyid} or "
4547 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4548 "symmtrically encrypted data file, leave the option value empty. Note that "
4549 "you cannot change encryption schemes once a data file has been saved."
4552 new_command("ISCACHED", 1, 0, 0, iscached_command
, _(
4553 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4554 "Determines the file cache status of the specified @var{filename}. "
4555 "The default is to test whether the filename is cached in memory. Passing "
4556 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4557 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4558 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4559 "Both the @option{--agent} and @option{--sign} options require an opened data "
4562 "An @emph{OK} response is returned if the specified @var{filename} is found "
4563 "in the cache. If not found in the cache but exists on the filesystem "
4564 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4567 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4568 "the file exists; it does not need to be opened nor cached. The lock will be "
4569 "released when the client exits or sends the @code{UNLOCK} command "
4573 new_command("CLEARCACHE", 1, 1, 0, clearcache_command
, _(
4574 "CLEARCACHE [<filename>]\n"
4575 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4576 "this will also clear any @command{gpg-agent} cached keys which may cause "
4577 "problems if another data file shares the same keys as @var{filename}."
4580 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command
, _(
4581 "CACHETIMEOUT <filename> <seconds>\n"
4582 "The time in @var{seconds} until @var{filename} will be removed from the "
4583 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4584 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4585 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4589 new_command("LIST", 0, 1, 0, list_command
, _(
4590 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4591 "If no element path is given then a newline separated list of root elements "
4592 "is returned with a data response. If given, then children of the specified "
4593 "element path are returned."
4595 "Each element path "
4596 "returned will have zero or more flags appened to it. These flags are "
4597 "delimited from the element path by a single space character. A flag itself "
4598 "is a single character. Flag @code{P} indicates that access to the element "
4599 "is denied. Flag @code{+} indicates that there are child nodes of "
4600 "the current element path. Flag @code{E} indicates that an element of the "
4601 "element path contained in a @var{target} attribute could not be found. Flag "
4602 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4603 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4604 "then an element path, is the element path of the @var{target} attribute "
4605 "contained in the current element."
4607 "When a specified element path contains an error, beit from the final "
4608 "element in the path or any previous element, the path is still shown but "
4609 "will contain the error flag for the element with the error. Determining "
4610 "the actual element which contains the error is up to the client. This can be "
4611 "done by traversing the final element up to parent elements that contain the "
4614 "The option @option{--recurse} may be used to list the entire element tree "
4615 "for a specified element path or the entire tree for all root elements."
4617 "When the @option{--inquire} option is passed then all remaining non-option "
4618 "arguments are retrieved via a server @emph{INQUIRE}."
4621 new_command("REALPATH", 0, 1, 0, realpath_command
, _(
4622 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4623 "Resolves all @code{target} attributes of the specified element path and "
4624 "returns the result with a data response. @xref{Target Attribute}, for details."
4626 "When the @option{--inquire} option is passed then all remaining non-option "
4627 "arguments are retrieved via a server @emph{INQUIRE}."
4630 new_command("STORE", 0, 1, 0, store_command
, _(
4631 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4632 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4634 "Creates a new element path or modifies the @var{content} of an existing "
4635 "element. If only a single element is specified then a new root element is "
4636 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4637 "set to the final @key{TAB} delimited element. If no @var{content} is "
4638 "specified after the final @key{TAB}, then the content of the existing "
4639 "element will be removed; or will be empty if creating a new element."
4641 "The only restriction of an element name is that it not contain whitespace "
4642 "characters. There is no other whitespace between the @key{TAB} delimited "
4643 "elements. It is recommended that the content of an element be base64 encoded "
4644 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4645 "parsing and @command{pwmd} syntax errors."
4648 new_command("RENAME", 0, 1, 0, rename_command
, _(
4649 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4650 "Renames the specified @var{element} to the new @var{value}. If an element of "
4651 "the same name as the @var{value} already exists it will be overwritten."
4653 "When the @option{--inquire} option is passed then all remaining non-option "
4654 "arguments are retrieved via a server @emph{INQUIRE}."
4657 new_command("COPY", 0, 1, 0, copy_command
, _(
4658 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4659 "Copies the entire element tree starting from the child node of the source "
4660 "element, to the destination element path. If the destination element path "
4661 "does not exist then it will be created; otherwise it is overwritten."
4663 "Note that attributes from the source element are merged into the "
4664 "destination element when the destination element path exists. When an "
4665 "attribute of the same name exists in both the source and destination "
4666 "elements then the destination attribute will be updated to the source "
4669 "When the @option{--inquire} option is passed then all remaining non-option "
4670 "arguments are retrieved via a server @emph{INQUIRE}."
4673 new_command("MOVE", 0, 1, 0, move_command
, _(
4674 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4675 "Moves the source element path to the destination element path. If the "
4676 "destination is not specified then it will be moved to the root node of the "
4677 "document. If the destination is specified and exists then it will be "
4678 "overwritten; otherwise non-existing elements of the destination element "
4679 "path will be created."
4681 "When the @option{--inquire} option is passed then all remaining non-option "
4682 "arguments are retrieved via a server @emph{INQUIRE}."
4685 new_command("DELETE", 0, 1, 0, delete_command
, _(
4686 "DELETE [--inquire] element[<TAB>child[..]]\n"
4687 "Removes the specified element path and all of its children. This may break "
4688 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4689 "refers to this element or any of its children."
4691 "When the @option{--inquire} option is passed then all remaining non-option "
4692 "arguments are retrieved via a server @emph{INQUIRE}."
4695 new_command("GET", 0, 1, 0, get_command
, _(
4696 "GET [--inquire] element[<TAB>child[..]]\n"
4697 "Retrieves the content of the specified element. The content is returned "
4698 "with a data response."
4700 "When the @option{--inquire} option is passed then all remaining non-option "
4701 "arguments are retrieved via a server @emph{INQUIRE}."
4704 new_command("ATTR", 0, 1, 0, attr_command
, _(
4705 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4707 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4708 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4709 "element. When no @var{value} is specified any existing value will be removed."
4711 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4712 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4713 "or @code{target} an error is returned. Use the @command{DELETE} command "
4714 "(@pxref{DELETE}) instead."
4716 "@item ATTR LIST element[<TAB>child[..]]\n"
4717 " Retrieves a newline separated list of attributes names and values "
4718 "from the specified element. Each attribute name and value is space delimited."
4720 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4721 " Retrieves the value of an @var{attribute} from an element."
4724 "When the @option{--inquire} option is passed then all remaining non-option "
4725 "arguments are retrieved via a server @emph{INQUIRE}."
4727 "@xref{Target Attribute}, for details about this special attribute and also "
4728 "@pxref{Other Attributes} for other attributes that are handled specially "
4729 "by @command{pwmd}."
4732 new_command("XPATH", 0, 1, 0, xpath_command
, _(
4733 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4734 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4735 "specified it is assumed the expression is a request to return a result. "
4736 "Otherwise, the result is set to the @var{value} argument and the document is "
4737 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4738 "is assumed to be empty and the document is updated. For example:"
4741 "XPATH //element[@@_name='password']@key{TAB}\n"
4744 "would clear the content of all @var{password} elements in the data file "
4745 "while leaving off the trailing @key{TAB} would return all @var{password} "
4746 "elements in @abbr{XML} format."
4748 "When the @option{--inquire} option is passed then all remaining non-option "
4749 "arguments are retrieved via a server @emph{INQUIRE}."
4751 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4752 "expression syntax."
4755 new_command("XPATHATTR", 0, 1, 0, xpathattr_command
, _(
4756 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4757 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4758 "attributes and does not return a result. For the @var{SET} operation the "
4759 "@var{value} is optional but the field is required. If not specified then "
4760 "the attribute value will be empty. For example:"
4763 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4766 "would create a @var{password} attribute for each @var{password} element "
4767 "found in the document. The attribute value will be empty but still exist."
4769 "When the @option{--inquire} option is passed then all remaining non-option "
4770 "arguments are retrieved via a server @emph{INQUIRE}."
4772 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4773 "expression syntax."
4776 new_command("IMPORT", 0, 1, 0, import_command
, _(
4777 "IMPORT [--root=element[<TAB>child[..]]]\n"
4778 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4780 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4781 "argument is raw @abbr{XML} data. The content is created as a child of "
4782 "the element path specified with the @option{--root} option or at the "
4783 "document root when not specified. Existing elements of the same name will "
4786 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4790 new_command("DUMP", 0, 1, 0, dump_command
, _(
4792 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4793 "dumping a specific node."
4796 new_command("LOCK", 0, 0, 0, lock_command
, _(
4798 "Locks the mutex associated with the opened file. This prevents other clients "
4799 "from sending commands to the same opened file until the client "
4800 "that sent this command either disconnects or sends the @code{UNLOCK} "
4801 "command. @xref{UNLOCK}."
4804 new_command("UNLOCK", 1, 0, 0, unlock_command
, _(
4806 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4807 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4808 "@pxref{ISCACHED})."
4811 new_command("GETCONFIG", 1, 1, 0, getconfig_command
, _(
4812 "GETCONFIG [filename] <parameter>\n"
4813 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4814 "data response. If no file has been opened then the value for @var{filename} "
4815 "or the default from the @var{global} section will be returned. If a file "
4816 "has been opened and no @var{filename} is specified, the value previously "
4817 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4820 new_command("OPTION", 1, 1, 0, option_command
, _(
4821 "OPTION <NAME>=[<VALUE>]\n"
4822 "Sets a client option @var{name} to @var{value}. The value for an option is "
4823 "kept for the duration of the connection with the exception of the "
4824 "@command{pinentry} options which are defaults for all future connections "
4825 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4828 "@item DISABLE-PINENTRY\n"
4829 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4830 "server inquire is sent to the client to obtain the passphrase. This option "
4831 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4832 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4833 "to use a @command{pinentry}."
4836 "Set or unset the X11 display to use when prompting for a passphrase."
4839 "Set the terminal device path to use when prompting for a passphrase."
4842 "Set the terminal type for use with @option{TTYNAME}."
4845 "Associates the thread ID of the connection with the specified textual "
4846 "representation. Useful for debugging log messages. May not contain whitespace."
4848 "@item LOCK-TIMEOUT\n"
4849 "When not @code{0}, the duration in tenths of a second to wait for the file "
4850 "mutex which has been locked by another thread to be released before returning "
4851 "an error. When @code{-1} the error will be returned immediately."
4855 new_command("LS", 1, 1, 0, ls_command
, _(
4857 "Returns a newline separated list of data files stored in the data directory "
4858 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4861 new_command("RESET", 1, 1, 0, NULL
, _(
4863 "Closes the currently opened file but keeps any previously set client options "
4867 new_command("NOP", 1, 1, 0, NULL
, _(
4869 "Does nothing. Always returns successfully."
4872 /* !END-HELP-TEXT! */
4873 new_command ("CANCEL", 1, 1, 0, NULL
, NULL
);
4874 new_command ("END", 1, 1, 0, NULL
, NULL
);
4875 new_command ("BYE", 1, 1, 0, NULL
, NULL
);
4878 for (i
= 0; command_table
[i
]; i
++);
4879 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4884 register_commands (assuan_context_t ctx
)
4888 for (; command_table
[i
]; i
++)
4890 if (!command_table
[i
]->handler
)
4893 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4894 command_table
[i
]->handler
,
4895 command_table
[i
]->help
);
4900 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4904 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4908 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4912 return assuan_register_post_cmd_notify (ctx
, command_finalize
);