2 Copyright (C) 2006-2022 Ben Kibbey <bjk@luxsci.net>
4 This file is part of pwmd.
6 Pwmd is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation.
10 Pwmd is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
29 #include <sys/types.h>
43 #include "pwmd-error.h"
48 #include "util-misc.h"
57 /* These are command option flags. */
58 #define OPT_INQUIRE 0x0001
59 #define OPT_NO_PASSPHRASE 0x0002
60 #define OPT_ASK 0x0004
61 #define OPT_LIST_RECURSE 0x0008
62 #define OPT_VERBOSE 0x0010
63 #define OPT_LOCK 0x0020
64 #define OPT_LOCK_ON_OPEN 0x0040
65 #define OPT_SIGN 0x0080
66 #define OPT_LIST_ALL 0x0100
67 #define OPT_DATA 0x0200
68 #define OPT_NO_AGENT 0x0400
69 #define OPT_SECRET 0x0800
70 #define OPT_INQUIRE_KEYID 0x1000
71 #define OPT_SYMMETRIC 0x4000
72 #define OPT_NO_SIGNER 0x8000
73 #define OPT_HTML 0x10000
74 #define OPT_CACHE_AGENT 0x20000
75 #define OPT_CACHE_SIGN 0x40000
76 #define OPT_KEYINFO_LEARN 0x80000
77 #define OPT_SEXP 0x100000
79 #define FLOCK_TYPE_NONE 0
80 #define FLOCK_TYPE_SH 0x0001
81 #define FLOCK_TYPE_EX 0x0002
82 #define FLOCK_TYPE_KEEP 0x0004
84 static char env_display
[256];
85 static char env_gpg_tty
[256];
86 static char env_term
[256];
87 static struct command_table_s
**command_table
;
89 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
90 static gpg_error_t
validate_checksum (struct client_s
*, const char *filename
,
91 struct cache_data_s
*, unsigned char **,
93 static gpg_error_t
update_checksum (struct client_s
*client
,
94 unsigned char *from_crc
, size_t crclen
);
95 static gpg_error_t
command_startup (assuan_context_t ctx
, const char *name
);
96 static void command_finalize (assuan_context_t ctx
, gpg_error_t rc
);
98 /* When 'status' is true the 'self' field of the status line will be false
99 * because we never send the STATE status message to the same client that
102 build_client_info_line (struct client_thread_s
*thd
, int status
)
104 char *uid
, *username
= NULL
;
109 uid
= str_asprintf("#%s", thd
->tls
->fp
);
112 uid
= str_asprintf("%u", thd
->peer
->uid
);
113 username
= get_username (thd
->peer
->uid
);
116 uid
= str_asprintf("%u", thd
->peer
->uid
);
117 username
= get_username (thd
->peer
->uid
);
119 line
= str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
121 thd
->name
? thd
->name
: "-",
122 thd
->cl
&& thd
->cl
->filename
123 && (thd
->cl
->flags
& FLAG_OPEN
)
124 ? thd
->cl
->filename
: "/",
126 thd
->remote
? thd
->peeraddr
: "-",
130 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
131 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
134 thd
->remote
? "-" : username
,
147 update_client_state (struct client_s
*client
, unsigned s
)
149 MUTEX_LOCK (&cn_mutex
);
150 if (client
->thd
->state
== s
)
152 MUTEX_UNLOCK (&cn_mutex
);
155 client
->thd
->state
= s
;
156 MUTEX_UNLOCK (&cn_mutex
);
158 if (client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
160 char *line
= build_client_info_line (client
->thd
, 1);
162 pthread_cleanup_push (xfree
, line
);
164 send_status_all_not_self (STATUS_STATE
, "%s", line
);
165 pthread_cleanup_pop (1);
170 unlock_file_mutex (struct client_s
*client
, int remove
)
174 // OPEN: keep the lock for the same file being reopened.
175 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
178 if (!(client
->flags
& FLAG_HAS_LOCK
))
179 return GPG_ERR_NOT_LOCKED
;
181 rc
= cache_unlock_mutex (client
->filename
, remove
);
183 rc
= GPG_ERR_INV_STATE
;
185 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
191 lock_file_mutex (struct client_s
*client
, int add
)
194 long timeout
= config_get_long (client
->filename
, "cache_timeout");
196 if (client
->flags
& FLAG_HAS_LOCK
)
199 rc
= cache_lock_mutex (client
->ctx
, client
->filename
,
200 client
->lock_timeout
, add
, timeout
);
202 client
->flags
|= FLAG_HAS_LOCK
;
208 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
211 int type
= !cmd
->flock_type
|| (cmd
->flock_type
& FLOCK_TYPE_SH
)
214 if (!(client
->flags
& FLAG_OPEN
))
215 return GPG_ERR_INV_STATE
;
217 rc
= lock_file_mutex (client
, 0);
218 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
221 rc
= lock_flock (client
->ctx
, client
->filename
, type
, &client
->flock_fd
);
224 rc
= validate_checksum (client
, client
->filename
, NULL
, NULL
, NULL
, 0);
225 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
228 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
230 else if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
233 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
234 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
235 unlock_file_mutex (client
, 0);
237 if (rc
|| !(cmd
->flock_type
& FLOCK_TYPE_KEEP
))
238 unlock_flock (&client
->flock_fd
);
244 parse_xml (assuan_context_t ctx
, int new)
246 struct client_s
*client
= assuan_get_pointer (ctx
);
247 int cached
= client
->doc
!= NULL
;
252 client
->doc
= xml_new_document ();
258 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
259 client
->crypto
->plaintext
= result
;
260 client
->crypto
->plaintext_size
= len
;
261 if (!client
->crypto
->plaintext
)
263 xmlFreeDoc (client
->doc
);
272 rc
= xml_parse_doc ((char *) client
->crypto
->plaintext
,
273 client
->crypto
->plaintext_size
,
274 (xmlDocPtr
*)&client
->doc
);
280 free_client (struct client_s
*client
)
282 cache_plaintext_release (&client
->doc
);
284 xfree (client
->filename
);
285 xfree (client
->last_error
);
286 crypto_free (client
->crypto
);
287 client
->crypto
= NULL
;
291 reset_client (struct client_s
*client
)
293 assuan_context_t ctx
= client
->ctx
;
294 struct client_thread_s
*thd
= client
->thd
;
295 long lock_timeout
= client
->lock_timeout
;
296 xmlErrorPtr xml_error
= client
->xml_error
;
297 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
298 int flock_fd
= client
->flock_fd
;
299 struct bulk_cmd_s
*bulk_p
= client
->bulk_p
;
301 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
303 free_client (client
);
304 memset (client
, 0, sizeof (struct client_s
));
305 client
->flock_fd
= flock_fd
;
306 client
->xml_error
= xml_error
;
309 client
->lock_timeout
= lock_timeout
;
310 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
311 client
->bulk_p
= bulk_p
;
321 strv_free ((char **) arg
);
325 parse_open_opt_lock (void *data
, void *value
)
327 struct client_s
*client
= data
;
330 client
->opts
|= OPT_LOCK_ON_OPEN
;
335 parse_opt_inquire (void *data
, void *value
)
337 struct client_s
*client
= data
;
340 client
->opts
|= OPT_INQUIRE
;
345 update_checksum (struct client_s
*client
, unsigned char *from_crc
,
350 struct cache_data_s
*cdata
;
355 rc
= get_checksum (client
->filename
, &crc
, &len
);
366 client
->crc
= xmalloc (len
);
367 memcpy (client
->crc
, crc
, len
);
368 pthread_cleanup_push (xfree
, crc
);
369 cdata
= cache_get_data (client
->filename
, NULL
);
370 pthread_cleanup_pop (0);
374 cdata
->crc
= xmalloc (len
);
375 memcpy (cdata
->crc
, crc
, len
);
384 validate_checksum (struct client_s
*client
, const char *filename
,
385 struct cache_data_s
*cdata
, unsigned char **r_crc
,
386 size_t *r_crclen
, int from_cdata
)
393 if (cdata
&& !cdata
->crc
)
394 return GPG_ERR_CHECKSUM
;
396 rc
= get_checksum (filename
, &crc
, &len
);
401 n
= memcmp (client
->crc
, crc
, len
);
402 else if ((client
->flags
& FLAG_NEW
) && cache_get_data (filename
, NULL
))
405 if ((!n
&& cdata
) || (from_cdata
&& cdata
&& crc
))
406 n
= memcmp (cdata
->crc
, crc
, len
);
416 return n
? GPG_ERR_CHECKSUM
: 0;
420 open_command (assuan_context_t ctx
, char *line
)
423 struct client_s
*client
= assuan_get_pointer (ctx
);
425 assuan_peercred_t peer
;
426 struct cache_data_s
*cdata
= NULL
;
428 unsigned char *crc
= NULL
;
431 struct argv_s
*args
[] = {
432 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
436 rc
= parse_options (&line
, args
, client
, 1);
438 return send_error (ctx
, rc
);
440 rc
= do_validate_peer (ctx
, line
, &peer
, NULL
);
441 if (rc
== GPG_ERR_FORBIDDEN
)
443 rc
= peer_is_invoker (client
);
444 if (rc
== GPG_ERR_EACCES
)
445 rc
= GPG_ERR_FORBIDDEN
;
449 return send_error (ctx
, rc
);
451 if (!valid_filename (line
))
452 return send_error (ctx
, GPG_ERR_INV_VALUE
);
454 /* This client may have locked a different file with ISCACHED --lock than
455 * the current filename. This will remove that lock. */
456 same_file
= client
->filename
&& !strcmp (line
, client
->filename
);
457 if (client
->flags
& FLAG_OPEN
||
458 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
460 unsigned opts
= client
->opts
;
461 unsigned flags
= client
->flags
;
464 client
->flags
|= FLAG_KEEP_LOCK
;
466 /* Another client wrote to the same data file as currently opened. */
467 cdata
= cache_get_data (client
->filename
, NULL
);
468 if (cdata
&& cdata
->crc
)
470 /* Remove cache entry for the new file that hadn't been written. */
471 else if (client
->flags
& FLAG_NEW
)
472 cache_clear (NULL
, client
->filename
, 0, 0);
475 reset_client (client
);
477 client
->flags
|= flags
;
478 client
->flags
&= ~(FLAG_LOCK_CMD
);
480 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
483 xfree (client
->filename
);
484 client
->filename
= str_dup (line
);
485 if (!client
->filename
)
486 return send_error(ctx
, GPG_ERR_ENOMEM
);
488 /* Need to lock the mutex here because file_modified() cannot without
489 * knowing the filename. */
490 rc
= lock_file_mutex (client
, 1);
492 client
->flags
&= ~FLAG_OPEN
;
496 rc
= lock_flock (ctx
, client
->filename
, LOCK_SH
, &client
->flock_fd
);
497 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
503 char *keyfile
= config_get_string (client
->filename
, "passphrase_file");
505 rc
= crypto_init (&client
->crypto
, client
->ctx
, client
->filename
,
506 client
->flags
& FLAG_NO_PINENTRY
, keyfile
);
510 rc
= open_check_file (client
->filename
, NULL
, NULL
, 1);
513 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_ENOENT
)
522 if (config_get_boolean ("global", "strict_open"))
524 rc
= peer_is_invoker (client
);
525 if (rc
== GPG_ERR_EACCES
)
526 rc
= GPG_ERR_FORBIDDEN
;
531 client
->flags
|= FLAG_NEW
;
532 // data file disappeared. clear the cache entry.
533 cache_clear (NULL
, client
->filename
, 1, 1);
540 cdata
= cache_get_data (client
->filename
, &defer
);
542 && !cache_plaintext_get (client
->filename
, &client
->doc
))
544 rc
= validate_checksum (client
, client
->filename
, cdata
, &crc
,
548 log_write ("%s: %s", client
->filename
,
550 cache_plaintext_release (&client
->doc
);
551 if (rc
== GPG_ERR_CHECKSUM
)
553 cache_clear (NULL
, client
->filename
, 1, 1);
560 if (!rc
&& client
->thd
->remote
561 && config_get_boolean (client
->filename
, "tcp_require_key")
562 && !(client
->flags
& FLAG_NEW
))
564 rc
= crypto_try_decrypt (client
,
565 (client
->flags
& FLAG_NO_PINENTRY
));
570 strv_free (client
->crypto
->pubkey
);
571 client
->crypto
->pubkey
= NULL
;
573 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
575 xfree (client
->crypto
->sigkey
);
576 client
->crypto
->sigkey
= NULL
;
578 client
->crypto
->sigkey
= str_dup (cdata
->sigkey
);
584 else if (cdata
&& cdata
->doc
) // cached document
586 if (!rc
&& !(client
->flags
& FLAG_NEW
))
588 rc
= validate_checksum (client
, client
->filename
, cdata
, &crc
,
590 if (rc
== GPG_ERR_CHECKSUM
)
599 rc
= cache_iscached (client
->filename
, &defer
, 0, 0);
600 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
610 log_write ("%s: %s", client
->filename
,
611 pwmd_strerror (GPG_ERR_CHECKSUM
));
612 cache_clear (NULL
, client
->filename
, 1, 1);
614 rc
= crypto_decrypt (client
, client
->crypto
);
619 if (client
->thd
->remote
620 && config_get_boolean (client
->filename
, "tcp_require_key")
621 && !(client
->flags
& FLAG_NEW
))
622 rc
= crypto_try_decrypt (client
,
623 (client
->flags
& FLAG_NO_PINENTRY
));
629 client
->crypto
->plaintext
= cdata
->doc
;
630 client
->crypto
->plaintext_size
= cdata
->size
;
631 rc
= cache_decrypt (client
->crypto
);
637 strv_free (client
->crypto
->pubkey
);
638 client
->crypto
->pubkey
= NULL
;
640 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
642 xfree (client
->crypto
->sigkey
);
643 client
->crypto
->sigkey
= NULL
;
645 client
->crypto
->sigkey
= str_dup (cdata
->sigkey
);
651 client
->crypto
->plaintext
= NULL
;
652 client
->crypto
->plaintext_size
= 0;
656 else // existing file
659 cached
= cdata
!= NULL
;
660 rc
= crypto_decrypt (client
, client
->crypto
);
665 if (!rc
&& !plaintext
)
667 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
670 rc
= cache_encrypt (client
->crypto
);
672 cache_clear (NULL
, client
->filename
, 1, 1);
675 long timeout
= config_get_long (client
->filename
,
678 cache_free_data_once (cdata
);
679 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
680 cdata
->doc
= client
->crypto
->plaintext
;
681 cdata
->size
= client
->crypto
->plaintext_size
;
682 cdata
->pubkey
= strv_dup(client
->crypto
->pubkey
);
683 cdata
->sigkey
= client
->crypto
->sigkey
? str_dup(client
->crypto
->sigkey
) : NULL
;
684 client
->crypto
->plaintext
= NULL
;
685 client
->crypto
->plaintext_size
= 0;
687 if (cached
) // wont increment the refcount
694 cdata
->crc
= xmalloc (crclen
);
695 memcpy (cdata
->crc
, crc
, crclen
);
698 rc
= cache_set_data (client
->filename
, cdata
);
699 /* The cache entry may have been removed and cache_set_data()
700 * already sent STATUS_CACHE. */
701 if (!cache_iscached (client
->filename
, NULL
, 0, 0))
702 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
705 rc
= cache_add_file (client
->filename
, cdata
, timeout
);
708 cache_plaintext_set (client
->filename
, client
->doc
, 0);
719 client
->crc
= xmalloc (crclen
);
720 memcpy (client
->crc
, crc
, crclen
);
726 if (!rc
&& !(client
->flags
& FLAG_NEW
) && !cached
)
727 rc
= update_checksum (client
, NULL
, 0);
731 client
->flags
|= FLAG_OPEN
;
733 if (client
->flags
& FLAG_NEW
)
734 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
736 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
737 rc
= do_lock (client
, 0);
739 crypto_free_non_keys (client
->crypto
);
743 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
745 if (client
->flags
& FLAG_NEW
)
746 cache_clear (NULL
, client
->filename
, 1, 1);
748 crypto_free (client
->crypto
);
749 client
->crypto
= NULL
;
750 client
->flags
&= ~FLAG_OPEN
;
751 cache_plaintext_release (&client
->doc
);
754 return send_error (ctx
, rc
);
757 /* If not an invoking_user or is an existing file, check that the list of user
758 * supplied key ID's are in the list of current key ID's obtained when
759 * decrypting the data file.
762 permitted_to_save (struct client_s
*client
, const char **keys
,
768 if ((client
->flags
& FLAG_NEW
) || (client
->opts
& OPT_SYMMETRIC
))
771 rc
= peer_is_invoker (client
);
774 else if (rc
== GPG_ERR_EACCES
)
775 rc
= GPG_ERR_FORBIDDEN
;
780 if ((!value
&& !keys
) || ((value
&& !*value
) && (keys
&& !*keys
)))
783 for (v
= value
; v
&& *v
; v
++)
785 const char **k
, *pv
= *v
;
788 if (*pv
== '0' && *(pv
+1) == 'x')
791 for (k
= keys
; k
&& *k
; k
++)
795 if (*pk
== '0' && *(pk
+1) == 'x')
798 rc
= !strcmp (pv
, pk
) ? 0 : GPG_ERR_FORBIDDEN
;
800 rc
= !strcmp (pv
, *k
) ? 0 : GPG_ERR_FORBIDDEN
;
810 return GPG_ERR_FORBIDDEN
;
816 /* Requires that the keyid be a fingerprint in 16 byte form. */
818 parse_save_opt_keyid_common (struct client_s
*client
, const char **list
,
819 const char *value
, char ***dst
)
826 keys
= str_split (value
, ",", 0);
828 return GPG_ERR_ENOMEM
;
831 rc
= crypto_keyid_to_16b (keys
);
833 rc
= permitted_to_save (client
, list
, (const char **)keys
);
844 parse_save_opt_keyid (void *data
, void *value
)
846 struct client_s
*client
= data
;
847 const char *str
= value
;
851 rc
= parse_save_opt_keyid_common (client
,
852 (const char **)client
->crypto
->pubkey
,
857 client
->crypto
->save
.pubkey
= dst
;
862 parse_save_opt_sign_keyid (void *data
, void *value
)
864 struct client_s
*client
= data
;
865 const char *str
= value
;
870 if (client
->crypto
->sigkey
)
872 if (!strv_printf (&tmp
, "%s", client
->crypto
->sigkey
))
873 return GPG_ERR_ENOMEM
;
876 rc
= parse_save_opt_keyid_common (client
, (const char **)tmp
, str
, &dst
);
882 client
->opts
|= OPT_NO_SIGNER
;
884 client
->crypto
->save
.sigkey
= str_dup (*dst
);
891 parse_save_opt_inquire_keyid (void *data
, void *value
)
893 struct client_s
*client
= data
;
897 if (!(client
->flags
& FLAG_NEW
))
899 gpg_error_t rc
= peer_is_invoker (client
);
902 return rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
;
905 client
->opts
|= OPT_INQUIRE_KEYID
;
910 parse_save_opt_symmetric (void *data
, void *value
)
912 struct client_s
*client
= data
;
915 client
->opts
|= OPT_SYMMETRIC
;
920 parse_genkey_opt_userid (void *data
, void *value
)
922 struct client_s
*client
= data
;
924 if (!(client
->flags
& FLAG_NEW
))
926 gpg_error_t rc
= peer_is_invoker (client
);
929 return rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
;
932 client
->crypto
->save
.userid
= str_dup (value
);
933 return client
->crypto
->save
.userid
? 0 : GPG_ERR_ENOMEM
;
937 parse_genkey_opt_algo (void *data
, void *value
)
939 struct client_s
*client
= data
;
941 client
->crypto
->save
.algo
= str_dup (value
);
942 return client
->crypto
->save
.algo
? 0 : GPG_ERR_ENOMEM
;
946 parse_genkey_opt_no_expire (void *data
, void *value
)
948 struct client_s
*client
= data
;
950 client
->crypto
->save
.flags
|= GPGME_CREATE_NOEXPIRE
;
955 parse_genkey_opt_expire (void *data
, void *value
)
957 struct client_s
*client
= data
;
963 t
= strtoul (value
, &p
, 10);
965 if (!errno
&& p
&& *p
)
966 rc
= GPG_ERR_INV_VALUE
;
967 else if (t
== ULONG_MAX
)
968 rc
= GPG_ERR_INV_VALUE
;
970 rc
= gpg_error_from_syserror ();
972 client
->crypto
->save
.expire
= t
;
978 parse_genkey_opt_no_passphrase (void *data
, void *value
)
980 struct client_s
*client
= data
;
983 client
->crypto
->save
.flags
|= GPGME_CREATE_NOPASSWD
;
987 /* Tests that the keys in new_keys are also in old_keys. */
989 compare_keys (char **new_keys
, char **old_keys
)
993 if (!old_keys
|| !*old_keys
)
996 crypto_keyid_to_16b (new_keys
);
998 for (o
= old_keys
; *o
; o
++)
1002 for (n
= new_keys
; *n
; n
++)
1004 if (!strcmp (*n
, *o
))
1009 return GPG_ERR_NOT_FOUND
;
1016 inquire_keyid (struct client_s
*client
)
1019 unsigned char *result
= NULL
;
1021 const char *s
= "KEYID";
1025 orig
= &client
->crypto
->pubkey
;
1026 save
= &client
->crypto
->save
.pubkey
;
1027 rc
= assuan_inquire (client
->ctx
, s
, &result
, &len
, 0);
1032 rc
= parse_save_opt_keyid_common (client
, (const char **)*orig
,
1033 (char *)result
, &dst
);
1043 /* When a key lookup in gpg-agent's cache fails, the agent tries the last
1044 * successful key to be unlocked. This prevents pwmd from successfully
1045 * clearing a signing key since the previous key, which more than likely
1046 * belongs to the same keypair as the encryption/decryption key, will have the
1047 * passphrase cached and therefore the signing key also cached. So we need to
1048 * clear both the signing and encryption keys to get the effect of requiring a
1049 * passphrase when generating a new keypair for an existing data file, or when
1050 * the "require_save_key" configuration parameter is set. This parameter is
1051 * mostly a failsafe of the gpg-agent option --ignore-cache-for-signing since
1052 * some/most/all users may fail to set it.
1055 save_command (assuan_context_t ctx
, char *line
)
1057 struct client_s
*client
= assuan_get_pointer (ctx
);
1058 struct cache_data_s
*cdata
= NULL
;
1060 gpg_error_t rc
= 0, cache_rc
= 0;
1062 struct argv_s
*args
[] = {
1063 &(struct argv_s
) {"keyid", OPTION_TYPE_ARG
, parse_save_opt_keyid
},
1064 &(struct argv_s
) {"inquire-keyid", OPTION_TYPE_NOARG
,
1065 parse_save_opt_inquire_keyid
},
1066 &(struct argv_s
) {"sign-keyid", OPTION_TYPE_OPTARG
,
1067 parse_save_opt_sign_keyid
},
1068 &(struct argv_s
) {"symmetric", OPTION_TYPE_NOARG
,
1069 parse_save_opt_symmetric
},
1073 crypto_free_save (&client
->crypto
->save
);
1074 client
->crypto
->save
.expire
= DEFAULT_EXPIRE
;
1076 rc
= crypto_is_symmetric (client
->filename
);
1077 if (!rc
|| rc
== GPG_ERR_BAD_DATA
|| rc
== GPG_ERR_ENOENT
)
1081 client
->opts
|= OPT_SYMMETRIC
;
1084 else if (rc
== GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
1091 return send_error (ctx
, rc
);
1093 rc
= parse_options (&line
, args
, client
, 0);
1095 return send_error (ctx
, rc
);
1097 if (config_get_boolean (client
->filename
, "require_save_key")
1098 || (client
->opts
& OPT_SYMMETRIC
))
1099 client
->opts
|= OPT_ASK
;
1101 rc
= open_check_file (client
->filename
, NULL
, NULL
, 1);
1102 if (rc
&& gpg_err_code (rc
) != GPG_ERR_ENOENT
)
1104 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
1105 return send_error (ctx
, rc
);
1109 cache_rc
= rc
= cache_iscached (client
->filename
, &defer
, 0, 1);
1111 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
1113 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
1117 return send_error (ctx
, rc
);
1119 /* Specifying both a recipient and symmetric encryption is an error. */
1120 if ((client
->opts
& OPT_INQUIRE_KEYID
) && (client
->opts
& OPT_SYMMETRIC
))
1121 return send_error (ctx
, GPG_ERR_CONFLICT
);
1122 /* Existing file with a recipient and wanting symmetric is an error. */
1123 else if ((client
->opts
& OPT_SYMMETRIC
) && client
->crypto
->save
.pubkey
)
1124 return send_error (ctx
, GPG_ERR_CONFLICT
);
1125 else if (client
->crypto
->save
.pubkey
&& (client
->opts
& OPT_INQUIRE_KEYID
))
1126 return send_error (ctx
, GPG_ERR_CONFLICT
);
1127 else if (!sym
&& (client
->opts
& OPT_SYMMETRIC
) && !(client
->flags
& FLAG_NEW
))
1128 return send_error (ctx
, GPG_ERR_CONFLICT
);
1129 /* New file that is not symmetric without either a recipient or signer. */
1130 else if ((client
->flags
& FLAG_NEW
) && !(client
->opts
& OPT_SYMMETRIC
)
1131 && (!client
->crypto
->save
.pubkey
|| !client
->crypto
->save
.sigkey
))
1132 return send_error (ctx
, GPG_ERR_SYNTAX
);
1133 else if (client
->opts
& OPT_INQUIRE_KEYID
)
1134 rc
= inquire_keyid (client
);
1138 client
->crypto
->keyfile
= config_get_string (client
->filename
,
1140 rc
= crypto_init_ctx (client
->crypto
, (client
->flags
& FLAG_NO_PINENTRY
),
1141 client
->crypto
->keyfile
);
1144 if (!rc
&& (client
->flags
& FLAG_NEW
))
1146 /* Require --keyid when --sign-keyid exists to keep KEYINFO
1148 if (!client
->crypto
->save
.pubkey
&& client
->crypto
->save
.sigkey
1149 && !(client
->opts
& OPT_SYMMETRIC
))
1150 rc
= GPG_ERR_NO_PUBKEY
;
1154 cdata
= cache_get_data (client
->filename
, NULL
);
1157 if (client
->crypto
->save
.pubkey
)
1158 rc
= compare_keys (client
->crypto
->save
.pubkey
, cdata
->pubkey
);
1160 /* Always allow a signer for symmetric data files. */
1161 if (!rc
&& client
->crypto
->save
.sigkey
1162 && !(client
->opts
& OPT_SYMMETRIC
))
1163 rc
= !strcmp (client
->crypto
->save
.sigkey
, cdata
->sigkey
)
1164 ? 0 : GPG_ERR_NOT_FOUND
;
1166 /* Prevent saving to a recipient who is not in the original recipient
1167 * list without a passphrase. */
1168 if (rc
|| (client
->crypto
->sigkey
&& (client
->opts
& OPT_NO_SIGNER
)))
1169 client
->opts
|= OPT_ASK
;
1171 if (rc
== GPG_ERR_NOT_FOUND
)
1173 rc
= peer_is_invoker (client
);
1174 if (rc
== GPG_ERR_EACCES
)
1175 rc
= GPG_ERR_FORBIDDEN
;
1178 if (!client
->crypto
->save
.pubkey
1179 && !(client
->opts
& OPT_SYMMETRIC
))
1180 client
->crypto
->save
.pubkey
= strv_dup (cdata
->pubkey
);
1182 if (!rc
&& !client
->crypto
->save
.sigkey
&& cdata
->sigkey
1183 && !(client
->opts
& OPT_NO_SIGNER
))
1184 client
->crypto
->save
.sigkey
= str_dup (cdata
->sigkey
);
1185 else if (!rc
&& !client
->crypto
->save
.sigkey
1186 && (client
->opts
& OPT_NO_SIGNER
)
1187 && !(client
->opts
& OPT_SYMMETRIC
))
1188 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
1192 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->pubkey
);
1193 client
->crypto
->save
.sigkey
= str_dup (client
->crypto
->sigkey
);
1197 if (!rc
&& !(client
->flags
& FLAG_NEW
))
1199 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1200 if (client
->opts
& OPT_SYMMETRIC
)
1201 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
1203 if (client
->opts
& OPT_ASK
)
1205 rc
= cache_clear_agent_keys (client
->filename
, 1, 1);
1207 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1211 if (!rc
&& client
->opts
& OPT_SYMMETRIC
)
1212 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
1215 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1219 xmlNodePtr root
= xmlDocGetRootElement (client
->doc
);
1221 rc
= xml_add_attribute (client
, root
, "_version", PACKAGE_VERSION
);
1228 xmlDocDumpFormatMemory (client
->doc
, &client
->crypto
->plaintext
, &size
,
1231 client
->crypto
->plaintext_size
= (size_t) size
;
1233 rc
= GPG_ERR_ENOMEM
;
1237 client
->crypto
->flags
|= (client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_SYMMETRIC
: 0;
1238 client
->crypto
->flags
|= (client
->flags
& FLAG_NEW
) ? CRYPTO_FLAG_NEWFILE
: 0;
1239 client
->crypto
->flags
|= !(client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_PASSWD_SIGN
: 0;
1240 rc
= crypto_encrypt (client
, client
->crypto
);
1241 client
->crypto
->flags
&= ~CRYPTO_FLAG_SYMMETRIC
;
1246 unsigned char *crc
= NULL
;
1249 rc
= crypto_write_file (client
->crypto
, &crc
, &crclen
);
1250 pthread_cleanup_push ((void *)xfree
, crc
);
1254 cdata
= cache_get_data (client
->filename
, NULL
);
1256 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1261 rc
= cache_encrypt (client
->crypto
);
1264 cache_clear (NULL
, client
->filename
, 1, 1);
1265 client
->flags
&= ~(FLAG_NEW
);
1267 strv_free (client
->crypto
->pubkey
);
1268 client
->crypto
->pubkey
= NULL
;
1269 if (client
->crypto
->save
.pubkey
)
1270 client
->crypto
->pubkey
= strv_dup (client
->crypto
->save
.pubkey
);
1272 xfree (client
->crypto
->sigkey
);
1273 client
->crypto
->sigkey
= NULL
;
1274 if (client
->crypto
->save
.sigkey
)
1275 client
->crypto
->sigkey
= str_dup (client
->crypto
->save
.sigkey
);
1281 long timeout
= config_get_long (client
->filename
, "cache_timeout");
1282 xmlDocPtr doc
= xmlCopyDoc (client
->doc
, 1);
1285 rc
= GPG_ERR_ENOMEM
;
1289 cache_plaintext_release (&client
->doc
);
1290 strv_free (cdata
->pubkey
);
1291 cdata
->pubkey
= client
->crypto
->save
.pubkey
;
1292 client
->crypto
->save
.pubkey
= NULL
;
1294 xfree (cdata
->sigkey
);
1295 cdata
->sigkey
= client
->crypto
->save
.sigkey
;
1296 client
->crypto
->save
.sigkey
= NULL
;
1301 /* cache_encrypt() does in-place encryption */
1303 cdata
->doc
= client
->crypto
->plaintext
;
1304 client
->crypto
->plaintext
= NULL
;
1305 cdata
->size
= client
->crypto
->plaintext_size
;
1306 client
->crypto
->plaintext_size
= 0;
1309 cache_plaintext_set (client
->filename
, client
->doc
, 0);
1311 /* Update in case the cache entry expires the next SAVE may
1312 * not have any known keys. */
1313 strv_free (client
->crypto
->pubkey
);
1314 client
->crypto
->pubkey
= NULL
;
1316 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
1318 xfree (client
->crypto
->sigkey
);
1319 client
->crypto
->sigkey
= NULL
;
1321 client
->crypto
->sigkey
= str_dup (cdata
->sigkey
);
1323 if (!cache_rc
) // wont increment refcount
1324 rc
= cache_set_data (client
->filename
, cdata
);
1326 rc
= cache_add_file (client
->filename
, cdata
, timeout
);
1329 if (!rc
&& (cache_rc
|| (client
->flags
& FLAG_NEW
)))
1330 send_status_all (STATUS_CACHE
, NULL
);
1334 rc
= update_checksum (client
, crc
, crclen
);
1335 client
->flags
&= ~(FLAG_NEW
);
1339 pthread_cleanup_pop (1);
1341 client
->did_cow
= 0;
1345 crypto_free_non_keys (client
->crypto
);
1346 return send_error (ctx
, rc
);
1350 parse_genkey_opt_usage (void *data
, void *v
)
1352 struct client_s
*client
= data
;
1355 if (!value
|| !*value
)
1356 return GPG_ERR_INV_VALUE
;
1357 else if (!strcmp (value
, "sign"))
1358 client
->crypto
->save
.flags
|= GPGME_CREATE_SIGN
;
1359 else if (!strcmp (value
, "encrypt"))
1360 client
->crypto
->save
.flags
|= GPGME_CREATE_ENCR
;
1361 else if (!strcmp (value
, "default"))
1362 client
->crypto
->save
.flags
&= ~(GPGME_CREATE_ENCR
|GPGME_CREATE_SIGN
);
1364 return GPG_ERR_INV_VALUE
;
1370 parse_genkey_opt_subkey_of (void *data
, void *v
)
1373 struct client_s
*client
= data
;
1375 char *tmp
[] = { value
, NULL
};
1376 gpgme_key_t
*keys
= NULL
;
1378 rc
= peer_is_invoker (client
);
1380 return rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
;
1382 if (!value
|| !*value
)
1383 return GPG_ERR_INV_VALUE
;
1385 rc
= crypto_list_keys (client
->crypto
, tmp
, 1, &keys
);
1389 if (!keys
|| !*keys
)
1391 crypto_free_key_list (keys
);
1392 return GPG_ERR_NO_SECKEY
;
1395 client
->crypto
->save
.mainkey
= keys
;
1400 genkey_command (assuan_context_t ctx
, char *line
)
1402 struct client_s
*client
= assuan_get_pointer (ctx
);
1403 struct crypto_s
*crypto
, *orig
;
1405 struct argv_s
*args
[] = {
1406 &(struct argv_s
) {"userid", OPTION_TYPE_ARG
,
1407 parse_genkey_opt_userid
},
1408 &(struct argv_s
) {"expire", OPTION_TYPE_ARG
,
1409 parse_genkey_opt_expire
},
1410 &(struct argv_s
) {"no-expire", OPTION_TYPE_NOARG
,
1411 parse_genkey_opt_no_expire
},
1412 &(struct argv_s
) {"algo", OPTION_TYPE_ARG
,
1413 parse_genkey_opt_algo
},
1414 &(struct argv_s
) {"no-passphrase", OPTION_TYPE_NOARG
,
1415 parse_genkey_opt_no_passphrase
},
1416 &(struct argv_s
) {"usage", OPTION_TYPE_ARG
,
1417 parse_genkey_opt_usage
},
1418 &(struct argv_s
) {"subkey-of", OPTION_TYPE_ARG
,
1419 parse_genkey_opt_subkey_of
},
1423 if (!(client
->flags
& FLAG_OPEN
))
1424 return send_error (ctx
, GPG_ERR_INV_STATE
);
1426 rc
= crypto_init (&crypto
, ctx
, client
->filename
,
1427 client
->flags
& FLAG_NO_PINENTRY
, NULL
);
1429 return send_error (ctx
, rc
);
1431 pthread_cleanup_push ((void *)crypto_free
, client
->crypto
);
1432 orig
= client
->crypto
;
1433 client
->crypto
= crypto
;
1434 rc
= parse_options (&line
, args
, client
, 0);
1437 if (!client
->crypto
->save
.userid
&& !client
->crypto
->save
.mainkey
)
1438 rc
= GPG_ERR_SYNTAX
;
1441 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1442 rc
= crypto_genkey (client
, client
->crypto
);
1446 pthread_cleanup_pop (0);
1447 crypto_free (crypto
);
1448 client
->crypto
= orig
;
1449 return send_error (ctx
, rc
);
1453 do_delete (assuan_context_t ctx
, char *line
)
1455 struct client_s
*client
= assuan_get_pointer (ctx
);
1456 struct xml_request_s
*req
;
1460 if (!line
|| !*line
)
1461 return GPG_ERR_SYNTAX
;
1463 rc
= xml_new_request (client
, line
, XML_CMD_DELETE
, &req
);
1467 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1468 xml_free_request (req
);
1472 rc
= xml_is_element_owner (client
, n
, 0);
1474 rc
= xml_unlink_node (client
, n
);
1479 /* Won't update cdata->plaintext. Other clients are working on the original or
1480 * copy of the same document. The first client to SAVE wins and requires others
1481 * to reopen the data file due to a checksum error. */
1483 copy_on_write (struct client_s
*client
)
1485 struct cache_data_s
*cdata
;
1487 if (client
->did_cow
)
1490 cdata
= cache_get_data (client
->filename
, NULL
);
1494 xmlDocPtr doc
= xmlCopyDoc (client
->doc
, 1);
1497 return GPG_ERR_ENOMEM
;
1499 rc
= cache_plaintext_set (client
->filename
, doc
, 1);
1506 (void)cache_plaintext_release (&client
->doc
);
1508 client
->did_cow
= 1;
1512 return GPG_ERR_NO_DATA
;
1516 delete_command (assuan_context_t ctx
, char *line
)
1518 struct client_s
*client
= assuan_get_pointer (ctx
);
1520 struct argv_s
*args
[] = {
1521 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1525 rc
= parse_options (&line
, args
, client
, 1);
1526 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1527 rc
= GPG_ERR_SYNTAX
;
1529 return send_error (ctx
, rc
);
1531 rc
= copy_on_write (client
);
1533 return send_error (ctx
, rc
);
1535 if (client
->opts
& OPT_INQUIRE
)
1537 unsigned char *result
;
1540 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1542 return send_error (ctx
, rc
);
1544 pthread_cleanup_push ((void *)xfree
, result
);
1545 rc
= do_delete (ctx
, (char *)result
);
1546 pthread_cleanup_pop (1);
1549 rc
= do_delete (ctx
, line
);
1551 return send_error (ctx
, rc
);
1555 update_element_expirey (struct client_s
*client
, xmlNodePtr n
)
1558 xmlChar
*expire
, *incr
;
1560 time_t e
, e_orig
, i
;
1561 time_t now
= time (NULL
);
1563 expire
= xml_attribute_value (n
, (xmlChar
*)"_expire");
1568 e
= strtoul ((char *)expire
, &p
, 10);
1569 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1572 rc
= GPG_ERR_ERANGE
;
1577 incr
= xml_attribute_value (n
, (xmlChar
*)"_age");
1581 i
= strtoul ((char *)incr
, &p
, 10);
1582 if (errno
|| *p
|| i
== ULONG_MAX
|| *incr
== '-')
1585 rc
= GPG_ERR_ERANGE
;
1592 p
= str_asprintf ("%lu", e
);
1595 rc
= GPG_ERR_ENOMEM
;
1599 rc
= xml_add_attribute (client
, n
, "_expire", p
);
1602 rc
= send_status (client
->ctx
, STATUS_EXPIRE
, "%lu %lu", e_orig
, e
);
1609 parse_opt_store_no_acl_inherit (void *data
, void *value
)
1611 struct client_s
*client
= data
;
1614 client
->flags
|= FLAG_NO_INHERIT_ACL
;
1619 store_command (assuan_context_t ctx
, char *line
)
1621 struct client_s
*client
= assuan_get_pointer (ctx
);
1624 unsigned char *result
;
1625 xmlNodePtr n
, parent
;
1627 char *content
= NULL
;
1628 struct xml_request_s
*req
;
1629 struct argv_s
*args
[] = {
1630 &(struct argv_s
) {"no-inherit-acl", OPTION_TYPE_NOARG
,
1631 parse_opt_store_no_acl_inherit
},
1635 rc
= parse_options (&line
, args
, client
, 1);
1637 return send_error (ctx
, rc
);
1639 if (!client
->bulk_p
&& line
&& *line
)
1640 return send_error (ctx
, GPG_ERR_SYNTAX
);
1642 rc
= copy_on_write (client
);
1644 return send_error (ctx
, rc
);
1646 if (!client
->bulk_p
)
1648 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1650 return send_error (ctx
, rc
);
1651 rc
= xml_new_request (client
, (char *)result
, XML_CMD_STORE
, &req
);
1652 if (!result
|| !*result
)
1655 return send_error (ctx
, GPG_ERR_SYNTAX
);
1658 len
= strv_length (req
->args
);
1659 has_content
= result
[strlen ((char *)result
) - 1] != '\t' && len
> 1;
1664 rc
= xml_new_request (client
, line
, XML_CMD_STORE
, &req
);
1665 len
= strv_length (req
->args
);
1666 has_content
= line
&& line
[strlen (line
) - 1] != '\t' && len
> 1;
1670 return send_error (ctx
, rc
);
1672 /* Prevent passing the element content around to save some memory
1674 if (*(req
->args
+1) && !xml_valid_element_path (req
->args
, has_content
))
1676 xml_free_request (req
);
1677 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1680 if (has_content
|| !*req
->args
[len
-1])
1682 content
= req
->args
[len
-1];
1683 req
->args
[len
-1] = NULL
;
1686 if (strv_length (req
->args
) > 1)
1688 rc
= xml_check_recursion (client
, req
);
1689 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1693 parent
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1697 rc
= xml_is_element_owner (client
, parent
, 0);
1700 n
= xml_find_text_node (parent
->children
);
1702 xmlNodeSetContent (n
, (xmlChar
*) content
);
1704 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1706 (void)xml_update_element_mtime (client
, parent
);
1707 (void)update_element_expirey (client
, parent
);
1713 xml_free_request (req
);
1714 return send_error (ctx
, rc
);
1718 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1720 struct client_s
*client
= assuan_get_pointer (ctx
);
1721 int to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1725 if (!(client
->flags
& FLAG_LOCK_CMD
))
1726 rc
= unlock_file_mutex (client
, 0);
1727 if (rc
&& rc
!= GPG_ERR_NOT_LOCKED
)
1732 client
->bulk_p
->result
= xmalloc (total
+1);
1733 if (!client
->bulk_p
->result
)
1734 return GPG_ERR_ENOMEM
;
1735 memcpy (client
->bulk_p
->result
, line
, total
);
1736 client
->bulk_p
->result
[total
] = 0;
1737 client
->bulk_p
->result_len
= total
;
1741 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1747 if (sent
+ to_send
> total
)
1748 to_send
= total
- sent
;
1750 rc
= assuan_send_data (ctx
, (char *) line
+ sent
, to_send
);
1754 while (!rc
&& sent
< total
);
1760 do_get (assuan_context_t ctx
, char *line
)
1762 struct client_s
*client
= assuan_get_pointer (ctx
);
1764 struct xml_request_s
*req
;
1767 if (!line
|| !*line
)
1768 return GPG_ERR_SYNTAX
;
1770 rc
= xml_new_request (client
, line
, XML_CMD_NONE
, &req
);
1774 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1777 if (n
&& n
->children
)
1781 n
= xml_find_text_node (n
->children
);
1782 if (!n
|| !n
->content
|| !*n
->content
)
1783 rc
= GPG_ERR_NO_DATA
;
1786 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1789 xmlChar
*expire
= xml_attribute_value (tmp
,
1790 (xmlChar
*)"_expire");
1793 time_t now
= time (NULL
);
1798 e
= strtoul ((char *)expire
, &p
, 10);
1799 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1800 log_write (_("invalid expire attribute value: %s"),
1804 pthread_cleanup_push (xmlFree
, expire
);
1805 rc
= send_status (ctx
, STATUS_EXPIRE
, "%lu 0", e
);
1806 pthread_cleanup_pop (0);
1815 rc
= GPG_ERR_NO_DATA
;
1818 xml_free_request (req
);
1823 get_command (assuan_context_t ctx
, char *line
)
1825 struct client_s
*client
= assuan_get_pointer (ctx
);
1827 struct argv_s
*args
[] = {
1828 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1832 rc
= parse_options (&line
, args
, client
, 1);
1833 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1834 rc
= GPG_ERR_SYNTAX
;
1836 return send_error (ctx
, rc
);
1838 if (client
->opts
& OPT_INQUIRE
)
1840 unsigned char *result
;
1843 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1845 return send_error (ctx
, rc
);
1847 pthread_cleanup_push ((void *)xfree
, result
);
1848 rc
= do_get (ctx
, (char *)result
);
1849 pthread_cleanup_pop (1);
1852 rc
= do_get (ctx
, line
);
1854 return send_error (ctx
, rc
);
1857 static void list_command_free1 (void *arg
);
1859 realpath_command (assuan_context_t ctx
, char *line
)
1862 char **realpath
= NULL
;
1863 struct client_s
*client
= assuan_get_pointer (ctx
);
1864 struct argv_s
*args
[] = {
1865 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1869 rc
= parse_options (&line
, args
, client
, 1);
1870 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1871 rc
= GPG_ERR_SYNTAX
;
1873 return send_error (ctx
, rc
);
1875 if (client
->opts
& OPT_INQUIRE
)
1877 unsigned char *result
;
1880 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1882 return send_error (ctx
, rc
);
1884 pthread_cleanup_push ((void *)xfree
, result
);
1885 (void)xml_resolve_path (client
, client
->doc
, result
, &realpath
, &rc
);
1886 pthread_cleanup_pop (1);
1889 (void)xml_resolve_path (client
, client
->doc
, (unsigned char *)line
,
1894 char *tmp
= strv_join ((char *)"\t", realpath
);
1896 strv_free (realpath
);
1898 return send_error (ctx
, GPG_ERR_ENOMEM
);
1900 pthread_cleanup_push ((void *)xfree
, tmp
);
1901 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
1902 pthread_cleanup_pop (1);
1905 return send_error (ctx
, rc
);
1909 list_command_free1 (void *arg
)
1912 string_free ((struct string_s
*) arg
, 1);
1916 parse_list_opt_recurse (void *data
, void *value
)
1918 struct client_s
*client
= data
;
1921 client
->opts
|= OPT_LIST_RECURSE
;
1926 parse_opt_verbose (void *data
, void *value
)
1928 struct client_s
*client
= data
;
1931 client
->opts
|= OPT_VERBOSE
;
1936 parse_opt_sexp (void *data
, void *value
)
1938 struct client_s
*client
= data
;
1941 client
->opts
|= OPT_SEXP
;
1946 list_path_once (struct client_s
*client
, char *line
,
1947 struct element_list_s
*elements
, struct string_s
*result
)
1951 rc
= xml_create_path_list (client
, client
->doc
, NULL
, elements
, line
, 0);
1952 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
)
1957 int total
= slist_length (elements
->list
);
1963 for (i
= 0; i
< total
; i
++)
1965 char *tmp
= slist_nth_data (elements
->list
, i
);
1967 string_append_printf (result
, "%s%s", tmp
,
1968 i
+ 1 == total
? "" : "\n");
1972 rc
= GPG_ERR_NO_DATA
;
1978 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1979 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1981 attribute_list_common (struct client_s
*client
, const char *path
,
1984 char **attrlist
= NULL
;
1988 xmlNodePtr n
, an
, r
;
1990 struct xml_request_s
*req
;
1992 rc
= xml_new_request (client
, path
, XML_CMD_ATTR_LIST
, &req
);
1996 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1997 xml_free_request (req
);
2001 gpg_error_t acl_rc
= 0;
2003 acl_rc
= xml_acl_check (client
, n
);
2004 for (a
= n
->properties
; a
; a
= a
->next
)
2007 int reserved
= xml_reserved_attribute ((char *)a
->name
);
2009 if (acl_rc
== GPG_ERR_EACCES
&& !reserved
)
2011 else if (acl_rc
&& acl_rc
!= GPG_ERR_EACCES
)
2017 if (reserved
&& target
)
2020 pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *));
2023 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2024 pwmd_strerror (GPG_ERR_ENOMEM
));
2025 rc
= GPG_ERR_ENOMEM
;
2031 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
, an
&& an
->content
2032 ? (char *)an
->content
: "");
2036 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2037 pwmd_strerror (GPG_ERR_ENOMEM
));
2038 rc
= GPG_ERR_ENOMEM
;
2042 attrlist
[++i
] = NULL
;
2045 if (!rc
&& !attrlist
)
2046 return GPG_ERR_NO_DATA
;
2050 r
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)path
, NULL
, &rc
);
2051 if (RESUMABLE_ERROR (rc
))
2066 strv_free (attrlist
);
2072 create_list_sexp (struct client_s
*client
, struct string_s
*string
,
2073 struct string_s
**r_string
)
2075 struct string_s
*str
= string
;
2077 struct string_s
*result
= string_new ("(11:list-result");
2078 char **strv
= str_split (str
->str
, "\n", 0);
2079 int t
= strv_length (strv
);
2082 string_large (result
);
2084 for (i
= 0; i
< t
; i
++)
2086 char **tmpv
= str_split (strv
[i
], " ", 0);
2087 char **attrlist
= NULL
;
2089 result
= string_append_printf (result
, "(4:path%i:%s", strlen (tmpv
[0]),
2092 result
= string_append_printf (result
, "5:flags%i:%s", strlen (tmpv
[1]),
2095 result
= string_append (result
, "5:flags0:");
2097 rc
= attribute_list_common (client
, tmpv
[0], &attrlist
);
2101 int at
= strv_length (attrlist
);
2103 result
= string_append (result
, "(5:attrs");
2105 for (ai
= 0; ai
< at
; ai
++)
2107 char **attrv
= str_split (attrlist
[ai
], " ", 0);
2108 char *value
= strv_join (" ", attrv
+1);
2110 result
= string_append_printf (result
, "%i:%s%i:%s",
2111 strlen (attrv
[0]), attrv
[0],
2112 strlen (value
), value
);
2117 result
= string_append (result
, ")");
2120 result
= string_append (result
, ")");
2121 strv_free (attrlist
);
2126 result
= string_append (result
, ")");
2134 do_list (assuan_context_t ctx
, char *line
)
2136 struct client_s
*client
= assuan_get_pointer (ctx
);
2137 gpg_error_t rc
= GPG_ERR_NO_DATA
;
2138 struct element_list_s
*elements
= NULL
;
2139 struct string_s
*result
= NULL
;
2141 if (!line
|| !*line
)
2143 struct string_s
*str
= string_new (NULL
);
2147 return GPG_ERR_ENOMEM
;
2150 pthread_cleanup_push ((void *)list_command_free1
, str
);
2151 n
= xmlDocGetRootElement (client
->doc
);
2152 for (n
= n
->children
; n
; n
= n
->next
)
2156 if (n
->type
!= XML_ELEMENT_NODE
)
2159 name
= xmlGetProp (n
, (xmlChar
*)"_name");
2162 rc
= GPG_ERR_ENOMEM
;
2166 elements
= xcalloc (1, sizeof (struct element_list_s
));
2170 rc
= GPG_ERR_ENOMEM
;
2174 elements
->prefix
= string_new (NULL
);
2175 if (!elements
->prefix
)
2178 xml_free_element_list (elements
);
2179 rc
= GPG_ERR_ENOMEM
;
2183 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
2184 elements
->root_only
= !elements
->recurse
;
2185 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
2186 rc
= list_path_once (client
, (char *)name
, elements
, str
);
2188 pthread_cleanup_pop (1);
2193 string_append (str
, "\n");
2198 if (client
->opts
& OPT_SEXP
)
2200 rc
= create_list_sexp (client
, str
, &result
);
2203 pthread_cleanup_push ((void *)list_command_free1
, result
);
2204 rc
= xfer_data (ctx
, result
->str
, result
->len
);
2205 pthread_cleanup_pop (1);
2209 rc
= xfer_data (ctx
, str
->str
, str
->len
);
2212 pthread_cleanup_pop (1);
2216 elements
= xcalloc (1, sizeof (struct element_list_s
));
2218 return GPG_ERR_ENOMEM
;
2220 elements
->prefix
= string_new (NULL
);
2221 if (!elements
->prefix
)
2223 xml_free_element_list (elements
);
2224 return GPG_ERR_ENOMEM
;
2227 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
2228 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
2229 struct string_s
*str
= string_new (NULL
);
2231 pthread_cleanup_push ((void *)list_command_free1
, str
);
2232 rc
= list_path_once (client
, line
, elements
, str
);
2235 if (client
->opts
& OPT_SEXP
)
2237 rc
= create_list_sexp (client
, str
, &result
);
2240 pthread_cleanup_push ((void *)list_command_free1
, result
);
2241 rc
= xfer_data (ctx
, result
->str
, result
->len
);
2242 pthread_cleanup_pop (1);
2246 rc
= xfer_data (ctx
, str
->str
, str
->len
);
2249 pthread_cleanup_pop (1);
2250 pthread_cleanup_pop (1);
2255 list_command (assuan_context_t ctx
, char *line
)
2257 struct client_s
*client
= assuan_get_pointer (ctx
);
2259 struct argv_s
*args
[] = {
2260 &(struct argv_s
) {"recurse", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
2261 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2262 /* These are deprecated but kept for backward compatibility with older
2264 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, NULL
},
2265 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
, NULL
},
2266 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
2267 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
, NULL
},
2268 &(struct argv_s
) {"sexp", OPTION_TYPE_NOARG
, parse_opt_sexp
},
2272 if (disable_list_and_dump
== 1)
2273 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2275 rc
= parse_options (&line
, args
, client
, 1);
2276 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2277 rc
= GPG_ERR_SYNTAX
;
2279 return send_error (ctx
, rc
);
2281 if (client
->opts
& OPT_INQUIRE
)
2283 unsigned char *result
;
2286 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2288 return send_error (ctx
, rc
);
2290 pthread_cleanup_push ((void *)xfree
, result
);
2291 rc
= do_list (ctx
, (char *)result
);
2292 pthread_cleanup_pop (1);
2295 rc
= do_list (ctx
, line
);
2297 return send_error (ctx
, rc
);
2301 * args[0] - element path
2304 attribute_list (assuan_context_t ctx
, char **args
)
2306 struct client_s
*client
= assuan_get_pointer (ctx
);
2307 char **attrlist
= NULL
;
2311 if (!args
|| !args
[0] || !*args
[0])
2312 return GPG_ERR_SYNTAX
;
2314 rc
= attribute_list_common (client
, args
[0], &attrlist
);
2315 pthread_cleanup_push ((void *)req_free
, attrlist
);
2319 line
= strv_join ("\n", attrlist
);
2322 pthread_cleanup_push ((void *)xfree
, line
);
2323 rc
= xfer_data (ctx
, line
, strlen (line
));
2324 pthread_cleanup_pop (1);
2328 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2329 pwmd_strerror (GPG_ERR_ENOMEM
));
2330 rc
= GPG_ERR_ENOMEM
;
2334 pthread_cleanup_pop (1);
2339 * args[0] - attribute
2340 * args[1] - element path
2343 attribute_delete (struct client_s
*client
, char **args
)
2348 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
2349 return GPG_ERR_SYNTAX
;
2351 rc
= xml_protected_attr (args
[0]);
2352 if (rc
|| !strcmp (args
[0], "_target"))
2353 return rc
? rc
: GPG_ERR_EPERM
;
2355 rc
= copy_on_write (client
);
2359 if (!xml_reserved_attribute (args
[0]))
2360 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2363 struct xml_request_s
*req
;
2365 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2369 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2370 xml_free_request (req
);
2374 rc
= xml_is_element_owner (client
, n
, 0);
2377 rc
= xml_delete_attribute (client
, n
, (xmlChar
*) args
[0]);
2383 * Creates the "_target" attribute. When other commands encounter an element
2384 * with this attribute they follow the "_target" after appending remaining
2385 * elements from the original element path to the target. The exception is the
2386 * ATTR command that operates on the element itself (for reserved attribute
2387 * names) and not the target.
2389 * If the source element path doesn't exist when using 'ATTR SET target', it is
2390 * created, but the destination element path must exist. This is similar to the
2391 * ls(1) command: ln -s src dst.
2393 * args[0] - source element path
2394 * args[1] - destination element path
2397 set_target_attribute (struct client_s
*client
, char **args
)
2399 struct xml_request_s
*req
= NULL
;
2404 if (!args
|| !args
[0] || !args
[1])
2405 return GPG_ERR_SYNTAX
;
2407 src
= str_split (args
[0], "\t", 0);
2409 return GPG_ERR_SYNTAX
;
2411 if (!xml_valid_element_path (src
, 0))
2414 return GPG_ERR_INV_VALUE
;
2417 dst
= str_split (args
[1], "\t", 0);
2421 return GPG_ERR_SYNTAX
;
2424 rc
= copy_on_write (client
);
2428 // Be sure the destination element path exists. */
2429 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
2433 (void)xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2434 if (rc
&& rc
!= GPG_ERR_EACCES
)
2437 xml_free_request (req
);
2440 if (!strcmp (args
[0], args
[1]))
2442 rc
= GPG_ERR_EEXIST
;
2446 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_TARGET
, &req
);
2450 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2451 if (rc
&& rc
!= GPG_ERR_EACCES
)
2456 rc
= xml_add_attribute (client
, n
, "_target", args
[1]);
2459 rc
= xml_remove_user_attributes (client
, n
);
2461 rc
= xml_unlink_node (client
, n
->children
);
2468 xml_free_request (req
);
2473 * args[0] - attribute
2474 * args[1] - element path
2477 attribute_get (assuan_context_t ctx
, char **args
)
2479 struct client_s
*client
= assuan_get_pointer (ctx
);
2483 struct xml_request_s
*req
;
2485 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
2486 return GPG_ERR_SYNTAX
;
2488 if (!xml_reserved_attribute (args
[0]))
2489 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2492 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2496 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2497 xml_free_request (req
);
2503 a
= xmlGetProp (n
, (xmlChar
*) args
[0]);
2505 return GPG_ERR_NOT_FOUND
;
2507 pthread_cleanup_push ((void *)xmlFree
, a
);
2510 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2512 rc
= GPG_ERR_NO_DATA
;
2514 pthread_cleanup_pop (1);
2519 * args[0] - attribute
2520 * args[1] - element path
2524 attribute_set (struct client_s
*client
, char **args
)
2526 struct xml_request_s
*req
;
2530 if (!args
|| !args
[0] || !args
[1])
2531 return GPG_ERR_SYNTAX
;
2534 * Reserved attribute names.
2536 if ((rc
= xml_protected_attr (args
[0])))
2538 else if (!strcmp (args
[0], "_target"))
2539 return set_target_attribute (client
, args
+ 1);
2540 else if (!strcmp (args
[0], "_acl"))
2541 rc
= xml_valid_username (args
[2]);
2542 else if (!xml_valid_attribute (args
[0]))
2543 return GPG_ERR_INV_VALUE
;
2548 if (!xml_valid_attribute_value (args
[2]))
2549 return GPG_ERR_INV_VALUE
;
2551 rc
= copy_on_write (client
);
2555 if (!xml_reserved_attribute (args
[0]))
2557 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2560 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
2563 rc
= xml_check_recursion (client
, req
);
2564 xml_free_request (req
);
2570 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2574 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2575 xml_free_request (req
);
2579 rc
= xml_is_element_owner (client
, n
, 0);
2582 rc
= xml_add_attribute (client
, n
, args
[0], args
[2]);
2589 * req[1] - attribute name or element path if command is LIST
2590 * req[2] - element path
2591 * req[2] - element path or value
2595 do_attr (assuan_context_t ctx
, char *line
)
2597 struct client_s
*client
= assuan_get_pointer (ctx
);
2601 if (!line
|| !*line
)
2602 return GPG_ERR_SYNTAX
;
2604 req
= str_split (line
, " ", 4);
2605 if (!req
|| !req
[0] || !req
[1])
2608 return GPG_ERR_SYNTAX
;
2611 pthread_cleanup_push ((void *)req_free
, req
);
2613 if (strcasecmp (req
[0], "SET") == 0)
2614 rc
= attribute_set (client
, req
+ 1);
2615 else if (strcasecmp (req
[0], "GET") == 0)
2616 rc
= attribute_get (ctx
, req
+ 1);
2617 else if (strcasecmp (req
[0], "DELETE") == 0)
2618 rc
= attribute_delete (client
, req
+ 1);
2619 else if (strcasecmp (req
[0], "LIST") == 0)
2620 rc
= attribute_list (ctx
, req
+ 1);
2622 rc
= GPG_ERR_SYNTAX
;
2624 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2625 pthread_cleanup_pop (1);
2630 attr_command (assuan_context_t ctx
, char *line
)
2632 struct client_s
*client
= assuan_get_pointer (ctx
);
2634 struct argv_s
*args
[] = {
2635 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2639 rc
= parse_options (&line
, args
, client
, 1);
2640 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2641 rc
= GPG_ERR_SYNTAX
;
2643 return send_error (ctx
, rc
);
2645 if (client
->opts
& OPT_INQUIRE
)
2647 unsigned char *result
;
2650 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2652 return send_error (ctx
, rc
);
2654 pthread_cleanup_push ((void *)xfree
, result
);
2655 rc
= do_attr (ctx
, (char *)result
);
2656 pthread_cleanup_pop (1);
2659 rc
= do_attr (ctx
, line
);
2661 return send_error (ctx
, rc
);
2665 parse_iscached_opt_lock (void *data
, void *value
)
2667 struct client_s
*client
= data
;
2670 client
->opts
|= OPT_LOCK
;
2675 parse_iscached_opt_agent (void *data
, void *value
)
2677 struct client_s
*client
= data
;
2680 client
->opts
|= OPT_CACHE_AGENT
;
2685 parse_iscached_opt_sign (void *data
, void *value
)
2687 struct client_s
*client
= data
;
2690 client
->opts
|= OPT_CACHE_SIGN
;
2695 iscached_command (assuan_context_t ctx
, char *line
)
2697 struct client_s
*client
= assuan_get_pointer (ctx
);
2700 struct argv_s
*args
[] = {
2701 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2702 &(struct argv_s
) {"agent", OPTION_TYPE_NOARG
, parse_iscached_opt_agent
},
2703 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_iscached_opt_sign
},
2707 if (!line
|| !*line
)
2708 return send_error (ctx
, GPG_ERR_SYNTAX
);
2710 rc
= parse_options (&line
, args
, client
, 1);
2712 return send_error (ctx
, rc
);
2713 else if (!valid_filename (line
))
2714 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2716 if (!(client
->flags
& FLAG_OPEN
)
2717 && ((client
->opts
& OPT_CACHE_AGENT
) || (client
->opts
& OPT_CACHE_SIGN
)))
2718 return send_error (ctx
, GPG_ERR_INV_STATE
);
2720 rc
= cache_iscached (line
, &defer
, (client
->opts
& OPT_CACHE_AGENT
),
2721 (client
->opts
& OPT_CACHE_SIGN
));
2723 rc
= GPG_ERR_NO_DATA
;
2727 struct cache_data_s
*cdata
= cache_get_data (line
, NULL
);
2731 rc
= validate_checksum (client
, line
, cdata
, NULL
, NULL
, 1);
2732 if (rc
== GPG_ERR_CHECKSUM
)
2733 rc
= GPG_ERR_NO_DATA
;
2737 if (client
->opts
& OPT_LOCK
2738 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
2739 || gpg_err_code (rc
) == GPG_ERR_ENOENT
))
2741 gpg_error_t trc
= rc
;
2743 if (client
->filename
&& strcmp (line
, client
->filename
))
2744 reset_client (client
);
2746 xfree (client
->filename
);
2747 client
->filename
= str_dup (line
);
2748 rc
= do_lock (client
, 1);
2753 return send_error (ctx
, rc
);
2757 clearcache_command (assuan_context_t ctx
, char *line
)
2759 struct client_s
*client
= assuan_get_pointer (ctx
);
2760 gpg_error_t rc
= 0, all_rc
= 0;
2764 struct client_thread_s
*once
= NULL
;
2768 pthread_cleanup_push (cache_unlock
, NULL
);
2769 count
= cache_file_count ();
2770 MUTEX_LOCK (&cn_mutex
);
2771 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
2773 if (!line
|| !*line
)
2776 t
= slist_length (cn_thread_list
);
2778 for (i
= 0; i
< t
; i
++)
2780 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2781 assuan_peercred_t peer
;
2786 /* Lock each connected clients' file mutex to prevent any other client
2787 * from accessing the cache entry (the file mutex is locked upon
2788 * command startup). The cache for the entry is not cleared if the
2789 * file mutex is locked by another client to prevent this function
2790 * from blocking. Rather, it returns an error.
2794 if (thd
->cl
->filename
)
2796 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
, NULL
);
2797 /* The current client doesn't have permission to open the other
2798 * filename do to "access" configuration parameter in a filename
2799 * section. Since we are clearning all cache entries the error
2800 * will be returned later during cache_clear(). */
2807 else // Idle client without opened file?
2810 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2811 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2813 /* The current client owns the lock. */
2814 if (pthread_equal (pthread_self (), thd
->tid
))
2818 if (cache_iscached (thd
->cl
->filename
, NULL
, 0, 0)
2825 /* The cache entry will be cleared when the other client
2826 * disconnects and cache_timer_thread() does its thing. */
2827 cache_defer_clear (thd
->cl
->filename
);
2830 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2838 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1, 0);
2839 cache_unlock_mutex (thd
->cl
->filename
, 0);
2849 /* A single data filename was specified. Lock only this data file
2850 * mutex and free the cache entry. */
2851 rc
= do_validate_peer (ctx
, line
, &peer
, NULL
);
2852 if (rc
== GPG_ERR_FORBIDDEN
)
2853 rc
= peer_is_invoker (client
);
2855 if (rc
== GPG_ERR_EACCES
)
2856 rc
= GPG_ERR_FORBIDDEN
;
2858 if (!rc
&& thd
->cl
->filename
&& !strcmp (thd
->cl
->filename
, line
))
2860 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2861 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2863 /* The current client owns the lock. */
2864 if (pthread_equal (pthread_self (), thd
->tid
))
2871 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1, 0);
2872 cache_unlock_mutex (thd
->cl
->filename
, 0);
2875 cache_defer_clear (thd
->cl
->filename
);
2877 /* Found a client with the opened file. The cache entry has been
2878 * either cleared (self) or deferred to be cleared. */
2884 /* Only connected clients' cache entries have been cleared. Now clear any
2885 * remaining cache entries without clients but only if there wasn't an
2886 * error from above since this would defeat the locking check of the
2887 * remaining entries. */
2888 if (all
&& !all_rc
&& cache_file_count ())
2890 rc
= cache_clear (client
, NULL
, 1, 0);
2891 if (rc
== GPG_ERR_EACCES
)
2892 rc
= GPG_ERR_FORBIDDEN
;
2895 /* No clients are using the specified file. */
2896 else if (!all_rc
&& !rc
&& !once
)
2897 rc
= cache_clear (NULL
, line
, 1, 0);
2899 /* Release the connection mutex. */
2900 pthread_cleanup_pop (1);
2902 if (!rc
|| (cache_file_count () && count
!= cache_file_count ()))
2903 send_status_all (STATUS_CACHE
, NULL
);
2905 /* Release the cache mutex. */
2906 pthread_cleanup_pop (1);
2908 /* One or more files were locked while clearing all cache entries. */
2912 return send_error (ctx
, rc
);
2916 cachetimeout_command (assuan_context_t ctx
, char *line
)
2918 struct client_s
*client
= assuan_get_pointer (ctx
);
2920 char **req
= str_split (line
, " ", 0);
2924 if (!(client
->flags
& FLAG_OPEN
))
2925 return send_error (ctx
, GPG_ERR_INV_STATE
);
2927 if (!req
|| !*req
|| strv_length (req
) > 1)
2930 return send_error (ctx
, GPG_ERR_SYNTAX
);
2934 timeout
= strtol (req
[0], &p
, 10);
2935 if (errno
!= 0 || *p
|| timeout
< (long)-1)
2936 rc
= GPG_ERR_SYNTAX
;
2940 rc
= cache_set_timeout (client
->filename
, timeout
);
2941 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2944 MUTEX_LOCK (&rcfile_mutex
);
2945 config_set_long_param (&global_config
, client
->filename
,
2946 "cache_timeout", req
[0]);
2947 MUTEX_UNLOCK (&rcfile_mutex
);
2952 return send_error (ctx
, rc
);
2956 dump_command (assuan_context_t ctx
, char *line
)
2960 struct client_s
*client
= assuan_get_pointer (ctx
);
2963 if (disable_list_and_dump
== 1)
2964 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2967 return send_error (ctx
, GPG_ERR_SYNTAX
);
2969 rc
= peer_is_invoker(client
);
2971 return send_error (ctx
, (rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
));
2973 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2977 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2978 pwmd_strerror (GPG_ERR_ENOMEM
));
2979 return send_error (ctx
, GPG_ERR_ENOMEM
);
2982 pthread_cleanup_push ((void *)xmlFree
, xml
);
2983 rc
= xfer_data (ctx
, (char *) xml
, len
);
2984 pthread_cleanup_pop (1);
2985 return send_error (ctx
, rc
);
2989 getconfig_command (assuan_context_t ctx
, char *line
)
2991 struct client_s
*client
= assuan_get_pointer (ctx
);
2993 char filename
[255] = { 0 }, param
[747] = { 0 };
2994 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2996 if (!line
|| !*line
)
2997 return send_error (ctx
, GPG_ERR_SYNTAX
);
2999 if (strchr (line
, ' '))
3001 int ret
= sscanf (line
, " %254[^ ] %746c", filename
, param
);
3004 return send_error (ctx
, gpg_error_from_syserror());
3009 if (fp
&& !valid_filename (fp
))
3010 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3012 paramp
= str_down (paramp
);
3013 p
= config_get_value (fp
? fp
: "global", paramp
);
3015 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
3017 tmp
= expand_homedir (p
);
3021 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
3022 pwmd_strerror (GPG_ERR_ENOMEM
));
3023 return send_error (ctx
, GPG_ERR_ENOMEM
);
3027 pthread_cleanup_push ((void *)xfree
, p
);
3028 rc
= xfer_data (ctx
, p
, strlen (p
));
3029 pthread_cleanup_pop (1);
3030 return send_error (ctx
, rc
);
3035 xmlXPathContextPtr xp
;
3036 xmlXPathObjectPtr result
;
3042 xpath_command_free (void *arg
)
3044 struct xpath_s
*xpath
= arg
;
3049 req_free (xpath
->req
);
3052 xmlBufferFree (xpath
->buf
);
3055 xmlXPathFreeObject (xpath
->result
);
3058 xmlXPathFreeContext (xpath
->xp
);
3062 do_xpath (assuan_context_t ctx
, char *line
)
3065 struct client_s
*client
= assuan_get_pointer (ctx
);
3066 struct xpath_s _x
= { 0 };
3067 struct xpath_s
*xpath
= &_x
;
3069 if (!line
|| !*line
)
3070 return GPG_ERR_SYNTAX
;
3072 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
3074 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
3075 return GPG_ERR_ENOMEM
;
3080 rc
= copy_on_write (client
);
3085 xpath
->xp
= xmlXPathNewContext (client
->doc
);
3088 rc
= GPG_ERR_BAD_DATA
;
3093 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
3096 rc
= GPG_ERR_BAD_DATA
;
3100 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
3102 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
3106 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
3107 xpath
->result
->nodesetval
,
3108 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
3112 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
3114 rc
= GPG_ERR_NO_DATA
;
3117 else if (xpath
->req
[1])
3123 pthread_cleanup_push ((void *)xpath_command_free
, &xpath
);
3124 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
3125 xmlBufferLength (xpath
->buf
));
3126 pthread_cleanup_pop (0);
3128 xpath_command_free (xpath
);
3133 xpath_command (assuan_context_t ctx
, char *line
)
3135 struct client_s
*client
= assuan_get_pointer (ctx
);
3137 struct argv_s
*args
[] = {
3138 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3142 if (disable_list_and_dump
== 1)
3143 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
3145 rc
= peer_is_invoker(client
);
3147 return send_error (ctx
, (rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
));
3149 rc
= parse_options (&line
, args
, client
, 1);
3150 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3151 rc
= GPG_ERR_SYNTAX
;
3153 return send_error (ctx
, rc
);
3155 if (client
->opts
& OPT_INQUIRE
)
3157 unsigned char *result
;
3160 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3162 return send_error (ctx
, rc
);
3164 pthread_cleanup_push ((void *)xfree
, result
);
3165 rc
= do_xpath (ctx
, (char *)result
);
3166 pthread_cleanup_pop (1);
3169 rc
= do_xpath (ctx
, line
);
3171 return send_error (ctx
, rc
);
3175 do_xpathattr (assuan_context_t ctx
, char *line
)
3177 struct client_s
*client
= assuan_get_pointer (ctx
);
3181 struct xpath_s _x
= { 0 };
3182 struct xpath_s
*xpath
= &_x
;
3184 if (!line
|| !*line
)
3185 return GPG_ERR_SYNTAX
;
3187 if ((req
= str_split (line
, " ", 3)) == NULL
)
3188 return GPG_ERR_ENOMEM
;
3192 rc
= GPG_ERR_SYNTAX
;
3196 if (!strcasecmp (req
[0], "SET"))
3198 else if (!strcasecmp (req
[0], "DELETE"))
3202 rc
= GPG_ERR_SYNTAX
;
3206 if (!req
[1] || !req
[2])
3208 rc
= GPG_ERR_SYNTAX
;
3212 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
3214 rc
= GPG_ERR_ENOMEM
;
3218 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
3220 rc
= GPG_ERR_SYNTAX
;
3224 rc
= copy_on_write (client
);
3228 xpath
->xp
= xmlXPathNewContext (client
->doc
);
3231 rc
= GPG_ERR_BAD_DATA
;
3235 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
3238 rc
= GPG_ERR_BAD_DATA
;
3242 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
3244 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
3248 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
3249 xpath
->result
->nodesetval
,
3250 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
3251 (xmlChar
*) req
[1]);
3254 xpath_command_free (xpath
);
3259 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
3261 xpathattr_command (assuan_context_t ctx
, char *line
)
3263 struct client_s
*client
= assuan_get_pointer (ctx
);
3265 struct argv_s
*args
[] = {
3266 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3270 if (disable_list_and_dump
== 1)
3271 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
3273 rc
= peer_is_invoker(client
);
3275 return send_error (ctx
, (rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
));
3277 rc
= parse_options (&line
, args
, client
, 1);
3278 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3279 rc
= GPG_ERR_SYNTAX
;
3281 return send_error (ctx
, rc
);
3283 if (client
->opts
& OPT_INQUIRE
)
3285 unsigned char *result
;
3288 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3290 return send_error (ctx
, rc
);
3292 pthread_cleanup_push ((void *)xfree
, result
);
3293 rc
= do_xpathattr (ctx
, (char *)result
);
3294 pthread_cleanup_pop (1);
3297 rc
= do_xpathattr (ctx
, line
);
3299 return send_error (ctx
, rc
);
3303 do_import (struct client_s
*client
, const char *root_element
,
3304 unsigned char *content
)
3306 xmlDocPtr doc
= NULL
;
3307 xmlNodePtr n
= NULL
, root
;
3309 struct string_s
*str
= NULL
, *tstr
= NULL
;
3310 struct xml_request_s
*req
= NULL
;
3312 if (!content
|| !*content
)
3313 return GPG_ERR_SYNTAX
;
3317 rc
= xml_new_request (client
, root_element
, XML_CMD_STORE
, &req
);
3324 if (!xml_valid_element_path (req
->args
, 0))
3326 xml_free_request (req
);
3328 return GPG_ERR_INV_VALUE
;
3332 str
= string_new_content ((char *)content
);
3333 tstr
= string_prepend (str
, "<pwmd>");
3337 tstr
= string_append (str
, "</pwmd>");
3339 rc
= GPG_ERR_ENOMEM
;
3344 rc
= GPG_ERR_ENOMEM
;
3349 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
3350 string_free (str
, 1);
3353 rc
= GPG_ERR_BAD_DATA
;
3357 root
= xmlDocGetRootElement (doc
);
3358 root
= root
->children
;
3359 if (!root
|| root
->type
!= XML_ELEMENT_NODE
)
3361 rc
= GPG_ERR_BAD_DATA
;
3365 rc
= xml_validate_import (client
, root
);
3371 /* Create the new specified root element path, if needed. */
3372 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
3377 /* Overwrite the children of the specified root element path. */
3378 (void)xml_unlink_node (client
, n
->children
);
3382 n
= xmlDocGetRootElement (client
->doc
);
3387 xmlChar
*name
= xml_attribute_value (root
, (xmlChar
*)"_name");
3390 rc
= GPG_ERR_BAD_DATA
;
3392 rc
= xml_is_element_owner (client
, n
, 0);
3396 /* No --root argument passed. Overwrite the current documents' root
3397 * element matching the root element of the imported data. */
3398 xml_free_request (req
);
3400 rc
= xml_new_request (client
, (char *)name
, XML_CMD_DELETE
, &req
);
3406 tmp
= xml_find_elements (client
, req
,
3407 xmlDocGetRootElement (req
->doc
), &rc
);
3408 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3413 rc
= xml_is_element_owner (client
, tmp
, 0);
3418 (void)xml_unlink_node (client
, tmp
);
3425 copy
= xmlCopyNodeList (root
);
3427 rc
= GPG_ERR_ENOMEM
;
3430 n
= xmlAddChildList (n
, copy
);
3432 rc
= GPG_ERR_ENOMEM
;
3437 if (!rc
&& n
&& n
->parent
)
3438 rc
= xml_update_element_mtime (client
, n
->parent
);
3441 xml_free_request (req
);
3450 parse_import_opt_root (void *data
, void *value
)
3452 struct client_s
*client
= data
;
3454 client
->import_root
= str_dup (value
);
3455 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
3459 import_command (assuan_context_t ctx
, char *line
)
3462 struct client_s
*client
= assuan_get_pointer (ctx
);
3463 unsigned char *result
;
3465 struct argv_s
*args
[] = {
3467 "root", OPTION_TYPE_ARG
, parse_import_opt_root
3472 xfree (client
->import_root
);
3473 client
->import_root
= NULL
;
3474 rc
= parse_options (&line
, args
, client
, client
->bulk_p
? 1 : 0);
3476 return send_error (ctx
, rc
);
3478 rc
= copy_on_write (client
);
3480 return send_error (ctx
, rc
);
3482 if (!client
->bulk_p
)
3484 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3487 xfree (client
->import_root
);
3488 client
->import_root
= NULL
;
3489 return send_error (ctx
, rc
);
3491 rc
= do_import (client
, client
->import_root
, result
);
3495 char *p
= str_dup (line
);
3498 rc
= GPG_ERR_ENOMEM
;
3500 rc
= do_import (client
, client
->import_root
, (unsigned char *)p
);
3503 xfree (client
->import_root
);
3504 client
->import_root
= NULL
;
3505 return send_error (ctx
, rc
);
3509 do_lock (struct client_s
*client
, int add
)
3511 gpg_error_t rc
= lock_file_mutex (client
, add
);
3514 client
->flags
|= FLAG_LOCK_CMD
;
3520 lock_command (assuan_context_t ctx
, char *line
)
3522 struct client_s
*client
= assuan_get_pointer (ctx
);
3526 return send_error (ctx
, GPG_ERR_SYNTAX
);
3528 rc
= do_lock (client
, 0);
3529 return send_error (ctx
, rc
);
3533 unlock_command (assuan_context_t ctx
, char *line
)
3535 struct client_s
*client
= assuan_get_pointer (ctx
);
3539 return send_error (ctx
, GPG_ERR_SYNTAX
);
3541 rc
= unlock_file_mutex (client
, 0);
3542 return send_error (ctx
, rc
);
3546 set_env (const char *name
, char *buf
, size_t size
, const char *value
)
3548 MUTEX_LOCK (&rcfile_mutex
);
3549 if (!value
|| !*value
)
3554 else if (!buf
[0] || strcmp (buf
, value
))
3556 snprintf (buf
, size
, "%s=%s", name
, value
);
3560 MUTEX_UNLOCK (&rcfile_mutex
);
3564 option_command (assuan_context_t ctx
, char *line
)
3566 struct client_s
*client
= assuan_get_pointer (ctx
);
3568 char namebuf
[255] = { 0 };
3569 char *name
= namebuf
;
3570 char *value
= NULL
, *p
, *tmp
= NULL
;
3572 p
= strchr (line
, '=');
3575 strncpy (namebuf
, line
, sizeof(namebuf
));
3576 namebuf
[sizeof(namebuf
)-1] = 0;
3580 tmp
= str_dup (line
);
3582 return send_error (ctx
, GPG_ERR_ENOMEM
);
3584 tmp
[strlen (line
) - strlen (p
)] = 0;
3585 strncpy (namebuf
, tmp
, sizeof (namebuf
));
3586 namebuf
[sizeof(namebuf
)-1] = 0;
3592 log_write2 ("OPTION name='%s' value='%s'", name
, value
);
3594 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3600 n
= strtol (value
, &tmp
, 10);
3602 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3605 client
->lock_timeout
= n
;
3607 else if (strcasecmp (name
, (char *) "client-state") == 0)
3611 MUTEX_LOCK (&client
->thd
->status_mutex
);
3614 n
= strtol (value
, &tmp
, 10);
3615 if ((tmp
&& *tmp
) || (n
< 0 || n
> 1))
3617 MUTEX_UNLOCK (&client
->thd
->status_mutex
);
3618 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3620 client
->thd
->send_state
= n
;
3623 client
->thd
->send_state
= 0;
3624 MUTEX_UNLOCK (&client
->thd
->status_mutex
);
3626 else if (strcasecmp (name
, (char *) "NAME") == 0)
3628 if (value
&& strchr (value
, ' '))
3629 rc
= GPG_ERR_INV_VALUE
;
3632 MUTEX_LOCK (&cn_mutex
);
3633 tmp
= pthread_getspecific (thread_name_key
);
3634 pthread_setspecific (thread_name_key
, NULL
);
3636 xfree (client
->thd
->name
);
3637 client
->thd
->name
= NULL
;
3638 if (value
&& *value
)
3640 pthread_setspecific (thread_name_key
, str_dup (value
));
3641 client
->thd
->name
= str_dup (value
);
3643 MUTEX_UNLOCK (&cn_mutex
);
3646 else if (strcasecmp (name
, "disable-pinentry") == 0)
3650 if (value
&& *value
)
3652 n
= (int) strtol (value
, &tmp
, 10);
3653 if (*tmp
|| n
< 0 || n
> 1)
3654 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3658 client
->flags
|= FLAG_NO_PINENTRY
;
3660 client
->flags
&= ~FLAG_NO_PINENTRY
;
3662 else if (strcasecmp (name
, "ttyname") == 0)
3663 set_env ("GPG_TTY", env_gpg_tty
, sizeof (env_gpg_tty
), value
);
3664 else if (strcasecmp (name
, "ttytype") == 0)
3665 set_env ("TERM", env_term
, sizeof (env_term
), value
);
3666 else if (strcasecmp (name
, "display") == 0)
3667 set_env ("DISPLAY", env_display
, sizeof (env_display
), value
);
3668 else if (strcasecmp (name
, "lc_messages") == 0)
3671 else if (strcasecmp (name
, "lc_ctype") == 0)
3674 else if (strcasecmp (name
, "desc") == 0)
3677 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3681 rc
= GPG_ERR_UNKNOWN_OPTION
;
3683 return send_error (ctx
, rc
);
3687 do_rename (assuan_context_t ctx
, char *line
)
3689 struct client_s
*client
= assuan_get_pointer (ctx
);
3691 xmlNodePtr src
, dst
;
3692 struct string_s
*str
= NULL
, *tstr
;
3693 struct xml_request_s
*req
;
3696 if (!line
|| !*line
)
3697 return GPG_ERR_SYNTAX
;
3699 args
= str_split (line
, " ", 0);
3700 if (!args
|| strv_length (args
) != 2)
3703 return GPG_ERR_SYNTAX
;
3706 if (!xml_valid_element ((xmlChar
*) args
[1]))
3709 return GPG_ERR_INV_VALUE
;
3712 rc
= xml_new_request (client
, args
[0], XML_CMD_RENAME
, &req
);
3719 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3723 rc
= xml_is_element_owner (client
, src
, 0);
3727 xmlChar
*a
= xmlGetProp (src
, (xmlChar
*) "_name");
3730 rc
= GPG_ERR_ENOMEM
;
3734 /* To prevent unwanted effects:
3736 * <element _name="a"><element _name="b"/></element>
3740 if (xmlStrEqual (a
, (xmlChar
*) args
[1]))
3743 rc
= GPG_ERR_EEXIST
;
3748 str
= string_new (args
[0]);
3751 rc
= GPG_ERR_ENOMEM
;
3755 p
= strrchr (str
->str
, '\t');
3757 tstr
= string_truncate (str
, strlen (str
->str
)-strlen (p
));
3759 tstr
= string_truncate (str
, 0);
3763 string_free (str
, 1);
3764 rc
= GPG_ERR_ENOMEM
;
3769 tstr
= string_append_printf (str
, "%s%s", str
->len
? "\t" : "", args
[1]);
3772 string_free (str
, 1);
3773 rc
= GPG_ERR_ENOMEM
;
3778 xml_free_request (req
);
3779 rc
= xml_new_request (client
, str
->str
, XML_CMD_RENAME
, &req
);
3780 string_free (str
, 1);
3787 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3788 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3793 /* Target may exist:
3795 * <element _name="a"/>
3796 * <element _name="b" target="a"/>
3802 rc
= GPG_ERR_EEXIST
;
3808 rc
= xml_is_element_owner (client
, dst
, 0);
3812 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3814 xml_unlink_node (client
, dst
);
3817 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3820 xml_free_request (req
);
3826 rename_command (assuan_context_t ctx
, char *line
)
3828 struct client_s
*client
= assuan_get_pointer (ctx
);
3830 struct argv_s
*args
[] = {
3831 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3835 rc
= parse_options (&line
, args
, client
, 1);
3836 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3837 rc
= GPG_ERR_SYNTAX
;
3839 return send_error (ctx
, rc
);
3841 rc
= copy_on_write (client
);
3843 return send_error (ctx
, rc
);
3845 if (client
->opts
& OPT_INQUIRE
)
3847 unsigned char *result
;
3850 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3852 return send_error (ctx
, rc
);
3854 pthread_cleanup_push ((void *)xfree
, result
);
3855 rc
= do_rename (ctx
, (char *)result
);
3856 pthread_cleanup_pop (1);
3859 rc
= do_rename (ctx
, line
);
3861 return send_error (ctx
, rc
);
3865 do_copy (assuan_context_t ctx
, char *line
)
3867 struct client_s
*client
= assuan_get_pointer (ctx
);
3868 char **args
, **targs
;
3869 xmlNodePtr src
, dst
, tree
= NULL
;
3870 struct xml_request_s
*req
;
3873 if (!line
|| !*line
)
3874 return GPG_ERR_SYNTAX
;
3876 args
= str_split (line
, " ", 0);
3877 if (!args
|| strv_length (args
) != 2)
3880 return GPG_ERR_SYNTAX
;
3883 targs
= str_split (args
[1], "\t", 0);
3887 return GPG_ERR_SYNTAX
;
3890 if (!xml_valid_element_path (targs
, 0))
3894 return GPG_ERR_INV_VALUE
;
3898 rc
= xml_new_request (client
, args
[0], XML_CMD_NONE
, &req
);
3905 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3909 tree
= xmlCopyNodeList (src
);
3912 rc
= GPG_ERR_ENOMEM
;
3916 xml_free_request (req
);
3917 /* Create the destination element path if it does not exist. */
3918 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &req
);
3925 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3929 rc
= xml_is_element_owner (client
, dst
, 0);
3933 if (!(req
->flags
& XML_REQUEST_FLAG_CREATED
))
3935 rc
= xml_validate_target (client
, req
->doc
, args
[1], src
);
3936 if (rc
|| src
== dst
)
3938 rc
= rc
? rc
: GPG_ERR_EEXIST
;
3943 /* Merge any attributes from the src node to the initial dst node. */
3944 for (xmlAttrPtr attr
= tree
->properties
; attr
; attr
= attr
->next
)
3946 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3949 xmlAttrPtr a
= xmlHasProp (dst
, attr
->name
);
3953 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3954 xmlNewProp (dst
, attr
->name
, tmp
);
3956 /* Create the default attributes. */
3957 rc
= xml_add_attribute (client
, dst
, NULL
, NULL
);
3960 xmlNodePtr n
= dst
->children
;
3961 (void)xml_unlink_node (client
, n
);
3962 dst
->children
= NULL
;
3966 n
= xmlCopyNodeList (tree
->children
);
3969 rc
= GPG_ERR_ENOMEM
;
3973 n
= xmlAddChildList (dst
, n
);
3976 rc
= GPG_ERR_ENOMEM
;
3980 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
)
3981 == dst
->parent
? dst
: dst
->parent
);
3986 (void)xml_unlink_node (client
, tree
);
3988 xml_free_request (req
);
3994 copy_command (assuan_context_t ctx
, char *line
)
3996 struct client_s
*client
= assuan_get_pointer (ctx
);
3998 struct argv_s
*args
[] = {
3999 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
4003 rc
= parse_options (&line
, args
, client
, 1);
4004 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
4005 rc
= GPG_ERR_SYNTAX
;
4007 return send_error (ctx
, rc
);
4009 rc
= copy_on_write (client
);
4011 return send_error (ctx
, rc
);
4013 if (client
->opts
& OPT_INQUIRE
)
4015 unsigned char *result
;
4018 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
4020 return send_error (ctx
, rc
);
4022 pthread_cleanup_push ((void *)xfree
, result
);
4023 rc
= do_copy (ctx
, (char *)result
);
4024 pthread_cleanup_pop (1);
4027 rc
= do_copy (ctx
, line
);
4029 return send_error (ctx
, rc
);
4033 do_move (assuan_context_t ctx
, char *line
)
4035 struct client_s
*client
= assuan_get_pointer (ctx
);
4037 xmlNodePtr src
, dst
;
4038 struct xml_request_s
*req
;
4041 if (!line
|| !*line
)
4042 return GPG_ERR_SYNTAX
;
4044 args
= str_split (line
, " ", 0);
4045 if (!args
|| strv_length (args
) != 2)
4048 return GPG_ERR_SYNTAX
;
4053 char **targs
= str_split (args
[1], "\t", 0);
4057 return GPG_ERR_ENOMEM
;
4060 if (!xml_valid_element_path (targs
, 0))
4064 return GPG_ERR_INV_VALUE
;
4070 rc
= xml_new_request (client
, args
[0], XML_CMD_MOVE
, &req
);
4077 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
4081 rc
= xml_is_element_owner (client
, src
, 0);
4085 xml_free_request (req
);
4089 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
4093 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
4097 dst
= xmlDocGetRootElement (client
->doc
);
4099 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
4104 for (xmlNodePtr n
= dst
; n
; n
= n
->parent
)
4108 rc
= GPG_ERR_EEXIST
;
4115 xmlChar
*a
= xml_attribute_value (src
, (xmlChar
*) "_name");
4116 xmlNodePtr dup
= xml_find_element (client
, dst
->children
, (char *) a
, &rc
);
4119 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
4128 rc
= GPG_ERR_EEXIST
;
4132 if (dst
== xmlDocGetRootElement (client
->doc
))
4137 while (n
->parent
&& n
->parent
!= dst
)
4140 a
= xml_attribute_value (n
, (xmlChar
*) "_name");
4141 xmlChar
*b
= xml_attribute_value (src
, (xmlChar
*) "_name");
4143 if (xmlStrEqual (a
, b
))
4146 xmlUnlinkNode (src
);
4147 (void)xml_unlink_node (client
, n
);
4154 (void)xml_unlink_node (client
, dup
);
4157 xmlUnlinkNode (dup
);
4161 if (!dst
&& req
&& req
->args
&& *req
->args
)
4163 struct xml_request_s
*nreq
= NULL
;
4164 xmlChar
*name
= xml_attribute_value (src
, (xmlChar
*) "_name");
4166 if (src
->parent
== xmlDocGetRootElement (client
->doc
)
4167 && !strcmp ((char *) name
, *req
->args
))
4170 rc
= GPG_ERR_EEXIST
;
4175 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &nreq
);
4178 dst
= xml_find_elements (client
, nreq
,
4179 xmlDocGetRootElement (nreq
->doc
), &rc
);
4180 xml_free_request (nreq
);
4187 xml_update_element_mtime (client
, src
->parent
);
4188 xmlUnlinkNode (src
);
4190 dst
= xmlAddChildList (dst
, src
);
4192 rc
= GPG_ERR_ENOMEM
;
4194 xml_update_element_mtime (client
, dst
->parent
);
4197 xml_free_request (req
);
4203 move_command (assuan_context_t ctx
, char *line
)
4205 struct client_s
*client
= assuan_get_pointer (ctx
);
4207 struct argv_s
*args
[] = {
4208 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
4212 rc
= parse_options (&line
, args
, client
, 1);
4213 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
4214 rc
= GPG_ERR_SYNTAX
;
4216 return send_error (ctx
, rc
);
4218 rc
= copy_on_write (client
);
4220 return send_error (ctx
, rc
);
4222 if (client
->opts
& OPT_INQUIRE
)
4224 unsigned char *result
;
4227 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
4229 return send_error (ctx
, rc
);
4231 pthread_cleanup_push ((void *)xfree
, result
);
4232 rc
= do_move (ctx
, (char *)result
);
4233 pthread_cleanup_pop (1);
4236 rc
= do_move (ctx
, line
);
4238 return send_error (ctx
, rc
);
4242 sort_files (const void *arg1
, const void *arg2
)
4244 char *const *a
= arg1
;
4245 char *const *b
= arg2
;
4247 return strcmp (*a
, *b
);
4251 ls_command (assuan_context_t ctx
, char *line
)
4253 struct client_s
*client
= assuan_get_pointer (ctx
);
4260 struct dirent
*cur
= NULL
;
4261 struct argv_s
*args
[] = {
4262 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4266 rc
= parse_options (&line
, args
, client
, 0);
4268 return send_error (ctx
, rc
);
4270 tmp
= str_asprintf ("%s/data", homedir
);
4271 dir
= expand_homedir (tmp
);
4274 return send_error (ctx
, GPG_ERR_ENOMEM
);
4277 rc
= gpg_error_from_errno (errno
);
4282 return send_error (ctx
, rc
);
4285 pthread_cleanup_push ((void *)closedir
, d
);
4289 while ((cur
= readdir (d
)))
4294 rc
= open_check_file (cur
->d_name
, NULL
, &st
, 1);
4298 if (client
->opts
& OPT_VERBOSE
)
4299 tmp
= str_asprintf ("%s %lu.%lu %lu.%lu %lu.%lu", cur
->d_name
,
4300 st
.st_atim
.tv_sec
, st
.st_atim
.tv_nsec
,
4301 st
.st_mtim
.tv_sec
, st
.st_mtim
.tv_nsec
,
4302 st
.st_ctim
.tv_sec
, st
.st_ctim
.tv_nsec
);
4304 tmp
= str_dup (cur
->d_name
);
4308 rc
= GPG_ERR_ENOMEM
;
4312 vtmp
= strv_cat (v
, tmp
);
4316 rc
= GPG_ERR_ENOMEM
;
4323 pthread_cleanup_pop (1); // closedir (d)
4325 if (rc
&& rc
!= GPG_ERR_ENODEV
)
4328 return send_error (ctx
, rc
);
4334 unsigned n
, t
= strv_length (v
);
4336 qsort (v
, t
, sizeof (char **), sort_files
);
4338 for (n
= 0; n
< t
; n
++)
4340 tmp
= str_asprintf ("%s%s\n", list
? list
: "", v
[n
]);
4344 rc
= GPG_ERR_ENOMEM
;
4355 return send_error (ctx
, rc
? rc
: GPG_ERR_NO_DATA
);
4357 list
[strlen (list
) - 1] = 0;
4358 pthread_cleanup_push (xfree
, list
);
4359 rc
= xfer_data (ctx
, list
, strlen (list
));
4360 pthread_cleanup_pop (1);
4361 return send_error (ctx
, rc
);
4364 /* Run all commands in the command list 'head'. The return code isn't
4365 * considered at all for a command. It is stored in the list and later sent to
4368 dispatch_bulk_commands (struct client_s
*client
, struct sexp_s
**list
)
4374 for (i
= 0; list
&& list
[i
];)
4376 struct bulk_cmd_s
*cur
= list
[i
]->user
;
4378 if (!strcmp (list
[i
]->tag
, "command"))
4383 else if (!strcmp (list
[i
]->tag
, "id"))
4389 if ((client
->flags
& FLAG_NO_PINENTRY
) && cur
->cmd
->inquire
)
4391 rc
= send_status (client
->ctx
, STATUS_BULK
, "BEGIN %s",
4400 client
->bulk_p
= cur
;
4403 cur
->rc
= command_startup (client
->ctx
, cur
->cmd
->name
);
4405 cur
->rc
= cur
->cmd
->handler (client
->ctx
, list
[i
]->data
);
4407 if ((client
->flags
& FLAG_NO_PINENTRY
) && cur
->cmd
->inquire
)
4409 (void)send_status (client
->ctx
, STATUS_BULK
, "END %s",
4412 command_finalize (client
->ctx
, cur
->rc
);
4413 cur
->rc
= cur
->rc
? cur
->rc
: client
->last_rc
;
4416 if (list
[i
+1] && !strcmp (list
[i
+1]->tag
, "rc"))
4418 gpg_error_t erc
= 0;
4421 if (list
[i
+1]->data
)
4423 char **rcs
= str_split (list
[i
+1]->data
, "|", 0);
4426 for (p
= rcs
; p
&& *p
; p
++)
4428 erc
= strtoul (*p
, NULL
, 10);
4445 rc
= dispatch_bulk_commands (client
, list
[++i
]->next
);
4459 bulk_command (assuan_context_t ctx
, char *line
)
4461 struct client_s
*client
= assuan_get_pointer (ctx
);
4463 unsigned char *result
, *p
;
4465 struct sexp_s
**sexp
= NULL
;
4466 struct argv_s
*args
[] = {
4467 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
4471 rc
= parse_options (&line
, args
, client
, 1);
4473 return send_error (ctx
, rc
);
4475 if ((client
->opts
& OPT_INQUIRE
) && line
&& *line
)
4476 rc
= GPG_ERR_SYNTAX
;
4477 else if (!(client
->opts
& OPT_INQUIRE
) && (!line
|| !*line
))
4478 rc
= GPG_ERR_SYNTAX
;
4481 return send_error (ctx
, rc
);
4483 if (client
->opts
& OPT_INQUIRE
)
4485 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
4487 return send_error (ctx
, rc
);
4492 p
= (unsigned char *)line
;
4493 len
= line
&& *line
? strlen (line
) : 0;
4497 return send_error (ctx
, GPG_ERR_SYNTAX
);
4499 rc
= bulk_parse_commands (&sexp
, (const char *)p
, command_table
);
4500 if (client
->opts
& OPT_INQUIRE
)
4504 return send_error (ctx
, rc
);
4506 pthread_cleanup_push ((void *)bulk_free_list
, sexp
);
4507 rc
= dispatch_bulk_commands (client
, sexp
);
4508 pthread_cleanup_pop (0);
4510 rc
= bulk_build_result (sexp
, (char **)&result
);
4512 bulk_free_list (sexp
);
4516 pthread_cleanup_push ((void *)xfree
, result
);
4517 rc
= xfer_data (ctx
, (char *)result
, strlen ((char *)result
));
4518 pthread_cleanup_pop (1);
4521 return send_error (ctx
, rc
);
4525 nop_command (assuan_context_t ctx
, char *line
)
4527 return send_error (ctx
, 0);
4531 bye_notify (assuan_context_t ctx
, char *line
)
4533 struct client_s
*cl
= assuan_get_pointer (ctx
);
4534 gpg_error_t ret
= 0;
4537 update_client_state (cl
, CLIENT_STATE_DISCON
);
4541 if (cl
->thd
->remote
)
4547 struct timeval tv
= { 0, 50000 };
4549 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_WR
);
4550 if (rc
== GNUTLS_E_AGAIN
)
4551 select (0, NULL
, NULL
, NULL
, &tv
);
4553 while (rc
== GNUTLS_E_AGAIN
);
4557 /* This will let assuan_process_next() return. */
4558 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
4560 cl
->last_rc
= gpg_error_from_errno (errno
);
4564 cl
->last_rc
= 0; // BYE command result
4569 reset_notify (assuan_context_t ctx
, char *line
)
4571 struct client_s
*client
= assuan_get_pointer (ctx
);
4575 reset_client (client
);
4581 * This is called before every Assuan command.
4584 command_startup (assuan_context_t ctx
, const char *name
)
4586 struct client_s
*client
= assuan_get_pointer (ctx
);
4588 struct command_table_s
*cmd
= NULL
;
4590 log_write2 ("command='%s'", name
);
4591 client
->last_rc
= client
->opts
= 0;
4593 for (int i
= 0; command_table
[i
]; i
++)
4595 if (!strcasecmp (name
, command_table
[i
]->name
))
4597 if (command_table
[i
]->ignore_startup
)
4600 cmd
= command_table
[i
];
4606 return GPG_ERR_UNKNOWN_COMMAND
;
4608 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
4611 if (!client
->bulk_p
&& !rc
)
4612 update_client_state (client
, CLIENT_STATE_COMMAND
);
4618 * This is called after every Assuan command.
4621 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
4623 struct client_s
*client
= assuan_get_pointer (ctx
);
4625 // Needed for the STORE command. Really should be an OPT_.
4626 client
->flags
&= ~(FLAG_NO_INHERIT_ACL
);
4628 if (!(client
->flags
& FLAG_LOCK_CMD
))
4629 unlock_file_mutex (client
, 0);
4631 unlock_flock (&client
->flock_fd
);
4632 log_write2 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
4635 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
4638 if (!client
->bulk_p
&& client
->thd
->state
!= CLIENT_STATE_DISCON
)
4639 update_client_state (client
, CLIENT_STATE_IDLE
);
4641 client
->bulk_p
= NULL
;
4645 parse_help_opt_html (void *data
, void *value
)
4647 struct client_s
*client
= data
;
4650 client
->opts
|= OPT_HTML
;
4655 help_command (assuan_context_t ctx
, char *line
)
4659 struct client_s
*client
= assuan_get_pointer (ctx
);
4660 struct argv_s
*args
[] = {
4661 &(struct argv_s
) {"html", OPTION_TYPE_NOARG
, parse_help_opt_html
},
4665 rc
= parse_options (&line
, args
, client
, 1);
4667 return send_error (ctx
, rc
);
4669 if (!line
|| !*line
)
4672 char *help
= str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
4673 "For commands that take an element path as an argument, each element is "
4674 "separated with an ASCII @key{TAB} character (ASCII 0x09). Not all help text is available here. For complete documentation refer to the texinfo or HTML manual."
4677 for (i
= 0; command_table
[i
]; i
++)
4679 if (!command_table
[i
]->help
)
4682 /* @npxref{} won't put a "See " or "see " in front of the command.
4683 * It's not a texinfo command but needed for --html. */
4684 tmp
= str_asprintf ("%s %s%s%s", help
,
4685 client
->opts
& OPT_HTML
? "@npxref{" : "",
4686 command_table
[i
]->name
,
4687 client
->opts
& OPT_HTML
? "}" : "");
4692 tmp
= strip_texi_and_wrap (help
, client
->opts
& OPT_HTML
);
4694 pthread_cleanup_push ((void *)xfree
, tmp
);
4695 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4696 pthread_cleanup_pop (1);
4697 return send_error (ctx
, rc
);
4700 for (i
= 0; command_table
[i
]; i
++)
4702 if (!strcasecmp (line
, command_table
[i
]->name
))
4706 if (!command_table
[i
]->help
)
4709 help
= strip_texi_and_wrap (command_table
[i
]->help
,
4710 client
->opts
& OPT_HTML
);
4711 tmp
= str_asprintf ("%s%s",
4712 (client
->opts
& OPT_HTML
) ? "" : _("Usage: "),
4715 pthread_cleanup_push ((void *)xfree
, tmp
);
4716 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4717 pthread_cleanup_pop (1);
4718 return send_error (ctx
, rc
);
4722 return send_error (ctx
, GPG_ERR_INV_NAME
);
4726 new_command (const char *name
, int inquire
, int ignore
, int unlock
,
4727 int flock_type
, gpg_error_t (*handler
) (assuan_context_t
, char *),
4731 struct command_table_s
**tmp
;
4734 for (i
= 0; command_table
[i
]; i
++);
4736 tmp
= xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
4738 command_table
= tmp
;
4739 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
4740 command_table
[i
]->name
= name
;
4741 command_table
[i
]->inquire
= inquire
;
4742 command_table
[i
]->handler
= handler
;
4743 command_table
[i
]->ignore_startup
= ignore
;
4744 command_table
[i
]->unlock
= unlock
;
4745 command_table
[i
]->flock_type
= flock_type
;
4746 command_table
[i
++]->help
= help
;
4747 command_table
[i
] = NULL
;
4755 for (i
= 0; command_table
[i
]; i
++)
4756 xfree (command_table
[i
]);
4758 xfree (command_table
);
4762 sort_commands (const void *arg1
, const void *arg2
)
4764 struct command_table_s
*const *a
= arg1
;
4765 struct command_table_s
*const *b
= arg2
;
4774 return strcmp ((*a
)->name
, (*b
)->name
);
4778 passwd_command (assuan_context_t ctx
, char *line
)
4780 struct client_s
*client
= assuan_get_pointer (ctx
);
4784 rc
= peer_is_invoker (client
);
4786 return send_error (ctx
, (rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
));
4788 if (client
->flags
& FLAG_NEW
)
4789 return send_error (ctx
, GPG_ERR_INV_STATE
);
4791 client
->crypto
->keyfile
= config_get_string (client
->filename
,
4793 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
,
4794 client
->crypto
->keyfile
);
4796 rc
= crypto_passwd (client
, client
->crypto
);
4798 crypto_free_non_keys (client
->crypto
);
4799 return send_error (ctx
, rc
);
4803 parse_opt_data (void *data
, void *value
)
4805 struct client_s
*client
= data
;
4808 client
->opts
|= OPT_DATA
;
4813 send_client_list (assuan_context_t ctx
)
4815 struct client_s
*client
= assuan_get_pointer (ctx
);
4818 if (client
->opts
& OPT_VERBOSE
)
4824 MUTEX_LOCK (&cn_mutex
);
4825 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
4826 t
= slist_length (cn_thread_list
);
4828 for (i
= 0; i
< t
; i
++)
4830 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4833 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4836 tmp
= build_client_info_line (thd
, 0);
4839 char **l
= strv_cat (list
, tmp
);
4841 rc
= GPG_ERR_ENOMEM
;
4846 rc
= GPG_ERR_ENOMEM
;
4855 pthread_cleanup_pop (1);
4859 line
= strv_join ("\n", list
);
4861 pthread_cleanup_push ((void *)xfree
, line
);
4862 rc
= xfer_data (ctx
, line
, strlen (line
));
4863 pthread_cleanup_pop (1);
4867 if (client
->opts
& OPT_DATA
)
4869 char buf
[ASSUAN_LINELENGTH
];
4871 MUTEX_LOCK (&cn_mutex
);
4872 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
4873 MUTEX_UNLOCK (&cn_mutex
);
4874 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4877 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4883 getinfo_command (assuan_context_t ctx
, char *line
)
4885 struct client_s
*client
= assuan_get_pointer (ctx
);
4887 char buf
[ASSUAN_LINELENGTH
];
4888 struct argv_s
*args
[] = {
4889 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4890 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4894 rc
= parse_options (&line
, args
, client
, 1);
4896 return send_error (ctx
, rc
);
4898 if (!line
|| !*line
)
4899 return send_error (ctx
, GPG_ERR_SYNTAX
);
4901 if (!strcasecmp (line
, "clients"))
4903 rc
= send_client_list (ctx
);
4905 else if (!strcasecmp (line
, "cache"))
4907 if (client
->opts
& OPT_DATA
)
4909 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4910 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4913 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4915 else if (!strcasecmp (line
, "pid"))
4917 long pid
= getpid ();
4919 snprintf (buf
, sizeof (buf
), "%li", pid
);
4920 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4922 else if (!strcasecmp (line
, "version"))
4924 char *tmp
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4932 pthread_cleanup_push (xfree
, tmp
);
4933 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4934 pthread_cleanup_pop (1);
4936 else if (!strcasecmp (line
, "last_error"))
4938 if (client
->last_error
)
4939 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4941 rc
= GPG_ERR_NO_DATA
;
4943 else if (!strcasecmp (line
, "user"))
4948 if (client
->thd
->remote
)
4949 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4951 user
= get_username (client
->thd
->peer
->uid
);
4953 user
= get_username (client
->thd
->peer
->uid
);
4957 pthread_cleanup_push ((void *)xfree
, user
);
4958 rc
= xfer_data (ctx
, user
, strlen (user
));
4959 pthread_cleanup_pop (1);
4962 rc
= GPG_ERR_NO_DATA
;
4965 rc
= gpg_error (GPG_ERR_SYNTAX
);
4967 return send_error (ctx
, rc
);
4971 parse_opt_secret (void *data
, void *value
)
4973 struct client_s
*client
= data
;
4976 client
->opts
|= OPT_SECRET
;
4981 parse_keyinfo_opt_learn (void *data
, void *value
)
4983 struct client_s
*client
= data
;
4986 client
->opts
|= OPT_KEYINFO_LEARN
;
4991 keyinfo_command (assuan_context_t ctx
, char *line
)
4993 struct client_s
*client
= assuan_get_pointer (ctx
);
4995 char **keys
= NULL
, **p
= NULL
;
4997 struct argv_s
*args
[] = {
4998 &(struct argv_s
) {"learn", OPTION_TYPE_NOARG
, parse_keyinfo_opt_learn
},
5002 rc
= parse_options (&line
, args
, client
, 0);
5004 return send_error (ctx
, rc
);
5007 return send_error (ctx
, GPG_ERR_SYNTAX
);
5009 if (!(client
->flags
& FLAG_OPEN
))
5010 return send_error (ctx
, GPG_ERR_INV_STATE
);
5012 if (client
->opts
& OPT_KEYINFO_LEARN
)
5014 rc
= cache_agent_command ("LEARN");
5015 return send_error (ctx
, rc
);
5018 if (client
->flags
& FLAG_NEW
)
5019 return send_error (ctx
, GPG_ERR_NO_DATA
);
5021 rc
= lock_flock (ctx
, client
->filename
, FLOCK_TYPE_SH
, &client
->flock_fd
);
5023 return send_error (ctx
, rc
);
5025 rc
= crypto_is_symmetric (client
->filename
);
5026 unlock_flock (&client
->flock_fd
);
5029 else if (rc
!= GPG_ERR_BAD_DATA
)
5030 return send_error (ctx
, rc
);
5035 p
= strv_catv (keys
, client
->crypto
->pubkey
);
5037 rc
= GPG_ERR_ENOMEM
;
5042 if (!rc
&& client
->crypto
->sigkey
)
5044 if (!strv_printf (&keys
, "S%s", client
->crypto
->sigkey
))
5047 return send_error (ctx
, GPG_ERR_ENOMEM
);
5055 line
= strv_join ("\n", keys
);
5057 pthread_cleanup_push ((void *)xfree
, line
);
5059 rc
= xfer_data (ctx
, line
, strlen (line
));
5061 rc
= GPG_ERR_ENOMEM
;
5063 pthread_cleanup_pop (1);
5066 rc
= GPG_ERR_NO_DATA
;
5071 return send_error (ctx
, rc
);
5075 deletekey_command (assuan_context_t ctx
, char *line
)
5078 struct client_s
*client
= assuan_get_pointer (ctx
);
5079 struct crypto_s
*crypto
= NULL
;
5080 char *keys
[] = { NULL
, NULL
};
5081 gpgme_key_t
*result
;
5084 if (!(client
->flags
& FLAG_OPEN
))
5085 return send_error (ctx
, GPG_ERR_INV_STATE
);
5087 rc
= peer_is_invoker (client
);
5089 return send_error (ctx
, (rc
== GPG_ERR_EACCES
? GPG_ERR_FORBIDDEN
: rc
));
5091 if (client
->flags
& FLAG_NO_PINENTRY
)
5092 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
5095 return send_error (ctx
, GPG_ERR_SYNTAX
);
5097 rc
= crypto_keyid_to_16b_once (line
);
5099 return send_error (ctx
, rc
);
5101 for (p
= client
->crypto
->pubkey
; p
&& *p
; p
++)
5103 if (!strcmp (*p
, line
))
5109 if (client
->crypto
->sigkey
&& strcmp (client
->crypto
->sigkey
, line
))
5110 return send_error (ctx
, GPG_ERR_FORBIDDEN
);
5111 else if (!client
->crypto
->sigkey
)
5112 return send_error (ctx
, GPG_ERR_NO_SECKEY
);
5115 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
5116 client
->flags
& FLAG_NO_PINENTRY
, NULL
);
5118 return send_error (ctx
, rc
);
5120 pthread_cleanup_push ((void *)crypto_free
, crypto
);
5122 rc
= crypto_list_keys (crypto
, keys
, 1, &result
);
5125 pthread_cleanup_push ((void *)crypto_free_key_list
, result
);
5126 rc
= crypto_delete_key (client
, crypto
, *result
, 1);
5127 pthread_cleanup_pop (1);
5130 pthread_cleanup_pop (1);
5131 return send_error (ctx
, rc
);
5135 kill_command (assuan_context_t ctx
, char *line
)
5137 struct client_s
*client
= assuan_get_pointer (ctx
);
5141 if (!line
|| !*line
)
5142 return send_error (ctx
, GPG_ERR_SYNTAX
);
5144 MUTEX_LOCK (&cn_mutex
);
5145 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
5146 t
= slist_length (cn_thread_list
);
5149 for (i
= 0; i
< t
; i
++)
5151 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
5152 char *tmp
= str_asprintf ("%p", thd
->tid
);
5154 if (strcmp (line
, tmp
))
5161 rc
= peer_is_invoker (client
);
5164 #ifdef HAVE_PTHREAD_CANCEL
5165 pthread_cancel (thd
->tid
);
5167 pthread_kill (thd
->tid
, SIGUSR2
);
5171 else if (rc
== GPG_ERR_EACCES
)
5172 rc
= GPG_ERR_FORBIDDEN
;
5176 if (config_get_boolean ("global", "strict_kill"))
5180 if (client
->thd
->remote
&& thd
->remote
)
5182 if (!thd
->tls
|| !thd
->tls
->fp
)
5184 rc
= GPG_ERR_INV_STATE
;
5187 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
5189 #ifdef HAVE_PTHREAD_CANCEL
5190 pthread_cancel (thd
->tid
);
5192 pthread_kill (thd
->tid
, SIGUSR2
);
5198 else if (!client
->thd
->remote
&& !thd
->remote
)
5201 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
5203 #ifdef HAVE_PTHREAD_CANCEL
5204 pthread_cancel (thd
->tid
);
5206 pthread_kill (thd
->tid
, SIGUSR2
);
5214 pthread_cleanup_pop (1);
5215 return send_error (ctx
, rc
);
5219 listkeys_command (assuan_context_t ctx
, char *line
)
5221 struct client_s
*client
= assuan_get_pointer (ctx
);
5222 struct crypto_s
*crypto
= NULL
;
5223 char **pattern
= NULL
;
5224 gpgme_key_t
*keys
= NULL
;
5225 char **result
= NULL
;
5227 struct argv_s
*args
[] = {
5228 &(struct argv_s
) {"secret-only", OPTION_TYPE_NOARG
, parse_opt_secret
},
5232 rc
= parse_options (&line
, args
, client
, 1);
5234 return send_error (ctx
, rc
);
5236 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
5237 client
->flags
& FLAG_NO_PINENTRY
, NULL
);
5239 return send_error (ctx
, rc
);
5241 pthread_cleanup_push ((void *)crypto_free
, crypto
);
5243 pattern
= str_split (line
, ",", 0);
5244 pthread_cleanup_push ((void *)(void *)strv_free
, pattern
);
5245 rc
= crypto_list_keys (crypto
, pattern
, client
->opts
& OPT_SECRET
,
5247 pthread_cleanup_pop (1);
5248 pthread_cleanup_pop (1);
5253 for (i
= 0; keys
[i
]; i
++)
5255 char *p
= crypto_key_info (keys
[i
]);
5260 rc
= GPG_ERR_ENOMEM
;
5264 r
= strv_cat (result
, p
);
5267 rc
= GPG_ERR_ENOMEM
;
5275 rc
= GPG_ERR_NO_DATA
;
5277 crypto_free_key_list (keys
);
5282 line
= strv_join ("\n", result
);
5284 pthread_cleanup_push ((void *)xfree
, line
);
5286 rc
= GPG_ERR_ENOMEM
;
5288 rc
= xfer_data (ctx
, line
, strlen (line
));
5290 pthread_cleanup_pop (1);
5295 if (gpg_err_code (rc
) == GPG_ERR_NO_PUBKEY
5296 || gpg_err_code (rc
) == GPG_ERR_NO_SECKEY
)
5297 rc
= GPG_ERR_NO_DATA
;
5299 return send_error (ctx
, rc
);
5305 /* !BEGIN-HELP-TEXT!
5307 * This comment is used as a marker to generate the offline documentation
5308 * found in doc/pwmd.info. The indentation needs to be kept for the awk
5309 * script to determine where commands begin and end.
5311 new_command("HELP", 0, 1, 1, 0, help_command
, _(
5312 "HELP [--html] [<COMMAND>]\n" /* Showing available commands. */
5313 "Show available commands or command specific help text."
5315 "The @option{--html} option will output the help text in HTML format."
5318 new_command("DELETEKEY", 0, 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, deletekey_command
, _(
5319 "DELETEKEY <keyid>\n" /* Deleting a key from the key ring. */
5320 "Deletes the public and secret key associated with key @var{keyid} from the "
5321 "keyring. The @var{keyid} must be one associated with the currently opened "
5323 "Note that no confirmation occurs. Also note that when the key is deleted, "
5324 "the current or other data files using this key will no longer be able to be "
5328 new_command("KILL", 0, 1, 0, 0, kill_command
, _(
5329 "KILL <thread_id>\n" /* Terminating another client. */
5330 "Terminates the client identified by @var{thread_id} and releases any file "
5331 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
5332 "for details about listing connected clients. An @code{invoking_user} "
5333 "(@pxref{Configuration}) may kill any client while others may only kill "
5334 "clients of the same @code{UID} or @abbr{TLS} fingerprint."
5337 new_command("LISTKEYS", 0, 1, 0, 0, listkeys_command
, _(
5338 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n" /* Listing keys in the key ring. */
5339 "Returns a new line separated list of key information matching a comma "
5340 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
5341 "specified, only keys matching @var{pattern} that also have a secret key "
5342 "available will be returned."
5345 new_command("KEYINFO", 0, 1, 0, 0, keyinfo_command
, _(
5346 "KEYINFO [--learn]\n" /* Showing keys used for the current data file. */
5347 "Returns a new line separated list of key ID's that the currently opened "
5348 "data file has recipients and signers for. If the key is a signing key it "
5349 "will be prefixed with an @code{S}. If the file is a new one, or has no "
5350 "signers in the case of being symmetrically encrypted, the error code "
5351 "@code{GPG_ERR_NO_DATA} is returned."
5353 "When the @option{--learn} option is passed, keys on a smartcard will be "
5357 new_command("GETINFO", 0, 1, 1, 0, getinfo_command
, _(
5358 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n" /* Obtaining server and client information. */
5359 "Get server and other information. The information is returned via a status "
5360 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
5363 "@var{CACHE} returns the number of cached documents."
5365 "@var{CLIENTS} returns the number of "
5366 "connected clients via a status message or a list of connected clients when "
5367 "the @option{--verbose} parameter is used (implies @option{--data}). A "
5368 "verbose line of a client list contains "
5370 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
5371 "IP address if remote, file lock status, whether the current client is self "
5372 "or not, client state (see below), "
5373 "user ID or TLS fingerprint of the connected client, username if the "
5374 "client is a local one else @code{-}, and finally the time stamp of when the "
5377 "Client state @code{0} is an unknown client state, state @code{1} indicates "
5378 "the client has connected but hasn't completed initializing, state @code{2} "
5379 "indicates that the client is idle, state @code{3} means the "
5380 "client is in a command and state @code{4} means the client is disconnecting. "
5382 "@var{PID} returns the process ID number of the server via a data response."
5384 "@var{VERSION} returns the server version number and compile-time features "
5385 "via a data response with each being space delimited."
5387 "@var{LAST_ERROR} returns a detailed description of the last failed command "
5388 "via a data response, when available."
5390 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
5391 "via a data response."
5394 new_command("PASSWD", 1, 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
5395 "PASSWD\n" /* Changing the passphrase for a key. */
5396 "Changes the passphrase of the secret key required to open the current "
5397 "data file. If the data file is symmetrically encrypted the error "
5398 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted "
5399 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
5400 "this command saving any unwanted changes to the @abbr{XML} document."
5402 "Note that when the current data file has been either encrypted or signed "
5403 "with a key stored on a smartcard this command will return an error. In this "
5404 "case you should instead use @command{gpg --card-edit} to change the "
5405 "pin of the smartcard or @command{gpg --edit-key} to change the passphrase "
5406 "of the key used to sign or encrypt the data file."
5408 "This command is not available to non-invoking clients "
5409 "(@pxref{Access Control})."
5412 new_command("OPEN", 1, 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
5413 "OPEN [--lock] <filename>\n" /* Opening a data file. */
5414 "Opens @var{filename}. When the @var{filename} is not found on the "
5415 "file-system then a new in-memory document will be created. If the file is "
5416 "found, it is looked for in the file cache and when found no passphrase will "
5417 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
5418 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
5419 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
5420 "@emph{INQUIRE} the client for the passphrase. Note than when configuration "
5421 "option @option{strict_open} is enabled and the client is not an "
5422 "@option{invoking_user}, an error will be returned when the data file does "
5425 "When the @option{--lock} option is passed then the file mutex will be "
5426 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
5427 "file had been opened."
5430 new_command("GENKEY", 1, 1, 1, 0, genkey_command
, _(
5431 "GENKEY --subkey-of=fpr | --userid=\"str\" [--no-expire | --expire=N] [--algo=\"str\"] [--no-passphrase] [--usage=\"default|sign|encrypt\"]\n" /* Generating a new key. */
5432 "Generates a new key based on option arguments. One of "
5433 "@option{--subkey-of} or @option{--userid} is "
5434 "required. The @option{--subkey-of} option will generate a subkey for the key "
5435 "of the specified fingerprint."
5438 new_command("SAVE", 1, 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
5439 "SAVE [--sign-keyid=[<fpr>]] [--symmetric | --keyid=<fpr>[,..] | --inquire-keyid]\n" /* Saving document changes to disk. */
5440 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
5441 "file that was opened when using the @code{OPEN} command (@pxref{OPEN})."
5443 "If the file is a new one one of @option{--symmetric}, @option{--keyid} or"
5444 "@option{--inquire-keyid} is required. When not @option{--symmetric} the "
5445 "option @option{--sign-keyid} is also required but optional otherwise."
5447 "You can encrypt the data file to a recipient other than the one that it "
5448 "was originally encrypted with by passing the @option{--keyid} or "
5449 "@option{--inquire-keyid} option with a comma separated list of "
5450 "public encryption key fingerprints as its argument. Use the "
5451 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
5452 "pattern. The @option{--sign-keyid} option may also be used to sign the data "
5453 "file with an alternate key by specifying the fingerprint of a signing key. "
5454 "Only one signing key is supported unlike the @option{--keyid} option. "
5455 "A passphrase to decrypt the data file "
5456 "will be required when one or more of the original encryption keys or signing "
5457 "key are not found in either of these two options' arguments or when the data "
5458 "file is symmetrically encrypted regardless of the @code{require_save_key} "
5459 "configuration parameter. The original encryption keys and signing key will be "
5460 "used when neither of these options are specified."
5462 "The @option{--keyid} and @option{--sign-keyid} options are not available "
5463 "to non-invoking clients "
5464 "(@pxref{Access Control}) when the recipients or signer do not match those "
5465 "that were used when the file was @code{OPEN}'ed."
5467 "The @option{--symmetric} option specifies that a new data file be "
5468 "conventionally encrypted. These types of data files do not use a recipient "
5469 "public key but may optionally be signed by using the @option{--sign-keyid} "
5470 "option. To remove the signing key from a symmtrically encrypted data file, "
5471 "leave the option value empty."
5473 "Note that you cannot change encryption schemes once a data file has been "
5477 new_command("ISCACHED", 0, 1, 0, 0, iscached_command
, _(
5478 "ISCACHED [--lock] [--agent [--sign]] <filename>\n" /* Testing cache status. */
5479 "Determines the file cache status of the specified @var{filename}. "
5480 "The default is to test whether the filename is cached in memory. Passing "
5481 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
5482 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
5483 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
5484 "Both the @option{--agent} and @option{--sign} options require an opened data "
5487 "An @emph{OK} response is returned if the specified @var{filename} is found "
5488 "in the cache. If not found in the cache but exists on the filesystem "
5489 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
5492 "The @option{--lock} option will lock the file mutex of @var{filename} when "
5493 "the file exists; it does not need to be opened nor cached. The lock will be "
5494 "released when the client exits or sends the @code{UNLOCK} command "
5495 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
5498 new_command("CLEARCACHE", 0, 1, 1, 0, clearcache_command
, _(
5499 "CLEARCACHE [<filename>]\n" /* Removing a cache entry. */
5500 "Clears a file cache entry for all or the specified @var{filename}. Note that "
5501 "this will also clear any @command{gpg-agent} cached keys which may cause "
5502 "problems if another data file shares the same keys as @var{filename}."
5504 "When clearing all cache entries a permissions test is done against the "
5505 "current client based on the @var{allowed} configuration parameter in a "
5506 "@var{filename} section. Both a cache entry may be cleared and an error "
5507 "returned depending on cached data files and client permissions."
5510 new_command("CACHETIMEOUT", 0, 1, 1, 0, cachetimeout_command
, _(
5511 "CACHETIMEOUT <seconds>\n" /* Setting the cache timeout. */
5512 "The time in @var{seconds} until the currently opened data file will be "
5513 "removed from the cache. @code{-1} will keep the cache entry forever, "
5514 "@code{0} will require the passphrase for each @code{OPEN} command "
5515 "(@pxref{OPEN}) or @code{SAVE} (@pxref{SAVE}) command. @xref{Configuration}, "
5516 "and the @code{cache_timeout} parameter."
5519 new_command("LIST", 0, 0, 1, 0, list_command
, _(
5520 "LIST [--inquire] [--recurse] [--sexp] [element[<TAB>child[..]]]\n" /* Showing document elements. */
5521 "If no element path is given then a newline separated list of root elements "
5522 "is returned with a data response. If given, then children of the specified "
5523 "element path are returned."
5525 "Each element path "
5526 "returned will have zero or more flags appened to it. These flags are "
5527 "delimited from the element path by a single space character. A flag itself "
5528 "is a single character. Flag @code{P} indicates that access to the element "
5529 "is denied. Flag @code{+} indicates that there are child nodes of "
5530 "the current element path. Flag @code{E} indicates that an element of the "
5531 "element path contained in a @var{target} attribute could not be found. Flag "
5532 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
5533 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
5534 "then an element path, is the element path of the @var{target} attribute "
5535 "contained in the current element."
5537 "When a specified element path contains an error either from the final "
5538 "element in the path or any previous element, the path is still shown but "
5539 "will contain the error flag for the element with the error. Determining "
5540 "the actual element which contains the error is up to the client. This can be "
5541 "done by traversing the final element up to parent elements that contain the "
5544 "The option @option{--recurse} may be used to list the entire element tree "
5545 "for a specified element path or the entire tree for all root elements."
5547 "The option @option{--sexp} outputs the list in an s-expression and also "
5548 "appends an elements' attributes and attribute values. The syntax is:\n"
5552 " (4:path N:<path> 5:flags N:<flags>\n"
5553 " (5:attrs N:<name> N:<value> ...)\n"
5559 "When the @option{--inquire} option is passed then all remaining non-option "
5560 "arguments are retrieved via a server @emph{INQUIRE}."
5563 new_command("REALPATH", 0, 0, 1, 0, realpath_command
, _(
5564 "REALPATH [--inquire] element[<TAB>child[..]]\n" /* Resolving an element. */
5565 "Resolves all @code{target} attributes of the specified element path and "
5566 "returns the result with a data response. @xref{Target Attribute}, for details."
5568 "When the @option{--inquire} option is passed then all remaining non-option "
5569 "arguments are retrieved via a server @emph{INQUIRE}."
5572 new_command("STORE", 0, 0, 1, 0, store_command
, _(
5573 "STORE [--no-inherit-acl] element[<TAB>child[..]]<TAB>[content]\n" /* Modifying the content of an element. */
5574 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5576 "Creates a new element path or modifies the @var{content} of an existing "
5577 "element. If only a single element is specified then a new root element is "
5578 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
5579 "set to the final @key{TAB} delimited element. If no @var{content} is "
5580 "specified after the final @key{TAB}, then the content of the existing "
5581 "element will be removed; or will be empty if creating a new element."
5583 "The option @option{--no-inherit-acl} prevents a newly created element from "
5584 "inheriting the value of the parent element @code{_acl} attribute. In either "
5585 "case, the current user is made the owner of the newly created element(s)."
5587 "The only restriction of an element name is that it not contain whitespace "
5588 "characters. There is no other whitespace between the @key{TAB} delimited "
5589 "elements. It is recommended that the content of an element be base64 encoded "
5590 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
5591 "parsing and @command{pwmd} syntax errors."
5594 new_command("RENAME", 0, 0, 1, 0, rename_command
, _(
5595 "RENAME [--inquire] element[<TAB>child[..]] <value>\n" /* Renaming an element. */
5596 "Renames the specified @var{element} to the new @var{value}. If an element of "
5597 "the same name as the @var{value} already exists it will be overwritten."
5599 "When the @option{--inquire} option is passed then all remaining non-option "
5600 "arguments are retrieved via a server @emph{INQUIRE}."
5603 new_command("COPY", 0, 0, 1, 0, copy_command
, _(
5604 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n" /* Copying an element. */
5605 "Copies the entire element tree starting from the child node of the source "
5606 "element, to the destination element path. If the destination element path "
5607 "does not exist then it will be created; otherwise it is overwritten."
5609 "Note that attributes from the source element are merged into the "
5610 "destination element when the destination element path exists. When an "
5611 "attribute of the same name exists in both the source and destination "
5612 "elements then the destination attribute will be updated to the source "
5615 "When the @option{--inquire} option is passed then all remaining non-option "
5616 "arguments are retrieved via a server @emph{INQUIRE}."
5619 new_command("MOVE", 0, 0, 1, 0, move_command
, _(
5620 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n" /* Moving an element. */
5621 "Moves the source element path to the destination element path. If the "
5622 "destination is not specified then it will be moved to the root node of the "
5623 "document. If the destination is specified and exists then it will be "
5624 "overwritten; otherwise non-existing elements of the destination element "
5625 "path will be created."
5627 "When the @option{--inquire} option is passed then all remaining non-option "
5628 "arguments are retrieved via a server @emph{INQUIRE}."
5631 new_command("DELETE", 0, 0, 1, 0, delete_command
, _(
5632 "DELETE [--inquire] element[<TAB>child[..]]\n" /* Deleting an element. */
5633 "Removes the specified element path and all of its children. This may break "
5634 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
5635 "refers to this element or any of its children."
5637 "When the @option{--inquire} option is passed then all remaining non-option "
5638 "arguments are retrieved via a server @emph{INQUIRE}."
5641 new_command("GET", 0, 0, 1, 0, get_command
, _(
5642 "GET [--inquire] element[<TAB>child[..]]\n" /* Getting the content of an element. */
5643 "Retrieves the content of the specified element. The content is returned "
5644 "with a data response."
5646 "When the @option{--inquire} option is passed then all remaining non-option "
5647 "arguments are retrieved via a server @emph{INQUIRE}."
5650 new_command("ATTR", 0, 0, 1, 0, attr_command
, _(
5651 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n" /* Modifying element attributes. */
5653 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
5654 " Stores or updates an @var{attribute} name and optional @var{value} of an "
5655 "element. When no @var{value} is specified any existing value will be removed."
5657 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
5658 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
5659 "or @code{target} an error is returned. Use the @command{DELETE} command "
5660 "(@pxref{DELETE}) instead."
5662 "@item ATTR LIST element[<TAB>child[..]]\n"
5663 " Retrieves a newline separated list of attributes names and values "
5664 "from the specified element. Each attribute name and value is space delimited."
5666 "@item ATTR GET attribute element[<TAB>child[..]]\n"
5667 " Retrieves the value of an @var{attribute} from an element."
5670 "When the @option{--inquire} option is passed then all remaining non-option "
5671 "arguments are retrieved via a server @emph{INQUIRE}."
5673 "@xref{Target Attribute}, for details about this special attribute and also "
5674 "@pxref{Other Attributes} for other attributes that are handled specially "
5675 "by @command{pwmd}."
5678 new_command("XPATH", 0, 0, 1, 0, xpath_command
, _(
5679 "XPATH [--inquire] <expression>[<TAB>[value]]\n" /* Modifying more than one element. */
5680 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
5681 "specified it is assumed the expression is a request to return a result. "
5682 "Otherwise, the result is set to the @var{value} argument and the document is "
5683 "updated. If there is no @var{value} after the @key{TAB} character, the value "
5684 "is assumed to be empty and the document is updated. For example:"
5687 "XPATH //element[@@_name='password']@key{TAB}\n"
5690 "would clear the content of all @var{password} elements in the data file "
5691 "while leaving off the trailing @key{TAB} would return all @var{password} "
5692 "elements in @abbr{XML} format."
5694 "When the @option{--inquire} option is passed then all remaining non-option "
5695 "arguments are retrieved via a server @emph{INQUIRE}."
5697 "See @url{https://www.w3schools.com/xml/xpath_intro.asp} for @abbr{XPATH} "
5698 "expression syntax."
5701 new_command("XPATHATTR", 0, 0, 1, 0, xpathattr_command
, _(
5702 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n" /* Modifying more than one element's attributes. */
5703 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
5704 "attributes and does not return a result. For the @var{SET} operation the "
5705 "@var{value} is optional but the field is required. If not specified then "
5706 "the attribute value will be empty. For example:"
5709 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
5712 "would create a @var{password} attribute for each @var{password} element "
5713 "found in the document. The attribute value will be empty but still exist."
5715 "When the @option{--inquire} option is passed then all remaining non-option "
5716 "arguments are retrieved via a server @emph{INQUIRE}."
5718 "See @url{https://www.w3schools.com/xml/xpath_intro.asp} for @abbr{XPATH} "
5719 "expression syntax."
5722 new_command("IMPORT", 0, 0, 1, 0, import_command
, _(
5723 "IMPORT [--root=element[<TAB>child[..]]]\n" /* Creating elements from XML. */
5724 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
5726 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
5727 "argument is raw @abbr{XML} data. The content is created as a child of "
5728 "the element path specified with the @option{--root} option or at the "
5729 "document root when not specified. Existing elements of the same name will "
5732 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
5736 new_command("DUMP", 0, 1, 1, 0, dump_command
, _(
5737 "DUMP\n" /* Showing the XML document. */
5738 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
5739 "dumping a specific node."
5742 new_command("LOCK", 0, 0, 0, 0, lock_command
, _(
5743 "LOCK\n" /* Locking the current data file. */
5744 "Locks the mutex associated with the opened file. This prevents other clients "
5745 "from sending commands to the same opened file until the client "
5746 "that sent this command either disconnects or sends the @code{UNLOCK} "
5747 "command. @xref{UNLOCK}."
5750 new_command("UNLOCK", 0, 1, 0, 0, unlock_command
, _(
5751 "UNLOCK\n" /* Removing a data file lock. */
5752 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
5753 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
5754 "@pxref{ISCACHED})."
5757 new_command("GETCONFIG", 0, 1, 1, 0, getconfig_command
, _(
5758 "GETCONFIG [filename] <parameter>\n" /* Obtaining a configuration value. */
5759 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
5760 "data response. If no file has been opened then the value for @var{filename} "
5761 "or the default from the @var{global} section will be returned. If a file "
5762 "has been opened and no @var{filename} is specified, the value previously "
5763 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
5766 new_command("OPTION", 0, 1, 1, 0, option_command
, _(
5767 "OPTION <NAME>=[<VALUE>]\n" /* Setting various client parameters. */
5768 "Sets a client option @var{name} to @var{value}. The value for an option is "
5769 "kept for the duration of the connection with the exception of the "
5770 "@command{pinentry} options which are defaults for all future connections "
5771 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
5774 "@item DISABLE-PINENTRY\n"
5775 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
5776 "server inquire is sent to the client to obtain the passphrase. This option "
5777 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
5778 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
5779 "to use a @command{pinentry}."
5782 "Set or unset the X11 display to use when prompting for a passphrase."
5785 "Set the terminal device path to use when prompting for a passphrase."
5788 "Set the terminal type for use with @option{TTYNAME}."
5791 "Associates the thread ID of the connection with the specified textual "
5792 "representation. Useful for debugging log messages. May not contain whitespace."
5794 "@item LOCK-TIMEOUT\n"
5795 "When not @code{0}, the duration in tenths of a second to wait for the file "
5796 "mutex which has been locked by another thread to be released before returning "
5797 "an error. When @code{-1} the error will be returned immediately."
5799 "@item CLIENT-STATE\n"
5800 "When set to @code{1} then client state status messages for other clients are "
5801 "sent to the current client. The default is @code{0}."
5805 new_command("LS", 0, 1, 1, 0, ls_command
, _(
5806 "LS [--verbose]\n" /* Showing available data files. */
5807 "Returns a newline separated list of data files stored in the data directory "
5808 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response. When the "
5809 "@var{--verbose} option is passed, the space-separated filesystem inode "
5810 "access, modification and change times are appended to the line."
5813 new_command("BULK", 0, 1, 1, 0, bulk_command
, _(
5814 "BULK [--inquire]\n" /* Run a series of commands in sequence. */
5815 "Parses a semi-canonical s-expression representing a series of protocol "
5816 "commands to be run in sequence (@pxref{Bulk Commands}). Returns a canonical "
5817 "s-expression containing each commands id, return value and result data "
5820 "When the @option{--inquire} option is passed then all remaining non-option "
5821 "arguments are retrieved via a server @emph{INQUIRE}."
5824 new_command("RESET", 0, 1, 1, 0, NULL
, _(
5825 "RESET\n" /* Resetting the client state. */
5826 "Closes the currently opened file but keeps any previously set client options "
5830 new_command("NOP", 0, 1, 1, 0, nop_command
, _(
5831 "NOP\n" /* Testing the connection. */
5832 "This command does nothing. It is useful for testing the connection for a "
5833 "timeout condition."
5836 /* !END-HELP-TEXT! */
5837 new_command ("CANCEL", 0, 1, 1, 0, NULL
, NULL
);
5838 new_command ("END", 0, 1, 1, 0, NULL
, NULL
);
5839 new_command ("BYE", 0, 1, 1, 0, NULL
, NULL
);
5842 for (i
= 0; command_table
[i
]; i
++);
5843 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
5848 register_commands (assuan_context_t ctx
)
5852 for (; command_table
[i
]; i
++)
5854 if (!command_table
[i
]->handler
)
5857 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
5858 command_table
[i
]->handler
,
5859 command_table
[i
]->help
);
5864 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
5868 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
5872 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
5876 return assuan_register_post_cmd_notify (ctx
, command_finalize
);