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
->filename
, 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
->filename
,
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 reset_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;
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
->filename
);
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
;
400 assuan_peercred_t peer
;
401 struct cache_data_s
*cdata
= NULL
;
403 unsigned char *crc
= NULL
;
405 struct argv_s
*args
[] = {
406 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
410 rc
= parse_options (&line
, args
, client
, 1);
412 return send_error (ctx
, rc
);
414 req
= str_split (line
, " ", 2);
416 return send_error (ctx
, GPG_ERR_SYNTAX
);
418 rc
= do_validate_peer (ctx
, req
[0], &peer
);
419 if (rc
== GPG_ERR_FORBIDDEN
)
420 rc
= peer_is_invoker (client
);
425 return send_error (ctx
, rc
);
429 if (!valid_filename (filename
))
432 return send_error (ctx
, GPG_ERR_INV_VALUE
);
435 pthread_cleanup_push ((void *)req_free
, req
);
436 /* This client may have locked a different file with ISCACHED --lock than
437 * the current filename. This will remove that lock. */
438 same_file
= client
->filename
&& !strcmp (filename
, client
->filename
);
439 if (client
->flags
& FLAG_OPEN
||
440 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
442 uint32_t opts
= client
->opts
;
443 uint32_t flags
= client
->flags
;
446 client
->flags
|= FLAG_KEEP_LOCK
;
447 else if (client
->flags
& FLAG_NEW
)
448 cache_clear (NULL
, client
->filename
, 0);
450 reset_client (client
);
452 client
->flags
|= flags
;
453 client
->flags
&= ~(FLAG_LOCK_CMD
);
455 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
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
->filename
);
501 client
->flags
|= FLAG_NEW
;
502 // data file disappeared. clear the cache entry.
503 cache_clear (NULL
, client
->filename
, 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 (NULL
, client
->filename
, 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 (NULL
, client
->filename
, 1);
591 int timeout
= config_get_integer (client
->filename
,
594 cache_free_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
->filename
, 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
->filename
, 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 (NULL
, client
->filename
, 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.
666 permitted_to_save (struct client_s
*client
, const char **keys
,
672 if ((client
->flags
& FLAG_NEW
) || (client
->opts
& OPT_SYMMETRIC
))
675 rc
= peer_is_invoker (client
);
680 if ((!value
&& !keys
) || ((value
&& !*value
) && (keys
&& !*keys
)))
683 for (v
= value
; v
&& *v
; v
++)
685 const char **k
, *pv
= *v
;
688 if (*pv
== '0' && *(pv
+1) == 'x')
691 for (k
= keys
; k
&& *k
; k
++)
695 if (*pk
== '0' && *(pk
+1) == 'x')
698 rc
= !strcmp (pv
, pk
) ? 0 : GPG_ERR_FORBIDDEN
;
700 rc
= !strcmp (pv
, *k
) ? 0 : GPG_ERR_FORBIDDEN
;
710 return GPG_ERR_FORBIDDEN
;
716 /* Requires that the keyid be a fingerprint in 16 byte form. */
718 parse_save_opt_keyid_common (struct client_s
*client
, const char **list
,
719 const char *value
, char ***dst
)
726 keys
= str_split (value
, ",", 0);
728 return GPG_ERR_ENOMEM
;
731 rc
= crypto_keyid_to_16b (keys
);
733 rc
= permitted_to_save (client
, list
, (const char **)keys
);
744 parse_save_opt_keyid (void *data
, void *value
)
746 struct client_s
*client
= data
;
747 const char *str
= value
;
751 rc
= parse_save_opt_keyid_common (client
,
752 (const char **)client
->crypto
->pubkey
,
757 client
->crypto
->save
.pubkey
= dst
;
762 parse_save_opt_sign_keyid (void *data
, void *value
)
764 struct client_s
*client
= data
;
765 const char *str
= value
;
769 rc
= parse_save_opt_keyid_common (client
,
770 (const char **)client
->crypto
->sigkey
,
776 client
->opts
|= OPT_NO_SIGNER
;
778 client
->crypto
->save
.sigkey
= dst
;
783 parse_save_opt_inquire (struct client_s
*client
, uint32_t opt
)
785 if (opt
== OPT_INQUIRE
&& !(client
->flags
& FLAG_NEW
))
787 gpg_error_t rc
= peer_is_invoker (client
);
798 parse_save_opt_inquire_keyparam (void *data
, void *value
)
800 return parse_save_opt_inquire (data
, OPT_INQUIRE
);
804 parse_save_opt_inquire_keyid (void *data
, void *value
)
806 return parse_save_opt_inquire (data
, OPT_INQUIRE_KEYID
);
810 parse_save_opt_inquire_sign_keyid (void *data
, void *value
)
812 return parse_save_opt_inquire (data
, OPT_INQUIRE_SIGN_KEYID
);
816 parse_save_opt_symmetric (void *data
, void *value
)
818 struct client_s
*client
= data
;
820 client
->opts
|= OPT_SYMMETRIC
;
824 /* Tests that the keys in new_keys are also in old_keys. */
826 compare_keys (char **new_keys
, char **old_keys
)
830 if (!old_keys
|| !*old_keys
)
833 crypto_keyid_to_16b (new_keys
);
835 for (o
= old_keys
; *o
; o
++)
839 for (n
= new_keys
; *n
; n
++)
841 if (!strcmp (*n
, *o
))
846 return GPG_ERR_NOT_FOUND
;
853 inquire_keyid (struct client_s
*client
, uint32_t opt
)
856 unsigned char *result
= NULL
;
862 if (opt
== OPT_INQUIRE_KEYID
)
865 orig
= &client
->crypto
->pubkey
;
866 save
= &client
->crypto
->save
.pubkey
;
870 s
= "INQUIRE_SIGN_KEYID";
871 orig
= &client
->crypto
->sigkey
;
872 save
= &client
->crypto
->save
.sigkey
;
875 rc
= assuan_inquire (client
->ctx
, s
, &result
, &len
, 0);
880 rc
= parse_save_opt_keyid_common (client
, (const char **)*orig
,
881 (char *)result
, &dst
);
884 if (!dst
&& opt
== OPT_INQUIRE_SIGN_KEYID
885 && client
->opts
& OPT_SYMMETRIC
)
886 client
->opts
|= OPT_NO_SIGNER
;
896 /* The caching test of gpg-agent is both a blessing and a curse. When a key
897 * lookup in its cache fails it tries the last successful key to be unlocked.
898 * This prevents pwmd from successfully clearing a signing key since the
899 * previous key, which more than likely belongs to the same keypair as the
900 * encryption/decryption key, will have the passphrase cached and therefore the
901 * signing key also cached. So we need to clear both the signing and encryption
902 * keys to get the effect of requiring a passphrase when generating a new
903 * keypair for an existing data file, or when the "require_save_key"
904 * configuration parameter is set. The "require_save_key" parameter is mostly a
905 * failsafe of the gpg-agent option --ignore-cache-for-signing since
906 * some/most/all users may fail to set it.
909 save_command (assuan_context_t ctx
, char *line
)
911 struct client_s
*client
= assuan_get_pointer (ctx
);
912 struct cache_data_s
*cdata
= NULL
;
913 gpg_error_t rc
= 0, cached
= 0;
916 struct argv_s
*args
[] = {
917 &(struct argv_s
) {"keyid", OPTION_TYPE_ARG
, parse_save_opt_keyid
},
918 &(struct argv_s
) {"sign-keyid", OPTION_TYPE_OPTARG
,
919 parse_save_opt_sign_keyid
},
920 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
921 parse_save_opt_inquire_keyparam
},
922 &(struct argv_s
) {"inquire-keyid", OPTION_TYPE_NOARG
,
923 parse_save_opt_inquire_keyid
},
924 &(struct argv_s
) {"inquire-sign-keyid", OPTION_TYPE_NOARG
,
925 parse_save_opt_inquire_sign_keyid
},
926 &(struct argv_s
) {"symmetric", OPTION_TYPE_NOARG
,
927 parse_save_opt_symmetric
},
931 crypto_free_save (&client
->crypto
->save
);
933 if (!(client
->flags
& FLAG_NEW
))
935 rc
= crypto_is_symmetric (client
->filename
);
936 if (!rc
|| rc
== GPG_ERR_BAD_DATA
)
939 client
->opts
|= OPT_SYMMETRIC
;
946 return send_error (ctx
, rc
);
948 rc
= parse_options (&line
, args
, client
, 0);
950 return send_error (ctx
, rc
);
952 if (config_get_boolean (client
->filename
, "require_save_key"))
953 client
->opts
|= OPT_ASK
;
955 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
956 return send_error (ctx
, gpg_error_from_errno (errno
));
958 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
960 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
961 return send_error (ctx
, GPG_ERR_ENOANO
);
965 cached
= rc
= cache_iscached (client
->filename
, &defer
, 0, 1);
967 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
969 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
973 return send_error (ctx
, rc
);
975 /* Specifying both a recipient and symmetric encryption is an error. */
976 if (client
->opts
& OPT_INQUIRE_KEYID
&& client
->opts
& OPT_SYMMETRIC
)
978 return send_error (ctx
, GPG_ERR_CONFLICT
);
980 else if (client
->opts
& OPT_INQUIRE
&& client
->opts
& OPT_SYMMETRIC
)
982 return send_error (ctx
, GPG_ERR_CONFLICT
);
984 /* Existing file with a recipient and wanting symmetric is an error. */
985 else if (client
->opts
& OPT_SYMMETRIC
&& client
->crypto
->save
.pubkey
)
987 return send_error (ctx
, GPG_ERR_CONFLICT
);
989 else if (((client
->opts
& OPT_INQUIRE_KEYID
)
990 || (client
->opts
& OPT_INQUIRE_SIGN_KEYID
)))
992 if (client
->opts
& OPT_INQUIRE
)
993 return send_error (ctx
, GPG_ERR_CONFLICT
);
995 if (client
->opts
& OPT_INQUIRE_KEYID
)
996 rc
= inquire_keyid (client
, OPT_INQUIRE_KEYID
);
998 if (!rc
&& (client
->opts
& OPT_INQUIRE_SIGN_KEYID
))
999 rc
= inquire_keyid (client
, OPT_INQUIRE_SIGN_KEYID
);
1001 else if ((client
->crypto
->save
.pubkey
|| client
->crypto
->save
.sigkey
)
1002 && client
->opts
& OPT_INQUIRE
)
1003 return send_error (ctx
, GPG_ERR_CONFLICT
);
1007 client
->crypto
->keyfile
= config_get_string (client
->filename
,
1009 rc
= crypto_init_ctx (client
->crypto
, (client
->flags
& FLAG_NO_PINENTRY
),
1010 client
->crypto
->keyfile
);
1013 if (!rc
&& (client
->opts
& OPT_INQUIRE
))
1015 /* Require a passphrase when generating a new key pair for an existing
1018 if (!(client
->flags
& FLAG_NEW
))
1020 rc
= cache_clear_agent_keys (client
->filename
, 1, 1);
1023 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1024 client
->opts
&= ~OPT_ASK
;
1030 unsigned char *params
= NULL
;
1033 rc
= assuan_inquire (client
->ctx
, "KEYPARAM", ¶ms
, &len
, 0);
1036 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1037 pthread_cleanup_push ((void *)xfree
, params
);
1038 rc
= crypto_genkey (client
, client
->crypto
, params
);
1039 pthread_cleanup_pop (1);
1043 else if (!rc
&& (client
->flags
& FLAG_NEW
))
1045 if (!client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
1046 && !(client
->opts
& OPT_SYMMETRIC
))
1048 char *params
= crypto_default_key_params ();
1051 rc
= GPG_ERR_ENOMEM
;
1055 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1056 pthread_cleanup_push ((void *)xfree
, params
);
1057 rc
= crypto_genkey (client
, client
->crypto
,
1058 (unsigned char *)params
);
1059 pthread_cleanup_pop (1);
1064 if (!client
->crypto
->save
.pubkey
&& client
->crypto
->save
.sigkey
1065 && !(client
->opts
& OPT_SYMMETRIC
))
1066 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->save
.sigkey
);
1068 if (client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
)
1069 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->save
.pubkey
);
1074 cdata
= cache_get_data (client
->filename
);
1077 if (client
->crypto
->save
.pubkey
)
1078 rc
= compare_keys (client
->crypto
->save
.pubkey
, cdata
->pubkey
);
1080 /* Always allow a signer for symmetric data files. */
1081 if (!rc
&& client
->crypto
->save
.sigkey
1082 && !(client
->opts
& OPT_SYMMETRIC
))
1083 rc
= compare_keys (client
->crypto
->save
.sigkey
, cdata
->sigkey
);
1085 /* Prevent saving to a recipient who is not in the original recipient
1086 * list without a passphrase. */
1087 if (rc
|| (client
->crypto
->sigkey
&& client
->opts
& OPT_NO_SIGNER
))
1088 client
->opts
|= OPT_ASK
;
1090 if (rc
== GPG_ERR_NOT_FOUND
)
1092 rc
= peer_is_invoker (client
);
1093 if (rc
== GPG_ERR_EACCES
)
1094 rc
= GPG_ERR_FORBIDDEN
;
1097 if (!client
->crypto
->save
.pubkey
1098 && !(client
->opts
& OPT_SYMMETRIC
))
1099 client
->crypto
->save
.pubkey
= strv_dup (cdata
->pubkey
);
1101 if (!rc
&& !client
->crypto
->save
.sigkey
&& cdata
->sigkey
1102 && !(client
->opts
& OPT_NO_SIGNER
))
1103 client
->crypto
->save
.sigkey
= strv_dup (cdata
->sigkey
);
1104 else if (!rc
&& !client
->crypto
->save
.sigkey
1105 && (client
->opts
& OPT_NO_SIGNER
)
1106 && !(client
->opts
& OPT_SYMMETRIC
))
1107 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
1111 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->pubkey
);
1112 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->sigkey
);
1116 if (!rc
&& !(client
->flags
& FLAG_NEW
))
1118 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1119 if (client
->opts
& OPT_SYMMETRIC
)
1120 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
1122 if (client
->opts
& OPT_ASK
)
1124 rc
= cache_clear_agent_keys (client
->filename
, 1, 1);
1126 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1130 if (!rc
&& client
->opts
& OPT_SYMMETRIC
)
1131 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
1134 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1140 xmlDocDumpFormatMemory (client
->doc
, &client
->crypto
->plaintext
, &size
,
1143 client
->crypto
->plaintext_size
= (size_t) size
;
1145 rc
= GPG_ERR_ENOMEM
;
1149 client
->crypto
->flags
|= (client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_SYMMETRIC
: 0;
1150 client
->crypto
->flags
|= (client
->flags
& FLAG_NEW
) ? CRYPTO_FLAG_NEWFILE
: 0;
1151 client
->crypto
->flags
|= !(client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_PASSWD_SIGN
: 0;
1152 rc
= crypto_encrypt (client
, client
->crypto
);
1153 client
->crypto
->flags
&= ~CRYPTO_FLAG_SYMMETRIC
;
1158 rc
= crypto_write_file (client
->crypto
);
1161 if (!cached
) // no error
1162 cdata
= cache_get_data (client
->filename
);
1167 rc
= cache_encrypt (client
->crypto
);
1170 cache_clear (NULL
, client
->filename
, 1);
1171 strv_free (client
->crypto
->pubkey
);
1172 client
->crypto
->pubkey
= strv_dup (client
->crypto
->save
.pubkey
);
1173 strv_free (client
->crypto
->sigkey
);
1174 client
->crypto
->sigkey
= strv_dup (client
->crypto
->save
.sigkey
);
1175 client
->flags
&= ~(FLAG_NEW
);
1181 int timeout
= config_get_integer (client
->filename
,
1184 cache_free_data_once (cdata
);
1185 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1186 cdata
->doc
= client
->crypto
->plaintext
;
1187 client
->crypto
->plaintext
= NULL
;
1188 cdata
->size
= client
->crypto
->plaintext_size
;
1189 client
->crypto
->plaintext_size
= 0;
1190 cdata
->pubkey
= client
->crypto
->save
.pubkey
;
1191 client
->crypto
->save
.pubkey
= NULL
;
1192 cdata
->sigkey
= client
->crypto
->save
.sigkey
;
1193 client
->crypto
->save
.sigkey
= NULL
;
1195 /* Update in case the cache entry expires the next SAVE may not
1196 * have any known keys. */
1197 strv_free (client
->crypto
->pubkey
);
1198 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
1199 strv_free (client
->crypto
->sigkey
);
1200 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
1202 if (!cached
) // no error and wont increment refcount
1203 rc
= cache_set_data (client
->filename
, cdata
);
1205 rc
= cache_add_file (client
->filename
, cdata
, timeout
);
1207 if (!rc
&& (cached
|| (client
->flags
& FLAG_NEW
)))
1208 send_status_all (STATUS_CACHE
, NULL
);
1212 rc
= update_checksum (client
);
1213 client
->flags
&= ~(FLAG_NEW
);
1219 crypto_free_non_keys (client
->crypto
);
1220 return send_error (ctx
, rc
);
1224 do_delete (assuan_context_t ctx
, char *line
)
1226 struct client_s
*client
= assuan_get_pointer (ctx
);
1227 struct xml_request_s
*req
;
1231 rc
= xml_new_request (client
, line
, XML_CMD_DELETE
, &req
);
1235 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1236 xml_free_request (req
);
1240 rc
= xml_is_element_owner (client
, n
);
1242 rc
= xml_unlink_node (client
, n
);
1248 delete_command (assuan_context_t ctx
, char *line
)
1250 struct client_s
*client
= assuan_get_pointer (ctx
);
1252 struct argv_s
*args
[] = {
1253 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1257 rc
= parse_options (&line
, args
, client
, 1);
1258 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1259 rc
= GPG_ERR_SYNTAX
;
1261 return send_error (ctx
, rc
);
1263 if (client
->opts
& OPT_INQUIRE
)
1265 unsigned char *result
;
1268 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1270 return send_error (ctx
, rc
);
1272 pthread_cleanup_push ((void *)xfree
, result
);
1273 rc
= do_delete (ctx
, (char *)result
);
1274 pthread_cleanup_pop (1);
1277 rc
= do_delete (ctx
, line
);
1279 return send_error (ctx
, rc
);
1283 update_element_expirey (struct client_s
*client
, xmlNodePtr n
)
1286 xmlChar
*expire
, *incr
;
1288 time_t e
, e_orig
, i
;
1289 time_t now
= time (NULL
);
1291 expire
= xml_attribute_value (n
, (xmlChar
*)"expire");
1296 e
= strtoul ((char *)expire
, &p
, 10);
1297 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1300 rc
= GPG_ERR_ERANGE
;
1305 incr
= xml_attribute_value (n
, (xmlChar
*)"expire_increment");
1309 i
= strtoul ((char *)incr
, &p
, 10);
1310 if (errno
|| *p
|| i
== ULONG_MAX
|| *incr
== '-')
1313 rc
= GPG_ERR_ERANGE
;
1320 p
= str_asprintf ("%lu", e
);
1323 rc
= GPG_ERR_ENOMEM
;
1327 rc
= xml_add_attribute (client
, n
, "expire", p
);
1330 rc
= send_status (client
->ctx
, STATUS_EXPIRE
, "%lu %lu", e_orig
, e
);
1337 store_command (assuan_context_t ctx
, char *line
)
1339 struct client_s
*client
= assuan_get_pointer (ctx
);
1342 unsigned char *result
;
1343 xmlNodePtr n
, parent
;
1345 char *content
= NULL
;
1346 struct xml_request_s
*req
;
1349 return send_error (ctx
, GPG_ERR_SYNTAX
);
1351 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1353 return send_error (ctx
, rc
);
1355 rc
= xml_new_request (client
, (char *)result
, XML_CMD_STORE
, &req
);
1358 return send_error (ctx
, rc
);
1360 /* Prevent passing the element content around to save some memory. */
1361 len
= strv_length (req
->args
);
1362 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1363 if (*(req
->args
+1) && !xml_valid_element_path (req
->args
, has_content
))
1365 xml_free_request (req
);
1366 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1369 if (has_content
|| !*req
->args
[len
-1])
1372 content
= req
->args
[len
-1];
1373 req
->args
[len
-1] = NULL
;
1376 if (strv_length (req
->args
) > 1)
1378 rc
= xml_check_recursion (client
, req
);
1379 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1383 parent
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1387 rc
= xml_is_element_owner (client
, parent
);
1390 n
= xml_find_text_node (parent
->children
);
1392 xmlNodeSetContent (n
, (xmlChar
*) content
);
1394 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1396 (void)xml_update_element_mtime (client
, parent
);
1397 (void)update_element_expirey (client
, parent
);
1403 xml_free_request (req
);
1404 return send_error (ctx
, rc
);
1408 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1410 struct client_s
*client
= assuan_get_pointer (ctx
);
1414 int progress
= config_get_integer ("global", "xfer_progress");
1417 if (!(client
->flags
& FLAG_LOCK_CMD
))
1418 rc
= unlock_file_mutex (client
, 0);
1419 if (rc
&& rc
!= GPG_ERR_NOT_LOCKED
)
1423 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1424 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1425 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1433 if (sent
+ to_send
> total
)
1434 to_send
= total
- sent
;
1436 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1437 flush
? 0 : to_send
);
1440 sent
+= flush
? 0 : to_send
;
1442 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1443 (sent
== total
&& flush
))
1444 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1446 if (!flush
&& !rc
&& sent
== total
)
1453 while (!rc
&& sent
< total
);
1459 do_get (assuan_context_t ctx
, char *line
)
1461 struct client_s
*client
= assuan_get_pointer (ctx
);
1463 struct xml_request_s
*req
;
1466 rc
= xml_new_request (client
, line
, XML_CMD_NONE
, &req
);
1470 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1473 if (n
&& n
->children
)
1477 n
= xml_find_text_node (n
->children
);
1478 if (!n
|| !n
->content
|| !*n
->content
)
1479 rc
= GPG_ERR_NO_DATA
;
1482 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1485 xmlChar
*expire
= xml_attribute_value (tmp
,
1486 (xmlChar
*)"expire");
1489 time_t now
= time (NULL
);
1494 e
= strtoul ((char *)expire
, &p
, 10);
1495 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1496 log_write (_("invalid expire attribute value: %s"),
1499 rc
= send_status (ctx
, STATUS_EXPIRE
, "%lu 0", e
);
1507 rc
= GPG_ERR_NO_DATA
;
1510 xml_free_request (req
);
1515 get_command (assuan_context_t ctx
, char *line
)
1517 struct client_s
*client
= assuan_get_pointer (ctx
);
1519 struct argv_s
*args
[] = {
1520 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1524 rc
= parse_options (&line
, args
, client
, 1);
1525 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1526 rc
= GPG_ERR_SYNTAX
;
1528 return send_error (ctx
, rc
);
1530 if (client
->opts
& OPT_INQUIRE
)
1532 unsigned char *result
;
1535 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1537 return send_error (ctx
, rc
);
1539 pthread_cleanup_push ((void *)xfree
, result
);
1540 rc
= do_get (ctx
, (char *)result
);
1541 pthread_cleanup_pop (1);
1544 rc
= do_get (ctx
, line
);
1546 return send_error (ctx
, rc
);
1549 static void list_command_free1 (void *arg
);
1551 realpath_command (assuan_context_t ctx
, char *line
)
1554 char **realpath
= NULL
;
1555 struct client_s
*client
= assuan_get_pointer (ctx
);
1556 struct argv_s
*args
[] = {
1557 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1561 rc
= parse_options (&line
, args
, client
, 1);
1562 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1563 rc
= GPG_ERR_SYNTAX
;
1565 return send_error (ctx
, rc
);
1567 if (client
->opts
& OPT_INQUIRE
)
1569 unsigned char *result
;
1572 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1574 return send_error (ctx
, rc
);
1576 pthread_cleanup_push ((void *)xfree
, result
);
1577 (void)xml_resolve_path (client
, client
->doc
, result
, &realpath
, &rc
);
1578 pthread_cleanup_pop (1);
1581 (void)xml_resolve_path (client
, client
->doc
, (unsigned char *)line
,
1586 char *tmp
= strv_join ("\t", realpath
);
1588 strv_free (realpath
);
1590 return send_error (ctx
, GPG_ERR_ENOMEM
);
1592 pthread_cleanup_push ((void *)xfree
, tmp
);
1593 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
1594 pthread_cleanup_pop (1);
1597 return send_error (ctx
, rc
);
1601 list_command_free1 (void *arg
)
1604 string_free ((struct string_s
*) arg
, 1);
1608 parse_list_opt_recurse (void *data
, void *value
)
1610 struct client_s
*client
= data
;
1612 client
->opts
|= OPT_LIST_RECURSE
;
1617 parse_opt_verbose (void *data
, void *value
)
1619 struct client_s
*client
= data
;
1621 client
->opts
|= OPT_VERBOSE
;
1626 list_path_once (struct client_s
*client
, char *line
,
1627 struct element_list_s
*elements
, struct string_s
*result
)
1631 rc
= xml_create_path_list (client
, client
->doc
, NULL
, elements
, line
, 0);
1632 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
)
1637 int total
= slist_length (elements
->list
);
1643 for (i
= 0; i
< total
; i
++)
1645 char *tmp
= slist_nth_data (elements
->list
, i
);
1647 string_append_printf (result
, "%s%s", tmp
,
1648 i
+ 1 == total
? "" : "\n");
1652 rc
= GPG_ERR_NO_DATA
;
1659 do_list (assuan_context_t ctx
, char *line
)
1661 struct client_s
*client
= assuan_get_pointer (ctx
);
1662 gpg_error_t rc
= GPG_ERR_NO_DATA
;
1663 struct element_list_s
*elements
= NULL
;
1665 if (!line
|| !*line
)
1667 struct string_s
*str
= string_new (NULL
);
1671 return GPG_ERR_ENOMEM
;
1673 pthread_cleanup_push ((void *)list_command_free1
, str
);
1674 n
= xmlDocGetRootElement (client
->doc
);
1675 for (n
= n
->children
; n
; n
= n
->next
)
1679 if (n
->type
!= XML_ELEMENT_NODE
)
1682 name
= xmlGetProp (n
, (xmlChar
*)"_name");
1685 rc
= GPG_ERR_ENOMEM
;
1689 elements
= xcalloc (1, sizeof (struct element_list_s
));
1693 rc
= GPG_ERR_ENOMEM
;
1697 elements
->prefix
= string_new (NULL
);
1698 if (!elements
->prefix
)
1701 xml_free_element_list (elements
);
1702 rc
= GPG_ERR_ENOMEM
;
1706 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1707 elements
->root_only
= !elements
->recurse
;
1708 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1709 rc
= list_path_once (client
, (char *)name
, elements
, str
);
1711 pthread_cleanup_pop (1);
1716 string_append (str
, "\n");
1720 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1722 pthread_cleanup_pop (1);
1726 elements
= xcalloc (1, sizeof (struct element_list_s
));
1728 return GPG_ERR_ENOMEM
;
1730 elements
->prefix
= string_new (NULL
);
1731 if (!elements
->prefix
)
1733 xml_free_element_list (elements
);
1734 return GPG_ERR_ENOMEM
;
1737 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1738 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1739 struct string_s
*str
= string_new (NULL
);
1740 pthread_cleanup_push ((void *)list_command_free1
, str
);
1741 rc
= list_path_once (client
, line
, elements
, str
);
1743 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1745 pthread_cleanup_pop (1);
1746 pthread_cleanup_pop (1);
1751 list_command (assuan_context_t ctx
, char *line
)
1753 struct client_s
*client
= assuan_get_pointer (ctx
);
1755 struct argv_s
*args
[] = {
1756 &(struct argv_s
) {"recurse", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1757 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1758 /* These are deprecated but kept for backward compatibility with older
1760 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, NULL
},
1761 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
, NULL
},
1762 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1763 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
, NULL
},
1767 if (disable_list_and_dump
== 1)
1768 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1770 rc
= parse_options (&line
, args
, client
, 1);
1771 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1772 rc
= GPG_ERR_SYNTAX
;
1774 return send_error (ctx
, rc
);
1776 if (client
->opts
& OPT_INQUIRE
)
1778 unsigned char *result
;
1781 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1783 return send_error (ctx
, rc
);
1785 pthread_cleanup_push ((void *)xfree
, result
);
1786 rc
= do_list (ctx
, (char *)result
);
1787 pthread_cleanup_pop (1);
1790 rc
= do_list (ctx
, line
);
1792 return send_error (ctx
, rc
);
1795 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1796 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1798 * args[0] - element path
1801 attribute_list (assuan_context_t ctx
, char **args
)
1803 struct client_s
*client
= assuan_get_pointer (ctx
);
1804 char **attrlist
= NULL
;
1808 xmlNodePtr n
, an
, r
;
1811 struct xml_request_s
*req
;
1813 if (!args
|| !args
[0] || !*args
[0])
1814 return GPG_ERR_SYNTAX
;
1816 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_LIST
, &req
);
1820 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1821 xml_free_request (req
);
1826 for (a
= n
->properties
; a
; a
= a
->next
)
1830 if (target
&& xml_reserved_attribute ((char *)a
->name
))
1833 pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *));
1837 strv_free (attrlist
);
1839 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1840 pwmd_strerror (GPG_ERR_ENOMEM
));
1841 return GPG_ERR_ENOMEM
;
1846 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
, an
&& an
->content
1847 ? (char *)an
->content
: "");
1851 strv_free (attrlist
);
1852 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1853 pwmd_strerror (GPG_ERR_ENOMEM
));
1854 return GPG_ERR_ENOMEM
;
1857 attrlist
[++i
] = NULL
;
1861 return GPG_ERR_NO_DATA
;
1865 r
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[0], NULL
, &rc
);
1866 if (!RESUMABLE_ERROR (rc
))
1868 strv_free (attrlist
);
1871 else if (!rc
&& r
!= n
)
1881 line
= strv_join ("\n", attrlist
);
1884 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1885 pwmd_strerror (GPG_ERR_ENOMEM
));
1886 strv_free (attrlist
);
1887 return GPG_ERR_ENOMEM
;
1890 pthread_cleanup_push ((void *)xfree
, line
);
1891 pthread_cleanup_push ((void *)req_free
, attrlist
);
1892 rc
= xfer_data (ctx
, line
, strlen (line
));
1893 pthread_cleanup_pop (1);
1894 pthread_cleanup_pop (1);
1899 * args[0] - attribute
1900 * args[1] - element path
1903 attribute_delete (struct client_s
*client
, char **args
)
1908 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
1909 return GPG_ERR_SYNTAX
;
1911 if (!strcmp (args
[0], "_name") || !strcmp (args
[0], "target"))
1912 return GPG_ERR_INV_ATTR
;
1914 if (!xml_reserved_attribute (args
[0]))
1915 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
1918 struct xml_request_s
*req
;
1920 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
1924 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1925 xml_free_request (req
);
1929 rc
= xml_is_element_owner (client
, n
);
1932 rc
= xml_delete_attribute (client
, n
, (xmlChar
*) args
[0]);
1938 * Creates the "target" attribute. When other commands encounter an element
1939 * with this attribute they follow the "target" after appending remaining
1940 * elements from the original element path to the target. The exception is the
1941 * ATTR command that operates on the element itself (for reserved attribute
1942 * names) and not the target.
1944 * If the source element path doesn't exist when using 'ATTR SET target', it is
1945 * created, but the destination element path must exist. This is simliar to the
1946 * ls(1) command: ln -s src dst.
1948 * args[0] - source element path
1949 * args[1] - destination element path
1952 set_target_attribute (struct client_s
*client
, char **args
)
1954 struct xml_request_s
*req
= NULL
;
1959 if (!args
|| !args
[0] || !args
[1])
1960 return GPG_ERR_SYNTAX
;
1962 src
= str_split (args
[0], "\t", 0);
1964 return GPG_ERR_SYNTAX
;
1966 if (!xml_valid_element_path (src
, 0))
1969 return GPG_ERR_INV_VALUE
;
1972 dst
= str_split (args
[1], "\t", 0);
1976 return GPG_ERR_SYNTAX
;
1979 // Be sure the destination element path exists. */
1980 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
1984 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1985 if (rc
&& rc
!= GPG_ERR_EACCES
)
1988 xml_free_request (req
);
1991 if (!strcmp (args
[0], args
[1]))
1993 rc
= GPG_ERR_EEXIST
;
1997 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_TARGET
, &req
);
2001 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2002 if (rc
&& rc
!= GPG_ERR_EACCES
)
2007 rc
= xml_add_attribute (client
, n
, "target", args
[1]);
2009 rc
= xml_unlink_node (client
, n
->children
);
2015 xml_free_request (req
);
2020 * args[0] - attribute
2021 * args[1] - element path
2024 attribute_get (assuan_context_t ctx
, char **args
)
2026 struct client_s
*client
= assuan_get_pointer (ctx
);
2030 struct xml_request_s
*req
;
2032 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
2033 return GPG_ERR_SYNTAX
;
2035 if (!xml_reserved_attribute (args
[0]))
2036 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2039 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2043 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2044 xml_free_request (req
);
2050 a
= xmlGetProp (n
, (xmlChar
*) args
[0]);
2052 return GPG_ERR_NOT_FOUND
;
2054 pthread_cleanup_push ((void *)xmlFree
, a
);
2057 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2059 rc
= GPG_ERR_NO_DATA
;
2061 pthread_cleanup_pop (1);
2066 * args[0] - attribute
2067 * args[1] - element path
2071 attribute_set (struct client_s
*client
, char **args
)
2073 struct xml_request_s
*req
;
2077 if (!args
|| !args
[0] || !args
[1])
2078 return GPG_ERR_SYNTAX
;
2081 * Reserved attribute names.
2083 if (!strcmp (args
[0], "_name")) // Use the RENAME command instead
2084 return GPG_ERR_INV_ATTR
;
2085 else if (!strcmp (args
[0], "target"))
2086 return set_target_attribute (client
, args
+ 1);
2087 else if (!xml_valid_attribute (args
[0]))
2088 return GPG_ERR_INV_VALUE
;
2090 if (!xml_valid_attribute_value (args
[2]))
2091 return GPG_ERR_INV_VALUE
;
2093 if (!xml_reserved_attribute (args
[0]))
2095 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2098 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
2101 rc
= xml_check_recursion (client
, req
);
2102 xml_free_request (req
);
2108 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2112 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2113 xml_free_request (req
);
2117 rc
= xml_is_element_owner (client
, n
);
2120 rc
= xml_add_attribute (client
, n
, args
[0], args
[2]);
2127 * req[1] - attribute name or element path if command is LIST
2128 * req[2] - element path
2129 * req[2] - element path or value
2133 do_attr (assuan_context_t ctx
, char *line
)
2135 struct client_s
*client
= assuan_get_pointer (ctx
);
2139 req
= str_split (line
, " ", 4);
2140 if (!req
|| !req
[0] || !req
[1])
2143 return GPG_ERR_SYNTAX
;
2146 pthread_cleanup_push ((void *)req_free
, req
);
2148 if (strcasecmp (req
[0], "SET") == 0)
2149 rc
= attribute_set (client
, req
+ 1);
2150 else if (strcasecmp (req
[0], "GET") == 0)
2151 rc
= attribute_get (ctx
, req
+ 1);
2152 else if (strcasecmp (req
[0], "DELETE") == 0)
2153 rc
= attribute_delete (client
, req
+ 1);
2154 else if (strcasecmp (req
[0], "LIST") == 0)
2155 rc
= attribute_list (ctx
, req
+ 1);
2157 rc
= GPG_ERR_SYNTAX
;
2159 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2160 pthread_cleanup_pop (1);
2165 attr_command (assuan_context_t ctx
, char *line
)
2167 struct client_s
*client
= assuan_get_pointer (ctx
);
2169 struct argv_s
*args
[] = {
2170 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2174 rc
= parse_options (&line
, args
, client
, 1);
2175 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2176 rc
= GPG_ERR_SYNTAX
;
2178 return send_error (ctx
, rc
);
2180 if (client
->opts
& OPT_INQUIRE
)
2182 unsigned char *result
;
2185 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2187 return send_error (ctx
, rc
);
2189 pthread_cleanup_push ((void *)xfree
, result
);
2190 rc
= do_attr (ctx
, (char *)result
);
2191 pthread_cleanup_pop (1);
2194 rc
= do_attr (ctx
, line
);
2196 return send_error (ctx
, rc
);
2200 parse_iscached_opt_lock (void *data
, void *value
)
2202 struct client_s
*client
= data
;
2205 client
->opts
|= OPT_LOCK
;
2210 parse_iscached_opt_agent (void *data
, void *value
)
2212 struct client_s
*client
= data
;
2215 client
->opts
|= OPT_CACHE_AGENT
;
2220 parse_iscached_opt_sign (void *data
, void *value
)
2222 struct client_s
*client
= data
;
2225 client
->opts
|= OPT_CACHE_SIGN
;
2230 iscached_command (assuan_context_t ctx
, char *line
)
2232 struct client_s
*client
= assuan_get_pointer (ctx
);
2235 struct argv_s
*args
[] = {
2236 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2237 &(struct argv_s
) {"agent", OPTION_TYPE_NOARG
, parse_iscached_opt_agent
},
2238 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_iscached_opt_sign
},
2242 if (!line
|| !*line
)
2243 return send_error (ctx
, GPG_ERR_SYNTAX
);
2245 rc
= parse_options (&line
, args
, client
, 1);
2247 return send_error (ctx
, rc
);
2248 else if (!valid_filename (line
))
2249 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2251 if (!(client
->flags
& FLAG_OPEN
)
2252 && ((client
->opts
& OPT_CACHE_AGENT
) || (client
->opts
& OPT_CACHE_SIGN
)))
2253 return send_error (ctx
, GPG_ERR_INV_STATE
);
2255 rc
= cache_iscached (line
, &defer
, (client
->opts
& OPT_CACHE_AGENT
),
2256 (client
->opts
& OPT_CACHE_SIGN
));
2258 rc
= GPG_ERR_NO_DATA
;
2262 struct cache_data_s
*cdata
= cache_get_data (line
);
2266 rc
= validate_checksum (client
, line
, cdata
, NULL
, NULL
);
2267 if (rc
== GPG_ERR_CHECKSUM
)
2268 rc
= GPG_ERR_NO_DATA
;
2272 if (client
->opts
& OPT_LOCK
2273 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2275 gpg_error_t trc
= rc
;
2277 if (strcmp (line
, client
->filename
))
2278 reset_client (client
);
2280 xfree (client
->filename
);
2281 client
->filename
= str_dup (line
);
2282 rc
= do_lock (client
, 1);
2287 return send_error (ctx
, rc
);
2291 clearcache_command (assuan_context_t ctx
, char *line
)
2293 struct client_s
*client
= assuan_get_pointer (ctx
);
2294 gpg_error_t rc
= 0, all_rc
= 0;
2298 struct client_thread_s
*once
= NULL
;
2302 pthread_cleanup_push (cache_release_mutex
, NULL
);
2303 count
= cache_file_count ();
2304 MUTEX_LOCK (&cn_mutex
);
2305 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
2307 if (!line
|| !*line
)
2310 t
= slist_length (cn_thread_list
);
2312 for (i
= 0; i
< t
; i
++)
2314 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2315 assuan_peercred_t peer
;
2320 /* Lock each connected clients' file mutex to prevent any other client
2321 * from accessing the cache entry (the file mutex is locked upon
2322 * command startup). The cache for the entry is not cleared if the
2323 * file mutex is locked by another client to prevent this function
2324 * from blocking. Rather, it returns an error.
2328 if (thd
->cl
->filename
)
2330 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2331 /* The current client doesn't have permission to open the other
2332 * filename do to "access" configuration parameter in a filename
2333 * section. Since we are clearning all cache entries the error
2334 * will be returned later during cache_clear(). */
2341 else // Idle client without opened file?
2344 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2345 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2347 /* The current client owns the lock. */
2348 if (pthread_equal (pthread_self (), thd
->tid
))
2352 if (cache_iscached (thd
->cl
->filename
, NULL
, 0, 0)
2359 /* The cache entry will be cleared when the other client
2360 * disconnects and cache_timer_thread() does its thing. */
2361 cache_defer_clear (thd
->cl
->filename
);
2364 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2372 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1);
2373 cache_unlock_mutex (thd
->cl
->filename
, 0);
2383 /* A single data filename was specified. Lock only this data file
2384 * mutex and free the cache entry. */
2385 rc
= do_validate_peer (ctx
, line
, &peer
);
2386 if (rc
== GPG_ERR_FORBIDDEN
)
2387 rc
= peer_is_invoker (client
);
2389 if (rc
== GPG_ERR_EACCES
)
2390 rc
= GPG_ERR_FORBIDDEN
;
2392 if (!rc
&& thd
->cl
->filename
&& !strcmp (thd
->cl
->filename
, line
))
2394 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2395 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2397 /* The current client owns the lock. */
2398 if (pthread_equal (pthread_self (), thd
->tid
))
2405 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1);
2406 cache_unlock_mutex (thd
->cl
->filename
, 0);
2409 cache_defer_clear (thd
->cl
->filename
);
2411 /* Found a client with the opened file. The cache entry has been
2412 * either cleared (self) or defered to be cleared. */
2418 /* Only connected clients' cache entries have been cleared. Now clear any
2419 * remaining cache entries without clients but only if there wasn't an
2420 * error from above since this would defeat the locking check of the
2421 * remaining entries. */
2422 if (all
&& !all_rc
&& cache_file_count ())
2424 rc
= cache_clear (client
, NULL
, 1);
2425 if (rc
== GPG_ERR_EACCES
)
2426 rc
= GPG_ERR_FORBIDDEN
;
2429 /* No clients are using the specified file. */
2430 else if (!all_rc
&& !rc
&& !once
)
2431 rc
= cache_clear (NULL
, line
, 1);
2433 /* Release the connection mutex. */
2434 pthread_cleanup_pop (1);
2436 if (!rc
|| (cache_file_count () && count
!= cache_file_count ()))
2437 send_status_all (STATUS_CACHE
, NULL
);
2439 /* Release the cache mutex. */
2440 pthread_cleanup_pop (1);
2442 /* One or more files were locked while clearing all cache entries. */
2446 return send_error (ctx
, rc
);
2450 cachetimeout_command (assuan_context_t ctx
, char *line
)
2452 struct client_s
*client
= assuan_get_pointer (ctx
);
2454 char **req
= str_split (line
, " ", 0);
2457 assuan_peercred_t peer
;
2459 if (!req
|| !*req
|| !req
[1])
2462 return send_error (ctx
, GPG_ERR_SYNTAX
);
2466 timeout
= (int) strtol (req
[1], &p
, 10);
2467 if (errno
!= 0 || *p
|| timeout
< -1)
2470 return send_error (ctx
, GPG_ERR_SYNTAX
);
2473 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2474 if (rc
== GPG_ERR_FORBIDDEN
)
2476 rc
= peer_is_invoker (client
);
2477 if (rc
== GPG_ERR_EACCES
)
2478 rc
= GPG_ERR_FORBIDDEN
;
2483 rc
= cache_set_timeout (req
[0], timeout
);
2484 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2487 MUTEX_LOCK (&rcfile_mutex
);
2488 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2490 MUTEX_UNLOCK (&rcfile_mutex
);
2495 return send_error (ctx
, rc
);
2499 dump_command (assuan_context_t ctx
, char *line
)
2503 struct client_s
*client
= assuan_get_pointer (ctx
);
2506 if (disable_list_and_dump
== 1)
2507 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2510 return send_error (ctx
, GPG_ERR_SYNTAX
);
2512 rc
= peer_is_invoker(client
);
2514 return send_error (ctx
, rc
);
2516 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2520 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2521 pwmd_strerror (GPG_ERR_ENOMEM
));
2522 return send_error (ctx
, GPG_ERR_ENOMEM
);
2525 pthread_cleanup_push ((void *)xmlFree
, xml
);
2526 rc
= xfer_data (ctx
, (char *) xml
, len
);
2527 pthread_cleanup_pop (1);
2528 return send_error (ctx
, rc
);
2532 getconfig_command (assuan_context_t ctx
, char *line
)
2534 struct client_s
*client
= assuan_get_pointer (ctx
);
2536 char filename
[255] = { 0 }, param
[747] = { 0 };
2537 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2539 if (!line
|| !*line
)
2540 return send_error (ctx
, GPG_ERR_SYNTAX
);
2542 if (strchr (line
, ' '))
2544 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2549 if (fp
&& !valid_filename (fp
))
2550 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2552 paramp
= str_down (paramp
);
2553 if (!strcmp (paramp
, "passphrase"))
2554 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2556 p
= config_get_value (fp
? fp
: "global", paramp
);
2558 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2560 tmp
= expand_homedir (p
);
2564 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2565 pwmd_strerror (GPG_ERR_ENOMEM
));
2566 return send_error (ctx
, GPG_ERR_ENOMEM
);
2570 pthread_cleanup_push ((void *)xfree
, p
);
2571 rc
= xfer_data (ctx
, p
, strlen (p
));
2572 pthread_cleanup_pop (1);
2573 return send_error (ctx
, rc
);
2578 xmlXPathContextPtr xp
;
2579 xmlXPathObjectPtr result
;
2585 xpath_command_free (void *arg
)
2587 struct xpath_s
*xpath
= arg
;
2592 req_free (xpath
->req
);
2595 xmlBufferFree (xpath
->buf
);
2598 xmlXPathFreeObject (xpath
->result
);
2601 xmlXPathFreeContext (xpath
->xp
);
2605 do_xpath (assuan_context_t ctx
, char *line
)
2608 struct client_s
*client
= assuan_get_pointer (ctx
);
2609 struct xpath_s _x
= { 0 };
2610 struct xpath_s
*xpath
= &_x
;
2612 if (!line
|| !*line
)
2613 return GPG_ERR_SYNTAX
;
2615 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2617 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2618 return GPG_ERR_ENOMEM
;
2621 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2624 rc
= GPG_ERR_BAD_DATA
;
2629 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2632 rc
= GPG_ERR_BAD_DATA
;
2636 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2638 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2642 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2643 xpath
->result
->nodesetval
,
2644 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2648 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2650 rc
= GPG_ERR_NO_DATA
;
2653 else if (xpath
->req
[1])
2659 pthread_cleanup_push ((void *)xpath_command_free
, &xpath
);
2660 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2661 xmlBufferLength (xpath
->buf
));
2662 pthread_cleanup_pop (0);
2664 xpath_command_free (xpath
);
2669 xpath_command (assuan_context_t ctx
, char *line
)
2671 struct client_s
*client
= assuan_get_pointer (ctx
);
2673 struct argv_s
*args
[] = {
2674 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2678 if (disable_list_and_dump
== 1)
2679 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2681 rc
= peer_is_invoker(client
);
2683 return send_error (ctx
, rc
);
2685 rc
= parse_options (&line
, args
, client
, 1);
2686 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2687 rc
= GPG_ERR_SYNTAX
;
2689 return send_error (ctx
, rc
);
2691 if (client
->opts
& OPT_INQUIRE
)
2693 unsigned char *result
;
2696 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2698 return send_error (ctx
, rc
);
2700 pthread_cleanup_push ((void *)xfree
, result
);
2701 rc
= do_xpath (ctx
, (char *)result
);
2702 pthread_cleanup_pop (1);
2705 rc
= do_xpath (ctx
, line
);
2707 return send_error (ctx
, rc
);
2711 do_xpathattr (assuan_context_t ctx
, char *line
)
2713 struct client_s
*client
= assuan_get_pointer (ctx
);
2717 struct xpath_s _x
= { 0 };
2718 struct xpath_s
*xpath
= &_x
;
2720 if (!line
|| !*line
)
2721 return GPG_ERR_SYNTAX
;
2723 if ((req
= str_split (line
, " ", 3)) == NULL
)
2724 return GPG_ERR_ENOMEM
;
2728 rc
= GPG_ERR_SYNTAX
;
2732 if (!strcasecmp (req
[0], "SET"))
2734 else if (!strcasecmp (req
[0], "DELETE"))
2738 rc
= GPG_ERR_SYNTAX
;
2742 if (!req
[1] || !req
[2])
2744 rc
= GPG_ERR_SYNTAX
;
2748 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2750 rc
= GPG_ERR_ENOMEM
;
2754 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2756 rc
= GPG_ERR_SYNTAX
;
2760 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2763 rc
= GPG_ERR_BAD_DATA
;
2767 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2770 rc
= GPG_ERR_BAD_DATA
;
2774 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2776 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2780 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2781 xpath
->result
->nodesetval
,
2782 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2783 (xmlChar
*) req
[1]);
2786 xpath_command_free (xpath
);
2791 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2793 xpathattr_command (assuan_context_t ctx
, char *line
)
2795 struct client_s
*client
= assuan_get_pointer (ctx
);
2797 struct argv_s
*args
[] = {
2798 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2802 if (disable_list_and_dump
== 1)
2803 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2805 rc
= peer_is_invoker(client
);
2807 return send_error (ctx
, rc
);
2809 rc
= parse_options (&line
, args
, client
, 1);
2810 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2811 rc
= GPG_ERR_SYNTAX
;
2813 return send_error (ctx
, rc
);
2815 if (client
->opts
& OPT_INQUIRE
)
2817 unsigned char *result
;
2820 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2822 return send_error (ctx
, rc
);
2824 pthread_cleanup_push ((void *)xfree
, result
);
2825 rc
= do_xpathattr (ctx
, (char *)result
);
2826 pthread_cleanup_pop (1);
2829 rc
= do_xpathattr (ctx
, line
);
2831 return send_error (ctx
, rc
);
2835 do_import (struct client_s
*client
, const char *root_element
,
2836 unsigned char *content
)
2838 xmlDocPtr doc
= NULL
;
2839 xmlNodePtr n
= NULL
, root
;
2841 struct string_s
*str
= NULL
;
2842 struct xml_request_s
*req
= NULL
;
2844 if (!content
|| !*content
)
2845 return GPG_ERR_SYNTAX
;
2849 rc
= xml_new_request (client
, root_element
, XML_CMD_STORE
, &req
);
2856 if (!xml_valid_element_path (req
->args
, 0))
2858 xml_free_request (req
);
2860 return GPG_ERR_INV_VALUE
;
2864 str
= string_new_content ((char *)content
);
2865 str
= string_prepend (str
, "<pwmd>");
2866 str
= string_append (str
, "</pwmd>");
2867 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2868 string_free (str
, 1);
2871 rc
= GPG_ERR_BAD_DATA
;
2875 root
= xmlDocGetRootElement (doc
);
2876 root
= root
->children
;
2877 if (root
->type
!= XML_ELEMENT_NODE
)
2879 rc
= GPG_ERR_BAD_DATA
;
2883 rc
= xml_validate_import (client
, root
);
2889 /* Create the new specified root element path, if needed. */
2890 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
2895 /* Overwrite the children of the specified root element path. */
2896 (void)xml_unlink_node (client
, n
->children
);
2900 n
= xmlDocGetRootElement (client
->doc
);
2905 xmlChar
*name
= xml_attribute_value (root
, (xmlChar
*)"_name");
2908 rc
= GPG_ERR_BAD_DATA
;
2911 /* No --root argument passed. Overwrite the current documents' root
2912 * element matching the root element of the imported data. */
2913 xml_free_request (req
);
2915 rc
= xml_new_request (client
, (char *)name
, XML_CMD_DELETE
, &req
);
2921 tmp
= xml_find_elements (client
, req
,
2922 xmlDocGetRootElement (req
->doc
), &rc
);
2923 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2927 (void)xml_unlink_node (client
, tmp
);
2935 copy
= xmlCopyNodeList (root
);
2937 rc
= GPG_ERR_ENOMEM
;
2940 n
= xmlAddChildList (n
, copy
);
2942 rc
= GPG_ERR_ENOMEM
;
2947 if (!rc
&& n
&& n
->parent
)
2948 rc
= xml_update_element_mtime (client
, n
->parent
);
2951 xml_free_request (req
);
2960 parse_import_opt_root (void *data
, void *value
)
2962 struct client_s
*client
= data
;
2964 client
->import_root
= str_dup (value
);
2965 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
2969 import_command (assuan_context_t ctx
, char *line
)
2972 struct client_s
*client
= assuan_get_pointer (ctx
);
2973 unsigned char *result
;
2975 struct argv_s
*args
[] = {
2976 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
2980 xfree (client
->import_root
);
2981 client
->import_root
= NULL
;
2982 rc
= parse_options (&line
, args
, client
, 0);
2984 return send_error (ctx
, rc
);
2986 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2989 xfree (client
->import_root
);
2990 client
->import_root
= NULL
;
2991 return send_error (ctx
, rc
);
2994 rc
= do_import (client
, client
->import_root
, result
);
2995 xfree (client
->import_root
);
2996 client
->import_root
= NULL
;
2997 return send_error (ctx
, rc
);
3001 do_lock (struct client_s
*client
, int add
)
3003 gpg_error_t rc
= lock_file_mutex (client
, add
);
3006 client
->flags
|= FLAG_LOCK_CMD
;
3012 lock_command (assuan_context_t ctx
, char *line
)
3014 struct client_s
*client
= assuan_get_pointer (ctx
);
3018 return send_error (ctx
, GPG_ERR_SYNTAX
);
3020 rc
= do_lock (client
, 0);
3021 return send_error (ctx
, rc
);
3025 unlock_command (assuan_context_t ctx
, char *line
)
3027 struct client_s
*client
= assuan_get_pointer (ctx
);
3031 return send_error (ctx
, GPG_ERR_SYNTAX
);
3033 rc
= unlock_file_mutex (client
, 0);
3034 return send_error (ctx
, rc
);
3038 get_set_env (const char *name
, const char *value
)
3040 if (value
&& *value
)
3041 setenv (name
, value
, 1);
3047 option_command (assuan_context_t ctx
, char *line
)
3049 struct client_s
*client
= assuan_get_pointer (ctx
);
3051 char namebuf
[255] = { 0 };
3052 char *name
= namebuf
;
3053 char *value
= NULL
, *p
, *tmp
= NULL
;
3055 p
= strchr (line
, '=');
3058 strncpy (namebuf
, line
, sizeof(namebuf
));
3059 namebuf
[sizeof(namebuf
)-1] = 0;
3063 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3064 namebuf
[sizeof(namebuf
)-1] = 0;
3068 log_write2 ("OPTION name='%s' value='%s'", name
, value
);
3070 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3076 n
= strtol (value
, &tmp
, 10);
3078 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3081 client
->lock_timeout
= n
;
3083 else if (strcasecmp (name
, (char *) "NAME") == 0)
3085 if (value
&& strchr (value
, ' '))
3086 rc
= GPG_ERR_INV_VALUE
;
3089 tmp
= pthread_getspecific (thread_name_key
);
3091 MUTEX_LOCK (&cn_mutex
);
3092 xfree (client
->thd
->name
);
3093 client
->thd
->name
= NULL
;
3095 if (!value
|| !*value
)
3096 pthread_setspecific (thread_name_key
, str_dup (""));
3099 client
->thd
->name
= str_dup (value
);
3100 pthread_setspecific (thread_name_key
, str_dup (value
));
3103 MUTEX_UNLOCK (&cn_mutex
);
3106 else if (strcasecmp (name
, "disable-pinentry") == 0)
3110 if (value
&& *value
)
3112 n
= (int) strtol (value
, &tmp
, 10);
3113 if (*tmp
|| n
< 0 || n
> 1)
3114 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3118 client
->flags
|= FLAG_NO_PINENTRY
;
3120 client
->flags
&= ~FLAG_NO_PINENTRY
;
3122 else if (strcasecmp (name
, "ttyname") == 0)
3124 get_set_env ("GPG_TTY", value
);
3126 else if (strcasecmp (name
, "ttytype") == 0)
3128 get_set_env ("TERM", value
);
3130 else if (strcasecmp (name
, "display") == 0)
3132 get_set_env ("DISPLAY", value
);
3134 else if (strcasecmp (name
, "lc_messages") == 0)
3137 else if (strcasecmp (name
, "lc_ctype") == 0)
3140 else if (strcasecmp (name
, "desc") == 0)
3143 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3147 rc
= GPG_ERR_UNKNOWN_OPTION
;
3149 return send_error (ctx
, rc
);
3153 do_rename (assuan_context_t ctx
, char *line
)
3155 struct client_s
*client
= assuan_get_pointer (ctx
);
3157 xmlNodePtr src
, dst
;
3158 struct string_s
*str
= NULL
, *tstr
;
3159 struct xml_request_s
*req
;
3162 args
= str_split (line
, " ", 0);
3163 if (!args
|| strv_length (args
) != 2)
3166 return GPG_ERR_SYNTAX
;
3169 if (!xml_valid_element ((xmlChar
*) args
[1]))
3172 return GPG_ERR_INV_VALUE
;
3175 rc
= xml_new_request (client
, args
[0], XML_CMD_RENAME
, &req
);
3182 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3186 rc
= xml_is_element_owner (client
, src
);
3190 xmlChar
*a
= xmlGetProp (src
, (xmlChar
*) "_name");
3193 rc
= GPG_ERR_ENOMEM
;
3197 /* To prevent unwanted effects:
3199 * <element _name="a"><element _name="b"/></element>
3203 if (xmlStrEqual (a
, (xmlChar
*) args
[1]))
3206 rc
= GPG_ERR_EEXIST
;
3211 str
= string_new (args
[0]);
3214 rc
= GPG_ERR_ENOMEM
;
3218 p
= strrchr (str
->str
, '\t');
3220 tstr
= string_truncate (str
, strlen (str
->str
)-strlen (p
));
3222 tstr
= string_truncate (str
, 0);
3226 string_free (str
, 1);
3227 rc
= GPG_ERR_ENOMEM
;
3232 tstr
= string_append_printf (str
, "%s%s", str
->len
? "\t" : "", args
[1]);
3235 string_free (str
, 1);
3236 rc
= GPG_ERR_ENOMEM
;
3241 xml_free_request (req
);
3242 rc
= xml_new_request (client
, str
->str
, XML_CMD_RENAME
, &req
);
3243 string_free (str
, 1);
3250 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3251 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3256 /* Target may exist:
3258 * <element _name="a"/>
3259 * <element _name="b" target="a"/>
3265 rc
= GPG_ERR_EEXIST
;
3271 rc
= xml_is_element_owner (client
, dst
);
3275 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3277 xml_unlink_node (client
, dst
);
3280 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3283 xml_free_request (req
);
3289 rename_command (assuan_context_t ctx
, char *line
)
3291 struct client_s
*client
= assuan_get_pointer (ctx
);
3293 struct argv_s
*args
[] = {
3294 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3298 rc
= parse_options (&line
, args
, client
, 1);
3299 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3300 rc
= GPG_ERR_SYNTAX
;
3302 return send_error (ctx
, rc
);
3304 if (client
->opts
& OPT_INQUIRE
)
3306 unsigned char *result
;
3309 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3311 return send_error (ctx
, rc
);
3313 pthread_cleanup_push ((void *)xfree
, result
);
3314 rc
= do_rename (ctx
, (char *)result
);
3315 pthread_cleanup_pop (1);
3318 rc
= do_rename (ctx
, line
);
3320 return send_error (ctx
, rc
);
3324 do_copy (assuan_context_t ctx
, char *line
)
3326 struct client_s
*client
= assuan_get_pointer (ctx
);
3327 char **args
, **targs
;
3328 xmlNodePtr src
, dst
, tree
= NULL
;
3329 struct xml_request_s
*req
;
3332 args
= str_split (line
, " ", 0);
3333 if (!args
|| strv_length (args
) != 2)
3336 return GPG_ERR_SYNTAX
;
3339 targs
= str_split (args
[1], "\t", 0);
3343 return GPG_ERR_SYNTAX
;
3346 if (!xml_valid_element_path (targs
, 0))
3350 return GPG_ERR_INV_VALUE
;
3354 rc
= xml_new_request (client
, args
[0], XML_CMD_NONE
, &req
);
3361 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3365 tree
= xmlCopyNodeList (src
);
3368 rc
= GPG_ERR_ENOMEM
;
3372 xml_free_request (req
);
3373 /* Create the destination element path if it does not exist. */
3374 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &req
);
3381 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3385 rc
= xml_is_element_owner (client
, dst
);
3389 rc
= xml_validate_target (client
, req
->doc
, args
[1], src
);
3390 if (rc
|| src
== dst
)
3392 rc
= rc
? rc
: GPG_ERR_EEXIST
;
3396 /* Merge any attributes from the src node to the initial dst node. */
3397 for (xmlAttrPtr attr
= tree
->properties
; attr
; attr
= attr
->next
)
3399 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3402 xmlAttrPtr a
= xmlHasProp (dst
, attr
->name
);
3406 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3407 xmlNewProp (dst
, attr
->name
, tmp
);
3409 /* Create the default attributes. */
3410 rc
= xml_add_attribute (client
, dst
, NULL
, NULL
);
3413 xmlNodePtr n
= dst
->children
;
3414 (void)xml_unlink_node (client
, n
);
3415 dst
->children
= NULL
;
3419 n
= xmlCopyNodeList (tree
->children
);
3422 rc
= GPG_ERR_ENOMEM
;
3426 n
= xmlAddChildList (dst
, n
);
3429 rc
= GPG_ERR_ENOMEM
;
3433 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
)
3434 == dst
->parent
? dst
: dst
->parent
);
3439 (void)xml_unlink_node (client
, tree
);
3441 xml_free_request (req
);
3447 copy_command (assuan_context_t ctx
, char *line
)
3449 struct client_s
*client
= assuan_get_pointer (ctx
);
3451 struct argv_s
*args
[] = {
3452 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3456 rc
= parse_options (&line
, args
, client
, 1);
3457 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3458 rc
= GPG_ERR_SYNTAX
;
3460 return send_error (ctx
, rc
);
3462 if (client
->opts
& OPT_INQUIRE
)
3464 unsigned char *result
;
3467 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3469 return send_error (ctx
, rc
);
3471 pthread_cleanup_push ((void *)xfree
, result
);
3472 rc
= do_copy (ctx
, (char *)result
);
3473 pthread_cleanup_pop (1);
3476 rc
= do_copy (ctx
, line
);
3478 return send_error (ctx
, rc
);
3482 do_move (assuan_context_t ctx
, char *line
)
3484 struct client_s
*client
= assuan_get_pointer (ctx
);
3486 xmlNodePtr src
, dst
;
3487 struct xml_request_s
*req
;
3490 args
= str_split (line
, " ", 0);
3491 if (!args
|| strv_length (args
) != 2)
3494 return GPG_ERR_SYNTAX
;
3499 char **targs
= str_split (args
[1], "\t", 0);
3503 return GPG_ERR_ENOMEM
;
3506 if (!xml_valid_element_path (targs
, 0))
3510 return GPG_ERR_INV_VALUE
;
3516 rc
= xml_new_request (client
, args
[0], XML_CMD_MOVE
, &req
);
3523 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3527 rc
= xml_is_element_owner (client
, src
);
3531 xml_free_request (req
);
3535 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
3539 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
3543 dst
= xmlDocGetRootElement (client
->doc
);
3545 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3550 for (xmlNodePtr n
= dst
; n
; n
= n
->parent
)
3554 rc
= GPG_ERR_EEXIST
;
3561 xmlChar
*a
= xml_attribute_value (src
, (xmlChar
*) "_name");
3562 xmlNodePtr dup
= xml_find_element (client
, dst
->children
, (char *) a
, &rc
);
3565 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3574 rc
= GPG_ERR_EEXIST
;
3578 if (dst
== xmlDocGetRootElement (client
->doc
))
3583 while (n
->parent
&& n
->parent
!= dst
)
3586 xmlChar
*a
= xml_attribute_value (n
, (xmlChar
*) "_name");
3587 xmlChar
*b
= xml_attribute_value (src
, (xmlChar
*) "_name");
3589 if (xmlStrEqual (a
, b
))
3592 xmlUnlinkNode (src
);
3593 (void)xml_unlink_node (client
, n
);
3600 (void)xml_unlink_node (client
, dup
);
3603 xmlUnlinkNode (dup
);
3607 if (!dst
&& *req
->args
)
3609 struct xml_request_s
*nreq
= NULL
;
3610 xmlChar
*name
= xml_attribute_value (src
, (xmlChar
*) "_name");
3612 if (src
->parent
== xmlDocGetRootElement (client
->doc
)
3613 && !strcmp ((char *) name
, *req
->args
))
3616 rc
= GPG_ERR_EEXIST
;
3621 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &nreq
);
3624 dst
= xml_find_elements (client
, nreq
,
3625 xmlDocGetRootElement (nreq
->doc
), &rc
);
3626 xml_free_request (nreq
);
3633 xml_update_element_mtime (client
, src
->parent
);
3634 xmlUnlinkNode (src
);
3636 dst
= xmlAddChildList (dst
, src
);
3638 rc
= GPG_ERR_ENOMEM
;
3640 xml_update_element_mtime (client
, dst
->parent
);
3643 xml_free_request (req
);
3649 move_command (assuan_context_t ctx
, char *line
)
3651 struct client_s
*client
= assuan_get_pointer (ctx
);
3653 struct argv_s
*args
[] = {
3654 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3658 rc
= parse_options (&line
, args
, client
, 1);
3659 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3660 rc
= GPG_ERR_SYNTAX
;
3662 return send_error (ctx
, rc
);
3664 if (client
->opts
& OPT_INQUIRE
)
3666 unsigned char *result
;
3669 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3671 return send_error (ctx
, rc
);
3673 pthread_cleanup_push ((void *)xfree
, result
);
3674 rc
= do_move (ctx
, (char *)result
);
3675 pthread_cleanup_pop (1);
3678 rc
= do_move (ctx
, line
);
3680 return send_error (ctx
, rc
);
3684 ls_command (assuan_context_t ctx
, char *line
)
3692 return send_error (ctx
, GPG_ERR_SYNTAX
);
3694 tmp
= str_asprintf ("%s/data", homedir
);
3695 dir
= expand_homedir (tmp
);
3698 rc
= gpg_error_from_errno (errno
);
3703 return send_error (ctx
, rc
);
3707 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3708 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3712 pthread_cleanup_push ((void *)xfree
, p
);
3713 pthread_cleanup_push ((void *)(void *)(void *)closedir
, d
);
3716 while (!readdir_r (d
, p
, &cur
) && cur
)
3720 if (stat (cur
->d_name
, &st
) == -1 || !S_ISREG (st
.st_mode
))
3723 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3730 rc
= GPG_ERR_ENOMEM
;
3738 pthread_cleanup_pop (1); // closedir (d)
3739 pthread_cleanup_pop (1); // xfree (p)
3742 return send_error (ctx
, rc
);
3745 return send_error (ctx
, GPG_ERR_NO_DATA
);
3747 list
[strlen (list
) - 1] = 0;
3748 pthread_cleanup_push ((void *)xfree
, list
);
3749 rc
= xfer_data (ctx
, list
, strlen (list
));
3750 pthread_cleanup_pop (1);
3751 return send_error (ctx
, rc
);
3755 bye_notify (assuan_context_t ctx
, char *line
)
3757 struct client_s
*cl
= assuan_get_pointer (ctx
);
3758 gpg_error_t ret
= 0;
3760 update_client_state (cl
, CLIENT_STATE_DISCON
);
3763 if (cl
->thd
->remote
)
3769 struct timeval tv
= { 0, 50000 };
3771 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3772 if (rc
== GNUTLS_E_AGAIN
)
3773 select (0, NULL
, NULL
, NULL
, &tv
);
3775 while (rc
== GNUTLS_E_AGAIN
);
3779 /* This will let assuan_process_next() return. */
3780 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
3782 cl
->last_rc
= gpg_error_from_errno (errno
);
3786 cl
->last_rc
= 0; // BYE command result
3791 reset_notify (assuan_context_t ctx
, char *line
)
3793 struct client_s
*client
= assuan_get_pointer (ctx
);
3796 reset_client (client
);
3802 * This is called before every Assuan command.
3805 command_startup (assuan_context_t ctx
, const char *name
)
3807 struct client_s
*client
= assuan_get_pointer (ctx
);
3809 struct command_table_s
*cmd
= NULL
;
3811 log_write2 ("command='%s'", name
);
3812 client
->last_rc
= client
->opts
= 0;
3814 for (int i
= 0; command_table
[i
]; i
++)
3816 if (!strcasecmp (name
, command_table
[i
]->name
))
3818 if (command_table
[i
]->ignore_startup
)
3820 cmd
= command_table
[i
];
3826 return GPG_ERR_UNKNOWN_COMMAND
;
3828 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3830 update_client_state (client
, CLIENT_STATE_COMMAND
);
3836 * This is called after every Assuan command.
3839 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3841 struct client_s
*client
= assuan_get_pointer (ctx
);
3843 if (!(client
->flags
& FLAG_LOCK_CMD
))
3844 unlock_file_mutex (client
, 0);
3846 unlock_flock (&client
->flock_fd
);
3847 log_write2 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
3848 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3850 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3852 if (client
->thd
->state
!= CLIENT_STATE_DISCON
)
3853 update_client_state (client
, CLIENT_STATE_IDLE
);
3857 parse_help_opt_html (void *data
, void *value
)
3859 struct client_s
*client
= data
;
3862 client
->opts
|= OPT_HTML
;
3867 help_command (assuan_context_t ctx
, char *line
)
3871 struct client_s
*client
= assuan_get_pointer (ctx
);
3872 struct argv_s
*args
[] = {
3873 &(struct argv_s
) {"html", OPTION_TYPE_NOARG
, parse_help_opt_html
},
3877 rc
= parse_options (&line
, args
, client
, 1);
3879 return send_error (ctx
, rc
);
3881 if (!line
|| !*line
)
3884 char *help
= str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3885 "For commands that take an element path as an argument, each element is "
3886 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3889 for (i
= 0; command_table
[i
]; i
++)
3891 if (!command_table
[i
]->help
)
3894 /* @npxref{} won't put a "See " or "see " in front of the command.
3895 * It's not a texinfo command but needed for --html. */
3896 tmp
= str_asprintf ("%s %s%s%s", help
,
3897 client
->opts
& OPT_HTML
? "@npxref{" : "",
3898 command_table
[i
]->name
,
3899 client
->opts
& OPT_HTML
? "}" : "");
3904 tmp
= strip_texi_and_wrap (help
, client
->opts
& OPT_HTML
);
3906 pthread_cleanup_push ((void *)xfree
, tmp
);
3907 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3908 pthread_cleanup_pop (1);
3909 return send_error (ctx
, rc
);
3912 for (i
= 0; command_table
[i
]; i
++)
3914 if (!strcasecmp (line
, command_table
[i
]->name
))
3918 if (!command_table
[i
]->help
)
3921 help
= strip_texi_and_wrap (command_table
[i
]->help
,
3922 client
->opts
& OPT_HTML
);
3923 tmp
= str_asprintf ("%s%s",
3924 (client
->opts
& OPT_HTML
) ? "" : _("Usage: "),
3927 pthread_cleanup_push ((void *)xfree
, tmp
);
3928 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3929 pthread_cleanup_pop (1);
3930 return send_error (ctx
, rc
);
3934 return send_error (ctx
, GPG_ERR_INV_NAME
);
3938 new_command (const char *name
, int ignore
, int unlock
, int flock_type
,
3939 gpg_error_t (*handler
) (assuan_context_t
, char *),
3943 struct command_table_s
**tmp
;
3946 for (i
= 0; command_table
[i
]; i
++);
3948 tmp
= xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3950 command_table
= tmp
;
3951 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3952 command_table
[i
]->name
= name
;
3953 command_table
[i
]->handler
= handler
;
3954 command_table
[i
]->ignore_startup
= ignore
;
3955 command_table
[i
]->unlock
= unlock
;
3956 command_table
[i
]->flock_type
= flock_type
;
3957 command_table
[i
++]->help
= help
;
3958 command_table
[i
] = NULL
;
3966 for (i
= 0; command_table
[i
]; i
++)
3967 xfree (command_table
[i
]);
3969 xfree (command_table
);
3973 sort_commands (const void *arg1
, const void *arg2
)
3975 struct command_table_s
*const *a
= arg1
;
3976 struct command_table_s
*const *b
= arg2
;
3985 return strcmp ((*a
)->name
, (*b
)->name
);
3989 passwd_command (assuan_context_t ctx
, char *line
)
3991 struct client_s
*client
= assuan_get_pointer (ctx
);
3994 rc
= peer_is_invoker (client
);
3995 if (rc
== GPG_ERR_EACCES
)
3996 return send_error (ctx
, GPG_ERR_FORBIDDEN
);
3998 return send_error (ctx
, rc
);
4000 if (client
->flags
& FLAG_NEW
)
4001 return send_error (ctx
, GPG_ERR_INV_STATE
);
4003 client
->crypto
->keyfile
= config_get_string (client
->filename
,
4005 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
,
4006 client
->crypto
->keyfile
);
4008 return send_error (ctx
, rc
);
4011 rc
= crypto_passwd (client
, client
->crypto
);
4013 crypto_free_non_keys (client
->crypto
);
4014 return send_error (ctx
, rc
);
4018 parse_opt_data (void *data
, void *value
)
4020 struct client_s
*client
= data
;
4023 client
->opts
|= OPT_DATA
;
4028 send_client_list (assuan_context_t ctx
)
4030 struct client_s
*client
= assuan_get_pointer (ctx
);
4033 if (client
->opts
& OPT_VERBOSE
)
4039 MUTEX_LOCK (&cn_mutex
);
4040 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
4041 t
= slist_length (cn_thread_list
);
4043 for (i
= 0; i
< t
; i
++)
4045 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4048 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4051 tmp
= build_client_info_line (thd
, 0);
4054 char **l
= strv_cat (list
, tmp
);
4056 rc
= GPG_ERR_ENOMEM
;
4061 rc
= GPG_ERR_ENOMEM
;
4070 pthread_cleanup_pop (1);
4074 line
= strv_join ("\n", list
);
4076 pthread_cleanup_push ((void *)xfree
, line
);
4077 rc
= xfer_data (ctx
, line
, strlen (line
));
4078 pthread_cleanup_pop (1);
4082 if (client
->opts
& OPT_DATA
)
4084 char buf
[ASSUAN_LINELENGTH
];
4086 MUTEX_LOCK (&cn_mutex
);
4087 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
4088 MUTEX_UNLOCK (&cn_mutex
);
4089 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4092 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4098 getinfo_command (assuan_context_t ctx
, char *line
)
4100 struct client_s
*client
= assuan_get_pointer (ctx
);
4102 char buf
[ASSUAN_LINELENGTH
];
4103 struct argv_s
*args
[] = {
4104 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4105 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4109 rc
= parse_options (&line
, args
, client
, 1);
4111 return send_error (ctx
, rc
);
4113 if (!strcasecmp (line
, "clients"))
4115 rc
= send_client_list (ctx
);
4117 else if (!strcasecmp (line
, "cache"))
4119 if (client
->opts
& OPT_DATA
)
4121 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4122 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4125 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4127 else if (!strcasecmp (line
, "pid"))
4130 pid_t pid
= getpid ();
4132 snprintf (buf
, sizeof (buf
), "%i", pid
);
4133 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4135 else if (!strcasecmp (line
, "version"))
4137 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4145 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4148 else if (!strcasecmp (line
, "last_error"))
4150 if (client
->last_error
)
4151 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4153 rc
= GPG_ERR_NO_DATA
;
4155 else if (!strcasecmp (line
, "user"))
4160 if (client
->thd
->remote
)
4161 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4163 user
= get_username (client
->thd
->peer
->uid
);
4165 user
= get_username (client
->thd
->peer
->uid
);
4169 pthread_cleanup_push ((void *)xfree
, user
);
4170 rc
= xfer_data (ctx
, user
, strlen (user
));
4171 pthread_cleanup_pop (1);
4174 rc
= GPG_ERR_NO_DATA
;
4177 rc
= gpg_error (GPG_ERR_SYNTAX
);
4179 return send_error (ctx
, rc
);
4183 parse_listkeys_opt_secret_only (void *data
, void *value
)
4185 struct client_s
*client
= data
;
4188 client
->opts
|= OPT_SECRET_ONLY
;
4193 keyinfo_command (assuan_context_t ctx
, char *line
)
4195 struct client_s
*client
= assuan_get_pointer (ctx
);
4197 char **keys
= NULL
, **p
= NULL
;
4201 return send_error (ctx
, GPG_ERR_SYNTAX
);
4203 if (!(client
->flags
& FLAG_OPEN
))
4204 return send_error (ctx
, GPG_ERR_INV_STATE
);
4206 if (client
->flags
& FLAG_NEW
)
4207 return send_error (ctx
, GPG_ERR_NO_DATA
);
4209 rc
= lock_flock (ctx
, client
->filename
, FLOCK_TYPE_SH
, &client
->flock_fd
);
4211 return send_error (ctx
, rc
);
4213 rc
= crypto_is_symmetric (client
->filename
);
4214 unlock_flock (&client
->flock_fd
);
4217 else if (rc
!= GPG_ERR_BAD_DATA
)
4218 return send_error (ctx
, rc
);
4223 p
= strv_catv(keys
, client
->crypto
->pubkey
);
4225 rc
= GPG_ERR_ENOMEM
;
4231 for (p
= client
->crypto
->sigkey
; p
&& *p
; p
++)
4233 if (!strv_printf(&keys
, "S%s", *p
))
4236 return send_error (ctx
, GPG_ERR_ENOMEM
);
4241 rc
= GPG_ERR_ENOMEM
;
4245 line
= strv_join ("\n", keys
);
4247 pthread_cleanup_push ((void *)xfree
, line
);
4249 rc
= xfer_data (ctx
, line
, strlen (line
));
4251 rc
= GPG_ERR_ENOMEM
;
4253 pthread_cleanup_pop (1);
4256 rc
= GPG_ERR_NO_DATA
;
4260 return send_error (ctx
, rc
);
4264 kill_command (assuan_context_t ctx
, char *line
)
4266 struct client_s
*client
= assuan_get_pointer (ctx
);
4270 if (!line
|| !*line
)
4271 return send_error (ctx
, GPG_ERR_SYNTAX
);
4273 MUTEX_LOCK (&cn_mutex
);
4274 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
4275 t
= slist_length (cn_thread_list
);
4278 for (i
= 0; i
< t
; i
++)
4280 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4281 char *tmp
= str_asprintf ("%p", thd
->tid
);
4283 if (strcmp (line
, tmp
))
4290 rc
= peer_is_invoker (client
);
4294 #ifdef HAVE_PTHREAD_CANCEL
4295 rc
= pthread_cancel (thd
->tid
);
4302 else if (rc
== GPG_ERR_EACCES
)
4303 rc
= GPG_ERR_FORBIDDEN
;
4307 if (config_get_boolean ("global", "strict_kill"))
4311 if (client
->thd
->remote
&& thd
->remote
)
4313 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
4315 #ifdef HAVE_PTHREAD_CANCEL
4316 rc
= pthread_cancel (thd
->tid
);
4324 else if (!client
->thd
->remote
&& !thd
->remote
)
4327 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
4329 #ifdef HAVE_PTHREAD_CANCEL
4330 rc
= pthread_cancel (thd
->tid
);
4340 pthread_cleanup_pop (1);
4341 return send_error (ctx
, rc
);
4345 listkeys_command (assuan_context_t ctx
, char *line
)
4347 struct client_s
*client
= assuan_get_pointer (ctx
);
4348 struct crypto_s
*crypto
= NULL
;
4349 char **pattern
= NULL
;
4350 gpgme_key_t
*keys
= NULL
;
4351 char **result
= NULL
;
4353 struct argv_s
*args
[] = {
4354 &(struct argv_s
) {"secret-only", OPTION_TYPE_NOARG
,
4355 parse_listkeys_opt_secret_only
},
4359 rc
= parse_options (&line
, args
, client
, 1);
4361 return send_error (ctx
, rc
);
4363 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
4364 client
->flags
& FLAG_NO_PINENTRY
, NULL
);
4366 return send_error (ctx
, rc
);
4368 pthread_cleanup_push ((void *)crypto_free
, crypto
);
4369 pattern
= str_split (line
, ",", 0);
4370 pthread_cleanup_push ((void *)(void *)strv_free
, pattern
);
4371 rc
= crypto_list_keys (crypto
, pattern
, client
->opts
& OPT_SECRET_ONLY
,
4373 pthread_cleanup_pop (1);
4374 pthread_cleanup_pop (1);
4379 for (i
= 0; keys
[i
]; i
++)
4381 char *p
= crypto_key_info (keys
[i
]);
4386 rc
= GPG_ERR_ENOMEM
;
4390 r
= strv_cat (result
, p
);
4393 rc
= GPG_ERR_ENOMEM
;
4401 rc
= GPG_ERR_NO_DATA
;
4403 crypto_free_key_list (keys
);
4408 line
= strv_join ("\n", result
);
4410 pthread_cleanup_push ((void *)xfree
, line
);
4412 rc
= GPG_ERR_ENOMEM
;
4414 rc
= xfer_data (ctx
, line
, strlen (line
));
4416 pthread_cleanup_pop (1);
4421 return send_error (ctx
, rc
);
4427 /* !BEGIN-HELP-TEXT!
4429 * This comment is used as a marker to generate the offline documentation
4430 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4431 * script to determine where commands begin and end.
4433 new_command("HELP", 1, 1, 0, help_command
, _(
4434 "HELP [--html] [<COMMAND>]\n"
4435 "Show available commands or command specific help text."
4437 "The @option{--html} option will output the help text in HTML format."
4440 new_command("KILL", 1, 0, 0, kill_command
, _(
4441 "KILL <thread_id>\n"
4442 "Terminates the client identified by @var{thread_id} and releases any file "
4443 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4444 "for details about listing connected clients. An @code{invoking_user} "
4445 "(@pxref{Configuration}) may kill any client while others may only kill "
4446 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4449 new_command("LISTKEYS", 0, 0, 0, listkeys_command
, _(
4450 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4451 "Returns a new line separated list of key information matching a comma "
4452 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4453 "specified, only keys matching @var{pattern} that also have a secret key "
4454 "available will be returned."
4457 new_command("KEYINFO", 1, 0, 0, keyinfo_command
, _(
4459 "Returns a new line separated list of key ID's that the currently opened "
4460 "data file has recipients and signers for. If the key is a signing key it "
4461 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4462 "signers in the case of being symmetrically encrypted, the error code "
4463 "@code{GPG_ERR_NO_DATA} is returned."
4466 new_command("GETINFO", 1, 1, 0, getinfo_command
, _(
4467 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4468 "Get server and other information. The information is returned via a status "
4469 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4472 "@var{CACHE} returns the number of cached documents."
4474 "@var{CLIENTS} returns the number of "
4475 "connected clients via a status message or a list of connected clients when "
4476 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4477 "verbose line of a client list contains "
4479 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4480 "IP address if remote, file lock status, whether the current client is self "
4481 "or not, client state (see below), "
4482 "user ID or TLS fingerprint of the connected client, username if the "
4483 "client is a local one else @code{-}, and finally the time stamp of when the "
4486 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4487 "the client has connected but hasn't completed initializing, state @code{2} "
4488 "indicates that the client is idle, state @code{3} means the "
4489 "client is in a command and state @code{4} means the client is disconnecting. "
4491 "@var{PID} returns the process ID number of the server via a data response."
4493 "@var{VERSION} returns the server version number and compile-time features "
4494 "via a data response with each being space delimited."
4496 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4497 "via a data response, when available."
4499 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4500 "via a data response."
4503 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
4505 "Changes the passphrase of the secret key required to open the current "
4506 "data file. If the data file is symmetrically encrypted, the error "
4507 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4508 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4509 "this command saving any unwanted changes to the @abbr{XML} document."
4511 "This command is not available to non-invoking clients "
4512 "(@pxref{Access Control})."
4515 new_command("OPEN", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
4516 "OPEN [--lock] <filename>\n"
4517 "Opens @var{filename}. When the @var{filename} is not found on the "
4518 "file-system then a new in-memory document will be created. If the file is "
4519 "found, it is looked for in the file cache and when found no passphrase will "
4520 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4521 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4522 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4523 "@emph{INQUIRE} the client for the passphrase."
4525 "When the @option{--lock} option is passed then the file mutex will be "
4526 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4527 "file had been opened."
4530 new_command("SAVE", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
4531 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<fpr>[,..] | [--inquire-keyid]] [--sign-keyid=<fpr>[,..] | [--inquire-sign-keyid]]\n"
4532 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4533 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4534 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4535 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4536 "for the passphrase of the secret key used for signing."
4538 "The @option{--inquire-keyparam} option will send an "
4539 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4540 "generating the new keypair. The inquired data is expected to be in "
4541 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4542 "details. Note that when this option is specified a new keypair will be "
4543 "generated reguardless if the file is a new one and that the passphrase for "
4544 "the current file will be required before generating the new keypair. This "
4545 "option is available to non-invoking clients (@pxref{Access Control}) only "
4546 "when the file is a new one."
4548 "You can encrypt the data file to a recipient other than the one that it "
4549 "was encrypted with by passing the @option{--keyid} or "
4550 "@option{--inquire-keyid} option with "
4551 "the fingerprint of a public encryption key as its argument. Use the "
4552 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4553 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4554 "may also be used to sign the data "
4555 "file with an alternate key by specifying the fingerprint of a secret key. "
4556 "A passphrase to decrypt the data file "
4557 "will be required if one or more of the original encryption or signing keys "
4558 "are not found in either of these two options' arguments. The original "
4559 "encryption or signing keys will be used when either of these options are "
4562 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4563 "for non-invoking clients "
4564 "(@pxref{Access Control}) when the recipients or signers do not match those "
4565 "that were used when the file was @code{OPEN}'ed."
4567 "The @option{--symmetric} option specifies that a new data file be "
4568 "conventionally encrypted. These types of data files do not use a recipient "
4569 "public key but may be signed by using the @option{--sign-keyid} or "
4570 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4571 "symmtrically encrypted data file, leave the option value empty. Note that "
4572 "you cannot change encryption schemes once a data file has been saved."
4575 new_command("ISCACHED", 1, 0, 0, iscached_command
, _(
4576 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4577 "Determines the file cache status of the specified @var{filename}. "
4578 "The default is to test whether the filename is cached in memory. Passing "
4579 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4580 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4581 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4582 "Both the @option{--agent} and @option{--sign} options require an opened data "
4585 "An @emph{OK} response is returned if the specified @var{filename} is found "
4586 "in the cache. If not found in the cache but exists on the filesystem "
4587 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4590 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4591 "the file exists; it does not need to be opened nor cached. The lock will be "
4592 "released when the client exits or sends the @code{UNLOCK} command "
4593 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4596 new_command("CLEARCACHE", 1, 1, 0, clearcache_command
, _(
4597 "CLEARCACHE [<filename>]\n"
4598 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4599 "this will also clear any @command{gpg-agent} cached keys which may cause "
4600 "problems if another data file shares the same keys as @var{filename}."
4602 "When clearing all cache entries a permissions test is done against the "
4603 "current client based on the @var{access} configuration parameter in a "
4604 "@var{filename} section. Both a cache entry may be cleared and an error "
4605 "returned depending on cached data files and client permissions."
4608 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command
, _(
4609 "CACHETIMEOUT <filename> <seconds>\n"
4610 "The time in @var{seconds} until @var{filename} will be removed from the "
4611 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4612 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4613 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4617 new_command("LIST", 0, 1, 0, list_command
, _(
4618 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4619 "If no element path is given then a newline separated list of root elements "
4620 "is returned with a data response. If given, then children of the specified "
4621 "element path are returned."
4623 "Each element path "
4624 "returned will have zero or more flags appened to it. These flags are "
4625 "delimited from the element path by a single space character. A flag itself "
4626 "is a single character. Flag @code{P} indicates that access to the element "
4627 "is denied. Flag @code{+} indicates that there are child nodes of "
4628 "the current element path. Flag @code{E} indicates that an element of the "
4629 "element path contained in a @var{target} attribute could not be found. Flag "
4630 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4631 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4632 "then an element path, is the element path of the @var{target} attribute "
4633 "contained in the current element."
4635 "When a specified element path contains an error, beit from the final "
4636 "element in the path or any previous element, the path is still shown but "
4637 "will contain the error flag for the element with the error. Determining "
4638 "the actual element which contains the error is up to the client. This can be "
4639 "done by traversing the final element up to parent elements that contain the "
4642 "The option @option{--recurse} may be used to list the entire element tree "
4643 "for a specified element path or the entire tree for all root elements."
4645 "When the @option{--inquire} option is passed then all remaining non-option "
4646 "arguments are retrieved via a server @emph{INQUIRE}."
4649 new_command("REALPATH", 0, 1, 0, realpath_command
, _(
4650 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4651 "Resolves all @code{target} attributes of the specified element path and "
4652 "returns the result with a data response. @xref{Target Attribute}, for details."
4654 "When the @option{--inquire} option is passed then all remaining non-option "
4655 "arguments are retrieved via a server @emph{INQUIRE}."
4658 new_command("STORE", 0, 1, 0, store_command
, _(
4659 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4660 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4662 "Creates a new element path or modifies the @var{content} of an existing "
4663 "element. If only a single element is specified then a new root element is "
4664 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4665 "set to the final @key{TAB} delimited element. If no @var{content} is "
4666 "specified after the final @key{TAB}, then the content of the existing "
4667 "element will be removed; or will be empty if creating a new element."
4669 "The only restriction of an element name is that it not contain whitespace "
4670 "characters. There is no other whitespace between the @key{TAB} delimited "
4671 "elements. It is recommended that the content of an element be base64 encoded "
4672 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4673 "parsing and @command{pwmd} syntax errors."
4676 new_command("RENAME", 0, 1, 0, rename_command
, _(
4677 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4678 "Renames the specified @var{element} to the new @var{value}. If an element of "
4679 "the same name as the @var{value} already exists it will be overwritten."
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("COPY", 0, 1, 0, copy_command
, _(
4686 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4687 "Copies the entire element tree starting from the child node of the source "
4688 "element, to the destination element path. If the destination element path "
4689 "does not exist then it will be created; otherwise it is overwritten."
4691 "Note that attributes from the source element are merged into the "
4692 "destination element when the destination element path exists. When an "
4693 "attribute of the same name exists in both the source and destination "
4694 "elements then the destination attribute will be updated to the source "
4697 "When the @option{--inquire} option is passed then all remaining non-option "
4698 "arguments are retrieved via a server @emph{INQUIRE}."
4701 new_command("MOVE", 0, 1, 0, move_command
, _(
4702 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4703 "Moves the source element path to the destination element path. If the "
4704 "destination is not specified then it will be moved to the root node of the "
4705 "document. If the destination is specified and exists then it will be "
4706 "overwritten; otherwise non-existing elements of the destination element "
4707 "path will be created."
4709 "When the @option{--inquire} option is passed then all remaining non-option "
4710 "arguments are retrieved via a server @emph{INQUIRE}."
4713 new_command("DELETE", 0, 1, 0, delete_command
, _(
4714 "DELETE [--inquire] element[<TAB>child[..]]\n"
4715 "Removes the specified element path and all of its children. This may break "
4716 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4717 "refers to this element or any of its children."
4719 "When the @option{--inquire} option is passed then all remaining non-option "
4720 "arguments are retrieved via a server @emph{INQUIRE}."
4723 new_command("GET", 0, 1, 0, get_command
, _(
4724 "GET [--inquire] element[<TAB>child[..]]\n"
4725 "Retrieves the content of the specified element. The content is returned "
4726 "with a data response."
4728 "When the @option{--inquire} option is passed then all remaining non-option "
4729 "arguments are retrieved via a server @emph{INQUIRE}."
4732 new_command("ATTR", 0, 1, 0, attr_command
, _(
4733 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4735 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4736 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4737 "element. When no @var{value} is specified any existing value will be removed."
4739 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4740 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4741 "or @code{target} an error is returned. Use the @command{DELETE} command "
4742 "(@pxref{DELETE}) instead."
4744 "@item ATTR LIST element[<TAB>child[..]]\n"
4745 " Retrieves a newline separated list of attributes names and values "
4746 "from the specified element. Each attribute name and value is space delimited."
4748 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4749 " Retrieves the value of an @var{attribute} from an element."
4752 "When the @option{--inquire} option is passed then all remaining non-option "
4753 "arguments are retrieved via a server @emph{INQUIRE}."
4755 "@xref{Target Attribute}, for details about this special attribute and also "
4756 "@pxref{Other Attributes} for other attributes that are handled specially "
4757 "by @command{pwmd}."
4760 new_command("XPATH", 0, 1, 0, xpath_command
, _(
4761 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4762 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4763 "specified it is assumed the expression is a request to return a result. "
4764 "Otherwise, the result is set to the @var{value} argument and the document is "
4765 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4766 "is assumed to be empty and the document is updated. For example:"
4769 "XPATH //element[@@_name='password']@key{TAB}\n"
4772 "would clear the content of all @var{password} elements in the data file "
4773 "while leaving off the trailing @key{TAB} would return all @var{password} "
4774 "elements in @abbr{XML} format."
4776 "When the @option{--inquire} option is passed then all remaining non-option "
4777 "arguments are retrieved via a server @emph{INQUIRE}."
4779 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4780 "expression syntax."
4783 new_command("XPATHATTR", 0, 1, 0, xpathattr_command
, _(
4784 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4785 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4786 "attributes and does not return a result. For the @var{SET} operation the "
4787 "@var{value} is optional but the field is required. If not specified then "
4788 "the attribute value will be empty. For example:"
4791 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4794 "would create a @var{password} attribute for each @var{password} element "
4795 "found in the document. The attribute value will be empty but still exist."
4797 "When the @option{--inquire} option is passed then all remaining non-option "
4798 "arguments are retrieved via a server @emph{INQUIRE}."
4800 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4801 "expression syntax."
4804 new_command("IMPORT", 0, 1, 0, import_command
, _(
4805 "IMPORT [--root=element[<TAB>child[..]]]\n"
4806 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4808 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4809 "argument is raw @abbr{XML} data. The content is created as a child of "
4810 "the element path specified with the @option{--root} option or at the "
4811 "document root when not specified. Existing elements of the same name will "
4814 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4818 new_command("DUMP", 0, 1, 0, dump_command
, _(
4820 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4821 "dumping a specific node."
4824 new_command("LOCK", 0, 0, 0, lock_command
, _(
4826 "Locks the mutex associated with the opened file. This prevents other clients "
4827 "from sending commands to the same opened file until the client "
4828 "that sent this command either disconnects or sends the @code{UNLOCK} "
4829 "command. @xref{UNLOCK}."
4832 new_command("UNLOCK", 1, 0, 0, unlock_command
, _(
4834 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4835 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4836 "@pxref{ISCACHED})."
4839 new_command("GETCONFIG", 1, 1, 0, getconfig_command
, _(
4840 "GETCONFIG [filename] <parameter>\n"
4841 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4842 "data response. If no file has been opened then the value for @var{filename} "
4843 "or the default from the @var{global} section will be returned. If a file "
4844 "has been opened and no @var{filename} is specified, the value previously "
4845 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4848 new_command("OPTION", 1, 1, 0, option_command
, _(
4849 "OPTION <NAME>=[<VALUE>]\n"
4850 "Sets a client option @var{name} to @var{value}. The value for an option is "
4851 "kept for the duration of the connection with the exception of the "
4852 "@command{pinentry} options which are defaults for all future connections "
4853 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4856 "@item DISABLE-PINENTRY\n"
4857 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4858 "server inquire is sent to the client to obtain the passphrase. This option "
4859 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4860 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4861 "to use a @command{pinentry}."
4864 "Set or unset the X11 display to use when prompting for a passphrase."
4867 "Set the terminal device path to use when prompting for a passphrase."
4870 "Set the terminal type for use with @option{TTYNAME}."
4873 "Associates the thread ID of the connection with the specified textual "
4874 "representation. Useful for debugging log messages. May not contain whitespace."
4876 "@item LOCK-TIMEOUT\n"
4877 "When not @code{0}, the duration in tenths of a second to wait for the file "
4878 "mutex which has been locked by another thread to be released before returning "
4879 "an error. When @code{-1} the error will be returned immediately."
4883 new_command("LS", 1, 1, 0, ls_command
, _(
4885 "Returns a newline separated list of data files stored in the data directory "
4886 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4889 new_command("RESET", 1, 1, 0, NULL
, _(
4891 "Closes the currently opened file but keeps any previously set client options "
4895 new_command("NOP", 1, 1, 0, NULL
, _(
4897 "Does nothing. Always returns successfully."
4900 /* !END-HELP-TEXT! */
4901 new_command ("CANCEL", 1, 1, 0, NULL
, NULL
);
4902 new_command ("END", 1, 1, 0, NULL
, NULL
);
4903 new_command ("BYE", 1, 1, 0, NULL
, NULL
);
4906 for (i
= 0; command_table
[i
]; i
++);
4907 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4912 register_commands (assuan_context_t ctx
)
4916 for (; command_table
[i
]; i
++)
4918 if (!command_table
[i
]->handler
)
4921 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4922 command_table
[i
]->handler
,
4923 command_table
[i
]->help
);
4928 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4932 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4936 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4940 return assuan_register_post_cmd_notify (ctx
, command_finalize
);