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"
54 /* These are command option flags. */
55 #define OPT_INQUIRE 0x0001
56 #define OPT_NO_PASSPHRASE 0x0002
57 #define OPT_ASK 0x0004
58 #define OPT_LIST_RECURSE 0x0008
59 #define OPT_VERBOSE 0x0010
60 #define OPT_LOCK 0x0020
61 #define OPT_LOCK_ON_OPEN 0x0040
62 #define OPT_SIGN 0x0080
63 #define OPT_LIST_ALL 0x0100
64 #define OPT_DATA 0x0200
65 #define OPT_NO_AGENT 0x0400
66 #define OPT_SECRET_ONLY 0x0800
67 #define OPT_INQUIRE_KEYID 0x1000
68 #define OPT_INQUIRE_SIGN_KEYID 0x2000
69 #define OPT_SYMMETRIC 0x4000
70 #define OPT_NO_SIGNER 0x8000
71 #define OPT_HTML 0x10000
72 #define OPT_CACHE_AGENT 0x20000
73 #define OPT_CACHE_SIGN 0x40000
75 #define FLOCK_TYPE_NONE 0
76 #define FLOCK_TYPE_SH 0x0001
77 #define FLOCK_TYPE_EX 0x0002
78 #define FLOCK_TYPE_KEEP 0x0004
80 struct command_table_s
83 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
86 int unlock
; // unlock the file mutex after validating the checksum
90 static struct command_table_s
**command_table
;
92 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
93 static gpg_error_t
validate_checksum (struct client_s
*, const char *filename
,
94 struct cache_data_s
*, unsigned char **,
96 static gpg_error_t
update_checksum (struct client_s
*client
);
98 /* When 'status' is true the 'self' field of the status line will be false
99 * because we never send the STATE status message to the same client that
102 build_client_info_line (struct client_thread_s
*thd
, int status
)
104 char *uid
, *username
= NULL
;
106 char *name
= pthread_getspecific (thread_name_key
);
110 uid
= str_asprintf("#%s", thd
->tls
->fp
);
113 uid
= str_asprintf("%u", thd
->peer
->uid
);
114 username
= get_username (thd
->peer
->uid
);
117 uid
= str_asprintf("%u", thd
->peer
->uid
);
118 username
= get_username (thd
->peer
->uid
);
120 line
= str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
123 thd
->cl
&& thd
->cl
->filename
124 && (thd
->cl
->flags
& FLAG_OPEN
)
125 ? thd
->cl
->filename
: "/",
127 thd
->remote
? thd
->peeraddr
: "-",
131 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
132 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
135 thd
->remote
? "-" : username
,
148 update_client_state (struct client_s
*client
, unsigned s
)
150 MUTEX_LOCK (&cn_mutex
);
151 client
->thd
->state
= s
;
152 MUTEX_UNLOCK (&cn_mutex
);
154 if (client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
156 char *line
= build_client_info_line (client
->thd
, 1);
158 pthread_cleanup_push (xfree
, line
);
160 send_status_all_not_self (STATUS_STATE
, "%s", line
);
161 pthread_cleanup_pop (1);
166 unlock_file_mutex (struct client_s
*client
, int remove
)
170 // OPEN: keep the lock for the same file being reopened.
171 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
174 if (!(client
->flags
& FLAG_HAS_LOCK
))
175 return GPG_ERR_NOT_LOCKED
;
177 rc
= cache_unlock_mutex (client
->filename
, remove
);
179 rc
= GPG_ERR_INV_STATE
;
181 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
187 lock_file_mutex (struct client_s
*client
, int add
)
190 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
192 if (client
->flags
& FLAG_HAS_LOCK
)
195 rc
= cache_lock_mutex (client
->ctx
, client
->filename
,
196 client
->lock_timeout
, add
, timeout
);
198 client
->flags
|= FLAG_HAS_LOCK
;
204 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
207 int type
= !cmd
->flock_type
|| (cmd
->flock_type
& FLOCK_TYPE_SH
)
210 if (!(client
->flags
& FLAG_OPEN
))
211 return GPG_ERR_INV_STATE
;
213 rc
= lock_file_mutex (client
, 0);
214 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
217 rc
= lock_flock (client
->ctx
, client
->filename
, type
, &client
->flock_fd
);
220 rc
= validate_checksum (client
, client
->filename
, NULL
, NULL
, NULL
);
221 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
224 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
226 else if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
229 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
230 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
231 unlock_file_mutex (client
, 0);
233 if (rc
|| !(cmd
->flock_type
& FLOCK_TYPE_KEEP
))
234 unlock_flock (&client
->flock_fd
);
240 parse_xml (assuan_context_t ctx
, int new)
242 struct client_s
*client
= assuan_get_pointer (ctx
);
243 int cached
= client
->doc
!= NULL
;
248 client
->doc
= xml_new_document ();
254 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
255 client
->crypto
->plaintext
= result
;
256 client
->crypto
->plaintext_size
= len
;
257 if (!client
->crypto
->plaintext
)
259 xmlFreeDoc (client
->doc
);
268 rc
= xml_parse_doc ((char *) client
->crypto
->plaintext
,
269 client
->crypto
->plaintext_size
,
270 (xmlDocPtr
*)&client
->doc
);
276 free_client (struct client_s
*client
)
279 xmlFreeDoc (client
->doc
);
282 xfree (client
->filename
);
283 xfree (client
->last_error
);
284 crypto_free (client
->crypto
);
285 client
->crypto
= NULL
;
289 reset_client (struct client_s
*client
)
291 assuan_context_t ctx
= client
->ctx
;
292 struct client_thread_s
*thd
= client
->thd
;
293 long lock_timeout
= client
->lock_timeout
;
294 xmlErrorPtr xml_error
= client
->xml_error
;
295 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
296 int flock_fd
= client
->flock_fd
;
298 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
299 free_client (client
);
300 memset (client
, 0, sizeof (struct client_s
));
301 client
->flock_fd
= flock_fd
;
302 client
->xml_error
= xml_error
;
305 client
->lock_timeout
= lock_timeout
;
306 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
315 strv_free ((char **) arg
);
319 parse_open_opt_lock (void *data
, void *value
)
321 struct client_s
*client
= data
;
324 client
->opts
|= OPT_LOCK_ON_OPEN
;
329 parse_opt_inquire (void *data
, void *value
)
331 struct client_s
*client
= data
;
334 client
->opts
|= OPT_INQUIRE
;
339 update_checksum (struct client_s
*client
)
343 struct cache_data_s
*cdata
;
344 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
351 cdata
= cache_get_data (client
->filename
);
355 cdata
->crc
= xmalloc (len
);
356 memcpy (cdata
->crc
, crc
, len
);
363 validate_checksum (struct client_s
*client
, const char *filename
,
364 struct cache_data_s
*cdata
, unsigned char **r_crc
,
372 if (cdata
&& !cdata
->crc
)
373 return GPG_ERR_CHECKSUM
;
375 rc
= get_checksum (filename
, &crc
, &len
);
380 n
= memcmp (cdata
->crc
, crc
, len
);
381 else if (client
->crc
)
382 n
= memcmp (client
->crc
, crc
, len
);
392 return n
? GPG_ERR_CHECKSUM
: 0;
396 open_command (assuan_context_t ctx
, char *line
)
399 struct client_s
*client
= assuan_get_pointer (ctx
);
400 char **req
, *filename
;
402 assuan_peercred_t peer
;
403 struct cache_data_s
*cdata
= NULL
;
405 unsigned char *crc
= NULL
;
407 struct argv_s
*args
[] = {
408 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
412 rc
= parse_options (&line
, args
, client
, 1);
414 return send_error (ctx
, rc
);
416 req
= str_split (line
, " ", 2);
418 return send_error (ctx
, GPG_ERR_SYNTAX
);
420 rc
= do_validate_peer (ctx
, req
[0], &peer
);
421 if (rc
== GPG_ERR_FORBIDDEN
)
422 rc
= peer_is_invoker (client
);
427 return send_error (ctx
, rc
);
431 if (!valid_filename (filename
))
434 return send_error (ctx
, GPG_ERR_INV_VALUE
);
437 pthread_cleanup_push ((void *)req_free
, req
);
438 /* This client may have locked a different file with ISCACHED --lock than
439 * the current filename. This will remove that lock. */
440 same_file
= client
->filename
&& !strcmp (filename
, client
->filename
);
441 if (client
->flags
& FLAG_OPEN
||
442 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
444 uint32_t opts
= client
->opts
;
445 uint32_t flags
= client
->flags
;
448 client
->flags
|= FLAG_KEEP_LOCK
;
449 else if (client
->flags
& FLAG_NEW
)
450 cache_clear (NULL
, client
->filename
, 0);
452 reset_client (client
);
454 client
->flags
|= flags
;
455 client
->flags
&= ~(FLAG_LOCK_CMD
);
457 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
460 client
->filename
= str_dup (filename
);
461 if (!client
->filename
)
464 return send_error(ctx
, GPG_ERR_ENOMEM
);
467 /* Need to lock the mutex here because file_modified() cannot without
468 * knowing the filename. */
469 rc
= lock_file_mutex (client
, 1);
471 client
->flags
&= ~FLAG_OPEN
;
475 rc
= lock_flock (ctx
, filename
, LOCK_SH
, &client
->flock_fd
);
476 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
483 char *keyfile
= config_get_string (client
->filename
, "passphrase_file");
485 rc
= crypto_init (&client
->crypto
, client
->ctx
, client
->filename
,
486 client
->flags
& FLAG_NO_PINENTRY
, keyfile
);
490 if (!rc
&& lstat (client
->filename
, &st
) == -1)
491 rc
= gpg_error_from_errno (errno
);
492 else if (!rc
&& !S_ISREG (st
.st_mode
)) // to match LS output
493 rc
= gpg_error_from_errno (ENODEV
);
496 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_ENOENT
)
498 cdata
= cache_get_data (client
->filename
);
503 client
->flags
|= FLAG_NEW
;
504 // data file disappeared. clear the cache entry.
505 cache_clear (NULL
, client
->filename
, 1);
508 else if (cdata
&& cdata
->doc
) // cached document
513 if (!rc
&& !(client
->flags
& FLAG_NEW
))
515 rc
= validate_checksum (client
, client
->filename
, cdata
, &crc
,
517 if (rc
== GPG_ERR_CHECKSUM
)
526 rc
= cache_iscached (client
->filename
, &defer
, 0, 0);
527 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
537 log_write ("%s: %s", client
->filename
,
538 pwmd_strerror (GPG_ERR_CHECKSUM
));
539 cache_clear (NULL
, client
->filename
, 1);
541 rc
= crypto_decrypt (client
, client
->crypto
);
546 if (client
->thd
->remote
547 && config_get_boolean (client
->filename
, "tcp_require_key")
548 && !(client
->flags
& FLAG_NEW
))
549 rc
= crypto_try_decrypt (client
,
550 (client
->flags
& FLAG_NO_PINENTRY
));
556 client
->crypto
->plaintext
= cdata
->doc
;
557 client
->crypto
->plaintext_size
= cdata
->size
;
558 rc
= cache_decrypt (client
->crypto
);
563 strv_free (client
->crypto
->pubkey
);
564 strv_free (client
->crypto
->sigkey
);
565 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
566 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
571 client
->crypto
->plaintext
= NULL
;
572 client
->crypto
->plaintext_size
= 0;
576 else // existing file
578 cached
= cdata
!= NULL
;
579 rc
= crypto_decrypt (client
, client
->crypto
);
585 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
588 rc
= cache_encrypt (client
->crypto
);
590 cache_clear (NULL
, client
->filename
, 1);
593 int timeout
= config_get_integer (client
->filename
,
596 cache_free_data_once (cdata
);
597 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
598 cdata
->doc
= client
->crypto
->plaintext
;
599 cdata
->size
= client
->crypto
->plaintext_size
;
600 cdata
->pubkey
= strv_dup(client
->crypto
->pubkey
);
601 cdata
->sigkey
= strv_dup(client
->crypto
->sigkey
);
602 client
->crypto
->plaintext
= NULL
;
603 client
->crypto
->plaintext_size
= 0;
605 if (cached
) // wont increment the refcount
607 /* Prevent using another FD to update the checksum for a
608 * cached data file. The validity has already been
611 client
->crc
= xmalloc (crclen
);
612 memcpy (client
->crc
, crc
, crclen
);
614 cdata
->crc
= xmalloc (crclen
);
615 memcpy (cdata
->crc
, crc
, crclen
);
617 rc
= cache_set_data (client
->filename
, cdata
);
618 /* The cache entry may have been removed and cache_set_data()
619 * already sent STATUS_CACHE. */
620 if (!cache_iscached (client
->filename
, NULL
, 0, 0))
621 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
624 rc
= cache_add_file (client
->filename
, cdata
, timeout
);
629 pthread_cleanup_pop (1);
632 if (!rc
&& !(client
->flags
& FLAG_NEW
) && !cached
)
633 rc
= update_checksum (client
);
637 client
->flags
|= FLAG_OPEN
;
639 if (client
->flags
& FLAG_NEW
)
640 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
642 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
643 rc
= do_lock (client
, 0);
648 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
650 if (client
->flags
& FLAG_NEW
)
651 cache_clear (NULL
, client
->filename
, 1);
653 crypto_free (client
->crypto
);
654 client
->crypto
= NULL
;
655 client
->flags
&= ~FLAG_OPEN
;
658 crypto_free_non_keys (client
->crypto
);
660 return send_error (ctx
, rc
);
663 /* If not the invoking_user or is an existing file, check that the list of user
664 * supplied key ID's are in the list of current key ID's obtained when
665 * decrypting the data file.
668 permitted_to_save (struct client_s
*client
, const char **keys
,
674 if ((client
->flags
& FLAG_NEW
) || (client
->opts
& OPT_SYMMETRIC
))
677 rc
= peer_is_invoker (client
);
682 if ((!value
&& !keys
) || ((value
&& !*value
) && (keys
&& !*keys
)))
685 for (v
= value
; v
&& *v
; v
++)
687 const char **k
, *pv
= *v
;
690 if (*pv
== '0' && *(pv
+1) == 'x')
693 for (k
= keys
; k
&& *k
; k
++)
697 if (*pk
== '0' && *(pk
+1) == 'x')
700 rc
= !strcmp (pv
, pk
) ? 0 : GPG_ERR_FORBIDDEN
;
702 rc
= !strcmp (pv
, *k
) ? 0 : GPG_ERR_FORBIDDEN
;
712 return GPG_ERR_FORBIDDEN
;
718 /* Requires that the keyid be a fingerprint in 16 byte form. */
720 parse_save_opt_keyid_common (struct client_s
*client
, const char **list
,
721 const char *value
, char ***dst
)
728 keys
= str_split (value
, ",", 0);
730 return GPG_ERR_ENOMEM
;
733 rc
= crypto_keyid_to_16b (keys
);
735 rc
= permitted_to_save (client
, list
, (const char **)keys
);
746 parse_save_opt_keyid (void *data
, void *value
)
748 struct client_s
*client
= data
;
749 const char *str
= value
;
753 rc
= parse_save_opt_keyid_common (client
,
754 (const char **)client
->crypto
->pubkey
,
759 client
->crypto
->save
.pubkey
= dst
;
764 parse_save_opt_sign_keyid (void *data
, void *value
)
766 struct client_s
*client
= data
;
767 const char *str
= value
;
771 rc
= parse_save_opt_keyid_common (client
,
772 (const char **)client
->crypto
->sigkey
,
778 client
->opts
|= OPT_NO_SIGNER
;
780 client
->crypto
->save
.sigkey
= dst
;
785 parse_save_opt_inquire (struct client_s
*client
, uint32_t opt
)
787 if (opt
== OPT_INQUIRE
&& !(client
->flags
& FLAG_NEW
))
789 gpg_error_t rc
= peer_is_invoker (client
);
800 parse_save_opt_inquire_keyparam (void *data
, void *value
)
803 return parse_save_opt_inquire (data
, OPT_INQUIRE
);
807 parse_save_opt_inquire_keyid (void *data
, void *value
)
810 return parse_save_opt_inquire (data
, OPT_INQUIRE_KEYID
);
814 parse_save_opt_inquire_sign_keyid (void *data
, void *value
)
817 return parse_save_opt_inquire (data
, OPT_INQUIRE_SIGN_KEYID
);
821 parse_save_opt_symmetric (void *data
, void *value
)
823 struct client_s
*client
= data
;
826 client
->opts
|= OPT_SYMMETRIC
;
830 /* Tests that the keys in new_keys are also in old_keys. */
832 compare_keys (char **new_keys
, char **old_keys
)
836 if (!old_keys
|| !*old_keys
)
839 crypto_keyid_to_16b (new_keys
);
841 for (o
= old_keys
; *o
; o
++)
845 for (n
= new_keys
; *n
; n
++)
847 if (!strcmp (*n
, *o
))
852 return GPG_ERR_NOT_FOUND
;
859 inquire_keyid (struct client_s
*client
, uint32_t opt
)
862 unsigned char *result
= NULL
;
868 if (opt
== OPT_INQUIRE_KEYID
)
871 orig
= &client
->crypto
->pubkey
;
872 save
= &client
->crypto
->save
.pubkey
;
876 s
= "INQUIRE_SIGN_KEYID";
877 orig
= &client
->crypto
->sigkey
;
878 save
= &client
->crypto
->save
.sigkey
;
881 rc
= assuan_inquire (client
->ctx
, s
, &result
, &len
, 0);
886 rc
= parse_save_opt_keyid_common (client
, (const char **)*orig
,
887 (char *)result
, &dst
);
890 if (!dst
&& opt
== OPT_INQUIRE_SIGN_KEYID
891 && client
->opts
& OPT_SYMMETRIC
)
892 client
->opts
|= OPT_NO_SIGNER
;
902 /* The caching test of gpg-agent is both a blessing and a curse. When a key
903 * lookup in its cache fails it tries the last successful key to be unlocked.
904 * This prevents pwmd from successfully clearing a signing key since the
905 * previous key, which more than likely belongs to the same keypair as the
906 * encryption/decryption key, will have the passphrase cached and therefore the
907 * signing key also cached. So we need to clear both the signing and encryption
908 * keys to get the effect of requiring a passphrase when generating a new
909 * keypair for an existing data file, or when the "require_save_key"
910 * configuration parameter is set. The "require_save_key" parameter is mostly a
911 * failsafe of the gpg-agent option --ignore-cache-for-signing since
912 * some/most/all users may fail to set it.
915 save_command (assuan_context_t ctx
, char *line
)
917 struct client_s
*client
= assuan_get_pointer (ctx
);
918 struct cache_data_s
*cdata
= NULL
;
919 gpg_error_t rc
= 0, cached
= 0;
922 struct argv_s
*args
[] = {
923 &(struct argv_s
) {"keyid", OPTION_TYPE_ARG
, parse_save_opt_keyid
},
924 &(struct argv_s
) {"sign-keyid", OPTION_TYPE_OPTARG
,
925 parse_save_opt_sign_keyid
},
926 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
927 parse_save_opt_inquire_keyparam
},
928 &(struct argv_s
) {"inquire-keyid", OPTION_TYPE_NOARG
,
929 parse_save_opt_inquire_keyid
},
930 &(struct argv_s
) {"inquire-sign-keyid", OPTION_TYPE_NOARG
,
931 parse_save_opt_inquire_sign_keyid
},
932 &(struct argv_s
) {"symmetric", OPTION_TYPE_NOARG
,
933 parse_save_opt_symmetric
},
937 crypto_free_save (&client
->crypto
->save
);
939 if (!(client
->flags
& FLAG_NEW
))
941 rc
= crypto_is_symmetric (client
->filename
);
942 if (!rc
|| rc
== GPG_ERR_BAD_DATA
)
945 client
->opts
|= OPT_SYMMETRIC
;
952 return send_error (ctx
, rc
);
954 rc
= parse_options (&line
, args
, client
, 0);
956 return send_error (ctx
, rc
);
959 if (config_get_boolean (client
->filename
, "require_save_key")
960 || (client
->opts
& OPT_SYMMETRIC
))
961 client
->opts
|= OPT_ASK
;
963 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
964 return send_error (ctx
, gpg_error_from_errno (errno
));
966 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
968 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENODEV
));
969 return send_error (ctx
, GPG_ERR_ENODEV
);
973 cached
= rc
= cache_iscached (client
->filename
, &defer
, 0, 1);
975 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
977 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
981 return send_error (ctx
, rc
);
983 /* Specifying both a recipient and symmetric encryption is an error. */
984 if (client
->opts
& OPT_INQUIRE_KEYID
&& client
->opts
& OPT_SYMMETRIC
)
986 return send_error (ctx
, GPG_ERR_CONFLICT
);
988 else if (client
->opts
& OPT_INQUIRE
&& client
->opts
& OPT_SYMMETRIC
)
990 return send_error (ctx
, GPG_ERR_CONFLICT
);
992 /* Existing file with a recipient and wanting symmetric is an error. */
993 else if (client
->opts
& OPT_SYMMETRIC
&& client
->crypto
->save
.pubkey
)
995 return send_error (ctx
, GPG_ERR_CONFLICT
);
997 else if (((client
->opts
& OPT_INQUIRE_KEYID
)
998 || (client
->opts
& OPT_INQUIRE_SIGN_KEYID
)))
1000 if (client
->opts
& OPT_INQUIRE
)
1001 return send_error (ctx
, GPG_ERR_CONFLICT
);
1003 if (client
->opts
& OPT_INQUIRE_KEYID
)
1004 rc
= inquire_keyid (client
, OPT_INQUIRE_KEYID
);
1006 if (!rc
&& (client
->opts
& OPT_INQUIRE_SIGN_KEYID
))
1007 rc
= inquire_keyid (client
, OPT_INQUIRE_SIGN_KEYID
);
1009 else if ((client
->crypto
->save
.pubkey
|| client
->crypto
->save
.sigkey
)
1010 && client
->opts
& OPT_INQUIRE
)
1011 return send_error (ctx
, GPG_ERR_CONFLICT
);
1015 client
->crypto
->keyfile
= config_get_string (client
->filename
,
1017 rc
= crypto_init_ctx (client
->crypto
, (client
->flags
& FLAG_NO_PINENTRY
),
1018 client
->crypto
->keyfile
);
1021 if (!rc
&& (client
->opts
& OPT_INQUIRE
))
1023 /* Require a passphrase when generating a new key pair for an existing
1026 if (!(client
->flags
& FLAG_NEW
))
1028 rc
= cache_clear_agent_keys (client
->filename
, 1, 1);
1031 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1032 client
->opts
&= ~OPT_ASK
;
1038 unsigned char *params
= NULL
;
1041 rc
= assuan_inquire (client
->ctx
, "KEYPARAM", ¶ms
, &len
, 0);
1044 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1045 pthread_cleanup_push ((void *)xfree
, params
);
1046 rc
= crypto_genkey (client
, client
->crypto
, params
);
1047 pthread_cleanup_pop (1);
1051 else if (!rc
&& (client
->flags
& FLAG_NEW
))
1053 if (!client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
1054 && !(client
->opts
& OPT_SYMMETRIC
))
1056 char *params
= crypto_default_key_params ();
1059 rc
= GPG_ERR_ENOMEM
;
1063 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1064 pthread_cleanup_push ((void *)xfree
, params
);
1065 rc
= crypto_genkey (client
, client
->crypto
,
1066 (unsigned char *)params
);
1067 pthread_cleanup_pop (1);
1072 if (!client
->crypto
->save
.pubkey
&& client
->crypto
->save
.sigkey
1073 && !(client
->opts
& OPT_SYMMETRIC
))
1074 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->save
.sigkey
);
1076 if (client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
)
1077 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->save
.pubkey
);
1082 cdata
= cache_get_data (client
->filename
);
1085 if (client
->crypto
->save
.pubkey
)
1086 rc
= compare_keys (client
->crypto
->save
.pubkey
, cdata
->pubkey
);
1088 /* Always allow a signer for symmetric data files. */
1089 if (!rc
&& client
->crypto
->save
.sigkey
1090 && !(client
->opts
& OPT_SYMMETRIC
))
1091 rc
= compare_keys (client
->crypto
->save
.sigkey
, cdata
->sigkey
);
1093 /* Prevent saving to a recipient who is not in the original recipient
1094 * list without a passphrase. */
1095 if (rc
|| (client
->crypto
->sigkey
&& client
->opts
& OPT_NO_SIGNER
))
1096 client
->opts
|= OPT_ASK
;
1098 if (rc
== GPG_ERR_NOT_FOUND
)
1100 rc
= peer_is_invoker (client
);
1101 if (rc
== GPG_ERR_EACCES
)
1102 rc
= GPG_ERR_FORBIDDEN
;
1105 if (!client
->crypto
->save
.pubkey
1106 && !(client
->opts
& OPT_SYMMETRIC
))
1107 client
->crypto
->save
.pubkey
= strv_dup (cdata
->pubkey
);
1109 if (!rc
&& !client
->crypto
->save
.sigkey
&& cdata
->sigkey
1110 && !(client
->opts
& OPT_NO_SIGNER
))
1111 client
->crypto
->save
.sigkey
= strv_dup (cdata
->sigkey
);
1112 else if (!rc
&& !client
->crypto
->save
.sigkey
1113 && (client
->opts
& OPT_NO_SIGNER
)
1114 && !(client
->opts
& OPT_SYMMETRIC
))
1115 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
1119 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->pubkey
);
1120 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->sigkey
);
1124 if (!rc
&& !(client
->flags
& FLAG_NEW
))
1126 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1127 if (client
->opts
& OPT_SYMMETRIC
)
1128 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
1130 if (client
->opts
& OPT_ASK
)
1132 rc
= cache_clear_agent_keys (client
->filename
, 1, 1);
1134 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1138 if (!rc
&& client
->opts
& OPT_SYMMETRIC
)
1139 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
1142 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1148 xmlDocDumpFormatMemory (client
->doc
, &client
->crypto
->plaintext
, &size
,
1151 client
->crypto
->plaintext_size
= (size_t) size
;
1153 rc
= GPG_ERR_ENOMEM
;
1157 client
->crypto
->flags
|= (client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_SYMMETRIC
: 0;
1158 client
->crypto
->flags
|= (client
->flags
& FLAG_NEW
) ? CRYPTO_FLAG_NEWFILE
: 0;
1159 client
->crypto
->flags
|= !(client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_PASSWD_SIGN
: 0;
1160 rc
= crypto_encrypt (client
, client
->crypto
);
1161 client
->crypto
->flags
&= ~CRYPTO_FLAG_SYMMETRIC
;
1166 rc
= crypto_write_file (client
->crypto
);
1169 if (!cached
) // no error
1170 cdata
= cache_get_data (client
->filename
);
1175 rc
= cache_encrypt (client
->crypto
);
1178 cache_clear (NULL
, client
->filename
, 1);
1179 strv_free (client
->crypto
->pubkey
);
1180 client
->crypto
->pubkey
= strv_dup (client
->crypto
->save
.pubkey
);
1181 strv_free (client
->crypto
->sigkey
);
1182 client
->crypto
->sigkey
= strv_dup (client
->crypto
->save
.sigkey
);
1183 client
->flags
&= ~(FLAG_NEW
);
1189 int timeout
= config_get_integer (client
->filename
,
1192 cache_free_data_once (cdata
);
1193 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1194 cdata
->doc
= client
->crypto
->plaintext
;
1195 client
->crypto
->plaintext
= NULL
;
1196 cdata
->size
= client
->crypto
->plaintext_size
;
1197 client
->crypto
->plaintext_size
= 0;
1198 cdata
->pubkey
= client
->crypto
->save
.pubkey
;
1199 client
->crypto
->save
.pubkey
= NULL
;
1200 cdata
->sigkey
= client
->crypto
->save
.sigkey
;
1201 client
->crypto
->save
.sigkey
= NULL
;
1203 /* Update in case the cache entry expires the next SAVE may not
1204 * have any known keys. */
1205 strv_free (client
->crypto
->pubkey
);
1206 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
1207 strv_free (client
->crypto
->sigkey
);
1208 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
1210 if (!cached
) // no error and wont increment refcount
1211 rc
= cache_set_data (client
->filename
, cdata
);
1213 rc
= cache_add_file (client
->filename
, cdata
, timeout
);
1215 if (!rc
&& (cached
|| (client
->flags
& FLAG_NEW
)))
1216 send_status_all (STATUS_CACHE
, NULL
);
1220 rc
= update_checksum (client
);
1221 client
->flags
&= ~(FLAG_NEW
);
1227 crypto_free_non_keys (client
->crypto
);
1228 return send_error (ctx
, rc
);
1232 do_delete (assuan_context_t ctx
, char *line
)
1234 struct client_s
*client
= assuan_get_pointer (ctx
);
1235 struct xml_request_s
*req
;
1239 rc
= xml_new_request (client
, line
, XML_CMD_DELETE
, &req
);
1243 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1244 xml_free_request (req
);
1248 rc
= xml_is_element_owner (client
, n
);
1250 rc
= xml_unlink_node (client
, n
);
1256 delete_command (assuan_context_t ctx
, char *line
)
1258 struct client_s
*client
= assuan_get_pointer (ctx
);
1260 struct argv_s
*args
[] = {
1261 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1265 rc
= parse_options (&line
, args
, client
, 1);
1266 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1267 rc
= GPG_ERR_SYNTAX
;
1269 return send_error (ctx
, rc
);
1271 if (client
->opts
& OPT_INQUIRE
)
1273 unsigned char *result
;
1276 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1278 return send_error (ctx
, rc
);
1280 pthread_cleanup_push ((void *)xfree
, result
);
1281 rc
= do_delete (ctx
, (char *)result
);
1282 pthread_cleanup_pop (1);
1285 rc
= do_delete (ctx
, line
);
1287 return send_error (ctx
, rc
);
1291 update_element_expirey (struct client_s
*client
, xmlNodePtr n
)
1294 xmlChar
*expire
, *incr
;
1296 time_t e
, e_orig
, i
;
1297 time_t now
= time (NULL
);
1299 expire
= xml_attribute_value (n
, (xmlChar
*)"expire");
1304 e
= strtoul ((char *)expire
, &p
, 10);
1305 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1308 rc
= GPG_ERR_ERANGE
;
1313 incr
= xml_attribute_value (n
, (xmlChar
*)"expire_increment");
1317 i
= strtoul ((char *)incr
, &p
, 10);
1318 if (errno
|| *p
|| i
== ULONG_MAX
|| *incr
== '-')
1321 rc
= GPG_ERR_ERANGE
;
1328 p
= str_asprintf ("%lu", e
);
1331 rc
= GPG_ERR_ENOMEM
;
1335 rc
= xml_add_attribute (client
, n
, "expire", p
);
1338 rc
= send_status (client
->ctx
, STATUS_EXPIRE
, "%lu %lu", e_orig
, e
);
1345 store_command (assuan_context_t ctx
, char *line
)
1347 struct client_s
*client
= assuan_get_pointer (ctx
);
1350 unsigned char *result
;
1351 xmlNodePtr n
, parent
;
1353 char *content
= NULL
;
1354 struct xml_request_s
*req
;
1357 return send_error (ctx
, GPG_ERR_SYNTAX
);
1359 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1361 return send_error (ctx
, rc
);
1363 rc
= xml_new_request (client
, (char *)result
, XML_CMD_STORE
, &req
);
1366 return send_error (ctx
, rc
);
1368 /* Prevent passing the element content around to save some memory. */
1369 len
= strv_length (req
->args
);
1370 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1371 if (*(req
->args
+1) && !xml_valid_element_path (req
->args
, has_content
))
1373 xml_free_request (req
);
1374 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1377 if (has_content
|| !*req
->args
[len
-1])
1380 content
= req
->args
[len
-1];
1381 req
->args
[len
-1] = NULL
;
1384 if (strv_length (req
->args
) > 1)
1386 rc
= xml_check_recursion (client
, req
);
1387 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1391 parent
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1395 rc
= xml_is_element_owner (client
, parent
);
1398 n
= xml_find_text_node (parent
->children
);
1400 xmlNodeSetContent (n
, (xmlChar
*) content
);
1402 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1404 (void)xml_update_element_mtime (client
, parent
);
1405 (void)update_element_expirey (client
, parent
);
1411 xml_free_request (req
);
1412 return send_error (ctx
, rc
);
1416 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1418 struct client_s
*client
= assuan_get_pointer (ctx
);
1422 int progress
= config_get_integer ("global", "xfer_progress");
1425 if (!(client
->flags
& FLAG_LOCK_CMD
))
1426 rc
= unlock_file_mutex (client
, 0);
1427 if (rc
&& rc
!= GPG_ERR_NOT_LOCKED
)
1431 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1432 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1433 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1441 if (sent
+ to_send
> total
)
1442 to_send
= total
- sent
;
1444 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1445 flush
? 0 : to_send
);
1448 sent
+= flush
? 0 : to_send
;
1450 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1451 (sent
== total
&& flush
))
1452 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1454 if (!flush
&& !rc
&& sent
== total
)
1461 while (!rc
&& sent
< total
);
1467 do_get (assuan_context_t ctx
, char *line
)
1469 struct client_s
*client
= assuan_get_pointer (ctx
);
1471 struct xml_request_s
*req
;
1474 rc
= xml_new_request (client
, line
, XML_CMD_NONE
, &req
);
1478 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1481 if (n
&& n
->children
)
1485 n
= xml_find_text_node (n
->children
);
1486 if (!n
|| !n
->content
|| !*n
->content
)
1487 rc
= GPG_ERR_NO_DATA
;
1490 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1493 xmlChar
*expire
= xml_attribute_value (tmp
,
1494 (xmlChar
*)"expire");
1497 time_t now
= time (NULL
);
1502 e
= strtoul ((char *)expire
, &p
, 10);
1503 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1504 log_write (_("invalid expire attribute value: %s"),
1507 rc
= send_status (ctx
, STATUS_EXPIRE
, "%lu 0", e
);
1515 rc
= GPG_ERR_NO_DATA
;
1518 xml_free_request (req
);
1523 get_command (assuan_context_t ctx
, char *line
)
1525 struct client_s
*client
= assuan_get_pointer (ctx
);
1527 struct argv_s
*args
[] = {
1528 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1532 rc
= parse_options (&line
, args
, client
, 1);
1533 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1534 rc
= GPG_ERR_SYNTAX
;
1536 return send_error (ctx
, rc
);
1538 if (client
->opts
& OPT_INQUIRE
)
1540 unsigned char *result
;
1543 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1545 return send_error (ctx
, rc
);
1547 pthread_cleanup_push ((void *)xfree
, result
);
1548 rc
= do_get (ctx
, (char *)result
);
1549 pthread_cleanup_pop (1);
1552 rc
= do_get (ctx
, line
);
1554 return send_error (ctx
, rc
);
1557 static void list_command_free1 (void *arg
);
1559 realpath_command (assuan_context_t ctx
, char *line
)
1562 char **realpath
= NULL
;
1563 struct client_s
*client
= assuan_get_pointer (ctx
);
1564 struct argv_s
*args
[] = {
1565 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1569 rc
= parse_options (&line
, args
, client
, 1);
1570 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1571 rc
= GPG_ERR_SYNTAX
;
1573 return send_error (ctx
, rc
);
1575 if (client
->opts
& OPT_INQUIRE
)
1577 unsigned char *result
;
1580 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1582 return send_error (ctx
, rc
);
1584 pthread_cleanup_push ((void *)xfree
, result
);
1585 (void)xml_resolve_path (client
, client
->doc
, result
, &realpath
, &rc
);
1586 pthread_cleanup_pop (1);
1589 (void)xml_resolve_path (client
, client
->doc
, (unsigned char *)line
,
1594 char *tmp
= strv_join ((char *)"\t", realpath
);
1596 strv_free (realpath
);
1598 return send_error (ctx
, GPG_ERR_ENOMEM
);
1600 pthread_cleanup_push ((void *)xfree
, tmp
);
1601 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
1602 pthread_cleanup_pop (1);
1605 return send_error (ctx
, rc
);
1609 list_command_free1 (void *arg
)
1612 string_free ((struct string_s
*) arg
, 1);
1616 parse_list_opt_recurse (void *data
, void *value
)
1618 struct client_s
*client
= data
;
1621 client
->opts
|= OPT_LIST_RECURSE
;
1626 parse_opt_verbose (void *data
, void *value
)
1628 struct client_s
*client
= data
;
1631 client
->opts
|= OPT_VERBOSE
;
1636 list_path_once (struct client_s
*client
, char *line
,
1637 struct element_list_s
*elements
, struct string_s
*result
)
1641 rc
= xml_create_path_list (client
, client
->doc
, NULL
, elements
, line
, 0);
1642 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
)
1647 int total
= slist_length (elements
->list
);
1653 for (i
= 0; i
< total
; i
++)
1655 char *tmp
= slist_nth_data (elements
->list
, i
);
1657 string_append_printf (result
, "%s%s", tmp
,
1658 i
+ 1 == total
? "" : "\n");
1662 rc
= GPG_ERR_NO_DATA
;
1669 do_list (assuan_context_t ctx
, char *line
)
1671 struct client_s
*client
= assuan_get_pointer (ctx
);
1672 gpg_error_t rc
= GPG_ERR_NO_DATA
;
1673 struct element_list_s
*elements
= NULL
;
1675 if (!line
|| !*line
)
1677 struct string_s
*str
= string_new (NULL
);
1681 return GPG_ERR_ENOMEM
;
1683 pthread_cleanup_push ((void *)list_command_free1
, str
);
1684 n
= xmlDocGetRootElement (client
->doc
);
1685 for (n
= n
->children
; n
; n
= n
->next
)
1689 if (n
->type
!= XML_ELEMENT_NODE
)
1692 name
= xmlGetProp (n
, (xmlChar
*)"_name");
1695 rc
= GPG_ERR_ENOMEM
;
1699 elements
= xcalloc (1, sizeof (struct element_list_s
));
1703 rc
= GPG_ERR_ENOMEM
;
1707 elements
->prefix
= string_new (NULL
);
1708 if (!elements
->prefix
)
1711 xml_free_element_list (elements
);
1712 rc
= GPG_ERR_ENOMEM
;
1716 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1717 elements
->root_only
= !elements
->recurse
;
1718 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1719 rc
= list_path_once (client
, (char *)name
, elements
, str
);
1721 pthread_cleanup_pop (1);
1726 string_append (str
, "\n");
1730 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1732 pthread_cleanup_pop (1);
1736 elements
= xcalloc (1, sizeof (struct element_list_s
));
1738 return GPG_ERR_ENOMEM
;
1740 elements
->prefix
= string_new (NULL
);
1741 if (!elements
->prefix
)
1743 xml_free_element_list (elements
);
1744 return GPG_ERR_ENOMEM
;
1747 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1748 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1749 struct string_s
*str
= string_new (NULL
);
1750 pthread_cleanup_push ((void *)list_command_free1
, str
);
1751 rc
= list_path_once (client
, line
, elements
, str
);
1753 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1755 pthread_cleanup_pop (1);
1756 pthread_cleanup_pop (1);
1761 list_command (assuan_context_t ctx
, char *line
)
1763 struct client_s
*client
= assuan_get_pointer (ctx
);
1765 struct argv_s
*args
[] = {
1766 &(struct argv_s
) {"recurse", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1767 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1768 /* These are deprecated but kept for backward compatibility with older
1770 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, NULL
},
1771 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
, NULL
},
1772 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1773 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
, NULL
},
1777 if (disable_list_and_dump
== 1)
1778 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1780 rc
= parse_options (&line
, args
, client
, 1);
1781 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1782 rc
= GPG_ERR_SYNTAX
;
1784 return send_error (ctx
, rc
);
1786 if (client
->opts
& OPT_INQUIRE
)
1788 unsigned char *result
;
1791 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1793 return send_error (ctx
, rc
);
1795 pthread_cleanup_push ((void *)xfree
, result
);
1796 rc
= do_list (ctx
, (char *)result
);
1797 pthread_cleanup_pop (1);
1800 rc
= do_list (ctx
, line
);
1802 return send_error (ctx
, rc
);
1805 #define RESUMABLE_ERROR(rc) (rc == GPG_ERR_ELOOP || rc == GPG_ERR_EACCES \
1806 || rc == GPG_ERR_ELEMENT_NOT_FOUND || !rc)
1808 * args[0] - element path
1811 attribute_list (assuan_context_t ctx
, char **args
)
1813 struct client_s
*client
= assuan_get_pointer (ctx
);
1814 char **attrlist
= NULL
;
1818 xmlNodePtr n
, an
, r
;
1821 struct xml_request_s
*req
;
1823 if (!args
|| !args
[0] || !*args
[0])
1824 return GPG_ERR_SYNTAX
;
1826 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_LIST
, &req
);
1830 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1831 xml_free_request (req
);
1836 for (a
= n
->properties
; a
; a
= a
->next
)
1840 if (target
&& xml_reserved_attribute ((char *)a
->name
))
1843 pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *));
1847 strv_free (attrlist
);
1849 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1850 pwmd_strerror (GPG_ERR_ENOMEM
));
1851 return GPG_ERR_ENOMEM
;
1856 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
, an
&& an
->content
1857 ? (char *)an
->content
: "");
1861 strv_free (attrlist
);
1862 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1863 pwmd_strerror (GPG_ERR_ENOMEM
));
1864 return GPG_ERR_ENOMEM
;
1867 attrlist
[++i
] = NULL
;
1871 return GPG_ERR_NO_DATA
;
1875 r
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[0], NULL
, &rc
);
1876 if (!RESUMABLE_ERROR (rc
))
1878 strv_free (attrlist
);
1881 else if (!rc
&& r
!= n
)
1891 line
= strv_join ("\n", attrlist
);
1894 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1895 pwmd_strerror (GPG_ERR_ENOMEM
));
1896 strv_free (attrlist
);
1897 return GPG_ERR_ENOMEM
;
1900 pthread_cleanup_push ((void *)xfree
, line
);
1901 pthread_cleanup_push ((void *)req_free
, attrlist
);
1902 rc
= xfer_data (ctx
, line
, strlen (line
));
1903 pthread_cleanup_pop (1);
1904 pthread_cleanup_pop (1);
1909 * args[0] - attribute
1910 * args[1] - element path
1913 attribute_delete (struct client_s
*client
, char **args
)
1918 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
1919 return GPG_ERR_SYNTAX
;
1921 if (!strcmp (args
[0], "_name") || !strcmp (args
[0], "target"))
1922 return GPG_ERR_INV_ATTR
;
1924 if (!xml_reserved_attribute (args
[0]))
1925 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
1928 struct xml_request_s
*req
;
1930 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
1934 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1935 xml_free_request (req
);
1939 rc
= xml_is_element_owner (client
, n
);
1942 rc
= xml_delete_attribute (client
, n
, (xmlChar
*) args
[0]);
1948 * Creates the "target" attribute. When other commands encounter an element
1949 * with this attribute they follow the "target" after appending remaining
1950 * elements from the original element path to the target. The exception is the
1951 * ATTR command that operates on the element itself (for reserved attribute
1952 * names) and not the target.
1954 * If the source element path doesn't exist when using 'ATTR SET target', it is
1955 * created, but the destination element path must exist. This is simliar to the
1956 * ls(1) command: ln -s src dst.
1958 * args[0] - source element path
1959 * args[1] - destination element path
1962 set_target_attribute (struct client_s
*client
, char **args
)
1964 struct xml_request_s
*req
= NULL
;
1969 if (!args
|| !args
[0] || !args
[1])
1970 return GPG_ERR_SYNTAX
;
1972 src
= str_split (args
[0], "\t", 0);
1974 return GPG_ERR_SYNTAX
;
1976 if (!xml_valid_element_path (src
, 0))
1979 return GPG_ERR_INV_VALUE
;
1982 dst
= str_split (args
[1], "\t", 0);
1986 return GPG_ERR_SYNTAX
;
1989 // Be sure the destination element path exists. */
1990 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
1994 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1995 if (rc
&& rc
!= GPG_ERR_EACCES
)
1998 xml_free_request (req
);
2001 if (!strcmp (args
[0], args
[1]))
2003 rc
= GPG_ERR_EEXIST
;
2007 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_TARGET
, &req
);
2011 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2012 if (rc
&& rc
!= GPG_ERR_EACCES
)
2017 rc
= xml_add_attribute (client
, n
, "target", args
[1]);
2019 rc
= xml_unlink_node (client
, n
->children
);
2025 xml_free_request (req
);
2030 * args[0] - attribute
2031 * args[1] - element path
2034 attribute_get (assuan_context_t ctx
, char **args
)
2036 struct client_s
*client
= assuan_get_pointer (ctx
);
2040 struct xml_request_s
*req
;
2042 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
2043 return GPG_ERR_SYNTAX
;
2045 if (!xml_reserved_attribute (args
[0]))
2046 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2049 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2053 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2054 xml_free_request (req
);
2060 a
= xmlGetProp (n
, (xmlChar
*) args
[0]);
2062 return GPG_ERR_NOT_FOUND
;
2064 pthread_cleanup_push ((void *)xmlFree
, a
);
2067 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2069 rc
= GPG_ERR_NO_DATA
;
2071 pthread_cleanup_pop (1);
2076 * args[0] - attribute
2077 * args[1] - element path
2081 attribute_set (struct client_s
*client
, char **args
)
2083 struct xml_request_s
*req
;
2087 if (!args
|| !args
[0] || !args
[1])
2088 return GPG_ERR_SYNTAX
;
2091 * Reserved attribute names.
2093 if (!strcmp (args
[0], "_name")) // Use the RENAME command instead
2094 return GPG_ERR_INV_ATTR
;
2095 else if (!strcmp (args
[0], "target"))
2096 return set_target_attribute (client
, args
+ 1);
2097 else if (!xml_valid_attribute (args
[0]))
2098 return GPG_ERR_INV_VALUE
;
2100 if (!xml_valid_attribute_value (args
[2]))
2101 return GPG_ERR_INV_VALUE
;
2103 if (!xml_reserved_attribute (args
[0]))
2105 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2108 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
2111 rc
= xml_check_recursion (client
, req
);
2112 xml_free_request (req
);
2118 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2122 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2123 xml_free_request (req
);
2127 rc
= xml_is_element_owner (client
, n
);
2130 rc
= xml_add_attribute (client
, n
, args
[0], args
[2]);
2137 * req[1] - attribute name or element path if command is LIST
2138 * req[2] - element path
2139 * req[2] - element path or value
2143 do_attr (assuan_context_t ctx
, char *line
)
2145 struct client_s
*client
= assuan_get_pointer (ctx
);
2149 req
= str_split (line
, " ", 4);
2150 if (!req
|| !req
[0] || !req
[1])
2153 return GPG_ERR_SYNTAX
;
2156 pthread_cleanup_push ((void *)req_free
, req
);
2158 if (strcasecmp (req
[0], "SET") == 0)
2159 rc
= attribute_set (client
, req
+ 1);
2160 else if (strcasecmp (req
[0], "GET") == 0)
2161 rc
= attribute_get (ctx
, req
+ 1);
2162 else if (strcasecmp (req
[0], "DELETE") == 0)
2163 rc
= attribute_delete (client
, req
+ 1);
2164 else if (strcasecmp (req
[0], "LIST") == 0)
2165 rc
= attribute_list (ctx
, req
+ 1);
2167 rc
= GPG_ERR_SYNTAX
;
2169 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2170 pthread_cleanup_pop (1);
2175 attr_command (assuan_context_t ctx
, char *line
)
2177 struct client_s
*client
= assuan_get_pointer (ctx
);
2179 struct argv_s
*args
[] = {
2180 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2184 rc
= parse_options (&line
, args
, client
, 1);
2185 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2186 rc
= GPG_ERR_SYNTAX
;
2188 return send_error (ctx
, rc
);
2190 if (client
->opts
& OPT_INQUIRE
)
2192 unsigned char *result
;
2195 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2197 return send_error (ctx
, rc
);
2199 pthread_cleanup_push ((void *)xfree
, result
);
2200 rc
= do_attr (ctx
, (char *)result
);
2201 pthread_cleanup_pop (1);
2204 rc
= do_attr (ctx
, line
);
2206 return send_error (ctx
, rc
);
2210 parse_iscached_opt_lock (void *data
, void *value
)
2212 struct client_s
*client
= data
;
2215 client
->opts
|= OPT_LOCK
;
2220 parse_iscached_opt_agent (void *data
, void *value
)
2222 struct client_s
*client
= data
;
2225 client
->opts
|= OPT_CACHE_AGENT
;
2230 parse_iscached_opt_sign (void *data
, void *value
)
2232 struct client_s
*client
= data
;
2235 client
->opts
|= OPT_CACHE_SIGN
;
2240 iscached_command (assuan_context_t ctx
, char *line
)
2242 struct client_s
*client
= assuan_get_pointer (ctx
);
2245 struct argv_s
*args
[] = {
2246 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2247 &(struct argv_s
) {"agent", OPTION_TYPE_NOARG
, parse_iscached_opt_agent
},
2248 &(struct argv_s
) {"sign", OPTION_TYPE_NOARG
, parse_iscached_opt_sign
},
2252 if (!line
|| !*line
)
2253 return send_error (ctx
, GPG_ERR_SYNTAX
);
2255 rc
= parse_options (&line
, args
, client
, 1);
2257 return send_error (ctx
, rc
);
2258 else if (!valid_filename (line
))
2259 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2261 if (!(client
->flags
& FLAG_OPEN
)
2262 && ((client
->opts
& OPT_CACHE_AGENT
) || (client
->opts
& OPT_CACHE_SIGN
)))
2263 return send_error (ctx
, GPG_ERR_INV_STATE
);
2265 rc
= cache_iscached (line
, &defer
, (client
->opts
& OPT_CACHE_AGENT
),
2266 (client
->opts
& OPT_CACHE_SIGN
));
2268 rc
= GPG_ERR_NO_DATA
;
2272 struct cache_data_s
*cdata
= cache_get_data (line
);
2276 rc
= validate_checksum (client
, line
, cdata
, NULL
, NULL
);
2277 if (rc
== GPG_ERR_CHECKSUM
)
2278 rc
= GPG_ERR_NO_DATA
;
2282 if (client
->opts
& OPT_LOCK
2283 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2285 gpg_error_t trc
= rc
;
2287 if (strcmp (line
, client
->filename
))
2288 reset_client (client
);
2290 xfree (client
->filename
);
2291 client
->filename
= str_dup (line
);
2292 rc
= do_lock (client
, 1);
2297 return send_error (ctx
, rc
);
2301 clearcache_command (assuan_context_t ctx
, char *line
)
2303 struct client_s
*client
= assuan_get_pointer (ctx
);
2304 gpg_error_t rc
= 0, all_rc
= 0;
2308 struct client_thread_s
*once
= NULL
;
2312 pthread_cleanup_push (cache_release_mutex
, NULL
);
2313 count
= cache_file_count ();
2314 MUTEX_LOCK (&cn_mutex
);
2315 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
2317 if (!line
|| !*line
)
2320 t
= slist_length (cn_thread_list
);
2322 for (i
= 0; i
< t
; i
++)
2324 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2325 assuan_peercred_t peer
;
2330 /* Lock each connected clients' file mutex to prevent any other client
2331 * from accessing the cache entry (the file mutex is locked upon
2332 * command startup). The cache for the entry is not cleared if the
2333 * file mutex is locked by another client to prevent this function
2334 * from blocking. Rather, it returns an error.
2338 if (thd
->cl
->filename
)
2340 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2341 /* The current client doesn't have permission to open the other
2342 * filename do to "access" configuration parameter in a filename
2343 * section. Since we are clearning all cache entries the error
2344 * will be returned later during cache_clear(). */
2351 else // Idle client without opened file?
2354 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2355 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2357 /* The current client owns the lock. */
2358 if (pthread_equal (pthread_self (), thd
->tid
))
2362 if (cache_iscached (thd
->cl
->filename
, NULL
, 0, 0)
2369 /* The cache entry will be cleared when the other client
2370 * disconnects and cache_timer_thread() does its thing. */
2371 cache_defer_clear (thd
->cl
->filename
);
2374 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2382 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1);
2383 cache_unlock_mutex (thd
->cl
->filename
, 0);
2393 /* A single data filename was specified. Lock only this data file
2394 * mutex and free the cache entry. */
2395 rc
= do_validate_peer (ctx
, line
, &peer
);
2396 if (rc
== GPG_ERR_FORBIDDEN
)
2397 rc
= peer_is_invoker (client
);
2399 if (rc
== GPG_ERR_EACCES
)
2400 rc
= GPG_ERR_FORBIDDEN
;
2402 if (!rc
&& thd
->cl
->filename
&& !strcmp (thd
->cl
->filename
, line
))
2404 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->filename
, -1, 0, -1);
2405 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2407 /* The current client owns the lock. */
2408 if (pthread_equal (pthread_self (), thd
->tid
))
2415 rc
= cache_clear (NULL
, thd
->cl
->filename
, 1);
2416 cache_unlock_mutex (thd
->cl
->filename
, 0);
2419 cache_defer_clear (thd
->cl
->filename
);
2421 /* Found a client with the opened file. The cache entry has been
2422 * either cleared (self) or defered to be cleared. */
2428 /* Only connected clients' cache entries have been cleared. Now clear any
2429 * remaining cache entries without clients but only if there wasn't an
2430 * error from above since this would defeat the locking check of the
2431 * remaining entries. */
2432 if (all
&& !all_rc
&& cache_file_count ())
2434 rc
= cache_clear (client
, NULL
, 1);
2435 if (rc
== GPG_ERR_EACCES
)
2436 rc
= GPG_ERR_FORBIDDEN
;
2439 /* No clients are using the specified file. */
2440 else if (!all_rc
&& !rc
&& !once
)
2441 rc
= cache_clear (NULL
, line
, 1);
2443 /* Release the connection mutex. */
2444 pthread_cleanup_pop (1);
2446 if (!rc
|| (cache_file_count () && count
!= cache_file_count ()))
2447 send_status_all (STATUS_CACHE
, NULL
);
2449 /* Release the cache mutex. */
2450 pthread_cleanup_pop (1);
2452 /* One or more files were locked while clearing all cache entries. */
2456 return send_error (ctx
, rc
);
2460 cachetimeout_command (assuan_context_t ctx
, char *line
)
2462 struct client_s
*client
= assuan_get_pointer (ctx
);
2464 char **req
= str_split (line
, " ", 0);
2467 assuan_peercred_t peer
;
2469 if (!req
|| !*req
|| !req
[1])
2472 return send_error (ctx
, GPG_ERR_SYNTAX
);
2476 timeout
= (int) strtol (req
[1], &p
, 10);
2477 if (errno
!= 0 || *p
|| timeout
< -1)
2480 return send_error (ctx
, GPG_ERR_SYNTAX
);
2483 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2484 if (rc
== GPG_ERR_FORBIDDEN
)
2486 rc
= peer_is_invoker (client
);
2487 if (rc
== GPG_ERR_EACCES
)
2488 rc
= GPG_ERR_FORBIDDEN
;
2493 rc
= cache_set_timeout (req
[0], timeout
);
2494 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2497 MUTEX_LOCK (&rcfile_mutex
);
2498 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2500 MUTEX_UNLOCK (&rcfile_mutex
);
2505 return send_error (ctx
, rc
);
2509 dump_command (assuan_context_t ctx
, char *line
)
2513 struct client_s
*client
= assuan_get_pointer (ctx
);
2516 if (disable_list_and_dump
== 1)
2517 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2520 return send_error (ctx
, GPG_ERR_SYNTAX
);
2522 rc
= peer_is_invoker(client
);
2524 return send_error (ctx
, rc
);
2526 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2530 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2531 pwmd_strerror (GPG_ERR_ENOMEM
));
2532 return send_error (ctx
, GPG_ERR_ENOMEM
);
2535 pthread_cleanup_push ((void *)xmlFree
, xml
);
2536 rc
= xfer_data (ctx
, (char *) xml
, len
);
2537 pthread_cleanup_pop (1);
2538 return send_error (ctx
, rc
);
2542 getconfig_command (assuan_context_t ctx
, char *line
)
2544 struct client_s
*client
= assuan_get_pointer (ctx
);
2546 char filename
[255] = { 0 }, param
[747] = { 0 };
2547 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2549 if (!line
|| !*line
)
2550 return send_error (ctx
, GPG_ERR_SYNTAX
);
2552 if (strchr (line
, ' '))
2554 int ret
= sscanf (line
, " %254[^ ] %746c", filename
, param
);
2557 return send_error (ctx
, gpg_error_from_syserror());
2562 if (fp
&& !valid_filename (fp
))
2563 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2565 paramp
= str_down (paramp
);
2566 p
= config_get_value (fp
? fp
: "global", paramp
);
2568 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2570 tmp
= expand_homedir (p
);
2574 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2575 pwmd_strerror (GPG_ERR_ENOMEM
));
2576 return send_error (ctx
, GPG_ERR_ENOMEM
);
2580 pthread_cleanup_push ((void *)xfree
, p
);
2581 rc
= xfer_data (ctx
, p
, strlen (p
));
2582 pthread_cleanup_pop (1);
2583 return send_error (ctx
, rc
);
2588 xmlXPathContextPtr xp
;
2589 xmlXPathObjectPtr result
;
2595 xpath_command_free (void *arg
)
2597 struct xpath_s
*xpath
= arg
;
2602 req_free (xpath
->req
);
2605 xmlBufferFree (xpath
->buf
);
2608 xmlXPathFreeObject (xpath
->result
);
2611 xmlXPathFreeContext (xpath
->xp
);
2615 do_xpath (assuan_context_t ctx
, char *line
)
2618 struct client_s
*client
= assuan_get_pointer (ctx
);
2619 struct xpath_s _x
= { 0 };
2620 struct xpath_s
*xpath
= &_x
;
2622 if (!line
|| !*line
)
2623 return GPG_ERR_SYNTAX
;
2625 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2627 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2628 return GPG_ERR_ENOMEM
;
2631 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2634 rc
= GPG_ERR_BAD_DATA
;
2639 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2642 rc
= GPG_ERR_BAD_DATA
;
2646 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2648 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2652 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2653 xpath
->result
->nodesetval
,
2654 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2658 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2660 rc
= GPG_ERR_NO_DATA
;
2663 else if (xpath
->req
[1])
2669 pthread_cleanup_push ((void *)xpath_command_free
, &xpath
);
2670 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2671 xmlBufferLength (xpath
->buf
));
2672 pthread_cleanup_pop (0);
2674 xpath_command_free (xpath
);
2679 xpath_command (assuan_context_t ctx
, char *line
)
2681 struct client_s
*client
= assuan_get_pointer (ctx
);
2683 struct argv_s
*args
[] = {
2684 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2688 if (disable_list_and_dump
== 1)
2689 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2691 rc
= peer_is_invoker(client
);
2693 return send_error (ctx
, rc
);
2695 rc
= parse_options (&line
, args
, client
, 1);
2696 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2697 rc
= GPG_ERR_SYNTAX
;
2699 return send_error (ctx
, rc
);
2701 if (client
->opts
& OPT_INQUIRE
)
2703 unsigned char *result
;
2706 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2708 return send_error (ctx
, rc
);
2710 pthread_cleanup_push ((void *)xfree
, result
);
2711 rc
= do_xpath (ctx
, (char *)result
);
2712 pthread_cleanup_pop (1);
2715 rc
= do_xpath (ctx
, line
);
2717 return send_error (ctx
, rc
);
2721 do_xpathattr (assuan_context_t ctx
, char *line
)
2723 struct client_s
*client
= assuan_get_pointer (ctx
);
2727 struct xpath_s _x
= { 0 };
2728 struct xpath_s
*xpath
= &_x
;
2730 if (!line
|| !*line
)
2731 return GPG_ERR_SYNTAX
;
2733 if ((req
= str_split (line
, " ", 3)) == NULL
)
2734 return GPG_ERR_ENOMEM
;
2738 rc
= GPG_ERR_SYNTAX
;
2742 if (!strcasecmp (req
[0], "SET"))
2744 else if (!strcasecmp (req
[0], "DELETE"))
2748 rc
= GPG_ERR_SYNTAX
;
2752 if (!req
[1] || !req
[2])
2754 rc
= GPG_ERR_SYNTAX
;
2758 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2760 rc
= GPG_ERR_ENOMEM
;
2764 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2766 rc
= GPG_ERR_SYNTAX
;
2770 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2773 rc
= GPG_ERR_BAD_DATA
;
2777 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2780 rc
= GPG_ERR_BAD_DATA
;
2784 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2786 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2790 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2791 xpath
->result
->nodesetval
,
2792 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2793 (xmlChar
*) req
[1]);
2796 xpath_command_free (xpath
);
2801 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2803 xpathattr_command (assuan_context_t ctx
, char *line
)
2805 struct client_s
*client
= assuan_get_pointer (ctx
);
2807 struct argv_s
*args
[] = {
2808 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2812 if (disable_list_and_dump
== 1)
2813 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2815 rc
= peer_is_invoker(client
);
2817 return send_error (ctx
, rc
);
2819 rc
= parse_options (&line
, args
, client
, 1);
2820 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2821 rc
= GPG_ERR_SYNTAX
;
2823 return send_error (ctx
, rc
);
2825 if (client
->opts
& OPT_INQUIRE
)
2827 unsigned char *result
;
2830 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2832 return send_error (ctx
, rc
);
2834 pthread_cleanup_push ((void *)xfree
, result
);
2835 rc
= do_xpathattr (ctx
, (char *)result
);
2836 pthread_cleanup_pop (1);
2839 rc
= do_xpathattr (ctx
, line
);
2841 return send_error (ctx
, rc
);
2845 do_import (struct client_s
*client
, const char *root_element
,
2846 unsigned char *content
)
2848 xmlDocPtr doc
= NULL
;
2849 xmlNodePtr n
= NULL
, root
;
2851 struct string_s
*str
= NULL
, *tstr
= NULL
;
2852 struct xml_request_s
*req
= NULL
;
2854 if (!content
|| !*content
)
2855 return GPG_ERR_SYNTAX
;
2859 rc
= xml_new_request (client
, root_element
, XML_CMD_STORE
, &req
);
2866 if (!xml_valid_element_path (req
->args
, 0))
2868 xml_free_request (req
);
2870 return GPG_ERR_INV_VALUE
;
2874 str
= string_new_content ((char *)content
);
2875 tstr
= string_prepend (str
, "<pwmd>");
2879 tstr
= string_append (str
, "</pwmd>");
2881 rc
= GPG_ERR_ENOMEM
;
2884 rc
= GPG_ERR_ENOMEM
;
2889 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2890 string_free (str
, 1);
2893 rc
= GPG_ERR_BAD_DATA
;
2897 root
= xmlDocGetRootElement (doc
);
2898 root
= root
->children
;
2899 if (root
->type
!= XML_ELEMENT_NODE
)
2901 rc
= GPG_ERR_BAD_DATA
;
2905 rc
= xml_validate_import (client
, root
);
2911 /* Create the new specified root element path, if needed. */
2912 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
2917 /* Overwrite the children of the specified root element path. */
2918 (void)xml_unlink_node (client
, n
->children
);
2922 n
= xmlDocGetRootElement (client
->doc
);
2927 xmlChar
*name
= xml_attribute_value (root
, (xmlChar
*)"_name");
2930 rc
= GPG_ERR_BAD_DATA
;
2933 /* No --root argument passed. Overwrite the current documents' root
2934 * element matching the root element of the imported data. */
2935 xml_free_request (req
);
2937 rc
= xml_new_request (client
, (char *)name
, XML_CMD_DELETE
, &req
);
2943 tmp
= xml_find_elements (client
, req
,
2944 xmlDocGetRootElement (req
->doc
), &rc
);
2945 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2949 (void)xml_unlink_node (client
, tmp
);
2957 copy
= xmlCopyNodeList (root
);
2959 rc
= GPG_ERR_ENOMEM
;
2962 n
= xmlAddChildList (n
, copy
);
2964 rc
= GPG_ERR_ENOMEM
;
2969 if (!rc
&& n
&& n
->parent
)
2970 rc
= xml_update_element_mtime (client
, n
->parent
);
2973 xml_free_request (req
);
2982 parse_import_opt_root (void *data
, void *value
)
2984 struct client_s
*client
= data
;
2986 client
->import_root
= str_dup (value
);
2987 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
2991 import_command (assuan_context_t ctx
, char *line
)
2994 struct client_s
*client
= assuan_get_pointer (ctx
);
2995 unsigned char *result
;
2997 struct argv_s
*args
[] = {
2998 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
3002 xfree (client
->import_root
);
3003 client
->import_root
= NULL
;
3004 rc
= parse_options (&line
, args
, client
, 0);
3006 return send_error (ctx
, rc
);
3008 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3011 xfree (client
->import_root
);
3012 client
->import_root
= NULL
;
3013 return send_error (ctx
, rc
);
3016 rc
= do_import (client
, client
->import_root
, result
);
3017 xfree (client
->import_root
);
3018 client
->import_root
= NULL
;
3019 return send_error (ctx
, rc
);
3023 do_lock (struct client_s
*client
, int add
)
3025 gpg_error_t rc
= lock_file_mutex (client
, add
);
3028 client
->flags
|= FLAG_LOCK_CMD
;
3034 lock_command (assuan_context_t ctx
, char *line
)
3036 struct client_s
*client
= assuan_get_pointer (ctx
);
3040 return send_error (ctx
, GPG_ERR_SYNTAX
);
3042 rc
= do_lock (client
, 0);
3043 return send_error (ctx
, rc
);
3047 unlock_command (assuan_context_t ctx
, char *line
)
3049 struct client_s
*client
= assuan_get_pointer (ctx
);
3053 return send_error (ctx
, GPG_ERR_SYNTAX
);
3055 rc
= unlock_file_mutex (client
, 0);
3056 return send_error (ctx
, rc
);
3060 get_set_env (const char *name
, const char *value
)
3062 if (value
&& *value
)
3063 setenv (name
, value
, 1);
3069 option_command (assuan_context_t ctx
, char *line
)
3071 struct client_s
*client
= assuan_get_pointer (ctx
);
3073 char namebuf
[255] = { 0 };
3074 char *name
= namebuf
;
3075 char *value
= NULL
, *p
, *tmp
= NULL
;
3077 p
= strchr (line
, '=');
3080 strncpy (namebuf
, line
, sizeof(namebuf
));
3081 namebuf
[sizeof(namebuf
)-1] = 0;
3085 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
3086 namebuf
[sizeof(namebuf
)-1] = 0;
3090 log_write2 ("OPTION name='%s' value='%s'", name
, value
);
3092 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
3098 n
= strtol (value
, &tmp
, 10);
3100 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3103 client
->lock_timeout
= n
;
3105 else if (strcasecmp (name
, (char *) "client-state") == 0)
3111 n
= strtol (value
, &tmp
, 10);
3112 if ((tmp
&& *tmp
) || (n
< 0 || n
> 1))
3113 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3114 client
->client_state
= n
;
3117 client
->client_state
= 0;
3119 else if (strcasecmp (name
, (char *) "NAME") == 0)
3121 if (value
&& strchr (value
, ' '))
3122 rc
= GPG_ERR_INV_VALUE
;
3125 tmp
= pthread_getspecific (thread_name_key
);
3126 pthread_setspecific (thread_name_key
, NULL
);
3128 MUTEX_LOCK (&cn_mutex
);
3130 if (value
&& *value
)
3131 pthread_setspecific (thread_name_key
, str_dup (value
));
3133 MUTEX_UNLOCK (&cn_mutex
);
3136 else if (strcasecmp (name
, "disable-pinentry") == 0)
3140 if (value
&& *value
)
3142 n
= (int) strtol (value
, &tmp
, 10);
3143 if (*tmp
|| n
< 0 || n
> 1)
3144 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3148 client
->flags
|= FLAG_NO_PINENTRY
;
3150 client
->flags
&= ~FLAG_NO_PINENTRY
;
3152 else if (strcasecmp (name
, "ttyname") == 0)
3154 get_set_env ("GPG_TTY", value
);
3156 else if (strcasecmp (name
, "ttytype") == 0)
3158 get_set_env ("TERM", value
);
3160 else if (strcasecmp (name
, "display") == 0)
3162 get_set_env ("DISPLAY", value
);
3164 else if (strcasecmp (name
, "lc_messages") == 0)
3167 else if (strcasecmp (name
, "lc_ctype") == 0)
3170 else if (strcasecmp (name
, "desc") == 0)
3173 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3177 rc
= GPG_ERR_UNKNOWN_OPTION
;
3179 return send_error (ctx
, rc
);
3183 do_rename (assuan_context_t ctx
, char *line
)
3185 struct client_s
*client
= assuan_get_pointer (ctx
);
3187 xmlNodePtr src
, dst
;
3188 struct string_s
*str
= NULL
, *tstr
;
3189 struct xml_request_s
*req
;
3192 args
= str_split (line
, " ", 0);
3193 if (!args
|| strv_length (args
) != 2)
3196 return GPG_ERR_SYNTAX
;
3199 if (!xml_valid_element ((xmlChar
*) args
[1]))
3202 return GPG_ERR_INV_VALUE
;
3205 rc
= xml_new_request (client
, args
[0], XML_CMD_RENAME
, &req
);
3212 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3216 rc
= xml_is_element_owner (client
, src
);
3220 xmlChar
*a
= xmlGetProp (src
, (xmlChar
*) "_name");
3223 rc
= GPG_ERR_ENOMEM
;
3227 /* To prevent unwanted effects:
3229 * <element _name="a"><element _name="b"/></element>
3233 if (xmlStrEqual (a
, (xmlChar
*) args
[1]))
3236 rc
= GPG_ERR_EEXIST
;
3241 str
= string_new (args
[0]);
3244 rc
= GPG_ERR_ENOMEM
;
3248 p
= strrchr (str
->str
, '\t');
3250 tstr
= string_truncate (str
, strlen (str
->str
)-strlen (p
));
3252 tstr
= string_truncate (str
, 0);
3256 string_free (str
, 1);
3257 rc
= GPG_ERR_ENOMEM
;
3262 tstr
= string_append_printf (str
, "%s%s", str
->len
? "\t" : "", args
[1]);
3265 string_free (str
, 1);
3266 rc
= GPG_ERR_ENOMEM
;
3271 xml_free_request (req
);
3272 rc
= xml_new_request (client
, str
->str
, XML_CMD_RENAME
, &req
);
3273 string_free (str
, 1);
3280 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3281 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3286 /* Target may exist:
3288 * <element _name="a"/>
3289 * <element _name="b" target="a"/>
3295 rc
= GPG_ERR_EEXIST
;
3301 rc
= xml_is_element_owner (client
, dst
);
3305 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3307 xml_unlink_node (client
, dst
);
3310 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3313 xml_free_request (req
);
3319 rename_command (assuan_context_t ctx
, char *line
)
3321 struct client_s
*client
= assuan_get_pointer (ctx
);
3323 struct argv_s
*args
[] = {
3324 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3328 rc
= parse_options (&line
, args
, client
, 1);
3329 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3330 rc
= GPG_ERR_SYNTAX
;
3332 return send_error (ctx
, rc
);
3334 if (client
->opts
& OPT_INQUIRE
)
3336 unsigned char *result
;
3339 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3341 return send_error (ctx
, rc
);
3343 pthread_cleanup_push ((void *)xfree
, result
);
3344 rc
= do_rename (ctx
, (char *)result
);
3345 pthread_cleanup_pop (1);
3348 rc
= do_rename (ctx
, line
);
3350 return send_error (ctx
, rc
);
3354 do_copy (assuan_context_t ctx
, char *line
)
3356 struct client_s
*client
= assuan_get_pointer (ctx
);
3357 char **args
, **targs
;
3358 xmlNodePtr src
, dst
, tree
= NULL
;
3359 struct xml_request_s
*req
;
3362 args
= str_split (line
, " ", 0);
3363 if (!args
|| strv_length (args
) != 2)
3366 return GPG_ERR_SYNTAX
;
3369 targs
= str_split (args
[1], "\t", 0);
3373 return GPG_ERR_SYNTAX
;
3376 if (!xml_valid_element_path (targs
, 0))
3380 return GPG_ERR_INV_VALUE
;
3384 rc
= xml_new_request (client
, args
[0], XML_CMD_NONE
, &req
);
3391 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3395 tree
= xmlCopyNodeList (src
);
3398 rc
= GPG_ERR_ENOMEM
;
3402 xml_free_request (req
);
3403 /* Create the destination element path if it does not exist. */
3404 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &req
);
3411 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3415 rc
= xml_is_element_owner (client
, dst
);
3419 rc
= xml_validate_target (client
, req
->doc
, args
[1], src
);
3420 if (rc
|| src
== dst
)
3422 rc
= rc
? rc
: GPG_ERR_EEXIST
;
3426 /* Merge any attributes from the src node to the initial dst node. */
3427 for (xmlAttrPtr attr
= tree
->properties
; attr
; attr
= attr
->next
)
3429 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3432 xmlAttrPtr a
= xmlHasProp (dst
, attr
->name
);
3436 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3437 xmlNewProp (dst
, attr
->name
, tmp
);
3439 /* Create the default attributes. */
3440 rc
= xml_add_attribute (client
, dst
, NULL
, NULL
);
3443 xmlNodePtr n
= dst
->children
;
3444 (void)xml_unlink_node (client
, n
);
3445 dst
->children
= NULL
;
3449 n
= xmlCopyNodeList (tree
->children
);
3452 rc
= GPG_ERR_ENOMEM
;
3456 n
= xmlAddChildList (dst
, n
);
3459 rc
= GPG_ERR_ENOMEM
;
3463 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
)
3464 == dst
->parent
? dst
: dst
->parent
);
3469 (void)xml_unlink_node (client
, tree
);
3471 xml_free_request (req
);
3477 copy_command (assuan_context_t ctx
, char *line
)
3479 struct client_s
*client
= assuan_get_pointer (ctx
);
3481 struct argv_s
*args
[] = {
3482 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3486 rc
= parse_options (&line
, args
, client
, 1);
3487 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3488 rc
= GPG_ERR_SYNTAX
;
3490 return send_error (ctx
, rc
);
3492 if (client
->opts
& OPT_INQUIRE
)
3494 unsigned char *result
;
3497 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3499 return send_error (ctx
, rc
);
3501 pthread_cleanup_push ((void *)xfree
, result
);
3502 rc
= do_copy (ctx
, (char *)result
);
3503 pthread_cleanup_pop (1);
3506 rc
= do_copy (ctx
, line
);
3508 return send_error (ctx
, rc
);
3512 do_move (assuan_context_t ctx
, char *line
)
3514 struct client_s
*client
= assuan_get_pointer (ctx
);
3516 xmlNodePtr src
, dst
;
3517 struct xml_request_s
*req
;
3520 args
= str_split (line
, " ", 0);
3521 if (!args
|| strv_length (args
) != 2)
3524 return GPG_ERR_SYNTAX
;
3529 char **targs
= str_split (args
[1], "\t", 0);
3533 return GPG_ERR_ENOMEM
;
3536 if (!xml_valid_element_path (targs
, 0))
3540 return GPG_ERR_INV_VALUE
;
3546 rc
= xml_new_request (client
, args
[0], XML_CMD_MOVE
, &req
);
3553 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3557 rc
= xml_is_element_owner (client
, src
);
3561 xml_free_request (req
);
3565 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
3569 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
3573 dst
= xmlDocGetRootElement (client
->doc
);
3575 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3580 for (xmlNodePtr n
= dst
; n
; n
= n
->parent
)
3584 rc
= GPG_ERR_EEXIST
;
3591 xmlChar
*a
= xml_attribute_value (src
, (xmlChar
*) "_name");
3592 xmlNodePtr dup
= xml_find_element (client
, dst
->children
, (char *) a
, &rc
);
3595 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3604 rc
= GPG_ERR_EEXIST
;
3608 if (dst
== xmlDocGetRootElement (client
->doc
))
3613 while (n
->parent
&& n
->parent
!= dst
)
3616 a
= xml_attribute_value (n
, (xmlChar
*) "_name");
3617 xmlChar
*b
= xml_attribute_value (src
, (xmlChar
*) "_name");
3619 if (xmlStrEqual (a
, b
))
3622 xmlUnlinkNode (src
);
3623 (void)xml_unlink_node (client
, n
);
3630 (void)xml_unlink_node (client
, dup
);
3633 xmlUnlinkNode (dup
);
3637 if (!dst
&& *req
->args
)
3639 struct xml_request_s
*nreq
= NULL
;
3640 xmlChar
*name
= xml_attribute_value (src
, (xmlChar
*) "_name");
3642 if (src
->parent
== xmlDocGetRootElement (client
->doc
)
3643 && !strcmp ((char *) name
, *req
->args
))
3646 rc
= GPG_ERR_EEXIST
;
3651 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &nreq
);
3654 dst
= xml_find_elements (client
, nreq
,
3655 xmlDocGetRootElement (nreq
->doc
), &rc
);
3656 xml_free_request (nreq
);
3663 xml_update_element_mtime (client
, src
->parent
);
3664 xmlUnlinkNode (src
);
3666 dst
= xmlAddChildList (dst
, src
);
3668 rc
= GPG_ERR_ENOMEM
;
3670 xml_update_element_mtime (client
, dst
->parent
);
3673 xml_free_request (req
);
3679 move_command (assuan_context_t ctx
, char *line
)
3681 struct client_s
*client
= assuan_get_pointer (ctx
);
3683 struct argv_s
*args
[] = {
3684 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3688 rc
= parse_options (&line
, args
, client
, 1);
3689 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3690 rc
= GPG_ERR_SYNTAX
;
3692 return send_error (ctx
, rc
);
3694 if (client
->opts
& OPT_INQUIRE
)
3696 unsigned char *result
;
3699 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3701 return send_error (ctx
, rc
);
3703 pthread_cleanup_push ((void *)xfree
, result
);
3704 rc
= do_move (ctx
, (char *)result
);
3705 pthread_cleanup_pop (1);
3708 rc
= do_move (ctx
, line
);
3710 return send_error (ctx
, rc
);
3714 ls_command (assuan_context_t ctx
, char *line
)
3722 return send_error (ctx
, GPG_ERR_SYNTAX
);
3724 tmp
= str_asprintf ("%s/data", homedir
);
3725 dir
= expand_homedir (tmp
);
3728 rc
= gpg_error_from_errno (errno
);
3733 return send_error (ctx
, rc
);
3737 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3738 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3742 pthread_cleanup_push ((void *)xfree
, p
);
3743 pthread_cleanup_push ((void *)(void *)(void *)closedir
, d
);
3746 while (!readdir_r (d
, p
, &cur
) && cur
)
3750 if (lstat (cur
->d_name
, &st
) == -1 || !S_ISREG (st
.st_mode
))
3753 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3760 rc
= GPG_ERR_ENOMEM
;
3768 pthread_cleanup_pop (1); // closedir (d)
3769 pthread_cleanup_pop (1); // xfree (p)
3772 return send_error (ctx
, rc
);
3775 return send_error (ctx
, GPG_ERR_NO_DATA
);
3777 list
[strlen (list
) - 1] = 0;
3778 pthread_cleanup_push ((void *)xfree
, list
);
3779 rc
= xfer_data (ctx
, list
, strlen (list
));
3780 pthread_cleanup_pop (1);
3781 return send_error (ctx
, rc
);
3785 bye_notify (assuan_context_t ctx
, char *line
)
3787 struct client_s
*cl
= assuan_get_pointer (ctx
);
3788 gpg_error_t ret
= 0;
3791 update_client_state (cl
, CLIENT_STATE_DISCON
);
3795 if (cl
->thd
->remote
)
3801 struct timeval tv
= { 0, 50000 };
3803 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3804 if (rc
== GNUTLS_E_AGAIN
)
3805 select (0, NULL
, NULL
, NULL
, &tv
);
3807 while (rc
== GNUTLS_E_AGAIN
);
3811 /* This will let assuan_process_next() return. */
3812 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
3814 cl
->last_rc
= gpg_error_from_errno (errno
);
3818 cl
->last_rc
= 0; // BYE command result
3823 reset_notify (assuan_context_t ctx
, char *line
)
3825 struct client_s
*client
= assuan_get_pointer (ctx
);
3829 reset_client (client
);
3835 * This is called before every Assuan command.
3838 command_startup (assuan_context_t ctx
, const char *name
)
3840 struct client_s
*client
= assuan_get_pointer (ctx
);
3842 struct command_table_s
*cmd
= NULL
;
3844 log_write2 ("command='%s'", name
);
3845 client
->last_rc
= client
->opts
= 0;
3847 for (int i
= 0; command_table
[i
]; i
++)
3849 if (!strcasecmp (name
, command_table
[i
]->name
))
3851 if (command_table
[i
]->ignore_startup
)
3853 cmd
= command_table
[i
];
3859 return GPG_ERR_UNKNOWN_COMMAND
;
3861 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3863 update_client_state (client
, CLIENT_STATE_COMMAND
);
3869 * This is called after every Assuan command.
3872 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3874 struct client_s
*client
= assuan_get_pointer (ctx
);
3876 if (!(client
->flags
& FLAG_LOCK_CMD
))
3877 unlock_file_mutex (client
, 0);
3879 unlock_flock (&client
->flock_fd
);
3880 log_write2 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
3881 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3883 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3885 if (client
->thd
->state
!= CLIENT_STATE_DISCON
)
3886 update_client_state (client
, CLIENT_STATE_IDLE
);
3890 parse_help_opt_html (void *data
, void *value
)
3892 struct client_s
*client
= data
;
3895 client
->opts
|= OPT_HTML
;
3900 help_command (assuan_context_t ctx
, char *line
)
3904 struct client_s
*client
= assuan_get_pointer (ctx
);
3905 struct argv_s
*args
[] = {
3906 &(struct argv_s
) {"html", OPTION_TYPE_NOARG
, parse_help_opt_html
},
3910 rc
= parse_options (&line
, args
, client
, 1);
3912 return send_error (ctx
, rc
);
3914 if (!line
|| !*line
)
3917 char *help
= str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3918 "For commands that take an element path as an argument, each element is "
3919 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3922 for (i
= 0; command_table
[i
]; i
++)
3924 if (!command_table
[i
]->help
)
3927 /* @npxref{} won't put a "See " or "see " in front of the command.
3928 * It's not a texinfo command but needed for --html. */
3929 tmp
= str_asprintf ("%s %s%s%s", help
,
3930 client
->opts
& OPT_HTML
? "@npxref{" : "",
3931 command_table
[i
]->name
,
3932 client
->opts
& OPT_HTML
? "}" : "");
3937 tmp
= strip_texi_and_wrap (help
, client
->opts
& OPT_HTML
);
3939 pthread_cleanup_push ((void *)xfree
, tmp
);
3940 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3941 pthread_cleanup_pop (1);
3942 return send_error (ctx
, rc
);
3945 for (i
= 0; command_table
[i
]; i
++)
3947 if (!strcasecmp (line
, command_table
[i
]->name
))
3951 if (!command_table
[i
]->help
)
3954 help
= strip_texi_and_wrap (command_table
[i
]->help
,
3955 client
->opts
& OPT_HTML
);
3956 tmp
= str_asprintf ("%s%s",
3957 (client
->opts
& OPT_HTML
) ? "" : _("Usage: "),
3960 pthread_cleanup_push ((void *)xfree
, tmp
);
3961 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3962 pthread_cleanup_pop (1);
3963 return send_error (ctx
, rc
);
3967 return send_error (ctx
, GPG_ERR_INV_NAME
);
3971 new_command (const char *name
, int ignore
, int unlock
, int flock_type
,
3972 gpg_error_t (*handler
) (assuan_context_t
, char *),
3976 struct command_table_s
**tmp
;
3979 for (i
= 0; command_table
[i
]; i
++);
3981 tmp
= xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3983 command_table
= tmp
;
3984 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3985 command_table
[i
]->name
= name
;
3986 command_table
[i
]->handler
= handler
;
3987 command_table
[i
]->ignore_startup
= ignore
;
3988 command_table
[i
]->unlock
= unlock
;
3989 command_table
[i
]->flock_type
= flock_type
;
3990 command_table
[i
++]->help
= help
;
3991 command_table
[i
] = NULL
;
3999 for (i
= 0; command_table
[i
]; i
++)
4000 xfree (command_table
[i
]);
4002 xfree (command_table
);
4006 sort_commands (const void *arg1
, const void *arg2
)
4008 struct command_table_s
*const *a
= arg1
;
4009 struct command_table_s
*const *b
= arg2
;
4018 return strcmp ((*a
)->name
, (*b
)->name
);
4022 passwd_command (assuan_context_t ctx
, char *line
)
4024 struct client_s
*client
= assuan_get_pointer (ctx
);
4028 rc
= peer_is_invoker (client
);
4029 if (rc
== GPG_ERR_EACCES
)
4030 return send_error (ctx
, GPG_ERR_FORBIDDEN
);
4032 return send_error (ctx
, rc
);
4034 if (client
->flags
& FLAG_NEW
)
4035 return send_error (ctx
, GPG_ERR_INV_STATE
);
4037 client
->crypto
->keyfile
= config_get_string (client
->filename
,
4039 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
,
4040 client
->crypto
->keyfile
);
4042 return send_error (ctx
, rc
);
4045 rc
= crypto_passwd (client
, client
->crypto
);
4047 crypto_free_non_keys (client
->crypto
);
4048 return send_error (ctx
, rc
);
4052 parse_opt_data (void *data
, void *value
)
4054 struct client_s
*client
= data
;
4057 client
->opts
|= OPT_DATA
;
4062 send_client_list (assuan_context_t ctx
)
4064 struct client_s
*client
= assuan_get_pointer (ctx
);
4067 if (client
->opts
& OPT_VERBOSE
)
4073 MUTEX_LOCK (&cn_mutex
);
4074 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
4075 t
= slist_length (cn_thread_list
);
4077 for (i
= 0; i
< t
; i
++)
4079 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4082 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
4085 tmp
= build_client_info_line (thd
, 0);
4088 char **l
= strv_cat (list
, tmp
);
4090 rc
= GPG_ERR_ENOMEM
;
4095 rc
= GPG_ERR_ENOMEM
;
4104 pthread_cleanup_pop (1);
4108 line
= strv_join ("\n", list
);
4110 pthread_cleanup_push ((void *)xfree
, line
);
4111 rc
= xfer_data (ctx
, line
, strlen (line
));
4112 pthread_cleanup_pop (1);
4116 if (client
->opts
& OPT_DATA
)
4118 char buf
[ASSUAN_LINELENGTH
];
4120 MUTEX_LOCK (&cn_mutex
);
4121 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
4122 MUTEX_UNLOCK (&cn_mutex
);
4123 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4126 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4132 getinfo_command (assuan_context_t ctx
, char *line
)
4134 struct client_s
*client
= assuan_get_pointer (ctx
);
4136 char buf
[ASSUAN_LINELENGTH
];
4137 struct argv_s
*args
[] = {
4138 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4139 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4143 rc
= parse_options (&line
, args
, client
, 1);
4145 return send_error (ctx
, rc
);
4147 if (!strcasecmp (line
, "clients"))
4149 rc
= send_client_list (ctx
);
4151 else if (!strcasecmp (line
, "cache"))
4153 if (client
->opts
& OPT_DATA
)
4155 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4156 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4159 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4161 else if (!strcasecmp (line
, "pid"))
4163 pid_t pid
= getpid ();
4165 snprintf (buf
, sizeof (buf
), "%u", pid
);
4166 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4168 else if (!strcasecmp (line
, "version"))
4170 char *tmp
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4178 pthread_cleanup_push (xfree
, tmp
);
4179 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
4180 pthread_cleanup_pop (1);
4182 else if (!strcasecmp (line
, "last_error"))
4184 if (client
->last_error
)
4185 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4187 rc
= GPG_ERR_NO_DATA
;
4189 else if (!strcasecmp (line
, "user"))
4194 if (client
->thd
->remote
)
4195 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4197 user
= get_username (client
->thd
->peer
->uid
);
4199 user
= get_username (client
->thd
->peer
->uid
);
4203 pthread_cleanup_push ((void *)xfree
, user
);
4204 rc
= xfer_data (ctx
, user
, strlen (user
));
4205 pthread_cleanup_pop (1);
4208 rc
= GPG_ERR_NO_DATA
;
4211 rc
= gpg_error (GPG_ERR_SYNTAX
);
4213 return send_error (ctx
, rc
);
4217 parse_listkeys_opt_secret_only (void *data
, void *value
)
4219 struct client_s
*client
= data
;
4222 client
->opts
|= OPT_SECRET_ONLY
;
4227 keyinfo_command (assuan_context_t ctx
, char *line
)
4229 struct client_s
*client
= assuan_get_pointer (ctx
);
4231 char **keys
= NULL
, **p
= NULL
;
4235 return send_error (ctx
, GPG_ERR_SYNTAX
);
4237 if (!(client
->flags
& FLAG_OPEN
))
4238 return send_error (ctx
, GPG_ERR_INV_STATE
);
4240 if (client
->flags
& FLAG_NEW
)
4241 return send_error (ctx
, GPG_ERR_NO_DATA
);
4243 rc
= lock_flock (ctx
, client
->filename
, FLOCK_TYPE_SH
, &client
->flock_fd
);
4245 return send_error (ctx
, rc
);
4247 rc
= crypto_is_symmetric (client
->filename
);
4248 unlock_flock (&client
->flock_fd
);
4251 else if (rc
!= GPG_ERR_BAD_DATA
)
4252 return send_error (ctx
, rc
);
4257 p
= strv_catv(keys
, client
->crypto
->pubkey
);
4259 rc
= GPG_ERR_ENOMEM
;
4265 for (p
= client
->crypto
->sigkey
; p
&& *p
; p
++)
4267 if (!strv_printf(&keys
, "S%s", *p
))
4270 return send_error (ctx
, GPG_ERR_ENOMEM
);
4275 rc
= GPG_ERR_ENOMEM
;
4281 line
= strv_join ("\n", keys
);
4283 pthread_cleanup_push ((void *)xfree
, line
);
4285 rc
= xfer_data (ctx
, line
, strlen (line
));
4287 rc
= GPG_ERR_ENOMEM
;
4289 pthread_cleanup_pop (1);
4292 rc
= GPG_ERR_NO_DATA
;
4297 return send_error (ctx
, rc
);
4301 kill_command (assuan_context_t ctx
, char *line
)
4303 struct client_s
*client
= assuan_get_pointer (ctx
);
4307 if (!line
|| !*line
)
4308 return send_error (ctx
, GPG_ERR_SYNTAX
);
4310 MUTEX_LOCK (&cn_mutex
);
4311 pthread_cleanup_push ((void *)release_mutex_cb
, &cn_mutex
);
4312 t
= slist_length (cn_thread_list
);
4315 for (i
= 0; i
< t
; i
++)
4317 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4318 char *tmp
= str_asprintf ("%p", thd
->tid
);
4320 if (strcmp (line
, tmp
))
4327 rc
= peer_is_invoker (client
);
4330 #ifdef HAVE_PTHREAD_CANCEL
4331 pthread_cancel (thd
->tid
);
4333 pthread_kill (thd
->tid
, SIGUSR2
);
4337 else if (rc
== GPG_ERR_EACCES
)
4338 rc
= GPG_ERR_FORBIDDEN
;
4342 if (config_get_boolean ("global", "strict_kill"))
4346 if (client
->thd
->remote
&& thd
->remote
)
4348 if (!thd
->tls
|| !thd
->tls
->fp
)
4350 rc
= GPG_ERR_INV_STATE
;
4353 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
4355 #ifdef HAVE_PTHREAD_CANCEL
4356 pthread_cancel (thd
->tid
);
4358 pthread_kill (thd
->tid
, SIGUSR2
);
4363 else if (!client
->thd
->remote
&& !thd
->remote
)
4366 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
4368 #ifdef HAVE_PTHREAD_CANCEL
4369 pthread_cancel (thd
->tid
);
4371 pthread_kill (thd
->tid
, SIGUSR2
);
4378 pthread_cleanup_pop (1);
4379 return send_error (ctx
, rc
);
4383 listkeys_command (assuan_context_t ctx
, char *line
)
4385 struct client_s
*client
= assuan_get_pointer (ctx
);
4386 struct crypto_s
*crypto
= NULL
;
4387 char **pattern
= NULL
;
4388 gpgme_key_t
*keys
= NULL
;
4389 char **result
= NULL
;
4391 struct argv_s
*args
[] = {
4392 &(struct argv_s
) {"secret-only", OPTION_TYPE_NOARG
,
4393 parse_listkeys_opt_secret_only
},
4397 rc
= parse_options (&line
, args
, client
, 1);
4399 return send_error (ctx
, rc
);
4401 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
4402 client
->flags
& FLAG_NO_PINENTRY
, NULL
);
4404 return send_error (ctx
, rc
);
4406 pthread_cleanup_push ((void *)crypto_free
, crypto
);
4407 pattern
= str_split (line
, ",", 0);
4408 pthread_cleanup_push ((void *)(void *)strv_free
, pattern
);
4409 rc
= crypto_list_keys (crypto
, pattern
, client
->opts
& OPT_SECRET_ONLY
,
4411 pthread_cleanup_pop (1);
4412 pthread_cleanup_pop (1);
4417 for (i
= 0; keys
[i
]; i
++)
4419 char *p
= crypto_key_info (keys
[i
]);
4424 rc
= GPG_ERR_ENOMEM
;
4428 r
= strv_cat (result
, p
);
4431 rc
= GPG_ERR_ENOMEM
;
4439 rc
= GPG_ERR_NO_DATA
;
4441 crypto_free_key_list (keys
);
4446 line
= strv_join ("\n", result
);
4448 pthread_cleanup_push ((void *)xfree
, line
);
4450 rc
= GPG_ERR_ENOMEM
;
4452 rc
= xfer_data (ctx
, line
, strlen (line
));
4454 pthread_cleanup_pop (1);
4459 if (gpg_err_code (rc
) == GPG_ERR_NO_PUBKEY
4460 || gpg_err_code (rc
) == GPG_ERR_NO_SECKEY
)
4461 rc
= GPG_ERR_NO_DATA
;
4463 return send_error (ctx
, rc
);
4469 /* !BEGIN-HELP-TEXT!
4471 * This comment is used as a marker to generate the offline documentation
4472 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4473 * script to determine where commands begin and end.
4475 new_command("HELP", 1, 1, 0, help_command
, _(
4476 "HELP [--html] [<COMMAND>]\n"
4477 "Show available commands or command specific help text."
4479 "The @option{--html} option will output the help text in HTML format."
4482 new_command("KILL", 1, 0, 0, kill_command
, _(
4483 "KILL <thread_id>\n"
4484 "Terminates the client identified by @var{thread_id} and releases any file "
4485 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4486 "for details about listing connected clients. An @code{invoking_user} "
4487 "(@pxref{Configuration}) may kill any client while others may only kill "
4488 "clients of the same @code{UID} or @abbr{TLS} fingerprint."
4491 new_command("LISTKEYS", 0, 0, 0, listkeys_command
, _(
4492 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4493 "Returns a new line separated list of key information matching a comma "
4494 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4495 "specified, only keys matching @var{pattern} that also have a secret key "
4496 "available will be returned."
4499 new_command("KEYINFO", 1, 0, 0, keyinfo_command
, _(
4501 "Returns a new line separated list of key ID's that the currently opened "
4502 "data file has recipients and signers for. If the key is a signing key it "
4503 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4504 "signers in the case of being symmetrically encrypted, the error code "
4505 "@code{GPG_ERR_NO_DATA} is returned."
4508 new_command("GETINFO", 1, 1, 0, getinfo_command
, _(
4509 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4510 "Get server and other information. The information is returned via a status "
4511 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4514 "@var{CACHE} returns the number of cached documents."
4516 "@var{CLIENTS} returns the number of "
4517 "connected clients via a status message or a list of connected clients when "
4518 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4519 "verbose line of a client list contains "
4521 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4522 "IP address if remote, file lock status, whether the current client is self "
4523 "or not, client state (see below), "
4524 "user ID or TLS fingerprint of the connected client, username if the "
4525 "client is a local one else @code{-}, and finally the time stamp of when the "
4528 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4529 "the client has connected but hasn't completed initializing, state @code{2} "
4530 "indicates that the client is idle, state @code{3} means the "
4531 "client is in a command and state @code{4} means the client is disconnecting. "
4533 "@var{PID} returns the process ID number of the server via a data response."
4535 "@var{VERSION} returns the server version number and compile-time features "
4536 "via a data response with each being space delimited."
4538 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4539 "via a data response, when available."
4541 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4542 "via a data response."
4545 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
4547 "Changes the passphrase of the secret key required to open the current "
4548 "data file. If the data file is symmetrically encrypted, the error "
4549 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4550 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4551 "this command saving any unwanted changes to the @abbr{XML} document."
4553 "This command is not available to non-invoking clients "
4554 "(@pxref{Access Control})."
4557 new_command("OPEN", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
4558 "OPEN [--lock] <filename>\n"
4559 "Opens @var{filename}. When the @var{filename} is not found on the "
4560 "file-system then a new in-memory document will be created. If the file is "
4561 "found, it is looked for in the file cache and when found no passphrase will "
4562 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4563 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4564 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4565 "@emph{INQUIRE} the client for the passphrase."
4567 "When the @option{--lock} option is passed then the file mutex will be "
4568 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4569 "file had been opened."
4572 new_command("SAVE", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
4573 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<fpr>[,..] | [--inquire-keyid]] [--sign-keyid=<fpr>[,..] | [--inquire-sign-keyid]]\n"
4574 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4575 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4576 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4577 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4578 "for the passphrase of the secret key used for signing."
4580 "The @option{--inquire-keyparam} option will send an "
4581 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4582 "generating the new keypair. The inquired data is expected to be in "
4583 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4584 "details. Note that when this option is specified a new keypair will be "
4585 "generated reguardless if the file is a new one and that the passphrase for "
4586 "the current file will be required before generating the new keypair. This "
4587 "option is available to non-invoking clients (@pxref{Access Control}) only "
4588 "when the file is a new one."
4590 "You can encrypt the data file to a recipient other than the one that it "
4591 "was encrypted with by passing the @option{--keyid} or "
4592 "@option{--inquire-keyid} option with "
4593 "the fingerprint of a public encryption key as its argument. Use the "
4594 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4595 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4596 "may also be used to sign the data "
4597 "file with an alternate key by specifying the fingerprint of a secret key. "
4598 "A passphrase to decrypt the data file "
4599 "will be required when one or more of the original encryption or signing keys "
4600 "are not found in either of these two options' arguments or when the data "
4601 "file is symmetrically encrypted, reguardless of the @code{require_save_key} "
4602 "configuration parameter. The original encryption or signing keys will be "
4603 "used when either of these options are not specified."
4605 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4606 "for non-invoking clients "
4607 "(@pxref{Access Control}) when the recipients or signers do not match those "
4608 "that were used when the file was @code{OPEN}'ed."
4610 "The @option{--symmetric} option specifies that a new data file be "
4611 "conventionally encrypted. These types of data files do not use a recipient "
4612 "public key but may be signed by using the @option{--sign-keyid} or "
4613 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4614 "symmtrically encrypted data file, leave the option value empty. Note that "
4615 "you cannot change encryption schemes once a data file has been saved."
4618 new_command("ISCACHED", 1, 0, 0, iscached_command
, _(
4619 "ISCACHED [--lock] [--agent [--sign]] <filename>\n"
4620 "Determines the file cache status of the specified @var{filename}. "
4621 "The default is to test whether the filename is cached in memory. Passing "
4622 "option @option{--agent} will test the @command{gpg-agent} cache for at most "
4623 "one cached key used for opening the data file (@pxref{OPEN}). To test if "
4624 "a signing key is cached, pass @option{--sign} along with @option{--agent}. "
4625 "Both the @option{--agent} and @option{--sign} options require an opened data "
4628 "An @emph{OK} response is returned if the specified @var{filename} is found "
4629 "in the cache. If not found in the cache but exists on the filesystem "
4630 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4633 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4634 "the file exists; it does not need to be opened nor cached. The lock will be "
4635 "released when the client exits or sends the @code{UNLOCK} command "
4636 "(@pxref{UNLOCK}). When this option is passed the current data file is closed."
4639 new_command("CLEARCACHE", 1, 1, 0, clearcache_command
, _(
4640 "CLEARCACHE [<filename>]\n"
4641 "Clears a file cache entry for all or the specified @var{filename}. Note that "
4642 "this will also clear any @command{gpg-agent} cached keys which may cause "
4643 "problems if another data file shares the same keys as @var{filename}."
4645 "When clearing all cache entries a permissions test is done against the "
4646 "current client based on the @var{access} configuration parameter in a "
4647 "@var{filename} section. Both a cache entry may be cleared and an error "
4648 "returned depending on cached data files and client permissions."
4651 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command
, _(
4652 "CACHETIMEOUT <filename> <seconds>\n"
4653 "The time in @var{seconds} until @var{filename} will be removed from the "
4654 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4655 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4656 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4660 new_command("LIST", 0, 1, 0, list_command
, _(
4661 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4662 "If no element path is given then a newline separated list of root elements "
4663 "is returned with a data response. If given, then children of the specified "
4664 "element path are returned."
4666 "Each element path "
4667 "returned will have zero or more flags appened to it. These flags are "
4668 "delimited from the element path by a single space character. A flag itself "
4669 "is a single character. Flag @code{P} indicates that access to the element "
4670 "is denied. Flag @code{+} indicates that there are child nodes of "
4671 "the current element path. Flag @code{E} indicates that an element of the "
4672 "element path contained in a @var{target} attribute could not be found. Flag "
4673 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4674 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4675 "then an element path, is the element path of the @var{target} attribute "
4676 "contained in the current element."
4678 "When a specified element path contains an error, beit from the final "
4679 "element in the path or any previous element, the path is still shown but "
4680 "will contain the error flag for the element with the error. Determining "
4681 "the actual element which contains the error is up to the client. This can be "
4682 "done by traversing the final element up to parent elements that contain the "
4685 "The option @option{--recurse} may be used to list the entire element tree "
4686 "for a specified element path or the entire tree for all root elements."
4688 "When the @option{--inquire} option is passed then all remaining non-option "
4689 "arguments are retrieved via a server @emph{INQUIRE}."
4692 new_command("REALPATH", 0, 1, 0, realpath_command
, _(
4693 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4694 "Resolves all @code{target} attributes of the specified element path and "
4695 "returns the result with a data response. @xref{Target Attribute}, for details."
4697 "When the @option{--inquire} option is passed then all remaining non-option "
4698 "arguments are retrieved via a server @emph{INQUIRE}."
4701 new_command("STORE", 0, 1, 0, store_command
, _(
4702 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4703 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4705 "Creates a new element path or modifies the @var{content} of an existing "
4706 "element. If only a single element is specified then a new root element is "
4707 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4708 "set to the final @key{TAB} delimited element. If no @var{content} is "
4709 "specified after the final @key{TAB}, then the content of the existing "
4710 "element will be removed; or will be empty if creating a new element."
4712 "The only restriction of an element name is that it not contain whitespace "
4713 "characters. There is no other whitespace between the @key{TAB} delimited "
4714 "elements. It is recommended that the content of an element be base64 encoded "
4715 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4716 "parsing and @command{pwmd} syntax errors."
4719 new_command("RENAME", 0, 1, 0, rename_command
, _(
4720 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4721 "Renames the specified @var{element} to the new @var{value}. If an element of "
4722 "the same name as the @var{value} already exists it will be overwritten."
4724 "When the @option{--inquire} option is passed then all remaining non-option "
4725 "arguments are retrieved via a server @emph{INQUIRE}."
4728 new_command("COPY", 0, 1, 0, copy_command
, _(
4729 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4730 "Copies the entire element tree starting from the child node of the source "
4731 "element, to the destination element path. If the destination element path "
4732 "does not exist then it will be created; otherwise it is overwritten."
4734 "Note that attributes from the source element are merged into the "
4735 "destination element when the destination element path exists. When an "
4736 "attribute of the same name exists in both the source and destination "
4737 "elements then the destination attribute will be updated to the source "
4740 "When the @option{--inquire} option is passed then all remaining non-option "
4741 "arguments are retrieved via a server @emph{INQUIRE}."
4744 new_command("MOVE", 0, 1, 0, move_command
, _(
4745 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4746 "Moves the source element path to the destination element path. If the "
4747 "destination is not specified then it will be moved to the root node of the "
4748 "document. If the destination is specified and exists then it will be "
4749 "overwritten; otherwise non-existing elements of the destination element "
4750 "path will be created."
4752 "When the @option{--inquire} option is passed then all remaining non-option "
4753 "arguments are retrieved via a server @emph{INQUIRE}."
4756 new_command("DELETE", 0, 1, 0, delete_command
, _(
4757 "DELETE [--inquire] element[<TAB>child[..]]\n"
4758 "Removes the specified element path and all of its children. This may break "
4759 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4760 "refers to this element or any of its children."
4762 "When the @option{--inquire} option is passed then all remaining non-option "
4763 "arguments are retrieved via a server @emph{INQUIRE}."
4766 new_command("GET", 0, 1, 0, get_command
, _(
4767 "GET [--inquire] element[<TAB>child[..]]\n"
4768 "Retrieves the content of the specified element. The content is returned "
4769 "with a data response."
4771 "When the @option{--inquire} option is passed then all remaining non-option "
4772 "arguments are retrieved via a server @emph{INQUIRE}."
4775 new_command("ATTR", 0, 1, 0, attr_command
, _(
4776 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4778 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4779 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4780 "element. When no @var{value} is specified any existing value will be removed."
4782 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4783 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4784 "or @code{target} an error is returned. Use the @command{DELETE} command "
4785 "(@pxref{DELETE}) instead."
4787 "@item ATTR LIST element[<TAB>child[..]]\n"
4788 " Retrieves a newline separated list of attributes names and values "
4789 "from the specified element. Each attribute name and value is space delimited."
4791 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4792 " Retrieves the value of an @var{attribute} from an element."
4795 "When the @option{--inquire} option is passed then all remaining non-option "
4796 "arguments are retrieved via a server @emph{INQUIRE}."
4798 "@xref{Target Attribute}, for details about this special attribute and also "
4799 "@pxref{Other Attributes} for other attributes that are handled specially "
4800 "by @command{pwmd}."
4803 new_command("XPATH", 0, 1, 0, xpath_command
, _(
4804 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4805 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4806 "specified it is assumed the expression is a request to return a result. "
4807 "Otherwise, the result is set to the @var{value} argument and the document is "
4808 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4809 "is assumed to be empty and the document is updated. For example:"
4812 "XPATH //element[@@_name='password']@key{TAB}\n"
4815 "would clear the content of all @var{password} elements in the data file "
4816 "while leaving off the trailing @key{TAB} would return all @var{password} "
4817 "elements in @abbr{XML} format."
4819 "When the @option{--inquire} option is passed then all remaining non-option "
4820 "arguments are retrieved via a server @emph{INQUIRE}."
4822 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4823 "expression syntax."
4826 new_command("XPATHATTR", 0, 1, 0, xpathattr_command
, _(
4827 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4828 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4829 "attributes and does not return a result. For the @var{SET} operation the "
4830 "@var{value} is optional but the field is required. If not specified then "
4831 "the attribute value will be empty. For example:"
4834 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4837 "would create a @var{password} attribute for each @var{password} element "
4838 "found in the document. The attribute value will be empty but still exist."
4840 "When the @option{--inquire} option is passed then all remaining non-option "
4841 "arguments are retrieved via a server @emph{INQUIRE}."
4843 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4844 "expression syntax."
4847 new_command("IMPORT", 0, 1, 0, import_command
, _(
4848 "IMPORT [--root=element[<TAB>child[..]]]\n"
4849 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4851 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4852 "argument is raw @abbr{XML} data. The content is created as a child of "
4853 "the element path specified with the @option{--root} option or at the "
4854 "document root when not specified. Existing elements of the same name will "
4857 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4861 new_command("DUMP", 0, 1, 0, dump_command
, _(
4863 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4864 "dumping a specific node."
4867 new_command("LOCK", 0, 0, 0, lock_command
, _(
4869 "Locks the mutex associated with the opened file. This prevents other clients "
4870 "from sending commands to the same opened file until the client "
4871 "that sent this command either disconnects or sends the @code{UNLOCK} "
4872 "command. @xref{UNLOCK}."
4875 new_command("UNLOCK", 1, 0, 0, unlock_command
, _(
4877 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4878 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4879 "@pxref{ISCACHED})."
4882 new_command("GETCONFIG", 1, 1, 0, getconfig_command
, _(
4883 "GETCONFIG [filename] <parameter>\n"
4884 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4885 "data response. If no file has been opened then the value for @var{filename} "
4886 "or the default from the @var{global} section will be returned. If a file "
4887 "has been opened and no @var{filename} is specified, the value previously "
4888 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4891 new_command("OPTION", 1, 1, 0, option_command
, _(
4892 "OPTION <NAME>=[<VALUE>]\n"
4893 "Sets a client option @var{name} to @var{value}. The value for an option is "
4894 "kept for the duration of the connection with the exception of the "
4895 "@command{pinentry} options which are defaults for all future connections "
4896 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4899 "@item DISABLE-PINENTRY\n"
4900 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4901 "server inquire is sent to the client to obtain the passphrase. This option "
4902 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4903 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4904 "to use a @command{pinentry}."
4907 "Set or unset the X11 display to use when prompting for a passphrase."
4910 "Set the terminal device path to use when prompting for a passphrase."
4913 "Set the terminal type for use with @option{TTYNAME}."
4916 "Associates the thread ID of the connection with the specified textual "
4917 "representation. Useful for debugging log messages. May not contain whitespace."
4919 "@item LOCK-TIMEOUT\n"
4920 "When not @code{0}, the duration in tenths of a second to wait for the file "
4921 "mutex which has been locked by another thread to be released before returning "
4922 "an error. When @code{-1} the error will be returned immediately."
4924 "@item CLIENT-STATE\n"
4925 "When set to @code{1} then client state status messages for other clients are "
4926 "sent to the current client. The default is @code{0}."
4930 new_command("LS", 1, 1, 0, ls_command
, _(
4932 "Returns a newline separated list of data files stored in the data directory "
4933 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4936 new_command("RESET", 1, 1, 0, NULL
, _(
4938 "Closes the currently opened file but keeps any previously set client options "
4942 new_command("NOP", 1, 1, 0, NULL
, _(
4944 "Does nothing. Always returns successfully."
4947 /* !END-HELP-TEXT! */
4948 new_command ("CANCEL", 1, 1, 0, NULL
, NULL
);
4949 new_command ("END", 1, 1, 0, NULL
, NULL
);
4950 new_command ("BYE", 1, 1, 0, NULL
, NULL
);
4953 for (i
= 0; command_table
[i
]; i
++);
4954 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4959 register_commands (assuan_context_t ctx
)
4963 for (; command_table
[i
]; i
++)
4965 if (!command_table
[i
]->handler
)
4968 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4969 command_table
[i
]->handler
,
4970 command_table
[i
]->help
);
4975 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4979 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4983 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4987 return assuan_register_post_cmd_notify (ctx
, command_finalize
);