2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015,
4 Ben Kibbey <bjk@luxsci.net>
6 This file is part of pwmd.
8 Pwmd is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
13 Pwmd is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Pwmd. If not, see <http://www.gnu.org/licenses/>.
30 #include <sys/types.h>
39 #include "pwmd-error.h"
44 #include "util-misc.h"
52 /* These are command option flags. */
53 #define OPT_INQUIRE 0x0001
54 #define OPT_NO_PASSPHRASE 0x0002
55 #define OPT_ASK 0x0004
56 #define OPT_LIST_RECURSE 0x0008
57 #define OPT_VERBOSE 0x0010
58 #define OPT_LOCK 0x0020
59 #define OPT_LOCK_ON_OPEN 0x0040
60 #define OPT_SIGN 0x0080
61 #define OPT_LIST_ALL 0x0100
62 #define OPT_DATA 0x0200
63 #define OPT_NO_AGENT 0x0400
64 #define OPT_SECRET_ONLY 0x0800
65 #define OPT_INQUIRE_KEYID 0x1000
66 #define OPT_INQUIRE_SIGN_KEYID 0x2000
67 #define OPT_SYMMETRIC 0x4000
68 #define OPT_NO_SIGNER 0x8000
69 #define OPT_HTML 0x10000
71 #define FLOCK_TYPE_NONE 0
72 #define FLOCK_TYPE_SH 0x0001
73 #define FLOCK_TYPE_EX 0x0002
74 #define FLOCK_TYPE_KEEP 0x0004
76 struct command_table_s
79 gpg_error_t (*handler
) (assuan_context_t
, char *line
);
82 int unlock
; // unlock the file mutex after validating the checksum
86 static struct command_table_s
**command_table
;
88 static gpg_error_t
do_lock (struct client_s
*client
, int add
);
89 static gpg_error_t
validate_checksum (struct client_s
*,
90 struct cache_data_s
*, unsigned char **,
92 static gpg_error_t
update_checksum (struct client_s
*client
);
94 /* When 'status' is true the 'self' field of the status line will be false
95 * because we never send the STATE status message to the same client that
98 build_client_info_line (struct client_thread_s
*thd
, int status
)
100 int with_state
= config_get_boolean ("global", "send_state");
101 char *uid
, *username
= NULL
;
106 uid
= str_asprintf("#%s", thd
->tls
->fp
);
109 uid
= str_asprintf("%u", thd
->peer
->uid
);
110 username
= get_username (thd
->peer
->uid
);
113 uid
= str_asprintf("%u", thd
->peer
->uid
);
114 username
= get_username (thd
->peer
->uid
);
116 line
= str_asprintf ("%p %s %s %s %u %u %u %s %s %u",
118 thd
->name
? thd
->name
: "-",
119 thd
->cl
&& thd
->cl
->filename
120 && (thd
->cl
->flags
& FLAG_OPEN
)
121 ? thd
->cl
->filename
: "/",
123 thd
->remote
? thd
->peeraddr
: "-",
127 thd
->cl
&& thd
->cl
->flags
& FLAG_HAS_LOCK
? 1 : 0,
128 !status
&& pthread_equal (pthread_self (), thd
->tid
) ? 1 : 0,
129 with_state
? thd
->state
: CLIENT_STATE_UNKNOWN
, uid
,
131 thd
->remote
? "-" : username
,
144 update_client_state (struct client_s
*client
, unsigned s
)
146 MUTEX_LOCK (&cn_mutex
);
147 client
->thd
->state
= s
;
148 MUTEX_UNLOCK (&cn_mutex
);
149 int n
= config_get_boolean ("global", "send_state");
151 if (n
&& client
->thd
->state
!= CLIENT_STATE_UNKNOWN
)
153 char *line
= build_client_info_line (client
->thd
, 1);
156 send_status_all_not_self (n
== 2, STATUS_STATE
, "%s", line
);
163 unlock_file_mutex (struct client_s
*client
, int remove
)
167 // OPEN: keep the lock for the same file being reopened.
168 if (client
->flags
& FLAG_KEEP_LOCK
&& client
->flags
& FLAG_HAS_LOCK
)
171 if (!(client
->flags
& FLAG_HAS_LOCK
))
172 return GPG_ERR_NOT_LOCKED
;
174 rc
= cache_unlock_mutex (client
->md5file
, remove
);
176 rc
= GPG_ERR_INV_STATE
;
178 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_LOCK_CMD
);
184 lock_file_mutex (struct client_s
*client
, int add
)
187 int timeout
= config_get_integer (client
->filename
, "cache_timeout");
189 if (client
->flags
& FLAG_HAS_LOCK
)
192 rc
= cache_lock_mutex (client
->ctx
, client
->md5file
,
193 client
->lock_timeout
, add
, timeout
);
195 client
->flags
|= FLAG_HAS_LOCK
;
201 file_modified (struct client_s
*client
, struct command_table_s
*cmd
)
204 int type
= !cmd
->flock_type
|| (cmd
->flock_type
& FLOCK_TYPE_SH
)
207 if (!(client
->flags
& FLAG_OPEN
))
208 return GPG_ERR_INV_STATE
;
210 rc
= lock_file_mutex (client
, 0);
211 if (rc
&& rc
!= GPG_ERR_NO_DATA
)
214 rc
= lock_flock (client
->ctx
, client
->filename
, type
, &client
->flock_fd
);
217 rc
= validate_checksum (client
, NULL
, NULL
, NULL
);
218 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
221 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
223 else if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
226 /* FLAG_HAS_LOCK may have been set by the LOCK command or OPEN --lock. */
227 if ((cmd
->unlock
&& !(client
->flags
& FLAG_HAS_LOCK
)) || rc
)
228 unlock_file_mutex (client
, 0);
230 if (rc
|| !(cmd
->flock_type
& FLOCK_TYPE_KEEP
))
231 unlock_flock (&client
->flock_fd
);
237 parse_xml (assuan_context_t ctx
, int new)
239 struct client_s
*client
= assuan_get_pointer (ctx
);
240 int cached
= client
->doc
!= NULL
;
245 client
->doc
= xml_new_document ();
251 xmlDocDumpFormatMemory (client
->doc
, &result
, &len
, 0);
252 client
->crypto
->plaintext
= result
;
253 client
->crypto
->plaintext_size
= len
;
254 if (!client
->crypto
->plaintext
)
256 xmlFreeDoc (client
->doc
);
265 rc
= xml_parse_doc ((char *) client
->crypto
->plaintext
,
266 client
->crypto
->plaintext_size
,
267 (xmlDocPtr
*)&client
->doc
);
273 free_client (struct client_s
*client
)
276 xmlFreeDoc (client
->doc
);
279 xfree (client
->filename
);
280 xfree (client
->last_error
);
281 crypto_cleanup (client
->crypto
);
282 client
->crypto
= NULL
;
286 cleanup_client (struct client_s
*client
)
288 assuan_context_t ctx
= client
->ctx
;
289 struct client_thread_s
*thd
= client
->thd
;
290 long lock_timeout
= client
->lock_timeout
;
291 xmlErrorPtr xml_error
= client
->xml_error
;
292 int no_pinentry
= (client
->flags
& FLAG_NO_PINENTRY
);
293 int flock_fd
= client
->flock_fd
;
295 unlock_file_mutex (client
, client
->flags
& FLAG_NEW
);
296 free_client (client
);
297 memset (client
, 0, sizeof (struct client_s
));
298 client
->flock_fd
= flock_fd
;
299 client
->xml_error
= xml_error
;
302 client
->lock_timeout
= lock_timeout
;
303 client
->flags
|= no_pinentry
? FLAG_NO_PINENTRY
: 0;
307 req_cleanup (void *arg
)
312 strv_free ((char **) arg
);
316 parse_open_opt_lock (void *data
, void *value
)
318 struct client_s
*client
= data
;
320 client
->opts
|= OPT_LOCK_ON_OPEN
;
325 parse_opt_inquire (void *data
, void *value
)
327 struct client_s
*client
= data
;
330 client
->opts
|= OPT_INQUIRE
;
335 update_checksum (struct client_s
*client
)
339 struct cache_data_s
*cdata
;
340 gpg_error_t rc
= get_checksum (client
->filename
, &crc
, &len
);
347 cdata
= cache_get_data (client
->md5file
);
351 cdata
->crc
= xmalloc (len
);
352 memcpy (cdata
->crc
, crc
, len
);
359 validate_checksum (struct client_s
*client
, struct cache_data_s
*cdata
,
360 unsigned char **r_crc
, size_t *r_crclen
)
367 if (cdata
&& !cdata
->crc
)
368 return GPG_ERR_CHECKSUM
;
370 rc
= get_checksum (client
->filename
, &crc
, &len
);
375 n
= memcmp (cdata
->crc
, crc
, len
);
376 else if (client
->crc
)
377 n
= memcmp (client
->crc
, crc
, len
);
387 return n
? GPG_ERR_CHECKSUM
: 0;
391 open_command (assuan_context_t ctx
, char *line
)
394 struct client_s
*client
= assuan_get_pointer (ctx
);
395 char **req
, *filename
;
396 unsigned char md5file
[16];
398 assuan_peercred_t peer
;
399 struct cache_data_s
*cdata
= NULL
;
401 unsigned char *crc
= NULL
;
403 struct argv_s
*args
[] = {
404 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_open_opt_lock
},
408 rc
= parse_options (&line
, args
, client
, 1);
410 return send_error (ctx
, rc
);
412 req
= str_split (line
, " ", 2);
414 return send_error (ctx
, GPG_ERR_SYNTAX
);
416 rc
= do_validate_peer (ctx
, req
[0], &peer
);
420 return send_error (ctx
, rc
);
424 if (!valid_filename (filename
))
427 return send_error (ctx
, GPG_ERR_INV_VALUE
);
430 pthread_cleanup_push ((void *)req_cleanup
, req
);
431 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, filename
, strlen (filename
));
432 /* This client may have locked a different file with ISCACHED --lock than
433 * the current filename. This will remove that lock. */
434 same_file
= !memcmp (md5file
, client
->md5file
, 16) ? 1 : 0;
435 if (client
->flags
& FLAG_OPEN
||
436 (client
->flags
& FLAG_HAS_LOCK
&& !same_file
))
438 uint32_t opts
= client
->opts
;
439 uint32_t flags
= client
->flags
;
442 client
->flags
|= FLAG_KEEP_LOCK
;
443 else if (client
->flags
& FLAG_NEW
)
444 cache_clear (client
->md5file
);
446 cleanup_client (client
);
448 client
->flags
|= flags
;
449 client
->flags
&= ~(FLAG_LOCK_CMD
);
451 client
->flags
&= ~(FLAG_HAS_LOCK
| FLAG_NEW
);
454 memcpy (client
->md5file
, md5file
, 16);
455 client
->filename
= str_dup (filename
);
456 if (!client
->filename
)
459 return send_error(ctx
, GPG_ERR_ENOMEM
);
462 /* Need to lock the mutex here because file_modified() cannot without
463 * knowing the filename. */
464 rc
= lock_file_mutex (client
, 1);
466 client
->flags
&= ~FLAG_OPEN
;
470 rc
= lock_flock (ctx
, filename
, LOCK_SH
, &client
->flock_fd
);
471 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
)
479 rc
= crypto_init (&client
->crypto
, client
->ctx
, client
->filename
,
480 client
->flags
& FLAG_NO_PINENTRY
);
481 if (!rc
&& stat (client
->filename
, &st
) == -1)
482 rc
= gpg_error_from_errno (errno
);
483 else if (!rc
&& !S_ISREG (st
.st_mode
)) // to match LS output
484 rc
= gpg_error_from_errno (ENOANO
);
487 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_ENOENT
)
489 cdata
= cache_get_data (client
->md5file
);
494 client
->flags
|= FLAG_NEW
;
495 // data file disappeared. clear the cache entry.
496 cache_clear (client
->md5file
);
499 else if (cdata
&& cdata
->doc
) // cached document
504 if (!rc
&& !(client
->flags
& FLAG_NEW
))
506 rc
= validate_checksum (client
, cdata
, &crc
, &crclen
);
507 if (rc
== GPG_ERR_CHECKSUM
)
516 rc
= cache_iscached (client
->filename
, &defer
);
517 if ((!rc
|| rc
== GPG_ERR_ENOENT
) && defer
)
527 log_write ("%s: %s", client
->filename
,
528 pwmd_strerror (GPG_ERR_CHECKSUM
));
529 cache_clear (client
->md5file
);
531 rc
= crypto_decrypt (client
, client
->crypto
);
536 if (client
->thd
->remote
537 && config_get_boolean (client
->filename
, "tcp_require_key")
538 && !(client
->flags
& FLAG_NEW
))
539 rc
= crypto_try_decrypt (client
,
540 (client
->flags
& FLAG_NO_PINENTRY
));
546 client
->crypto
->plaintext
= cdata
->doc
;
547 client
->crypto
->plaintext_size
= cdata
->size
;
548 rc
= cache_decrypt (client
->crypto
);
553 strv_free (client
->crypto
->pubkey
);
554 strv_free (client
->crypto
->sigkey
);
555 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
556 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
561 client
->crypto
->plaintext
= NULL
;
562 client
->crypto
->plaintext_size
= 0;
566 else // existing file
568 cached
= cdata
!= NULL
;
569 rc
= crypto_decrypt (client
, client
->crypto
);
575 rc
= parse_xml (ctx
, client
->flags
& FLAG_NEW
);
578 rc
= cache_encrypt (client
->crypto
);
580 cache_clear (client
->md5file
);
583 int timeout
= config_get_integer (client
->filename
,
586 free_cache_data_once (cdata
);
587 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
588 cdata
->doc
= client
->crypto
->plaintext
;
589 cdata
->size
= client
->crypto
->plaintext_size
;
590 cdata
->pubkey
= strv_dup(client
->crypto
->pubkey
);
591 cdata
->sigkey
= strv_dup(client
->crypto
->sigkey
);
592 client
->crypto
->plaintext
= NULL
;
593 client
->crypto
->plaintext_size
= 0;
595 if (cached
) // wont increment the refcount
597 /* Prevent using another FD to update the checksum for a
598 * cached data file. The validity has already been
601 client
->crc
= xmalloc (crclen
);
602 memcpy (client
->crc
, crc
, crclen
);
604 cdata
->crc
= xmalloc (crclen
);
605 memcpy (cdata
->crc
, crc
, crclen
);
607 rc
= cache_set_data (client
->md5file
, cdata
);
608 /* The cache entry may have been removed and cache_set_data()
609 * already sent STATUS_CACHE. */
610 if (!cache_iscached (client
->filename
, NULL
))
611 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
614 rc
= cache_add_file (client
->md5file
, cdata
, timeout
);
619 pthread_cleanup_pop (1);
622 if (!rc
&& !(client
->flags
& FLAG_NEW
) && !cached
)
623 rc
= update_checksum (client
);
627 client
->flags
|= FLAG_OPEN
;
629 if (client
->flags
& FLAG_NEW
)
630 rc
= send_status (ctx
, STATUS_NEWFILE
, NULL
);
632 if (!rc
&& (client
->opts
& OPT_LOCK_ON_OPEN
))
633 rc
= do_lock (client
, 0);
638 log_write ("%s: %s", client
->filename
, pwmd_strerror (rc
));
640 if (client
->flags
& FLAG_NEW
)
641 cache_clear (client
->md5file
);
643 crypto_cleanup (client
->crypto
);
644 client
->crypto
= NULL
;
645 client
->flags
&= ~FLAG_OPEN
;
648 crypto_cleanup_non_keys (client
->crypto
);
649 return send_error (ctx
, rc
);
652 /* If not the invoking_user or is an existing file, check that the list of user
653 * supplied key ID's are in the list of current key ID's obtained when
654 * decrypting the data file. Both short and long form keyid fingerprints are
657 * 8 byte with optional "0x" prefix
661 * Although, the key ID's are converted to the 8 byte form before calling this
662 * function in parse_save_opt_keyid_common() to keep consistancy with KEYINFO
666 permitted_to_save (struct client_s
*client
, const char **keys
,
672 if ((client
->flags
& FLAG_NEW
) || (client
->opts
& OPT_SYMMETRIC
))
675 rc
= peer_is_invoker (client
);
680 if ((!value
&& !keys
) || ((value
&& !*value
) && (keys
&& !*keys
)))
683 for (v
= value
; v
&& *v
; v
++)
685 const char **k
, *pv
= *v
;
688 if (*pv
== '0' && *(pv
+1) == 'x')
691 for (k
= keys
; k
&& *k
; k
++)
695 if (*pk
== '0' && *(pk
+1) == 'x')
698 rc
= !strcmp (pv
, pk
) ? 0 : GPG_ERR_FORBIDDEN
;
700 rc
= !strcmp (pv
, *k
) ? 0 : GPG_ERR_FORBIDDEN
;
710 return GPG_ERR_FORBIDDEN
;
717 parse_save_opt_keyid_common (struct client_s
*client
, const char **list
,
718 const char *value
, char ***dst
)
725 p
= str_split (value
, ",", 0);
727 return GPG_ERR_ENOMEM
;
730 crypto_keyid_to_8b (p
);
731 rc
= permitted_to_save (client
, list
, (const char **)p
);
741 parse_save_opt_keyid (void *data
, void *value
)
743 struct client_s
*client
= data
;
744 const char *str
= value
;
748 rc
= parse_save_opt_keyid_common (client
,
749 (const char **)client
->crypto
->pubkey
,
754 client
->crypto
->save
.pubkey
= dst
;
759 parse_save_opt_sign_keyid (void *data
, void *value
)
761 struct client_s
*client
= data
;
762 const char *str
= value
;
766 rc
= parse_save_opt_keyid_common (client
,
767 (const char **)client
->crypto
->sigkey
,
773 client
->opts
|= OPT_NO_SIGNER
;
775 client
->crypto
->save
.sigkey
= dst
;
780 parse_save_opt_inquire (struct client_s
*client
, uint32_t opt
)
782 if (opt
== OPT_INQUIRE
&& !(client
->flags
& FLAG_NEW
))
784 gpg_error_t rc
= peer_is_invoker (client
);
795 parse_save_opt_inquire_keyparam (void *data
, void *value
)
797 return parse_save_opt_inquire (data
, OPT_INQUIRE
);
801 parse_save_opt_inquire_keyid (void *data
, void *value
)
803 return parse_save_opt_inquire (data
, OPT_INQUIRE_KEYID
);
807 parse_save_opt_inquire_sign_keyid (void *data
, void *value
)
809 return parse_save_opt_inquire (data
, OPT_INQUIRE_SIGN_KEYID
);
813 parse_save_opt_symmetric (void *data
, void *value
)
815 struct client_s
*client
= data
;
817 client
->opts
|= OPT_SYMMETRIC
;
821 /* Tests that the keys in new_keys are also in old_keys. */
823 compare_keys (char **new_keys
, char **old_keys
)
827 if (!old_keys
|| !*old_keys
)
830 crypto_keyid_to_8b (new_keys
);
832 for (o
= old_keys
; *o
; o
++)
836 for (n
= new_keys
; *n
; n
++)
838 if (!strcmp (*n
, *o
))
843 return GPG_ERR_NOT_FOUND
;
850 inquire_keyid (struct client_s
*client
, uint32_t opt
)
853 unsigned char *result
= NULL
;
859 if (opt
== OPT_INQUIRE_KEYID
)
862 orig
= &client
->crypto
->pubkey
;
863 save
= &client
->crypto
->save
.pubkey
;
867 s
= "INQUIRE_SIGN_KEYID";
868 orig
= &client
->crypto
->sigkey
;
869 save
= &client
->crypto
->save
.sigkey
;
872 rc
= assuan_inquire (client
->ctx
, s
, &result
, &len
, 0);
877 rc
= parse_save_opt_keyid_common (client
, (const char **)*orig
,
878 (char *)result
, &dst
);
881 if (!dst
&& opt
== OPT_INQUIRE_SIGN_KEYID
882 && client
->opts
& OPT_SYMMETRIC
)
883 client
->opts
|= OPT_NO_SIGNER
;
894 save_command (assuan_context_t ctx
, char *line
)
896 struct client_s
*client
= assuan_get_pointer (ctx
);
897 struct cache_data_s
*cdata
= NULL
;
898 gpg_error_t rc
= 0, cached
= 0;
901 struct argv_s
*args
[] = {
902 &(struct argv_s
) {"keyid", OPTION_TYPE_ARG
, parse_save_opt_keyid
},
903 &(struct argv_s
) {"sign-keyid", OPTION_TYPE_OPTARG
,
904 parse_save_opt_sign_keyid
},
905 &(struct argv_s
) {"inquire-keyparam", OPTION_TYPE_NOARG
,
906 parse_save_opt_inquire_keyparam
},
907 &(struct argv_s
) {"inquire-keyid", OPTION_TYPE_NOARG
,
908 parse_save_opt_inquire_keyid
},
909 &(struct argv_s
) {"inquire-sign-keyid", OPTION_TYPE_NOARG
,
910 parse_save_opt_inquire_sign_keyid
},
911 &(struct argv_s
) {"symmetric", OPTION_TYPE_NOARG
,
912 parse_save_opt_symmetric
},
916 crypto_cleanup_save (&client
->crypto
->save
);
918 if (!(client
->flags
& FLAG_NEW
))
920 rc
= crypto_is_symmetric (client
->filename
);
921 if (!rc
|| rc
== GPG_ERR_BAD_DATA
)
924 client
->opts
|= OPT_SYMMETRIC
;
931 return send_error (ctx
, rc
);
933 rc
= parse_options (&line
, args
, client
, 0);
935 return send_error (ctx
, rc
);
937 if (config_get_boolean (client
->filename
, "require_save_key"))
938 client
->opts
|= OPT_ASK
;
940 if (lstat (client
->filename
, &st
) == -1 && errno
!= ENOENT
)
941 return send_error (ctx
, gpg_error_from_errno (errno
));
943 if (errno
!= ENOENT
&& !S_ISREG (st
.st_mode
))
945 log_write ("%s: %s", client
->filename
, pwmd_strerror (GPG_ERR_ENOANO
));
946 return send_error (ctx
, GPG_ERR_ENOANO
);
950 cached
= rc
= cache_iscached (client
->filename
, &defer
);
952 if (gpg_err_code (rc
) == GPG_ERR_ENOENT
&& (client
->flags
& FLAG_NEW
))
954 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
958 return send_error (ctx
, rc
);
960 /* Specifying both a recipient and symmetric encryption is an error. */
961 if (client
->opts
& OPT_INQUIRE_KEYID
&& client
->opts
& OPT_SYMMETRIC
)
963 return send_error (ctx
, GPG_ERR_CONFLICT
);
965 else if (client
->opts
& OPT_INQUIRE
&& client
->opts
& OPT_SYMMETRIC
)
967 return send_error (ctx
, GPG_ERR_CONFLICT
);
969 /* Existing file with a recipient and wanting symmetric is an error. */
970 else if (client
->opts
& OPT_SYMMETRIC
&& client
->crypto
->save
.pubkey
)
972 return send_error (ctx
, GPG_ERR_CONFLICT
);
974 else if (((client
->opts
& OPT_INQUIRE_KEYID
)
975 || (client
->opts
& OPT_INQUIRE_SIGN_KEYID
)))
977 if (client
->opts
& OPT_INQUIRE
)
978 return send_error (ctx
, GPG_ERR_CONFLICT
);
980 if (client
->opts
& OPT_INQUIRE_KEYID
)
981 rc
= inquire_keyid (client
, OPT_INQUIRE_KEYID
);
983 if (!rc
&& (client
->opts
& OPT_INQUIRE_SIGN_KEYID
))
984 rc
= inquire_keyid (client
, OPT_INQUIRE_SIGN_KEYID
);
986 else if ((client
->crypto
->save
.pubkey
|| client
->crypto
->save
.sigkey
)
987 && client
->opts
& OPT_INQUIRE
)
988 return send_error (ctx
, GPG_ERR_CONFLICT
);
991 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
);
993 if (!rc
&& (client
->opts
& OPT_INQUIRE
))
995 /* Require a passphrase when generating a new key pair for an existing
998 if (!(client
->flags
& FLAG_NEW
))
1000 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1001 client
->opts
&= ~OPT_ASK
;
1006 unsigned char *params
= NULL
;
1009 rc
= assuan_inquire (client
->ctx
, "KEYPARAM", ¶ms
, &len
, 0);
1012 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1013 pthread_cleanup_push ((void *)xfree
, params
);
1014 rc
= crypto_genkey (client
, client
->crypto
, params
);
1015 pthread_cleanup_pop (1);
1019 else if (!rc
&& (client
->flags
& FLAG_NEW
))
1021 if (!client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
1022 && !(client
->opts
& OPT_SYMMETRIC
))
1024 char *params
= crypto_default_key_params ();
1027 rc
= GPG_ERR_ENOMEM
;
1031 client
->crypto
->flags
|= CRYPTO_FLAG_NEWFILE
;
1032 pthread_cleanup_push ((void *)xfree
, params
);
1033 rc
= crypto_genkey (client
, client
->crypto
,
1034 (unsigned char *)params
);
1035 pthread_cleanup_pop (1);
1040 if (!client
->crypto
->save
.pubkey
&& client
->crypto
->save
.sigkey
1041 && !(client
->opts
& OPT_SYMMETRIC
))
1042 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->save
.sigkey
);
1044 if (client
->crypto
->save
.pubkey
&& !client
->crypto
->save
.sigkey
)
1045 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->save
.pubkey
);
1050 cdata
= cache_get_data (client
->md5file
);
1053 if (client
->crypto
->save
.pubkey
)
1054 rc
= compare_keys (client
->crypto
->save
.pubkey
, cdata
->pubkey
);
1056 /* Always allow a signer for symmetric data files. */
1057 if (!rc
&& client
->crypto
->save
.sigkey
1058 && !(client
->opts
& OPT_SYMMETRIC
))
1059 rc
= compare_keys (client
->crypto
->save
.sigkey
, cdata
->sigkey
);
1061 /* Prevent saving to a recipient who is not in the original recipient
1062 * list without a passphrase. */
1063 if (rc
|| (client
->crypto
->sigkey
&& client
->opts
& OPT_NO_SIGNER
))
1064 client
->opts
|= OPT_ASK
;
1066 if (rc
== GPG_ERR_NOT_FOUND
)
1068 rc
= peer_is_invoker (client
);
1069 if (rc
== GPG_ERR_EACCES
)
1070 rc
= GPG_ERR_FORBIDDEN
;
1073 if (!client
->crypto
->save
.pubkey
1074 && !(client
->opts
& OPT_SYMMETRIC
))
1075 client
->crypto
->save
.pubkey
= strv_dup (cdata
->pubkey
);
1077 if (!client
->crypto
->save
.sigkey
&& cdata
->sigkey
1078 && !(client
->opts
& OPT_NO_SIGNER
))
1079 client
->crypto
->save
.sigkey
= strv_dup (cdata
->sigkey
);
1080 else if (!client
->crypto
->save
.sigkey
1081 && (client
->opts
& OPT_NO_SIGNER
)
1082 && !(client
->opts
& OPT_SYMMETRIC
))
1083 rc
= GPG_ERR_NO_SIGNATURE_SCHEME
;
1087 client
->crypto
->save
.pubkey
= strv_dup (client
->crypto
->pubkey
);
1088 client
->crypto
->save
.sigkey
= strv_dup (client
->crypto
->sigkey
);
1092 if (!rc
&& !(client
->flags
& FLAG_NEW
))
1094 /* Fixes {NEW_,SIGN_}PASSPHRASE inquire keywords. See passphrase_cb(). */
1095 if (client
->opts
& OPT_SYMMETRIC
)
1096 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD
;
1098 if (client
->opts
& OPT_ASK
)
1099 rc
= crypto_try_decrypt (client
, client
->flags
& FLAG_NO_PINENTRY
);
1102 if (!rc
&& client
->opts
& OPT_SYMMETRIC
)
1103 client
->crypto
->flags
|= CRYPTO_FLAG_PASSWD_NEW
;
1106 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
));
1112 xmlDocDumpFormatMemory (client
->doc
, &client
->crypto
->plaintext
, &size
,
1115 client
->crypto
->plaintext_size
= (size_t) size
;
1117 rc
= GPG_ERR_ENOMEM
;
1121 client
->crypto
->flags
|= (client
->opts
& OPT_SYMMETRIC
) ? CRYPTO_FLAG_SYMMETRIC
: 0;
1122 client
->crypto
->flags
|= (client
->flags
& FLAG_NEW
) ? CRYPTO_FLAG_NEWFILE
: 0;
1123 rc
= crypto_encrypt (client
, client
->crypto
);
1124 client
->crypto
->flags
&= ~CRYPTO_FLAG_SYMMETRIC
;
1129 rc
= crypto_write_file (client
->crypto
);
1132 if (!cached
) // no error
1133 cdata
= cache_get_data (client
->md5file
);
1138 rc
= cache_encrypt (client
->crypto
);
1141 cache_clear (client
->md5file
);
1142 strv_free (client
->crypto
->pubkey
);
1143 client
->crypto
->pubkey
= strv_dup (client
->crypto
->save
.pubkey
);
1144 strv_free (client
->crypto
->sigkey
);
1145 client
->crypto
->sigkey
= strv_dup (client
->crypto
->save
.sigkey
);
1146 client
->flags
&= ~(FLAG_NEW
);
1152 int timeout
= config_get_integer (client
->filename
,
1155 free_cache_data_once (cdata
);
1156 cdata
= xcalloc (1, sizeof (struct cache_data_s
));
1157 cdata
->doc
= client
->crypto
->plaintext
;
1158 client
->crypto
->plaintext
= NULL
;
1159 cdata
->size
= client
->crypto
->plaintext_size
;
1160 client
->crypto
->plaintext_size
= 0;
1161 cdata
->pubkey
= client
->crypto
->save
.pubkey
;
1162 client
->crypto
->save
.pubkey
= NULL
;
1163 cdata
->sigkey
= client
->crypto
->save
.sigkey
;
1164 client
->crypto
->save
.sigkey
= NULL
;
1166 /* Update in case the cache entry expires the next SAVE may not
1167 * have any known keys. */
1168 strv_free (client
->crypto
->pubkey
);
1169 client
->crypto
->pubkey
= strv_dup (cdata
->pubkey
);
1170 strv_free (client
->crypto
->sigkey
);
1171 client
->crypto
->sigkey
= strv_dup (cdata
->sigkey
);
1173 if (!cached
) // no error and wont increment refcount
1174 rc
= cache_set_data (client
->md5file
, cdata
);
1176 rc
= cache_add_file (client
->md5file
, cdata
, timeout
);
1178 if (!rc
&& (cached
|| (client
->flags
& FLAG_NEW
)))
1179 send_status_all (STATUS_CACHE
, NULL
);
1183 rc
= update_checksum (client
);
1184 client
->flags
&= ~(FLAG_NEW
);
1190 crypto_cleanup_non_keys (client
->crypto
);
1191 return send_error (ctx
, rc
);
1195 do_delete (assuan_context_t ctx
, char *line
)
1197 struct client_s
*client
= assuan_get_pointer (ctx
);
1198 struct xml_request_s
*req
;
1202 rc
= xml_new_request (client
, line
, XML_CMD_DELETE
, &req
);
1206 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1207 xml_free_request (req
);
1211 rc
= xml_is_element_owner (client
, n
);
1213 rc
= xml_unlink_node (client
, n
);
1219 delete_command (assuan_context_t ctx
, char *line
)
1221 struct client_s
*client
= assuan_get_pointer (ctx
);
1223 struct argv_s
*args
[] = {
1224 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1228 rc
= parse_options (&line
, args
, client
, 1);
1229 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1230 rc
= GPG_ERR_SYNTAX
;
1232 return send_error (ctx
, rc
);
1234 if (client
->opts
& OPT_INQUIRE
)
1236 unsigned char *result
;
1239 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1241 return send_error (ctx
, rc
);
1243 pthread_cleanup_push ((void *)xfree
, result
);
1244 rc
= do_delete (ctx
, (char *)result
);
1245 pthread_cleanup_pop (1);
1248 rc
= do_delete (ctx
, line
);
1250 return send_error (ctx
, rc
);
1254 update_element_expirey (struct client_s
*client
, xmlNodePtr n
)
1257 xmlChar
*expire
, *incr
;
1259 time_t e
, e_orig
, i
;
1260 time_t now
= time (NULL
);
1262 expire
= xml_attribute_value (n
, (xmlChar
*)"expire");
1267 e
= strtoul ((char *)expire
, &p
, 10);
1268 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1271 rc
= GPG_ERR_ERANGE
;
1276 incr
= xml_attribute_value (n
, (xmlChar
*)"expire_increment");
1280 i
= strtoul ((char *)incr
, &p
, 10);
1281 if (errno
|| *p
|| i
== ULONG_MAX
|| *incr
== '-')
1284 rc
= GPG_ERR_ERANGE
;
1291 p
= str_asprintf ("%lu", e
);
1294 rc
= GPG_ERR_ENOMEM
;
1298 rc
= xml_add_attribute (client
, n
, "expire", p
);
1301 rc
= send_status (client
->ctx
, STATUS_EXPIRE
, "%lu %lu", e_orig
, e
);
1308 store_command (assuan_context_t ctx
, char *line
)
1310 struct client_s
*client
= assuan_get_pointer (ctx
);
1313 unsigned char *result
;
1314 xmlNodePtr n
, parent
;
1316 char *content
= NULL
;
1317 struct xml_request_s
*req
;
1320 return send_error (ctx
, GPG_ERR_SYNTAX
);
1322 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1324 return send_error (ctx
, rc
);
1326 rc
= xml_new_request (client
, (char *)result
, XML_CMD_STORE
, &req
);
1329 return send_error (ctx
, rc
);
1331 /* Prevent passing the element content around to save some memory. */
1332 len
= strv_length (req
->args
);
1333 has_content
= line
[strlen (line
) - 1] != '\t' && len
> 1;
1334 if (*(req
->args
+1) && !xml_valid_element_path (req
->args
, has_content
))
1336 xml_free_request (req
);
1337 return send_error (ctx
, GPG_ERR_INV_VALUE
);
1340 if (has_content
|| !*req
->args
[len
-1])
1343 content
= req
->args
[len
-1];
1344 req
->args
[len
-1] = NULL
;
1347 if (strv_length (req
->args
) > 1)
1349 rc
= xml_check_recursion (client
, req
, has_content
);
1350 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
1354 parent
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1358 rc
= xml_is_element_owner (client
, parent
);
1361 n
= xml_find_text_node (parent
->children
);
1363 xmlNodeSetContent (n
, (xmlChar
*) content
);
1365 xmlNodeAddContent (parent
, (xmlChar
*) content
);
1367 (void)xml_update_element_mtime (client
, parent
);
1368 (void)update_element_expirey (client
, parent
);
1374 xml_free_request (req
);
1375 return send_error (ctx
, rc
);
1379 xfer_data (assuan_context_t ctx
, const char *line
, int total
)
1381 struct client_s
*client
= assuan_get_pointer (ctx
);
1385 int progress
= config_get_integer ("global", "xfer_progress");
1388 if (!(client
->flags
& FLAG_LOCK_CMD
))
1389 rc
= unlock_file_mutex (client
, 0);
1390 if (rc
&& rc
!= GPG_ERR_NOT_LOCKED
)
1394 progress
> 0 ? (progress
/ ASSUAN_LINELENGTH
) * ASSUAN_LINELENGTH
: 0;
1395 to_send
= total
< ASSUAN_LINELENGTH
? total
: ASSUAN_LINELENGTH
;
1396 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1404 if (sent
+ to_send
> total
)
1405 to_send
= total
- sent
;
1407 rc
= assuan_send_data (ctx
, flush
? NULL
: (char *) line
+ sent
,
1408 flush
? 0 : to_send
);
1411 sent
+= flush
? 0 : to_send
;
1413 if ((progress
&& !(sent
% progress
) && sent
!= total
) ||
1414 (sent
== total
&& flush
))
1415 rc
= send_status (ctx
, STATUS_XFER
, "%li %li", sent
, total
);
1417 if (!flush
&& !rc
&& sent
== total
)
1424 while (!rc
&& sent
< total
);
1430 do_get (assuan_context_t ctx
, char *line
)
1432 struct client_s
*client
= assuan_get_pointer (ctx
);
1434 struct xml_request_s
*req
;
1437 rc
= xml_new_request (client
, line
, XML_CMD_NONE
, &req
);
1441 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1444 if (n
&& n
->children
)
1448 n
= xml_find_text_node (n
->children
);
1449 if (!n
|| !n
->content
|| !*n
->content
)
1450 rc
= GPG_ERR_NO_DATA
;
1453 rc
= xfer_data (ctx
, (char *) n
->content
, xmlStrlen (n
->content
));
1456 xmlChar
*expire
= xml_attribute_value (tmp
,
1457 (xmlChar
*)"expire");
1460 time_t now
= time (NULL
);
1465 e
= strtoul ((char *)expire
, &p
, 10);
1466 if (errno
|| *p
|| e
== ULONG_MAX
|| *expire
== '-')
1467 log_write (_("invalid expire attribute value: %s"),
1470 rc
= send_status (ctx
, STATUS_EXPIRE
, "%lu 0", e
);
1478 rc
= GPG_ERR_NO_DATA
;
1481 xml_free_request (req
);
1486 get_command (assuan_context_t ctx
, char *line
)
1488 struct client_s
*client
= assuan_get_pointer (ctx
);
1490 struct argv_s
*args
[] = {
1491 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1495 rc
= parse_options (&line
, args
, client
, 1);
1496 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1497 rc
= GPG_ERR_SYNTAX
;
1499 return send_error (ctx
, rc
);
1501 if (client
->opts
& OPT_INQUIRE
)
1503 unsigned char *result
;
1506 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1508 return send_error (ctx
, rc
);
1510 pthread_cleanup_push ((void *)xfree
, result
);
1511 rc
= do_get (ctx
, (char *)result
);
1512 pthread_cleanup_pop (1);
1515 rc
= do_get (ctx
, line
);
1517 return send_error (ctx
, rc
);
1520 static void list_command_cleanup1 (void *arg
);
1522 realpath_command (assuan_context_t ctx
, char *line
)
1525 char **realpath
= NULL
;
1526 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 (void)xml_resolve_path (client
, client
->doc
, result
, &realpath
, &rc
);
1549 pthread_cleanup_pop (1);
1552 (void)xml_resolve_path (client
, client
->doc
, (unsigned char *)line
,
1557 char *tmp
= strv_join ("\t", realpath
);
1559 strv_free (realpath
);
1561 return send_error (ctx
, GPG_ERR_ENOMEM
);
1563 pthread_cleanup_push ((void *)xfree
, tmp
);
1564 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
1565 pthread_cleanup_pop (1);
1568 return send_error (ctx
, rc
);
1572 list_command_cleanup1 (void *arg
)
1575 string_free ((struct string_s
*) arg
, 1);
1579 parse_list_opt_recurse (void *data
, void *value
)
1581 struct client_s
*client
= data
;
1583 client
->opts
|= OPT_LIST_RECURSE
;
1588 parse_opt_verbose (void *data
, void *value
)
1590 struct client_s
*client
= data
;
1592 client
->opts
|= OPT_VERBOSE
;
1597 list_path_once (struct client_s
*client
, char *line
,
1598 struct element_list_s
*elements
, struct string_s
*result
)
1602 rc
= xml_create_path_list (client
, client
->doc
, NULL
, elements
, line
, 0);
1603 if (rc
== GPG_ERR_ELOOP
|| rc
== GPG_ERR_EACCES
)
1608 int total
= slist_length (elements
->list
);
1614 for (i
= 0; i
< total
; i
++)
1616 char *tmp
= slist_nth_data (elements
->list
, i
);
1618 string_append_printf (result
, "%s%s", tmp
,
1619 i
+ 1 == total
? "" : "\n");
1623 rc
= GPG_ERR_NO_DATA
;
1630 do_list (assuan_context_t ctx
, char *line
)
1632 struct client_s
*client
= assuan_get_pointer (ctx
);
1633 gpg_error_t rc
= GPG_ERR_NO_DATA
;
1634 struct element_list_s
*elements
= NULL
;
1636 if (!line
|| !*line
)
1638 struct string_s
*str
= string_new (NULL
);
1642 return GPG_ERR_ENOMEM
;
1644 pthread_cleanup_push ((void *)list_command_cleanup1
, str
);
1645 n
= xmlDocGetRootElement (client
->doc
);
1646 for (n
= n
->children
; n
; n
= n
->next
)
1650 if (n
->type
!= XML_ELEMENT_NODE
)
1653 name
= xmlGetProp (n
, (xmlChar
*)"_name");
1656 rc
= GPG_ERR_ENOMEM
;
1660 elements
= xcalloc (1, sizeof (struct element_list_s
));
1664 rc
= GPG_ERR_ENOMEM
;
1668 elements
->prefix
= string_new (NULL
);
1669 if (!elements
->prefix
)
1672 xml_free_element_list (elements
);
1673 rc
= GPG_ERR_ENOMEM
;
1677 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1678 elements
->root_only
= !elements
->recurse
;
1679 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1680 rc
= list_path_once (client
, (char *)name
, elements
, str
);
1682 pthread_cleanup_pop (1);
1687 string_append (str
, "\n");
1691 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1693 pthread_cleanup_pop (1);
1697 elements
= xcalloc (1, sizeof (struct element_list_s
));
1699 return GPG_ERR_ENOMEM
;
1701 elements
->prefix
= string_new (NULL
);
1702 if (!elements
->prefix
)
1704 xml_free_element_list (elements
);
1705 return GPG_ERR_ENOMEM
;
1708 elements
->recurse
= client
->opts
& OPT_LIST_RECURSE
;
1709 pthread_cleanup_push ((void *)xml_free_element_list
, elements
);
1710 struct string_s
*str
= string_new (NULL
);
1711 pthread_cleanup_push ((void *)list_command_cleanup1
, str
);
1712 rc
= list_path_once (client
, line
, elements
, str
);
1714 rc
= xfer_data (ctx
, str
->str
, str
->len
);
1716 pthread_cleanup_pop (1);
1717 pthread_cleanup_pop (1);
1722 list_command (assuan_context_t ctx
, char *line
)
1724 struct client_s
*client
= assuan_get_pointer (ctx
);
1726 struct argv_s
*args
[] = {
1727 &(struct argv_s
) {"recurse", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1728 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
1729 /* These are deprecated but kept for backward compatibility with older
1731 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, NULL
},
1732 &(struct argv_s
) {"with-target", OPTION_TYPE_NOARG
, NULL
},
1733 &(struct argv_s
) {"all", OPTION_TYPE_NOARG
, parse_list_opt_recurse
},
1734 &(struct argv_s
) {"no-recurse", OPTION_TYPE_NOARG
, NULL
},
1738 if (disable_list_and_dump
== 1)
1739 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
1741 rc
= parse_options (&line
, args
, client
, 1);
1742 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
1743 rc
= GPG_ERR_SYNTAX
;
1745 return send_error (ctx
, rc
);
1747 if (client
->opts
& OPT_INQUIRE
)
1749 unsigned char *result
;
1752 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
1754 return send_error (ctx
, rc
);
1756 pthread_cleanup_push ((void *)xfree
, result
);
1757 rc
= do_list (ctx
, (char *)result
);
1758 pthread_cleanup_pop (1);
1761 rc
= do_list (ctx
, line
);
1763 return send_error (ctx
, rc
);
1767 * args[0] - element path
1770 attribute_list (assuan_context_t ctx
, char **args
)
1772 struct client_s
*client
= assuan_get_pointer (ctx
);
1773 char **attrlist
= NULL
;
1777 xmlNodePtr n
, an
, r
;
1780 struct xml_request_s
*req
;
1782 if (!args
|| !args
[0] || !*args
[0])
1783 return GPG_ERR_SYNTAX
;
1785 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_LIST
, &req
);
1789 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1790 xml_free_request (req
);
1795 for (a
= n
->properties
; a
; a
= a
->next
)
1799 if (target
&& xml_reserved_attribute ((char *)a
->name
))
1802 pa
= xrealloc (attrlist
, (i
+ 2) * sizeof (char *));
1806 strv_free (attrlist
);
1808 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1809 pwmd_strerror (GPG_ERR_ENOMEM
));
1810 return GPG_ERR_ENOMEM
;
1815 attrlist
[i
] = str_asprintf ("%s %s", (char *) a
->name
, an
&& an
->content
1816 ? (char *)an
->content
: "");
1820 strv_free (attrlist
);
1821 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1822 pwmd_strerror (GPG_ERR_ENOMEM
));
1823 return GPG_ERR_ENOMEM
;
1826 attrlist
[++i
] = NULL
;
1830 return GPG_ERR_NO_DATA
;
1834 r
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[0], NULL
, &rc
);
1835 if (rc
&& rc
!= GPG_ERR_EACCES
)
1837 strv_free (attrlist
);
1840 else if (!rc
&& r
!= n
)
1850 line
= strv_join ("\n", attrlist
);
1853 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
1854 pwmd_strerror (GPG_ERR_ENOMEM
));
1855 strv_free (attrlist
);
1856 return GPG_ERR_ENOMEM
;
1859 pthread_cleanup_push ((void *)xfree
, line
);
1860 pthread_cleanup_push ((void *)req_cleanup
, attrlist
);
1861 rc
= xfer_data (ctx
, line
, strlen (line
));
1862 pthread_cleanup_pop (1);
1863 pthread_cleanup_pop (1);
1868 * args[0] - attribute
1869 * args[1] - element path
1872 attribute_delete (struct client_s
*client
, char **args
)
1877 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
1878 return GPG_ERR_SYNTAX
;
1880 if (!strcmp (args
[0], "_name") || !strcmp (args
[0], "target"))
1881 return GPG_ERR_INV_ATTR
;
1883 if (!xml_reserved_attribute (args
[0]))
1884 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
1887 struct xml_request_s
*req
;
1889 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
1893 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1894 xml_free_request (req
);
1898 rc
= xml_is_element_owner (client
, n
);
1901 rc
= xml_delete_attribute (client
, n
, (xmlChar
*) args
[0]);
1907 * Creates the "target" attribute. When other commands encounter an element
1908 * with this attribute they follow the "target" after appending remaining
1909 * elements from the original element path to the target. The exception is the
1910 * ATTR command that operates on the element itself (for reserved attribute
1911 * names) and not the target.
1913 * If the source element path doesn't exist when using 'ATTR SET target', it is
1914 * created, but the destination element path must exist. This is simliar to the
1915 * ls(1) command: ln -s src dst.
1917 * args[0] - source element path
1918 * args[1] - destination element path
1921 set_target_attribute (struct client_s
*client
, char **args
)
1923 struct xml_request_s
*req
= NULL
;
1928 if (!args
|| !args
[0] || !args
[1])
1929 return GPG_ERR_SYNTAX
;
1931 src
= str_split (args
[0], "\t", 0);
1933 return GPG_ERR_SYNTAX
;
1935 if (!xml_valid_element_path (src
, 0))
1938 return GPG_ERR_INV_VALUE
;
1941 dst
= str_split (args
[1], "\t", 0);
1945 return GPG_ERR_SYNTAX
;
1948 // Be sure the destination element path exists. */
1949 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
1953 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1954 if (rc
&& rc
!= GPG_ERR_EACCES
)
1957 xml_free_request (req
);
1960 if (!strcmp (args
[0], args
[1]))
1962 rc
= GPG_ERR_EEXIST
;
1966 rc
= xml_new_request (client
, args
[0], XML_CMD_ATTR_TARGET
, &req
);
1970 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
1971 if (rc
&& rc
!= GPG_ERR_EACCES
)
1976 rc
= xml_add_attribute (client
, n
, "target", args
[1]);
1978 rc
= xml_unlink_node (client
, n
->children
);
1984 xml_free_request (req
);
1989 * args[0] - attribute
1990 * args[1] - element path
1993 attribute_get (assuan_context_t ctx
, char **args
)
1995 struct client_s
*client
= assuan_get_pointer (ctx
);
1999 struct xml_request_s
*req
;
2001 if (!args
|| !args
[0] || !*args
[0] || !args
[1] || !*args
[1])
2002 return GPG_ERR_SYNTAX
;
2004 if (!xml_reserved_attribute (args
[0]))
2005 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2008 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2012 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2013 xml_free_request (req
);
2019 a
= xmlGetProp (n
, (xmlChar
*) args
[0]);
2021 return GPG_ERR_NOT_FOUND
;
2023 pthread_cleanup_push ((void *)xmlFree
, a
);
2026 rc
= xfer_data (ctx
, (char *) a
, xmlStrlen (a
));
2028 rc
= GPG_ERR_NO_DATA
;
2030 pthread_cleanup_pop (1);
2035 * args[0] - attribute
2036 * args[1] - element path
2040 attribute_set (struct client_s
*client
, char **args
)
2042 struct xml_request_s
*req
;
2046 if (!args
|| !args
[0] || !args
[1])
2047 return GPG_ERR_SYNTAX
;
2050 * Reserved attribute names.
2052 if (!strcmp (args
[0], "_name")) // Use the RENAME command instead
2053 return GPG_ERR_INV_ATTR
;
2054 else if (!strcmp (args
[0], "target"))
2055 return set_target_attribute (client
, args
+ 1);
2056 else if (!xml_valid_attribute (args
[0]))
2057 return GPG_ERR_INV_VALUE
;
2059 if (!xml_valid_attribute_value (args
[2]))
2060 return GPG_ERR_INV_VALUE
;
2062 if (!xml_reserved_attribute (args
[0]))
2063 n
= xml_resolve_path (client
, client
->doc
, (xmlChar
*)args
[1], NULL
, &rc
);
2066 rc
= xml_new_request (client
, args
[1], XML_CMD_ATTR
, &req
);
2070 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
2071 xml_free_request (req
);
2075 rc
= xml_is_element_owner (client
, n
);
2078 rc
= xml_add_attribute (client
, n
, args
[0], args
[2]);
2085 * req[1] - attribute name or element path if command is LIST
2086 * req[2] - element path
2087 * req[2] - element path or value
2091 do_attr (assuan_context_t ctx
, char *line
)
2093 struct client_s
*client
= assuan_get_pointer (ctx
);
2097 req
= str_split (line
, " ", 4);
2098 if (!req
|| !req
[0] || !req
[1])
2101 return GPG_ERR_SYNTAX
;
2104 pthread_cleanup_push ((void *)req_cleanup
, req
);
2106 if (strcasecmp (req
[0], "SET") == 0)
2107 rc
= attribute_set (client
, req
+ 1);
2108 else if (strcasecmp (req
[0], "GET") == 0)
2109 rc
= attribute_get (ctx
, req
+ 1);
2110 else if (strcasecmp (req
[0], "DELETE") == 0)
2111 rc
= attribute_delete (client
, req
+ 1);
2112 else if (strcasecmp (req
[0], "LIST") == 0)
2113 rc
= attribute_list (ctx
, req
+ 1);
2115 rc
= GPG_ERR_SYNTAX
;
2117 client
->flags
&= ~(FLAG_ACL_IGNORE
|FLAG_ACL_ERROR
);
2118 pthread_cleanup_pop (1);
2123 attr_command (assuan_context_t ctx
, char *line
)
2125 struct client_s
*client
= assuan_get_pointer (ctx
);
2127 struct argv_s
*args
[] = {
2128 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2132 rc
= parse_options (&line
, args
, client
, 1);
2133 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2134 rc
= GPG_ERR_SYNTAX
;
2136 return send_error (ctx
, rc
);
2138 if (client
->opts
& OPT_INQUIRE
)
2140 unsigned char *result
;
2143 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2145 return send_error (ctx
, rc
);
2147 pthread_cleanup_push ((void *)xfree
, result
);
2148 rc
= do_attr (ctx
, (char *)result
);
2149 pthread_cleanup_pop (1);
2152 rc
= do_attr (ctx
, line
);
2154 return send_error (ctx
, rc
);
2158 parse_iscached_opt_lock (void *data
, void *value
)
2160 struct client_s
*client
= data
;
2163 client
->opts
|= OPT_LOCK
;
2168 iscached_command (assuan_context_t ctx
, char *line
)
2170 struct client_s
*client
= assuan_get_pointer (ctx
);
2172 struct argv_s
*args
[] = {
2173 &(struct argv_s
) {"lock", OPTION_TYPE_NOARG
, parse_iscached_opt_lock
},
2177 if (!line
|| !*line
)
2178 return send_error (ctx
, GPG_ERR_SYNTAX
);
2180 rc
= parse_options (&line
, args
, client
, 1);
2182 return send_error (ctx
, rc
);
2183 else if (!valid_filename (line
))
2184 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2186 rc
= cache_iscached (line
, NULL
);
2187 if (client
->opts
& OPT_LOCK
2188 && (!rc
|| gpg_err_code (rc
) == GPG_ERR_NO_DATA
))
2190 unsigned char md5file
[16];
2191 gpg_error_t trc
= rc
;
2193 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2194 if (memcmp (md5file
, client
->md5file
, 16))
2195 cleanup_client (client
);
2197 memcpy (client
->md5file
, md5file
, 16);
2198 rc
= do_lock (client
, 1);
2203 return send_error (ctx
, rc
);
2207 clearcache_command (assuan_context_t ctx
, char *line
)
2209 gpg_error_t rc
= 0, all_rc
= 0;
2210 unsigned char md5file
[16];
2214 struct client_thread_s
*once
= NULL
;
2217 MUTEX_LOCK (&cn_mutex
);
2218 pthread_cleanup_push ((void *)cleanup_mutex_cb
, &cn_mutex
);
2220 if (!line
|| !*line
)
2223 t
= slist_length (cn_thread_list
);
2225 for (i
= 0; i
< t
; i
++)
2227 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
2228 assuan_peercred_t peer
;
2233 /* Lock each connected clients' file mutex to prevent any other client
2234 * from accessing the cache entry (the file mutex is locked upon
2235 * command startup). The cache for the entry is not cleared if the
2236 * file mutex is locked by another client to prevent this function
2241 if (thd
->cl
->filename
)
2243 rc
= do_validate_peer (ctx
, thd
->cl
->filename
, &peer
);
2244 all_rc
= !all_rc
? rc
: all_rc
;
2252 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0, -1);
2253 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2255 if (pthread_equal (pthread_self (), thd
->tid
))
2259 if (!thd
->cl
->filename
||
2260 cache_iscached (thd
->cl
->filename
,
2261 NULL
) == GPG_ERR_NO_DATA
)
2267 cache_defer_clear (thd
->cl
->md5file
);
2270 else if (gpg_err_code (rc
) == GPG_ERR_NO_DATA
)
2278 rc
= cache_clear (thd
->cl
->md5file
);
2279 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2287 /* A single data filename was specified. Lock only this data file
2288 * mutex and free the cache entry. */
2291 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2292 rc
= do_validate_peer (ctx
, line
, &peer
);
2294 if (!rc
&& !memcmp (thd
->cl
->md5file
, md5file
, sizeof (md5file
)))
2296 rc
= cache_lock_mutex (thd
->cl
->ctx
, thd
->cl
->md5file
, -1, 0,
2298 if (gpg_err_code (rc
) == GPG_ERR_LOCKED
)
2300 if (pthread_equal (pthread_self (), thd
->tid
))
2307 rc
= cache_clear (thd
->cl
->md5file
);
2308 cache_unlock_mutex (thd
->cl
->md5file
, 0);
2312 cache_defer_clear (thd
->cl
->md5file
);
2320 /* Only connected clients' cache entries have been cleared. Now clear any
2321 * remaining cache entries without clients but only if there wasn't an
2322 * error from above since this would defeat the locking check of the
2323 * remaining entries. */
2329 /* No clients are using the specified file. */
2330 else if (!all_rc
&& !rc
&& !once
)
2332 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, line
, strlen (line
));
2333 rc
= cache_clear (md5file
);
2336 /* Release the connection mutex. */
2337 pthread_cleanup_pop (1);
2341 send_status_all (STATUS_CACHE
, NULL
);
2343 /* One or more files were locked while clearing all cache entries. */
2347 return send_error (ctx
, rc
);
2351 cachetimeout_command (assuan_context_t ctx
, char *line
)
2354 char **req
= str_split (line
, " ", 0);
2357 assuan_peercred_t peer
;
2359 if (!req
|| !*req
|| !req
[1])
2362 return send_error (ctx
, GPG_ERR_SYNTAX
);
2366 timeout
= (int) strtol (req
[1], &p
, 10);
2367 if (errno
!= 0 || *p
|| timeout
< -1)
2370 return send_error (ctx
, GPG_ERR_SYNTAX
);
2373 rc
= do_validate_peer (ctx
, req
[0], &peer
);
2376 unsigned char md5file
[16];
2378 gcry_md_hash_buffer (GCRY_MD_MD5
, md5file
, req
[0], strlen (req
[0]));
2379 rc
= cache_set_timeout (md5file
, timeout
);
2380 if (!rc
|| gpg_err_code (rc
) == GPG_ERR_NOT_FOUND
)
2383 MUTEX_LOCK (&rcfile_mutex
);
2384 config_set_int_param (&global_config
, req
[0], "cache_timeout",
2386 MUTEX_UNLOCK (&rcfile_mutex
);
2391 return send_error (ctx
, rc
);
2395 dump_command (assuan_context_t ctx
, char *line
)
2399 struct client_s
*client
= assuan_get_pointer (ctx
);
2402 if (disable_list_and_dump
== 1)
2403 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2406 return send_error (ctx
, GPG_ERR_SYNTAX
);
2408 rc
= peer_is_invoker(client
);
2410 return send_error (ctx
, rc
);
2412 xmlDocDumpFormatMemory (client
->doc
, &xml
, &len
, 1);
2416 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2417 pwmd_strerror (GPG_ERR_ENOMEM
));
2418 return send_error (ctx
, GPG_ERR_ENOMEM
);
2421 pthread_cleanup_push ((void *)xmlFree
, xml
);
2422 rc
= xfer_data (ctx
, (char *) xml
, len
);
2423 pthread_cleanup_pop (1);
2424 return send_error (ctx
, rc
);
2428 getconfig_command (assuan_context_t ctx
, char *line
)
2430 struct client_s
*client
= assuan_get_pointer (ctx
);
2432 char filename
[255] = { 0 }, param
[747] = { 0 };
2433 char *p
, *tmp
= NULL
, *fp
= client
->filename
, *paramp
= line
;
2435 if (!line
|| !*line
)
2436 return send_error (ctx
, GPG_ERR_SYNTAX
);
2438 if (strchr (line
, ' '))
2440 sscanf (line
, " %254[^ ] %746c", filename
, param
);
2445 if (fp
&& !valid_filename (fp
))
2446 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2448 paramp
= str_down (paramp
);
2449 if (!strcmp (paramp
, "passphrase"))
2450 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2452 p
= config_get_value (fp
? fp
: "global", paramp
);
2454 return send_error (ctx
, GPG_ERR_UNKNOWN_OPTION
);
2456 tmp
= expand_homedir (p
);
2460 log_write ("%s(%i): %s", __FILE__
, __LINE__
,
2461 pwmd_strerror (GPG_ERR_ENOMEM
));
2462 return send_error (ctx
, GPG_ERR_ENOMEM
);
2466 pthread_cleanup_push ((void *)xfree
, p
);
2467 rc
= xfer_data (ctx
, p
, strlen (p
));
2468 pthread_cleanup_pop (1);
2469 return send_error (ctx
, rc
);
2474 xmlXPathContextPtr xp
;
2475 xmlXPathObjectPtr result
;
2481 xpath_command_cleanup (void *arg
)
2483 struct xpath_s
*xpath
= arg
;
2488 req_cleanup (xpath
->req
);
2491 xmlBufferFree (xpath
->buf
);
2494 xmlXPathFreeObject (xpath
->result
);
2497 xmlXPathFreeContext (xpath
->xp
);
2501 do_xpath (assuan_context_t ctx
, char *line
)
2504 struct client_s
*client
= assuan_get_pointer (ctx
);
2505 struct xpath_s _x
= { 0 };
2506 struct xpath_s
*xpath
= &_x
;
2508 if (!line
|| !*line
)
2509 return GPG_ERR_SYNTAX
;
2511 if ((xpath
->req
= str_split (line
, "\t", 2)) == NULL
)
2513 if (strv_printf (&xpath
->req
, "%s", line
) == 0)
2514 return GPG_ERR_ENOMEM
;
2517 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2520 rc
= GPG_ERR_BAD_DATA
;
2525 xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2528 rc
= GPG_ERR_BAD_DATA
;
2532 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2534 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2538 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2539 xpath
->result
->nodesetval
,
2540 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, 0,
2544 else if (!xpath
->req
[1] && !xmlBufferLength (xpath
->buf
))
2546 rc
= GPG_ERR_NO_DATA
;
2549 else if (xpath
->req
[1])
2555 pthread_cleanup_push ((void *)xpath_command_cleanup
, &xpath
);
2556 rc
= xfer_data (ctx
, (char *) xmlBufferContent (xpath
->buf
),
2557 xmlBufferLength (xpath
->buf
));
2558 pthread_cleanup_pop (0);
2560 xpath_command_cleanup (xpath
);
2565 xpath_command (assuan_context_t ctx
, char *line
)
2567 struct client_s
*client
= assuan_get_pointer (ctx
);
2569 struct argv_s
*args
[] = {
2570 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2574 if (disable_list_and_dump
== 1)
2575 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2577 rc
= peer_is_invoker(client
);
2579 return send_error (ctx
, rc
);
2581 rc
= parse_options (&line
, args
, client
, 1);
2582 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2583 rc
= GPG_ERR_SYNTAX
;
2585 return send_error (ctx
, rc
);
2587 if (client
->opts
& OPT_INQUIRE
)
2589 unsigned char *result
;
2592 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2594 return send_error (ctx
, rc
);
2596 pthread_cleanup_push ((void *)xfree
, result
);
2597 rc
= do_xpath (ctx
, (char *)result
);
2598 pthread_cleanup_pop (1);
2601 rc
= do_xpath (ctx
, line
);
2603 return send_error (ctx
, rc
);
2607 do_xpathattr (assuan_context_t ctx
, char *line
)
2609 struct client_s
*client
= assuan_get_pointer (ctx
);
2613 struct xpath_s _x
= { 0 };
2614 struct xpath_s
*xpath
= &_x
;
2616 if (!line
|| !*line
)
2617 return GPG_ERR_SYNTAX
;
2619 if ((req
= str_split (line
, " ", 3)) == NULL
)
2620 return GPG_ERR_ENOMEM
;
2624 rc
= GPG_ERR_SYNTAX
;
2628 if (!strcasecmp (req
[0], "SET"))
2630 else if (!strcasecmp (req
[0], "DELETE"))
2634 rc
= GPG_ERR_SYNTAX
;
2638 if (!req
[1] || !req
[2])
2640 rc
= GPG_ERR_SYNTAX
;
2644 if ((xpath
->req
= str_split (req
[2], "\t", 3)) == NULL
)
2646 rc
= GPG_ERR_ENOMEM
;
2650 if (!xpath
->req
[0] || (!xpath
->req
[1] && !cmd
) || (xpath
->req
[1] && cmd
))
2652 rc
= GPG_ERR_SYNTAX
;
2656 xpath
->xp
= xmlXPathNewContext (client
->doc
);
2659 rc
= GPG_ERR_BAD_DATA
;
2663 xpath
->result
= xmlXPathEvalExpression ((xmlChar
*) xpath
->req
[0], xpath
->xp
);
2666 rc
= GPG_ERR_BAD_DATA
;
2670 if (xmlXPathNodeSetIsEmpty (xpath
->result
->nodesetval
))
2672 rc
= GPG_ERR_ELEMENT_NOT_FOUND
;
2676 rc
= xml_recurse_xpath_nodeset (client
, client
->doc
,
2677 xpath
->result
->nodesetval
,
2678 (xmlChar
*) xpath
->req
[1], &xpath
->buf
, cmd
,
2679 (xmlChar
*) req
[1]);
2682 xpath_command_cleanup (xpath
);
2687 /* XPATHATTR SET|DELETE <name> <expression>[<TAB>[value]] */
2689 xpathattr_command (assuan_context_t ctx
, char *line
)
2691 struct client_s
*client
= assuan_get_pointer (ctx
);
2693 struct argv_s
*args
[] = {
2694 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
2698 if (disable_list_and_dump
== 1)
2699 return send_error (ctx
, GPG_ERR_NOT_IMPLEMENTED
);
2701 rc
= peer_is_invoker(client
);
2703 return send_error (ctx
, rc
);
2705 rc
= parse_options (&line
, args
, client
, 1);
2706 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
2707 rc
= GPG_ERR_SYNTAX
;
2709 return send_error (ctx
, rc
);
2711 if (client
->opts
& OPT_INQUIRE
)
2713 unsigned char *result
;
2716 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2718 return send_error (ctx
, rc
);
2720 pthread_cleanup_push ((void *)xfree
, result
);
2721 rc
= do_xpathattr (ctx
, (char *)result
);
2722 pthread_cleanup_pop (1);
2725 rc
= do_xpathattr (ctx
, line
);
2727 return send_error (ctx
, rc
);
2731 do_import (struct client_s
*client
, const char *root_element
,
2732 unsigned char *content
)
2734 xmlDocPtr doc
= NULL
;
2735 xmlNodePtr n
= NULL
, root
;
2737 struct string_s
*str
= NULL
;
2738 struct xml_request_s
*req
= NULL
;
2740 if (!content
|| !*content
)
2741 return GPG_ERR_SYNTAX
;
2745 rc
= xml_new_request (client
, root_element
, XML_CMD_STORE
, &req
);
2752 if (!xml_valid_element_path (req
->args
, 0))
2754 xml_free_request (req
);
2756 return GPG_ERR_INV_VALUE
;
2760 str
= string_new_content ((char *)content
);
2761 str
= string_prepend (str
, "<pwmd>");
2762 str
= string_append (str
, "</pwmd>");
2763 doc
= xmlReadDoc ((xmlChar
*) str
->str
, NULL
, "UTF-8", XML_PARSE_NOBLANKS
);
2764 string_free (str
, 1);
2767 rc
= GPG_ERR_BAD_DATA
;
2771 root
= xmlDocGetRootElement (doc
);
2772 root
= root
->children
;
2773 if (root
->type
!= XML_ELEMENT_NODE
)
2775 rc
= GPG_ERR_BAD_DATA
;
2779 rc
= xml_validate_import (client
, root
);
2785 /* Create the new specified root element path, if needed. */
2786 n
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
2791 /* Overwrite the children of the specified root element path. */
2792 xmlUnlinkNode (n
->children
);
2793 xmlFreeNodeList (n
->children
);
2797 n
= xmlDocGetRootElement (client
->doc
);
2802 xmlChar
*name
= xml_attribute_value (root
, (xmlChar
*)"_name");
2805 rc
= GPG_ERR_BAD_DATA
;
2808 /* No --root argument passed. Overwrite the current documents' root
2809 * element matching the root element of the imported data. */
2810 xml_free_request (req
);
2812 rc
= xml_new_request (client
, (char *)name
, XML_CMD_DELETE
, &req
);
2818 tmp
= xml_find_elements (client
, req
,
2819 xmlDocGetRootElement (req
->doc
), &rc
);
2820 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
2825 xmlUnlinkNode (tmp
);
2826 xmlFreeNodeList (tmp
);
2835 copy
= xmlCopyNodeList (root
);
2837 rc
= GPG_ERR_ENOMEM
;
2840 n
= xmlAddChildList (n
, copy
);
2842 rc
= GPG_ERR_ENOMEM
;
2847 if (!rc
&& n
&& n
->parent
)
2848 rc
= xml_update_element_mtime (client
, n
->parent
);
2851 xml_free_request (req
);
2860 parse_import_opt_root (void *data
, void *value
)
2862 struct client_s
*client
= data
;
2864 client
->import_root
= str_dup (value
);
2865 return client
->import_root
? 0 : GPG_ERR_ENOMEM
;
2869 import_command (assuan_context_t ctx
, char *line
)
2872 struct client_s
*client
= assuan_get_pointer (ctx
);
2873 unsigned char *result
;
2875 struct argv_s
*args
[] = {
2876 &(struct argv_s
) {"root", OPTION_TYPE_ARG
, parse_import_opt_root
},
2880 xfree (client
->import_root
);
2881 client
->import_root
= NULL
;
2882 rc
= parse_options (&line
, args
, client
, 0);
2884 return send_error (ctx
, rc
);
2886 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
2889 xfree (client
->import_root
);
2890 client
->import_root
= NULL
;
2891 return send_error (ctx
, rc
);
2894 rc
= do_import (client
, client
->import_root
, result
);
2895 xfree (client
->import_root
);
2896 client
->import_root
= NULL
;
2897 return send_error (ctx
, rc
);
2901 do_lock (struct client_s
*client
, int add
)
2903 gpg_error_t rc
= lock_file_mutex (client
, add
);
2906 client
->flags
|= FLAG_LOCK_CMD
;
2912 lock_command (assuan_context_t ctx
, char *line
)
2914 struct client_s
*client
= assuan_get_pointer (ctx
);
2918 return send_error (ctx
, GPG_ERR_SYNTAX
);
2920 rc
= do_lock (client
, 0);
2921 return send_error (ctx
, rc
);
2925 unlock_command (assuan_context_t ctx
, char *line
)
2927 struct client_s
*client
= assuan_get_pointer (ctx
);
2931 return send_error (ctx
, GPG_ERR_SYNTAX
);
2933 rc
= unlock_file_mutex (client
, 0);
2934 return send_error (ctx
, rc
);
2938 get_set_env (const char *name
, const char *value
)
2940 if (value
&& *value
)
2941 setenv (name
, value
, 1);
2947 option_command (assuan_context_t ctx
, char *line
)
2949 struct client_s
*client
= assuan_get_pointer (ctx
);
2951 char namebuf
[255] = { 0 };
2952 char *name
= namebuf
;
2953 char *value
= NULL
, *p
, *tmp
= NULL
;
2955 p
= strchr (line
, '=');
2958 strncpy (namebuf
, line
, sizeof(namebuf
));
2959 namebuf
[sizeof(namebuf
)-1] = 0;
2963 strncpy (namebuf
, line
, strlen (line
)-strlen (p
));
2964 namebuf
[sizeof(namebuf
)-1] = 0;
2968 log_write2 ("OPTION name='%s' value='%s'", name
, value
);
2970 if (strcasecmp (name
, (char *) "lock-timeout") == 0)
2976 n
= strtol (value
, &tmp
, 10);
2978 return send_error (ctx
, GPG_ERR_INV_VALUE
);
2981 client
->lock_timeout
= n
;
2983 else if (strcasecmp (name
, (char *) "NAME") == 0)
2985 if (value
&& strchr (value
, ' '))
2986 rc
= GPG_ERR_INV_VALUE
;
2989 tmp
= pthread_getspecific (thread_name_key
);
2991 MUTEX_LOCK (&cn_mutex
);
2992 xfree (client
->thd
->name
);
2993 client
->thd
->name
= NULL
;
2995 if (!value
|| !*value
)
2996 pthread_setspecific (thread_name_key
, str_dup (""));
2999 client
->thd
->name
= str_dup (value
);
3000 pthread_setspecific (thread_name_key
, str_dup (value
));
3003 MUTEX_UNLOCK (&cn_mutex
);
3006 else if (strcasecmp (name
, "disable-pinentry") == 0)
3010 if (value
&& *value
)
3012 n
= (int) strtol (value
, &tmp
, 10);
3013 if (*tmp
|| n
< 0 || n
> 1)
3014 return send_error (ctx
, GPG_ERR_INV_VALUE
);
3018 client
->flags
|= FLAG_NO_PINENTRY
;
3020 client
->flags
&= ~FLAG_NO_PINENTRY
;
3022 else if (strcasecmp (name
, "ttyname") == 0)
3024 get_set_env ("GPG_TTY", value
);
3026 else if (strcasecmp (name
, "ttytype") == 0)
3028 get_set_env ("TERM", value
);
3030 else if (strcasecmp (name
, "display") == 0)
3032 get_set_env ("DISPLAY", value
);
3034 else if (strcasecmp (name
, "lc_messages") == 0)
3037 else if (strcasecmp (name
, "lc_ctype") == 0)
3040 else if (strcasecmp (name
, "desc") == 0)
3043 else if (strcasecmp (name
, "pinentry-timeout") == 0)
3047 rc
= GPG_ERR_UNKNOWN_OPTION
;
3049 return send_error (ctx
, rc
);
3053 do_rename (assuan_context_t ctx
, char *line
)
3055 struct client_s
*client
= assuan_get_pointer (ctx
);
3057 xmlNodePtr src
, dst
;
3058 struct string_s
*str
= NULL
, *tstr
;
3059 struct xml_request_s
*req
;
3062 args
= str_split (line
, " ", 0);
3063 if (!args
|| strv_length (args
) != 2)
3066 return GPG_ERR_SYNTAX
;
3069 if (!xml_valid_element ((xmlChar
*) args
[1]))
3072 return GPG_ERR_INV_VALUE
;
3075 rc
= xml_new_request (client
, args
[0], XML_CMD_RENAME
, &req
);
3082 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3086 rc
= xml_is_element_owner (client
, src
);
3090 xmlChar
*a
= xmlGetProp (src
, (xmlChar
*) "_name");
3093 rc
= GPG_ERR_ENOMEM
;
3097 /* To prevent unwanted effects:
3099 * <element _name="a"><element _name="b"/></element>
3103 if (xmlStrEqual (a
, (xmlChar
*) args
[1]))
3106 rc
= GPG_ERR_CONFLICT
;
3111 str
= string_new (args
[0]);
3114 rc
= GPG_ERR_ENOMEM
;
3118 p
= strrchr (str
->str
, '\t');
3120 tstr
= string_truncate (str
, strlen (str
->str
)-strlen (p
));
3122 tstr
= string_truncate (str
, 0);
3126 string_free (str
, 1);
3127 rc
= GPG_ERR_ENOMEM
;
3132 tstr
= string_append_printf (str
, "%s%s", str
->len
? "\t" : "", args
[1]);
3135 string_free (str
, 1);
3136 rc
= GPG_ERR_ENOMEM
;
3141 xml_free_request (req
);
3142 rc
= xml_new_request (client
, str
->str
, XML_CMD_RENAME
, &req
);
3143 string_free (str
, 1);
3150 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3151 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3156 /* Target may exist:
3158 * <element _name="a"/>
3159 * <element _name="b" target="a"/>
3165 rc
= GPG_ERR_CONFLICT
;
3171 rc
= xml_is_element_owner (client
, dst
);
3175 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3177 xml_unlink_node (client
, dst
);
3180 rc
= xml_add_attribute (client
, src
, "_name", args
[1]);
3183 xml_free_request (req
);
3189 rename_command (assuan_context_t ctx
, char *line
)
3191 struct client_s
*client
= assuan_get_pointer (ctx
);
3193 struct argv_s
*args
[] = {
3194 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3198 rc
= parse_options (&line
, args
, client
, 1);
3199 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3200 rc
= GPG_ERR_SYNTAX
;
3202 return send_error (ctx
, rc
);
3204 if (client
->opts
& OPT_INQUIRE
)
3206 unsigned char *result
;
3209 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3211 return send_error (ctx
, rc
);
3213 pthread_cleanup_push ((void *)xfree
, result
);
3214 rc
= do_rename (ctx
, (char *)result
);
3215 pthread_cleanup_pop (1);
3218 rc
= do_rename (ctx
, line
);
3220 return send_error (ctx
, rc
);
3224 do_copy (assuan_context_t ctx
, char *line
)
3226 struct client_s
*client
= assuan_get_pointer (ctx
);
3227 char **args
, **targs
;
3228 xmlNodePtr src
, dst
, tree
= NULL
;
3229 struct xml_request_s
*req
;
3232 args
= str_split (line
, " ", 0);
3233 if (!args
|| strv_length (args
) != 2)
3236 return GPG_ERR_SYNTAX
;
3239 targs
= str_split (args
[1], "\t", 0);
3243 return GPG_ERR_ENOMEM
;
3246 if (!xml_valid_element_path (targs
, 0))
3250 return GPG_ERR_INV_VALUE
;
3254 rc
= xml_new_request (client
, args
[0], XML_CMD_NONE
, &req
);
3261 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3265 tree
= xmlCopyNodeList (src
);
3268 rc
= GPG_ERR_ENOMEM
;
3272 xml_free_request (req
);
3273 /* Create the destination element path if it does not exist. */
3274 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &req
);
3281 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3285 rc
= xml_is_element_owner (client
, dst
);
3289 rc
= xml_validate_target (client
, req
->doc
, args
[1], src
);
3290 if (rc
|| src
== dst
)
3292 rc
= GPG_ERR_CONFLICT
;
3296 /* Merge any attributes from the src node to the initial dst node. */
3297 for (xmlAttrPtr attr
= tree
->properties
; attr
; attr
= attr
->next
)
3299 if (xmlStrEqual (attr
->name
, (xmlChar
*) "_name"))
3302 xmlAttrPtr a
= xmlHasProp (dst
, attr
->name
);
3306 xmlChar
*tmp
= xmlNodeGetContent (attr
->children
);
3307 xmlNewProp (dst
, attr
->name
, tmp
);
3309 /* Create the default attributes. */
3310 rc
= xml_add_attribute (client
, dst
, NULL
, NULL
);
3313 xmlNodePtr n
= dst
->children
;
3315 xmlFreeNodeList (n
);
3316 dst
->children
= NULL
;
3320 n
= xmlCopyNodeList (tree
->children
);
3323 rc
= GPG_ERR_ENOMEM
;
3327 n
= xmlAddChildList (dst
, n
);
3330 rc
= GPG_ERR_ENOMEM
;
3334 rc
= xml_update_element_mtime (client
, xmlDocGetRootElement (client
->doc
)
3335 == dst
->parent
? dst
: dst
->parent
);
3341 xmlUnlinkNode (tree
);
3342 xmlFreeNodeList (tree
);
3345 xml_free_request (req
);
3351 copy_command (assuan_context_t ctx
, char *line
)
3353 struct client_s
*client
= assuan_get_pointer (ctx
);
3355 struct argv_s
*args
[] = {
3356 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3360 rc
= parse_options (&line
, args
, client
, 1);
3361 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3362 rc
= GPG_ERR_SYNTAX
;
3364 return send_error (ctx
, rc
);
3366 if (client
->opts
& OPT_INQUIRE
)
3368 unsigned char *result
;
3371 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3373 return send_error (ctx
, rc
);
3375 pthread_cleanup_push ((void *)xfree
, result
);
3376 rc
= do_copy (ctx
, (char *)result
);
3377 pthread_cleanup_pop (1);
3380 rc
= do_copy (ctx
, line
);
3382 return send_error (ctx
, rc
);
3386 do_move (assuan_context_t ctx
, char *line
)
3388 struct client_s
*client
= assuan_get_pointer (ctx
);
3390 xmlNodePtr src
, dst
;
3391 struct xml_request_s
*req
;
3394 args
= str_split (line
, " ", 0);
3395 if (!args
|| strv_length (args
) != 2)
3398 return GPG_ERR_SYNTAX
;
3403 char **targs
= str_split (args
[1], "\t", 0);
3407 return GPG_ERR_ENOMEM
;
3410 if (!xml_valid_element_path (targs
, 0))
3414 return GPG_ERR_INV_VALUE
;
3420 rc
= xml_new_request (client
, args
[0], XML_CMD_MOVE
, &req
);
3427 src
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
), &rc
);
3431 rc
= xml_is_element_owner (client
, src
);
3435 xml_free_request (req
);
3439 rc
= xml_new_request (client
, args
[1], XML_CMD_NONE
, &req
);
3443 dst
= xml_find_elements (client
, req
, xmlDocGetRootElement (req
->doc
),
3447 dst
= xmlDocGetRootElement (client
->doc
);
3449 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3454 for (xmlNodePtr n
= dst
; n
; n
= n
->parent
)
3458 rc
= GPG_ERR_CONFLICT
;
3465 xmlChar
*a
= xml_attribute_value (src
, (xmlChar
*) "_name");
3466 xmlNodePtr dup
= xml_find_element (client
, dst
->children
, (char *) a
,
3470 if (rc
&& rc
!= GPG_ERR_ELEMENT_NOT_FOUND
)
3479 rc
= GPG_ERR_CONFLICT
;
3483 if (dst
== xmlDocGetRootElement (client
->doc
))
3488 while (n
->parent
&& n
->parent
!= dst
)
3491 xmlChar
*a
= xml_attribute_value (n
, (xmlChar
*) "_name");
3492 xmlChar
*b
= xml_attribute_value (src
, (xmlChar
*) "_name");
3494 if (xmlStrEqual (a
, b
))
3497 xmlUnlinkNode (src
);
3499 xmlFreeNodeList (n
);
3507 xmlUnlinkNode (dup
);
3508 xmlFreeNodeList (dup
);
3512 xmlUnlinkNode (dup
);
3516 if (!dst
&& *req
->args
)
3518 struct xml_request_s
*nreq
= NULL
;
3519 xmlChar
*name
= xml_attribute_value (src
, (xmlChar
*) "_name");
3521 if (src
->parent
== xmlDocGetRootElement (client
->doc
)
3522 && !strcmp ((char *) name
, *req
->args
))
3525 rc
= GPG_ERR_CONFLICT
;
3530 rc
= xml_new_request (client
, args
[1], XML_CMD_STORE
, &nreq
);
3533 dst
= xml_find_elements (client
, nreq
,
3534 xmlDocGetRootElement (nreq
->doc
), &rc
);
3535 xml_free_request (nreq
);
3542 xml_update_element_mtime (client
, src
->parent
);
3543 xmlUnlinkNode (src
);
3545 dst
= xmlAddChildList (dst
, src
);
3547 rc
= GPG_ERR_ENOMEM
;
3549 xml_update_element_mtime (client
, dst
->parent
);
3552 xml_free_request (req
);
3558 move_command (assuan_context_t ctx
, char *line
)
3560 struct client_s
*client
= assuan_get_pointer (ctx
);
3562 struct argv_s
*args
[] = {
3563 &(struct argv_s
) {"inquire", OPTION_TYPE_NOARG
, parse_opt_inquire
},
3567 rc
= parse_options (&line
, args
, client
, 1);
3568 if (!rc
&& (client
->opts
& OPT_INQUIRE
) && line
&& *line
)
3569 rc
= GPG_ERR_SYNTAX
;
3571 return send_error (ctx
, rc
);
3573 if (client
->opts
& OPT_INQUIRE
)
3575 unsigned char *result
;
3578 rc
= assuan_inquire (ctx
, "DATA", &result
, &len
, 0);
3580 return send_error (ctx
, rc
);
3582 pthread_cleanup_push ((void *)xfree
, result
);
3583 rc
= do_move (ctx
, (char *)result
);
3584 pthread_cleanup_pop (1);
3587 rc
= do_move (ctx
, line
);
3589 return send_error (ctx
, rc
);
3593 ls_command (assuan_context_t ctx
, char *line
)
3601 return send_error (ctx
, GPG_ERR_SYNTAX
);
3603 tmp
= str_asprintf ("%s/data", homedir
);
3604 dir
= expand_homedir (tmp
);
3607 rc
= gpg_error_from_errno (errno
);
3612 return send_error (ctx
, rc
);
3616 offsetof (struct dirent
, d_name
) +pathconf (dir
, _PC_NAME_MAX
) + 1;
3617 struct dirent
*p
= xmalloc (len
), *cur
= NULL
;
3621 pthread_cleanup_push ((void *)xfree
, p
);
3622 pthread_cleanup_push ((void *)(void *)(void *)closedir
, d
);
3625 while (!readdir_r (d
, p
, &cur
) && cur
)
3629 if (stat (cur
->d_name
, &st
) == -1 || !S_ISREG (st
.st_mode
))
3632 tmp
= str_asprintf ("%s%s\n", list
? list
: "", cur
->d_name
);
3639 rc
= GPG_ERR_ENOMEM
;
3647 pthread_cleanup_pop (1); // closedir (d)
3648 pthread_cleanup_pop (1); // xfree (p)
3651 return send_error (ctx
, rc
);
3654 return send_error (ctx
, GPG_ERR_NO_DATA
);
3656 list
[strlen (list
) - 1] = 0;
3657 pthread_cleanup_push ((void *)xfree
, list
);
3658 rc
= xfer_data (ctx
, list
, strlen (list
));
3659 pthread_cleanup_pop (1);
3660 return send_error (ctx
, rc
);
3664 bye_notify (assuan_context_t ctx
, char *line
)
3666 struct client_s
*cl
= assuan_get_pointer (ctx
);
3667 gpg_error_t ret
= 0;
3669 MUTEX_LOCK(&cn_mutex
);
3670 cl
->thd
->state
= CLIENT_STATE_DISCON
;
3671 MUTEX_UNLOCK(&cn_mutex
);
3674 if (cl
->thd
->remote
)
3680 struct timeval tv
= { 0, 50000 };
3682 rc
= gnutls_bye (cl
->thd
->tls
->ses
, GNUTLS_SHUT_RDWR
);
3683 if (rc
== GNUTLS_E_AGAIN
)
3684 select (0, NULL
, NULL
, NULL
, &tv
);
3686 while (rc
== GNUTLS_E_AGAIN
);
3690 /* This will let assuan_process_next() return. */
3691 if (fcntl (cl
->thd
->fd
, F_SETFL
, O_NONBLOCK
) == -1)
3693 cl
->last_rc
= gpg_error_from_errno (errno
);
3697 cl
->last_rc
= 0; // BYE command result
3702 reset_notify (assuan_context_t ctx
, char *line
)
3704 struct client_s
*client
= assuan_get_pointer (ctx
);
3707 cleanup_client (client
);
3713 * This is called before every Assuan command.
3716 command_startup (assuan_context_t ctx
, const char *name
)
3718 struct client_s
*client
= assuan_get_pointer (ctx
);
3720 struct command_table_s
*cmd
= NULL
;
3722 log_write2 ("command='%s'", name
);
3723 client
->last_rc
= client
->opts
= 0;
3725 for (int i
= 0; command_table
[i
]; i
++)
3727 if (!strcasecmp (name
, command_table
[i
]->name
))
3729 if (command_table
[i
]->ignore_startup
)
3731 cmd
= command_table
[i
];
3737 return GPG_ERR_UNKNOWN_COMMAND
;
3739 client
->last_rc
= rc
= gpg_error (file_modified (client
, cmd
));
3741 update_client_state (client
, CLIENT_STATE_COMMAND
);
3747 * This is called after every Assuan command.
3750 command_finalize (assuan_context_t ctx
, gpg_error_t rc
)
3752 struct client_s
*client
= assuan_get_pointer (ctx
);
3754 if (!(client
->flags
& FLAG_LOCK_CMD
))
3755 unlock_file_mutex (client
, 0);
3757 unlock_flock (&client
->flock_fd
);
3758 log_write2 (_("command completed: rc=%u"), rc
? rc
: client
->last_rc
);
3759 client
->last_rc
= gpg_error (GPG_ERR_UNKNOWN_COMMAND
);
3761 client
->thd
->buffer_timeout
= client
->thd
->last_buffer_size
= 0;
3763 update_client_state (client
, CLIENT_STATE_IDLE
);
3767 parse_help_opt_html (void *data
, void *value
)
3769 struct client_s
*client
= data
;
3772 client
->opts
|= OPT_HTML
;
3777 help_command (assuan_context_t ctx
, char *line
)
3781 struct client_s
*client
= assuan_get_pointer (ctx
);
3782 struct argv_s
*args
[] = {
3783 &(struct argv_s
) {"html", OPTION_TYPE_NOARG
, parse_help_opt_html
},
3787 rc
= parse_options (&line
, args
, client
, 1);
3789 return send_error (ctx
, rc
);
3791 if (!line
|| !*line
)
3794 char *help
= str_dup (_("Usage: HELP [--html] [<COMMAND>]\n"
3795 "For commands that take an element path as an argument, each element is "
3796 "separated with an ASCII @key{TAB} character (ASCII 0x09)."
3799 for (i
= 0; command_table
[i
]; i
++)
3801 if (!command_table
[i
]->help
)
3804 /* @npxref{} won't put a "See " or "see " in front of the command.
3805 * It's not a texinfo command but needed for --html. */
3806 tmp
= str_asprintf ("%s %s%s%s", help
,
3807 client
->opts
& OPT_HTML
? "@npxref{" : "",
3808 command_table
[i
]->name
,
3809 client
->opts
& OPT_HTML
? "}" : "");
3814 tmp
= strip_texi_and_wrap (help
, client
->opts
& OPT_HTML
);
3816 pthread_cleanup_push ((void *)xfree
, tmp
);
3817 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3818 pthread_cleanup_pop (1);
3819 return send_error (ctx
, rc
);
3822 for (i
= 0; command_table
[i
]; i
++)
3824 if (!strcasecmp (line
, command_table
[i
]->name
))
3828 if (!command_table
[i
]->help
)
3831 help
= strip_texi_and_wrap (command_table
[i
]->help
,
3832 client
->opts
& OPT_HTML
);
3833 tmp
= str_asprintf ("%s%s",
3834 (client
->opts
& OPT_HTML
) ? "" : _("Usage: "),
3837 pthread_cleanup_push ((void *)xfree
, tmp
);
3838 rc
= xfer_data (ctx
, tmp
, strlen (tmp
));
3839 pthread_cleanup_pop (1);
3840 return send_error (ctx
, rc
);
3844 return send_error (ctx
, GPG_ERR_INV_NAME
);
3848 new_command (const char *name
, int ignore
, int unlock
, int flock_type
,
3849 gpg_error_t (*handler
) (assuan_context_t
, char *),
3853 struct command_table_s
**tmp
;
3856 for (i
= 0; command_table
[i
]; i
++);
3858 tmp
= xrealloc (command_table
, (i
+ 2) * sizeof (struct command_table_s
*));
3860 command_table
= tmp
;
3861 command_table
[i
] = xcalloc (1, sizeof (struct command_table_s
));
3862 command_table
[i
]->name
= name
;
3863 command_table
[i
]->handler
= handler
;
3864 command_table
[i
]->ignore_startup
= ignore
;
3865 command_table
[i
]->unlock
= unlock
;
3866 command_table
[i
]->flock_type
= flock_type
;
3867 command_table
[i
++]->help
= help
;
3868 command_table
[i
] = NULL
;
3876 for (i
= 0; command_table
[i
]; i
++)
3877 xfree (command_table
[i
]);
3879 xfree (command_table
);
3883 sort_commands (const void *arg1
, const void *arg2
)
3885 struct command_table_s
*const *a
= arg1
;
3886 struct command_table_s
*const *b
= arg2
;
3895 return strcmp ((*a
)->name
, (*b
)->name
);
3899 passwd_command (assuan_context_t ctx
, char *line
)
3901 struct client_s
*client
= assuan_get_pointer (ctx
);
3904 rc
= peer_is_invoker (client
);
3905 if (rc
== GPG_ERR_EACCES
)
3906 return send_error (ctx
, GPG_ERR_FORBIDDEN
);
3908 return send_error (ctx
, rc
);
3910 if (client
->flags
& FLAG_NEW
)
3911 return send_error (ctx
, GPG_ERR_INV_STATE
);
3913 rc
= crypto_init_ctx (client
->crypto
, client
->flags
& FLAG_NO_PINENTRY
);
3915 return send_error (ctx
, rc
);
3918 rc
= crypto_passwd (client
, client
->crypto
);
3920 crypto_cleanup_non_keys (client
->crypto
);
3921 return send_error (ctx
, rc
);
3925 parse_opt_data (void *data
, void *value
)
3927 struct client_s
*client
= data
;
3930 client
->opts
|= OPT_DATA
;
3935 send_client_list (assuan_context_t ctx
)
3937 struct client_s
*client
= assuan_get_pointer (ctx
);
3940 if (client
->opts
& OPT_VERBOSE
)
3946 MUTEX_LOCK (&cn_mutex
);
3947 pthread_cleanup_push ((void *)cleanup_mutex_cb
, &cn_mutex
);
3948 t
= slist_length (cn_thread_list
);
3950 for (i
= 0; i
< t
; i
++)
3952 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
3955 if (thd
->state
== CLIENT_STATE_UNKNOWN
)
3958 tmp
= build_client_info_line (thd
, 0);
3961 char **l
= strv_cat (list
, tmp
);
3963 rc
= GPG_ERR_ENOMEM
;
3968 rc
= GPG_ERR_ENOMEM
;
3977 pthread_cleanup_pop (1);
3981 line
= strv_join ("\n", list
);
3983 pthread_cleanup_push ((void *)xfree
, line
);
3984 rc
= xfer_data (ctx
, line
, strlen (line
));
3985 pthread_cleanup_pop (1);
3989 if (client
->opts
& OPT_DATA
)
3991 char buf
[ASSUAN_LINELENGTH
];
3993 MUTEX_LOCK (&cn_mutex
);
3994 snprintf (buf
, sizeof (buf
), "%u", slist_length (cn_thread_list
));
3995 MUTEX_UNLOCK (&cn_mutex
);
3996 rc
= xfer_data (ctx
, buf
, strlen (buf
));
3999 rc
= send_status (ctx
, STATUS_CLIENTS
, NULL
);
4005 getinfo_command (assuan_context_t ctx
, char *line
)
4007 struct client_s
*client
= assuan_get_pointer (ctx
);
4009 char buf
[ASSUAN_LINELENGTH
];
4010 struct argv_s
*args
[] = {
4011 &(struct argv_s
) {"data", OPTION_TYPE_NOARG
, parse_opt_data
},
4012 &(struct argv_s
) {"verbose", OPTION_TYPE_NOARG
, parse_opt_verbose
},
4016 rc
= parse_options (&line
, args
, client
, 1);
4018 return send_error (ctx
, rc
);
4020 if (!strcasecmp (line
, "clients"))
4022 rc
= send_client_list (ctx
);
4024 else if (!strcasecmp (line
, "cache"))
4026 if (client
->opts
& OPT_DATA
)
4028 snprintf (buf
, sizeof (buf
), "%u", cache_file_count ());
4029 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4032 rc
= send_status (ctx
, STATUS_CACHE
, NULL
);
4034 else if (!strcasecmp (line
, "pid"))
4037 pid_t pid
= getpid ();
4039 snprintf (buf
, sizeof (buf
), "%i", pid
);
4040 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4042 else if (!strcasecmp (line
, "version"))
4044 char *buf
= str_asprintf ("0x%06x %s", VERSION_HEX
,
4052 rc
= xfer_data (ctx
, buf
, strlen (buf
));
4055 else if (!strcasecmp (line
, "last_error"))
4057 if (client
->last_error
)
4058 rc
= xfer_data (ctx
, client
->last_error
, strlen (client
->last_error
));
4060 rc
= GPG_ERR_NO_DATA
;
4062 else if (!strcasecmp (line
, "user"))
4067 if (client
->thd
->remote
)
4068 user
= str_asprintf ("#%s", client
->thd
->tls
->fp
);
4070 user
= get_username (client
->thd
->peer
->uid
);
4072 user
= get_username (client
->thd
->peer
->uid
);
4076 pthread_cleanup_push ((void *)xfree
, user
);
4077 rc
= xfer_data (ctx
, user
, strlen (user
));
4078 pthread_cleanup_pop (1);
4081 rc
= GPG_ERR_NO_DATA
;
4084 rc
= gpg_error (GPG_ERR_SYNTAX
);
4086 return send_error (ctx
, rc
);
4090 parse_listkeys_opt_secret_only (void *data
, void *value
)
4092 struct client_s
*client
= data
;
4095 client
->opts
|= OPT_SECRET_ONLY
;
4100 keyinfo_command (assuan_context_t ctx
, char *line
)
4102 struct client_s
*client
= assuan_get_pointer (ctx
);
4104 char **keys
= NULL
, **p
= NULL
;
4108 return send_error (ctx
, GPG_ERR_SYNTAX
);
4110 if (!(client
->flags
& FLAG_OPEN
))
4111 return send_error (ctx
, GPG_ERR_INV_STATE
);
4113 if (client
->flags
& FLAG_NEW
)
4114 return send_error (ctx
, GPG_ERR_NO_DATA
);
4116 rc
= lock_flock (ctx
, client
->filename
, FLOCK_TYPE_SH
, &client
->flock_fd
);
4118 return send_error (ctx
, rc
);
4120 rc
= crypto_is_symmetric (client
->filename
);
4121 unlock_flock (&client
->flock_fd
);
4124 else if (rc
!= GPG_ERR_BAD_DATA
)
4125 return send_error (ctx
, rc
);
4130 p
= strv_catv(keys
, client
->crypto
->pubkey
);
4132 rc
= GPG_ERR_ENOMEM
;
4138 for (p
= client
->crypto
->sigkey
; p
&& *p
; p
++)
4140 if (!strv_printf(&keys
, "S%s", *p
))
4143 return send_error (ctx
, GPG_ERR_ENOMEM
);
4148 rc
= GPG_ERR_ENOMEM
;
4152 line
= strv_join ("\n", keys
);
4154 pthread_cleanup_push ((void *)xfree
, line
);
4156 rc
= xfer_data (ctx
, line
, strlen (line
));
4158 rc
= GPG_ERR_ENOMEM
;
4160 pthread_cleanup_pop (1);
4163 rc
= GPG_ERR_NO_DATA
;
4167 return send_error (ctx
, rc
);
4171 kill_command (assuan_context_t ctx
, char *line
)
4173 struct client_s
*client
= assuan_get_pointer (ctx
);
4177 if (!line
|| !*line
)
4178 return send_error (ctx
, GPG_ERR_SYNTAX
);
4180 MUTEX_LOCK (&cn_mutex
);
4181 pthread_cleanup_push ((void *)cleanup_mutex_cb
, &cn_mutex
);
4182 t
= slist_length (cn_thread_list
);
4185 for (i
= 0; i
< t
; i
++)
4187 struct client_thread_s
*thd
= slist_nth_data (cn_thread_list
, i
);
4188 char *tmp
= str_asprintf ("%p", thd
->tid
);
4190 if (strcmp (line
, tmp
))
4197 rc
= peer_is_invoker (client
);
4200 #ifdef HAVE_PTHREAD_CANCEL
4201 rc
= pthread_cancel (thd
->tid
);
4209 rc
= GPG_ERR_FORBIDDEN
;
4210 if (config_get_boolean ("global", "strict_kill"))
4214 if (client
->thd
->remote
&& thd
->remote
)
4216 if (!strcmp (client
->thd
->tls
->fp
, thd
->tls
->fp
))
4218 #ifdef HAVE_PTHREAD_CANCEL
4219 rc
= pthread_cancel (thd
->tid
);
4227 else if (!client
->thd
->remote
&& !thd
->remote
)
4230 if (client
->thd
->peer
->uid
== thd
->peer
->uid
)
4232 #ifdef HAVE_PTHREAD_CANCEL
4233 rc
= pthread_cancel (thd
->tid
);
4243 pthread_cleanup_pop (1);
4244 return send_error (ctx
, rc
);
4248 listkeys_command (assuan_context_t ctx
, char *line
)
4250 struct client_s
*client
= assuan_get_pointer (ctx
);
4251 struct crypto_s
*crypto
= NULL
;
4252 char **pattern
= NULL
;
4253 gpgme_key_t
*keys
= NULL
;
4254 char **result
= NULL
;
4256 struct argv_s
*args
[] = {
4257 &(struct argv_s
) {"secret-only", OPTION_TYPE_NOARG
,
4258 parse_listkeys_opt_secret_only
},
4262 rc
= parse_options (&line
, args
, client
, 1);
4264 return send_error (ctx
, rc
);
4266 rc
= crypto_init (&crypto
, client
->ctx
, client
->filename
,
4267 client
->flags
& FLAG_NO_PINENTRY
);
4269 return send_error (ctx
, rc
);
4271 pthread_cleanup_push ((void *)(void *)crypto_cleanup
, crypto
);
4272 pattern
= str_split (line
, ",", 0);
4273 pthread_cleanup_push ((void *)(void *)strv_free
, pattern
);
4274 rc
= crypto_list_keys (crypto
, pattern
, client
->opts
& OPT_SECRET_ONLY
,
4276 pthread_cleanup_pop (1);
4277 pthread_cleanup_pop (1);
4282 for (i
= 0; keys
[i
]; i
++)
4284 char *p
= crypto_key_info (keys
[i
]);
4289 rc
= GPG_ERR_ENOMEM
;
4293 r
= strv_cat (result
, p
);
4296 rc
= GPG_ERR_ENOMEM
;
4304 rc
= GPG_ERR_NO_DATA
;
4306 crypto_free_key_list (keys
);
4311 line
= strv_join ("\n", result
);
4313 pthread_cleanup_push ((void *)xfree
, line
);
4315 rc
= GPG_ERR_ENOMEM
;
4317 rc
= xfer_data (ctx
, line
, strlen (line
));
4319 pthread_cleanup_pop (1);
4324 return send_error (ctx
, rc
);
4330 /* !BEGIN-HELP-TEXT!
4332 * This comment is used as a marker to generate the offline documentation
4333 * found in doc/pwmd.info. The indentation needs to be kept for the awk
4334 * script to determine where commands begin and end.
4336 new_command("HELP", 1, 1, 0, help_command
, _(
4337 "HELP [--html] [<COMMAND>]\n"
4338 "Show available commands or command specific help text."
4340 "The @option{--html} option will output the help text in HTML format."
4343 new_command("KILL", 1, 0, 0, kill_command
, _(
4344 "KILL <thread_id>\n"
4345 "Terminates the client identified by @var{thread_id} and releases any file "
4346 "lock or other resources it has held. See @code{GETINFO} (@pxref{GETINFO}) "
4347 "for details about listing connected clients. An @code{invoking_user} "
4348 "(@pxref{Configuration}) may kill any client while others may only kill "
4349 "clients of the same @code{UID} or @abbr{TLS} fingerprint.\n"
4352 new_command("LISTKEYS", 0, 0, 0, listkeys_command
, _(
4353 "LISTKEYS [--secret-only] [pattern[,<pattern>]]\n"
4354 "Returns a new line separated list of key information matching a comma "
4355 "separated list of @var{pattern}'s. When option @option{--secret-only} is "
4356 "specified, only keys matching @var{pattern} that also have a secret key "
4357 "available will be returned."
4360 new_command("KEYINFO", 1, 0, 0, keyinfo_command
, _(
4362 "Returns a new line separated list of key ID's that the currently opened "
4363 "data file has recipients and signers for. If the key is a signing key it "
4364 "will be prefixed with an @code{S}. If the file is a new one, or has no "
4365 "signers in the case of being symmetrically encrypted, the error code "
4366 "@code{GPG_ERR_NO_DATA} is returned."
4369 new_command("GETINFO", 1, 1, 0, getinfo_command
, _(
4370 "GETINFO [--data] [--verbose] CACHE | CLIENTS | PID | USER | LAST_ERROR | VERSION\n"
4371 "Get server and other information. The information is returned via a status "
4372 "message (@pxref{Status Messages}) unless otherwise noted or @option{--data} "
4375 "@var{CACHE} returns the number of cached documents."
4377 "@var{CLIENTS} returns the number of "
4378 "connected clients via a status message or a list of connected clients when "
4379 "the @option{--verbose} parameter is used (implies @option{--data}). A "
4380 "verbose line of a client list contains "
4382 "fields: the thread ID, client name, opened file (@code{/} if none opened), "
4383 "IP address if remote, file lock status, whether the current client is self "
4384 "or not, client state (see below), "
4385 "user ID or TLS fingerprint of the connected client, username if the "
4386 "client is a local one else @code{-}, and finally the time stamp of when the "
4389 "Client state @code{0} is an unknown client state, state @code{1} indicates "
4390 "the client has connected but hasn't completed initializing, state @code{2} "
4391 "indicates that the client is idle, state @code{3} means the "
4392 "client is in a command and state @code{4} means the client is disconnecting. "
4394 "@var{PID} returns the process ID number of the server via a data response."
4396 "@var{VERSION} returns the server version number and compile-time features "
4397 "via a data response with each being space delimited."
4399 "@var{LAST_ERROR} returns a detailed description of the last failed command "
4400 "via a data response, when available."
4402 "@var{USER} returns the username or @abbr{TLS} hash of the connected client "
4403 "via a data response."
4406 new_command("PASSWD", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, passwd_command
, _(
4408 "Changes the passphrase of the secret key required to open the current "
4409 "data file. If the data file is symmetrically encrypted, the error "
4410 "@code{GPG_ERR_NOT_SUPPORTED} is returned. When symmetrically encrypted, "
4411 "the @code{SAVE} command (@pxref{SAVE}) should be used instead to prevent "
4412 "this command saving any unwanted changes to the @abbr{XML} document."
4414 "This command is not available to non-invoking clients "
4415 "(@pxref{Access Control})."
4418 new_command("OPEN", 1, 1, FLOCK_TYPE_SH
|FLOCK_TYPE_KEEP
, open_command
, _(
4419 "OPEN [--lock] <filename>\n"
4420 "Opens @var{filename}. When the @var{filename} is not found on the "
4421 "file-system then a new in-memory document will be created. If the file is "
4422 "found, it is looked for in the file cache and when found no passphrase will "
4423 "be required to open it. When not cached, @cite{pinentry(1)} will be used to "
4424 "retrieve the passphrase for decryption unless @option{disable-pinentry} "
4425 "(@pxref{OPTION}) was specified in which case @command{pwmd} will "
4426 "@emph{INQUIRE} the client for the passphrase."
4428 "When the @option{--lock} option is passed then the file mutex will be "
4429 "locked as if the @code{LOCK} command (@pxref{LOCK}) had been sent after the "
4430 "file had been opened."
4433 new_command("SAVE", 0, 0, FLOCK_TYPE_EX
|FLOCK_TYPE_KEEP
, save_command
, _(
4434 "SAVE [--symmetric] [--inquire-keyparam] [--keyid=<keyid>[,..] | [--inquire-keyid]] [--sign-keyid=<fingerprint>[,..] | [--inquire-sign-keyid]]\n"
4435 "Writes the in-memory @abbr{XML} document to disk. The file written to is the "
4436 "file that was opened using the @code{OPEN} command (@pxref{OPEN}). If the "
4437 "file is a new one or the option @option{--inquire-keyparam} was passed, a "
4438 "new keypair will be generated and @cite{pinentry(1)} will be used to prompt "
4439 "for the passphrase of the secret key used for signing."
4441 "The @option{--inquire-keyparam} option will send an "
4442 "@emph{INQUIRE} to the client to obtain the key parameters to use for "
4443 "generating the new keypair. The inquired data is expected to be in "
4444 "@cite{gnupg} @abbr{XML} format. See the @cite{gnupg} documentation for "
4445 "details. Note that when this option is specified a new keypair will be "
4446 "generated reguardless if the file is a new one and that the passphrase for "
4447 "the current file will be required before generating the new keypair. This "
4448 "option is available to non-invoking clients (@pxref{Access Control}) only "
4449 "when the file is a new one."
4451 "You can encrypt the data file to a recipient other than the one that it "
4452 "was encrypted with by passing the @option{--keyid} or "
4453 "@option{--inquire-keyid} option with "
4454 "the key ID of a public encryption key as its argument. Use the "
4455 "@command{LISTKEYS} command (@pxref{LISTKEYS}) to show key information by "
4456 "pattern. The @option{--sign-keyid} or @option{--inquire-sign-keyid} option "
4457 "may also be used to sign the data "
4458 "file with an alternate key by specifying the key ID of a secret key. "
4459 "A passphrase to decrypt the data file "
4460 "will be required if one or more of the original encryption or signing keys "
4461 "are not found in either of these two options' arguments. The original "
4462 "encryption or signing keys will be used when either of these options are "
4465 "The @option{--keyid} and @option{--sign-keyid} options are not available "
4466 "for non-invoking clients "
4467 "(@pxref{Access Control}) when the recipients or signers do not match those "
4468 "that were used when the file was @code{OPEN}'ed."
4470 "The @option{--symmetric} option specifies that a new data file be "
4471 "conventionally encrypted. These types of data files do not use a recipient "
4472 "public key but may be signed by using the @option{--sign-keyid} or "
4473 "@option{--inquire-sign-keyid} options. To remove all signers from a "
4474 "symmtrically encrypted data file, leave the option value empty. Note that "
4475 "you cannot change encryption schemes once a data file has been saved."
4478 new_command("ISCACHED", 1, 0, 0, iscached_command
, _(
4479 "ISCACHED [--lock] <filename>\n"
4480 "An @emph{OK} response is returned if the specified @var{filename} is found "
4481 "in the file cache. If not found in the cache but exists on the filesystem "
4482 "then @code{GPG_ERR_NO_DATA} is returned. Otherwise a filesystem error is "
4485 "The @option{--lock} option will lock the file mutex of @var{filename} when "
4486 "the file exists; it does not need to be opened nor cached. The lock will be "
4487 "released when the client exits or sends the @code{UNLOCK} command "
4491 new_command("CLEARCACHE", 1, 1, 0, clearcache_command
, _(
4492 "CLEARCACHE [<filename>]\n"
4493 "Clears a file cache entry for all or the specified @var{filename}."
4496 new_command("CACHETIMEOUT", 1, 1, 0, cachetimeout_command
, _(
4497 "CACHETIMEOUT <filename> <seconds>\n"
4498 "The time in @var{seconds} until @var{filename} will be removed from the "
4499 "cache. @code{-1} will keep the cache entry forever, @code{0} will require "
4500 "the passphrase for each @code{OPEN} command (@pxref{OPEN}) or @code{SAVE} "
4501 "(@pxref{SAVE}) command. @xref{Configuration}, and the @code{cache_timeout} "
4505 new_command("LIST", 0, 1, 0, list_command
, _(
4506 "LIST [--inquire] [--recurse] [element[<TAB>child[..]]]\n"
4507 "If no element path is given then a newline separated list of root elements "
4508 "is returned with a data response. If given, then children of the specified "
4509 "element path are returned."
4511 "Each element path "
4512 "returned will have zero or more flags appened to it. These flags are "
4513 "delimited from the element path by a single space character. A flag itself "
4514 "is a single character. Flag @code{P} indicates that access to the element "
4515 "is denied. Flag @code{+} indicates that there are child nodes of "
4516 "the current element path. Flag @code{E} indicates that an element of the "
4517 "element path contained in a @var{target} attribute could not be found. Flag "
4518 "@code{O} indicates that a @var{target} attribute recursion limit was reached "
4519 "(@pxref{Configuration}). Flag @code{T}, followed by a single space character, "
4520 "then an element path, is the element path of the @var{target} attribute "
4521 "contained in the current element."
4523 "When a specified element path contains an error, beit from the final "
4524 "element in the path or any previous element, the path is still shown but "
4525 "will contain the error flag for the element with the error. Determining "
4526 "the actual element which contains the error is up to the client. This can be "
4527 "done by traversing the final element up to parent elements that contain the "
4530 "The option @option{--recurse} may be used to list the entire element tree "
4531 "for a specified element path or the entire tree for all root elements."
4533 "When the @option{--inquire} option is passed then all remaining non-option "
4534 "arguments are retrieved via a server @emph{INQUIRE}."
4537 new_command("REALPATH", 0, 1, 0, realpath_command
, _(
4538 "REALPATH [--inquire] element[<TAB>child[..]]\n"
4539 "Resolves all @code{target} attributes of the specified element path and "
4540 "returns the result with a data response. @xref{Target Attribute}, for details."
4542 "When the @option{--inquire} option is passed then all remaining non-option "
4543 "arguments are retrieved via a server @emph{INQUIRE}."
4546 new_command("STORE", 0, 1, 0, store_command
, _(
4547 "STORE element[<TAB>child[..]]<TAB>[content]\n"
4548 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4550 "Creates a new element path or modifies the @var{content} of an existing "
4551 "element. If only a single element is specified then a new root element is "
4552 "created. Otherwise, elements are @key{TAB} delimited and the content will be "
4553 "set to the final @key{TAB} delimited element. If no @var{content} is "
4554 "specified after the final @key{TAB}, then the content of the existing "
4555 "element will be removed; or will be empty if creating a new element."
4557 "The only restriction of an element name is that it not contain whitespace "
4558 "characters. There is no other whitespace between the @key{TAB} delimited "
4559 "elements. It is recommended that the content of an element be base64 encoded "
4560 "when it contains control or @key{TAB} characters to prevent @abbr{XML} "
4561 "parsing and @command{pwmd} syntax errors."
4564 new_command("RENAME", 0, 1, 0, rename_command
, _(
4565 "RENAME [--inquire] element[<TAB>child[..]] <value>\n"
4566 "Renames the specified @var{element} to the new @var{value}. If an element of "
4567 "the same name as the @var{value} already exists it will be overwritten."
4569 "When the @option{--inquire} option is passed then all remaining non-option "
4570 "arguments are retrieved via a server @emph{INQUIRE}."
4573 new_command("COPY", 0, 1, 0, copy_command
, _(
4574 "COPY [--inquire] source[<TAB>child[..]] dest[<TAB>child[..]]\n"
4575 "Copies the entire element tree starting from the child node of the source "
4576 "element, to the destination element path. If the destination element path "
4577 "does not exist then it will be created; otherwise it is overwritten."
4579 "Note that attributes from the source element are merged into the "
4580 "destination element when the destination element path exists. When an "
4581 "attribute of the same name exists in both the source and destination "
4582 "elements then the destination attribute will be updated to the source "
4585 "When the @option{--inquire} option is passed then all remaining non-option "
4586 "arguments are retrieved via a server @emph{INQUIRE}."
4589 new_command("MOVE", 0, 1, 0, move_command
, _(
4590 "MOVE [--inquire] source[<TAB>child[..]] [dest[<TAB>child[..]]]\n"
4591 "Moves the source element path to the destination element path. If the "
4592 "destination is not specified then it will be moved to the root node of the "
4593 "document. If the destination is specified and exists then it will be "
4594 "overwritten; otherwise non-existing elements of the destination element "
4595 "path will be created."
4597 "When the @option{--inquire} option is passed then all remaining non-option "
4598 "arguments are retrieved via a server @emph{INQUIRE}."
4601 new_command("DELETE", 0, 1, 0, delete_command
, _(
4602 "DELETE [--inquire] element[<TAB>child[..]]\n"
4603 "Removes the specified element path and all of its children. This may break "
4604 "an element with a @code{target} attribute (@pxref{Target Attribute}) that "
4605 "refers to this element or any of its children."
4607 "When the @option{--inquire} option is passed then all remaining non-option "
4608 "arguments are retrieved via a server @emph{INQUIRE}."
4611 new_command("GET", 0, 1, 0, get_command
, _(
4612 "GET [--inquire] element[<TAB>child[..]]\n"
4613 "Retrieves the content of the specified element. The content is returned "
4614 "with a data response."
4616 "When the @option{--inquire} option is passed then all remaining non-option "
4617 "arguments are retrieved via a server @emph{INQUIRE}."
4620 new_command("ATTR", 0, 1, 0, attr_command
, _(
4621 "ATTR [--inquire] SET|GET|DELETE|LIST [<attribute>] element[<TAB>child[..]] ..\n"
4623 "@item ATTR SET attribute element[<TAB>child[..]] [value]\n"
4624 " Stores or updates an @var{attribute} name and optional @var{value} of an "
4625 "element. When no @var{value} is specified any existing value will be removed."
4627 "@item ATTR DELETE attribute element[<TAB>child[..]]\n"
4628 " Removes an attribute from an element. If @var{attribute} is @code{_name} "
4629 "or @code{target} an error is returned. Use the @command{DELETE} command "
4630 "(@pxref{DELETE}) instead."
4632 "@item ATTR LIST element[<TAB>child[..]]\n"
4633 " Retrieves a newline separated list of attributes names and values "
4634 "from the specified element. Each attribute name and value is space delimited."
4636 "@item ATTR GET attribute element[<TAB>child[..]]\n"
4637 " Retrieves the value of an @var{attribute} from an element."
4640 "When the @option{--inquire} option is passed then all remaining non-option "
4641 "arguments are retrieved via a server @emph{INQUIRE}."
4643 "@xref{Target Attribute}, for details about this special attribute and also "
4644 "@pxref{Other Attributes} for other attributes that are handled specially "
4645 "by @command{pwmd}."
4648 new_command("XPATH", 0, 1, 0, xpath_command
, _(
4649 "XPATH [--inquire] <expression>[<TAB>[value]]\n"
4650 "Evaluates an XPath @var{expression}. If no @var{value} argument is "
4651 "specified it is assumed the expression is a request to return a result. "
4652 "Otherwise, the result is set to the @var{value} argument and the document is "
4653 "updated. If there is no @var{value} after the @key{TAB} character, the value "
4654 "is assumed to be empty and the document is updated. For example:"
4657 "XPATH //element[@@_name='password']@key{TAB}\n"
4660 "would clear the content of all @var{password} elements in the data file "
4661 "while leaving off the trailing @key{TAB} would return all @var{password} "
4662 "elements in @abbr{XML} format."
4664 "When the @option{--inquire} option is passed then all remaining non-option "
4665 "arguments are retrieved via a server @emph{INQUIRE}."
4667 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4668 "expression syntax."
4671 new_command("XPATHATTR", 0, 1, 0, xpathattr_command
, _(
4672 "XPATHATTR [--inquire] SET|DELETE <name> <expression>[<TAB>[<value>]]\n"
4673 "Like the @code{XPATH} command (@pxref{XPATH}) but operates on element "
4674 "attributes and does not return a result. For the @var{SET} operation the "
4675 "@var{value} is optional but the field is required. If not specified then "
4676 "the attribute value will be empty. For example:"
4679 "XPATHATTR SET password //element[@@_name='password']@key{TAB}\n"
4682 "would create a @var{password} attribute for each @var{password} element "
4683 "found in the document. The attribute value will be empty but still exist."
4685 "When the @option{--inquire} option is passed then all remaining non-option "
4686 "arguments are retrieved via a server @emph{INQUIRE}."
4688 "See @url{http://www.w3schools.com/xpath/xpath_syntax.asp} for @abbr{XPATH} "
4689 "expression syntax."
4692 new_command("IMPORT", 0, 1, 0, import_command
, _(
4693 "IMPORT [--root=element[<TAB>child[..]]]\n"
4694 "This command uses a server @emph{INQUIRE} to retrieve data from the client."
4696 "Like the @code{STORE} command (@pxref{STORE}), but the @var{content} "
4697 "argument is raw @abbr{XML} data. The content is created as a child of "
4698 "the element path specified with the @option{--root} option or at the "
4699 "document root when not specified. Existing elements of the same name will "
4702 "The content must begin with an @abbr{XML} element node. @xref{Introduction}, "
4706 new_command("DUMP", 0, 1, 0, dump_command
, _(
4708 "Shows the in memory @abbr{XML} document with indenting. @xref{XPATH}, for "
4709 "dumping a specific node."
4712 new_command("LOCK", 0, 0, 0, lock_command
, _(
4714 "Locks the mutex associated with the opened file. This prevents other clients "
4715 "from sending commands to the same opened file until the client "
4716 "that sent this command either disconnects or sends the @code{UNLOCK} "
4717 "command. @xref{UNLOCK}."
4720 new_command("UNLOCK", 1, 0, 0, unlock_command
, _(
4722 "Unlocks the file mutex which was locked with the @code{LOCK} command or "
4723 "a commands' @option{--lock} option (@pxref{LOCK}, @pxref{OPEN}, "
4724 "@pxref{ISCACHED})."
4727 new_command("GETCONFIG", 1, 1, 0, getconfig_command
, _(
4728 "GETCONFIG [filename] <parameter>\n"
4729 "Returns the value of a @command{pwmd} configuration @var{parameter} with a "
4730 "data response. If no file has been opened then the value for @var{filename} "
4731 "or the default from the @var{global} section will be returned. If a file "
4732 "has been opened and no @var{filename} is specified, the value previously "
4733 "set with the @code{OPTION} command (@pxref{OPTION}) will be returned."
4736 new_command("OPTION", 1, 1, 0, option_command
, _(
4737 "OPTION <NAME>=[<VALUE>]\n"
4738 "Sets a client option @var{name} to @var{value}. The value for an option is "
4739 "kept for the duration of the connection with the exception of the "
4740 "@command{pinentry} options which are defaults for all future connections "
4741 "(@pxref{Pinentry}). When @var{value} is empty the option is unset."
4744 "@item DISABLE-PINENTRY\n"
4745 "Disable use of @command{pinentry} for passphrase retrieval. When @code{1}, a "
4746 "server inquire is sent to the client to obtain the passphrase. This option "
4747 "may be set as needed before the @code{OPEN} (@pxref{OPEN}), @code{PASSWD} "
4748 "(@pxref{PASSWD}) and @code{SAVE} (@pxref{SAVE}) commands. Set to @code{0} "
4749 "to use a @command{pinentry}."
4752 "Set or unset the X11 display to use when prompting for a passphrase."
4755 "Set the terminal device path to use when prompting for a passphrase."
4758 "Set the terminal type for use with @option{TTYNAME}."
4761 "Associates the thread ID of the connection with the specified textual "
4762 "representation. Useful for debugging log messages. May not contain whitespace."
4764 "@item LOCK-TIMEOUT\n"
4765 "When not @code{0}, the duration in tenths of a second to wait for the file "
4766 "mutex which has been locked by another thread to be released before returning "
4767 "an error. When @code{-1} the error will be returned immediately."
4771 new_command("LS", 1, 1, 0, ls_command
, _(
4773 "Returns a newline separated list of data files stored in the data directory "
4774 "@file{HOMEDIR/data} (@pxref{Invoking}) with a data response."
4777 new_command("RESET", 1, 1, 0, NULL
, _(
4779 "Closes the currently opened file but keeps any previously set client options "
4783 new_command("NOP", 1, 1, 0, NULL
, _(
4785 "Does nothing. Always returns successfully."
4788 /* !END-HELP-TEXT! */
4789 new_command ("CANCEL", 1, 1, 0, NULL
, NULL
);
4790 new_command ("END", 1, 1, 0, NULL
, NULL
);
4791 new_command ("BYE", 1, 1, 0, NULL
, NULL
);
4794 for (i
= 0; command_table
[i
]; i
++);
4795 qsort (command_table
, i
- 1, sizeof (struct command_table_s
*),
4800 register_commands (assuan_context_t ctx
)
4804 for (; command_table
[i
]; i
++)
4806 if (!command_table
[i
]->handler
)
4809 rc
= assuan_register_command (ctx
, command_table
[i
]->name
,
4810 command_table
[i
]->handler
,
4811 command_table
[i
]->help
);
4816 rc
= assuan_register_bye_notify (ctx
, bye_notify
);
4820 rc
= assuan_register_reset_notify (ctx
, reset_notify
);
4824 rc
= assuan_register_pre_cmd_notify (ctx
, command_startup
);
4828 return assuan_register_post_cmd_notify (ctx
, command_finalize
);