2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
40 #include "pwmd-error.h"
45 #include "util-misc.h"
53 /* These are command option flags. */
54 #define OPT_INQUIRE 0x0001
55 #define OPT_NO_PASSPHRASE 0x0002
56 #define OPT_ASK 0x0004
57 #define OPT_LIST_RECURSE 0x0008
58 #define OPT_VERBOSE 0x0010
59 #define OPT_LOCK 0x0020
60 #define OPT_LOCK_ON_OPEN 0x0040
61 #define OPT_SIGN 0x0080
62 #define OPT_LIST_ALL 0x0100
63 #define OPT_DATA 0x0200
64 #define OPT_NO_AGENT 0x0400
65 #define OPT_SECRET_ONLY 0x0800
66 #define OPT_INQUIRE_KEYID 0x1000
67 #define OPT_INQUIRE_SIGN_KEYID 0x2000
68 #define OPT_SYMMETRIC 0x4000
69 #define OPT_NO_SIGNER 0x8000
70 #define OPT_HTML 0x10000
71 #define OPT_CACHE_AGENT 0x20000
72 #define OPT_CACHE_SIGN 0x40000
74 #define FLOCK_TYPE_NONE 0
75 #define FLOCK_TYPE_SH 0x0001
76 #define FLOCK_TYPE_EX 0x0002
77 #define FLOCK_TYPE_KEEP 0x0004
79 struct command_table_s
82 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
85 int unlock
; // unlock the file mutex after validating the checksum
89 static struct command_table_s
**command_table
;
91 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
92 static gpg_error_t
validate_checksum (struct client_s
*, const char *filename
,
93 struct cache_data_s
*, unsigned char **,
95 static gpg_error_t
update_checksum (struct client_s
*client
);
97 /* When 'status' is true the 'self' field of the status line will be false
98 * because we never send the STATE status message to the same client that
101 build_client_info_line (struct client_thread_s
*thd
, int status
)
103 char *uid
, *username
= NULL
;
108 uid
= str_asprintf("#%s", thd
->tls
->fp
);
111 uid
= str_asprintf("%u", thd
->peer
->uid
);
112 username
= get_username (thd
->peer
->uid
);
115 uid
= str_asprintf("%u", thd
->peer
->uid
);
116 username
= get_username (thd
->peer
->uid
);
118 line
= str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
120 thd
->name
? thd
->name
: "-",
121 thd
->cl
&& thd
->cl
->filename
122 && (thd
->cl
->flags
& FLAG_OPEN
)
123 ? thd
->cl
->filename
: "/",
125 thd
->remote
? thd
->peeraddr
: "-",
129 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
130 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
133 thd
->remote
? "-" : username
,
146 update_client_state (struct client_s
*client
, unsigned s
)
148 MUTEX_LOCK (&cn_mutex
);
149 client
->thd
->state
= s
;
150 MUTEX_UNLOCK (&cn_mutex
);
152 if (client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
154 char *line
= build_client_info_line (client
->thd
, 1);
156 pthread_cleanup_push (xfree
, line
);
158 send_status_all_not_self (STATUS_STATE
, "%s", line
);
159 pthread_cleanup_pop (1);
164 unlock_file_mutex (struct client_s
*client
, int remove
)
168 // OPEN: keep the lock for the same file being reopened.
169 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
172 if (!(client
->flags
& FLAG_HAS_LOCK
))
173 return GPG_ERR_NOT_LOCKED
;
175 rc
= cache_unlock_mutex (client
->filename
, remove
);
177 rc
= GPG_ERR_INV_STATE
;
179 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
185 lock_file_mutex (struct client_s
*client
, int add
)
188 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
190 if (client
->flags
& FLAG_HAS_LOCK
)
193 rc
= cache_lock_mutex (client
->ctx
, client
->filename
,
194 client
->lock_timeout
, add
, timeout
);
196 client
->flags
|= FLAG_HAS_LOCK
;
202 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
205 int type
= !cmd
->flock_type
|| (cmd
->flock_type
& FLOCK_TYPE_SH
)
208 if (!(client
->flags
& FLAG_OPEN
))
209 return GPG_ERR_INV_STATE
;
211 rc
= lock_file_mutex (client
, 0);
212 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
215 rc
= lock_flock (client
->ctx
, client
->filename
, type
, &client
->flock_fd
);
218 rc
= validate_checksum (client
, client
->filename
, NULL
, NULL
, NULL
);
219 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
222 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
224 else if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
227 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
228 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
229 unlock_file_mutex (client
, 0);
231 if (rc
|| !(cmd
->flock_type
& FLOCK_TYPE_KEEP
))
232 unlock_flock (&client
->flock_fd
);
238 parse_xml (assuan_context_t ctx
, int new)
240 struct client_s
*client
= assuan_get_pointer (ctx
);
241 int cached
= client
->doc
!= NULL
;
246 client
->doc
= xml_new_document ();
252 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
253 client
->crypto
->plaintext
= result
;
254 client
->crypto
->plaintext_size
= len
;
255 if (!client
->crypto
->plaintext
)
257 xmlFreeDoc (client
->doc
);
266 rc
= xml_parse_doc ((char *) client
->crypto
->plaintext
,
267 client
->crypto
->plaintext_size
,
268 (xmlDocPtr
*)&client
->doc
);
274 free_client (struct client_s
*client
)
277 xmlFreeDoc (client
->doc
);
280 xfree (client
->filename
);
281 xfree (client
->last_error
);
282 crypto_free (client
->crypto
);
283 client
->crypto
= NULL
;
287 reset_client (struct client_s
*client
)
289 assuan_context_t ctx
= client
->ctx
;
290 struct client_thread_s
*thd
= client
->thd
;
291 long lock_timeout
= client
->lock_timeout
;
292 xmlErrorPtr xml_error
= client
->xml_error
;
293 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
294 int flock_fd
= client
->flock_fd
;
296 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
297 free_client (client
);
298 memset (client
, 0, sizeof (struct client_s
));
299 client
->flock_fd
= flock_fd
;
300 client
->xml_error
= xml_error
;
303 client
->lock_timeout
= lock_timeout
;
304 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
313 strv_free ((char **) arg
);
317 parse_open_opt_lock (void *data
, void *value
)
319 struct client_s
*client
= data
;
321 client
->opts
|= OPT_LOCK_ON_OPEN
;
326 parse_opt_inquire (void *data
, void *value
)
328 struct client_s
*client
= data
;
331 client
->opts
|= OPT_INQUIRE
;
336 update_checksum (struct client_s
*client
)
340 struct cache_data_s
*cdata
;
341 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
348 cdata
= cache_get_data (client
->filename
);
352 cdata
->crc
= xmalloc (len
);
353 memcpy (cdata
->crc
, crc
, len
);
360 validate_checksum (struct client_s
*client
, const char *filename
,
361 struct cache_data_s
*cdata
, unsigned char **r_crc
,
369 if (cdata
&& !cdata
->crc
)
370 return GPG_ERR_CHECKSUM
;
372 rc
= get_checksum (filename
, &crc
, &len
);
377 n
= memcmp (cdata
->crc
, crc
, len
);
378 else if (client
->crc
)
379 n
= memcmp (client
->crc
, crc
, len
);
389 return n
? GPG_ERR_CHECKSUM
: 0;
393 open_command (assuan_context_t ctx
, char *line
)
396 struct client_s
*client
= assuan_get_pointer (ctx
);
397 char **req
, *filename
;
399 assuan_peercred_t peer
;
400 struct cache_data_s
*cdata
= NULL
;
402 unsigned char *crc
= NULL
;
404 struct argv_s
*args
[] = {
405 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
409 rc
= parse_options (&line
, args
, client
, 1);
411 return send_error (ctx
, rc
);
413 req
= str_split (line
, " ", 2);
415 return send_error (ctx
, GPG_ERR_SYNTAX
);
417 rc
= do_validate_peer (ctx
, req
[0], &peer
);
418 if (rc
== GPG_ERR_FORBIDDEN
)
419 rc
= peer_is_invoker (client
);
424 return send_error (ctx
, rc
);
428 if (!valid_filename (filename
))
431 return send_error (ctx
, GPG_ERR_INV_VALUE
);
434 pthread_cleanup_push ((void *)req_free
, req
);
435 /* This client may have locked a different file with ISCACHED --lock than
436 * the current filename. This will remove that lock. */
437 same_file
= client
->filename
&& !strcmp (filename
, client
->filename
);
438 if (client
->flags
& FLAG_OPEN
||
439 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
441 uint32_t opts
= client
->opts
;
442 uint32_t flags
= client
->flags
;
445 client
->flags
|= FLAG_KEEP_LOCK
;
446 else if (client
->flags
& FLAG_NEW
)
447 cache_clear (NULL
, client
->filename
, 0);
449 reset_client (client
);
451 client
->flags
|= flags
;
452 client
->flags
&= ~(FLAG_LOCK_CMD
);
454 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
457 client
->filename
= str_dup (filename
);
458 if (!client
->filename
)
461 return send_error(ctx
, GPG_ERR_ENOMEM
);
464 /* Need to lock the mutex here because file_modified() cannot without
465 * knowing the filename. */
466 rc
= lock_file_mutex (client
, 1);
468 client
->flags
&= ~FLAG_OPEN
;
472 rc
= lock_flock (ctx
, filename
, LOCK_SH
, &client
->flock_fd
);
473 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
480 char *keyfile
= config_get_string (client
->filename
, "passphrase_file");
482 rc
= crypto_init (&client
->crypto
, client
->ctx
, client
->filename
,
483 client
->flags
& FLAG_NO_PINENTRY
, keyfile
);
487 if (!rc
&& stat (client
->filename
, &st
) == -1)
488 rc
= gpg_error_from_errno (errno
);
489 else if (!rc
&& !S_ISREG (st
.st_mode
)) // to match LS output
490 rc
= gpg_error_from_errno (ENOANO
);
493 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_ENOENT
)
495 cdata
= cache_get_data (client
->filename
);
500 client
->flags
|= FLAG_NEW
;
501 // data file disappeared. clear the cache entry.
502 cache_clear (NULL
, client
->filename
, 1);
505 else if (cdata
&& cdata
->doc
) // cached document
510 if (!rc
&& !(client
->flags
& FLAG_NEW
))
512 rc
= validate_checksum (client
, client
->filename
, cdata
, &crc
,
514 if (rc
== GPG_ERR_CHECKSUM
)
523 rc
= cache_iscached (client
->filename
, &defer
, 0, 0);
524 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
534 log_write ("%s: %s", client
->filename
,
535 pwmd_strerror (GPG_ERR_CHECKSUM
));
536 cache_clear (NULL
, client
->filename
, 1);
538 rc
= crypto_decrypt (client
, client
->crypto
);
543 if (client
->thd
->remote
544 && config_get_boolean (client
->filename
, "tcp_require_key")
545 && !(client
->flags
& FLAG_NEW
))
546 rc
= crypto_try_decrypt (client
,
547 (client
->flags
& FLAG_NO_PINENTRY
));
553 client
->crypto
->plaintext
= cdata
->doc
;
554 client
->crypto
->plaintext_size
= cdata
->size
;
555 rc
= cache_decrypt (client
->crypto
);
560 strv_free (client
->crypto
->pubkey
);
561 strv_free (client
->crypto
->sigkey
);
562 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
563 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
568 client
->crypto
->plaintext
= NULL
;
569 client
->crypto
->plaintext_size
= 0;
573 else // existing file
575 cached
= cdata
!= NULL
;
576 rc
= crypto_decrypt (client
, client
->crypto
);
582 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
585 rc
= cache_encrypt (client
->crypto
);
587 cache_clear (NULL
, client
->filename
, 1);
590 int timeout
= config_get_integer (client
->filename
,
593 cache_free_data_once (cdata
);
594 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
595 cdata
->doc
= client
->crypto
->plaintext
;
596 cdata
->size
= client
->crypto
->plaintext_size
;
597 cdata
->pubkey
= strv_dup(client
->crypto
->pubkey
);
598 cdata
->sigkey
= strv_dup(client
->crypto
->sigkey
);
599 client
->crypto
->plaintext
= NULL
;
600 client
->crypto
->plaintext_size
= 0;
602 if (cached
) // wont increment the refcount
604 /* Prevent using another FD to update the checksum for a
605 * cached data file. The validity has already been
608 client
->crc
= xmalloc (crclen
);
609 memcpy (client
->crc
, crc
, crclen
);
611 cdata
->crc
= xmalloc (crclen
);
612 memcpy (cdata
->crc
, crc
, crclen
);
614 rc
= cache_set_data (client
->filename
, cdata
);
615 /* The cache entry may have been removed and cache_set_data()
616 * already sent STATUS_CACHE. */
617 if (!cache_iscached (client
->filename
, NULL
, 0, 0))
618 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
621 rc
= cache_add_file (client
->filename
, cdata
, timeout
);
626 pthread_cleanup_pop (1);
629 if (!rc
&& !(client
->flags
& FLAG_NEW
) && !cached
)
630 rc
= update_checksum (client
);
634 client
->flags
|= FLAG_OPEN
;
636 if (client
->flags
& FLAG_NEW
)
637 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
639 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
640 rc
= do_lock (client
, 0);
645 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
647 if (client
->flags
& FLAG_NEW
)
648 cache_clear (NULL
, client
->filename
, 1);
650 crypto_free (client
->crypto
);
651 client
->crypto
= NULL
;
652 client
->flags
&= ~FLAG_OPEN
;
655 crypto_free_non_keys (client
->crypto
);
657 return send_error (ctx
, rc
);
660 /* If not the invoking_user or is an existing file, check that the list of user
661 * supplied key ID's are in the list of current key ID's obtained when
662 * decrypting the data file.
665 permitted_to_save (struct client_s
*client
, const char **keys
,
671 if ((client
->flags
& FLAG_NEW
) || (client
->opts
& OPT_SYMMETRIC
))
674 rc
= peer_is_invoker (client
);
679 if ((!value
&& !keys
) || ((value
&& !*value
) && (keys
&& !*keys
)))
682 for (v
= value
; v
&& *v
; v
++)
684 const char **k
, *pv
= *v
;
687 if (*pv
== '0' && *(pv
+1) == 'x')
690 for (k
= keys
; k
&& *k
; k
++)
694 if (*pk
== '0' && *(pk
+1) == 'x')
697 rc
= !strcmp (pv
, pk
) ? 0 : GPG_ERR_FORBIDDEN
;
699 rc
= !strcmp (pv
, *k
) ? 0 : GPG_ERR_FORBIDDEN
;
709 return GPG_ERR_FORBIDDEN
;
715 /* Requires that the keyid be a fingerprint in 16 byte form. */
717 parse_save_opt_keyid_common (struct client_s
*client
, const char **list
,
718 const char *value
, char ***dst
)
725 keys
= str_split (value
, ",", 0);
727 return GPG_ERR_ENOMEM
;
730 rc
= crypto_keyid_to_16b (keys
);
732 rc
= permitted_to_save (client
, list
, (const char **)keys
);
743 parse_save_opt_keyid (void *data
, void *value
)
745 struct client_s
*client
= data
;
746 const char *str
= value
;
750 rc
= parse_save_opt_keyid_common (client
,
751 (const char **)client
->crypto
->pubkey
,
756 client
->crypto
->save
.pubkey
= dst
;
761 parse_save_opt_sign_keyid (void *data
, void *value
)
763 struct client_s
*client
= data
;
764 const char *str
= value
;
768 rc
= parse_save_opt_keyid_common (client
,
769 (const char **)client
->crypto
->sigkey
,
775 client
->opts
|= OPT_NO_SIGNER
;
777 client
->crypto
->save
.sigkey
= dst
;
782 parse_save_opt_inquire (struct client_s
*client
, uint32_t opt
)
784 if (opt
== OPT_INQUIRE
&& !(client
->flags
& FLAG_NEW
))
786 gpg_error_t rc
= peer_is_invoker (client
);
797 parse_save_opt_inquire_keyparam (void *data
, void *value
)
799 return parse_save_opt_inquire (data
, OPT_INQUIRE
);
803 parse_save_opt_inquire_keyid (void *data
, void *value
)
805 return parse_save_opt_inquire (data
, OPT_INQUIRE_KEYID
);
809 parse_save_opt_inquire_sign_keyid (void *data
, void *value
)
811 return parse_save_opt_inquire (data
, OPT_INQUIRE_SIGN_KEYID
);
815 parse_save_opt_symmetric (void *data
, void *value
)
817 struct client_s
*client
= data
;
819 client
->opts
|= OPT_SYMMETRIC
;
823 /* Tests that the keys in new_keys are also in old_keys. */
825 compare_keys (char **new_keys
, char **old_keys
)
829 if (!old_keys
|| !*old_keys
)
832 crypto_keyid_to_16b (new_keys
);
834 for (o
= old_keys
; *o
; o
++)
838 for (n
= new_keys
; *n
; n
++)
840 if (!strcmp (*n
, *o
))
845 return GPG_ERR_NOT_FOUND
;
852 inquire_keyid (struct client_s
*client
, uint32_t opt
)
855 unsigned char *result
= NULL
;
861 if (opt
== OPT_INQUIRE_KEYID
)
864 orig
= &client
->crypto
->pubkey
;
865 save
= &client
->crypto
->save
.pubkey
;
869 s
= "INQUIRE_SIGN_KEYID";
870 orig
= &client
->crypto
->sigkey
;
871 save
= &client
->crypto
->save
.sigkey
;
874 rc
= assuan_inquire (client
->ctx
, s
, &result
, &len
, 0);
879 rc
= parse_save_opt_keyid_common (client
, (const char **)*orig
,
880 (char *)result
, &dst
);
883 if (!dst
&& opt
== OPT_INQUIRE_SIGN_KEYID
884 && client
->opts
& OPT_SYMMETRIC
)
885 client
->opts
|= OPT_NO_SIGNER
;
895 /* The caching test of gpg-agent is both a blessing and a curse. When a key
896 * lookup in its cache fails it tries the last successful key to be unlocked.
897 * This prevents pwmd from successfully clearing a signing key since the
898 * previous key, which more than likely belongs to the same keypair as the
899 * encryption/decryption key, will have the passphrase cached and therefore the
900 * signing key also cached. So we need to clear both the signing and encryption
901 * keys to get the effect of requiring a passphrase when generating a new
902 * keypair for an existing data file, or when the "require_save_key"
903 * configuration parameter is set. The "require_save_key" parameter is mostly a
904 * failsafe of the gpg-agent option --ignore-cache-for-signing since
905 * some/most/all users may fail to set it.
908 save_command (assuan_context_t ctx
, char *line
)
910 struct client_s
*client
= assuan_get_pointer (ctx
);
911 struct cache_data_s
*cdata
= NULL
;
912 gpg_error_t rc
= 0, cached
= 0;
915 struct argv_s
*args
[] = {
916 &(struct argv_s
) {"keyid", OPTION_TYPE_ARG
, parse_save_opt_keyid
},
917 &(struct argv_s
) {"sign-keyid", OPTION_TYPE_OPTARG
,
918 parse_save_opt_sign_keyid
},
919 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
920 parse_save_opt_inquire_keyparam
},
921 &(struct argv_s
) {"inquire-keyid", OPTION_TYPE_NOARG
,
922 parse_save_opt_inquire_keyid
},
923 &(struct argv_s
) {"inquire-sign-keyid", OPTION_TYPE_NOARG
,
924 parse_save_opt_inquire_sign_keyid
},
925 &(struct argv_s
) {"symmetric", OPTION_TYPE_NOARG
,
926 parse_save_opt_symmetric
},
930 crypto_free_save (&client
->crypto
->save
);
932 if (!(client
->flags
& FLAG_NEW
))
934 rc
= crypto_is_symmetric (client
->filename
);
935 if (!rc
|| rc
== GPG_ERR_BAD_DATA
)
938 client
->opts
|= OPT_SYMMETRIC
;
945 return send_error (ctx
, rc
);
947 rc
= parse_options (&line
, args
, client
, 0);
949 return send_error (ctx
, rc
);
951 if (config_get_boolean (client
->filename
, "require_save_key"))
952 client
->opts
|= OPT_ASK
;
954 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
955 return send_error (ctx
, gpg_error_from_errno (errno
));
957 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
959 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
960 return send_error (ctx
, GPG_ERR_ENOANO
);
964 cached
= rc
= cache_iscached (client
->filename
, &defer
, 0, 1);
966 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
968 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
972 return send_error (ctx
, rc
);
974 /* Specifying both a recipient and symmetric encryption is an error. */
975 if (client
->opts
& OPT_INQUIRE_KEYID
&& client
->opts
& OPT_SYMMETRIC
)
977 return send_error (ctx
, GPG_ERR_CONFLICT
);
979 else if (client
->opts
& OPT_INQUIRE
&& client
->opts
& OPT_SYMMETRIC
)
981 return send_error (ctx
, GPG_ERR_CONFLICT
);
983 /* Existing file with a recipient and wanting symmetric is an error. */
984 else if (client
->opts
& OPT_SYMMETRIC
&& client
->crypto
->save
.pubkey
)
986 return send_error (ctx
, GPG_ERR_CONFLICT
);
988 else if (((client
->opts
& OPT_INQUIRE_KEYID
)
989 || (client
->opts
& OPT_INQUIRE_SIGN_KEYID
)))
991 if (client
->opts
& OPT_INQUIRE
)
992 return send_error (ctx
, GPG_ERR_CONFLICT
);
994 if (client
->opts
& OPT_INQUIRE_KEYID
)
995 rc
= inquire_keyid (client
, OPT_INQUIRE_KEYID
);
997 if (!rc
&& (client
->opts
& OPT_INQUIRE_SIGN_KEYID
))
998 rc
= inquire_keyid (client
, OPT_INQUIRE_SIGN_KEYID
);
1000 else if ((client
->crypto
->save
.pubkey
|| client
->crypto
->save
.sigkey
)
1001 && client
->opts
& OPT_INQUIRE
)
1002 return send_error (ctx
, GPG_ERR_CONFLICT
);
1006 client
->crypto
->keyfile
= config_get_string (client
->filename
,
1008 rc
= crypto_init_ctx (client
->crypto
, (client
->flags
& FLAG_NO_PINENTRY
),
1009 client
->crypto
->keyfile
);
1012 if (!rc
&& (client
->opts
& OPT_INQUIRE
))
1014 /* Require a passphrase when generating a new key pair for an existing
1017 if (!(client
->flags
& FLAG_NEW
))
1019 rc
= cache_clear_agent_keys (client
->filename
, 1, 1);
1022 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1023 client
->opts
&= ~OPT_ASK
;
1029 unsigned char *params
= NULL
;
1032 rc
= assuan_inquire (client
->ctx
, "KEYPARAM", ¶ms
, &len
, 0);
1035 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1036 pthread_cleanup_push ((void *)xfree
, params
);
1037 rc
= crypto_genkey (client
, client
->crypto
, params
);
1038 pthread_cleanup_pop (1);
1042 else if (!rc
&& (client
->flags
& FLAG_NEW
))
1044 if (!client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
1045 && !(client
->opts
& OPT_SYMMETRIC
))
1047 char *params
= crypto_default_key_params ();
1050 rc
= GPG_ERR_ENOMEM
;
1054 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1055 pthread_cleanup_push ((void *)xfree
, params
);
1056 rc
= crypto_genkey (client
, client
->crypto
,
1057 (unsigned char *)params
);
1058 pthread_cleanup_pop (1);
1063 if (!client
->crypto
->save
.pubkey
&& client
->crypto
->save
.sigkey
1064 && !(client
->opts
& OPT_SYMMETRIC
))
1065 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->save
.sigkey
);
1067 if (client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
)
1068 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->save
.pubkey
);
1073 cdata
= cache_get_data (client
->filename
);
1076 if (client
->crypto
->save
.pubkey
)
1077 rc
= compare_keys (client
->crypto
->save
.pubkey
, cdata
->pubkey
);
1079 /* Always allow a signer for symmetric data files. */
1080 if (!rc
&& client
->crypto
->save
.sigkey
1081 && !(client
->opts
& OPT_SYMMETRIC
))
1082 rc
= compare_keys (client
->crypto
->save
.sigkey
, cdata
->sigkey
);
1084 /* Prevent saving to a recipient who is not in the original recipient
1085 * list without a passphrase. */
1086 if (rc
|| (client
->crypto
->sigkey
&& client
->opts
& OPT_NO_SIGNER
))
1087 client
->opts
|= OPT_ASK
;
1089 if (rc
== GPG_ERR_NOT_FOUND
)
1091 rc
= peer_is_invoker (client
);
1092 if (rc
== GPG_ERR_EACCES
)
1093 rc
= GPG_ERR_FORBIDDEN
;
1096 if (!client
->crypto
->save
.pubkey
1097 && !(client
->opts
& OPT_SYMMETRIC
))
1098 client
->crypto
->save
.pubkey
= strv_dup (cdata
->pubkey
);
1100 if (!rc
&& !client
->crypto
->save
.sigkey
&& cdata
->sigkey
1101 && !(client
->opts
& OPT_NO_SIGNER
))
1102 client
->crypto
->save
.sigkey
= strv_dup (cdata
->sigkey
);
1103 else if (!rc
&& !client
->crypto
->save
.sigkey
1104 && (client
->opts
& OPT_NO_SIGNER
)
1105 && !(client
->opts
& OPT_SYMMETRIC
))
1106 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
1110 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->pubkey
);
1111 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->sigkey
);
1115 if (!rc
&& !(client
->flags
& FLAG_NEW
))
1117 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1118 if (client
->opts
& OPT_SYMMETRIC
)
1119 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
1121 if (client
->opts
& OPT_ASK
)
1123 rc
= cache_clear_agent_keys (client
->filename
, 1, 1);
1125 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1129 if (!rc
&& client
->opts
& OPT_SYMMETRIC
)
1130 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
1133 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1139 xmlDocDumpFormatMemory (client
->doc
, &client
->crypto
->plaintext
, &size
,
1142 client
->crypto
->plaintext_size
= (size_t) size
;
1144 rc
= GPG_ERR_ENOMEM
;
1148 client
->crypto
->flags
|= (client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_SYMMETRIC
: 0;
1149 client
->crypto
->flags
|= (client
->flags
& FLAG_NEW
) ? CRYPTO_FLAG_NEWFILE
: 0;
1150 client
->crypto
->flags
|= !(client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_PASSWD_SIGN
: 0;
1151 rc
= crypto_encrypt (client
, client
->crypto
);
1152 client
->crypto
->flags
&= ~CRYPTO_FLAG_SYMMETRIC
;
1157 rc
= crypto_write_file (client
->crypto
);
1160 if (!cached
) // no error
1161 cdata
= cache_get_data (client
->filename
);
1166 rc
= cache_encrypt (client
->crypto
);
1169 cache_clear (NULL
, client
->filename
, 1);
1170 strv_free (client
->crypto
->pubkey
);
1171 client
->crypto
->pubkey
= strv_dup (client
->crypto
->save
.pubkey
);
1172 strv_free (client
->crypto
->sigkey
);
1173 client
->crypto
->sigkey
= strv_dup (client
->crypto
->save
.sigkey
);
1174 client
->flags
&= ~(FLAG_NEW
);
1180 int timeout
= config_get_integer (client
->filename
,
1183 cache_free_data_once (cdata
);
1184 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1185 cdata
->doc
= client
->crypto
->plaintext
;
1186 client
->crypto
->plaintext
= NULL
;
1187 cdata
->size
= client
->crypto
->plaintext_size
;
1188 client
->crypto
->plaintext_size
= 0;
1189 cdata
->pubkey
= client
->crypto
->save
.pubkey
;
1190 client
->crypto
->save
.pubkey
= NULL
;
1191 cdata
->sigkey
= client
->crypto
->save
.sigkey
;
1192 client
->crypto
->save
.sigkey
= NULL
;
1194 /* Update in case the cache entry expires the next SAVE may not
1195 * have any known keys. */
1196 strv_free (client
->crypto
->pubkey
);
1197 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
1198 strv_free (client
->crypto
->sigkey
);
1199 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
1201 if (!cached
) // no error and wont increment refcount
1202 rc
= cache_set_data (client
->filename
, cdata
);
1204 rc
= cache_add_file (client
->filename
, cdata
, timeout
);
1206 if (!rc
&& (cached
|| (client
->flags
& FLAG_NEW
)))
1207 send_status_all (STATUS_CACHE
, NULL
);
1211 rc
= update_checksum (client
);
1212 client
->flags
&= ~(FLAG_NEW
);
1218 crypto_free_non_keys (client
->crypto
);
1219 return send_error (ctx
, rc
);
1223 do_delete (assuan_context_t ctx
, char *line
)
1225 struct client_s
*client
= assuan_get_pointer (ctx
);
1226 struct xml_request_s
*req
;
1230 rc
= xml_new_request (client
, line
, XML_CMD_DELETE
, &req
);
1234 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1235 xml_free_request (req
);
1239 rc
= xml_is_element_owner (client
, n
);
1241 rc
= xml_unlink_node (client
, n
);
1247 delete_command (assuan_context_t ctx
, char *line
)
1249 struct client_s
*client
= assuan_get_pointer (ctx
);
1251 struct argv_s
*args
[] = {
1252 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1256 rc
= parse_options (&line
, args
, client
, 1);
1257 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1258 rc
= GPG_ERR_SYNTAX
;
1260 return send_error (ctx
, rc
);
1262 if (client
->opts
& OPT_INQUIRE
)
1264 unsigned char *result
;
1267 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1269 return send_error (ctx
, rc
);
1271 pthread_cleanup_push ((void *)xfree
, result
);
1272 rc
= do_delete (ctx
, (char *)result
);
1273 pthread_cleanup_pop (1);
1276 rc
= do_delete (ctx
, line
);
1278 return send_error (ctx
, rc
);
1282 update_element_expirey (struct client_s
*client
, xmlNodePtr n
)
1285 xmlChar
*expire
, *incr
;
1287 time_t e
, e_orig
, i
;
1288 time_t now
= time (NULL
);
1290 expire
= xml_attribute_value (n
, (xmlChar
*)"expire");
1295 e
= strtoul ((char *)expire
, &p
, 10);
1296 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1299 rc
= GPG_ERR_ERANGE
;
1304 incr
= xml_attribute_value (n
, (xmlChar
*)"expire_increment");
1308 i
= strtoul ((char *)incr
, &p
, 10);
1309 if (errno
|| *p
|| i
== ULONG_MAX
|| *incr
== '-')
1312 rc
= GPG_ERR_ERANGE
;
1319 p
= str_asprintf ("%lu", e
);
1322 rc
= GPG_ERR_ENOMEM
;
1326 rc
= xml_add_attribute (client
, n
, "expire", p
);
1329 rc
= send_status (client
->ctx
, STATUS_EXPIRE
, "%lu %lu", e_orig
, e
);
1336 store_command (assuan_context_t ctx
, char *line
)
1338 struct client_s
*client
= assuan_get_pointer (ctx
);
1341 unsigned char *result
;
1342 xmlNodePtr n
, parent
;
1344 char *content
= NULL
;
1345 struct xml_request_s
*req
;
1348 return send_error (ctx
, GPG_ERR_SYNTAX
);
1350 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1352 return send_error (ctx
, rc
);
1354 rc
= xml_new_request (client
, (char *)result
, XML_CMD_STORE
, &req
);
1357 return send_error (ctx
, rc
);
1359 /* Prevent passing the element content around to save some memory. */
1360 len
= strv_length (req
->args
);
1361 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1362 if (*(req
->args
+1) && !xml_valid_element_path (req
->args
, has_content
))
1364 xml_free_request (req
);
1365 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1368 if (has_content
|| !*req
->args
[len
-1])
1371 content
= req
->args
[len
-1];
1372 req
->args
[len
-1] = NULL
;
1375 if (strv_length (req
->args
) > 1)
1377 rc
= xml_check_recursion (client
, req
);
1378 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1382 parent
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1386 rc
= xml_is_element_owner (client
, parent
);
1389 n
= xml_find_text_node (parent
->children
);
1391 xmlNodeSetContent (n
, (xmlChar
*) content
);
1393 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1395 (void)xml_update_element_mtime (client
, parent
);
1396 (void)update_element_expirey (client
, parent
);
1402 xml_free_request (req
);
1403 return send_error (ctx
, rc
);
1407 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1409 struct client_s
*client
= assuan_get_pointer (ctx
);
1413 int progress
= config_get_integer ("global", "xfer_progress");
1416 if (!(client
->flags
& FLAG_LOCK_CMD
))
1417 rc
= unlock_file_mutex (client
, 0);
1418 if (rc
&& rc
!= GPG_ERR_NOT_LOCKED
)
1422 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1423 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1424 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1432 if (sent
+ to_send
> total
)
1433 to_send
= total
- sent
;
1435 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1436 flush
? 0 : to_send
);
1439 sent
+= flush
? 0 : to_send
;
1441 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1442 (sent
== total
&& flush
))
1443 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1445 if (!flush
&& !rc
&& sent
== total
)
1452 while (!rc
&& sent
< total
);
1458 do_get (assuan_context_t ctx
, char *line
)
1460 struct client_s
*client
= assuan_get_pointer (ctx
);
1462 struct xml_request_s
*req
;
1465 rc
= xml_new_request (client
, line
, XML_CMD_NONE
, &req
);
1469 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1472 if (n
&& n
->children
)
1476 n
= xml_find_text_node (n
->children
);
1477 if (!n
|| !n
->content
|| !*n
->content
)
1478 rc
= GPG_ERR_NO_DATA
;
1481 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1484 xmlChar
*expire
= xml_attribute_value (tmp
,
1485 (xmlChar
*)"expire");
1488 time_t now
= time (NULL
);
1493 e
= strtoul ((char *)expire
, &p
, 10);
1494 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1495 log_write (_("invalid expire attribute value: %s"),
1498 rc
= send_status (ctx
, STATUS_EXPIRE
, "%lu 0", e
);
1506 rc
= GPG_ERR_NO_DATA
;
1509 xml_free_request (req
);
1514 get_command (assuan_context_t ctx
, char *line
)
1516 struct client_s
*client
= assuan_get_pointer (ctx
);
1518 struct argv_s
*args
[] = {
1519 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1523 rc
= parse_options (&line
, args
, client
, 1);
1524 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1525 rc
= GPG_ERR_SYNTAX
;
1527 return send_error (ctx
, rc
);
1529 if (client
->opts
& OPT_INQUIRE
)
1531 unsigned char *result
;
1534 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1536 return send_error (ctx
, rc
);
1538 pthread_cleanup_push ((void *)xfree
, result
);
1539 rc
= do_get (ctx
, (char *)result
);
1540 pthread_cleanup_pop (1);
1543 rc
= do_get (ctx
, line
);
1545 return send_error (ctx
, rc
);
1548 static void list_command_free1 (void *arg
);
1550 realpath_command (assuan_context_t ctx
, char *line
)
1553 char **realpath
= NULL
;
1554 struct client_s
*client
= assuan_get_pointer (ctx
);
1555 struct argv_s
*args
[] = {
1556 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1560 rc
= parse_options (&line
, args
, client
, 1);
1561 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1562 rc
= GPG_ERR_SYNTAX
;
1564 return send_error (ctx
, rc
);
1566 if (client
->opts
& OPT_INQUIRE
)
1568 unsigned char *result
;
1571 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1573 return send_error (ctx
, rc
);
1575 pthread_cleanup_push ((void *)xfree
, result
);
1576 (void)xml_resolve_path (client
, client
->doc
, result
, &realpath
, &rc
);
1577 pthread_cleanup_pop (1);
1580 (void)xml_resolve_path (client
, client
->doc
, (unsigned char *)line
,
1585 char *tmp
= strv_join ("\t", realpath
);
1587 strv_free (realpath
);
1589 return send_error (ctx
, GPG_ERR_ENOMEM
);
1591 pthread_cleanup_push ((void *)xfree
, tmp
);
1592 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
1593 pthread_cleanup_pop (1);
1596 return send_error (ctx
, rc
);
1600 list_command_free1 (void *arg
)
1603 string_free ((struct string_s
*) arg
, 1);
1607 parse_list_opt_recurse (void *data
, void *value
)
1609 struct client_s
*client
= data
;
1611 client
->opts
|= OPT_LIST_RECURSE
;
1616 parse_opt_verbose (void *data
, void *value
)
1618 struct client_s
*client
= data
;
1620 client
->opts
|= OPT_VERBOSE
;
1625 list_path_once (struct client_s
*client
, char *line
,
1626 struct element_list_s
*elements
, struct string_s
*result
)
1630 rc
= xml_create_path_list (client
, client
->doc
, NULL
, elements
, line
, 0);
1631 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
)
1636 int total
= slist_length (elements
->list
);
1642 for (i
= 0; i
< total
; i
++)
1644 char *tmp
= slist_nth_data (elements
->list
, i
);
1646 string_append_printf (result
, "%s%s", tmp
,
1647 i
+ 1 == total
? "" : "\n");
1651 rc
= GPG_ERR_NO_DATA
;
1658 do_list (assuan_context_t ctx
, char *line
)
1660 struct client_s
*client
= assuan_get_pointer (ctx
);
1661 gpg_error_t rc
= GPG_ERR_NO_DATA
;
1662 struct element_list_s
*elements
= NULL
;
1664 if (!line
|| !*line
)
1666 struct string_s
*str
= string_new (NULL
);
1670 return GPG_ERR_ENOMEM
;
1672 pthread_cleanup_push ((void *)list_command_free1
, str
);
1673 n
= xmlDocGetRootElement (client
->doc
);
1674 for (n
= n
->children
; n
; n
= n
->next
)
1678 if (n
->type
!= XML_ELEMENT_NODE
)
1681 name
= xmlGetProp (n
, (xmlChar
*)"_name");
1684 rc
= GPG_ERR_ENOMEM
;
1688 elements
= xcalloc (1, sizeof (struct element_list_s
));
1692 rc
= GPG_ERR_ENOMEM
;
1696 elements
->prefix
= string_new (NULL
);
1697 if (!elements
->prefix
)
1700 xml_free_element_list (elements
);
1701 rc
= GPG_ERR_ENOMEM
;
1705 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1706 elements
->root_only
= !elements
->recurse
;
1707 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1708 rc
= list_path_once (client
, (char *)name
, elements
, str
);
1710 pthread_cleanup_pop (1);
1715 string_append (str
, "\n");
1719 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1721 pthread_cleanup_pop (1);
1725 elements
= xcalloc (1, sizeof (struct element_list_s
));
1727 return GPG_ERR_ENOMEM
;
1729 elements
->prefix
= string_new (NULL
);
1730 if (!elements
->prefix
)
1732 xml_free_element_list (elements
);
1733 return GPG_ERR_ENOMEM
;
1736 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1737 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1738 struct string_s
*str
= string_new (NULL
);
1739 pthread_cleanup_push ((void *)list_command_free1
, str
);
1740 rc
= list_path_once (client
, line
, elements
, str
);
1742 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1744 pthread_cleanup_pop (1);
1745 pthread_cleanup_pop (1);
1750 list_command (assuan_context_t ctx
, char *line
)
1752 struct client_s
*client
= assuan_get_pointer (ctx
);
1754 struct argv_s
*args
[] = {
1755 &(struct argv_s
) {"recurse", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1756 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1757 /* These are deprecated but kept for backward compatibility with older
1759 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, NULL
},
1760 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
, NULL
},
1761 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1762 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
, NULL
},
1766 if (disable_list_and_dump
== 1)
1767 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1769 rc
= parse_options (&line
, args
, client
, 1);
1770 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1771 rc
= GPG_ERR_SYNTAX
;
1773 return send_error (ctx
, rc
);
1775 if (client
->opts
& OPT_INQUIRE
)
1777 unsigned char *result
;
1780 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1782 return send_error (ctx
, rc
);
1784 pthread_cleanup_push ((void *)xfree
, result
);
1785 rc
= do_list (ctx
, (char *)result
);
1786 pthread_cleanup_pop (1);
1789 rc
= do_list (ctx
, line
);
1791 return send_error (ctx
, rc
);
1794 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1795 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1797 * args[0] - element path
1800 attribute_list (assuan_context_t ctx
, char **args
)
1802 struct client_s
*client
= assuan_get_pointer (ctx
);
1803 char **attrlist
= NULL
;
1807 xmlNodePtr n
, an
, r
;
1810 struct xml_request_s
*req
;
1812 if (!args
|| !args
[0] || !*args
[0])
1813 return GPG_ERR_SYNTAX
;
1815 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_LIST
, &req
);
1819 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1820 xml_free_request (req
);
1825 for (a
= n
->properties
; a
; a
= a
->next
)
1829 if (target
&& xml_reserved_attribute ((char *)a
->name
))
1832 pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *));
1836 strv_free (attrlist
);
1838 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1839 pwmd_strerror (GPG_ERR_ENOMEM
));
1840 return GPG_ERR_ENOMEM
;
1845 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
, an
&& an
->content
1846 ? (char *)an
->content
: "");
1850 strv_free (attrlist
);
1851 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1852 pwmd_strerror (GPG_ERR_ENOMEM
));
1853 return GPG_ERR_ENOMEM
;
1856 attrlist
[++i
] = NULL
;
1860 return GPG_ERR_NO_DATA
;
1864 r
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[0], NULL
, &rc
);
1865 if (!RESUMABLE_ERROR (rc
))
1867 strv_free (attrlist
);
1870 else if (!rc
&& r
!= n
)
1880 line
= strv_join ("\n", attrlist
);
1883 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1884 pwmd_strerror (GPG_ERR_ENOMEM
));
1885 strv_free (attrlist
);
1886 return GPG_ERR_ENOMEM
;
1889 pthread_cleanup_push ((void *)xfree
, line
);
1890 pthread_cleanup_push ((void *)req_free
, attrlist
);
1891 rc
= xfer_data (ctx
, line
, strlen (line
));
1892 pthread_cleanup_pop (1);
1893 pthread_cleanup_pop (1);
1898 * args[0] - attribute
1899 * args[1] - element path
1902 attribute_delete (struct client_s
*client
, char **args
)
1907 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
1908 return GPG_ERR_SYNTAX
;
1910 if (!strcmp (args
[0], "_name") || !strcmp (args
[0], "target"))
1911 return GPG_ERR_INV_ATTR
;
1913 if (!xml_reserved_attribute (args
[0]))
1914 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
1917 struct xml_request_s
*req
;
1919 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
1923 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1924 xml_free_request (req
);
1928 rc
= xml_is_element_owner (client
, n
);
1931 rc
= xml_delete_attribute (client
, n
, (xmlChar
*) args
[0]);
1937 * Creates the "target" attribute. When other commands encounter an element
1938 * with this attribute they follow the "target" after appending remaining
1939 * elements from the original element path to the target. The exception is the
1940 * ATTR command that operates on the element itself (for reserved attribute
1941 * names) and not the target.
1943 * If the source element path doesn't exist when using 'ATTR SET target', it is
1944 * created, but the destination element path must exist. This is simliar to the
1945 * ls(1) command: ln -s src dst.
1947 * args[0] - source element path
1948 * args[1] - destination element path
1951 set_target_attribute (struct client_s
*client
, char **args
)
1953 struct xml_request_s
*req
= NULL
;
1958 if (!args
|| !args
[0] || !args
[1])
1959 return GPG_ERR_SYNTAX
;
1961 src
= str_split (args
[0], "\t", 0);
1963 return GPG_ERR_SYNTAX
;
1965 if (!xml_valid_element_path (src
, 0))
1968 return GPG_ERR_INV_VALUE
;
1971 dst
= str_split (args
[1], "\t", 0);
1975 return GPG_ERR_SYNTAX
;
1978 // Be sure the destination element path exists. */
1979 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
1983 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1984 if (rc
&& rc
!= GPG_ERR_EACCES
)
1987 xml_free_request (req
);
1990 if (!strcmp (args
[0], args
[1]))
1992 rc
= GPG_ERR_EEXIST
;
1996 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_TARGET
, &req
);
2000 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2001 if (rc
&& rc
!= GPG_ERR_EACCES
)
2006 rc
= xml_add_attribute (client
, n
, "target", args
[1]);
2008 rc
= xml_unlink_node (client
, n
->children
);
2014 xml_free_request (req
);
2019 * args[0] - attribute
2020 * args[1] - element path
2023 attribute_get (assuan_context_t ctx
, char **args
)
2025 struct client_s
*client
= assuan_get_pointer (ctx
);
2029 struct xml_request_s
*req
;
2031 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
2032 return GPG_ERR_SYNTAX
;
2034 if (!xml_reserved_attribute (args
[0]))
2035 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2038 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2042 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2043 xml_free_request (req
);
2049 a
= xmlGetProp (n
, (xmlChar
*) args
[0]);
2051 return GPG_ERR_NOT_FOUND
;
2053 pthread_cleanup_push ((void *)xmlFree
, a
);
2056 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2058 rc
= GPG_ERR_NO_DATA
;
2060 pthread_cleanup_pop (1);
2065 * args[0] - attribute
2066 * args[1] - element path
2070 attribute_set (struct client_s
*client
, char **args
)
2072 struct xml_request_s
*req
;
2076 if (!args
|| !args
[0] || !args
[1])
2077 return GPG_ERR_SYNTAX
;
2080 * Reserved attribute names.
2082 if (!strcmp (args
[0], "_name")) // Use the RENAME command instead
2083 return GPG_ERR_INV_ATTR
;
2084 else if (!strcmp (args
[0], "target"))
2085 return set_target_attribute (client
, args
+ 1);
2086 else if (!xml_valid_attribute (args
[0]))
2087 return GPG_ERR_INV_VALUE
;
2089 if (!xml_valid_attribute_value (args
[2]))
2090 return GPG_ERR_INV_VALUE
;
2092 if (!xml_reserved_attribute (args
[0]))
2094 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2097 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
2100 rc
= xml_check_recursion (client
, req
);
2101 xml_free_request (req
);
2107 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2111 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2112 xml_free_request (req
);
2116 rc
= xml_is_element_owner (client
, n
);
2119 rc
= xml_add_attribute (client
, n
, args
[0], args
[2]);
2126 * req[1] - attribute name or element path if command is LIST
2127 * req[2] - element path
2128 * req[2] - element path or value
2132 do_attr (assuan_context_t ctx
, char *line
)
2134 struct client_s
*client
= assuan_get_pointer (ctx
);
2138 req
= str_split (line
, " ", 4);
2139 if (!req
|| !req
[0] || !req
[1])
2142 return GPG_ERR_SYNTAX
;
2145 pthread_cleanup_push ((void *)req_free
, req
);
2147 if (strcasecmp (req
[0], "SET") == 0)
2148 rc
= attribute_set (client
, req
+ 1);
2149 else if (strcasecmp (req
[0], "GET") == 0)
2150 rc
= attribute_get (ctx
, req
+ 1);
2151 else if (strcasecmp (req
[0], "DELETE") == 0)
2152 rc
= attribute_delete (client
, req
+ 1);
2153 else if (strcasecmp (req
[0], "LIST") == 0)
2154 rc
= attribute_list (ctx
, req
+ 1);
2156 rc
= GPG_ERR_SYNTAX
;
2158 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2159 pthread_cleanup_pop (1);
2164 attr_command (assuan_context_t ctx
, char *line
)
2166 struct client_s
*client
= assuan_get_pointer (ctx
);
2168 struct argv_s
*args
[] = {
2169 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2173 rc
= parse_options (&line
, args
, client
, 1);
2174 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2175 rc
= GPG_ERR_SYNTAX
;
2177 return send_error (ctx
, rc
);
2179 if (client
->opts
& OPT_INQUIRE
)
2181 unsigned char *result
;
2184 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2186 return send_error (ctx
, rc
);
2188 pthread_cleanup_push ((void *)xfree
, result
);
2189 rc
= do_attr (ctx
, (char *)result
);
2190 pthread_cleanup_pop (1);
2193 rc
= do_attr (ctx
, line
);
2195 return send_error (ctx
, rc
);
2199 parse_iscached_opt_lock (void *data
, void *value
)
2201 struct client_s
*client
= data
;
2204 client
->opts
|= OPT_LOCK
;
2209 parse_iscached_opt_agent (void *data
, void *value
)
2211 struct client_s
*client
= data
;
2214 client
->opts
|= OPT_CACHE_AGENT
;
2219 parse_iscached_opt_sign (void *data
, void *value
)
2221 struct client_s
*client
= data
;
2224 client
->opts
|= OPT_CACHE_SIGN
;
2229 iscached_command (assuan_context_t ctx
, char *line
)
2231 struct client_s
*client
= assuan_get_pointer (ctx
);
2234 struct argv_s
*args
[] = {
2235 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2236 &(struct argv_s
) {"agent", OPTION_TYPE_NOARG
, parse_iscached_opt_agent
},
2237 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_iscached_opt_sign
},
2241 if (!line
|| !*line
)
2242 return send_error (ctx
, GPG_ERR_SYNTAX
);
2244 rc
= parse_options (&line
, args
, client
, 1);
2246 return send_error (ctx
, rc
);
2247 else if (!valid_filename (line
))
2248 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2250 if (!(client
->flags
& FLAG_OPEN
)
2251 && ((client
->opts
& OPT_CACHE_AGENT
) || (client
->opts
& OPT_CACHE_SIGN
)))
2252 return send_error (ctx
, GPG_ERR_INV_STATE
);
2254 rc
= cache_iscached (line
, &defer
, (client
->opts
& OPT_CACHE_AGENT
),
2255 (client
->opts
& OPT_CACHE_SIGN
));
2257 rc
= GPG_ERR_NO_DATA
;
2261 struct cache_data_s
*cdata
= cache_get_data (line
);
2265 rc
= validate_checksum (client
, line
, cdata
, NULL
, NULL
);
2266 if (rc
== GPG_ERR_CHECKSUM
)
2267 rc
= GPG_ERR_NO_DATA
;
2271 if (client
->opts
& OPT_LOCK
2272 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2274 gpg_error_t trc
= rc
;
2276 if (strcmp (line
, client
->filename
))
2277 reset_client (client
);
2279 xfree (client
->filename
);
2280 client
->filename
= str_dup (line
);
2281 rc
= do_lock (client
, 1);
2286 return send_error (ctx
, rc
);
2290 clearcache_command (assuan_context_t ctx
, char *line
)
2292 struct client_s
*client
= assuan_get_pointer (ctx
);
2293 gpg_error_t rc
= 0, all_rc
= 0;
2297 struct client_thread_s
*once
= NULL
;
2301 pthread_cleanup_push (cache_release_mutex
, NULL
);
2302 count
= cache_file_count ();
2303 MUTEX_LOCK (&cn_mutex
);
2304 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
2306 if (!line
|| !*line
)
2309 t
= slist_length (cn_thread_list
);
2311 for (i
= 0; i
< t
; i
++)
2313 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2314 assuan_peercred_t peer
;
2319 /* Lock each connected clients' file mutex to prevent any other client
2320 * from accessing the cache entry (the file mutex is locked upon
2321 * command startup). The cache for the entry is not cleared if the
2322 * file mutex is locked by another client to prevent this function
2323 * from blocking. Rather, it returns an error.
2327 if (thd
->cl
->filename
)
2329 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2330 /* The current client doesn't have permission to open the other
2331 * filename do to "access" configuration parameter in a filename
2332 * section. Since we are clearning all cache entries the error
2333 * will be returned later during cache_clear(). */
2340 else // Idle client without opened file?
2343 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2344 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2346 /* The current client owns the lock. */
2347 if (pthread_equal (pthread_self (), thd
->tid
))
2351 if (cache_iscached (thd
->cl
->filename
, NULL
, 0, 0)
2358 /* The cache entry will be cleared when the other client
2359 * disconnects and cache_timer_thread() does its thing. */
2360 cache_defer_clear (thd
->cl
->filename
);
2363 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2371 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1);
2372 cache_unlock_mutex (thd
->cl
->filename
, 0);
2382 /* A single data filename was specified. Lock only this data file
2383 * mutex and free the cache entry. */
2384 rc
= do_validate_peer (ctx
, line
, &peer
);
2385 if (rc
== GPG_ERR_FORBIDDEN
)
2386 rc
= peer_is_invoker (client
);
2388 if (rc
== GPG_ERR_EACCES
)
2389 rc
= GPG_ERR_FORBIDDEN
;
2391 if (!rc
&& thd
->cl
->filename
&& !strcmp (thd
->cl
->filename
, line
))
2393 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2394 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2396 /* The current client owns the lock. */
2397 if (pthread_equal (pthread_self (), thd
->tid
))
2404 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1);
2405 cache_unlock_mutex (thd
->cl
->filename
, 0);
2408 cache_defer_clear (thd
->cl
->filename
);
2410 /* Found a client with the opened file. The cache entry has been
2411 * either cleared (self) or defered to be cleared. */
2417 /* Only connected clients' cache entries have been cleared. Now clear any
2418 * remaining cache entries without clients but only if there wasn't an
2419 * error from above since this would defeat the locking check of the
2420 * remaining entries. */
2421 if (all
&& !all_rc
&& cache_file_count ())
2423 rc
= cache_clear (client
, NULL
, 1);
2424 if (rc
== GPG_ERR_EACCES
)
2425 rc
= GPG_ERR_FORBIDDEN
;
2428 /* No clients are using the specified file. */
2429 else if (!all_rc
&& !rc
&& !once
)
2430 rc
= cache_clear (NULL
, line
, 1);
2432 /* Release the connection mutex. */
2433 pthread_cleanup_pop (1);
2435 if (!rc
|| (cache_file_count () && count
!= cache_file_count ()))
2436 send_status_all (STATUS_CACHE
, NULL
);
2438 /* Release the cache mutex. */
2439 pthread_cleanup_pop (1);
2441 /* One or more files were locked while clearing all cache entries. */
2445 return send_error (ctx
, rc
);
2449 cachetimeout_command (assuan_context_t ctx
, char *line
)
2451 struct client_s
*client
= assuan_get_pointer (ctx
);
2453 char **req
= str_split (line
, " ", 0);
2456 assuan_peercred_t peer
;
2458 if (!req
|| !*req
|| !req
[1])
2461 return send_error (ctx
, GPG_ERR_SYNTAX
);
2465 timeout
= (int) strtol (req
[1], &p
, 10);
2466 if (errno
!= 0 || *p
|| timeout
< -1)
2469 return send_error (ctx
, GPG_ERR_SYNTAX
);
2472 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2473 if (rc
== GPG_ERR_FORBIDDEN
)
2475 rc
= peer_is_invoker (client
);
2476 if (rc
== GPG_ERR_EACCES
)
2477 rc
= GPG_ERR_FORBIDDEN
;
2482 rc
= cache_set_timeout (req
[0], timeout
);
2483 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2486 MUTEX_LOCK (&rcfile_mutex
);
2487 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2489 MUTEX_UNLOCK (&rcfile_mutex
);
2494 return send_error (ctx
, rc
);
2498 dump_command (assuan_context_t ctx
, char *line
)
2502 struct client_s
*client
= assuan_get_pointer (ctx
);
2505 if (disable_list_and_dump
== 1)
2506 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2509 return send_error (ctx
, GPG_ERR_SYNTAX
);
2511 rc
= peer_is_invoker(client
);
2513 return send_error (ctx
, rc
);
2515 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2519 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2520 pwmd_strerror (GPG_ERR_ENOMEM
));
2521 return send_error (ctx
, GPG_ERR_ENOMEM
);
2524 pthread_cleanup_push ((void *)xmlFree
, xml
);
2525 rc
= xfer_data (ctx
, (char *) xml
, len
);
2526 pthread_cleanup_pop (1);
2527 return send_error (ctx
, rc
);
2531 getconfig_command (assuan_context_t ctx
, char *line
)
2533 struct client_s
*client
= assuan_get_pointer (ctx
);
2535 char filename
[255] = { 0 }, param
[747] = { 0 };
2536 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2538 if (!line
|| !*line
)
2539 return send_error (ctx
, GPG_ERR_SYNTAX
);
2541 if (strchr (line
, ' '))
2543 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2548 if (fp
&& !valid_filename (fp
))
2549 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2551 paramp
= str_down (paramp
);
2552 if (!strcmp (paramp
, "passphrase"))
2553 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2555 p
= config_get_value (fp
? fp
: "global", paramp
);
2557 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2559 tmp
= expand_homedir (p
);
2563 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2564 pwmd_strerror (GPG_ERR_ENOMEM
));
2565 return send_error (ctx
, GPG_ERR_ENOMEM
);
2569 pthread_cleanup_push ((void *)xfree
, p
);
2570 rc
= xfer_data (ctx
, p
, strlen (p
));
2571 pthread_cleanup_pop (1);
2572 return send_error (ctx
, rc
);
2577 xmlXPathContextPtr xp
;
2578 xmlXPathObjectPtr result
;
2584 xpath_command_free (void *arg
)
2586 struct xpath_s
*xpath
= arg
;
2591 req_free (xpath
->req
);
2594 xmlBufferFree (xpath
->buf
);
2597 xmlXPathFreeObject (xpath
->result
);
2600 xmlXPathFreeContext (xpath
->xp
);
2604 do_xpath (assuan_context_t ctx
, char *line
)
2607 struct client_s
*client
= assuan_get_pointer (ctx
);
2608 struct xpath_s _x
= { 0 };
2609 struct xpath_s
*xpath
= &_x
;
2611 if (!line
|| !*line
)
2612 return GPG_ERR_SYNTAX
;
2614 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2616 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2617 return GPG_ERR_ENOMEM
;
2620 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2623 rc
= GPG_ERR_BAD_DATA
;
2628 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2631 rc
= GPG_ERR_BAD_DATA
;
2635 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2637 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2641 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2642 xpath
->result
->nodesetval
,
2643 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2647 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2649 rc
= GPG_ERR_NO_DATA
;
2652 else if (xpath
->req
[1])
2658 pthread_cleanup_push ((void *)xpath_command_free
, &xpath
);
2659 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2660 xmlBufferLength (xpath
->buf
));
2661 pthread_cleanup_pop (0);
2663 xpath_command_free (xpath
);
2668 xpath_command (assuan_context_t ctx
, char *line
)
2670 struct client_s
*client
= assuan_get_pointer (ctx
);
2672 struct argv_s
*args
[] = {
2673 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2677 if (disable_list_and_dump
== 1)
2678 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2680 rc
= peer_is_invoker(client
);
2682 return send_error (ctx
, rc
);
2684 rc
= parse_options (&line
, args
, client
, 1);
2685 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2686 rc
= GPG_ERR_SYNTAX
;
2688 return send_error (ctx
, rc
);
2690 if (client
->opts
& OPT_INQUIRE
)
2692 unsigned char *result
;
2695 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2697 return send_error (ctx
, rc
);
2699 pthread_cleanup_push ((void *)xfree
, result
);
2700 rc
= do_xpath (ctx
, (char *)result
);
2701 pthread_cleanup_pop (1);
2704 rc
= do_xpath (ctx
, line
);
2706 return send_error (ctx
, rc
);
2710 do_xpathattr (assuan_context_t ctx
, char *line
)
2712 struct client_s
*client
= assuan_get_pointer (ctx
);
2716 struct xpath_s _x
= { 0 };
2717 struct xpath_s
*xpath
= &_x
;
2719 if (!line
|| !*line
)
2720 return GPG_ERR_SYNTAX
;
2722 if ((req
= str_split (line
, " ", 3)) == NULL
)
2723 return GPG_ERR_ENOMEM
;
2727 rc
= GPG_ERR_SYNTAX
;
2731 if (!strcasecmp (req
[0], "SET"))
2733 else if (!strcasecmp (req
[0], "DELETE"))
2737 rc
= GPG_ERR_SYNTAX
;
2741 if (!req
[1] || !req
[2])
2743 rc
= GPG_ERR_SYNTAX
;
2747 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2749 rc
= GPG_ERR_ENOMEM
;
2753 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2755 rc
= GPG_ERR_SYNTAX
;
2759 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2762 rc
= GPG_ERR_BAD_DATA
;
2766 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2769 rc
= GPG_ERR_BAD_DATA
;
2773 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2775 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2779 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2780 xpath
->result
->nodesetval
,
2781 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2782 (xmlChar
*) req
[1]);
2785 xpath_command_free (xpath
);
2790 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2792 xpathattr_command (assuan_context_t ctx
, char *line
)
2794 struct client_s
*client
= assuan_get_pointer (ctx
);
2796 struct argv_s
*args
[] = {
2797 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2801 if (disable_list_and_dump
== 1)
2802 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2804 rc
= peer_is_invoker(client
);
2806 return send_error (ctx
, rc
);
2808 rc
= parse_options (&line
, args
, client
, 1);
2809 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2810 rc
= GPG_ERR_SYNTAX
;
2812 return send_error (ctx
, rc
);
2814 if (client
->opts
& OPT_INQUIRE
)
2816 unsigned char *result
;
2819 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2821 return send_error (ctx
, rc
);
2823 pthread_cleanup_push ((void *)xfree
, result
);
2824 rc
= do_xpathattr (ctx
, (char *)result
);
2825 pthread_cleanup_pop (1);
2828 rc
= do_xpathattr (ctx
, line
);
2830 return send_error (ctx
, rc
);
2834 do_import (struct client_s
*client
, const char *root_element
,
2835 unsigned char *content
)
2837 xmlDocPtr doc
= NULL
;
2838 xmlNodePtr n
= NULL
, root
;
2840 struct string_s
*str
= NULL
;
2841 struct xml_request_s
*req
= NULL
;
2843 if (!content
|| !*content
)
2844 return GPG_ERR_SYNTAX
;
2848 rc
= xml_new_request (client
, root_element
, XML_CMD_STORE
, &req
);
2855 if (!xml_valid_element_path (req
->args
, 0))
2857 xml_free_request (req
);
2859 return GPG_ERR_INV_VALUE
;
2863 str
= string_new_content ((char *)content
);
2864 str
= string_prepend (str
, "<pwmd>");
2865 str
= string_append (str
, "</pwmd>");
2866 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2867 string_free (str
, 1);
2870 rc
= GPG_ERR_BAD_DATA
;
2874 root
= xmlDocGetRootElement (doc
);
2875 root
= root
->children
;
2876 if (root
->type
!= XML_ELEMENT_NODE
)
2878 rc
= GPG_ERR_BAD_DATA
;
2882 rc
= xml_validate_import (client
, root
);
2888 /* Create the new specified root element path, if needed. */
2889 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
2894 /* Overwrite the children of the specified root element path. */
2895 (void)xml_unlink_node (client
, n
->children
);
2899 n
= xmlDocGetRootElement (client
->doc
);
2904 xmlChar
*name
= xml_attribute_value (root
, (xmlChar
*)"_name");
2907 rc
= GPG_ERR_BAD_DATA
;
2910 /* No --root argument passed. Overwrite the current documents' root
2911 * element matching the root element of the imported data. */
2912 xml_free_request (req
);
2914 rc
= xml_new_request (client
, (char *)name
, XML_CMD_DELETE
, &req
);
2920 tmp
= xml_find_elements (client
, req
,
2921 xmlDocGetRootElement (req
->doc
), &rc
);
2922 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2926 (void)xml_unlink_node (client
, tmp
);
2934 copy
= xmlCopyNodeList (root
);
2936 rc
= GPG_ERR_ENOMEM
;
2939 n
= xmlAddChildList (n
, copy
);
2941 rc
= GPG_ERR_ENOMEM
;
2946 if (!rc
&& n
&& n
->parent
)
2947 rc
= xml_update_element_mtime (client
, n
->parent
);
2950 xml_free_request (req
);
2959 parse_import_opt_root (void *data
, void *value
)
2961 struct client_s
*client
= data
;
2963 client
->import_root
= str_dup (value
);
2964 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
2968 import_command (assuan_context_t ctx
, char *line
)
2971 struct client_s
*client
= assuan_get_pointer (ctx
);
2972 unsigned char *result
;
2974 struct argv_s
*args
[] = {
2975 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
2979 xfree (client
->import_root
);
2980 client
->import_root
= NULL
;
2981 rc
= parse_options (&line
, args
, client
, 0);
2983 return send_error (ctx
, rc
);
2985 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2988 xfree (client
->import_root
);
2989 client
->import_root
= NULL
;
2990 return send_error (ctx
, rc
);
2993 rc
= do_import (client
, client
->import_root
, result
);
2994 xfree (client
->import_root
);
2995 client
->import_root
= NULL
;
2996 return send_error (ctx
, rc
);
3000 do_lock (struct client_s
*client
, int add
)
3002 gpg_error_t rc
= lock_file_mutex (client
, add
);
3005 client
->flags
|= FLAG_LOCK_CMD
;
3011 lock_command (assuan_context_t ctx
, char *line
)
3013 struct client_s
*client
= assuan_get_pointer (ctx
);
3017 return send_error (ctx
, GPG_ERR_SYNTAX
);
3019 rc
= do_lock (client
, 0);
3020 return send_error (ctx
, rc
);
3024 unlock_command (assuan_context_t ctx
, char *line
)
3026 struct client_s
*client
= assuan_get_pointer (ctx
);
3030 return send_error (ctx
, GPG_ERR_SYNTAX
);
3032 rc
= unlock_file_mutex (client
, 0);
3033 return send_error (ctx
, rc
);
3037 get_set_env (const char *name
, const char *value
)
3039 if (value
&& *value
)
3040 setenv (name
, value
, 1);
3046 option_command (assuan_context_t ctx
, char *line
)
3048 struct client_s
*client
= assuan_get_pointer (ctx
);
3050 char namebuf
[255] = { 0 };
3051 char *name
= namebuf
;
3052 char *value
= NULL
, *p
, *tmp
= NULL
;
3054 p
= strchr (line
, '=');
3057 strncpy (namebuf
, line
, sizeof(namebuf
));
3058 namebuf
[sizeof(namebuf
)-1] = 0;
3062 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3063 namebuf
[sizeof(namebuf
)-1] = 0;
3067 log_write2 ("OPTION name='%s' value='%s'", name
, value
);
3069 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3075 n
= strtol (value
, &tmp
, 10);
3077 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3080 client
->lock_timeout
= n
;
3082 else if (strcasecmp (name
, (char *) "client-state") == 0)
3088 n
= strtol (value
, &tmp
, 10);
3089 if ((tmp
&& *tmp
) || (n
< 0 || n
> 1))
3090 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3091 client
->client_state
= n
;
3094 client
->client_state
= 0;
3096 else if (strcasecmp (name
, (char *) "NAME") == 0)
3098 if (value
&& strchr (value
, ' '))
3099 rc
= GPG_ERR_INV_VALUE
;
3102 tmp
= pthread_getspecific (thread_name_key
);
3104 MUTEX_LOCK (&cn_mutex
);
3105 xfree (client
->thd
->name
);
3106 client
->thd
->name
= NULL
;
3108 if (!value
|| !*value
)
3109 pthread_setspecific (thread_name_key
, str_dup (""));
3112 client
->thd
->name
= str_dup (value
);
3113 pthread_setspecific (thread_name_key
, str_dup (value
));
3116 MUTEX_UNLOCK (&cn_mutex
);
3119 else if (strcasecmp (name
, "disable-pinentry") == 0)
3123 if (value
&& *value
)
3125 n
= (int) strtol (value
, &tmp
, 10);
3126 if (*tmp
|| n
< 0 || n
> 1)
3127 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3131 client
->flags
|= FLAG_NO_PINENTRY
;
3133 client
->flags
&= ~FLAG_NO_PINENTRY
;
3135 else if (strcasecmp (name
, "ttyname") == 0)
3137 get_set_env ("GPG_TTY", value
);
3139 else if (strcasecmp (name
, "ttytype") == 0)
3141 get_set_env ("TERM", value
);
3143 else if (strcasecmp (name
, "display") == 0)
3145 get_set_env ("DISPLAY", value
);
3147 else if (strcasecmp (name
, "lc_messages") == 0)
3150 else if (strcasecmp (name
, "lc_ctype") == 0)
3153 else if (strcasecmp (name
, "desc") == 0)
3156 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3160 rc
= GPG_ERR_UNKNOWN_OPTION
;
3162 return send_error (ctx
, rc
);
3166 do_rename (assuan_context_t ctx
, char *line
)
3168 struct client_s
*client
= assuan_get_pointer (ctx
);
3170 xmlNodePtr src
, dst
;
3171 struct string_s
*str
= NULL
, *tstr
;
3172 struct xml_request_s
*req
;
3175 args
= str_split (line
, " ", 0);
3176 if (!args
|| strv_length (args
) != 2)
3179 return GPG_ERR_SYNTAX
;
3182 if (!xml_valid_element ((xmlChar
*) args
[1]))
3185 return GPG_ERR_INV_VALUE
;
3188 rc
= xml_new_request (client
, args
[0], XML_CMD_RENAME
, &req
);
3195 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3199 rc
= xml_is_element_owner (client
, src
);
3203 xmlChar
*a
= xmlGetProp (src
, (xmlChar
*) "_name");
3206 rc
= GPG_ERR_ENOMEM
;
3210 /* To prevent unwanted effects:
3212 * <element _name="a"><element _name="b"/></element>
3216 if (xmlStrEqual (a
, (xmlChar
*) args
[1]))
3219 rc
= GPG_ERR_EEXIST
;
3224 str
= string_new (args
[0]);
3227 rc
= GPG_ERR_ENOMEM
;
3231 p
= strrchr (str
->str
, '\t');
3233 tstr
= string_truncate (str
, strlen (str
->str
)-strlen (p
));
3235 tstr
= string_truncate (str
, 0);
3239 string_free (str
, 1);
3240 rc
= GPG_ERR_ENOMEM
;
3245 tstr
= string_append_printf (str
, "%s%s", str
->len
? "\t" : "", args
[1]);
3248 string_free (str
, 1);
3249 rc
= GPG_ERR_ENOMEM
;
3254 xml_free_request (req
);
3255 rc
= xml_new_request (client
, str
->str
, XML_CMD_RENAME
, &req
);
3256 string_free (str
, 1);
3263 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3264 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3269 /* Target may exist:
3271 * <element _name="a"/>
3272 * <element _name="b" target="a"/>
3278 rc
= GPG_ERR_EEXIST
;
3284 rc
= xml_is_element_owner (client
, dst
);
3288 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3290 xml_unlink_node (client
, dst
);
3293 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3296 xml_free_request (req
);
3302 rename_command (assuan_context_t ctx
, char *line
)
3304 struct client_s
*client
= assuan_get_pointer (ctx
);
3306 struct argv_s
*args
[] = {
3307 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3311 rc
= parse_options (&line
, args
, client
, 1);
3312 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3313 rc
= GPG_ERR_SYNTAX
;
3315 return send_error (ctx
, rc
);
3317 if (client
->opts
& OPT_INQUIRE
)
3319 unsigned char *result
;
3322 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3324 return send_error (ctx
, rc
);
3326 pthread_cleanup_push ((void *)xfree
, result
);
3327 rc
= do_rename (ctx
, (char *)result
);
3328 pthread_cleanup_pop (1);
3331 rc
= do_rename (ctx
, line
);
3333 return send_error (ctx
, rc
);
3337 do_copy (assuan_context_t ctx
, char *line
)
3339 struct client_s
*client
= assuan_get_pointer (ctx
);
3340 char **args
, **targs
;
3341 xmlNodePtr src
, dst
, tree
= NULL
;
3342 struct xml_request_s
*req
;
3345 args
= str_split (line
, " ", 0);
3346 if (!args
|| strv_length (args
) != 2)
3349 return GPG_ERR_SYNTAX
;
3352 targs
= str_split (args
[1], "\t", 0);
3356 return GPG_ERR_SYNTAX
;
3359 if (!xml_valid_element_path (targs
, 0))
3363 return GPG_ERR_INV_VALUE
;
3367 rc
= xml_new_request (client
, args
[0], XML_CMD_NONE
, &req
);
3374 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3378 tree
= xmlCopyNodeList (src
);
3381 rc
= GPG_ERR_ENOMEM
;
3385 xml_free_request (req
);
3386 /* Create the destination element path if it does not exist. */
3387 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &req
);
3394 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3398 rc
= xml_is_element_owner (client
, dst
);
3402 rc
= xml_validate_target (client
, req
->doc
, args
[1], src
);
3403 if (rc
|| src
== dst
)
3405 rc
= rc
? rc
: GPG_ERR_EEXIST
;
3409 /* Merge any attributes from the src node to the initial dst node. */
3410 for (xmlAttrPtr attr
= tree
->properties
; attr
; attr
= attr
->next
)
3412 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3415 xmlAttrPtr a
= xmlHasProp (dst
, attr
->name
);
3419 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3420 xmlNewProp (dst
, attr
->name
, tmp
);
3422 /* Create the default attributes. */
3423 rc
= xml_add_attribute (client
, dst
, NULL
, NULL
);
3426 xmlNodePtr n
= dst
->children
;
3427 (void)xml_unlink_node (client
, n
);
3428 dst
->children
= NULL
;
3432 n
= xmlCopyNodeList (tree
->children
);
3435 rc
= GPG_ERR_ENOMEM
;
3439 n
= xmlAddChildList (dst
, n
);
3442 rc
= GPG_ERR_ENOMEM
;
3446 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
)
3447 == dst
->parent
? dst
: dst
->parent
);
3452 (void)xml_unlink_node (client
, tree
);
3454 xml_free_request (req
);
3460 copy_command (assuan_context_t ctx
, char *line
)
3462 struct client_s
*client
= assuan_get_pointer (ctx
);
3464 struct argv_s
*args
[] = {
3465 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3469 rc
= parse_options (&line
, args
, client
, 1);
3470 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3471 rc
= GPG_ERR_SYNTAX
;
3473 return send_error (ctx
, rc
);
3475 if (client
->opts
& OPT_INQUIRE
)
3477 unsigned char *result
;
3480 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3482 return send_error (ctx
, rc
);
3484 pthread_cleanup_push ((void *)xfree
, result
);
3485 rc
= do_copy (ctx
, (char *)result
);
3486 pthread_cleanup_pop (1);
3489 rc
= do_copy (ctx
, line
);
3491 return send_error (ctx
, rc
);
3495 do_move (assuan_context_t ctx
, char *line
)
3497 struct client_s
*client
= assuan_get_pointer (ctx
);
3499 xmlNodePtr src
, dst
;
3500 struct xml_request_s
*req
;
3503 args
= str_split (line
, " ", 0);
3504 if (!args
|| strv_length (args
) != 2)
3507 return GPG_ERR_SYNTAX
;
3512 char **targs
= str_split (args
[1], "\t", 0);
3516 return GPG_ERR_ENOMEM
;
3519 if (!xml_valid_element_path (targs
, 0))
3523 return GPG_ERR_INV_VALUE
;
3529 rc
= xml_new_request (client
, args
[0], XML_CMD_MOVE
, &req
);
3536 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3540 rc
= xml_is_element_owner (client
, src
);
3544 xml_free_request (req
);
3548 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
3552 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
3556 dst
= xmlDocGetRootElement (client
->doc
);
3558 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3563 for (xmlNodePtr n
= dst
; n
; n
= n
->parent
)
3567 rc
= GPG_ERR_EEXIST
;
3574 xmlChar
*a
= xml_attribute_value (src
, (xmlChar
*) "_name");
3575 xmlNodePtr dup
= xml_find_element (client
, dst
->children
, (char *) a
, &rc
);
3578 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3587 rc
= GPG_ERR_EEXIST
;
3591 if (dst
== xmlDocGetRootElement (client
->doc
))
3596 while (n
->parent
&& n
->parent
!= dst
)
3599 xmlChar
*a
= xml_attribute_value (n
, (xmlChar
*) "_name");
3600 xmlChar
*b
= xml_attribute_value (src
, (xmlChar
*) "_name");
3602 if (xmlStrEqual (a
, b
))
3605 xmlUnlinkNode (src
);
3606 (void)xml_unlink_node (client
, n
);
3613 (void)xml_unlink_node (client
, dup
);
3616 xmlUnlinkNode (dup
);
3620 if (!dst
&& *req
->args
)
3622 struct xml_request_s
*nreq
= NULL
;
3623 xmlChar
*name
= xml_attribute_value (src
, (xmlChar
*) "_name");
3625 if (src
->parent
== xmlDocGetRootElement (client
->doc
)
3626 && !strcmp ((char *) name
, *req
->args
))
3629 rc
= GPG_ERR_EEXIST
;
3634 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &nreq
);
3637 dst
= xml_find_elements (client
, nreq
,
3638 xmlDocGetRootElement (nreq
->doc
), &rc
);
3639 xml_free_request (nreq
);
3646 xml_update_element_mtime (client
, src
->parent
);
3647 xmlUnlinkNode (src
);
3649 dst
= xmlAddChildList (dst
, src
);
3651 rc
= GPG_ERR_ENOMEM
;
3653 xml_update_element_mtime (client
, dst
->parent
);
3656 xml_free_request (req
);
3662 move_command (assuan_context_t ctx
, char *line
)
3664 struct client_s
*client
= assuan_get_pointer (ctx
);
3666 struct argv_s
*args
[] = {
3667 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3671 rc
= parse_options (&line
, args
, client
, 1);
3672 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3673 rc
= GPG_ERR_SYNTAX
;
3675 return send_error (ctx
, rc
);
3677 if (client
->opts
& OPT_INQUIRE
)
3679 unsigned char *result
;
3682 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3684 return send_error (ctx
, rc
);
3686 pthread_cleanup_push ((void *)xfree
, result
);
3687 rc
= do_move (ctx
, (char *)result
);
3688 pthread_cleanup_pop (1);
3691 rc
= do_move (ctx
, line
);
3693 return send_error (ctx
, rc
);
3697 ls_command (assuan_context_t ctx
, char *line
)
3705 return send_error (ctx
, GPG_ERR_SYNTAX
);
3707 tmp
= str_asprintf ("%s/data", homedir
);
3708 dir
= expand_homedir (tmp
);
3711 rc
= gpg_error_from_errno (errno
);
3716 return send_error (ctx
, rc
);
3720 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3721 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3725 pthread_cleanup_push ((void *)xfree
, p
);
3726 pthread_cleanup_push ((void *)(void *)(void *)closedir
, d
);
3729 while (!readdir_r (d
, p
, &cur
) && cur
)
3733 if (stat (cur
->d_name
, &st
) == -1 || !S_ISREG (st
.st_mode
))
3736 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3743 rc
= GPG_ERR_ENOMEM
;
3751 pthread_cleanup_pop (1); // closedir (d)
3752 pthread_cleanup_pop (1); // xfree (p)
3755 return send_error (ctx
, rc
);
3758 return send_error (ctx
, GPG_ERR_NO_DATA
);
3760 list
[strlen (list
) - 1] = 0;
3761 pthread_cleanup_push ((void *)xfree
, list
);
3762 rc
= xfer_data (ctx
, list
, strlen (list
));
3763 pthread_cleanup_pop (1);
3764 return send_error (ctx
, rc
);
3768 bye_notify (assuan_context_t ctx
, char *line
)
3770 struct client_s
*cl
= assuan_get_pointer (ctx
);
3771 gpg_error_t ret
= 0;
3773 update_client_state (cl
, CLIENT_STATE_DISCON
);
3776 if (cl
->thd
->remote
)
3782 struct timeval tv
= { 0, 50000 };
3784 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3785 if (rc
== GNUTLS_E_AGAIN
)
3786 select (0, NULL
, NULL
, NULL
, &tv
);
3788 while (rc
== GNUTLS_E_AGAIN
);
3792 /* This will let assuan_process_next() return. */
3793 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
3795 cl
->last_rc
= gpg_error_from_errno (errno
);
3799 cl
->last_rc
= 0; // BYE command result
3804 reset_notify (assuan_context_t ctx
, char *line
)
3806 struct client_s
*client
= assuan_get_pointer (ctx
);
3809 reset_client (client
);
3815 * This is called before every Assuan command.
3818 command_startup (assuan_context_t ctx
, const char *name
)
3820 struct client_s
*client
= assuan_get_pointer (ctx
);
3822 struct command_table_s
*cmd
= NULL
;
3824 log_write2 ("command='%s'", name
);
3825 client
->last_rc
= client
->opts
= 0;
3827 for (int i
= 0; command_table
[i
]; i
++)
3829 if (!strcasecmp (name
, command_table
[i
]->name
))
3831 if (command_table
[i
]->ignore_startup
)
3833 cmd
= command_table
[i
];
3839 return GPG_ERR_UNKNOWN_COMMAND
;
3841 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3843 update_client_state (client
, CLIENT_STATE_COMMAND
);
3849 * This is called after every Assuan command.
3852 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3854 struct client_s
*client
= assuan_get_pointer (ctx
);
3856 if (!(client
->flags
& FLAG_LOCK_CMD
))
3857 unlock_file_mutex (client
, 0);
3859 unlock_flock (&client
->flock_fd
);
3860 log_write2 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
3861 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3863 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3865 if (client
->thd
->state
!= CLIENT_STATE_DISCON
)
3866 update_client_state (client
, CLIENT_STATE_IDLE
);
3870 parse_help_opt_html (void *data
, void *value
)
3872 struct client_s
*client
= data
;
3875 client
->opts
|= OPT_HTML
;
3880 help_command (assuan_context_t ctx
, char *line
)
3884 struct client_s
*client
= assuan_get_pointer (ctx
);
3885 struct argv_s
*args
[] = {
3886 &(struct argv_s
) {"html", OPTION_TYPE_NOARG
, parse_help_opt_html
},
3890 rc
= parse_options (&line
, args
, client
, 1);
3892 return send_error (ctx
, rc
);
3894 if (!line
|| !*line
)
3897 char *help
= str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3898 "For commands that take an element path as an argument, each element is "
3899 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3902 for (i
= 0; command_table
[i
]; i
++)
3904 if (!command_table
[i
]->help
)
3907 /* @npxref{} won't put a "See " or "see " in front of the command.
3908 * It's not a texinfo command but needed for --html. */
3909 tmp
= str_asprintf ("%s %s%s%s", help
,
3910 client
->opts
& OPT_HTML
? "@npxref{" : "",
3911 command_table
[i
]->name
,
3912 client
->opts
& OPT_HTML
? "}" : "");
3917 tmp
= strip_texi_and_wrap (help
, client
->opts
& OPT_HTML
);
3919 pthread_cleanup_push ((void *)xfree
, tmp
);
3920 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3921 pthread_cleanup_pop (1);
3922 return send_error (ctx
, rc
);
3925 for (i
= 0; command_table
[i
]; i
++)
3927 if (!strcasecmp (line
, command_table
[i
]->name
))
3931 if (!command_table
[i
]->help
)
3934 help
= strip_texi_and_wrap (command_table
[i
]->help
,
3935 client
->opts
& OPT_HTML
);
3936 tmp
= str_asprintf ("%s%s",
3937 (client
->opts
& OPT_HTML
) ? "" : _("Usage: "),
3940 pthread_cleanup_push ((void *)xfree
, tmp
);
3941 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3942 pthread_cleanup_pop (1);
3943 return send_error (ctx
, rc
);
3947 return send_error (ctx
, GPG_ERR_INV_NAME
);
3951 new_command (const char *name
, int ignore
, int unlock
, int flock_type
,
3952 gpg_error_t (*handler
) (assuan_context_t
, char *),
3956 struct command_table_s
**tmp
;
3959 for (i
= 0; command_table
[i
]; i
++);
3961 tmp
= xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3963 command_table
= tmp
;
3964 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3965 command_table
[i
]->name
= name
;
3966 command_table
[i
]->handler
= handler
;
3967 command_table
[i
]->ignore_startup
= ignore
;
3968 command_table
[i
]->unlock
= unlock
;
3969 command_table
[i
]->flock_type
= flock_type
;
3970 command_table
[i
++]->help
= help
;
3971 command_table
[i
] = NULL
;
3979 for (i
= 0; command_table
[i
]; i
++)
3980 xfree (command_table
[i
]);
3982 xfree (command_table
);
3986 sort_commands (const void *arg1
, const void *arg2
)
3988 struct command_table_s
*const *a
= arg1
;
3989 struct command_table_s
*const *b
= arg2
;
3998 return strcmp ((*a
)->name
, (*b
)->name
);
4002 passwd_command (assuan_context_t ctx
, char *line
)
4004 struct client_s
*client
= assuan_get_pointer (ctx
);
4007 rc
= peer_is_invoker (client
);
4008 if (rc
== GPG_ERR_EACCES
)
4009 return send_error (ctx
, GPG_ERR_FORBIDDEN
);
4011 return send_error (ctx
, rc
);
4013 if (client
->flags
& FLAG_NEW
)
4014 return send_error (ctx
, GPG_ERR_INV_STATE
);
4016 client
->crypto
->keyfile
= config_get_string (client
->filename
,
4018 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
,
4019 client
->crypto
->keyfile
);
4021 return send_error (ctx
, rc
);
4024 rc
= crypto_passwd (client
, client
->crypto
);
4026 crypto_free_non_keys (client
->crypto
);
4027 return send_error (ctx
, rc
);
4031 parse_opt_data (void *data
, void *value
)
4033 struct client_s
*client
= data
;
4036 client
->opts
|= OPT_DATA
;
4041 send_client_list (assuan_context_t ctx
)
4043 struct client_s
*client
= assuan_get_pointer (ctx
);
4046 if (client
->opts
& OPT_VERBOSE
)
4052 MUTEX_LOCK (&cn_mutex
);
4053 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
4054 t
= slist_length (cn_thread_list
);
4056 for (i
= 0; i
< t
; i
++)
4058 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4061 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4064 tmp
= build_client_info_line (thd
, 0);
4067 char **l
= strv_cat (list
, tmp
);
4069 rc
= GPG_ERR_ENOMEM
;
4074 rc
= GPG_ERR_ENOMEM
;
4083 pthread_cleanup_pop (1);
4087 line
= strv_join ("\n", list
);
4089 pthread_cleanup_push ((void *)xfree
, line
);
4090 rc
= xfer_data (ctx
, line
, strlen (line
));
4091 pthread_cleanup_pop (1);
4095 if (client
->opts
& OPT_DATA
)
4097 char buf
[ASSUAN_LINELENGTH
];
4099 MUTEX_LOCK (&cn_mutex
);
4100 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
4101 MUTEX_UNLOCK (&cn_mutex
);
4102 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4105 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4111 getinfo_command (assuan_context_t ctx
, char *line
)
4113 struct client_s
*client
= assuan_get_pointer (ctx
);
4115 char buf
[ASSUAN_LINELENGTH
];
4116 struct argv_s
*args
[] = {
4117 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4118 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4122 rc
= parse_options (&line
, args
, client
, 1);
4124 return send_error (ctx
, rc
);
4126 if (!strcasecmp (line
, "clients"))
4128 rc
= send_client_list (ctx
);
4130 else if (!strcasecmp (line
, "cache"))
4132 if (client
->opts
& OPT_DATA
)
4134 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4135 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4138 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4140 else if (!strcasecmp (line
, "pid"))
4143 pid_t pid
= getpid ();
4145 snprintf (buf
, sizeof (buf
), "%i", pid
);
4146 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4148 else if (!strcasecmp (line
, "version"))
4150 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4158 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4161 else if (!strcasecmp (line
, "last_error"))
4163 if (client
->last_error
)
4164 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4166 rc
= GPG_ERR_NO_DATA
;
4168 else if (!strcasecmp (line
, "user"))
4173 if (client
->thd
->remote
)
4174 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4176 user
= get_username (client
->thd
->peer
->uid
);
4178 user
= get_username (client
->thd
->peer
->uid
);
4182 pthread_cleanup_push ((void *)xfree
, user
);
4183 rc
= xfer_data (ctx
, user
, strlen (user
));
4184 pthread_cleanup_pop (1);
4187 rc
= GPG_ERR_NO_DATA
;
4190 rc
= gpg_error (GPG_ERR_SYNTAX
);
4192 return send_error (ctx
, rc
);
4196 parse_listkeys_opt_secret_only (void *data
, void *value
)
4198 struct client_s
*client
= data
;
4201 client
->opts
|= OPT_SECRET_ONLY
;
4206 keyinfo_command (assuan_context_t ctx
, char *line
)
4208 struct client_s
*client
= assuan_get_pointer (ctx
);
4210 char **keys
= NULL
, **p
= NULL
;
4214 return send_error (ctx
, GPG_ERR_SYNTAX
);
4216 if (!(client
->flags
& FLAG_OPEN
))
4217 return send_error (ctx
, GPG_ERR_INV_STATE
);
4219 if (client
->flags
& FLAG_NEW
)
4220 return send_error (ctx
, GPG_ERR_NO_DATA
);
4222 rc
= lock_flock (ctx
, client
->filename
, FLOCK_TYPE_SH
, &client
->flock_fd
);
4224 return send_error (ctx
, rc
);
4226 rc
= crypto_is_symmetric (client
->filename
);
4227 unlock_flock (&client
->flock_fd
);
4230 else if (rc
!= GPG_ERR_BAD_DATA
)
4231 return send_error (ctx
, rc
);
4236 p
= strv_catv(keys
, client
->crypto
->pubkey
);
4238 rc
= GPG_ERR_ENOMEM
;
4244 for (p
= client
->crypto
->sigkey
; p
&& *p
; p
++)
4246 if (!strv_printf(&keys
, "S%s", *p
))
4249 return send_error (ctx
, GPG_ERR_ENOMEM
);
4254 rc
= GPG_ERR_ENOMEM
;
4258 line
= strv_join ("\n", keys
);
4260 pthread_cleanup_push ((void *)xfree
, line
);
4262 rc
= xfer_data (ctx
, line
, strlen (line
));
4264 rc
= GPG_ERR_ENOMEM
;
4266 pthread_cleanup_pop (1);
4269 rc
= GPG_ERR_NO_DATA
;
4273 return send_error (ctx
, rc
);
4277 kill_command (assuan_context_t ctx
, char *line
)
4279 struct client_s
*client
= assuan_get_pointer (ctx
);
4283 if (!line
|| !*line
)
4284 return send_error (ctx
, GPG_ERR_SYNTAX
);
4286 MUTEX_LOCK (&cn_mutex
);
4287 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
4288 t
= slist_length (cn_thread_list
);
4291 for (i
= 0; i
< t
; i
++)
4293 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4294 char *tmp
= str_asprintf ("%p", thd
->tid
);
4296 if (strcmp (line
, tmp
))
4303 rc
= peer_is_invoker (client
);
4306 #ifdef HAVE_PTHREAD_CANCEL
4307 pthread_cancel (thd
->tid
);
4309 pthread_kill (thd
->tid
, SIGUSR2
);
4313 else if (rc
== GPG_ERR_EACCES
)
4314 rc
= GPG_ERR_FORBIDDEN
;
4318 if (config_get_boolean ("global", "strict_kill"))
4322 if (client
->thd
->remote
&& thd
->remote
)
4324 if (!thd
->tls
|| !thd
->tls
->fp
)
4326 rc
= GPG_ERR_INV_STATE
;
4329 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
4331 #ifdef HAVE_PTHREAD_CANCEL
4332 pthread_cancel (thd
->tid
);
4334 pthread_kill (thd
->tid
, SIGUSR2
);
4339 else if (!client
->thd
->remote
&& !thd
->remote
)
4342 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
4344 #ifdef HAVE_PTHREAD_CANCEL
4345 pthread_cancel (thd
->tid
);
4347 pthread_kill (thd
->tid
, SIGUSR2
);
4354 pthread_cleanup_pop (1);
4355 return send_error (ctx
, rc
);
4359 listkeys_command (assuan_context_t ctx
, char *line
)
4361 struct client_s
*client
= assuan_get_pointer (ctx
);
4362 struct crypto_s
*crypto
= NULL
;
4363 char **pattern
= NULL
;
4364 gpgme_key_t
*keys
= NULL
;
4365 char **result
= NULL
;
4367 struct argv_s
*args
[] = {
4368 &(struct argv_s
) {"secret-only", OPTION_TYPE_NOARG
,
4369 parse_listkeys_opt_secret_only
},
4373 rc
= parse_options (&line
, args
, client
, 1);
4375 return send_error (ctx
, rc
);
4377 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
4378 client
->flags
& FLAG_NO_PINENTRY
, NULL
);
4380 return send_error (ctx
, rc
);
4382 pthread_cleanup_push ((void *)crypto_free
, crypto
);
4383 pattern
= str_split (line
, ",", 0);
4384 pthread_cleanup_push ((void *)(void *)strv_free
, pattern
);
4385 rc
= crypto_list_keys (crypto
, pattern
, client
->opts
& OPT_SECRET_ONLY
,
4387 pthread_cleanup_pop (1);
4388 pthread_cleanup_pop (1);
4393 for (i
= 0; keys
[i
]; i
++)
4395 char *p
= crypto_key_info (keys
[i
]);
4400 rc
= GPG_ERR_ENOMEM
;
4404 r
= strv_cat (result
, p
);
4407 rc
= GPG_ERR_ENOMEM
;
4415 rc
= GPG_ERR_NO_DATA
;
4417 crypto_free_key_list (keys
);
4422 line
= strv_join ("\n", result
);
4424 pthread_cleanup_push ((void *)xfree
, line
);
4426 rc
= GPG_ERR_ENOMEM
;
4428 rc
= xfer_data (ctx
, line
, strlen (line
));
4430 pthread_cleanup_pop (1);
4435 return send_error (ctx
, rc
);
4441 /* !BEGIN-HELP-TEXT!
4443 * This comment is used as a marker to generate the offline documentation
4444 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4445 * script to determine where commands begin and end.
4447 new_command("HELP", 1, 1, 0, help_command
, _(
4448 "HELP [--html] [<COMMAND>]\n"
4449 "Show available commands or command specific help text."
4451 "The @option{--html} option will output the help text in HTML format."
4454 new_command("KILL", 1, 0, 0, kill_command
, _(
4455 "KILL <thread_id>\n"
4456 "Terminates the client identified by @var{thread_id} and releases any file "
4457 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4458 "for details about listing connected clients. An @code{invoking_user} "
4459 "(@pxref{Configuration}) may kill any client while others may only kill "
4460 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4463 new_command("LISTKEYS", 0, 0, 0, listkeys_command
, _(
4464 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4465 "Returns a new line separated list of key information matching a comma "
4466 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4467 "specified, only keys matching @var{pattern} that also have a secret key "
4468 "available will be returned."
4471 new_command("KEYINFO", 1, 0, 0, keyinfo_command
, _(
4473 "Returns a new line separated list of key ID's that the currently opened "
4474 "data file has recipients and signers for. If the key is a signing key it "
4475 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4476 "signers in the case of being symmetrically encrypted, the error code "
4477 "@code{GPG_ERR_NO_DATA} is returned."
4480 new_command("GETINFO", 1, 1, 0, getinfo_command
, _(
4481 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4482 "Get server and other information. The information is returned via a status "
4483 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4486 "@var{CACHE} returns the number of cached documents."
4488 "@var{CLIENTS} returns the number of "
4489 "connected clients via a status message or a list of connected clients when "
4490 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4491 "verbose line of a client list contains "
4493 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4494 "IP address if remote, file lock status, whether the current client is self "
4495 "or not, client state (see below), "
4496 "user ID or TLS fingerprint of the connected client, username if the "
4497 "client is a local one else @code{-}, and finally the time stamp of when the "
4500 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4501 "the client has connected but hasn't completed initializing, state @code{2} "
4502 "indicates that the client is idle, state @code{3} means the "
4503 "client is in a command and state @code{4} means the client is disconnecting. "
4505 "@var{PID} returns the process ID number of the server via a data response."
4507 "@var{VERSION} returns the server version number and compile-time features "
4508 "via a data response with each being space delimited."
4510 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4511 "via a data response, when available."
4513 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4514 "via a data response."
4517 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
4519 "Changes the passphrase of the secret key required to open the current "
4520 "data file. If the data file is symmetrically encrypted, the error "
4521 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4522 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4523 "this command saving any unwanted changes to the @abbr{XML} document."
4525 "This command is not available to non-invoking clients "
4526 "(@pxref{Access Control})."
4529 new_command("OPEN", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
4530 "OPEN [--lock] <filename>\n"
4531 "Opens @var{filename}. When the @var{filename} is not found on the "
4532 "file-system then a new in-memory document will be created. If the file is "
4533 "found, it is looked for in the file cache and when found no passphrase will "
4534 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4535 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4536 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4537 "@emph{INQUIRE} the client for the passphrase."
4539 "When the @option{--lock} option is passed then the file mutex will be "
4540 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4541 "file had been opened."
4544 new_command("SAVE", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
4545 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<fpr>[,..] | [--inquire-keyid]] [--sign-keyid=<fpr>[,..] | [--inquire-sign-keyid]]\n"
4546 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4547 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4548 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4549 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4550 "for the passphrase of the secret key used for signing."
4552 "The @option{--inquire-keyparam} option will send an "
4553 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4554 "generating the new keypair. The inquired data is expected to be in "
4555 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4556 "details. Note that when this option is specified a new keypair will be "
4557 "generated reguardless if the file is a new one and that the passphrase for "
4558 "the current file will be required before generating the new keypair. This "
4559 "option is available to non-invoking clients (@pxref{Access Control}) only "
4560 "when the file is a new one."
4562 "You can encrypt the data file to a recipient other than the one that it "
4563 "was encrypted with by passing the @option{--keyid} or "
4564 "@option{--inquire-keyid} option with "
4565 "the fingerprint of a public encryption key as its argument. Use the "
4566 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4567 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4568 "may also be used to sign the data "
4569 "file with an alternate key by specifying the fingerprint of a secret key. "
4570 "A passphrase to decrypt the data file "
4571 "will be required if one or more of the original encryption or signing keys "
4572 "are not found in either of these two options' arguments. The original "
4573 "encryption or signing keys will be used when either of these options are "
4576 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4577 "for non-invoking clients "
4578 "(@pxref{Access Control}) when the recipients or signers do not match those "
4579 "that were used when the file was @code{OPEN}'ed."
4581 "The @option{--symmetric} option specifies that a new data file be "
4582 "conventionally encrypted. These types of data files do not use a recipient "
4583 "public key but may be signed by using the @option{--sign-keyid} or "
4584 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4585 "symmtrically encrypted data file, leave the option value empty. Note that "
4586 "you cannot change encryption schemes once a data file has been saved."
4589 new_command("ISCACHED", 1, 0, 0, iscached_command
, _(
4590 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4591 "Determines the file cache status of the specified @var{filename}. "
4592 "The default is to test whether the filename is cached in memory. Passing "
4593 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4594 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4595 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4596 "Both the @option{--agent} and @option{--sign} options require an opened data "
4599 "An @emph{OK} response is returned if the specified @var{filename} is found "
4600 "in the cache. If not found in the cache but exists on the filesystem "
4601 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4604 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4605 "the file exists; it does not need to be opened nor cached. The lock will be "
4606 "released when the client exits or sends the @code{UNLOCK} command "
4607 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4610 new_command("CLEARCACHE", 1, 1, 0, clearcache_command
, _(
4611 "CLEARCACHE [<filename>]\n"
4612 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4613 "this will also clear any @command{gpg-agent} cached keys which may cause "
4614 "problems if another data file shares the same keys as @var{filename}."
4616 "When clearing all cache entries a permissions test is done against the "
4617 "current client based on the @var{access} configuration parameter in a "
4618 "@var{filename} section. Both a cache entry may be cleared and an error "
4619 "returned depending on cached data files and client permissions."
4622 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command
, _(
4623 "CACHETIMEOUT <filename> <seconds>\n"
4624 "The time in @var{seconds} until @var{filename} will be removed from the "
4625 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4626 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4627 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4631 new_command("LIST", 0, 1, 0, list_command
, _(
4632 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4633 "If no element path is given then a newline separated list of root elements "
4634 "is returned with a data response. If given, then children of the specified "
4635 "element path are returned."
4637 "Each element path "
4638 "returned will have zero or more flags appened to it. These flags are "
4639 "delimited from the element path by a single space character. A flag itself "
4640 "is a single character. Flag @code{P} indicates that access to the element "
4641 "is denied. Flag @code{+} indicates that there are child nodes of "
4642 "the current element path. Flag @code{E} indicates that an element of the "
4643 "element path contained in a @var{target} attribute could not be found. Flag "
4644 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4645 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4646 "then an element path, is the element path of the @var{target} attribute "
4647 "contained in the current element."
4649 "When a specified element path contains an error, beit from the final "
4650 "element in the path or any previous element, the path is still shown but "
4651 "will contain the error flag for the element with the error. Determining "
4652 "the actual element which contains the error is up to the client. This can be "
4653 "done by traversing the final element up to parent elements that contain the "
4656 "The option @option{--recurse} may be used to list the entire element tree "
4657 "for a specified element path or the entire tree for all root elements."
4659 "When the @option{--inquire} option is passed then all remaining non-option "
4660 "arguments are retrieved via a server @emph{INQUIRE}."
4663 new_command("REALPATH", 0, 1, 0, realpath_command
, _(
4664 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4665 "Resolves all @code{target} attributes of the specified element path and "
4666 "returns the result with a data response. @xref{Target Attribute}, for details."
4668 "When the @option{--inquire} option is passed then all remaining non-option "
4669 "arguments are retrieved via a server @emph{INQUIRE}."
4672 new_command("STORE", 0, 1, 0, store_command
, _(
4673 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4674 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4676 "Creates a new element path or modifies the @var{content} of an existing "
4677 "element. If only a single element is specified then a new root element is "
4678 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4679 "set to the final @key{TAB} delimited element. If no @var{content} is "
4680 "specified after the final @key{TAB}, then the content of the existing "
4681 "element will be removed; or will be empty if creating a new element."
4683 "The only restriction of an element name is that it not contain whitespace "
4684 "characters. There is no other whitespace between the @key{TAB} delimited "
4685 "elements. It is recommended that the content of an element be base64 encoded "
4686 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4687 "parsing and @command{pwmd} syntax errors."
4690 new_command("RENAME", 0, 1, 0, rename_command
, _(
4691 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4692 "Renames the specified @var{element} to the new @var{value}. If an element of "
4693 "the same name as the @var{value} already exists it will be overwritten."
4695 "When the @option{--inquire} option is passed then all remaining non-option "
4696 "arguments are retrieved via a server @emph{INQUIRE}."
4699 new_command("COPY", 0, 1, 0, copy_command
, _(
4700 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4701 "Copies the entire element tree starting from the child node of the source "
4702 "element, to the destination element path. If the destination element path "
4703 "does not exist then it will be created; otherwise it is overwritten."
4705 "Note that attributes from the source element are merged into the "
4706 "destination element when the destination element path exists. When an "
4707 "attribute of the same name exists in both the source and destination "
4708 "elements then the destination attribute will be updated to the source "
4711 "When the @option{--inquire} option is passed then all remaining non-option "
4712 "arguments are retrieved via a server @emph{INQUIRE}."
4715 new_command("MOVE", 0, 1, 0, move_command
, _(
4716 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4717 "Moves the source element path to the destination element path. If the "
4718 "destination is not specified then it will be moved to the root node of the "
4719 "document. If the destination is specified and exists then it will be "
4720 "overwritten; otherwise non-existing elements of the destination element "
4721 "path will be created."
4723 "When the @option{--inquire} option is passed then all remaining non-option "
4724 "arguments are retrieved via a server @emph{INQUIRE}."
4727 new_command("DELETE", 0, 1, 0, delete_command
, _(
4728 "DELETE [--inquire] element[<TAB>child[..]]\n"
4729 "Removes the specified element path and all of its children. This may break "
4730 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4731 "refers to this element or any of its children."
4733 "When the @option{--inquire} option is passed then all remaining non-option "
4734 "arguments are retrieved via a server @emph{INQUIRE}."
4737 new_command("GET", 0, 1, 0, get_command
, _(
4738 "GET [--inquire] element[<TAB>child[..]]\n"
4739 "Retrieves the content of the specified element. The content is returned "
4740 "with a data response."
4742 "When the @option{--inquire} option is passed then all remaining non-option "
4743 "arguments are retrieved via a server @emph{INQUIRE}."
4746 new_command("ATTR", 0, 1, 0, attr_command
, _(
4747 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4749 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4750 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4751 "element. When no @var{value} is specified any existing value will be removed."
4753 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4754 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4755 "or @code{target} an error is returned. Use the @command{DELETE} command "
4756 "(@pxref{DELETE}) instead."
4758 "@item ATTR LIST element[<TAB>child[..]]\n"
4759 " Retrieves a newline separated list of attributes names and values "
4760 "from the specified element. Each attribute name and value is space delimited."
4762 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4763 " Retrieves the value of an @var{attribute} from an element."
4766 "When the @option{--inquire} option is passed then all remaining non-option "
4767 "arguments are retrieved via a server @emph{INQUIRE}."
4769 "@xref{Target Attribute}, for details about this special attribute and also "
4770 "@pxref{Other Attributes} for other attributes that are handled specially "
4771 "by @command{pwmd}."
4774 new_command("XPATH", 0, 1, 0, xpath_command
, _(
4775 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4776 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4777 "specified it is assumed the expression is a request to return a result. "
4778 "Otherwise, the result is set to the @var{value} argument and the document is "
4779 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4780 "is assumed to be empty and the document is updated. For example:"
4783 "XPATH //element[@@_name='password']@key{TAB}\n"
4786 "would clear the content of all @var{password} elements in the data file "
4787 "while leaving off the trailing @key{TAB} would return all @var{password} "
4788 "elements in @abbr{XML} format."
4790 "When the @option{--inquire} option is passed then all remaining non-option "
4791 "arguments are retrieved via a server @emph{INQUIRE}."
4793 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4794 "expression syntax."
4797 new_command("XPATHATTR", 0, 1, 0, xpathattr_command
, _(
4798 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4799 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4800 "attributes and does not return a result. For the @var{SET} operation the "
4801 "@var{value} is optional but the field is required. If not specified then "
4802 "the attribute value will be empty. For example:"
4805 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4808 "would create a @var{password} attribute for each @var{password} element "
4809 "found in the document. The attribute value will be empty but still exist."
4811 "When the @option{--inquire} option is passed then all remaining non-option "
4812 "arguments are retrieved via a server @emph{INQUIRE}."
4814 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4815 "expression syntax."
4818 new_command("IMPORT", 0, 1, 0, import_command
, _(
4819 "IMPORT [--root=element[<TAB>child[..]]]\n"
4820 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4822 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4823 "argument is raw @abbr{XML} data. The content is created as a child of "
4824 "the element path specified with the @option{--root} option or at the "
4825 "document root when not specified. Existing elements of the same name will "
4828 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4832 new_command("DUMP", 0, 1, 0, dump_command
, _(
4834 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4835 "dumping a specific node."
4838 new_command("LOCK", 0, 0, 0, lock_command
, _(
4840 "Locks the mutex associated with the opened file. This prevents other clients "
4841 "from sending commands to the same opened file until the client "
4842 "that sent this command either disconnects or sends the @code{UNLOCK} "
4843 "command. @xref{UNLOCK}."
4846 new_command("UNLOCK", 1, 0, 0, unlock_command
, _(
4848 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4849 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4850 "@pxref{ISCACHED})."
4853 new_command("GETCONFIG", 1, 1, 0, getconfig_command
, _(
4854 "GETCONFIG [filename] <parameter>\n"
4855 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4856 "data response. If no file has been opened then the value for @var{filename} "
4857 "or the default from the @var{global} section will be returned. If a file "
4858 "has been opened and no @var{filename} is specified, the value previously "
4859 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4862 new_command("OPTION", 1, 1, 0, option_command
, _(
4863 "OPTION <NAME>=[<VALUE>]\n"
4864 "Sets a client option @var{name} to @var{value}. The value for an option is "
4865 "kept for the duration of the connection with the exception of the "
4866 "@command{pinentry} options which are defaults for all future connections "
4867 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4870 "@item DISABLE-PINENTRY\n"
4871 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4872 "server inquire is sent to the client to obtain the passphrase. This option "
4873 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4874 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4875 "to use a @command{pinentry}."
4878 "Set or unset the X11 display to use when prompting for a passphrase."
4881 "Set the terminal device path to use when prompting for a passphrase."
4884 "Set the terminal type for use with @option{TTYNAME}."
4887 "Associates the thread ID of the connection with the specified textual "
4888 "representation. Useful for debugging log messages. May not contain whitespace."
4890 "@item LOCK-TIMEOUT\n"
4891 "When not @code{0}, the duration in tenths of a second to wait for the file "
4892 "mutex which has been locked by another thread to be released before returning "
4893 "an error. When @code{-1} the error will be returned immediately."
4895 "@item CLIENT-STATE\n"
4896 "When set to @code{1} then client state status messages for other clients are "
4897 "sent to the current client. The default is @code{0}."
4901 new_command("LS", 1, 1, 0, ls_command
, _(
4903 "Returns a newline separated list of data files stored in the data directory "
4904 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4907 new_command("RESET", 1, 1, 0, NULL
, _(
4909 "Closes the currently opened file but keeps any previously set client options "
4913 new_command("NOP", 1, 1, 0, NULL
, _(
4915 "Does nothing. Always returns successfully."
4918 /* !END-HELP-TEXT! */
4919 new_command ("CANCEL", 1, 1, 0, NULL
, NULL
);
4920 new_command ("END", 1, 1, 0, NULL
, NULL
);
4921 new_command ("BYE", 1, 1, 0, NULL
, NULL
);
4924 for (i
= 0; command_table
[i
]; i
++);
4925 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4930 register_commands (assuan_context_t ctx
)
4934 for (; command_table
[i
]; i
++)
4936 if (!command_table
[i
]->handler
)
4939 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4940 command_table
[i
]->handler
,
4941 command_table
[i
]->help
);
4946 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4950 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4954 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4958 return assuan_register_post_cmd_notify (ctx
, command_finalize
);